blob: 9e97d3172a249508d577fd0831bd14405d3a0e2a [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Arne Jansenbed92ea2012-06-28 18:03:02 +02002/*
3 * Copyright (C) 2011 STRATO. All rights reserved.
Arne Jansenbed92ea2012-06-28 18:03:02 +02004 */
5
6#include <linux/sched.h>
7#include <linux/pagemap.h>
8#include <linux/writeback.h>
9#include <linux/blkdev.h>
10#include <linux/rbtree.h>
11#include <linux/slab.h>
12#include <linux/workqueue.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000013#include <linux/btrfs.h>
Filipe Manana7aa6d352020-11-23 18:30:54 +000014#include <linux/sched/mm.h>
Arne Jansenbed92ea2012-06-28 18:03:02 +020015
16#include "ctree.h"
17#include "transaction.h"
18#include "disk-io.h"
19#include "locking.h"
20#include "ulist.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020021#include "backref.h"
Jan Schmidt2f232032013-04-25 16:04:51 +000022#include "extent_io.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070023#include "qgroup.h"
Josef Bacikaac00232019-06-20 15:37:44 -040024#include "block-group.h"
Qu Wenruo49e5fb42020-06-28 13:07:15 +080025#include "sysfs.h"
Qu Wenruoe69bcee2015-04-17 10:23:16 +080026
Arne Jansenbed92ea2012-06-28 18:03:02 +020027/* TODO XXX FIXME
28 * - subvol delete -> delete when ref goes to 0? delete limits also?
29 * - reorganize keys
30 * - compressed
31 * - sync
Arne Jansenbed92ea2012-06-28 18:03:02 +020032 * - copy also limits on subvol creation
33 * - limit
Andrea Gelmini52042d82018-11-28 12:05:13 +010034 * - caches for ulists
Arne Jansenbed92ea2012-06-28 18:03:02 +020035 * - performance benchmarks
36 * - check all ioctl parameters
37 */
38
Qu Wenruof59c0342017-12-12 15:34:24 +080039/*
40 * Helpers to access qgroup reservation
41 *
42 * Callers should ensure the lock context and type are valid
43 */
44
45static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
46{
47 u64 ret = 0;
48 int i;
49
50 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
51 ret += qgroup->rsv.values[i];
52
53 return ret;
54}
55
56#ifdef CONFIG_BTRFS_DEBUG
57static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
58{
59 if (type == BTRFS_QGROUP_RSV_DATA)
60 return "data";
Qu Wenruo733e03a2017-12-12 15:34:29 +080061 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
62 return "meta_pertrans";
63 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
64 return "meta_prealloc";
Qu Wenruof59c0342017-12-12 15:34:24 +080065 return NULL;
66}
67#endif
68
Qu Wenruo64ee4e72017-12-12 15:34:27 +080069static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
70 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080071 enum btrfs_qgroup_rsv_type type)
72{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080073 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080074 qgroup->rsv.values[type] += num_bytes;
75}
76
Qu Wenruo64ee4e72017-12-12 15:34:27 +080077static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
78 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080079 enum btrfs_qgroup_rsv_type type)
80{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080081 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080082 if (qgroup->rsv.values[type] >= num_bytes) {
83 qgroup->rsv.values[type] -= num_bytes;
84 return;
85 }
86#ifdef CONFIG_BTRFS_DEBUG
87 WARN_RATELIMIT(1,
88 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
89 qgroup->qgroupid, qgroup_rsv_type_str(type),
90 qgroup->rsv.values[type], num_bytes);
91#endif
92 qgroup->rsv.values[type] = 0;
93}
94
Qu Wenruo64ee4e72017-12-12 15:34:27 +080095static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
96 struct btrfs_qgroup *dest,
97 struct btrfs_qgroup *src)
Qu Wenruof59c0342017-12-12 15:34:24 +080098{
99 int i;
100
101 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800102 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800103}
104
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800105static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
106 struct btrfs_qgroup *dest,
Qu Wenruof59c0342017-12-12 15:34:24 +0800107 struct btrfs_qgroup *src)
108{
109 int i;
110
111 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800112 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800113}
114
Qu Wenruo9c542132015-03-12 16:10:13 +0800115static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
116 int mod)
117{
118 if (qg->old_refcnt < seq)
119 qg->old_refcnt = seq;
120 qg->old_refcnt += mod;
121}
122
123static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
124 int mod)
125{
126 if (qg->new_refcnt < seq)
127 qg->new_refcnt = seq;
128 qg->new_refcnt += mod;
129}
130
131static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
132{
133 if (qg->old_refcnt < seq)
134 return 0;
135 return qg->old_refcnt - seq;
136}
137
138static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
139{
140 if (qg->new_refcnt < seq)
141 return 0;
142 return qg->new_refcnt - seq;
143}
144
Arne Jansenbed92ea2012-06-28 18:03:02 +0200145/*
146 * glue structure to represent the relations between qgroups.
147 */
148struct btrfs_qgroup_list {
149 struct list_head next_group;
150 struct list_head next_member;
151 struct btrfs_qgroup *group;
152 struct btrfs_qgroup *member;
153};
154
David Sterbaef2fff62016-10-26 16:23:50 +0200155static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
156{
157 return (u64)(uintptr_t)qg;
158}
159
160static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
161{
162 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
163}
Josef Bacikfcebe452014-05-13 17:30:47 -0700164
Jan Schmidtb382a322013-05-28 15:47:24 +0000165static int
166qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
167 int init_flags);
168static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000169
Wang Shilong58400fc2013-04-07 10:50:17 +0000170/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200171static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
172 u64 qgroupid)
173{
174 struct rb_node *n = fs_info->qgroup_tree.rb_node;
175 struct btrfs_qgroup *qgroup;
176
177 while (n) {
178 qgroup = rb_entry(n, struct btrfs_qgroup, node);
179 if (qgroup->qgroupid < qgroupid)
180 n = n->rb_left;
181 else if (qgroup->qgroupid > qgroupid)
182 n = n->rb_right;
183 else
184 return qgroup;
185 }
186 return NULL;
187}
188
189/* must be called with qgroup_lock held */
190static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
191 u64 qgroupid)
192{
193 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
194 struct rb_node *parent = NULL;
195 struct btrfs_qgroup *qgroup;
196
197 while (*p) {
198 parent = *p;
199 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
200
201 if (qgroup->qgroupid < qgroupid)
202 p = &(*p)->rb_left;
203 else if (qgroup->qgroupid > qgroupid)
204 p = &(*p)->rb_right;
205 else
206 return qgroup;
207 }
208
209 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
210 if (!qgroup)
211 return ERR_PTR(-ENOMEM);
212
213 qgroup->qgroupid = qgroupid;
214 INIT_LIST_HEAD(&qgroup->groups);
215 INIT_LIST_HEAD(&qgroup->members);
216 INIT_LIST_HEAD(&qgroup->dirty);
217
218 rb_link_node(&qgroup->node, parent, p);
219 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
220
221 return qgroup;
222}
223
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800224static void __del_qgroup_rb(struct btrfs_fs_info *fs_info,
225 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200226{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200227 struct btrfs_qgroup_list *list;
228
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800229 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200230 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200231 while (!list_empty(&qgroup->groups)) {
232 list = list_first_entry(&qgroup->groups,
233 struct btrfs_qgroup_list, next_group);
234 list_del(&list->next_group);
235 list_del(&list->next_member);
236 kfree(list);
237 }
238
239 while (!list_empty(&qgroup->members)) {
240 list = list_first_entry(&qgroup->members,
241 struct btrfs_qgroup_list, next_member);
242 list_del(&list->next_group);
243 list_del(&list->next_member);
244 kfree(list);
245 }
246 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800247}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200248
Wang Shilong4082bd32013-08-14 09:13:36 +0800249/* must be called with qgroup_lock held */
250static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
251{
252 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
253
254 if (!qgroup)
255 return -ENOENT;
256
257 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800258 __del_qgroup_rb(fs_info, qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200259 return 0;
260}
261
262/* must be called with qgroup_lock held */
263static int add_relation_rb(struct btrfs_fs_info *fs_info,
264 u64 memberid, u64 parentid)
265{
266 struct btrfs_qgroup *member;
267 struct btrfs_qgroup *parent;
268 struct btrfs_qgroup_list *list;
269
270 member = find_qgroup_rb(fs_info, memberid);
271 parent = find_qgroup_rb(fs_info, parentid);
272 if (!member || !parent)
273 return -ENOENT;
274
275 list = kzalloc(sizeof(*list), GFP_ATOMIC);
276 if (!list)
277 return -ENOMEM;
278
279 list->group = parent;
280 list->member = member;
281 list_add_tail(&list->next_group, &member->groups);
282 list_add_tail(&list->next_member, &parent->members);
283
284 return 0;
285}
286
287/* must be called with qgroup_lock held */
288static int del_relation_rb(struct btrfs_fs_info *fs_info,
289 u64 memberid, u64 parentid)
290{
291 struct btrfs_qgroup *member;
292 struct btrfs_qgroup *parent;
293 struct btrfs_qgroup_list *list;
294
295 member = find_qgroup_rb(fs_info, memberid);
296 parent = find_qgroup_rb(fs_info, parentid);
297 if (!member || !parent)
298 return -ENOENT;
299
300 list_for_each_entry(list, &member->groups, next_group) {
301 if (list->group == parent) {
302 list_del(&list->next_group);
303 list_del(&list->next_member);
304 kfree(list);
305 return 0;
306 }
307 }
308 return -ENOENT;
309}
310
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400311#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
312int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
313 u64 rfer, u64 excl)
314{
315 struct btrfs_qgroup *qgroup;
316
317 qgroup = find_qgroup_rb(fs_info, qgroupid);
318 if (!qgroup)
319 return -EINVAL;
320 if (qgroup->rfer != rfer || qgroup->excl != excl)
321 return -EINVAL;
322 return 0;
323}
324#endif
325
Arne Jansenbed92ea2012-06-28 18:03:02 +0200326/*
327 * The full config is read in one go, only called from open_ctree()
328 * It doesn't use any locking, as at this point we're still single-threaded
329 */
330int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
331{
332 struct btrfs_key key;
333 struct btrfs_key found_key;
334 struct btrfs_root *quota_root = fs_info->quota_root;
335 struct btrfs_path *path = NULL;
336 struct extent_buffer *l;
337 int slot;
338 int ret = 0;
339 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000340 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200341
Josef Bacikafcdd122016-09-02 15:40:02 -0400342 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +0200343 return 0;
344
David Sterba323b88f2017-02-13 12:10:20 +0100345 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000346 if (!fs_info->qgroup_ulist) {
347 ret = -ENOMEM;
348 goto out;
349 }
350
Arne Jansenbed92ea2012-06-28 18:03:02 +0200351 path = btrfs_alloc_path();
352 if (!path) {
353 ret = -ENOMEM;
354 goto out;
355 }
356
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800357 ret = btrfs_sysfs_add_qgroups(fs_info);
358 if (ret < 0)
359 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200360 /* default this to quota off, in case no status key is found */
361 fs_info->qgroup_flags = 0;
362
363 /*
364 * pass 1: read status, all qgroup infos and limits
365 */
366 key.objectid = 0;
367 key.type = 0;
368 key.offset = 0;
369 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
370 if (ret)
371 goto out;
372
373 while (1) {
374 struct btrfs_qgroup *qgroup;
375
376 slot = path->slots[0];
377 l = path->nodes[0];
378 btrfs_item_key_to_cpu(l, &found_key, slot);
379
380 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
381 struct btrfs_qgroup_status_item *ptr;
382
383 ptr = btrfs_item_ptr(l, slot,
384 struct btrfs_qgroup_status_item);
385
386 if (btrfs_qgroup_status_version(l, ptr) !=
387 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500388 btrfs_err(fs_info,
389 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200390 goto out;
391 }
392 if (btrfs_qgroup_status_generation(l, ptr) !=
393 fs_info->generation) {
394 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500395 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400396 "qgroup generation mismatch, marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200397 }
398 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
399 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000400 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200401 goto next1;
402 }
403
404 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
405 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
406 goto next1;
407
408 qgroup = find_qgroup_rb(fs_info, found_key.offset);
409 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
410 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Geert Uytterhoevend41e36a2015-07-06 15:38:11 +0200411 btrfs_err(fs_info, "inconsistent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200412 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
413 }
414 if (!qgroup) {
415 qgroup = add_qgroup_rb(fs_info, found_key.offset);
416 if (IS_ERR(qgroup)) {
417 ret = PTR_ERR(qgroup);
418 goto out;
419 }
420 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800421 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
422 if (ret < 0)
423 goto out;
424
Arne Jansenbed92ea2012-06-28 18:03:02 +0200425 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:
Filipe Manana3d05cad2020-11-23 14:28:44 +0000501 btrfs_free_path(path);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200502 fs_info->qgroup_flags |= flags;
Josef Bacikafcdd122016-09-02 15:40:02 -0400503 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
504 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
505 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
506 ret >= 0)
Jan Schmidtb382a322013-05-28 15:47:24 +0000507 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200508
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;
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800513 btrfs_sysfs_del_qgroups(fs_info);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000514 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000515
Arne Jansenbed92ea2012-06-28 18:03:02 +0200516 return ret < 0 ? ret : 0;
517}
518
Qu Wenruo59582532020-06-10 09:04:44 +0800519/*
520 * Called in close_ctree() when quota is still enabled. This verifies we don't
521 * leak some reserved space.
522 *
523 * Return false if no reserved space is left.
524 * Return true if some reserved space is leaked.
525 */
526bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info)
527{
528 struct rb_node *node;
529 bool ret = false;
530
531 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
532 return ret;
533 /*
534 * Since we're unmounting, there is no race and no need to grab qgroup
535 * lock. And here we don't go post-order to provide a more user
536 * friendly sorted result.
537 */
538 for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
539 struct btrfs_qgroup *qgroup;
540 int i;
541
542 qgroup = rb_entry(node, struct btrfs_qgroup, node);
543 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
544 if (qgroup->rsv.values[i]) {
545 ret = true;
546 btrfs_warn(fs_info,
Qu Wenruo06f67c42020-06-28 13:07:14 +0800547 "qgroup %hu/%llu has unreleased space, type %d rsv %llu",
Qu Wenruo59582532020-06-10 09:04:44 +0800548 btrfs_qgroup_level(qgroup->qgroupid),
549 btrfs_qgroup_subvolid(qgroup->qgroupid),
550 i, qgroup->rsv.values[i]);
551 }
552 }
553 }
554 return ret;
555}
556
Arne Jansenbed92ea2012-06-28 18:03:02 +0200557/*
Wang Shilonge685da12013-08-14 09:13:37 +0800558 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
559 * first two are in single-threaded paths.And for the third one, we have set
560 * quota_root to be null with qgroup_lock held before, so it is safe to clean
561 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200562 */
563void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
564{
565 struct rb_node *n;
566 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200567
568 while ((n = rb_first(&fs_info->qgroup_tree))) {
569 qgroup = rb_entry(n, struct btrfs_qgroup, node);
570 rb_erase(n, &fs_info->qgroup_tree);
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800571 __del_qgroup_rb(fs_info, qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200572 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800573 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +0100574 * We call btrfs_free_qgroup_config() when unmounting
Nicholas D Steeves01327612016-05-19 21:18:45 -0400575 * filesystem and disabling quota, so we set qgroup_ulist
Wang Shilong1e7bac12013-07-13 21:02:54 +0800576 * to be null here to avoid double free.
577 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000578 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800579 fs_info->qgroup_ulist = NULL;
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800580 btrfs_sysfs_del_qgroups(fs_info);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200581}
582
Lu Fengqi711169c2018-07-18 14:45:24 +0800583static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
584 u64 dst)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200585{
586 int ret;
Lu Fengqi711169c2018-07-18 14:45:24 +0800587 struct btrfs_root *quota_root = trans->fs_info->quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200588 struct btrfs_path *path;
589 struct btrfs_key key;
590
591 path = btrfs_alloc_path();
592 if (!path)
593 return -ENOMEM;
594
595 key.objectid = src;
596 key.type = BTRFS_QGROUP_RELATION_KEY;
597 key.offset = dst;
598
599 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
600
601 btrfs_mark_buffer_dirty(path->nodes[0]);
602
603 btrfs_free_path(path);
604 return ret;
605}
606
Lu Fengqi99d7f092018-07-18 14:45:25 +0800607static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
608 u64 dst)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200609{
610 int ret;
Lu Fengqi99d7f092018-07-18 14:45:25 +0800611 struct btrfs_root *quota_root = trans->fs_info->quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200612 struct btrfs_path *path;
613 struct btrfs_key key;
614
615 path = btrfs_alloc_path();
616 if (!path)
617 return -ENOMEM;
618
619 key.objectid = src;
620 key.type = BTRFS_QGROUP_RELATION_KEY;
621 key.offset = dst;
622
623 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
624 if (ret < 0)
625 goto out;
626
627 if (ret > 0) {
628 ret = -ENOENT;
629 goto out;
630 }
631
632 ret = btrfs_del_item(trans, quota_root, path);
633out:
634 btrfs_free_path(path);
635 return ret;
636}
637
638static int add_qgroup_item(struct btrfs_trans_handle *trans,
639 struct btrfs_root *quota_root, u64 qgroupid)
640{
641 int ret;
642 struct btrfs_path *path;
643 struct btrfs_qgroup_info_item *qgroup_info;
644 struct btrfs_qgroup_limit_item *qgroup_limit;
645 struct extent_buffer *leaf;
646 struct btrfs_key key;
647
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400648 if (btrfs_is_testing(quota_root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400649 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200650
Arne Jansenbed92ea2012-06-28 18:03:02 +0200651 path = btrfs_alloc_path();
652 if (!path)
653 return -ENOMEM;
654
655 key.objectid = 0;
656 key.type = BTRFS_QGROUP_INFO_KEY;
657 key.offset = qgroupid;
658
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700659 /*
660 * Avoid a transaction abort by catching -EEXIST here. In that
661 * case, we proceed by re-initializing the existing structure
662 * on disk.
663 */
664
Arne Jansenbed92ea2012-06-28 18:03:02 +0200665 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
666 sizeof(*qgroup_info));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700667 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200668 goto out;
669
670 leaf = path->nodes[0];
671 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
672 struct btrfs_qgroup_info_item);
673 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
674 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
675 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
676 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
677 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
678
679 btrfs_mark_buffer_dirty(leaf);
680
681 btrfs_release_path(path);
682
683 key.type = BTRFS_QGROUP_LIMIT_KEY;
684 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
685 sizeof(*qgroup_limit));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700686 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200687 goto out;
688
689 leaf = path->nodes[0];
690 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
691 struct btrfs_qgroup_limit_item);
692 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
693 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
694 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
695 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
696 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
697
698 btrfs_mark_buffer_dirty(leaf);
699
700 ret = 0;
701out:
702 btrfs_free_path(path);
703 return ret;
704}
705
Lu Fengqi69104612018-07-18 14:45:26 +0800706static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200707{
708 int ret;
Lu Fengqi69104612018-07-18 14:45:26 +0800709 struct btrfs_root *quota_root = trans->fs_info->quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200710 struct btrfs_path *path;
711 struct btrfs_key key;
712
713 path = btrfs_alloc_path();
714 if (!path)
715 return -ENOMEM;
716
717 key.objectid = 0;
718 key.type = BTRFS_QGROUP_INFO_KEY;
719 key.offset = qgroupid;
720 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
721 if (ret < 0)
722 goto out;
723
724 if (ret > 0) {
725 ret = -ENOENT;
726 goto out;
727 }
728
729 ret = btrfs_del_item(trans, quota_root, path);
730 if (ret)
731 goto out;
732
733 btrfs_release_path(path);
734
735 key.type = BTRFS_QGROUP_LIMIT_KEY;
736 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
737 if (ret < 0)
738 goto out;
739
740 if (ret > 0) {
741 ret = -ENOENT;
742 goto out;
743 }
744
745 ret = btrfs_del_item(trans, quota_root, path);
746
747out:
748 btrfs_free_path(path);
749 return ret;
750}
751
752static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
Dongsheng Yang1510e712014-11-20 21:01:41 -0500753 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200754{
Lu Fengqiac8a8662018-07-18 14:45:27 +0800755 struct btrfs_root *quota_root = trans->fs_info->quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200756 struct btrfs_path *path;
757 struct btrfs_key key;
758 struct extent_buffer *l;
759 struct btrfs_qgroup_limit_item *qgroup_limit;
760 int ret;
761 int slot;
762
763 key.objectid = 0;
764 key.type = BTRFS_QGROUP_LIMIT_KEY;
Dongsheng Yang1510e712014-11-20 21:01:41 -0500765 key.offset = qgroup->qgroupid;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200766
767 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000768 if (!path)
769 return -ENOMEM;
770
Lu Fengqiac8a8662018-07-18 14:45:27 +0800771 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200772 if (ret > 0)
773 ret = -ENOENT;
774
775 if (ret)
776 goto out;
777
778 l = path->nodes[0];
779 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100780 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Dongsheng Yang1510e712014-11-20 21:01:41 -0500781 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
782 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
783 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
784 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
785 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200786
787 btrfs_mark_buffer_dirty(l);
788
789out:
790 btrfs_free_path(path);
791 return ret;
792}
793
794static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +0200795 struct btrfs_qgroup *qgroup)
796{
Lu Fengqi3e07e9a2018-07-18 14:45:28 +0800797 struct btrfs_fs_info *fs_info = trans->fs_info;
798 struct btrfs_root *quota_root = fs_info->quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200799 struct btrfs_path *path;
800 struct btrfs_key key;
801 struct extent_buffer *l;
802 struct btrfs_qgroup_info_item *qgroup_info;
803 int ret;
804 int slot;
805
Lu Fengqi3e07e9a2018-07-18 14:45:28 +0800806 if (btrfs_is_testing(fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400807 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200808
Arne Jansenbed92ea2012-06-28 18:03:02 +0200809 key.objectid = 0;
810 key.type = BTRFS_QGROUP_INFO_KEY;
811 key.offset = qgroup->qgroupid;
812
813 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000814 if (!path)
815 return -ENOMEM;
816
Lu Fengqi3e07e9a2018-07-18 14:45:28 +0800817 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200818 if (ret > 0)
819 ret = -ENOENT;
820
821 if (ret)
822 goto out;
823
824 l = path->nodes[0];
825 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100826 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200827 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
828 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
829 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
830 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
831 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
832
833 btrfs_mark_buffer_dirty(l);
834
835out:
836 btrfs_free_path(path);
837 return ret;
838}
839
Lu Fengqi2e980ac2018-07-18 14:45:29 +0800840static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200841{
Lu Fengqi2e980ac2018-07-18 14:45:29 +0800842 struct btrfs_fs_info *fs_info = trans->fs_info;
843 struct btrfs_root *quota_root = fs_info->quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200844 struct btrfs_path *path;
845 struct btrfs_key key;
846 struct extent_buffer *l;
847 struct btrfs_qgroup_status_item *ptr;
848 int ret;
849 int slot;
850
851 key.objectid = 0;
852 key.type = BTRFS_QGROUP_STATUS_KEY;
853 key.offset = 0;
854
855 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000856 if (!path)
857 return -ENOMEM;
858
Lu Fengqi2e980ac2018-07-18 14:45:29 +0800859 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200860 if (ret > 0)
861 ret = -ENOENT;
862
863 if (ret)
864 goto out;
865
866 l = path->nodes[0];
867 slot = path->slots[0];
868 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
869 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
870 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000871 btrfs_set_qgroup_status_rescan(l, ptr,
872 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200873
874 btrfs_mark_buffer_dirty(l);
875
876out:
877 btrfs_free_path(path);
878 return ret;
879}
880
881/*
882 * called with qgroup_lock held
883 */
884static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root)
886{
887 struct btrfs_path *path;
888 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000889 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200890 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000891 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200892
Arne Jansenbed92ea2012-06-28 18:03:02 +0200893 path = btrfs_alloc_path();
894 if (!path)
895 return -ENOMEM;
896
Wang Shilong06b3a862013-02-27 11:16:57 +0000897 path->leave_spinning = 1;
898
899 key.objectid = 0;
900 key.offset = 0;
901 key.type = 0;
902
Arne Jansenbed92ea2012-06-28 18:03:02 +0200903 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200904 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000905 if (ret < 0)
906 goto out;
907 leaf = path->nodes[0];
908 nr = btrfs_header_nritems(leaf);
909 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200910 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000911 /*
912 * delete the leaf one by one
913 * since the whole tree is going
914 * to be deleted.
915 */
916 path->slots[0] = 0;
917 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200918 if (ret)
919 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000920
Arne Jansenbed92ea2012-06-28 18:03:02 +0200921 btrfs_release_path(path);
922 }
923 ret = 0;
924out:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200925 btrfs_free_path(path);
926 return ret;
927}
928
Nikolay Borisov340f1aa2018-07-05 14:50:48 +0300929int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200930{
931 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000932 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200933 struct btrfs_path *path = NULL;
934 struct btrfs_qgroup_status_item *ptr;
935 struct extent_buffer *leaf;
936 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000937 struct btrfs_key found_key;
938 struct btrfs_qgroup *qgroup = NULL;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +0300939 struct btrfs_trans_handle *trans = NULL;
Filipe Mananaa855fbe2020-11-23 18:31:02 +0000940 struct ulist *ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200941 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000942 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200943
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000944 mutex_lock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +0200945 if (fs_info->quota_root)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200946 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200947
Filipe Mananaa855fbe2020-11-23 18:31:02 +0000948 ulist = ulist_alloc(GFP_KERNEL);
949 if (!ulist) {
David Sterba7503b832018-12-19 19:47:37 +0100950 ret = -ENOMEM;
951 goto out;
952 }
953
Qu Wenruo49e5fb42020-06-28 13:07:15 +0800954 ret = btrfs_sysfs_add_qgroups(fs_info);
955 if (ret < 0)
956 goto out;
Filipe Mananaa855fbe2020-11-23 18:31:02 +0000957
958 /*
959 * Unlock qgroup_ioctl_lock before starting the transaction. This is to
960 * avoid lock acquisition inversion problems (reported by lockdep) between
961 * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
962 * start a transaction.
963 * After we started the transaction lock qgroup_ioctl_lock again and
964 * check if someone else created the quota root in the meanwhile. If so,
965 * just return success and release the transaction handle.
966 *
967 * Also we don't need to worry about someone else calling
968 * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
969 * that function returns 0 (success) when the sysfs entries already exist.
970 */
971 mutex_unlock(&fs_info->qgroup_ioctl_lock);
972
Nikolay Borisov340f1aa2018-07-05 14:50:48 +0300973 /*
974 * 1 for quota root item
975 * 1 for BTRFS_QGROUP_STATUS item
976 *
977 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
978 * per subvolume. However those are not currently reserved since it
979 * would be a lot of overkill.
980 */
981 trans = btrfs_start_transaction(tree_root, 2);
Filipe Mananaa855fbe2020-11-23 18:31:02 +0000982
983 mutex_lock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +0300984 if (IS_ERR(trans)) {
985 ret = PTR_ERR(trans);
986 trans = NULL;
987 goto out;
988 }
989
Filipe Mananaa855fbe2020-11-23 18:31:02 +0000990 if (fs_info->quota_root)
991 goto out;
992
993 fs_info->qgroup_ulist = ulist;
994 ulist = NULL;
995
Arne Jansenbed92ea2012-06-28 18:03:02 +0200996 /*
997 * initially create the quota tree
998 */
David Sterba9b7a2442019-03-20 13:20:49 +0100999 quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001000 if (IS_ERR(quota_root)) {
1001 ret = PTR_ERR(quota_root);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001002 btrfs_abort_transaction(trans, ret);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001003 goto out;
1004 }
1005
1006 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001007 if (!path) {
1008 ret = -ENOMEM;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001009 btrfs_abort_transaction(trans, ret);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001010 goto out_free_root;
1011 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001012
1013 key.objectid = 0;
1014 key.type = BTRFS_QGROUP_STATUS_KEY;
1015 key.offset = 0;
1016
1017 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
1018 sizeof(*ptr));
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001019 if (ret) {
1020 btrfs_abort_transaction(trans, ret);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001021 goto out_free_path;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001022 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001023
1024 leaf = path->nodes[0];
1025 ptr = btrfs_item_ptr(leaf, path->slots[0],
1026 struct btrfs_qgroup_status_item);
1027 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
1028 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
1029 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
1030 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1031 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +00001032 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001033
1034 btrfs_mark_buffer_dirty(leaf);
1035
Wang Shilong7708f022013-04-07 10:24:57 +00001036 key.objectid = 0;
1037 key.type = BTRFS_ROOT_REF_KEY;
1038 key.offset = 0;
1039
1040 btrfs_release_path(path);
1041 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
1042 if (ret > 0)
1043 goto out_add_root;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001044 if (ret < 0) {
1045 btrfs_abort_transaction(trans, ret);
Wang Shilong7708f022013-04-07 10:24:57 +00001046 goto out_free_path;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001047 }
Wang Shilong7708f022013-04-07 10:24:57 +00001048
1049 while (1) {
1050 slot = path->slots[0];
1051 leaf = path->nodes[0];
1052 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1053
1054 if (found_key.type == BTRFS_ROOT_REF_KEY) {
Josef Bacik5223cc62020-10-19 16:02:29 -04001055
1056 /* Release locks on tree_root before we access quota_root */
1057 btrfs_release_path(path);
1058
Wang Shilong7708f022013-04-07 10:24:57 +00001059 ret = add_qgroup_item(trans, quota_root,
1060 found_key.offset);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001061 if (ret) {
1062 btrfs_abort_transaction(trans, ret);
Wang Shilong7708f022013-04-07 10:24:57 +00001063 goto out_free_path;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001064 }
Wang Shilong7708f022013-04-07 10:24:57 +00001065
Wang Shilong7708f022013-04-07 10:24:57 +00001066 qgroup = add_qgroup_rb(fs_info, found_key.offset);
1067 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +00001068 ret = PTR_ERR(qgroup);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001069 btrfs_abort_transaction(trans, ret);
Wang Shilong7708f022013-04-07 10:24:57 +00001070 goto out_free_path;
1071 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001072 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1073 if (ret < 0) {
1074 btrfs_abort_transaction(trans, ret);
1075 goto out_free_path;
1076 }
Josef Bacik5223cc62020-10-19 16:02:29 -04001077 ret = btrfs_search_slot_for_read(tree_root, &found_key,
1078 path, 1, 0);
1079 if (ret < 0) {
1080 btrfs_abort_transaction(trans, ret);
1081 goto out_free_path;
1082 }
1083 if (ret > 0) {
1084 /*
1085 * Shouldn't happen, but in case it does we
1086 * don't need to do the btrfs_next_item, just
1087 * continue.
1088 */
1089 continue;
1090 }
Wang Shilong7708f022013-04-07 10:24:57 +00001091 }
1092 ret = btrfs_next_item(tree_root, path);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001093 if (ret < 0) {
1094 btrfs_abort_transaction(trans, ret);
Wang Shilong7708f022013-04-07 10:24:57 +00001095 goto out_free_path;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001096 }
Wang Shilong7708f022013-04-07 10:24:57 +00001097 if (ret)
1098 break;
1099 }
1100
1101out_add_root:
1102 btrfs_release_path(path);
1103 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001104 if (ret) {
1105 btrfs_abort_transaction(trans, ret);
Wang Shilong7708f022013-04-07 10:24:57 +00001106 goto out_free_path;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001107 }
Wang Shilong7708f022013-04-07 10:24:57 +00001108
Wang Shilong7708f022013-04-07 10:24:57 +00001109 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
1110 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +00001111 ret = PTR_ERR(qgroup);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001112 btrfs_abort_transaction(trans, ret);
Wang Shilong7708f022013-04-07 10:24:57 +00001113 goto out_free_path;
1114 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001115 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1116 if (ret < 0) {
1117 btrfs_abort_transaction(trans, ret);
1118 goto out_free_path;
1119 }
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001120
1121 ret = btrfs_commit_transaction(trans);
Dan Carpenterb9b8a412018-08-20 11:25:33 +03001122 trans = NULL;
1123 if (ret)
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001124 goto out_free_path;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001125
Filipe Manana9a6f2092018-11-19 14:15:36 +00001126 /*
1127 * Set quota enabled flag after committing the transaction, to avoid
1128 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
1129 * creation.
1130 */
1131 spin_lock(&fs_info->qgroup_lock);
1132 fs_info->quota_root = quota_root;
1133 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1134 spin_unlock(&fs_info->qgroup_lock);
1135
Nikolay Borisov5d235152018-01-31 10:52:04 +02001136 ret = qgroup_rescan_init(fs_info, 0, 1);
1137 if (!ret) {
1138 qgroup_rescan_zero_tracking(fs_info);
Qu Wenruod61acbb2020-02-07 13:38:20 +08001139 fs_info->qgroup_rescan_running = true;
Nikolay Borisov5d235152018-01-31 10:52:04 +02001140 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1141 &fs_info->qgroup_rescan_work);
1142 }
1143
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001144out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001145 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001146out_free_root:
Josef Bacik8c389382020-02-14 16:11:42 -05001147 if (ret)
Josef Bacik00246522020-01-24 09:33:01 -05001148 btrfs_put_root(quota_root);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001149out:
Jan Schmidteb1716a2013-05-28 15:47:23 +00001150 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00001151 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +00001152 fs_info->qgroup_ulist = NULL;
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001153 btrfs_sysfs_del_qgroups(fs_info);
Jan Schmidteb1716a2013-05-28 15:47:23 +00001154 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001155 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001156 if (ret && trans)
1157 btrfs_end_transaction(trans);
1158 else if (trans)
1159 ret = btrfs_end_transaction(trans);
1160 ulist_free(ulist);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001161 return ret;
1162}
1163
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001164int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001165{
Arne Jansenbed92ea2012-06-28 18:03:02 +02001166 struct btrfs_root *quota_root;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001167 struct btrfs_trans_handle *trans = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001168 int ret = 0;
1169
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001170 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +00001171 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001172 goto out;
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001173 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001174
1175 /*
1176 * 1 For the root item
1177 *
1178 * We should also reserve enough items for the quota tree deletion in
1179 * btrfs_clean_quota_tree but this is not done.
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001180 *
1181 * Also, we must always start a transaction without holding the mutex
1182 * qgroup_ioctl_lock, see btrfs_quota_enable().
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001183 */
1184 trans = btrfs_start_transaction(fs_info->tree_root, 1);
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001185
1186 mutex_lock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001187 if (IS_ERR(trans)) {
1188 ret = PTR_ERR(trans);
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001189 trans = NULL;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001190 goto out;
1191 }
1192
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001193 if (!fs_info->quota_root)
1194 goto out;
1195
Josef Bacikafcdd122016-09-02 15:40:02 -04001196 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04001197 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -08001198 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001199 quota_root = fs_info->quota_root;
1200 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +08001201 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001202 spin_unlock(&fs_info->qgroup_lock);
1203
Wang Shilonge685da12013-08-14 09:13:37 +08001204 btrfs_free_qgroup_config(fs_info);
1205
Arne Jansenbed92ea2012-06-28 18:03:02 +02001206 ret = btrfs_clean_quota_tree(trans, quota_root);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001207 if (ret) {
1208 btrfs_abort_transaction(trans, ret);
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001209 goto out;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001210 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001211
Lu Fengqiab9ce7d2018-08-01 11:32:27 +08001212 ret = btrfs_del_root(trans, &quota_root->root_key);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001213 if (ret) {
1214 btrfs_abort_transaction(trans, ret);
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001215 goto out;
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001216 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001217
1218 list_del(&quota_root->dirty_list);
1219
1220 btrfs_tree_lock(quota_root->node);
David Sterba6a884d7d2019-03-20 14:30:02 +01001221 btrfs_clean_tree_block(quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001222 btrfs_tree_unlock(quota_root->node);
1223 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1224
Josef Bacik00246522020-01-24 09:33:01 -05001225 btrfs_put_root(quota_root);
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03001226
Arne Jansenbed92ea2012-06-28 18:03:02 +02001227out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001228 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Filipe Mananaa855fbe2020-11-23 18:31:02 +00001229 if (ret && trans)
1230 btrfs_end_transaction(trans);
1231 else if (trans)
1232 ret = btrfs_end_transaction(trans);
1233
Arne Jansenbed92ea2012-06-28 18:03:02 +02001234 return ret;
1235}
1236
Jan Schmidt2f232032013-04-25 16:04:51 +00001237static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1238 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001239{
Jan Schmidt2f232032013-04-25 16:04:51 +00001240 if (list_empty(&qgroup->dirty))
1241 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001242}
1243
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001244/*
Qu Wenruo429d6272017-12-12 15:34:26 +08001245 * The easy accounting, we're updating qgroup relationship whose child qgroup
1246 * only has exclusive extents.
1247 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01001248 * In this case, all exclusive extents will also be exclusive for parent, so
Qu Wenruo429d6272017-12-12 15:34:26 +08001249 * excl/rfer just get added/removed.
1250 *
1251 * So is qgroup reservation space, which should also be added/removed to
1252 * parent.
1253 * Or when child tries to release reservation space, parent will underflow its
1254 * reservation (for relationship adding case).
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001255 *
1256 * Caller should hold fs_info->qgroup_lock.
1257 */
1258static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1259 struct ulist *tmp, u64 ref_root,
Qu Wenruo429d6272017-12-12 15:34:26 +08001260 struct btrfs_qgroup *src, int sign)
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001261{
1262 struct btrfs_qgroup *qgroup;
1263 struct btrfs_qgroup_list *glist;
1264 struct ulist_node *unode;
1265 struct ulist_iterator uiter;
Qu Wenruo429d6272017-12-12 15:34:26 +08001266 u64 num_bytes = src->excl;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001267 int ret = 0;
1268
1269 qgroup = find_qgroup_rb(fs_info, ref_root);
1270 if (!qgroup)
1271 goto out;
1272
1273 qgroup->rfer += sign * num_bytes;
1274 qgroup->rfer_cmpr += sign * num_bytes;
1275
1276 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1277 qgroup->excl += sign * num_bytes;
1278 qgroup->excl_cmpr += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001279
1280 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001281 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001282 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001283 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001284
1285 qgroup_dirty(fs_info, qgroup);
1286
1287 /* Get all of the parent groups that contain this qgroup */
1288 list_for_each_entry(glist, &qgroup->groups, next_group) {
1289 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001290 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001291 if (ret < 0)
1292 goto out;
1293 }
1294
1295 /* Iterate all of the parents and adjust their reference counts */
1296 ULIST_ITER_INIT(&uiter);
1297 while ((unode = ulist_next(tmp, &uiter))) {
David Sterbaef2fff62016-10-26 16:23:50 +02001298 qgroup = unode_aux_to_qgroup(unode);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001299 qgroup->rfer += sign * num_bytes;
1300 qgroup->rfer_cmpr += sign * num_bytes;
1301 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1302 qgroup->excl += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001303 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001304 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001305 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001306 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001307 qgroup->excl_cmpr += sign * num_bytes;
1308 qgroup_dirty(fs_info, qgroup);
1309
1310 /* Add any parents of the parents */
1311 list_for_each_entry(glist, &qgroup->groups, next_group) {
1312 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001313 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001314 if (ret < 0)
1315 goto out;
1316 }
1317 }
1318 ret = 0;
1319out:
1320 return ret;
1321}
1322
1323
1324/*
1325 * Quick path for updating qgroup with only excl refs.
1326 *
1327 * In that case, just update all parent will be enough.
1328 * Or we needs to do a full rescan.
1329 * Caller should also hold fs_info->qgroup_lock.
1330 *
1331 * Return 0 for quick update, return >0 for need to full rescan
1332 * and mark INCONSISTENT flag.
1333 * Return < 0 for other error.
1334 */
1335static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1336 struct ulist *tmp, u64 src, u64 dst,
1337 int sign)
1338{
1339 struct btrfs_qgroup *qgroup;
1340 int ret = 1;
1341 int err = 0;
1342
1343 qgroup = find_qgroup_rb(fs_info, src);
1344 if (!qgroup)
1345 goto out;
1346 if (qgroup->excl == qgroup->rfer) {
1347 ret = 0;
1348 err = __qgroup_excl_accounting(fs_info, tmp, dst,
Qu Wenruo429d6272017-12-12 15:34:26 +08001349 qgroup, sign);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001350 if (err < 0) {
1351 ret = err;
1352 goto out;
1353 }
1354 }
1355out:
1356 if (ret)
1357 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1358 return ret;
1359}
1360
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08001361int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1362 u64 dst)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001363{
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08001364 struct btrfs_fs_info *fs_info = trans->fs_info;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001365 struct btrfs_qgroup *parent;
1366 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001367 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001368 struct ulist *tmp;
Filipe Manana7aa6d352020-11-23 18:30:54 +00001369 unsigned int nofs_flag;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001370 int ret = 0;
1371
Qu Wenruo8465ece2015-02-27 16:24:22 +08001372 /* Check the level of src and dst first */
1373 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1374 return -EINVAL;
1375
Filipe Manana7aa6d352020-11-23 18:30:54 +00001376 /* We hold a transaction handle open, must do a NOFS allocation. */
1377 nofs_flag = memalloc_nofs_save();
David Sterba6602caf2017-02-13 12:41:02 +01001378 tmp = ulist_alloc(GFP_KERNEL);
Filipe Manana7aa6d352020-11-23 18:30:54 +00001379 memalloc_nofs_restore(nofs_flag);
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001380 if (!tmp)
1381 return -ENOMEM;
1382
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001383 mutex_lock(&fs_info->qgroup_ioctl_lock);
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03001384 if (!fs_info->quota_root) {
Marcos Paulo de Souza8a36e402019-11-25 21:58:51 -03001385 ret = -ENOTCONN;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001386 goto out;
1387 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001388 member = find_qgroup_rb(fs_info, src);
1389 parent = find_qgroup_rb(fs_info, dst);
1390 if (!member || !parent) {
1391 ret = -EINVAL;
1392 goto out;
1393 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001394
Wang Shilong534e6622013-04-17 14:49:51 +00001395 /* check if such qgroup relation exist firstly */
1396 list_for_each_entry(list, &member->groups, next_group) {
1397 if (list->group == parent) {
1398 ret = -EEXIST;
1399 goto out;
1400 }
1401 }
1402
Lu Fengqi711169c2018-07-18 14:45:24 +08001403 ret = add_qgroup_relation_item(trans, src, dst);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001404 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001405 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001406
Lu Fengqi711169c2018-07-18 14:45:24 +08001407 ret = add_qgroup_relation_item(trans, dst, src);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001408 if (ret) {
Lu Fengqi99d7f092018-07-18 14:45:25 +08001409 del_qgroup_relation_item(trans, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001410 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001411 }
1412
1413 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001414 ret = add_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001415 if (ret < 0) {
1416 spin_unlock(&fs_info->qgroup_lock);
1417 goto out;
1418 }
1419 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001420 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001421out:
1422 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001423 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001424 return ret;
1425}
1426
Lu Fengqi6b36f1a2018-07-18 14:45:31 +08001427static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1428 u64 dst)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001429{
Lu Fengqi6b36f1a2018-07-18 14:45:31 +08001430 struct btrfs_fs_info *fs_info = trans->fs_info;
Wang Shilong534e6622013-04-17 14:49:51 +00001431 struct btrfs_qgroup *parent;
1432 struct btrfs_qgroup *member;
1433 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001434 struct ulist *tmp;
Qu Wenruo73798c42019-08-06 22:05:07 +08001435 bool found = false;
Filipe Manana7aa6d352020-11-23 18:30:54 +00001436 unsigned int nofs_flag;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001437 int ret = 0;
Qu Wenruo73798c42019-08-06 22:05:07 +08001438 int ret2;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001439
Filipe Manana7aa6d352020-11-23 18:30:54 +00001440 /* We hold a transaction handle open, must do a NOFS allocation. */
1441 nofs_flag = memalloc_nofs_save();
David Sterba6602caf2017-02-13 12:41:02 +01001442 tmp = ulist_alloc(GFP_KERNEL);
Filipe Manana7aa6d352020-11-23 18:30:54 +00001443 memalloc_nofs_restore(nofs_flag);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001444 if (!tmp)
1445 return -ENOMEM;
1446
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03001447 if (!fs_info->quota_root) {
Marcos Paulo de Souza8a36e402019-11-25 21:58:51 -03001448 ret = -ENOTCONN;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001449 goto out;
1450 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001451
Wang Shilong534e6622013-04-17 14:49:51 +00001452 member = find_qgroup_rb(fs_info, src);
1453 parent = find_qgroup_rb(fs_info, dst);
Qu Wenruo73798c42019-08-06 22:05:07 +08001454 /*
1455 * The parent/member pair doesn't exist, then try to delete the dead
1456 * relation items only.
1457 */
1458 if (!member || !parent)
1459 goto delete_item;
Wang Shilong534e6622013-04-17 14:49:51 +00001460
1461 /* check if such qgroup relation exist firstly */
1462 list_for_each_entry(list, &member->groups, next_group) {
Qu Wenruo73798c42019-08-06 22:05:07 +08001463 if (list->group == parent) {
1464 found = true;
1465 break;
1466 }
Wang Shilong534e6622013-04-17 14:49:51 +00001467 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001468
Qu Wenruo73798c42019-08-06 22:05:07 +08001469delete_item:
1470 ret = del_qgroup_relation_item(trans, src, dst);
1471 if (ret < 0 && ret != -ENOENT)
1472 goto out;
1473 ret2 = del_qgroup_relation_item(trans, dst, src);
1474 if (ret2 < 0 && ret2 != -ENOENT)
1475 goto out;
1476
1477 /* At least one deletion succeeded, return 0 */
1478 if (!ret || !ret2)
1479 ret = 0;
1480
1481 if (found) {
1482 spin_lock(&fs_info->qgroup_lock);
1483 del_relation_rb(fs_info, src, dst);
1484 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1485 spin_unlock(&fs_info->qgroup_lock);
1486 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001487out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001488 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001489 return ret;
1490}
1491
Lu Fengqi39616c22018-07-18 14:45:32 +08001492int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1493 u64 dst)
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001494{
Lu Fengqi39616c22018-07-18 14:45:32 +08001495 struct btrfs_fs_info *fs_info = trans->fs_info;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001496 int ret = 0;
1497
1498 mutex_lock(&fs_info->qgroup_ioctl_lock);
Lu Fengqi6b36f1a2018-07-18 14:45:31 +08001499 ret = __del_qgroup_relation(trans, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001500 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001501
Arne Jansenbed92ea2012-06-28 18:03:02 +02001502 return ret;
1503}
1504
Lu Fengqi49a05ec2018-07-18 14:45:33 +08001505int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001506{
Lu Fengqi49a05ec2018-07-18 14:45:33 +08001507 struct btrfs_fs_info *fs_info = trans->fs_info;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001508 struct btrfs_root *quota_root;
1509 struct btrfs_qgroup *qgroup;
1510 int ret = 0;
1511
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001512 mutex_lock(&fs_info->qgroup_ioctl_lock);
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03001513 if (!fs_info->quota_root) {
Marcos Paulo de Souza8a36e402019-11-25 21:58:51 -03001514 ret = -ENOTCONN;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001515 goto out;
1516 }
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03001517 quota_root = fs_info->quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001518 qgroup = find_qgroup_rb(fs_info, qgroupid);
1519 if (qgroup) {
1520 ret = -EEXIST;
1521 goto out;
1522 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001523
1524 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001525 if (ret)
1526 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001527
1528 spin_lock(&fs_info->qgroup_lock);
1529 qgroup = add_qgroup_rb(fs_info, qgroupid);
1530 spin_unlock(&fs_info->qgroup_lock);
1531
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001532 if (IS_ERR(qgroup)) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02001533 ret = PTR_ERR(qgroup);
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001534 goto out;
1535 }
1536 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001537out:
1538 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001539 return ret;
1540}
1541
Lu Fengqi3efbee12018-07-18 14:45:34 +08001542int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001543{
Lu Fengqi3efbee12018-07-18 14:45:34 +08001544 struct btrfs_fs_info *fs_info = trans->fs_info;
Arne Jansen2cf68702013-01-17 01:22:09 -07001545 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001546 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001547 int ret = 0;
1548
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001549 mutex_lock(&fs_info->qgroup_ioctl_lock);
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03001550 if (!fs_info->quota_root) {
Marcos Paulo de Souza8a36e402019-11-25 21:58:51 -03001551 ret = -ENOTCONN;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001552 goto out;
1553 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001554
Arne Jansen2cf68702013-01-17 01:22:09 -07001555 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001556 if (!qgroup) {
1557 ret = -ENOENT;
1558 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001559 }
Lu Fengqib90e22b2018-10-11 13:42:56 +08001560
1561 /* Check if there are no children of this qgroup */
1562 if (!list_empty(&qgroup->members)) {
1563 ret = -EBUSY;
1564 goto out;
1565 }
1566
Lu Fengqi69104612018-07-18 14:45:26 +08001567 ret = del_qgroup_item(trans, qgroupid);
Sargun Dhillon36b96fd2017-09-17 09:02:29 +00001568 if (ret && ret != -ENOENT)
1569 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001570
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001571 while (!list_empty(&qgroup->groups)) {
1572 list = list_first_entry(&qgroup->groups,
1573 struct btrfs_qgroup_list, next_group);
Lu Fengqi6b36f1a2018-07-18 14:45:31 +08001574 ret = __del_qgroup_relation(trans, qgroupid,
1575 list->group->qgroupid);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001576 if (ret)
1577 goto out;
1578 }
1579
Arne Jansenbed92ea2012-06-28 18:03:02 +02001580 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001581 del_qgroup_rb(fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001582 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001583out:
1584 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001585 return ret;
1586}
1587
Lu Fengqif0042d52018-07-18 14:45:35 +08001588int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001589 struct btrfs_qgroup_limit *limit)
1590{
Lu Fengqif0042d52018-07-18 14:45:35 +08001591 struct btrfs_fs_info *fs_info = trans->fs_info;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001592 struct btrfs_qgroup *qgroup;
1593 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001594 /* Sometimes we would want to clear the limit on this qgroup.
1595 * To meet this requirement, we treat the -1 as a special value
1596 * which tell kernel to clear the limit on this qgroup.
1597 */
1598 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001599
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001600 mutex_lock(&fs_info->qgroup_ioctl_lock);
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03001601 if (!fs_info->quota_root) {
Marcos Paulo de Souza8a36e402019-11-25 21:58:51 -03001602 ret = -ENOTCONN;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001603 goto out;
1604 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001605
Wang Shilongddb47af2013-04-07 10:50:20 +00001606 qgroup = find_qgroup_rb(fs_info, qgroupid);
1607 if (!qgroup) {
1608 ret = -ENOENT;
1609 goto out;
1610 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001611
Wang Shilong58400fc2013-04-07 10:50:17 +00001612 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001613 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1614 if (limit->max_rfer == CLEAR_VALUE) {
1615 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1616 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1617 qgroup->max_rfer = 0;
1618 } else {
1619 qgroup->max_rfer = limit->max_rfer;
1620 }
1621 }
1622 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1623 if (limit->max_excl == CLEAR_VALUE) {
1624 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1625 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1626 qgroup->max_excl = 0;
1627 } else {
1628 qgroup->max_excl = limit->max_excl;
1629 }
1630 }
1631 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1632 if (limit->rsv_rfer == CLEAR_VALUE) {
1633 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1634 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1635 qgroup->rsv_rfer = 0;
1636 } else {
1637 qgroup->rsv_rfer = limit->rsv_rfer;
1638 }
1639 }
1640 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1641 if (limit->rsv_excl == CLEAR_VALUE) {
1642 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1643 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1644 qgroup->rsv_excl = 0;
1645 } else {
1646 qgroup->rsv_excl = limit->rsv_excl;
1647 }
1648 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001649 qgroup->lim_flags |= limit->flags;
1650
Arne Jansenbed92ea2012-06-28 18:03:02 +02001651 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001652
Lu Fengqiac8a8662018-07-18 14:45:27 +08001653 ret = update_qgroup_limit_item(trans, qgroup);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001654 if (ret) {
1655 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1656 btrfs_info(fs_info, "unable to update quota limit for %llu",
1657 qgroupid);
1658 }
1659
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001660out:
1661 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001662 return ret;
1663}
Mark Fasheh11526512014-07-17 12:39:01 -07001664
Qu Wenruo50b3e042016-10-18 09:31:27 +08001665int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +08001666 struct btrfs_delayed_ref_root *delayed_refs,
1667 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001668{
1669 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1670 struct rb_node *parent_node = NULL;
1671 struct btrfs_qgroup_extent_record *entry;
1672 u64 bytenr = record->bytenr;
1673
David Sterbaa4666e62018-03-16 02:21:22 +01001674 lockdep_assert_held(&delayed_refs->lock);
Qu Wenruo50b3e042016-10-18 09:31:27 +08001675 trace_btrfs_qgroup_trace_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001676
Qu Wenruo3368d002015-04-16 14:34:17 +08001677 while (*p) {
1678 parent_node = *p;
1679 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1680 node);
Qu Wenruo1418bae2019-01-23 15:15:12 +08001681 if (bytenr < entry->bytenr) {
Qu Wenruo3368d002015-04-16 14:34:17 +08001682 p = &(*p)->rb_left;
Qu Wenruo1418bae2019-01-23 15:15:12 +08001683 } else if (bytenr > entry->bytenr) {
Qu Wenruo3368d002015-04-16 14:34:17 +08001684 p = &(*p)->rb_right;
Qu Wenruo1418bae2019-01-23 15:15:12 +08001685 } else {
1686 if (record->data_rsv && !entry->data_rsv) {
1687 entry->data_rsv = record->data_rsv;
1688 entry->data_rsv_refroot =
1689 record->data_rsv_refroot;
1690 }
Qu Wenruocb93b522016-08-15 10:36:50 +08001691 return 1;
Qu Wenruo1418bae2019-01-23 15:15:12 +08001692 }
Qu Wenruo3368d002015-04-16 14:34:17 +08001693 }
1694
1695 rb_link_node(&record->node, parent_node, p);
1696 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001697 return 0;
1698}
1699
Qu Wenruofb235dc2017-02-15 10:43:03 +08001700int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1701 struct btrfs_qgroup_extent_record *qrecord)
1702{
1703 struct ulist *old_root;
1704 u64 bytenr = qrecord->bytenr;
1705 int ret;
1706
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001707 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
Nikolay Borisov952bd3db2018-01-29 15:53:01 +02001708 if (ret < 0) {
1709 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1710 btrfs_warn(fs_info,
1711"error accounting new delayed refs extent (err code: %d), quota inconsistent",
1712 ret);
1713 return 0;
1714 }
Qu Wenruofb235dc2017-02-15 10:43:03 +08001715
1716 /*
1717 * Here we don't need to get the lock of
1718 * trans->transaction->delayed_refs, since inserted qrecord won't
1719 * be deleted, only qrecord->node may be modified (new qrecord insert)
1720 *
1721 * So modifying qrecord->old_roots is safe here
1722 */
1723 qrecord->old_roots = old_root;
1724 return 0;
1725}
1726
Lu Fengqia95f3aa2018-07-18 16:28:03 +08001727int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
1728 u64 num_bytes, gfp_t gfp_flag)
Qu Wenruocb93b522016-08-15 10:36:50 +08001729{
Lu Fengqia95f3aa2018-07-18 16:28:03 +08001730 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruocb93b522016-08-15 10:36:50 +08001731 struct btrfs_qgroup_extent_record *record;
1732 struct btrfs_delayed_ref_root *delayed_refs;
1733 int ret;
1734
Josef Bacikafcdd122016-09-02 15:40:02 -04001735 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1736 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001737 return 0;
Qu Wenruo1418bae2019-01-23 15:15:12 +08001738 record = kzalloc(sizeof(*record), gfp_flag);
Qu Wenruocb93b522016-08-15 10:36:50 +08001739 if (!record)
1740 return -ENOMEM;
1741
1742 delayed_refs = &trans->transaction->delayed_refs;
1743 record->bytenr = bytenr;
1744 record->num_bytes = num_bytes;
1745 record->old_roots = NULL;
1746
1747 spin_lock(&delayed_refs->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001748 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
Qu Wenruocb93b522016-08-15 10:36:50 +08001749 spin_unlock(&delayed_refs->lock);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001750 if (ret > 0) {
Qu Wenruocb93b522016-08-15 10:36:50 +08001751 kfree(record);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001752 return 0;
1753 }
1754 return btrfs_qgroup_trace_extent_post(fs_info, record);
Qu Wenruo3368d002015-04-16 14:34:17 +08001755}
1756
Qu Wenruo33d1f052016-10-18 09:31:28 +08001757int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Qu Wenruo33d1f052016-10-18 09:31:28 +08001758 struct extent_buffer *eb)
1759{
Lu Fengqi8d38d7e2018-07-18 14:45:37 +08001760 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001761 int nr = btrfs_header_nritems(eb);
1762 int i, extent_type, ret;
1763 struct btrfs_key key;
1764 struct btrfs_file_extent_item *fi;
1765 u64 bytenr, num_bytes;
1766
1767 /* We can be called directly from walk_up_proc() */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001769 return 0;
1770
1771 for (i = 0; i < nr; i++) {
1772 btrfs_item_key_to_cpu(eb, &key, i);
1773
1774 if (key.type != BTRFS_EXTENT_DATA_KEY)
1775 continue;
1776
1777 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1778 /* filter out non qgroup-accountable extents */
1779 extent_type = btrfs_file_extent_type(eb, fi);
1780
1781 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1782 continue;
1783
1784 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1785 if (!bytenr)
1786 continue;
1787
1788 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1789
Lu Fengqia95f3aa2018-07-18 16:28:03 +08001790 ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
1791 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001792 if (ret)
1793 return ret;
1794 }
Jeff Mahoneycddf3b22017-06-20 08:15:26 -04001795 cond_resched();
Qu Wenruo33d1f052016-10-18 09:31:28 +08001796 return 0;
1797}
1798
1799/*
1800 * Walk up the tree from the bottom, freeing leaves and any interior
1801 * nodes which have had all slots visited. If a node (leaf or
1802 * interior) is freed, the node above it will have it's slot
1803 * incremented. The root node will never be freed.
1804 *
1805 * At the end of this function, we should have a path which has all
1806 * slots incremented to the next position for a search. If we need to
1807 * read a new node it will be NULL and the node above it will have the
1808 * correct slot selected for a later read.
1809 *
1810 * If we increment the root nodes slot counter past the number of
1811 * elements, 1 is returned to signal completion of the search.
1812 */
David Sterba15b34512017-02-10 20:30:23 +01001813static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
Qu Wenruo33d1f052016-10-18 09:31:28 +08001814{
1815 int level = 0;
1816 int nr, slot;
1817 struct extent_buffer *eb;
1818
1819 if (root_level == 0)
1820 return 1;
1821
1822 while (level <= root_level) {
1823 eb = path->nodes[level];
1824 nr = btrfs_header_nritems(eb);
1825 path->slots[level]++;
1826 slot = path->slots[level];
1827 if (slot >= nr || level == 0) {
1828 /*
1829 * Don't free the root - we will detect this
1830 * condition after our loop and return a
1831 * positive value for caller to stop walking the tree.
1832 */
1833 if (level != root_level) {
1834 btrfs_tree_unlock_rw(eb, path->locks[level]);
1835 path->locks[level] = 0;
1836
1837 free_extent_buffer(eb);
1838 path->nodes[level] = NULL;
1839 path->slots[level] = 0;
1840 }
1841 } else {
1842 /*
1843 * We have a valid slot to walk back down
1844 * from. Stop here so caller can process these
1845 * new nodes.
1846 */
1847 break;
1848 }
1849
1850 level++;
1851 }
1852
1853 eb = path->nodes[root_level];
1854 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1855 return 1;
1856
1857 return 0;
1858}
1859
Qu Wenruo25982562018-09-27 14:42:30 +08001860/*
1861 * Helper function to trace a subtree tree block swap.
1862 *
1863 * The swap will happen in highest tree block, but there may be a lot of
1864 * tree blocks involved.
1865 *
1866 * For example:
1867 * OO = Old tree blocks
1868 * NN = New tree blocks allocated during balance
1869 *
1870 * File tree (257) Reloc tree for 257
1871 * L2 OO NN
1872 * / \ / \
1873 * L1 OO OO (a) OO NN (a)
1874 * / \ / \ / \ / \
1875 * L0 OO OO OO OO OO OO NN NN
1876 * (b) (c) (b) (c)
1877 *
1878 * When calling qgroup_trace_extent_swap(), we will pass:
1879 * @src_eb = OO(a)
1880 * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
1881 * @dst_level = 0
1882 * @root_level = 1
1883 *
1884 * In that case, qgroup_trace_extent_swap() will search from OO(a) to
1885 * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
1886 *
1887 * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
1888 *
1889 * 1) Tree search from @src_eb
1890 * It should acts as a simplified btrfs_search_slot().
1891 * The key for search can be extracted from @dst_path->nodes[dst_level]
1892 * (first key).
1893 *
1894 * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
1895 * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
Andrea Gelmini52042d82018-11-28 12:05:13 +01001896 * They should be marked during previous (@dst_level = 1) iteration.
Qu Wenruo25982562018-09-27 14:42:30 +08001897 *
1898 * 3) Mark file extents in leaves dirty
1899 * We don't have good way to pick out new file extents only.
1900 * So we still follow the old method by scanning all file extents in
1901 * the leave.
1902 *
Andrea Gelmini52042d82018-11-28 12:05:13 +01001903 * This function can free us from keeping two paths, thus later we only need
Qu Wenruo25982562018-09-27 14:42:30 +08001904 * to care about how to iterate all new tree blocks in reloc tree.
1905 */
1906static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
1907 struct extent_buffer *src_eb,
1908 struct btrfs_path *dst_path,
Qu Wenruo3d0174f2018-09-27 14:42:35 +08001909 int dst_level, int root_level,
1910 bool trace_leaf)
Qu Wenruo25982562018-09-27 14:42:30 +08001911{
1912 struct btrfs_key key;
1913 struct btrfs_path *src_path;
1914 struct btrfs_fs_info *fs_info = trans->fs_info;
1915 u32 nodesize = fs_info->nodesize;
1916 int cur_level = root_level;
1917 int ret;
1918
1919 BUG_ON(dst_level > root_level);
1920 /* Level mismatch */
1921 if (btrfs_header_level(src_eb) != root_level)
1922 return -EINVAL;
1923
1924 src_path = btrfs_alloc_path();
1925 if (!src_path) {
1926 ret = -ENOMEM;
1927 goto out;
1928 }
1929
1930 if (dst_level)
1931 btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1932 else
1933 btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1934
1935 /* For src_path */
David Sterba67439da2019-10-08 13:28:47 +02001936 atomic_inc(&src_eb->refs);
Qu Wenruo25982562018-09-27 14:42:30 +08001937 src_path->nodes[root_level] = src_eb;
1938 src_path->slots[root_level] = dst_path->slots[root_level];
1939 src_path->locks[root_level] = 0;
1940
1941 /* A simplified version of btrfs_search_slot() */
1942 while (cur_level >= dst_level) {
1943 struct btrfs_key src_key;
1944 struct btrfs_key dst_key;
1945
1946 if (src_path->nodes[cur_level] == NULL) {
1947 struct btrfs_key first_key;
1948 struct extent_buffer *eb;
1949 int parent_slot;
1950 u64 child_gen;
1951 u64 child_bytenr;
1952
1953 eb = src_path->nodes[cur_level + 1];
1954 parent_slot = src_path->slots[cur_level + 1];
1955 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1956 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1957 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
1958
1959 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1960 cur_level, &first_key);
1961 if (IS_ERR(eb)) {
1962 ret = PTR_ERR(eb);
1963 goto out;
1964 } else if (!extent_buffer_uptodate(eb)) {
1965 free_extent_buffer(eb);
1966 ret = -EIO;
1967 goto out;
1968 }
1969
1970 src_path->nodes[cur_level] = eb;
1971
1972 btrfs_tree_read_lock(eb);
Josef Bacikac5887c2020-08-20 11:46:10 -04001973 src_path->locks[cur_level] = BTRFS_READ_LOCK;
Qu Wenruo25982562018-09-27 14:42:30 +08001974 }
1975
1976 src_path->slots[cur_level] = dst_path->slots[cur_level];
1977 if (cur_level) {
1978 btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
1979 &dst_key, dst_path->slots[cur_level]);
1980 btrfs_node_key_to_cpu(src_path->nodes[cur_level],
1981 &src_key, src_path->slots[cur_level]);
1982 } else {
1983 btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
1984 &dst_key, dst_path->slots[cur_level]);
1985 btrfs_item_key_to_cpu(src_path->nodes[cur_level],
1986 &src_key, src_path->slots[cur_level]);
1987 }
1988 /* Content mismatch, something went wrong */
1989 if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
1990 ret = -ENOENT;
1991 goto out;
1992 }
1993 cur_level--;
1994 }
1995
1996 /*
1997 * Now both @dst_path and @src_path have been populated, record the tree
1998 * blocks for qgroup accounting.
1999 */
2000 ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
2001 nodesize, GFP_NOFS);
2002 if (ret < 0)
2003 goto out;
2004 ret = btrfs_qgroup_trace_extent(trans,
2005 dst_path->nodes[dst_level]->start,
2006 nodesize, GFP_NOFS);
2007 if (ret < 0)
2008 goto out;
2009
2010 /* Record leaf file extents */
Qu Wenruo3d0174f2018-09-27 14:42:35 +08002011 if (dst_level == 0 && trace_leaf) {
Qu Wenruo25982562018-09-27 14:42:30 +08002012 ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
2013 if (ret < 0)
2014 goto out;
2015 ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
2016 }
2017out:
2018 btrfs_free_path(src_path);
2019 return ret;
2020}
2021
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002022/*
2023 * Helper function to do recursive generation-aware depth-first search, to
2024 * locate all new tree blocks in a subtree of reloc tree.
2025 *
2026 * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
2027 * reloc tree
2028 * L2 NN (a)
2029 * / \
2030 * L1 OO NN (b)
2031 * / \ / \
2032 * L0 OO OO OO NN
2033 * (c) (d)
2034 * If we pass:
2035 * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
2036 * @cur_level = 1
2037 * @root_level = 1
2038 *
2039 * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
2040 * above tree blocks along with their counter parts in file tree.
Andrea Gelmini52042d82018-11-28 12:05:13 +01002041 * While during search, old tree blocks OO(c) will be skipped as tree block swap
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002042 * won't affect OO(c).
2043 */
2044static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
2045 struct extent_buffer *src_eb,
2046 struct btrfs_path *dst_path,
2047 int cur_level, int root_level,
Qu Wenruo3d0174f2018-09-27 14:42:35 +08002048 u64 last_snapshot, bool trace_leaf)
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002049{
2050 struct btrfs_fs_info *fs_info = trans->fs_info;
2051 struct extent_buffer *eb;
2052 bool need_cleanup = false;
2053 int ret = 0;
2054 int i;
2055
2056 /* Level sanity check */
Nikolay Borisov7ff2c2a2019-03-18 17:45:19 +02002057 if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
2058 root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002059 root_level < cur_level) {
2060 btrfs_err_rl(fs_info,
2061 "%s: bad levels, cur_level=%d root_level=%d",
2062 __func__, cur_level, root_level);
2063 return -EUCLEAN;
2064 }
2065
2066 /* Read the tree block if needed */
2067 if (dst_path->nodes[cur_level] == NULL) {
2068 struct btrfs_key first_key;
2069 int parent_slot;
2070 u64 child_gen;
2071 u64 child_bytenr;
2072
2073 /*
2074 * dst_path->nodes[root_level] must be initialized before
2075 * calling this function.
2076 */
2077 if (cur_level == root_level) {
2078 btrfs_err_rl(fs_info,
2079 "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
2080 __func__, root_level, root_level, cur_level);
2081 return -EUCLEAN;
2082 }
2083
2084 /*
2085 * We need to get child blockptr/gen from parent before we can
2086 * read it.
2087 */
2088 eb = dst_path->nodes[cur_level + 1];
2089 parent_slot = dst_path->slots[cur_level + 1];
2090 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2091 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2092 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2093
2094 /* This node is old, no need to trace */
2095 if (child_gen < last_snapshot)
2096 goto out;
2097
2098 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2099 cur_level, &first_key);
2100 if (IS_ERR(eb)) {
2101 ret = PTR_ERR(eb);
2102 goto out;
2103 } else if (!extent_buffer_uptodate(eb)) {
2104 free_extent_buffer(eb);
2105 ret = -EIO;
2106 goto out;
2107 }
2108
2109 dst_path->nodes[cur_level] = eb;
2110 dst_path->slots[cur_level] = 0;
2111
2112 btrfs_tree_read_lock(eb);
Josef Bacikac5887c2020-08-20 11:46:10 -04002113 dst_path->locks[cur_level] = BTRFS_READ_LOCK;
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002114 need_cleanup = true;
2115 }
2116
2117 /* Now record this tree block and its counter part for qgroups */
2118 ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
Qu Wenruo3d0174f2018-09-27 14:42:35 +08002119 root_level, trace_leaf);
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002120 if (ret < 0)
2121 goto cleanup;
2122
2123 eb = dst_path->nodes[cur_level];
2124
2125 if (cur_level > 0) {
2126 /* Iterate all child tree blocks */
2127 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2128 /* Skip old tree blocks as they won't be swapped */
2129 if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
2130 continue;
2131 dst_path->slots[cur_level] = i;
2132
2133 /* Recursive call (at most 7 times) */
2134 ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
2135 dst_path, cur_level - 1, root_level,
Qu Wenruo3d0174f2018-09-27 14:42:35 +08002136 last_snapshot, trace_leaf);
Qu Wenruoea49f3e2018-09-27 14:42:31 +08002137 if (ret < 0)
2138 goto cleanup;
2139 }
2140 }
2141
2142cleanup:
2143 if (need_cleanup) {
2144 /* Clean up */
2145 btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
2146 dst_path->locks[cur_level]);
2147 free_extent_buffer(dst_path->nodes[cur_level]);
2148 dst_path->nodes[cur_level] = NULL;
2149 dst_path->slots[cur_level] = 0;
2150 dst_path->locks[cur_level] = 0;
2151 }
2152out:
2153 return ret;
2154}
2155
Qu Wenruo5aea1a42019-01-23 15:15:15 +08002156static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
2157 struct extent_buffer *src_eb,
2158 struct extent_buffer *dst_eb,
2159 u64 last_snapshot, bool trace_leaf)
2160{
2161 struct btrfs_fs_info *fs_info = trans->fs_info;
2162 struct btrfs_path *dst_path = NULL;
2163 int level;
2164 int ret;
2165
2166 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2167 return 0;
2168
2169 /* Wrong parameter order */
2170 if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
2171 btrfs_err_rl(fs_info,
2172 "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
2173 btrfs_header_generation(src_eb),
2174 btrfs_header_generation(dst_eb));
2175 return -EUCLEAN;
2176 }
2177
2178 if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
2179 ret = -EIO;
2180 goto out;
2181 }
2182
2183 level = btrfs_header_level(dst_eb);
2184 dst_path = btrfs_alloc_path();
2185 if (!dst_path) {
2186 ret = -ENOMEM;
2187 goto out;
2188 }
2189 /* For dst_path */
David Sterba67439da2019-10-08 13:28:47 +02002190 atomic_inc(&dst_eb->refs);
Qu Wenruo5aea1a42019-01-23 15:15:15 +08002191 dst_path->nodes[level] = dst_eb;
2192 dst_path->slots[level] = 0;
2193 dst_path->locks[level] = 0;
2194
2195 /* Do the generation aware breadth-first search */
2196 ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
2197 level, last_snapshot, trace_leaf);
2198 if (ret < 0)
2199 goto out;
2200 ret = 0;
2201
2202out:
2203 btrfs_free_path(dst_path);
2204 if (ret < 0)
2205 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2206 return ret;
2207}
2208
Qu Wenruo33d1f052016-10-18 09:31:28 +08002209int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
Qu Wenruo33d1f052016-10-18 09:31:28 +08002210 struct extent_buffer *root_eb,
2211 u64 root_gen, int root_level)
2212{
Lu Fengqideb40622018-07-18 14:45:38 +08002213 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08002214 int ret = 0;
2215 int level;
2216 struct extent_buffer *eb = root_eb;
2217 struct btrfs_path *path = NULL;
2218
Nikolay Borisovb6e6bca2017-07-12 09:42:19 +03002219 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002220 BUG_ON(root_eb == NULL);
2221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002222 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08002223 return 0;
2224
2225 if (!extent_buffer_uptodate(root_eb)) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002226 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002227 if (ret)
2228 goto out;
2229 }
2230
2231 if (root_level == 0) {
Lu Fengqi8d38d7e2018-07-18 14:45:37 +08002232 ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002233 goto out;
2234 }
2235
2236 path = btrfs_alloc_path();
2237 if (!path)
2238 return -ENOMEM;
2239
2240 /*
2241 * Walk down the tree. Missing extent blocks are filled in as
2242 * we go. Metadata is accounted every time we read a new
2243 * extent block.
2244 *
2245 * When we reach a leaf, we account for file extent items in it,
2246 * walk back up the tree (adjusting slot pointers as we go)
2247 * and restart the search process.
2248 */
David Sterba67439da2019-10-08 13:28:47 +02002249 atomic_inc(&root_eb->refs); /* For path */
Qu Wenruo33d1f052016-10-18 09:31:28 +08002250 path->nodes[root_level] = root_eb;
2251 path->slots[root_level] = 0;
2252 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
2253walk_down:
2254 level = root_level;
2255 while (level >= 0) {
2256 if (path->nodes[level] == NULL) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002257 struct btrfs_key first_key;
Qu Wenruo33d1f052016-10-18 09:31:28 +08002258 int parent_slot;
2259 u64 child_gen;
2260 u64 child_bytenr;
2261
2262 /*
2263 * We need to get child blockptr/gen from parent before
2264 * we can read it.
2265 */
2266 eb = path->nodes[level + 1];
2267 parent_slot = path->slots[level + 1];
2268 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2269 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08002270 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002271
Qu Wenruo581c1762018-03-29 09:08:11 +08002272 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2273 level, &first_key);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002274 if (IS_ERR(eb)) {
2275 ret = PTR_ERR(eb);
2276 goto out;
2277 } else if (!extent_buffer_uptodate(eb)) {
2278 free_extent_buffer(eb);
2279 ret = -EIO;
2280 goto out;
2281 }
2282
2283 path->nodes[level] = eb;
2284 path->slots[level] = 0;
2285
2286 btrfs_tree_read_lock(eb);
Josef Bacikac5887c2020-08-20 11:46:10 -04002287 path->locks[level] = BTRFS_READ_LOCK;
Qu Wenruo33d1f052016-10-18 09:31:28 +08002288
Lu Fengqia95f3aa2018-07-18 16:28:03 +08002289 ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002290 fs_info->nodesize,
2291 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002292 if (ret)
2293 goto out;
2294 }
2295
2296 if (level == 0) {
Lu Fengqi8d38d7e2018-07-18 14:45:37 +08002297 ret = btrfs_qgroup_trace_leaf_items(trans,
2298 path->nodes[level]);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002299 if (ret)
2300 goto out;
2301
2302 /* Nonzero return here means we completed our search */
David Sterba15b34512017-02-10 20:30:23 +01002303 ret = adjust_slots_upwards(path, root_level);
Qu Wenruo33d1f052016-10-18 09:31:28 +08002304 if (ret)
2305 break;
2306
2307 /* Restart search with new slots */
2308 goto walk_down;
2309 }
2310
2311 level--;
2312 }
2313
2314 ret = 0;
2315out:
2316 btrfs_free_path(path);
2317
2318 return ret;
2319}
2320
Qu Wenruod810ef22015-04-12 16:52:34 +08002321#define UPDATE_NEW 0
2322#define UPDATE_OLD 1
2323/*
2324 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2325 */
2326static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2327 struct ulist *roots, struct ulist *tmp,
2328 struct ulist *qgroups, u64 seq, int update_old)
2329{
2330 struct ulist_node *unode;
2331 struct ulist_iterator uiter;
2332 struct ulist_node *tmp_unode;
2333 struct ulist_iterator tmp_uiter;
2334 struct btrfs_qgroup *qg;
2335 int ret = 0;
2336
2337 if (!roots)
2338 return 0;
2339 ULIST_ITER_INIT(&uiter);
2340 while ((unode = ulist_next(roots, &uiter))) {
2341 qg = find_qgroup_rb(fs_info, unode->val);
2342 if (!qg)
2343 continue;
2344
2345 ulist_reinit(tmp);
David Sterbaef2fff62016-10-26 16:23:50 +02002346 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
Qu Wenruod810ef22015-04-12 16:52:34 +08002347 GFP_ATOMIC);
2348 if (ret < 0)
2349 return ret;
David Sterbaef2fff62016-10-26 16:23:50 +02002350 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
Qu Wenruod810ef22015-04-12 16:52:34 +08002351 if (ret < 0)
2352 return ret;
2353 ULIST_ITER_INIT(&tmp_uiter);
2354 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
2355 struct btrfs_qgroup_list *glist;
2356
David Sterbaef2fff62016-10-26 16:23:50 +02002357 qg = unode_aux_to_qgroup(tmp_unode);
Qu Wenruod810ef22015-04-12 16:52:34 +08002358 if (update_old)
2359 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2360 else
2361 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2362 list_for_each_entry(glist, &qg->groups, next_group) {
2363 ret = ulist_add(qgroups, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02002364 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08002365 GFP_ATOMIC);
2366 if (ret < 0)
2367 return ret;
2368 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02002369 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08002370 GFP_ATOMIC);
2371 if (ret < 0)
2372 return ret;
2373 }
2374 }
2375 }
2376 return 0;
2377}
2378
Josef Bacikfcebe452014-05-13 17:30:47 -07002379/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08002380 * Update qgroup rfer/excl counters.
2381 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08002382 *
Randy Dunlap260db432020-08-04 19:48:34 -07002383 * Excl update is tricky, the update is split into 2 parts.
Qu Wenruo823ae5b2015-04-12 16:59:57 +08002384 * Part 1: Possible exclusive <-> sharing detect:
2385 * | A | !A |
2386 * -------------------------------------
2387 * B | * | - |
2388 * -------------------------------------
2389 * !B | + | ** |
2390 * -------------------------------------
2391 *
2392 * Conditions:
2393 * A: cur_old_roots < nr_old_roots (not exclusive before)
2394 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
2395 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04002396 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08002397 *
2398 * Results:
2399 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
2400 * *: Definitely not changed. **: Possible unchanged.
2401 *
2402 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
2403 *
2404 * To make the logic clear, we first use condition A and B to split
2405 * combination into 4 results.
2406 *
2407 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
2408 * only on variant maybe 0.
2409 *
2410 * Lastly, check result **, since there are 2 variants maybe 0, split them
2411 * again(2x2).
2412 * But this time we don't need to consider other things, the codes and logic
2413 * is easy to understand now.
2414 */
2415static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
2416 struct ulist *qgroups,
2417 u64 nr_old_roots,
2418 u64 nr_new_roots,
2419 u64 num_bytes, u64 seq)
2420{
2421 struct ulist_node *unode;
2422 struct ulist_iterator uiter;
2423 struct btrfs_qgroup *qg;
2424 u64 cur_new_count, cur_old_count;
2425
2426 ULIST_ITER_INIT(&uiter);
2427 while ((unode = ulist_next(qgroups, &uiter))) {
2428 bool dirty = false;
2429
David Sterbaef2fff62016-10-26 16:23:50 +02002430 qg = unode_aux_to_qgroup(unode);
Qu Wenruo823ae5b2015-04-12 16:59:57 +08002431 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2432 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2433
Qu Wenruo8b317902018-04-30 15:04:44 +08002434 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
2435 cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002436
Qu Wenruo823ae5b2015-04-12 16:59:57 +08002437 /* Rfer update part */
2438 if (cur_old_count == 0 && cur_new_count > 0) {
2439 qg->rfer += num_bytes;
2440 qg->rfer_cmpr += num_bytes;
2441 dirty = true;
2442 }
2443 if (cur_old_count > 0 && cur_new_count == 0) {
2444 qg->rfer -= num_bytes;
2445 qg->rfer_cmpr -= num_bytes;
2446 dirty = true;
2447 }
2448
2449 /* Excl update part */
2450 /* Exclusive/none -> shared case */
2451 if (cur_old_count == nr_old_roots &&
2452 cur_new_count < nr_new_roots) {
2453 /* Exclusive -> shared */
2454 if (cur_old_count != 0) {
2455 qg->excl -= num_bytes;
2456 qg->excl_cmpr -= num_bytes;
2457 dirty = true;
2458 }
2459 }
2460
2461 /* Shared -> exclusive/none case */
2462 if (cur_old_count < nr_old_roots &&
2463 cur_new_count == nr_new_roots) {
2464 /* Shared->exclusive */
2465 if (cur_new_count != 0) {
2466 qg->excl += num_bytes;
2467 qg->excl_cmpr += num_bytes;
2468 dirty = true;
2469 }
2470 }
2471
2472 /* Exclusive/none -> exclusive/none case */
2473 if (cur_old_count == nr_old_roots &&
2474 cur_new_count == nr_new_roots) {
2475 if (cur_old_count == 0) {
2476 /* None -> exclusive/none */
2477
2478 if (cur_new_count != 0) {
2479 /* None -> exclusive */
2480 qg->excl += num_bytes;
2481 qg->excl_cmpr += num_bytes;
2482 dirty = true;
2483 }
2484 /* None -> none, nothing changed */
2485 } else {
2486 /* Exclusive -> exclusive/none */
2487
2488 if (cur_new_count == 0) {
2489 /* Exclusive -> none */
2490 qg->excl -= num_bytes;
2491 qg->excl_cmpr -= num_bytes;
2492 dirty = true;
2493 }
2494 /* Exclusive -> exclusive, nothing changed */
2495 }
2496 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08002497
Qu Wenruo823ae5b2015-04-12 16:59:57 +08002498 if (dirty)
2499 qgroup_dirty(fs_info, qg);
2500 }
2501 return 0;
2502}
2503
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002504/*
2505 * Check if the @roots potentially is a list of fs tree roots
2506 *
2507 * Return 0 for definitely not a fs/subvol tree roots ulist
2508 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
2509 * one as well)
2510 */
2511static int maybe_fs_roots(struct ulist *roots)
2512{
2513 struct ulist_node *unode;
2514 struct ulist_iterator uiter;
2515
2516 /* Empty one, still possible for fs roots */
2517 if (!roots || roots->nnodes == 0)
2518 return 1;
2519
2520 ULIST_ITER_INIT(&uiter);
2521 unode = ulist_next(roots, &uiter);
2522 if (!unode)
2523 return 1;
2524
2525 /*
2526 * If it contains fs tree roots, then it must belong to fs/subvol
2527 * trees.
2528 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2529 */
2530 return is_fstree(unode->val);
2531}
2532
Lu Fengqi8696d762018-07-18 14:45:39 +08002533int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2534 u64 num_bytes, struct ulist *old_roots,
2535 struct ulist *new_roots)
Qu Wenruo550d7a22015-04-16 15:37:33 +08002536{
Lu Fengqi8696d762018-07-18 14:45:39 +08002537 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002538 struct ulist *qgroups = NULL;
2539 struct ulist *tmp = NULL;
2540 u64 seq;
2541 u64 nr_new_roots = 0;
2542 u64 nr_old_roots = 0;
2543 int ret = 0;
2544
Johannes Thumshirn26ef8492020-01-08 21:07:32 +09002545 /*
2546 * If quotas get disabled meanwhile, the resouces need to be freed and
2547 * we can't just exit here.
2548 */
David Sterba81353d52017-02-13 14:05:24 +01002549 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Johannes Thumshirn26ef8492020-01-08 21:07:32 +09002550 goto out_free;
David Sterba81353d52017-02-13 14:05:24 +01002551
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002552 if (new_roots) {
2553 if (!maybe_fs_roots(new_roots))
2554 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002555 nr_new_roots = new_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002556 }
2557 if (old_roots) {
2558 if (!maybe_fs_roots(old_roots))
2559 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002560 nr_old_roots = old_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002561 }
2562
2563 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2564 if (nr_old_roots == 0 && nr_new_roots == 0)
2565 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002566
Qu Wenruo550d7a22015-04-16 15:37:33 +08002567 BUG_ON(!fs_info->quota_root);
2568
Qu Wenruoc9f6f3c2018-05-03 09:59:02 +08002569 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2570 num_bytes, nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002571
Qu Wenruo550d7a22015-04-16 15:37:33 +08002572 qgroups = ulist_alloc(GFP_NOFS);
2573 if (!qgroups) {
2574 ret = -ENOMEM;
2575 goto out_free;
2576 }
2577 tmp = ulist_alloc(GFP_NOFS);
2578 if (!tmp) {
2579 ret = -ENOMEM;
2580 goto out_free;
2581 }
2582
2583 mutex_lock(&fs_info->qgroup_rescan_lock);
2584 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2585 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2586 mutex_unlock(&fs_info->qgroup_rescan_lock);
2587 ret = 0;
2588 goto out_free;
2589 }
2590 }
2591 mutex_unlock(&fs_info->qgroup_rescan_lock);
2592
2593 spin_lock(&fs_info->qgroup_lock);
2594 seq = fs_info->qgroup_seq;
2595
2596 /* Update old refcnts using old_roots */
2597 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2598 UPDATE_OLD);
2599 if (ret < 0)
2600 goto out;
2601
2602 /* Update new refcnts using new_roots */
2603 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2604 UPDATE_NEW);
2605 if (ret < 0)
2606 goto out;
2607
2608 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2609 num_bytes, seq);
2610
2611 /*
2612 * Bump qgroup_seq to avoid seq overlap
2613 */
2614 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2615out:
2616 spin_unlock(&fs_info->qgroup_lock);
2617out_free:
2618 ulist_free(tmp);
2619 ulist_free(qgroups);
2620 ulist_free(old_roots);
2621 ulist_free(new_roots);
2622 return ret;
2623}
2624
Nikolay Borisov460fb202018-03-15 16:00:25 +02002625int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
Qu Wenruo550d7a22015-04-16 15:37:33 +08002626{
Nikolay Borisov460fb202018-03-15 16:00:25 +02002627 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002628 struct btrfs_qgroup_extent_record *record;
2629 struct btrfs_delayed_ref_root *delayed_refs;
2630 struct ulist *new_roots = NULL;
2631 struct rb_node *node;
Qu Wenruoc337e7b2018-09-27 14:42:29 +08002632 u64 num_dirty_extents = 0;
Qu Wenruo9086db82015-04-20 09:53:50 +08002633 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002634 int ret = 0;
2635
2636 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08002637 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002638 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2639 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2640 node);
2641
Qu Wenruoc337e7b2018-09-27 14:42:29 +08002642 num_dirty_extents++;
Jeff Mahoneybc074522016-06-09 17:27:55 -04002643 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002644
Qu Wenruo550d7a22015-04-16 15:37:33 +08002645 if (!ret) {
2646 /*
Qu Wenruod1b8b942017-02-27 15:10:35 +08002647 * Old roots should be searched when inserting qgroup
2648 * extent record
2649 */
2650 if (WARN_ON(!record->old_roots)) {
2651 /* Search commit root to find old_roots */
2652 ret = btrfs_find_all_roots(NULL, fs_info,
2653 record->bytenr, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002654 &record->old_roots, false);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002655 if (ret < 0)
2656 goto cleanup;
2657 }
2658
Qu Wenruo1418bae2019-01-23 15:15:12 +08002659 /* Free the reserved data space */
2660 btrfs_qgroup_free_refroot(fs_info,
2661 record->data_rsv_refroot,
2662 record->data_rsv,
2663 BTRFS_QGROUP_RSV_DATA);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002664 /*
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002665 * Use SEQ_LAST as time_seq to do special search, which
Qu Wenruo550d7a22015-04-16 15:37:33 +08002666 * doesn't lock tree or delayed_refs and search current
2667 * root. It's safe inside commit_transaction().
2668 */
2669 ret = btrfs_find_all_roots(trans, fs_info,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002670 record->bytenr, SEQ_LAST, &new_roots, false);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002671 if (ret < 0)
2672 goto cleanup;
Qu Wenruod1b8b942017-02-27 15:10:35 +08002673 if (qgroup_to_skip) {
Qu Wenruo9086db82015-04-20 09:53:50 +08002674 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002675 ulist_del(record->old_roots, qgroup_to_skip,
2676 0);
2677 }
Lu Fengqi8696d762018-07-18 14:45:39 +08002678 ret = btrfs_qgroup_account_extent(trans, record->bytenr,
2679 record->num_bytes,
2680 record->old_roots,
2681 new_roots);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002682 record->old_roots = NULL;
2683 new_roots = NULL;
2684 }
2685cleanup:
2686 ulist_free(record->old_roots);
2687 ulist_free(new_roots);
2688 new_roots = NULL;
2689 rb_erase(node, &delayed_refs->dirty_extent_root);
2690 kfree(record);
2691
2692 }
Qu Wenruoc337e7b2018-09-27 14:42:29 +08002693 trace_qgroup_num_dirty_extents(fs_info, trans->transid,
2694 num_dirty_extents);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002695 return ret;
2696}
2697
Josef Bacikfcebe452014-05-13 17:30:47 -07002698/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002699 * called from commit_transaction. Writes all changed qgroups to disk.
2700 */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08002701int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002702{
Lu Fengqi280f8bd2018-07-18 14:45:40 +08002703 struct btrfs_fs_info *fs_info = trans->fs_info;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002704 int ret = 0;
2705
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03002706 if (!fs_info->quota_root)
Nikolay Borisov5d235152018-01-31 10:52:04 +02002707 return ret;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002708
2709 spin_lock(&fs_info->qgroup_lock);
2710 while (!list_empty(&fs_info->dirty_qgroups)) {
2711 struct btrfs_qgroup *qgroup;
2712 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2713 struct btrfs_qgroup, dirty);
2714 list_del_init(&qgroup->dirty);
2715 spin_unlock(&fs_info->qgroup_lock);
Lu Fengqi3e07e9a2018-07-18 14:45:28 +08002716 ret = update_qgroup_info_item(trans, qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002717 if (ret)
2718 fs_info->qgroup_flags |=
2719 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Lu Fengqiac8a8662018-07-18 14:45:27 +08002720 ret = update_qgroup_limit_item(trans, qgroup);
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05002721 if (ret)
2722 fs_info->qgroup_flags |=
2723 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002724 spin_lock(&fs_info->qgroup_lock);
2725 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002726 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02002727 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2728 else
2729 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2730 spin_unlock(&fs_info->qgroup_lock);
2731
Lu Fengqi2e980ac2018-07-18 14:45:29 +08002732 ret = update_qgroup_status_item(trans);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002733 if (ret)
2734 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2735
Arne Jansenbed92ea2012-06-28 18:03:02 +02002736 return ret;
2737}
2738
2739/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002740 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002741 * when a snapshot or a subvolume is created. Throwing an error will
2742 * cause a transaction abort so we take extra care here to only error
2743 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02002744 */
Lu Fengqia93774222018-07-18 14:45:41 +08002745int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
2746 u64 objectid, struct btrfs_qgroup_inherit *inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002747{
2748 int ret = 0;
2749 int i;
2750 u64 *i_qgroups;
Qu Wenruoe88439d2019-06-13 17:31:24 +08002751 bool committing = false;
Lu Fengqia93774222018-07-18 14:45:41 +08002752 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana552f0322018-11-19 16:20:34 +00002753 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002754 struct btrfs_qgroup *srcgroup;
2755 struct btrfs_qgroup *dstgroup;
Qu Wenruocbab8ad2020-04-02 14:37:35 +08002756 bool need_rescan = false;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002757 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002758 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002759
Qu Wenruoe88439d2019-06-13 17:31:24 +08002760 /*
2761 * There are only two callers of this function.
2762 *
2763 * One in create_subvol() in the ioctl context, which needs to hold
2764 * the qgroup_ioctl_lock.
2765 *
2766 * The other one in create_pending_snapshot() where no other qgroup
2767 * code can modify the fs as they all need to either start a new trans
2768 * or hold a trans handler, thus we don't need to hold
2769 * qgroup_ioctl_lock.
2770 * This would avoid long and complex lock chain and make lockdep happy.
2771 */
2772 spin_lock(&fs_info->trans_lock);
2773 if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
2774 committing = true;
2775 spin_unlock(&fs_info->trans_lock);
2776
2777 if (!committing)
2778 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04002779 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002780 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002781
Filipe Manana552f0322018-11-19 16:20:34 +00002782 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002783 if (!quota_root) {
2784 ret = -EINVAL;
2785 goto out;
2786 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002787
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002788 if (inherit) {
2789 i_qgroups = (u64 *)(inherit + 1);
2790 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2791 2 * inherit->num_excl_copies;
2792 for (i = 0; i < nums; ++i) {
2793 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05002794
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002795 /*
2796 * Zero out invalid groups so we can ignore
2797 * them later.
2798 */
2799 if (!srcgroup ||
2800 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2801 *i_qgroups = 0ULL;
2802
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002803 ++i_qgroups;
2804 }
2805 }
2806
Arne Jansenbed92ea2012-06-28 18:03:02 +02002807 /*
2808 * create a tracking group for the subvol itself
2809 */
2810 ret = add_qgroup_item(trans, quota_root, objectid);
2811 if (ret)
2812 goto out;
2813
Arne Jansenbed92ea2012-06-28 18:03:02 +02002814 /*
2815 * add qgroup to all inherited groups
2816 */
2817 if (inherit) {
2818 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002819 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2820 if (*i_qgroups == 0)
2821 continue;
Lu Fengqi711169c2018-07-18 14:45:24 +08002822 ret = add_qgroup_relation_item(trans, objectid,
2823 *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002824 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002825 goto out;
Lu Fengqi711169c2018-07-18 14:45:24 +08002826 ret = add_qgroup_relation_item(trans, *i_qgroups,
2827 objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002828 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002829 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002830 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002831 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002832 }
2833
2834
2835 spin_lock(&fs_info->qgroup_lock);
2836
2837 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002838 if (IS_ERR(dstgroup)) {
2839 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002840 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002841 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002842
Dongsheng Yange8c85412014-11-20 20:58:34 -05002843 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05002844 dstgroup->lim_flags = inherit->lim.flags;
2845 dstgroup->max_rfer = inherit->lim.max_rfer;
2846 dstgroup->max_excl = inherit->lim.max_excl;
2847 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2848 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05002849
Lu Fengqiac8a8662018-07-18 14:45:27 +08002850 ret = update_qgroup_limit_item(trans, dstgroup);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002851 if (ret) {
2852 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002853 btrfs_info(fs_info,
2854 "unable to update quota limit for %llu",
2855 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002856 goto unlock;
2857 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002858 }
2859
Arne Jansenbed92ea2012-06-28 18:03:02 +02002860 if (srcid) {
2861 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002862 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002863 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002864
2865 /*
2866 * We call inherit after we clone the root in order to make sure
2867 * our counts don't go crazy, so at this point the only
2868 * difference between the two roots should be the root node.
2869 */
Lu Fengqic8389d42018-07-17 16:58:22 +08002870 level_size = fs_info->nodesize;
Josef Bacikfcebe452014-05-13 17:30:47 -07002871 dstgroup->rfer = srcgroup->rfer;
2872 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2873 dstgroup->excl = level_size;
2874 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002875 srcgroup->excl = level_size;
2876 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002877
2878 /* inherit the limit info */
2879 dstgroup->lim_flags = srcgroup->lim_flags;
2880 dstgroup->max_rfer = srcgroup->max_rfer;
2881 dstgroup->max_excl = srcgroup->max_excl;
2882 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2883 dstgroup->rsv_excl = srcgroup->rsv_excl;
2884
Arne Jansenbed92ea2012-06-28 18:03:02 +02002885 qgroup_dirty(fs_info, dstgroup);
2886 qgroup_dirty(fs_info, srcgroup);
2887 }
2888
Chris Masonf3a87f12012-09-14 20:06:30 -04002889 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002890 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002891
2892 i_qgroups = (u64 *)(inherit + 1);
2893 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002894 if (*i_qgroups) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002895 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002896 if (ret)
2897 goto unlock;
2898 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002899 ++i_qgroups;
Qu Wenruocbab8ad2020-04-02 14:37:35 +08002900
2901 /*
2902 * If we're doing a snapshot, and adding the snapshot to a new
2903 * qgroup, the numbers are guaranteed to be incorrect.
2904 */
2905 if (srcid)
2906 need_rescan = true;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002907 }
2908
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002909 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002910 struct btrfs_qgroup *src;
2911 struct btrfs_qgroup *dst;
2912
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002913 if (!i_qgroups[0] || !i_qgroups[1])
2914 continue;
2915
Arne Jansenbed92ea2012-06-28 18:03:02 +02002916 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2917 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2918
2919 if (!src || !dst) {
2920 ret = -EINVAL;
2921 goto unlock;
2922 }
2923
2924 dst->rfer = src->rfer - level_size;
2925 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Qu Wenruocbab8ad2020-04-02 14:37:35 +08002926
2927 /* Manually tweaking numbers certainly needs a rescan */
2928 need_rescan = true;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002929 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002930 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002931 struct btrfs_qgroup *src;
2932 struct btrfs_qgroup *dst;
2933
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002934 if (!i_qgroups[0] || !i_qgroups[1])
2935 continue;
2936
Arne Jansenbed92ea2012-06-28 18:03:02 +02002937 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2938 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2939
2940 if (!src || !dst) {
2941 ret = -EINVAL;
2942 goto unlock;
2943 }
2944
2945 dst->excl = src->excl + level_size;
2946 dst->excl_cmpr = src->excl_cmpr + level_size;
Qu Wenruocbab8ad2020-04-02 14:37:35 +08002947 need_rescan = true;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002948 }
2949
2950unlock:
2951 spin_unlock(&fs_info->qgroup_lock);
Qu Wenruo49e5fb42020-06-28 13:07:15 +08002952 if (!ret)
2953 ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002954out:
Qu Wenruoe88439d2019-06-13 17:31:24 +08002955 if (!committing)
2956 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruocbab8ad2020-04-02 14:37:35 +08002957 if (need_rescan)
2958 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002959 return ret;
2960}
2961
Qu Wenruoadca4d92020-07-13 18:50:49 +08002962static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002963{
2964 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002965 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002966 return false;
2967
2968 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002969 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002970 return false;
2971
2972 return true;
2973}
2974
Qu Wenruodba21322017-12-12 15:34:25 +08002975static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2976 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002977{
Arne Jansenbed92ea2012-06-28 18:03:02 +02002978 struct btrfs_qgroup *qgroup;
2979 struct btrfs_fs_info *fs_info = root->fs_info;
2980 u64 ref_root = root->root_key.objectid;
2981 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002982 struct ulist_node *unode;
2983 struct ulist_iterator uiter;
2984
2985 if (!is_fstree(ref_root))
2986 return 0;
2987
2988 if (num_bytes == 0)
2989 return 0;
Sargun Dhillonf29efe22017-05-11 21:17:33 +00002990
2991 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2992 capable(CAP_SYS_RESOURCE))
2993 enforce = false;
2994
Arne Jansenbed92ea2012-06-28 18:03:02 +02002995 spin_lock(&fs_info->qgroup_lock);
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03002996 if (!fs_info->quota_root)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002997 goto out;
2998
2999 qgroup = find_qgroup_rb(fs_info, ref_root);
3000 if (!qgroup)
3001 goto out;
3002
3003 /*
3004 * in a first step, we check all affected qgroups if any limits would
3005 * be exceeded
3006 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00003007 ulist_reinit(fs_info->qgroup_ulist);
3008 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003009 qgroup_to_aux(qgroup), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00003010 if (ret < 0)
3011 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003012 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00003013 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02003014 struct btrfs_qgroup *qg;
3015 struct btrfs_qgroup_list *glist;
3016
David Sterbaef2fff62016-10-26 16:23:50 +02003017 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003018
Qu Wenruoadca4d92020-07-13 18:50:49 +08003019 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02003020 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00003021 goto out;
3022 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02003023
3024 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00003025 ret = ulist_add(fs_info->qgroup_ulist,
3026 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003027 qgroup_to_aux(glist->group), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00003028 if (ret < 0)
3029 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003030 }
3031 }
Wang Shilong3c971852013-04-17 14:00:36 +00003032 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003033 /*
3034 * no limits exceeded, now record the reservation into all qgroups
3035 */
3036 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00003037 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02003038 struct btrfs_qgroup *qg;
3039
David Sterbaef2fff62016-10-26 16:23:50 +02003040 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003041
Qu Wenruo64ee4e72017-12-12 15:34:27 +08003042 qgroup_rsv_add(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003043 }
3044
3045out:
3046 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003047 return ret;
3048}
3049
Qu Wenruoe1211d02017-12-12 15:34:30 +08003050/*
3051 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
3052 * qgroup).
3053 *
3054 * Will handle all higher level qgroup too.
3055 *
3056 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
3057 * This special case is only used for META_PERTRANS type.
3058 */
Qu Wenruo297d7502015-09-08 17:08:37 +08003059void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003060 u64 ref_root, u64 num_bytes,
3061 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02003062{
Arne Jansenbed92ea2012-06-28 18:03:02 +02003063 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003064 struct ulist_node *unode;
3065 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00003066 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003067
3068 if (!is_fstree(ref_root))
3069 return;
3070
3071 if (num_bytes == 0)
3072 return;
3073
Qu Wenruoe1211d02017-12-12 15:34:30 +08003074 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
3075 WARN(1, "%s: Invalid type to free", __func__);
3076 return;
3077 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02003078 spin_lock(&fs_info->qgroup_lock);
3079
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03003080 if (!fs_info->quota_root)
Arne Jansenbed92ea2012-06-28 18:03:02 +02003081 goto out;
3082
3083 qgroup = find_qgroup_rb(fs_info, ref_root);
3084 if (!qgroup)
3085 goto out;
3086
Qu Wenruoe1211d02017-12-12 15:34:30 +08003087 if (num_bytes == (u64)-1)
Qu Wenruo82874752017-12-12 15:34:34 +08003088 /*
3089 * We're freeing all pertrans rsv, get reserved value from
3090 * level 0 qgroup as real num_bytes to free.
3091 */
Qu Wenruoe1211d02017-12-12 15:34:30 +08003092 num_bytes = qgroup->rsv.values[type];
3093
Wang Shilong1e8f9152013-05-06 11:03:27 +00003094 ulist_reinit(fs_info->qgroup_ulist);
3095 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003096 qgroup_to_aux(qgroup), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00003097 if (ret < 0)
3098 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003099 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00003100 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02003101 struct btrfs_qgroup *qg;
3102 struct btrfs_qgroup_list *glist;
3103
David Sterbaef2fff62016-10-26 16:23:50 +02003104 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003105
Qu Wenruo64ee4e72017-12-12 15:34:27 +08003106 qgroup_rsv_release(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003107
3108 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00003109 ret = ulist_add(fs_info->qgroup_ulist,
3110 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003111 qgroup_to_aux(glist->group), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00003112 if (ret < 0)
3113 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02003114 }
3115 }
3116
3117out:
3118 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02003119}
3120
Jan Schmidt2f232032013-04-25 16:04:51 +00003121/*
Qu Wenruoff3d27a02018-05-14 09:38:13 +08003122 * Check if the leaf is the last leaf. Which means all node pointers
3123 * are at their last position.
3124 */
3125static bool is_last_leaf(struct btrfs_path *path)
3126{
3127 int i;
3128
3129 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
3130 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
3131 return false;
3132 }
3133 return true;
3134}
3135
3136/*
Jan Schmidt2f232032013-04-25 16:04:51 +00003137 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08003138 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00003139 */
Lu Fengqi62088ca2018-07-18 14:45:42 +08003140static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
3141 struct btrfs_path *path)
Jan Schmidt2f232032013-04-25 16:04:51 +00003142{
Lu Fengqi62088ca2018-07-18 14:45:42 +08003143 struct btrfs_fs_info *fs_info = trans->fs_info;
Jan Schmidt2f232032013-04-25 16:04:51 +00003144 struct btrfs_key found;
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08003145 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00003146 struct ulist *roots = NULL;
Josef Bacikfcebe452014-05-13 17:30:47 -07003147 u64 num_bytes;
Qu Wenruoff3d27a02018-05-14 09:38:13 +08003148 bool done;
Jan Schmidt2f232032013-04-25 16:04:51 +00003149 int slot;
3150 int ret;
3151
Jan Schmidt2f232032013-04-25 16:04:51 +00003152 mutex_lock(&fs_info->qgroup_rescan_lock);
3153 ret = btrfs_search_slot_for_read(fs_info->extent_root,
3154 &fs_info->qgroup_rescan_progress,
3155 path, 1, 0);
3156
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04003157 btrfs_debug(fs_info,
3158 "current progress key (%llu %u %llu), search_slot ret %d",
3159 fs_info->qgroup_rescan_progress.objectid,
3160 fs_info->qgroup_rescan_progress.type,
3161 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00003162
3163 if (ret) {
3164 /*
3165 * The rescan is about to end, we will not be scanning any
3166 * further blocks. We cannot unset the RESCAN flag here, because
3167 * we want to commit the transaction if everything went well.
3168 * To make the live accounting work in this phase, we set our
3169 * scan progress pointer such that every real extent objectid
3170 * will be smaller.
3171 */
3172 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3173 btrfs_release_path(path);
3174 mutex_unlock(&fs_info->qgroup_rescan_lock);
3175 return ret;
3176 }
Qu Wenruoff3d27a02018-05-14 09:38:13 +08003177 done = is_last_leaf(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00003178
3179 btrfs_item_key_to_cpu(path->nodes[0], &found,
3180 btrfs_header_nritems(path->nodes[0]) - 1);
3181 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
3182
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08003183 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
3184 if (!scratch_leaf) {
3185 ret = -ENOMEM;
3186 mutex_unlock(&fs_info->qgroup_rescan_lock);
3187 goto out;
3188 }
Jan Schmidt2f232032013-04-25 16:04:51 +00003189 slot = path->slots[0];
3190 btrfs_release_path(path);
3191 mutex_unlock(&fs_info->qgroup_rescan_lock);
3192
3193 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
3194 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05003195 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
3196 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00003197 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05003198 if (found.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04003199 num_bytes = fs_info->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05003200 else
3201 num_bytes = found.offset;
3202
Josef Bacikfcebe452014-05-13 17:30:47 -07003203 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04003204 &roots, false);
Jan Schmidt2f232032013-04-25 16:04:51 +00003205 if (ret < 0)
3206 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08003207 /* For rescan, just pass old_roots as NULL */
Lu Fengqi8696d762018-07-18 14:45:39 +08003208 ret = btrfs_qgroup_account_extent(trans, found.objectid,
3209 num_bytes, NULL, roots);
Qu Wenruo9d220c92015-04-13 11:02:16 +08003210 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00003211 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00003212 }
Jan Schmidt2f232032013-04-25 16:04:51 +00003213out:
Nikolay Borisovdf449712018-08-15 18:26:56 +03003214 if (scratch_leaf)
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08003215 free_extent_buffer(scratch_leaf);
Jan Schmidt2f232032013-04-25 16:04:51 +00003216
Qu Wenruo6f7de192018-06-27 18:19:55 +08003217 if (done && !ret) {
Qu Wenruoff3d27a02018-05-14 09:38:13 +08003218 ret = 1;
Qu Wenruo6f7de192018-06-27 18:19:55 +08003219 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3220 }
Jan Schmidt2f232032013-04-25 16:04:51 +00003221 return ret;
3222}
3223
Qu Wenruod458b052014-02-28 10:46:19 +08003224static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00003225{
Jan Schmidtb382a322013-05-28 15:47:24 +00003226 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
3227 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00003228 struct btrfs_path *path;
3229 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00003230 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003231 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00003232
3233 path = btrfs_alloc_path();
3234 if (!path)
3235 goto out;
Qu Wenruob6debf12018-05-14 09:38:12 +08003236 /*
3237 * Rescan should only search for commit root, and any later difference
3238 * should be recorded by qgroup
3239 */
3240 path->search_commit_root = 1;
3241 path->skip_locking = 1;
Jan Schmidt2f232032013-04-25 16:04:51 +00003242
3243 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08003244 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00003245 trans = btrfs_start_transaction(fs_info->fs_root, 0);
3246 if (IS_ERR(trans)) {
3247 err = PTR_ERR(trans);
3248 break;
3249 }
Josef Bacikafcdd122016-09-02 15:40:02 -04003250 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00003251 err = -EINTR;
3252 } else {
Lu Fengqi62088ca2018-07-18 14:45:42 +08003253 err = qgroup_rescan_leaf(trans, path);
Jan Schmidt2f232032013-04-25 16:04:51 +00003254 }
3255 if (err > 0)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003256 btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00003257 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003258 btrfs_end_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00003259 }
3260
3261out:
Jan Schmidt2f232032013-04-25 16:04:51 +00003262 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00003263
3264 mutex_lock(&fs_info->qgroup_rescan_lock);
Qu Wenruo33931682015-02-27 16:24:24 +08003265 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00003266 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3267 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3268 } else if (err < 0) {
3269 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3270 }
3271 mutex_unlock(&fs_info->qgroup_rescan_lock);
3272
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003273 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003274 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003275 * qgroup info.
3276 */
3277 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3278 if (IS_ERR(trans)) {
3279 err = PTR_ERR(trans);
Filipe Manana13fc1d22019-09-24 10:49:54 +01003280 trans = NULL;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003281 btrfs_err(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02003282 "fail to start transaction for status update: %d",
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003283 err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003284 }
Filipe Manana13fc1d22019-09-24 10:49:54 +01003285
3286 mutex_lock(&fs_info->qgroup_rescan_lock);
3287 if (!btrfs_fs_closing(fs_info))
3288 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3289 if (trans) {
3290 ret = update_qgroup_status_item(trans);
3291 if (ret < 0) {
3292 err = ret;
3293 btrfs_err(fs_info, "fail to update qgroup status: %d",
3294 err);
3295 }
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003296 }
Filipe Manana13fc1d22019-09-24 10:49:54 +01003297 fs_info->qgroup_rescan_running = false;
3298 complete_all(&fs_info->qgroup_rescan_completion);
3299 mutex_unlock(&fs_info->qgroup_rescan_lock);
3300
3301 if (!trans)
3302 return;
3303
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003304 btrfs_end_transaction(trans);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08003305
Justin Maggard7343dd62015-11-04 15:56:16 -08003306 if (btrfs_fs_closing(fs_info)) {
3307 btrfs_info(fs_info, "qgroup scan paused");
3308 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003309 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08003310 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00003311 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003312 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00003313 }
3314}
3315
Jan Schmidtb382a322013-05-28 15:47:24 +00003316/*
3317 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3318 * memory required for the rescan context.
3319 */
3320static int
3321qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3322 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00003323{
3324 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00003325
Qu Wenruo9593bf492018-05-02 13:28:03 +08003326 if (!init_flags) {
3327 /* we're resuming qgroup rescan at mount time */
Filipe Mananae4e7ede2018-06-27 00:43:15 +01003328 if (!(fs_info->qgroup_flags &
3329 BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
Qu Wenruo9593bf492018-05-02 13:28:03 +08003330 btrfs_warn(fs_info,
Nikolay Borisov37d02592019-11-18 14:16:44 +02003331 "qgroup rescan init failed, qgroup rescan is not queued");
Filipe Mananae4e7ede2018-06-27 00:43:15 +01003332 ret = -EINVAL;
3333 } else if (!(fs_info->qgroup_flags &
3334 BTRFS_QGROUP_STATUS_FLAG_ON)) {
Qu Wenruo9593bf492018-05-02 13:28:03 +08003335 btrfs_warn(fs_info,
Nikolay Borisov37d02592019-11-18 14:16:44 +02003336 "qgroup rescan init failed, qgroup is not enabled");
Filipe Mananae4e7ede2018-06-27 00:43:15 +01003337 ret = -EINVAL;
3338 }
3339
3340 if (ret)
3341 return ret;
Jan Schmidtb382a322013-05-28 15:47:24 +00003342 }
Jan Schmidt2f232032013-04-25 16:04:51 +00003343
3344 mutex_lock(&fs_info->qgroup_rescan_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00003345
3346 if (init_flags) {
Qu Wenruo9593bf492018-05-02 13:28:03 +08003347 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3348 btrfs_warn(fs_info,
3349 "qgroup rescan is already in progress");
Jan Schmidtb382a322013-05-28 15:47:24 +00003350 ret = -EINPROGRESS;
Qu Wenruo9593bf492018-05-02 13:28:03 +08003351 } else if (!(fs_info->qgroup_flags &
3352 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3353 btrfs_warn(fs_info,
3354 "qgroup rescan init failed, qgroup is not enabled");
Jan Schmidtb382a322013-05-28 15:47:24 +00003355 ret = -EINVAL;
Qu Wenruo9593bf492018-05-02 13:28:03 +08003356 }
Jan Schmidtb382a322013-05-28 15:47:24 +00003357
3358 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00003359 mutex_unlock(&fs_info->qgroup_rescan_lock);
Qu Wenruo9593bf492018-05-02 13:28:03 +08003360 return ret;
Jan Schmidtb382a322013-05-28 15:47:24 +00003361 }
Jan Schmidtb382a322013-05-28 15:47:24 +00003362 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3363 }
3364
3365 memset(&fs_info->qgroup_rescan_progress, 0,
3366 sizeof(fs_info->qgroup_rescan_progress));
3367 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00003368 init_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidtb382a322013-05-28 15:47:24 +00003369 mutex_unlock(&fs_info->qgroup_rescan_lock);
3370
Qu Wenruofc97fab2014-02-28 10:46:16 +08003371 btrfs_init_work(&fs_info->qgroup_rescan_work,
3372 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00003373 return 0;
3374}
Jan Schmidt2f232032013-04-25 16:04:51 +00003375
Jan Schmidtb382a322013-05-28 15:47:24 +00003376static void
3377qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3378{
3379 struct rb_node *n;
3380 struct btrfs_qgroup *qgroup;
3381
3382 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00003383 /* clear all current qgroup tracking information */
3384 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3385 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3386 qgroup->rfer = 0;
3387 qgroup->rfer_cmpr = 0;
3388 qgroup->excl = 0;
3389 qgroup->excl_cmpr = 0;
Qu Wenruo9c7b0c22018-08-10 10:20:26 +08003390 qgroup_dirty(fs_info, qgroup);
Jan Schmidt2f232032013-04-25 16:04:51 +00003391 }
3392 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00003393}
Jan Schmidt2f232032013-04-25 16:04:51 +00003394
Jan Schmidtb382a322013-05-28 15:47:24 +00003395int
3396btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3397{
3398 int ret = 0;
3399 struct btrfs_trans_handle *trans;
3400
3401 ret = qgroup_rescan_init(fs_info, 0, 1);
3402 if (ret)
3403 return ret;
3404
3405 /*
3406 * We have set the rescan_progress to 0, which means no more
3407 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3408 * However, btrfs_qgroup_account_ref may be right after its call
3409 * to btrfs_find_all_roots, in which case it would still do the
3410 * accounting.
3411 * To solve this, we're committing the transaction, which will
3412 * ensure we run all delayed refs and only after that, we are
3413 * going to clear all tracking information for a clean start.
3414 */
3415
3416 trans = btrfs_join_transaction(fs_info->fs_root);
3417 if (IS_ERR(trans)) {
3418 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3419 return PTR_ERR(trans);
3420 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003421 ret = btrfs_commit_transaction(trans);
Jan Schmidtb382a322013-05-28 15:47:24 +00003422 if (ret) {
3423 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3424 return ret;
3425 }
3426
3427 qgroup_rescan_zero_tracking(fs_info);
3428
Qu Wenruod61acbb2020-02-07 13:38:20 +08003429 mutex_lock(&fs_info->qgroup_rescan_lock);
3430 fs_info->qgroup_rescan_running = true;
Qu Wenruofc97fab2014-02-28 10:46:16 +08003431 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3432 &fs_info->qgroup_rescan_work);
Qu Wenruod61acbb2020-02-07 13:38:20 +08003433 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00003434
3435 return 0;
3436}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00003437
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04003438int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
3439 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00003440{
3441 int running;
3442 int ret = 0;
3443
3444 mutex_lock(&fs_info->qgroup_rescan_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04003445 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00003446 mutex_unlock(&fs_info->qgroup_rescan_lock);
3447
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04003448 if (!running)
3449 return 0;
3450
3451 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00003452 ret = wait_for_completion_interruptible(
3453 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04003454 else
3455 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00003456
3457 return ret;
3458}
Jan Schmidtb382a322013-05-28 15:47:24 +00003459
3460/*
3461 * this is only called from open_ctree where we're still single threaded, thus
3462 * locking is omitted here.
3463 */
3464void
3465btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3466{
Qu Wenruod61acbb2020-02-07 13:38:20 +08003467 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3468 mutex_lock(&fs_info->qgroup_rescan_lock);
3469 fs_info->qgroup_rescan_running = true;
Qu Wenruofc97fab2014-02-28 10:46:16 +08003470 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3471 &fs_info->qgroup_rescan_work);
Qu Wenruod61acbb2020-02-07 13:38:20 +08003472 mutex_unlock(&fs_info->qgroup_rescan_lock);
3473 }
Jan Schmidtb382a322013-05-28 15:47:24 +00003474}
Qu Wenruo52472552015-10-12 16:05:40 +08003475
Qu Wenruo263da812020-07-08 14:24:45 +08003476#define rbtree_iterate_from_safe(node, next, start) \
3477 for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
3478
3479static int qgroup_unreserve_range(struct btrfs_inode *inode,
3480 struct extent_changeset *reserved, u64 start,
3481 u64 len)
3482{
3483 struct rb_node *node;
3484 struct rb_node *next;
Dan Carpenterf07728d2020-10-23 14:26:33 +03003485 struct ulist_node *entry;
Qu Wenruo263da812020-07-08 14:24:45 +08003486 int ret = 0;
3487
3488 node = reserved->range_changed.root.rb_node;
Dan Carpenterf07728d2020-10-23 14:26:33 +03003489 if (!node)
3490 return 0;
Qu Wenruo263da812020-07-08 14:24:45 +08003491 while (node) {
3492 entry = rb_entry(node, struct ulist_node, rb_node);
3493 if (entry->val < start)
3494 node = node->rb_right;
Qu Wenruo263da812020-07-08 14:24:45 +08003495 else
Dan Carpenterf07728d2020-10-23 14:26:33 +03003496 node = node->rb_left;
Qu Wenruo263da812020-07-08 14:24:45 +08003497 }
3498
Qu Wenruo263da812020-07-08 14:24:45 +08003499 if (entry->val > start && rb_prev(&entry->rb_node))
3500 entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
3501 rb_node);
3502
3503 rbtree_iterate_from_safe(node, next, &entry->rb_node) {
3504 u64 entry_start;
3505 u64 entry_end;
3506 u64 entry_len;
3507 int clear_ret;
3508
3509 entry = rb_entry(node, struct ulist_node, rb_node);
3510 entry_start = entry->val;
3511 entry_end = entry->aux;
3512 entry_len = entry_end - entry_start + 1;
3513
3514 if (entry_start >= start + len)
3515 break;
3516 if (entry_start + entry_len <= start)
3517 continue;
3518 /*
3519 * Now the entry is in [start, start + len), revert the
3520 * EXTENT_QGROUP_RESERVED bit.
3521 */
3522 clear_ret = clear_extent_bits(&inode->io_tree, entry_start,
3523 entry_end, EXTENT_QGROUP_RESERVED);
3524 if (!ret && clear_ret < 0)
3525 ret = clear_ret;
3526
3527 ulist_del(&reserved->range_changed, entry->val, entry->aux);
3528 if (likely(reserved->bytes_changed >= entry_len)) {
3529 reserved->bytes_changed -= entry_len;
3530 } else {
3531 WARN_ON(1);
3532 reserved->bytes_changed = 0;
3533 }
3534 }
3535
3536 return ret;
3537}
3538
Qu Wenruo52472552015-10-12 16:05:40 +08003539/*
Qu Wenruoc53e9652020-07-13 18:50:48 +08003540 * Try to free some space for qgroup.
Qu Wenruo52472552015-10-12 16:05:40 +08003541 *
Qu Wenruoc53e9652020-07-13 18:50:48 +08003542 * For qgroup, there are only 3 ways to free qgroup space:
3543 * - Flush nodatacow write
3544 * Any nodatacow write will free its reserved data space at run_delalloc_range().
3545 * In theory, we should only flush nodatacow inodes, but it's not yet
3546 * possible, so we need to flush the whole root.
Qu Wenruo52472552015-10-12 16:05:40 +08003547 *
Qu Wenruoc53e9652020-07-13 18:50:48 +08003548 * - Wait for ordered extents
3549 * When ordered extents are finished, their reserved metadata is finally
3550 * converted to per_trans status, which can be freed by later commit
3551 * transaction.
Qu Wenruo52472552015-10-12 16:05:40 +08003552 *
Qu Wenruoc53e9652020-07-13 18:50:48 +08003553 * - Commit transaction
3554 * This would free the meta_per_trans space.
3555 * In theory this shouldn't provide much space, but any more qgroup space
3556 * is needed.
Qu Wenruo52472552015-10-12 16:05:40 +08003557 */
Qu Wenruoc53e9652020-07-13 18:50:48 +08003558static int try_flush_qgroup(struct btrfs_root *root)
3559{
3560 struct btrfs_trans_handle *trans;
3561 int ret;
Qu Wenruo6f232772020-11-11 19:38:18 +08003562 bool can_commit = true;
Qu Wenruoc53e9652020-07-13 18:50:48 +08003563
3564 /*
3565 * We don't want to run flush again and again, so if there is a running
3566 * one, we won't try to start a new flush, but exit directly.
3567 */
3568 if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
3569 wait_event(root->qgroup_flush_wait,
3570 !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
3571 return 0;
3572 }
3573
Qu Wenruo6f232772020-11-11 19:38:18 +08003574 /*
3575 * If current process holds a transaction, we shouldn't flush, as we
3576 * assume all space reservation happens before a transaction handle is
3577 * held.
3578 *
3579 * But there are cases like btrfs_delayed_item_reserve_metadata() where
3580 * we try to reserve space with one transction handle already held.
3581 * In that case we can't commit transaction, but at least try to end it
3582 * and hope the started data writes can free some space.
3583 */
3584 if (current->journal_info &&
3585 current->journal_info != BTRFS_SEND_TRANS_STUB)
3586 can_commit = false;
3587
Qu Wenruoc53e9652020-07-13 18:50:48 +08003588 ret = btrfs_start_delalloc_snapshot(root);
3589 if (ret < 0)
3590 goto out;
3591 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
3592
3593 trans = btrfs_join_transaction(root);
3594 if (IS_ERR(trans)) {
3595 ret = PTR_ERR(trans);
3596 goto out;
3597 }
3598
Qu Wenruo6f232772020-11-11 19:38:18 +08003599 if (can_commit)
3600 ret = btrfs_commit_transaction(trans);
3601 else
3602 ret = btrfs_end_transaction(trans);
Qu Wenruoc53e9652020-07-13 18:50:48 +08003603out:
3604 clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
3605 wake_up(&root->qgroup_flush_wait);
3606 return ret;
3607}
3608
3609static int qgroup_reserve_data(struct btrfs_inode *inode,
Qu Wenruo364ecf32017-02-27 15:10:38 +08003610 struct extent_changeset **reserved_ret, u64 start,
3611 u64 len)
Qu Wenruo52472552015-10-12 16:05:40 +08003612{
Nikolay Borisov7661a3e2020-06-03 08:55:37 +03003613 struct btrfs_root *root = inode->root;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003614 struct extent_changeset *reserved;
Qu Wenruo263da812020-07-08 14:24:45 +08003615 bool new_reserved = false;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003616 u64 orig_reserved;
3617 u64 to_reserve;
Qu Wenruo52472552015-10-12 16:05:40 +08003618 int ret;
3619
Josef Bacikafcdd122016-09-02 15:40:02 -04003620 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003621 !is_fstree(root->root_key.objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08003622 return 0;
3623
Qu Wenruo364ecf32017-02-27 15:10:38 +08003624 /* @reserved parameter is mandatory for qgroup */
3625 if (WARN_ON(!reserved_ret))
3626 return -EINVAL;
3627 if (!*reserved_ret) {
Qu Wenruo263da812020-07-08 14:24:45 +08003628 new_reserved = true;
Qu Wenruo364ecf32017-02-27 15:10:38 +08003629 *reserved_ret = extent_changeset_alloc();
3630 if (!*reserved_ret)
3631 return -ENOMEM;
3632 }
3633 reserved = *reserved_ret;
3634 /* Record already reserved space */
3635 orig_reserved = reserved->bytes_changed;
Nikolay Borisov7661a3e2020-06-03 08:55:37 +03003636 ret = set_record_extent_bits(&inode->io_tree, start,
Qu Wenruo364ecf32017-02-27 15:10:38 +08003637 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
3638
3639 /* Newly reserved space */
3640 to_reserve = reserved->bytes_changed - orig_reserved;
Nikolay Borisov7661a3e2020-06-03 08:55:37 +03003641 trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
Qu Wenruo364ecf32017-02-27 15:10:38 +08003642 to_reserve, QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08003643 if (ret < 0)
Qu Wenruo263da812020-07-08 14:24:45 +08003644 goto out;
Qu Wenruodba21322017-12-12 15:34:25 +08003645 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo52472552015-10-12 16:05:40 +08003646 if (ret < 0)
3647 goto cleanup;
3648
Qu Wenruo52472552015-10-12 16:05:40 +08003649 return ret;
3650
3651cleanup:
Qu Wenruo263da812020-07-08 14:24:45 +08003652 qgroup_unreserve_range(inode, reserved, start, len);
3653out:
3654 if (new_reserved) {
3655 extent_changeset_release(reserved);
3656 kfree(reserved);
3657 *reserved_ret = NULL;
3658 }
Qu Wenruo52472552015-10-12 16:05:40 +08003659 return ret;
3660}
Qu Wenruof695fdc2015-10-12 16:28:06 +08003661
Qu Wenruoc53e9652020-07-13 18:50:48 +08003662/*
3663 * Reserve qgroup space for range [start, start + len).
3664 *
3665 * This function will either reserve space from related qgroups or do nothing
3666 * if the range is already reserved.
3667 *
3668 * Return 0 for successful reservation
3669 * Return <0 for error (including -EQUOT)
3670 *
3671 * NOTE: This function may sleep for memory allocation, dirty page flushing and
3672 * commit transaction. So caller should not hold any dirty page locked.
3673 */
3674int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
3675 struct extent_changeset **reserved_ret, u64 start,
3676 u64 len)
3677{
3678 int ret;
3679
3680 ret = qgroup_reserve_data(inode, reserved_ret, start, len);
3681 if (ret <= 0 && ret != -EDQUOT)
3682 return ret;
3683
3684 ret = try_flush_qgroup(inode->root);
3685 if (ret < 0)
3686 return ret;
3687 return qgroup_reserve_data(inode, reserved_ret, start, len);
3688}
3689
Qu Wenruobc42bda2017-02-27 15:10:39 +08003690/* Free ranges specified by @reserved, normally in error path */
Nikolay Borisovdf2cfd12020-06-03 08:55:09 +03003691static int qgroup_free_reserved_data(struct btrfs_inode *inode,
Qu Wenruobc42bda2017-02-27 15:10:39 +08003692 struct extent_changeset *reserved, u64 start, u64 len)
3693{
Nikolay Borisovdf2cfd12020-06-03 08:55:09 +03003694 struct btrfs_root *root = inode->root;
Qu Wenruobc42bda2017-02-27 15:10:39 +08003695 struct ulist_node *unode;
3696 struct ulist_iterator uiter;
3697 struct extent_changeset changeset;
3698 int freed = 0;
3699 int ret;
3700
3701 extent_changeset_init(&changeset);
3702 len = round_up(start + len, root->fs_info->sectorsize);
3703 start = round_down(start, root->fs_info->sectorsize);
3704
3705 ULIST_ITER_INIT(&uiter);
3706 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
3707 u64 range_start = unode->val;
3708 /* unode->aux is the inclusive end */
3709 u64 range_len = unode->aux - range_start + 1;
3710 u64 free_start;
3711 u64 free_len;
3712
3713 extent_changeset_release(&changeset);
3714
3715 /* Only free range in range [start, start + len) */
3716 if (range_start >= start + len ||
3717 range_start + range_len <= start)
3718 continue;
3719 free_start = max(range_start, start);
3720 free_len = min(start + len, range_start + range_len) -
3721 free_start;
3722 /*
3723 * TODO: To also modify reserved->ranges_reserved to reflect
3724 * the modification.
3725 *
3726 * However as long as we free qgroup reserved according to
3727 * EXTENT_QGROUP_RESERVED, we won't double free.
3728 * So not need to rush.
3729 */
Nikolay Borisovdf2cfd12020-06-03 08:55:09 +03003730 ret = clear_record_extent_bits(&inode->io_tree, free_start,
3731 free_start + free_len - 1,
Qu Wenruobc42bda2017-02-27 15:10:39 +08003732 EXTENT_QGROUP_RESERVED, &changeset);
3733 if (ret < 0)
3734 goto out;
3735 freed += changeset.bytes_changed;
3736 }
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003737 btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003738 BTRFS_QGROUP_RSV_DATA);
Qu Wenruobc42bda2017-02-27 15:10:39 +08003739 ret = freed;
3740out:
3741 extent_changeset_release(&changeset);
3742 return ret;
3743}
3744
Nikolay Borisov8769af92020-06-03 08:55:10 +03003745static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
Qu Wenruobc42bda2017-02-27 15:10:39 +08003746 struct extent_changeset *reserved, u64 start, u64 len,
3747 int free)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003748{
3749 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08003750 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003751 int ret;
3752
Nikolay Borisov8769af92020-06-03 08:55:10 +03003753 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))
Qu Wenruo3628b4c2018-10-09 14:36:45 +08003754 return 0;
3755
Qu Wenruobc42bda2017-02-27 15:10:39 +08003756 /* In release case, we shouldn't have @reserved */
3757 WARN_ON(!free && reserved);
3758 if (free && reserved)
Nikolay Borisov8769af92020-06-03 08:55:10 +03003759 return qgroup_free_reserved_data(inode, reserved, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08003760 extent_changeset_init(&changeset);
Nikolay Borisov8769af92020-06-03 08:55:10 +03003761 ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1,
3762 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003763 if (ret < 0)
3764 goto out;
3765
Qu Wenruod51ea5d2017-03-13 15:52:09 +08003766 if (free)
3767 trace_op = QGROUP_FREE;
Nikolay Borisov8769af92020-06-03 08:55:10 +03003768 trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
Qu Wenruod51ea5d2017-03-13 15:52:09 +08003769 changeset.bytes_changed, trace_op);
3770 if (free)
Nikolay Borisov8769af92020-06-03 08:55:10 +03003771 btrfs_qgroup_free_refroot(inode->root->fs_info,
3772 inode->root->root_key.objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003773 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08003774 ret = changeset.bytes_changed;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003775out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08003776 extent_changeset_release(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003777 return ret;
3778}
3779
3780/*
3781 * Free a reserved space range from io_tree and related qgroups
3782 *
3783 * Should be called when a range of pages get invalidated before reaching disk.
3784 * Or for error cleanup case.
Qu Wenruobc42bda2017-02-27 15:10:39 +08003785 * if @reserved is given, only reserved range in [@start, @start + @len) will
3786 * be freed.
Qu Wenruof695fdc2015-10-12 16:28:06 +08003787 *
3788 * For data written to disk, use btrfs_qgroup_release_data().
3789 *
3790 * NOTE: This function may sleep for memory allocation.
3791 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03003792int btrfs_qgroup_free_data(struct btrfs_inode *inode,
Qu Wenruobc42bda2017-02-27 15:10:39 +08003793 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003794{
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03003795 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003796}
3797
3798/*
3799 * Release a reserved space range from io_tree only.
3800 *
3801 * Should be called when a range of pages get written to disk and corresponding
3802 * FILE_EXTENT is inserted into corresponding root.
3803 *
3804 * Since new qgroup accounting framework will only update qgroup numbers at
3805 * commit_transaction() time, its reserved space shouldn't be freed from
3806 * related qgroups.
3807 *
3808 * But we should release the range from io_tree, to allow further write to be
3809 * COWed.
3810 *
3811 * NOTE: This function may sleep for memory allocation.
3812 */
Nikolay Borisov72b7d152020-06-03 08:55:18 +03003813int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003814{
Nikolay Borisov72b7d152020-06-03 08:55:18 +03003815 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003816}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003817
Qu Wenruo82874752017-12-12 15:34:34 +08003818static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3819 enum btrfs_qgroup_rsv_type type)
3820{
3821 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3822 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3823 return;
3824 if (num_bytes == 0)
3825 return;
3826
3827 spin_lock(&root->qgroup_meta_rsv_lock);
3828 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3829 root->qgroup_meta_rsv_prealloc += num_bytes;
3830 else
3831 root->qgroup_meta_rsv_pertrans += num_bytes;
3832 spin_unlock(&root->qgroup_meta_rsv_lock);
3833}
3834
3835static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3836 enum btrfs_qgroup_rsv_type type)
3837{
3838 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3839 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3840 return 0;
3841 if (num_bytes == 0)
3842 return 0;
3843
3844 spin_lock(&root->qgroup_meta_rsv_lock);
3845 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3846 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3847 num_bytes);
3848 root->qgroup_meta_rsv_prealloc -= num_bytes;
3849 } else {
3850 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3851 num_bytes);
3852 root->qgroup_meta_rsv_pertrans -= num_bytes;
3853 }
3854 spin_unlock(&root->qgroup_meta_rsv_lock);
3855 return num_bytes;
3856}
3857
Qu Wenruoc53e9652020-07-13 18:50:48 +08003858static int qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
Qu Wenruo733e03a2017-12-12 15:34:29 +08003859 enum btrfs_qgroup_rsv_type type, bool enforce)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003860{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003861 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003862 int ret;
3863
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003864 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003865 !is_fstree(root->root_key.objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003866 return 0;
3867
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003868 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruofd2b0072019-10-17 10:38:36 +08003869 trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
Qu Wenruo733e03a2017-12-12 15:34:29 +08003870 ret = qgroup_reserve(root, num_bytes, enforce, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003871 if (ret < 0)
3872 return ret;
Qu Wenruo82874752017-12-12 15:34:34 +08003873 /*
3874 * Record what we have reserved into root.
3875 *
3876 * To avoid quota disabled->enabled underflow.
3877 * In that case, we may try to free space we haven't reserved
3878 * (since quota was disabled), so record what we reserved into root.
3879 * And ensure later release won't underflow this number.
3880 */
3881 add_root_meta_rsv(root, num_bytes, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003882 return ret;
3883}
3884
Qu Wenruoc53e9652020-07-13 18:50:48 +08003885int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3886 enum btrfs_qgroup_rsv_type type, bool enforce)
3887{
3888 int ret;
3889
3890 ret = qgroup_reserve_meta(root, num_bytes, type, enforce);
3891 if (ret <= 0 && ret != -EDQUOT)
3892 return ret;
3893
3894 ret = try_flush_qgroup(root);
3895 if (ret < 0)
3896 return ret;
3897 return qgroup_reserve_meta(root, num_bytes, type, enforce);
3898}
3899
Qu Wenruo733e03a2017-12-12 15:34:29 +08003900void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003901{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003902 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003903
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003904 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003905 !is_fstree(root->root_key.objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003906 return;
3907
Qu Wenruoe1211d02017-12-12 15:34:30 +08003908 /* TODO: Update trace point to handle such free */
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003909 trace_qgroup_meta_free_all_pertrans(root);
Qu Wenruoe1211d02017-12-12 15:34:30 +08003910 /* Special value -1 means to free all reserved space */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003911 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1,
Qu Wenruo733e03a2017-12-12 15:34:29 +08003912 BTRFS_QGROUP_RSV_META_PERTRANS);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003913}
3914
Qu Wenruo733e03a2017-12-12 15:34:29 +08003915void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3916 enum btrfs_qgroup_rsv_type type)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003917{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003918 struct btrfs_fs_info *fs_info = root->fs_info;
3919
3920 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003921 !is_fstree(root->root_key.objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003922 return;
3923
Qu Wenruo82874752017-12-12 15:34:34 +08003924 /*
3925 * reservation for META_PREALLOC can happen before quota is enabled,
3926 * which can lead to underflow.
3927 * Here ensure we will only free what we really have reserved.
3928 */
3929 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003930 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruofd2b0072019-10-17 10:38:36 +08003931 trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003932 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid,
3933 num_bytes, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003934}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003935
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003936static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3937 int num_bytes)
3938{
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003939 struct btrfs_qgroup *qgroup;
3940 struct ulist_node *unode;
3941 struct ulist_iterator uiter;
3942 int ret = 0;
3943
3944 if (num_bytes == 0)
3945 return;
Marcos Paulo de Souzae3b0edd2019-11-25 21:58:50 -03003946 if (!fs_info->quota_root)
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003947 return;
3948
3949 spin_lock(&fs_info->qgroup_lock);
3950 qgroup = find_qgroup_rb(fs_info, ref_root);
3951 if (!qgroup)
3952 goto out;
3953 ulist_reinit(fs_info->qgroup_ulist);
3954 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003955 qgroup_to_aux(qgroup), GFP_ATOMIC);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003956 if (ret < 0)
3957 goto out;
3958 ULIST_ITER_INIT(&uiter);
3959 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3960 struct btrfs_qgroup *qg;
3961 struct btrfs_qgroup_list *glist;
3962
3963 qg = unode_aux_to_qgroup(unode);
3964
3965 qgroup_rsv_release(fs_info, qg, num_bytes,
3966 BTRFS_QGROUP_RSV_META_PREALLOC);
3967 qgroup_rsv_add(fs_info, qg, num_bytes,
3968 BTRFS_QGROUP_RSV_META_PERTRANS);
3969 list_for_each_entry(glist, &qg->groups, next_group) {
3970 ret = ulist_add(fs_info->qgroup_ulist,
3971 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003972 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003973 if (ret < 0)
3974 goto out;
3975 }
3976 }
3977out:
3978 spin_unlock(&fs_info->qgroup_lock);
3979}
3980
3981void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3982{
3983 struct btrfs_fs_info *fs_info = root->fs_info;
3984
3985 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003986 !is_fstree(root->root_key.objectid))
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003987 return;
Qu Wenruo82874752017-12-12 15:34:34 +08003988 /* Same as btrfs_qgroup_free_meta_prealloc() */
3989 num_bytes = sub_root_meta_rsv(root, num_bytes,
3990 BTRFS_QGROUP_RSV_META_PREALLOC);
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003991 trace_qgroup_meta_convert(root, num_bytes);
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09003992 qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003993}
3994
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003995/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003996 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003997 * time
3998 */
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03003999void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
Qu Wenruo56fa9d02015-10-13 09:53:10 +08004000{
4001 struct extent_changeset changeset;
4002 struct ulist_node *unode;
4003 struct ulist_iterator iter;
4004 int ret;
4005
Qu Wenruo364ecf32017-02-27 15:10:38 +08004006 extent_changeset_init(&changeset);
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03004007 ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02004008 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08004009
4010 WARN_ON(ret < 0);
4011 if (WARN_ON(changeset.bytes_changed)) {
4012 ULIST_ITER_INIT(&iter);
David Sterba53d32352017-02-13 13:42:29 +01004013 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03004014 btrfs_warn(inode->root->fs_info,
4015 "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
4016 btrfs_ino(inode), unode->val, unode->aux);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08004017 }
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03004018 btrfs_qgroup_free_refroot(inode->root->fs_info,
4019 inode->root->root_key.objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08004020 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
David Sterba0b08e1f2017-02-13 14:24:35 +01004021
Qu Wenruo56fa9d02015-10-13 09:53:10 +08004022 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08004023 extent_changeset_release(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08004024}
Qu Wenruo370a11b2019-01-23 15:15:16 +08004025
4026void btrfs_qgroup_init_swapped_blocks(
4027 struct btrfs_qgroup_swapped_blocks *swapped_blocks)
4028{
4029 int i;
4030
4031 spin_lock_init(&swapped_blocks->lock);
4032 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
4033 swapped_blocks->blocks[i] = RB_ROOT;
4034 swapped_blocks->swapped = false;
4035}
4036
4037/*
4038 * Delete all swapped blocks record of @root.
4039 * Every record here means we skipped a full subtree scan for qgroup.
4040 *
4041 * Gets called when committing one transaction.
4042 */
4043void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
4044{
4045 struct btrfs_qgroup_swapped_blocks *swapped_blocks;
4046 int i;
4047
4048 swapped_blocks = &root->swapped_blocks;
4049
4050 spin_lock(&swapped_blocks->lock);
4051 if (!swapped_blocks->swapped)
4052 goto out;
4053 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4054 struct rb_root *cur_root = &swapped_blocks->blocks[i];
4055 struct btrfs_qgroup_swapped_block *entry;
4056 struct btrfs_qgroup_swapped_block *next;
4057
4058 rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
4059 node)
4060 kfree(entry);
4061 swapped_blocks->blocks[i] = RB_ROOT;
4062 }
4063 swapped_blocks->swapped = false;
4064out:
4065 spin_unlock(&swapped_blocks->lock);
4066}
4067
4068/*
4069 * Add subtree roots record into @subvol_root.
4070 *
4071 * @subvol_root: tree root of the subvolume tree get swapped
4072 * @bg: block group under balance
4073 * @subvol_parent/slot: pointer to the subtree root in subvolume tree
4074 * @reloc_parent/slot: pointer to the subtree root in reloc tree
4075 * BOTH POINTERS ARE BEFORE TREE SWAP
4076 * @last_snapshot: last snapshot generation of the subvolume tree
4077 */
4078int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
4079 struct btrfs_root *subvol_root,
David Sterba32da53862019-10-29 19:20:18 +01004080 struct btrfs_block_group *bg,
Qu Wenruo370a11b2019-01-23 15:15:16 +08004081 struct extent_buffer *subvol_parent, int subvol_slot,
4082 struct extent_buffer *reloc_parent, int reloc_slot,
4083 u64 last_snapshot)
4084{
4085 struct btrfs_fs_info *fs_info = subvol_root->fs_info;
4086 struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
4087 struct btrfs_qgroup_swapped_block *block;
4088 struct rb_node **cur;
4089 struct rb_node *parent = NULL;
4090 int level = btrfs_header_level(subvol_parent) - 1;
4091 int ret = 0;
4092
4093 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4094 return 0;
4095
4096 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
4097 btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
4098 btrfs_err_rl(fs_info,
4099 "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
4100 __func__,
4101 btrfs_node_ptr_generation(subvol_parent, subvol_slot),
4102 btrfs_node_ptr_generation(reloc_parent, reloc_slot));
4103 return -EUCLEAN;
4104 }
4105
4106 block = kmalloc(sizeof(*block), GFP_NOFS);
4107 if (!block) {
4108 ret = -ENOMEM;
4109 goto out;
4110 }
4111
4112 /*
4113 * @reloc_parent/slot is still before swap, while @block is going to
4114 * record the bytenr after swap, so we do the swap here.
4115 */
4116 block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
4117 block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
4118 reloc_slot);
4119 block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
4120 block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
4121 subvol_slot);
4122 block->last_snapshot = last_snapshot;
4123 block->level = level;
Qu Wenruo57949d02019-05-21 19:28:08 +08004124
4125 /*
4126 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
4127 * no one else can modify tree blocks thus we qgroup will not change
4128 * no matter the value of trace_leaf.
4129 */
4130 if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
Qu Wenruo370a11b2019-01-23 15:15:16 +08004131 block->trace_leaf = true;
4132 else
4133 block->trace_leaf = false;
4134 btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
4135
4136 /* Insert @block into @blocks */
4137 spin_lock(&blocks->lock);
4138 cur = &blocks->blocks[level].rb_node;
4139 while (*cur) {
4140 struct btrfs_qgroup_swapped_block *entry;
4141
4142 parent = *cur;
4143 entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
4144 node);
4145
4146 if (entry->subvol_bytenr < block->subvol_bytenr) {
4147 cur = &(*cur)->rb_left;
4148 } else if (entry->subvol_bytenr > block->subvol_bytenr) {
4149 cur = &(*cur)->rb_right;
4150 } else {
4151 if (entry->subvol_generation !=
4152 block->subvol_generation ||
4153 entry->reloc_bytenr != block->reloc_bytenr ||
4154 entry->reloc_generation !=
4155 block->reloc_generation) {
4156 /*
4157 * Duplicated but mismatch entry found.
4158 * Shouldn't happen.
4159 *
4160 * Marking qgroup inconsistent should be enough
4161 * for end users.
4162 */
4163 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4164 ret = -EEXIST;
4165 }
4166 kfree(block);
4167 goto out_unlock;
4168 }
4169 }
4170 rb_link_node(&block->node, parent, cur);
4171 rb_insert_color(&block->node, &blocks->blocks[level]);
4172 blocks->swapped = true;
4173out_unlock:
4174 spin_unlock(&blocks->lock);
4175out:
4176 if (ret < 0)
4177 fs_info->qgroup_flags |=
4178 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4179 return ret;
4180}
Qu Wenruof616f5c2019-01-23 15:15:17 +08004181
4182/*
4183 * Check if the tree block is a subtree root, and if so do the needed
4184 * delayed subtree trace for qgroup.
4185 *
4186 * This is called during btrfs_cow_block().
4187 */
4188int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
4189 struct btrfs_root *root,
4190 struct extent_buffer *subvol_eb)
4191{
4192 struct btrfs_fs_info *fs_info = root->fs_info;
4193 struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
4194 struct btrfs_qgroup_swapped_block *block;
4195 struct extent_buffer *reloc_eb = NULL;
4196 struct rb_node *node;
4197 bool found = false;
4198 bool swapped = false;
4199 int level = btrfs_header_level(subvol_eb);
4200 int ret = 0;
4201 int i;
4202
4203 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4204 return 0;
4205 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
4206 return 0;
4207
4208 spin_lock(&blocks->lock);
4209 if (!blocks->swapped) {
4210 spin_unlock(&blocks->lock);
4211 return 0;
4212 }
4213 node = blocks->blocks[level].rb_node;
4214
4215 while (node) {
4216 block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
4217 if (block->subvol_bytenr < subvol_eb->start) {
4218 node = node->rb_left;
4219 } else if (block->subvol_bytenr > subvol_eb->start) {
4220 node = node->rb_right;
4221 } else {
4222 found = true;
4223 break;
4224 }
4225 }
4226 if (!found) {
4227 spin_unlock(&blocks->lock);
4228 goto out;
4229 }
4230 /* Found one, remove it from @blocks first and update blocks->swapped */
4231 rb_erase(&block->node, &blocks->blocks[level]);
4232 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4233 if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
4234 swapped = true;
4235 break;
4236 }
4237 }
4238 blocks->swapped = swapped;
4239 spin_unlock(&blocks->lock);
4240
4241 /* Read out reloc subtree root */
4242 reloc_eb = read_tree_block(fs_info, block->reloc_bytenr,
4243 block->reloc_generation, block->level,
4244 &block->first_key);
4245 if (IS_ERR(reloc_eb)) {
4246 ret = PTR_ERR(reloc_eb);
4247 reloc_eb = NULL;
4248 goto free_out;
4249 }
4250 if (!extent_buffer_uptodate(reloc_eb)) {
4251 ret = -EIO;
4252 goto free_out;
4253 }
4254
4255 ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
4256 block->last_snapshot, block->trace_leaf);
4257free_out:
4258 kfree(block);
4259 free_extent_buffer(reloc_eb);
4260out:
4261 if (ret < 0) {
4262 btrfs_err_rl(fs_info,
4263 "failed to account subtree at bytenr %llu: %d",
4264 subvol_eb->start, ret);
4265 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4266 }
4267 return ret;
4268}
Jeff Mahoney81f7eb02020-02-11 15:25:37 +08004269
4270void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
4271{
4272 struct btrfs_qgroup_extent_record *entry;
4273 struct btrfs_qgroup_extent_record *next;
4274 struct rb_root *root;
4275
4276 root = &trans->delayed_refs.dirty_extent_root;
4277 rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
4278 ulist_free(entry->old_roots);
4279 kfree(entry);
4280 }
4281}