blob: 09c7e4fd550f4d10b8febcc745e0ba4138d9f35b [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>
Arne Jansenbed92ea2012-06-28 18:03:02 +020014
15#include "ctree.h"
16#include "transaction.h"
17#include "disk-io.h"
18#include "locking.h"
19#include "ulist.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020020#include "backref.h"
Jan Schmidt2f232032013-04-25 16:04:51 +000021#include "extent_io.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070022#include "qgroup.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020023
Qu Wenruoe69bcee2015-04-17 10:23:16 +080024
Arne Jansenbed92ea2012-06-28 18:03:02 +020025/* TODO XXX FIXME
26 * - subvol delete -> delete when ref goes to 0? delete limits also?
27 * - reorganize keys
28 * - compressed
29 * - sync
Arne Jansenbed92ea2012-06-28 18:03:02 +020030 * - copy also limits on subvol creation
31 * - limit
32 * - caches fuer ulists
33 * - performance benchmarks
34 * - check all ioctl parameters
35 */
36
Qu Wenruof59c0342017-12-12 15:34:24 +080037/*
38 * Helpers to access qgroup reservation
39 *
40 * Callers should ensure the lock context and type are valid
41 */
42
43static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
44{
45 u64 ret = 0;
46 int i;
47
48 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
49 ret += qgroup->rsv.values[i];
50
51 return ret;
52}
53
54#ifdef CONFIG_BTRFS_DEBUG
55static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
56{
57 if (type == BTRFS_QGROUP_RSV_DATA)
58 return "data";
Qu Wenruo733e03a2017-12-12 15:34:29 +080059 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
60 return "meta_pertrans";
61 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
62 return "meta_prealloc";
Qu Wenruof59c0342017-12-12 15:34:24 +080063 return NULL;
64}
65#endif
66
Qu Wenruo64ee4e72017-12-12 15:34:27 +080067static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
68 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080069 enum btrfs_qgroup_rsv_type type)
70{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080071 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080072 qgroup->rsv.values[type] += num_bytes;
73}
74
Qu Wenruo64ee4e72017-12-12 15:34:27 +080075static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
76 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080077 enum btrfs_qgroup_rsv_type type)
78{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080079 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080080 if (qgroup->rsv.values[type] >= num_bytes) {
81 qgroup->rsv.values[type] -= num_bytes;
82 return;
83 }
84#ifdef CONFIG_BTRFS_DEBUG
85 WARN_RATELIMIT(1,
86 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
87 qgroup->qgroupid, qgroup_rsv_type_str(type),
88 qgroup->rsv.values[type], num_bytes);
89#endif
90 qgroup->rsv.values[type] = 0;
91}
92
Qu Wenruo64ee4e72017-12-12 15:34:27 +080093static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
94 struct btrfs_qgroup *dest,
95 struct btrfs_qgroup *src)
Qu Wenruof59c0342017-12-12 15:34:24 +080096{
97 int i;
98
99 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800100 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800101}
102
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800103static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
104 struct btrfs_qgroup *dest,
Qu Wenruof59c0342017-12-12 15:34:24 +0800105 struct btrfs_qgroup *src)
106{
107 int i;
108
109 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800110 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800111}
112
Qu Wenruo9c542132015-03-12 16:10:13 +0800113static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
114 int mod)
115{
116 if (qg->old_refcnt < seq)
117 qg->old_refcnt = seq;
118 qg->old_refcnt += mod;
119}
120
121static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
122 int mod)
123{
124 if (qg->new_refcnt < seq)
125 qg->new_refcnt = seq;
126 qg->new_refcnt += mod;
127}
128
129static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
130{
131 if (qg->old_refcnt < seq)
132 return 0;
133 return qg->old_refcnt - seq;
134}
135
136static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
137{
138 if (qg->new_refcnt < seq)
139 return 0;
140 return qg->new_refcnt - seq;
141}
142
Arne Jansenbed92ea2012-06-28 18:03:02 +0200143/*
144 * glue structure to represent the relations between qgroups.
145 */
146struct btrfs_qgroup_list {
147 struct list_head next_group;
148 struct list_head next_member;
149 struct btrfs_qgroup *group;
150 struct btrfs_qgroup *member;
151};
152
David Sterbaef2fff62016-10-26 16:23:50 +0200153static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
154{
155 return (u64)(uintptr_t)qg;
156}
157
158static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
159{
160 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
161}
Josef Bacikfcebe452014-05-13 17:30:47 -0700162
Jan Schmidtb382a322013-05-28 15:47:24 +0000163static int
164qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
165 int init_flags);
166static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000167
Wang Shilong58400fc2013-04-07 10:50:17 +0000168/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200169static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
170 u64 qgroupid)
171{
172 struct rb_node *n = fs_info->qgroup_tree.rb_node;
173 struct btrfs_qgroup *qgroup;
174
175 while (n) {
176 qgroup = rb_entry(n, struct btrfs_qgroup, node);
177 if (qgroup->qgroupid < qgroupid)
178 n = n->rb_left;
179 else if (qgroup->qgroupid > qgroupid)
180 n = n->rb_right;
181 else
182 return qgroup;
183 }
184 return NULL;
185}
186
187/* must be called with qgroup_lock held */
188static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
189 u64 qgroupid)
190{
191 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
192 struct rb_node *parent = NULL;
193 struct btrfs_qgroup *qgroup;
194
195 while (*p) {
196 parent = *p;
197 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
198
199 if (qgroup->qgroupid < qgroupid)
200 p = &(*p)->rb_left;
201 else if (qgroup->qgroupid > qgroupid)
202 p = &(*p)->rb_right;
203 else
204 return qgroup;
205 }
206
207 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
208 if (!qgroup)
209 return ERR_PTR(-ENOMEM);
210
211 qgroup->qgroupid = qgroupid;
212 INIT_LIST_HEAD(&qgroup->groups);
213 INIT_LIST_HEAD(&qgroup->members);
214 INIT_LIST_HEAD(&qgroup->dirty);
215
216 rb_link_node(&qgroup->node, parent, p);
217 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
218
219 return qgroup;
220}
221
Wang Shilong4082bd32013-08-14 09:13:36 +0800222static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200223{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200224 struct btrfs_qgroup_list *list;
225
Arne Jansenbed92ea2012-06-28 18:03:02 +0200226 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200227 while (!list_empty(&qgroup->groups)) {
228 list = list_first_entry(&qgroup->groups,
229 struct btrfs_qgroup_list, next_group);
230 list_del(&list->next_group);
231 list_del(&list->next_member);
232 kfree(list);
233 }
234
235 while (!list_empty(&qgroup->members)) {
236 list = list_first_entry(&qgroup->members,
237 struct btrfs_qgroup_list, next_member);
238 list_del(&list->next_group);
239 list_del(&list->next_member);
240 kfree(list);
241 }
242 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800243}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200244
Wang Shilong4082bd32013-08-14 09:13:36 +0800245/* must be called with qgroup_lock held */
246static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
247{
248 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
249
250 if (!qgroup)
251 return -ENOENT;
252
253 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
254 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200255 return 0;
256}
257
258/* must be called with qgroup_lock held */
259static int add_relation_rb(struct btrfs_fs_info *fs_info,
260 u64 memberid, u64 parentid)
261{
262 struct btrfs_qgroup *member;
263 struct btrfs_qgroup *parent;
264 struct btrfs_qgroup_list *list;
265
266 member = find_qgroup_rb(fs_info, memberid);
267 parent = find_qgroup_rb(fs_info, parentid);
268 if (!member || !parent)
269 return -ENOENT;
270
271 list = kzalloc(sizeof(*list), GFP_ATOMIC);
272 if (!list)
273 return -ENOMEM;
274
275 list->group = parent;
276 list->member = member;
277 list_add_tail(&list->next_group, &member->groups);
278 list_add_tail(&list->next_member, &parent->members);
279
280 return 0;
281}
282
283/* must be called with qgroup_lock held */
284static int del_relation_rb(struct btrfs_fs_info *fs_info,
285 u64 memberid, u64 parentid)
286{
287 struct btrfs_qgroup *member;
288 struct btrfs_qgroup *parent;
289 struct btrfs_qgroup_list *list;
290
291 member = find_qgroup_rb(fs_info, memberid);
292 parent = find_qgroup_rb(fs_info, parentid);
293 if (!member || !parent)
294 return -ENOENT;
295
296 list_for_each_entry(list, &member->groups, next_group) {
297 if (list->group == parent) {
298 list_del(&list->next_group);
299 list_del(&list->next_member);
300 kfree(list);
301 return 0;
302 }
303 }
304 return -ENOENT;
305}
306
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400307#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
308int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
309 u64 rfer, u64 excl)
310{
311 struct btrfs_qgroup *qgroup;
312
313 qgroup = find_qgroup_rb(fs_info, qgroupid);
314 if (!qgroup)
315 return -EINVAL;
316 if (qgroup->rfer != rfer || qgroup->excl != excl)
317 return -EINVAL;
318 return 0;
319}
320#endif
321
Arne Jansenbed92ea2012-06-28 18:03:02 +0200322/*
323 * The full config is read in one go, only called from open_ctree()
324 * It doesn't use any locking, as at this point we're still single-threaded
325 */
326int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
327{
328 struct btrfs_key key;
329 struct btrfs_key found_key;
330 struct btrfs_root *quota_root = fs_info->quota_root;
331 struct btrfs_path *path = NULL;
332 struct extent_buffer *l;
333 int slot;
334 int ret = 0;
335 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000336 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200337
Josef Bacikafcdd122016-09-02 15:40:02 -0400338 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +0200339 return 0;
340
David Sterba323b88f2017-02-13 12:10:20 +0100341 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000342 if (!fs_info->qgroup_ulist) {
343 ret = -ENOMEM;
344 goto out;
345 }
346
Arne Jansenbed92ea2012-06-28 18:03:02 +0200347 path = btrfs_alloc_path();
348 if (!path) {
349 ret = -ENOMEM;
350 goto out;
351 }
352
353 /* default this to quota off, in case no status key is found */
354 fs_info->qgroup_flags = 0;
355
356 /*
357 * pass 1: read status, all qgroup infos and limits
358 */
359 key.objectid = 0;
360 key.type = 0;
361 key.offset = 0;
362 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
363 if (ret)
364 goto out;
365
366 while (1) {
367 struct btrfs_qgroup *qgroup;
368
369 slot = path->slots[0];
370 l = path->nodes[0];
371 btrfs_item_key_to_cpu(l, &found_key, slot);
372
373 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
374 struct btrfs_qgroup_status_item *ptr;
375
376 ptr = btrfs_item_ptr(l, slot,
377 struct btrfs_qgroup_status_item);
378
379 if (btrfs_qgroup_status_version(l, ptr) !=
380 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500381 btrfs_err(fs_info,
382 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200383 goto out;
384 }
385 if (btrfs_qgroup_status_generation(l, ptr) !=
386 fs_info->generation) {
387 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500388 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400389 "qgroup generation mismatch, marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200390 }
391 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
392 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000393 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200394 goto next1;
395 }
396
397 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
398 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
399 goto next1;
400
401 qgroup = find_qgroup_rb(fs_info, found_key.offset);
402 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
403 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Geert Uytterhoevend41e36a2015-07-06 15:38:11 +0200404 btrfs_err(fs_info, "inconsistent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200405 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
406 }
407 if (!qgroup) {
408 qgroup = add_qgroup_rb(fs_info, found_key.offset);
409 if (IS_ERR(qgroup)) {
410 ret = PTR_ERR(qgroup);
411 goto out;
412 }
413 }
414 switch (found_key.type) {
415 case BTRFS_QGROUP_INFO_KEY: {
416 struct btrfs_qgroup_info_item *ptr;
417
418 ptr = btrfs_item_ptr(l, slot,
419 struct btrfs_qgroup_info_item);
420 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
421 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
422 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
423 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
424 /* generation currently unused */
425 break;
426 }
427 case BTRFS_QGROUP_LIMIT_KEY: {
428 struct btrfs_qgroup_limit_item *ptr;
429
430 ptr = btrfs_item_ptr(l, slot,
431 struct btrfs_qgroup_limit_item);
432 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
433 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
434 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
435 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
436 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
437 break;
438 }
439 }
440next1:
441 ret = btrfs_next_item(quota_root, path);
442 if (ret < 0)
443 goto out;
444 if (ret)
445 break;
446 }
447 btrfs_release_path(path);
448
449 /*
450 * pass 2: read all qgroup relations
451 */
452 key.objectid = 0;
453 key.type = BTRFS_QGROUP_RELATION_KEY;
454 key.offset = 0;
455 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
456 if (ret)
457 goto out;
458 while (1) {
459 slot = path->slots[0];
460 l = path->nodes[0];
461 btrfs_item_key_to_cpu(l, &found_key, slot);
462
463 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
464 goto next2;
465
466 if (found_key.objectid > found_key.offset) {
467 /* parent <- member, not needed to build config */
468 /* FIXME should we omit the key completely? */
469 goto next2;
470 }
471
472 ret = add_relation_rb(fs_info, found_key.objectid,
473 found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700474 if (ret == -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500475 btrfs_warn(fs_info,
476 "orphan qgroup relation 0x%llx->0x%llx",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200477 found_key.objectid, found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700478 ret = 0; /* ignore the error */
479 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200480 if (ret)
481 goto out;
482next2:
483 ret = btrfs_next_item(quota_root, path);
484 if (ret < 0)
485 goto out;
486 if (ret)
487 break;
488 }
489out:
490 fs_info->qgroup_flags |= flags;
Josef Bacikafcdd122016-09-02 15:40:02 -0400491 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
492 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
493 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
494 ret >= 0)
Jan Schmidtb382a322013-05-28 15:47:24 +0000495 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200496 btrfs_free_path(path);
497
Jan Schmidteb1716a2013-05-28 15:47:23 +0000498 if (ret < 0) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000499 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000500 fs_info->qgroup_ulist = NULL;
Jan Schmidtb382a322013-05-28 15:47:24 +0000501 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidteb1716a2013-05-28 15:47:23 +0000502 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000503
Arne Jansenbed92ea2012-06-28 18:03:02 +0200504 return ret < 0 ? ret : 0;
505}
506
507/*
Wang Shilonge685da12013-08-14 09:13:37 +0800508 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
509 * first two are in single-threaded paths.And for the third one, we have set
510 * quota_root to be null with qgroup_lock held before, so it is safe to clean
511 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200512 */
513void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
514{
515 struct rb_node *n;
516 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200517
518 while ((n = rb_first(&fs_info->qgroup_tree))) {
519 qgroup = rb_entry(n, struct btrfs_qgroup, node);
520 rb_erase(n, &fs_info->qgroup_tree);
Wang Shilong4082bd32013-08-14 09:13:36 +0800521 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200522 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800523 /*
524 * we call btrfs_free_qgroup_config() when umounting
Nicholas D Steeves01327612016-05-19 21:18:45 -0400525 * filesystem and disabling quota, so we set qgroup_ulist
Wang Shilong1e7bac12013-07-13 21:02:54 +0800526 * to be null here to avoid double free.
527 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000528 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800529 fs_info->qgroup_ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200530}
531
532static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
533 struct btrfs_root *quota_root,
534 u64 src, u64 dst)
535{
536 int ret;
537 struct btrfs_path *path;
538 struct btrfs_key key;
539
540 path = btrfs_alloc_path();
541 if (!path)
542 return -ENOMEM;
543
544 key.objectid = src;
545 key.type = BTRFS_QGROUP_RELATION_KEY;
546 key.offset = dst;
547
548 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
549
550 btrfs_mark_buffer_dirty(path->nodes[0]);
551
552 btrfs_free_path(path);
553 return ret;
554}
555
556static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
557 struct btrfs_root *quota_root,
558 u64 src, u64 dst)
559{
560 int ret;
561 struct btrfs_path *path;
562 struct btrfs_key key;
563
564 path = btrfs_alloc_path();
565 if (!path)
566 return -ENOMEM;
567
568 key.objectid = src;
569 key.type = BTRFS_QGROUP_RELATION_KEY;
570 key.offset = dst;
571
572 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
573 if (ret < 0)
574 goto out;
575
576 if (ret > 0) {
577 ret = -ENOENT;
578 goto out;
579 }
580
581 ret = btrfs_del_item(trans, quota_root, path);
582out:
583 btrfs_free_path(path);
584 return ret;
585}
586
587static int add_qgroup_item(struct btrfs_trans_handle *trans,
588 struct btrfs_root *quota_root, u64 qgroupid)
589{
590 int ret;
591 struct btrfs_path *path;
592 struct btrfs_qgroup_info_item *qgroup_info;
593 struct btrfs_qgroup_limit_item *qgroup_limit;
594 struct extent_buffer *leaf;
595 struct btrfs_key key;
596
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400597 if (btrfs_is_testing(quota_root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400598 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200599
Arne Jansenbed92ea2012-06-28 18:03:02 +0200600 path = btrfs_alloc_path();
601 if (!path)
602 return -ENOMEM;
603
604 key.objectid = 0;
605 key.type = BTRFS_QGROUP_INFO_KEY;
606 key.offset = qgroupid;
607
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700608 /*
609 * Avoid a transaction abort by catching -EEXIST here. In that
610 * case, we proceed by re-initializing the existing structure
611 * on disk.
612 */
613
Arne Jansenbed92ea2012-06-28 18:03:02 +0200614 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
615 sizeof(*qgroup_info));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700616 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200617 goto out;
618
619 leaf = path->nodes[0];
620 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
621 struct btrfs_qgroup_info_item);
622 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
623 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
624 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
625 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
626 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
627
628 btrfs_mark_buffer_dirty(leaf);
629
630 btrfs_release_path(path);
631
632 key.type = BTRFS_QGROUP_LIMIT_KEY;
633 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
634 sizeof(*qgroup_limit));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700635 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200636 goto out;
637
638 leaf = path->nodes[0];
639 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
640 struct btrfs_qgroup_limit_item);
641 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
642 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
643 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
644 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
645 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
646
647 btrfs_mark_buffer_dirty(leaf);
648
649 ret = 0;
650out:
651 btrfs_free_path(path);
652 return ret;
653}
654
655static int del_qgroup_item(struct btrfs_trans_handle *trans,
656 struct btrfs_root *quota_root, u64 qgroupid)
657{
658 int ret;
659 struct btrfs_path *path;
660 struct btrfs_key key;
661
662 path = btrfs_alloc_path();
663 if (!path)
664 return -ENOMEM;
665
666 key.objectid = 0;
667 key.type = BTRFS_QGROUP_INFO_KEY;
668 key.offset = qgroupid;
669 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
670 if (ret < 0)
671 goto out;
672
673 if (ret > 0) {
674 ret = -ENOENT;
675 goto out;
676 }
677
678 ret = btrfs_del_item(trans, quota_root, path);
679 if (ret)
680 goto out;
681
682 btrfs_release_path(path);
683
684 key.type = BTRFS_QGROUP_LIMIT_KEY;
685 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
686 if (ret < 0)
687 goto out;
688
689 if (ret > 0) {
690 ret = -ENOENT;
691 goto out;
692 }
693
694 ret = btrfs_del_item(trans, quota_root, path);
695
696out:
697 btrfs_free_path(path);
698 return ret;
699}
700
701static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
Dongsheng Yang1510e712014-11-20 21:01:41 -0500702 struct btrfs_root *root,
703 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200704{
705 struct btrfs_path *path;
706 struct btrfs_key key;
707 struct extent_buffer *l;
708 struct btrfs_qgroup_limit_item *qgroup_limit;
709 int ret;
710 int slot;
711
712 key.objectid = 0;
713 key.type = BTRFS_QGROUP_LIMIT_KEY;
Dongsheng Yang1510e712014-11-20 21:01:41 -0500714 key.offset = qgroup->qgroupid;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200715
716 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000717 if (!path)
718 return -ENOMEM;
719
Arne Jansenbed92ea2012-06-28 18:03:02 +0200720 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
721 if (ret > 0)
722 ret = -ENOENT;
723
724 if (ret)
725 goto out;
726
727 l = path->nodes[0];
728 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100729 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Dongsheng Yang1510e712014-11-20 21:01:41 -0500730 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
731 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
732 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
733 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
734 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200735
736 btrfs_mark_buffer_dirty(l);
737
738out:
739 btrfs_free_path(path);
740 return ret;
741}
742
743static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
744 struct btrfs_root *root,
745 struct btrfs_qgroup *qgroup)
746{
747 struct btrfs_path *path;
748 struct btrfs_key key;
749 struct extent_buffer *l;
750 struct btrfs_qgroup_info_item *qgroup_info;
751 int ret;
752 int slot;
753
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400754 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400755 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200756
Arne Jansenbed92ea2012-06-28 18:03:02 +0200757 key.objectid = 0;
758 key.type = BTRFS_QGROUP_INFO_KEY;
759 key.offset = qgroup->qgroupid;
760
761 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000762 if (!path)
763 return -ENOMEM;
764
Arne Jansenbed92ea2012-06-28 18:03:02 +0200765 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
766 if (ret > 0)
767 ret = -ENOENT;
768
769 if (ret)
770 goto out;
771
772 l = path->nodes[0];
773 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100774 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200775 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
776 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
777 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
778 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
779 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
780
781 btrfs_mark_buffer_dirty(l);
782
783out:
784 btrfs_free_path(path);
785 return ret;
786}
787
788static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
789 struct btrfs_fs_info *fs_info,
790 struct btrfs_root *root)
791{
792 struct btrfs_path *path;
793 struct btrfs_key key;
794 struct extent_buffer *l;
795 struct btrfs_qgroup_status_item *ptr;
796 int ret;
797 int slot;
798
799 key.objectid = 0;
800 key.type = BTRFS_QGROUP_STATUS_KEY;
801 key.offset = 0;
802
803 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000804 if (!path)
805 return -ENOMEM;
806
Arne Jansenbed92ea2012-06-28 18:03:02 +0200807 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
808 if (ret > 0)
809 ret = -ENOENT;
810
811 if (ret)
812 goto out;
813
814 l = path->nodes[0];
815 slot = path->slots[0];
816 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
817 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
818 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000819 btrfs_set_qgroup_status_rescan(l, ptr,
820 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200821
822 btrfs_mark_buffer_dirty(l);
823
824out:
825 btrfs_free_path(path);
826 return ret;
827}
828
829/*
830 * called with qgroup_lock held
831 */
832static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root)
834{
835 struct btrfs_path *path;
836 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000837 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200838 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000839 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200840
Arne Jansenbed92ea2012-06-28 18:03:02 +0200841 path = btrfs_alloc_path();
842 if (!path)
843 return -ENOMEM;
844
Wang Shilong06b3a862013-02-27 11:16:57 +0000845 path->leave_spinning = 1;
846
847 key.objectid = 0;
848 key.offset = 0;
849 key.type = 0;
850
Arne Jansenbed92ea2012-06-28 18:03:02 +0200851 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200852 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000853 if (ret < 0)
854 goto out;
855 leaf = path->nodes[0];
856 nr = btrfs_header_nritems(leaf);
857 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200858 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000859 /*
860 * delete the leaf one by one
861 * since the whole tree is going
862 * to be deleted.
863 */
864 path->slots[0] = 0;
865 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200866 if (ret)
867 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000868
Arne Jansenbed92ea2012-06-28 18:03:02 +0200869 btrfs_release_path(path);
870 }
871 ret = 0;
872out:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200873 btrfs_free_path(path);
874 return ret;
875}
876
877int btrfs_quota_enable(struct btrfs_trans_handle *trans,
878 struct btrfs_fs_info *fs_info)
879{
880 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000881 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200882 struct btrfs_path *path = NULL;
883 struct btrfs_qgroup_status_item *ptr;
884 struct extent_buffer *leaf;
885 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000886 struct btrfs_key found_key;
887 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200888 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000889 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200890
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000891 mutex_lock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +0200892 if (fs_info->quota_root)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200893 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200894
David Sterba52bf8e72017-02-13 11:03:44 +0100895 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000896 if (!fs_info->qgroup_ulist) {
897 ret = -ENOMEM;
898 goto out;
899 }
900
Arne Jansenbed92ea2012-06-28 18:03:02 +0200901 /*
902 * initially create the quota tree
903 */
904 quota_root = btrfs_create_tree(trans, fs_info,
905 BTRFS_QUOTA_TREE_OBJECTID);
906 if (IS_ERR(quota_root)) {
907 ret = PTR_ERR(quota_root);
908 goto out;
909 }
910
911 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000912 if (!path) {
913 ret = -ENOMEM;
914 goto out_free_root;
915 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200916
917 key.objectid = 0;
918 key.type = BTRFS_QGROUP_STATUS_KEY;
919 key.offset = 0;
920
921 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
922 sizeof(*ptr));
923 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000924 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200925
926 leaf = path->nodes[0];
927 ptr = btrfs_item_ptr(leaf, path->slots[0],
928 struct btrfs_qgroup_status_item);
929 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
930 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
931 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
932 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
933 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000934 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200935
936 btrfs_mark_buffer_dirty(leaf);
937
Wang Shilong7708f022013-04-07 10:24:57 +0000938 key.objectid = 0;
939 key.type = BTRFS_ROOT_REF_KEY;
940 key.offset = 0;
941
942 btrfs_release_path(path);
943 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
944 if (ret > 0)
945 goto out_add_root;
946 if (ret < 0)
947 goto out_free_path;
948
949
950 while (1) {
951 slot = path->slots[0];
952 leaf = path->nodes[0];
953 btrfs_item_key_to_cpu(leaf, &found_key, slot);
954
955 if (found_key.type == BTRFS_ROOT_REF_KEY) {
956 ret = add_qgroup_item(trans, quota_root,
957 found_key.offset);
958 if (ret)
959 goto out_free_path;
960
Wang Shilong7708f022013-04-07 10:24:57 +0000961 qgroup = add_qgroup_rb(fs_info, found_key.offset);
962 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000963 ret = PTR_ERR(qgroup);
964 goto out_free_path;
965 }
Wang Shilong7708f022013-04-07 10:24:57 +0000966 }
967 ret = btrfs_next_item(tree_root, path);
968 if (ret < 0)
969 goto out_free_path;
970 if (ret)
971 break;
972 }
973
974out_add_root:
975 btrfs_release_path(path);
976 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
977 if (ret)
978 goto out_free_path;
979
Wang Shilong7708f022013-04-07 10:24:57 +0000980 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
981 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000982 ret = PTR_ERR(qgroup);
983 goto out_free_path;
984 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000985 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200986 fs_info->quota_root = quota_root;
Nikolay Borisov5d235152018-01-31 10:52:04 +0200987 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200988 spin_unlock(&fs_info->qgroup_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +0200989 ret = qgroup_rescan_init(fs_info, 0, 1);
990 if (!ret) {
991 qgroup_rescan_zero_tracking(fs_info);
992 btrfs_queue_work(fs_info->qgroup_rescan_workers,
993 &fs_info->qgroup_rescan_work);
994 }
995
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000996out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200997 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000998out_free_root:
999 if (ret) {
1000 free_extent_buffer(quota_root->node);
1001 free_extent_buffer(quota_root->commit_root);
1002 kfree(quota_root);
1003 }
1004out:
Jan Schmidteb1716a2013-05-28 15:47:23 +00001005 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00001006 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +00001007 fs_info->qgroup_ulist = NULL;
1008 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001009 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001010 return ret;
1011}
1012
1013int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1014 struct btrfs_fs_info *fs_info)
1015{
Arne Jansenbed92ea2012-06-28 18:03:02 +02001016 struct btrfs_root *quota_root;
1017 int ret = 0;
1018
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001019 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +00001020 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001021 goto out;
Josef Bacikafcdd122016-09-02 15:40:02 -04001022 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04001023 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -08001024 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001025 quota_root = fs_info->quota_root;
1026 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +08001027 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001028 spin_unlock(&fs_info->qgroup_lock);
1029
Wang Shilonge685da12013-08-14 09:13:37 +08001030 btrfs_free_qgroup_config(fs_info);
1031
Arne Jansenbed92ea2012-06-28 18:03:02 +02001032 ret = btrfs_clean_quota_tree(trans, quota_root);
1033 if (ret)
1034 goto out;
1035
Jeff Mahoney1cd54472017-08-17 10:25:11 -04001036 ret = btrfs_del_root(trans, fs_info, &quota_root->root_key);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001037 if (ret)
1038 goto out;
1039
1040 list_del(&quota_root->dirty_list);
1041
1042 btrfs_tree_lock(quota_root->node);
David Sterba7c302b42017-02-10 18:47:57 +01001043 clean_tree_block(fs_info, quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001044 btrfs_tree_unlock(quota_root->node);
1045 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1046
1047 free_extent_buffer(quota_root->node);
1048 free_extent_buffer(quota_root->commit_root);
1049 kfree(quota_root);
1050out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001051 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001052 return ret;
1053}
1054
Jan Schmidt2f232032013-04-25 16:04:51 +00001055static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1056 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001057{
Jan Schmidt2f232032013-04-25 16:04:51 +00001058 if (list_empty(&qgroup->dirty))
1059 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001060}
1061
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001062/*
Qu Wenruo429d6272017-12-12 15:34:26 +08001063 * The easy accounting, we're updating qgroup relationship whose child qgroup
1064 * only has exclusive extents.
1065 *
1066 * In this case, all exclsuive extents will also be exlusive for parent, so
1067 * excl/rfer just get added/removed.
1068 *
1069 * So is qgroup reservation space, which should also be added/removed to
1070 * parent.
1071 * Or when child tries to release reservation space, parent will underflow its
1072 * reservation (for relationship adding case).
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001073 *
1074 * Caller should hold fs_info->qgroup_lock.
1075 */
1076static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1077 struct ulist *tmp, u64 ref_root,
Qu Wenruo429d6272017-12-12 15:34:26 +08001078 struct btrfs_qgroup *src, int sign)
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001079{
1080 struct btrfs_qgroup *qgroup;
1081 struct btrfs_qgroup_list *glist;
1082 struct ulist_node *unode;
1083 struct ulist_iterator uiter;
Qu Wenruo429d6272017-12-12 15:34:26 +08001084 u64 num_bytes = src->excl;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001085 int ret = 0;
1086
1087 qgroup = find_qgroup_rb(fs_info, ref_root);
1088 if (!qgroup)
1089 goto out;
1090
1091 qgroup->rfer += sign * num_bytes;
1092 qgroup->rfer_cmpr += sign * num_bytes;
1093
1094 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1095 qgroup->excl += sign * num_bytes;
1096 qgroup->excl_cmpr += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001097
1098 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001099 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001100 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001101 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001102
1103 qgroup_dirty(fs_info, qgroup);
1104
1105 /* Get all of the parent groups that contain this qgroup */
1106 list_for_each_entry(glist, &qgroup->groups, next_group) {
1107 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001108 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001109 if (ret < 0)
1110 goto out;
1111 }
1112
1113 /* Iterate all of the parents and adjust their reference counts */
1114 ULIST_ITER_INIT(&uiter);
1115 while ((unode = ulist_next(tmp, &uiter))) {
David Sterbaef2fff62016-10-26 16:23:50 +02001116 qgroup = unode_aux_to_qgroup(unode);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001117 qgroup->rfer += sign * num_bytes;
1118 qgroup->rfer_cmpr += sign * num_bytes;
1119 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1120 qgroup->excl += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001121 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001122 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001123 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001124 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001125 qgroup->excl_cmpr += sign * num_bytes;
1126 qgroup_dirty(fs_info, qgroup);
1127
1128 /* Add any parents of the parents */
1129 list_for_each_entry(glist, &qgroup->groups, next_group) {
1130 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001131 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001132 if (ret < 0)
1133 goto out;
1134 }
1135 }
1136 ret = 0;
1137out:
1138 return ret;
1139}
1140
1141
1142/*
1143 * Quick path for updating qgroup with only excl refs.
1144 *
1145 * In that case, just update all parent will be enough.
1146 * Or we needs to do a full rescan.
1147 * Caller should also hold fs_info->qgroup_lock.
1148 *
1149 * Return 0 for quick update, return >0 for need to full rescan
1150 * and mark INCONSISTENT flag.
1151 * Return < 0 for other error.
1152 */
1153static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1154 struct ulist *tmp, u64 src, u64 dst,
1155 int sign)
1156{
1157 struct btrfs_qgroup *qgroup;
1158 int ret = 1;
1159 int err = 0;
1160
1161 qgroup = find_qgroup_rb(fs_info, src);
1162 if (!qgroup)
1163 goto out;
1164 if (qgroup->excl == qgroup->rfer) {
1165 ret = 0;
1166 err = __qgroup_excl_accounting(fs_info, tmp, dst,
Qu Wenruo429d6272017-12-12 15:34:26 +08001167 qgroup, sign);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001168 if (err < 0) {
1169 ret = err;
1170 goto out;
1171 }
1172 }
1173out:
1174 if (ret)
1175 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1176 return ret;
1177}
1178
Arne Jansenbed92ea2012-06-28 18:03:02 +02001179int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1180 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1181{
1182 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001183 struct btrfs_qgroup *parent;
1184 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001185 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001186 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001187 int ret = 0;
1188
Qu Wenruo8465ece2015-02-27 16:24:22 +08001189 /* Check the level of src and dst first */
1190 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1191 return -EINVAL;
1192
David Sterba6602caf2017-02-13 12:41:02 +01001193 tmp = ulist_alloc(GFP_KERNEL);
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001194 if (!tmp)
1195 return -ENOMEM;
1196
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001197 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001198 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001199 if (!quota_root) {
1200 ret = -EINVAL;
1201 goto out;
1202 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001203 member = find_qgroup_rb(fs_info, src);
1204 parent = find_qgroup_rb(fs_info, dst);
1205 if (!member || !parent) {
1206 ret = -EINVAL;
1207 goto out;
1208 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001209
Wang Shilong534e6622013-04-17 14:49:51 +00001210 /* check if such qgroup relation exist firstly */
1211 list_for_each_entry(list, &member->groups, next_group) {
1212 if (list->group == parent) {
1213 ret = -EEXIST;
1214 goto out;
1215 }
1216 }
1217
Arne Jansenbed92ea2012-06-28 18:03:02 +02001218 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1219 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001220 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001221
1222 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1223 if (ret) {
1224 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001225 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001226 }
1227
1228 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001229 ret = add_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001230 if (ret < 0) {
1231 spin_unlock(&fs_info->qgroup_lock);
1232 goto out;
1233 }
1234 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001235 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001236out:
1237 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001238 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001239 return ret;
1240}
1241
David Sterba025db912017-02-13 13:00:51 +01001242static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001243 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1244{
1245 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001246 struct btrfs_qgroup *parent;
1247 struct btrfs_qgroup *member;
1248 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001249 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001250 int ret = 0;
1251 int err;
1252
David Sterba6602caf2017-02-13 12:41:02 +01001253 tmp = ulist_alloc(GFP_KERNEL);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001254 if (!tmp)
1255 return -ENOMEM;
1256
Arne Jansenbed92ea2012-06-28 18:03:02 +02001257 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001258 if (!quota_root) {
1259 ret = -EINVAL;
1260 goto out;
1261 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001262
Wang Shilong534e6622013-04-17 14:49:51 +00001263 member = find_qgroup_rb(fs_info, src);
1264 parent = find_qgroup_rb(fs_info, dst);
1265 if (!member || !parent) {
1266 ret = -EINVAL;
1267 goto out;
1268 }
1269
1270 /* check if such qgroup relation exist firstly */
1271 list_for_each_entry(list, &member->groups, next_group) {
1272 if (list->group == parent)
1273 goto exist;
1274 }
1275 ret = -ENOENT;
1276 goto out;
1277exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001278 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1279 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1280 if (err && !ret)
1281 ret = err;
1282
1283 spin_lock(&fs_info->qgroup_lock);
1284 del_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001285 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001286 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001287out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001288 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001289 return ret;
1290}
1291
1292int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1293 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1294{
1295 int ret = 0;
1296
1297 mutex_lock(&fs_info->qgroup_ioctl_lock);
1298 ret = __del_qgroup_relation(trans, fs_info, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001299 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001300
Arne Jansenbed92ea2012-06-28 18:03:02 +02001301 return ret;
1302}
1303
1304int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -05001305 struct btrfs_fs_info *fs_info, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001306{
1307 struct btrfs_root *quota_root;
1308 struct btrfs_qgroup *qgroup;
1309 int ret = 0;
1310
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001311 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001312 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001313 if (!quota_root) {
1314 ret = -EINVAL;
1315 goto out;
1316 }
Wang Shilong534e6622013-04-17 14:49:51 +00001317 qgroup = find_qgroup_rb(fs_info, qgroupid);
1318 if (qgroup) {
1319 ret = -EEXIST;
1320 goto out;
1321 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001322
1323 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001324 if (ret)
1325 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001326
1327 spin_lock(&fs_info->qgroup_lock);
1328 qgroup = add_qgroup_rb(fs_info, qgroupid);
1329 spin_unlock(&fs_info->qgroup_lock);
1330
1331 if (IS_ERR(qgroup))
1332 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001333out:
1334 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001335 return ret;
1336}
1337
1338int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1339 struct btrfs_fs_info *fs_info, u64 qgroupid)
1340{
1341 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001342 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001343 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001344 int ret = 0;
1345
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001346 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001347 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001348 if (!quota_root) {
1349 ret = -EINVAL;
1350 goto out;
1351 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001352
Arne Jansen2cf68702013-01-17 01:22:09 -07001353 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001354 if (!qgroup) {
1355 ret = -ENOENT;
1356 goto out;
1357 } else {
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001358 /* check if there are no children of this qgroup */
1359 if (!list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001360 ret = -EBUSY;
1361 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001362 }
1363 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001364 ret = del_qgroup_item(trans, quota_root, qgroupid);
Sargun Dhillon36b96fd2017-09-17 09:02:29 +00001365 if (ret && ret != -ENOENT)
1366 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001367
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001368 while (!list_empty(&qgroup->groups)) {
1369 list = list_first_entry(&qgroup->groups,
1370 struct btrfs_qgroup_list, next_group);
1371 ret = __del_qgroup_relation(trans, fs_info,
1372 qgroupid,
1373 list->group->qgroupid);
1374 if (ret)
1375 goto out;
1376 }
1377
Arne Jansenbed92ea2012-06-28 18:03:02 +02001378 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001379 del_qgroup_rb(fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001380 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001381out:
1382 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001383 return ret;
1384}
1385
1386int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1387 struct btrfs_fs_info *fs_info, u64 qgroupid,
1388 struct btrfs_qgroup_limit *limit)
1389{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001390 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001391 struct btrfs_qgroup *qgroup;
1392 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001393 /* Sometimes we would want to clear the limit on this qgroup.
1394 * To meet this requirement, we treat the -1 as a special value
1395 * which tell kernel to clear the limit on this qgroup.
1396 */
1397 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001398
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001399 mutex_lock(&fs_info->qgroup_ioctl_lock);
1400 quota_root = fs_info->quota_root;
1401 if (!quota_root) {
1402 ret = -EINVAL;
1403 goto out;
1404 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001405
Wang Shilongddb47af2013-04-07 10:50:20 +00001406 qgroup = find_qgroup_rb(fs_info, qgroupid);
1407 if (!qgroup) {
1408 ret = -ENOENT;
1409 goto out;
1410 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001411
Wang Shilong58400fc2013-04-07 10:50:17 +00001412 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001413 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1414 if (limit->max_rfer == CLEAR_VALUE) {
1415 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1416 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1417 qgroup->max_rfer = 0;
1418 } else {
1419 qgroup->max_rfer = limit->max_rfer;
1420 }
1421 }
1422 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1423 if (limit->max_excl == CLEAR_VALUE) {
1424 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1425 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1426 qgroup->max_excl = 0;
1427 } else {
1428 qgroup->max_excl = limit->max_excl;
1429 }
1430 }
1431 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1432 if (limit->rsv_rfer == CLEAR_VALUE) {
1433 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1434 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1435 qgroup->rsv_rfer = 0;
1436 } else {
1437 qgroup->rsv_rfer = limit->rsv_rfer;
1438 }
1439 }
1440 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1441 if (limit->rsv_excl == CLEAR_VALUE) {
1442 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1443 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1444 qgroup->rsv_excl = 0;
1445 } else {
1446 qgroup->rsv_excl = limit->rsv_excl;
1447 }
1448 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001449 qgroup->lim_flags |= limit->flags;
1450
Arne Jansenbed92ea2012-06-28 18:03:02 +02001451 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001452
1453 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1454 if (ret) {
1455 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1456 btrfs_info(fs_info, "unable to update quota limit for %llu",
1457 qgroupid);
1458 }
1459
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001460out:
1461 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001462 return ret;
1463}
Mark Fasheh11526512014-07-17 12:39:01 -07001464
Qu Wenruo50b3e042016-10-18 09:31:27 +08001465int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +08001466 struct btrfs_delayed_ref_root *delayed_refs,
1467 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001468{
1469 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1470 struct rb_node *parent_node = NULL;
1471 struct btrfs_qgroup_extent_record *entry;
1472 u64 bytenr = record->bytenr;
1473
David Sterbaa4666e62018-03-16 02:21:22 +01001474 lockdep_assert_held(&delayed_refs->lock);
Qu Wenruo50b3e042016-10-18 09:31:27 +08001475 trace_btrfs_qgroup_trace_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001476
Qu Wenruo3368d002015-04-16 14:34:17 +08001477 while (*p) {
1478 parent_node = *p;
1479 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1480 node);
1481 if (bytenr < entry->bytenr)
1482 p = &(*p)->rb_left;
1483 else if (bytenr > entry->bytenr)
1484 p = &(*p)->rb_right;
1485 else
Qu Wenruocb93b522016-08-15 10:36:50 +08001486 return 1;
Qu Wenruo3368d002015-04-16 14:34:17 +08001487 }
1488
1489 rb_link_node(&record->node, parent_node, p);
1490 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001491 return 0;
1492}
1493
Qu Wenruofb235dc2017-02-15 10:43:03 +08001494int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1495 struct btrfs_qgroup_extent_record *qrecord)
1496{
1497 struct ulist *old_root;
1498 u64 bytenr = qrecord->bytenr;
1499 int ret;
1500
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001501 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
Nikolay Borisov952bd3db2018-01-29 15:53:01 +02001502 if (ret < 0) {
1503 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1504 btrfs_warn(fs_info,
1505"error accounting new delayed refs extent (err code: %d), quota inconsistent",
1506 ret);
1507 return 0;
1508 }
Qu Wenruofb235dc2017-02-15 10:43:03 +08001509
1510 /*
1511 * Here we don't need to get the lock of
1512 * trans->transaction->delayed_refs, since inserted qrecord won't
1513 * be deleted, only qrecord->node may be modified (new qrecord insert)
1514 *
1515 * So modifying qrecord->old_roots is safe here
1516 */
1517 qrecord->old_roots = old_root;
1518 return 0;
1519}
1520
Qu Wenruo50b3e042016-10-18 09:31:27 +08001521int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
Qu Wenruocb93b522016-08-15 10:36:50 +08001522 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1523 gfp_t gfp_flag)
1524{
1525 struct btrfs_qgroup_extent_record *record;
1526 struct btrfs_delayed_ref_root *delayed_refs;
1527 int ret;
1528
Josef Bacikafcdd122016-09-02 15:40:02 -04001529 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1530 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001531 return 0;
1532 if (WARN_ON(trans == NULL))
1533 return -EINVAL;
1534 record = kmalloc(sizeof(*record), gfp_flag);
1535 if (!record)
1536 return -ENOMEM;
1537
1538 delayed_refs = &trans->transaction->delayed_refs;
1539 record->bytenr = bytenr;
1540 record->num_bytes = num_bytes;
1541 record->old_roots = NULL;
1542
1543 spin_lock(&delayed_refs->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001544 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
Qu Wenruocb93b522016-08-15 10:36:50 +08001545 spin_unlock(&delayed_refs->lock);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001546 if (ret > 0) {
Qu Wenruocb93b522016-08-15 10:36:50 +08001547 kfree(record);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001548 return 0;
1549 }
1550 return btrfs_qgroup_trace_extent_post(fs_info, record);
Qu Wenruo3368d002015-04-16 14:34:17 +08001551}
1552
Qu Wenruo33d1f052016-10-18 09:31:28 +08001553int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001554 struct btrfs_fs_info *fs_info,
Qu Wenruo33d1f052016-10-18 09:31:28 +08001555 struct extent_buffer *eb)
1556{
1557 int nr = btrfs_header_nritems(eb);
1558 int i, extent_type, ret;
1559 struct btrfs_key key;
1560 struct btrfs_file_extent_item *fi;
1561 u64 bytenr, num_bytes;
1562
1563 /* We can be called directly from walk_up_proc() */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001564 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001565 return 0;
1566
1567 for (i = 0; i < nr; i++) {
1568 btrfs_item_key_to_cpu(eb, &key, i);
1569
1570 if (key.type != BTRFS_EXTENT_DATA_KEY)
1571 continue;
1572
1573 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1574 /* filter out non qgroup-accountable extents */
1575 extent_type = btrfs_file_extent_type(eb, fi);
1576
1577 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1578 continue;
1579
1580 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1581 if (!bytenr)
1582 continue;
1583
1584 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1585
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001586 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1587 num_bytes, GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001588 if (ret)
1589 return ret;
1590 }
Jeff Mahoneycddf3b22017-06-20 08:15:26 -04001591 cond_resched();
Qu Wenruo33d1f052016-10-18 09:31:28 +08001592 return 0;
1593}
1594
1595/*
1596 * Walk up the tree from the bottom, freeing leaves and any interior
1597 * nodes which have had all slots visited. If a node (leaf or
1598 * interior) is freed, the node above it will have it's slot
1599 * incremented. The root node will never be freed.
1600 *
1601 * At the end of this function, we should have a path which has all
1602 * slots incremented to the next position for a search. If we need to
1603 * read a new node it will be NULL and the node above it will have the
1604 * correct slot selected for a later read.
1605 *
1606 * If we increment the root nodes slot counter past the number of
1607 * elements, 1 is returned to signal completion of the search.
1608 */
David Sterba15b34512017-02-10 20:30:23 +01001609static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
Qu Wenruo33d1f052016-10-18 09:31:28 +08001610{
1611 int level = 0;
1612 int nr, slot;
1613 struct extent_buffer *eb;
1614
1615 if (root_level == 0)
1616 return 1;
1617
1618 while (level <= root_level) {
1619 eb = path->nodes[level];
1620 nr = btrfs_header_nritems(eb);
1621 path->slots[level]++;
1622 slot = path->slots[level];
1623 if (slot >= nr || level == 0) {
1624 /*
1625 * Don't free the root - we will detect this
1626 * condition after our loop and return a
1627 * positive value for caller to stop walking the tree.
1628 */
1629 if (level != root_level) {
1630 btrfs_tree_unlock_rw(eb, path->locks[level]);
1631 path->locks[level] = 0;
1632
1633 free_extent_buffer(eb);
1634 path->nodes[level] = NULL;
1635 path->slots[level] = 0;
1636 }
1637 } else {
1638 /*
1639 * We have a valid slot to walk back down
1640 * from. Stop here so caller can process these
1641 * new nodes.
1642 */
1643 break;
1644 }
1645
1646 level++;
1647 }
1648
1649 eb = path->nodes[root_level];
1650 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1651 return 1;
1652
1653 return 0;
1654}
1655
1656int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
1658 struct extent_buffer *root_eb,
1659 u64 root_gen, int root_level)
1660{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001661 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001662 int ret = 0;
1663 int level;
1664 struct extent_buffer *eb = root_eb;
1665 struct btrfs_path *path = NULL;
1666
Nikolay Borisovb6e6bca2017-07-12 09:42:19 +03001667 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001668 BUG_ON(root_eb == NULL);
1669
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001670 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001671 return 0;
1672
1673 if (!extent_buffer_uptodate(root_eb)) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001674 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001675 if (ret)
1676 goto out;
1677 }
1678
1679 if (root_level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001680 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001681 goto out;
1682 }
1683
1684 path = btrfs_alloc_path();
1685 if (!path)
1686 return -ENOMEM;
1687
1688 /*
1689 * Walk down the tree. Missing extent blocks are filled in as
1690 * we go. Metadata is accounted every time we read a new
1691 * extent block.
1692 *
1693 * When we reach a leaf, we account for file extent items in it,
1694 * walk back up the tree (adjusting slot pointers as we go)
1695 * and restart the search process.
1696 */
1697 extent_buffer_get(root_eb); /* For path */
1698 path->nodes[root_level] = root_eb;
1699 path->slots[root_level] = 0;
1700 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1701walk_down:
1702 level = root_level;
1703 while (level >= 0) {
1704 if (path->nodes[level] == NULL) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001705 struct btrfs_key first_key;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001706 int parent_slot;
1707 u64 child_gen;
1708 u64 child_bytenr;
1709
1710 /*
1711 * We need to get child blockptr/gen from parent before
1712 * we can read it.
1713 */
1714 eb = path->nodes[level + 1];
1715 parent_slot = path->slots[level + 1];
1716 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1717 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08001718 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001719
Qu Wenruo581c1762018-03-29 09:08:11 +08001720 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1721 level, &first_key);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001722 if (IS_ERR(eb)) {
1723 ret = PTR_ERR(eb);
1724 goto out;
1725 } else if (!extent_buffer_uptodate(eb)) {
1726 free_extent_buffer(eb);
1727 ret = -EIO;
1728 goto out;
1729 }
1730
1731 path->nodes[level] = eb;
1732 path->slots[level] = 0;
1733
1734 btrfs_tree_read_lock(eb);
1735 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1736 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1737
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001738 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1739 child_bytenr,
1740 fs_info->nodesize,
1741 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001742 if (ret)
1743 goto out;
1744 }
1745
1746 if (level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001747 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1748 path->nodes[level]);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001749 if (ret)
1750 goto out;
1751
1752 /* Nonzero return here means we completed our search */
David Sterba15b34512017-02-10 20:30:23 +01001753 ret = adjust_slots_upwards(path, root_level);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001754 if (ret)
1755 break;
1756
1757 /* Restart search with new slots */
1758 goto walk_down;
1759 }
1760
1761 level--;
1762 }
1763
1764 ret = 0;
1765out:
1766 btrfs_free_path(path);
1767
1768 return ret;
1769}
1770
Qu Wenruod810ef22015-04-12 16:52:34 +08001771#define UPDATE_NEW 0
1772#define UPDATE_OLD 1
1773/*
1774 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1775 */
1776static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1777 struct ulist *roots, struct ulist *tmp,
1778 struct ulist *qgroups, u64 seq, int update_old)
1779{
1780 struct ulist_node *unode;
1781 struct ulist_iterator uiter;
1782 struct ulist_node *tmp_unode;
1783 struct ulist_iterator tmp_uiter;
1784 struct btrfs_qgroup *qg;
1785 int ret = 0;
1786
1787 if (!roots)
1788 return 0;
1789 ULIST_ITER_INIT(&uiter);
1790 while ((unode = ulist_next(roots, &uiter))) {
1791 qg = find_qgroup_rb(fs_info, unode->val);
1792 if (!qg)
1793 continue;
1794
1795 ulist_reinit(tmp);
David Sterbaef2fff62016-10-26 16:23:50 +02001796 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
Qu Wenruod810ef22015-04-12 16:52:34 +08001797 GFP_ATOMIC);
1798 if (ret < 0)
1799 return ret;
David Sterbaef2fff62016-10-26 16:23:50 +02001800 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
Qu Wenruod810ef22015-04-12 16:52:34 +08001801 if (ret < 0)
1802 return ret;
1803 ULIST_ITER_INIT(&tmp_uiter);
1804 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1805 struct btrfs_qgroup_list *glist;
1806
David Sterbaef2fff62016-10-26 16:23:50 +02001807 qg = unode_aux_to_qgroup(tmp_unode);
Qu Wenruod810ef22015-04-12 16:52:34 +08001808 if (update_old)
1809 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1810 else
1811 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1812 list_for_each_entry(glist, &qg->groups, next_group) {
1813 ret = ulist_add(qgroups, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001814 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001815 GFP_ATOMIC);
1816 if (ret < 0)
1817 return ret;
1818 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001819 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001820 GFP_ATOMIC);
1821 if (ret < 0)
1822 return ret;
1823 }
1824 }
1825 }
1826 return 0;
1827}
1828
Josef Bacikfcebe452014-05-13 17:30:47 -07001829/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001830 * Update qgroup rfer/excl counters.
1831 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08001832 *
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001833 * Excl update is tricky, the update is split into 2 part.
1834 * Part 1: Possible exclusive <-> sharing detect:
1835 * | A | !A |
1836 * -------------------------------------
1837 * B | * | - |
1838 * -------------------------------------
1839 * !B | + | ** |
1840 * -------------------------------------
1841 *
1842 * Conditions:
1843 * A: cur_old_roots < nr_old_roots (not exclusive before)
1844 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1845 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04001846 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001847 *
1848 * Results:
1849 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1850 * *: Definitely not changed. **: Possible unchanged.
1851 *
1852 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1853 *
1854 * To make the logic clear, we first use condition A and B to split
1855 * combination into 4 results.
1856 *
1857 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1858 * only on variant maybe 0.
1859 *
1860 * Lastly, check result **, since there are 2 variants maybe 0, split them
1861 * again(2x2).
1862 * But this time we don't need to consider other things, the codes and logic
1863 * is easy to understand now.
1864 */
1865static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1866 struct ulist *qgroups,
1867 u64 nr_old_roots,
1868 u64 nr_new_roots,
1869 u64 num_bytes, u64 seq)
1870{
1871 struct ulist_node *unode;
1872 struct ulist_iterator uiter;
1873 struct btrfs_qgroup *qg;
1874 u64 cur_new_count, cur_old_count;
1875
1876 ULIST_ITER_INIT(&uiter);
1877 while ((unode = ulist_next(qgroups, &uiter))) {
1878 bool dirty = false;
1879
David Sterbaef2fff62016-10-26 16:23:50 +02001880 qg = unode_aux_to_qgroup(unode);
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001881 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1882 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1883
Jeff Mahoneybc074522016-06-09 17:27:55 -04001884 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1885 cur_old_count, cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001886
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001887 /* Rfer update part */
1888 if (cur_old_count == 0 && cur_new_count > 0) {
1889 qg->rfer += num_bytes;
1890 qg->rfer_cmpr += num_bytes;
1891 dirty = true;
1892 }
1893 if (cur_old_count > 0 && cur_new_count == 0) {
1894 qg->rfer -= num_bytes;
1895 qg->rfer_cmpr -= num_bytes;
1896 dirty = true;
1897 }
1898
1899 /* Excl update part */
1900 /* Exclusive/none -> shared case */
1901 if (cur_old_count == nr_old_roots &&
1902 cur_new_count < nr_new_roots) {
1903 /* Exclusive -> shared */
1904 if (cur_old_count != 0) {
1905 qg->excl -= num_bytes;
1906 qg->excl_cmpr -= num_bytes;
1907 dirty = true;
1908 }
1909 }
1910
1911 /* Shared -> exclusive/none case */
1912 if (cur_old_count < nr_old_roots &&
1913 cur_new_count == nr_new_roots) {
1914 /* Shared->exclusive */
1915 if (cur_new_count != 0) {
1916 qg->excl += num_bytes;
1917 qg->excl_cmpr += num_bytes;
1918 dirty = true;
1919 }
1920 }
1921
1922 /* Exclusive/none -> exclusive/none case */
1923 if (cur_old_count == nr_old_roots &&
1924 cur_new_count == nr_new_roots) {
1925 if (cur_old_count == 0) {
1926 /* None -> exclusive/none */
1927
1928 if (cur_new_count != 0) {
1929 /* None -> exclusive */
1930 qg->excl += num_bytes;
1931 qg->excl_cmpr += num_bytes;
1932 dirty = true;
1933 }
1934 /* None -> none, nothing changed */
1935 } else {
1936 /* Exclusive -> exclusive/none */
1937
1938 if (cur_new_count == 0) {
1939 /* Exclusive -> none */
1940 qg->excl -= num_bytes;
1941 qg->excl_cmpr -= num_bytes;
1942 dirty = true;
1943 }
1944 /* Exclusive -> exclusive, nothing changed */
1945 }
1946 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08001947
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001948 if (dirty)
1949 qgroup_dirty(fs_info, qg);
1950 }
1951 return 0;
1952}
1953
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001954/*
1955 * Check if the @roots potentially is a list of fs tree roots
1956 *
1957 * Return 0 for definitely not a fs/subvol tree roots ulist
1958 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1959 * one as well)
1960 */
1961static int maybe_fs_roots(struct ulist *roots)
1962{
1963 struct ulist_node *unode;
1964 struct ulist_iterator uiter;
1965
1966 /* Empty one, still possible for fs roots */
1967 if (!roots || roots->nnodes == 0)
1968 return 1;
1969
1970 ULIST_ITER_INIT(&uiter);
1971 unode = ulist_next(roots, &uiter);
1972 if (!unode)
1973 return 1;
1974
1975 /*
1976 * If it contains fs tree roots, then it must belong to fs/subvol
1977 * trees.
1978 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
1979 */
1980 return is_fstree(unode->val);
1981}
1982
Qu Wenruo442244c2015-04-16 17:18:36 +08001983int
Qu Wenruo550d7a22015-04-16 15:37:33 +08001984btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1985 struct btrfs_fs_info *fs_info,
1986 u64 bytenr, u64 num_bytes,
1987 struct ulist *old_roots, struct ulist *new_roots)
1988{
1989 struct ulist *qgroups = NULL;
1990 struct ulist *tmp = NULL;
1991 u64 seq;
1992 u64 nr_new_roots = 0;
1993 u64 nr_old_roots = 0;
1994 int ret = 0;
1995
David Sterba81353d52017-02-13 14:05:24 +01001996 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1997 return 0;
1998
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001999 if (new_roots) {
2000 if (!maybe_fs_roots(new_roots))
2001 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002002 nr_new_roots = new_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002003 }
2004 if (old_roots) {
2005 if (!maybe_fs_roots(old_roots))
2006 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002007 nr_old_roots = old_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002008 }
2009
2010 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2011 if (nr_old_roots == 0 && nr_new_roots == 0)
2012 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002013
Qu Wenruo550d7a22015-04-16 15:37:33 +08002014 BUG_ON(!fs_info->quota_root);
2015
Jeff Mahoneybc074522016-06-09 17:27:55 -04002016 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
2017 nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002018
Qu Wenruo550d7a22015-04-16 15:37:33 +08002019 qgroups = ulist_alloc(GFP_NOFS);
2020 if (!qgroups) {
2021 ret = -ENOMEM;
2022 goto out_free;
2023 }
2024 tmp = ulist_alloc(GFP_NOFS);
2025 if (!tmp) {
2026 ret = -ENOMEM;
2027 goto out_free;
2028 }
2029
2030 mutex_lock(&fs_info->qgroup_rescan_lock);
2031 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2032 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2033 mutex_unlock(&fs_info->qgroup_rescan_lock);
2034 ret = 0;
2035 goto out_free;
2036 }
2037 }
2038 mutex_unlock(&fs_info->qgroup_rescan_lock);
2039
2040 spin_lock(&fs_info->qgroup_lock);
2041 seq = fs_info->qgroup_seq;
2042
2043 /* Update old refcnts using old_roots */
2044 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2045 UPDATE_OLD);
2046 if (ret < 0)
2047 goto out;
2048
2049 /* Update new refcnts using new_roots */
2050 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2051 UPDATE_NEW);
2052 if (ret < 0)
2053 goto out;
2054
2055 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2056 num_bytes, seq);
2057
2058 /*
2059 * Bump qgroup_seq to avoid seq overlap
2060 */
2061 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2062out:
2063 spin_unlock(&fs_info->qgroup_lock);
2064out_free:
2065 ulist_free(tmp);
2066 ulist_free(qgroups);
2067 ulist_free(old_roots);
2068 ulist_free(new_roots);
2069 return ret;
2070}
2071
Nikolay Borisov460fb202018-03-15 16:00:25 +02002072int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
Qu Wenruo550d7a22015-04-16 15:37:33 +08002073{
Nikolay Borisov460fb202018-03-15 16:00:25 +02002074 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002075 struct btrfs_qgroup_extent_record *record;
2076 struct btrfs_delayed_ref_root *delayed_refs;
2077 struct ulist *new_roots = NULL;
2078 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08002079 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002080 int ret = 0;
2081
2082 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08002083 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002084 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2085 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2086 node);
2087
Jeff Mahoneybc074522016-06-09 17:27:55 -04002088 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002089
Qu Wenruo550d7a22015-04-16 15:37:33 +08002090 if (!ret) {
2091 /*
Qu Wenruod1b8b942017-02-27 15:10:35 +08002092 * Old roots should be searched when inserting qgroup
2093 * extent record
2094 */
2095 if (WARN_ON(!record->old_roots)) {
2096 /* Search commit root to find old_roots */
2097 ret = btrfs_find_all_roots(NULL, fs_info,
2098 record->bytenr, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002099 &record->old_roots, false);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002100 if (ret < 0)
2101 goto cleanup;
2102 }
2103
2104 /*
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002105 * Use SEQ_LAST as time_seq to do special search, which
Qu Wenruo550d7a22015-04-16 15:37:33 +08002106 * doesn't lock tree or delayed_refs and search current
2107 * root. It's safe inside commit_transaction().
2108 */
2109 ret = btrfs_find_all_roots(trans, fs_info,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002110 record->bytenr, SEQ_LAST, &new_roots, false);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002111 if (ret < 0)
2112 goto cleanup;
Qu Wenruod1b8b942017-02-27 15:10:35 +08002113 if (qgroup_to_skip) {
Qu Wenruo9086db82015-04-20 09:53:50 +08002114 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002115 ulist_del(record->old_roots, qgroup_to_skip,
2116 0);
2117 }
Qu Wenruo550d7a22015-04-16 15:37:33 +08002118 ret = btrfs_qgroup_account_extent(trans, fs_info,
2119 record->bytenr, record->num_bytes,
2120 record->old_roots, new_roots);
2121 record->old_roots = NULL;
2122 new_roots = NULL;
2123 }
2124cleanup:
2125 ulist_free(record->old_roots);
2126 ulist_free(new_roots);
2127 new_roots = NULL;
2128 rb_erase(node, &delayed_refs->dirty_extent_root);
2129 kfree(record);
2130
2131 }
2132 return ret;
2133}
2134
Josef Bacikfcebe452014-05-13 17:30:47 -07002135/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002136 * called from commit_transaction. Writes all changed qgroups to disk.
2137 */
2138int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2139 struct btrfs_fs_info *fs_info)
2140{
2141 struct btrfs_root *quota_root = fs_info->quota_root;
2142 int ret = 0;
2143
2144 if (!quota_root)
Nikolay Borisov5d235152018-01-31 10:52:04 +02002145 return ret;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002146
2147 spin_lock(&fs_info->qgroup_lock);
2148 while (!list_empty(&fs_info->dirty_qgroups)) {
2149 struct btrfs_qgroup *qgroup;
2150 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2151 struct btrfs_qgroup, dirty);
2152 list_del_init(&qgroup->dirty);
2153 spin_unlock(&fs_info->qgroup_lock);
2154 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2155 if (ret)
2156 fs_info->qgroup_flags |=
2157 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05002158 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2159 if (ret)
2160 fs_info->qgroup_flags |=
2161 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002162 spin_lock(&fs_info->qgroup_lock);
2163 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002164 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02002165 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2166 else
2167 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2168 spin_unlock(&fs_info->qgroup_lock);
2169
2170 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2171 if (ret)
2172 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2173
Arne Jansenbed92ea2012-06-28 18:03:02 +02002174 return ret;
2175}
2176
2177/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002178 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002179 * when a snapshot or a subvolume is created. Throwing an error will
2180 * cause a transaction abort so we take extra care here to only error
2181 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02002182 */
2183int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2184 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2185 struct btrfs_qgroup_inherit *inherit)
2186{
2187 int ret = 0;
2188 int i;
2189 u64 *i_qgroups;
2190 struct btrfs_root *quota_root = fs_info->quota_root;
2191 struct btrfs_qgroup *srcgroup;
2192 struct btrfs_qgroup *dstgroup;
2193 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002194 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002195
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002196 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04002197 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002198 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002199
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002200 if (!quota_root) {
2201 ret = -EINVAL;
2202 goto out;
2203 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002204
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002205 if (inherit) {
2206 i_qgroups = (u64 *)(inherit + 1);
2207 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2208 2 * inherit->num_excl_copies;
2209 for (i = 0; i < nums; ++i) {
2210 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05002211
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002212 /*
2213 * Zero out invalid groups so we can ignore
2214 * them later.
2215 */
2216 if (!srcgroup ||
2217 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2218 *i_qgroups = 0ULL;
2219
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002220 ++i_qgroups;
2221 }
2222 }
2223
Arne Jansenbed92ea2012-06-28 18:03:02 +02002224 /*
2225 * create a tracking group for the subvol itself
2226 */
2227 ret = add_qgroup_item(trans, quota_root, objectid);
2228 if (ret)
2229 goto out;
2230
Arne Jansenbed92ea2012-06-28 18:03:02 +02002231 if (srcid) {
2232 struct btrfs_root *srcroot;
2233 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002234
2235 srckey.objectid = srcid;
2236 srckey.type = BTRFS_ROOT_ITEM_KEY;
2237 srckey.offset = (u64)-1;
2238 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2239 if (IS_ERR(srcroot)) {
2240 ret = PTR_ERR(srcroot);
2241 goto out;
2242 }
2243
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002244 level_size = fs_info->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002245 }
2246
2247 /*
2248 * add qgroup to all inherited groups
2249 */
2250 if (inherit) {
2251 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002252 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2253 if (*i_qgroups == 0)
2254 continue;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002255 ret = add_qgroup_relation_item(trans, quota_root,
2256 objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002257 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002258 goto out;
2259 ret = add_qgroup_relation_item(trans, quota_root,
2260 *i_qgroups, objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002261 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002262 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002263 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002264 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002265 }
2266
2267
2268 spin_lock(&fs_info->qgroup_lock);
2269
2270 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002271 if (IS_ERR(dstgroup)) {
2272 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002273 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002274 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002275
Dongsheng Yange8c85412014-11-20 20:58:34 -05002276 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05002277 dstgroup->lim_flags = inherit->lim.flags;
2278 dstgroup->max_rfer = inherit->lim.max_rfer;
2279 dstgroup->max_excl = inherit->lim.max_excl;
2280 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2281 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05002282
2283 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2284 if (ret) {
2285 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002286 btrfs_info(fs_info,
2287 "unable to update quota limit for %llu",
2288 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002289 goto unlock;
2290 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002291 }
2292
Arne Jansenbed92ea2012-06-28 18:03:02 +02002293 if (srcid) {
2294 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002295 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002296 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002297
2298 /*
2299 * We call inherit after we clone the root in order to make sure
2300 * our counts don't go crazy, so at this point the only
2301 * difference between the two roots should be the root node.
2302 */
2303 dstgroup->rfer = srcgroup->rfer;
2304 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2305 dstgroup->excl = level_size;
2306 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002307 srcgroup->excl = level_size;
2308 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002309
2310 /* inherit the limit info */
2311 dstgroup->lim_flags = srcgroup->lim_flags;
2312 dstgroup->max_rfer = srcgroup->max_rfer;
2313 dstgroup->max_excl = srcgroup->max_excl;
2314 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2315 dstgroup->rsv_excl = srcgroup->rsv_excl;
2316
Arne Jansenbed92ea2012-06-28 18:03:02 +02002317 qgroup_dirty(fs_info, dstgroup);
2318 qgroup_dirty(fs_info, srcgroup);
2319 }
2320
Chris Masonf3a87f12012-09-14 20:06:30 -04002321 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002322 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002323
2324 i_qgroups = (u64 *)(inherit + 1);
2325 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002326 if (*i_qgroups) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002327 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002328 if (ret)
2329 goto unlock;
2330 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002331 ++i_qgroups;
2332 }
2333
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002334 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002335 struct btrfs_qgroup *src;
2336 struct btrfs_qgroup *dst;
2337
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002338 if (!i_qgroups[0] || !i_qgroups[1])
2339 continue;
2340
Arne Jansenbed92ea2012-06-28 18:03:02 +02002341 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2342 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2343
2344 if (!src || !dst) {
2345 ret = -EINVAL;
2346 goto unlock;
2347 }
2348
2349 dst->rfer = src->rfer - level_size;
2350 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002351 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002352 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002353 struct btrfs_qgroup *src;
2354 struct btrfs_qgroup *dst;
2355
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002356 if (!i_qgroups[0] || !i_qgroups[1])
2357 continue;
2358
Arne Jansenbed92ea2012-06-28 18:03:02 +02002359 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2360 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2361
2362 if (!src || !dst) {
2363 ret = -EINVAL;
2364 goto unlock;
2365 }
2366
2367 dst->excl = src->excl + level_size;
2368 dst->excl_cmpr = src->excl_cmpr + level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002369 }
2370
2371unlock:
2372 spin_unlock(&fs_info->qgroup_lock);
2373out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002374 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002375 return ret;
2376}
2377
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002378static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2379{
2380 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002381 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002382 return false;
2383
2384 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002385 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002386 return false;
2387
2388 return true;
2389}
2390
Qu Wenruodba21322017-12-12 15:34:25 +08002391static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2392 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002393{
2394 struct btrfs_root *quota_root;
2395 struct btrfs_qgroup *qgroup;
2396 struct btrfs_fs_info *fs_info = root->fs_info;
2397 u64 ref_root = root->root_key.objectid;
2398 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002399 struct ulist_node *unode;
2400 struct ulist_iterator uiter;
2401
2402 if (!is_fstree(ref_root))
2403 return 0;
2404
2405 if (num_bytes == 0)
2406 return 0;
Sargun Dhillonf29efe22017-05-11 21:17:33 +00002407
2408 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2409 capable(CAP_SYS_RESOURCE))
2410 enforce = false;
2411
Arne Jansenbed92ea2012-06-28 18:03:02 +02002412 spin_lock(&fs_info->qgroup_lock);
2413 quota_root = fs_info->quota_root;
2414 if (!quota_root)
2415 goto out;
2416
2417 qgroup = find_qgroup_rb(fs_info, ref_root);
2418 if (!qgroup)
2419 goto out;
2420
2421 /*
2422 * in a first step, we check all affected qgroups if any limits would
2423 * be exceeded
2424 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002425 ulist_reinit(fs_info->qgroup_ulist);
2426 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002427 qgroup_to_aux(qgroup), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002428 if (ret < 0)
2429 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002430 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002431 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002432 struct btrfs_qgroup *qg;
2433 struct btrfs_qgroup_list *glist;
2434
David Sterbaef2fff62016-10-26 16:23:50 +02002435 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002436
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002437 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002438 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002439 goto out;
2440 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002441
2442 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002443 ret = ulist_add(fs_info->qgroup_ulist,
2444 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002445 qgroup_to_aux(glist->group), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002446 if (ret < 0)
2447 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002448 }
2449 }
Wang Shilong3c971852013-04-17 14:00:36 +00002450 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002451 /*
2452 * no limits exceeded, now record the reservation into all qgroups
2453 */
2454 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002455 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002456 struct btrfs_qgroup *qg;
2457
David Sterbaef2fff62016-10-26 16:23:50 +02002458 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002459
Qu Wenruo64ee4e72017-12-12 15:34:27 +08002460 trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
2461 qgroup_rsv_add(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002462 }
2463
2464out:
2465 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002466 return ret;
2467}
2468
Qu Wenruoe1211d02017-12-12 15:34:30 +08002469/*
2470 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
2471 * qgroup).
2472 *
2473 * Will handle all higher level qgroup too.
2474 *
2475 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
2476 * This special case is only used for META_PERTRANS type.
2477 */
Qu Wenruo297d7502015-09-08 17:08:37 +08002478void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
Qu Wenruod4e5c922017-12-12 15:34:23 +08002479 u64 ref_root, u64 num_bytes,
2480 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002481{
2482 struct btrfs_root *quota_root;
2483 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002484 struct ulist_node *unode;
2485 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00002486 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002487
2488 if (!is_fstree(ref_root))
2489 return;
2490
2491 if (num_bytes == 0)
2492 return;
2493
Qu Wenruoe1211d02017-12-12 15:34:30 +08002494 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
2495 WARN(1, "%s: Invalid type to free", __func__);
2496 return;
2497 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002498 spin_lock(&fs_info->qgroup_lock);
2499
2500 quota_root = fs_info->quota_root;
2501 if (!quota_root)
2502 goto out;
2503
2504 qgroup = find_qgroup_rb(fs_info, ref_root);
2505 if (!qgroup)
2506 goto out;
2507
Qu Wenruoe1211d02017-12-12 15:34:30 +08002508 if (num_bytes == (u64)-1)
Qu Wenruo82874752017-12-12 15:34:34 +08002509 /*
2510 * We're freeing all pertrans rsv, get reserved value from
2511 * level 0 qgroup as real num_bytes to free.
2512 */
Qu Wenruoe1211d02017-12-12 15:34:30 +08002513 num_bytes = qgroup->rsv.values[type];
2514
Wang Shilong1e8f9152013-05-06 11:03:27 +00002515 ulist_reinit(fs_info->qgroup_ulist);
2516 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002517 qgroup_to_aux(qgroup), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002518 if (ret < 0)
2519 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002520 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002521 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002522 struct btrfs_qgroup *qg;
2523 struct btrfs_qgroup_list *glist;
2524
David Sterbaef2fff62016-10-26 16:23:50 +02002525 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002526
Qu Wenruo64ee4e72017-12-12 15:34:27 +08002527 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
2528 qgroup_rsv_release(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002529
2530 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002531 ret = ulist_add(fs_info->qgroup_ulist,
2532 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002533 qgroup_to_aux(glist->group), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002534 if (ret < 0)
2535 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002536 }
2537 }
2538
2539out:
2540 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002541}
2542
Jan Schmidt2f232032013-04-25 16:04:51 +00002543/*
2544 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08002545 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00002546 */
2547static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002548qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002549 struct btrfs_trans_handle *trans)
Jan Schmidt2f232032013-04-25 16:04:51 +00002550{
2551 struct btrfs_key found;
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002552 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002553 struct ulist *roots = NULL;
David Sterba3284da72015-02-25 15:47:32 +01002554 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Josef Bacikfcebe452014-05-13 17:30:47 -07002555 u64 num_bytes;
Jan Schmidt2f232032013-04-25 16:04:51 +00002556 int slot;
2557 int ret;
2558
Jan Schmidt2f232032013-04-25 16:04:51 +00002559 mutex_lock(&fs_info->qgroup_rescan_lock);
2560 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2561 &fs_info->qgroup_rescan_progress,
2562 path, 1, 0);
2563
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002564 btrfs_debug(fs_info,
2565 "current progress key (%llu %u %llu), search_slot ret %d",
2566 fs_info->qgroup_rescan_progress.objectid,
2567 fs_info->qgroup_rescan_progress.type,
2568 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002569
2570 if (ret) {
2571 /*
2572 * The rescan is about to end, we will not be scanning any
2573 * further blocks. We cannot unset the RESCAN flag here, because
2574 * we want to commit the transaction if everything went well.
2575 * To make the live accounting work in this phase, we set our
2576 * scan progress pointer such that every real extent objectid
2577 * will be smaller.
2578 */
2579 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2580 btrfs_release_path(path);
2581 mutex_unlock(&fs_info->qgroup_rescan_lock);
2582 return ret;
2583 }
2584
2585 btrfs_item_key_to_cpu(path->nodes[0], &found,
2586 btrfs_header_nritems(path->nodes[0]) - 1);
2587 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2588
2589 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002590 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2591 if (!scratch_leaf) {
2592 ret = -ENOMEM;
2593 mutex_unlock(&fs_info->qgroup_rescan_lock);
2594 goto out;
2595 }
2596 extent_buffer_get(scratch_leaf);
2597 btrfs_tree_read_lock(scratch_leaf);
2598 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
Jan Schmidt2f232032013-04-25 16:04:51 +00002599 slot = path->slots[0];
2600 btrfs_release_path(path);
2601 mutex_unlock(&fs_info->qgroup_rescan_lock);
2602
2603 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2604 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002605 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2606 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002607 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002608 if (found.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04002609 num_bytes = fs_info->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002610 else
2611 num_bytes = found.offset;
2612
Josef Bacikfcebe452014-05-13 17:30:47 -07002613 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002614 &roots, false);
Jan Schmidt2f232032013-04-25 16:04:51 +00002615 if (ret < 0)
2616 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08002617 /* For rescan, just pass old_roots as NULL */
2618 ret = btrfs_qgroup_account_extent(trans, fs_info,
2619 found.objectid, num_bytes, NULL, roots);
2620 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00002621 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002622 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002623out:
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002624 if (scratch_leaf) {
2625 btrfs_tree_read_unlock_blocking(scratch_leaf);
2626 free_extent_buffer(scratch_leaf);
2627 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002628 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2629
2630 return ret;
2631}
2632
Qu Wenruod458b052014-02-28 10:46:19 +08002633static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002634{
Jan Schmidtb382a322013-05-28 15:47:24 +00002635 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2636 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002637 struct btrfs_path *path;
2638 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002639 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002640 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002641
2642 path = btrfs_alloc_path();
2643 if (!path)
2644 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002645
2646 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08002647 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002648 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2649 if (IS_ERR(trans)) {
2650 err = PTR_ERR(trans);
2651 break;
2652 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002653 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002654 err = -EINTR;
2655 } else {
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002656 err = qgroup_rescan_leaf(fs_info, path, trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002657 }
2658 if (err > 0)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002659 btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002660 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002661 btrfs_end_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002662 }
2663
2664out:
Jan Schmidt2f232032013-04-25 16:04:51 +00002665 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002666
2667 mutex_lock(&fs_info->qgroup_rescan_lock);
Justin Maggard7343dd62015-11-04 15:56:16 -08002668 if (!btrfs_fs_closing(fs_info))
2669 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidt2f232032013-04-25 16:04:51 +00002670
Qu Wenruo33931682015-02-27 16:24:24 +08002671 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00002672 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2673 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2674 } else if (err < 0) {
2675 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2676 }
2677 mutex_unlock(&fs_info->qgroup_rescan_lock);
2678
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002679 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002680 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002681 * qgroup info.
2682 */
2683 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2684 if (IS_ERR(trans)) {
2685 err = PTR_ERR(trans);
2686 btrfs_err(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002687 "fail to start transaction for status update: %d",
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002688 err);
2689 goto done;
2690 }
2691 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2692 if (ret < 0) {
2693 err = ret;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002694 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002695 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002696 btrfs_end_transaction(trans);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002697
Justin Maggard7343dd62015-11-04 15:56:16 -08002698 if (btrfs_fs_closing(fs_info)) {
2699 btrfs_info(fs_info, "qgroup scan paused");
2700 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002701 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08002702 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00002703 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002704 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002705 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002706
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002707done:
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002708 mutex_lock(&fs_info->qgroup_rescan_lock);
2709 fs_info->qgroup_rescan_running = false;
2710 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002711 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002712}
2713
Jan Schmidtb382a322013-05-28 15:47:24 +00002714/*
2715 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2716 * memory required for the rescan context.
2717 */
2718static int
2719qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2720 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002721{
2722 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002723
Jan Schmidtb382a322013-05-28 15:47:24 +00002724 if (!init_flags &&
2725 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2726 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2727 ret = -EINVAL;
2728 goto err;
2729 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002730
2731 mutex_lock(&fs_info->qgroup_rescan_lock);
2732 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002733
2734 if (init_flags) {
2735 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2736 ret = -EINPROGRESS;
2737 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2738 ret = -EINVAL;
2739
2740 if (ret) {
2741 spin_unlock(&fs_info->qgroup_lock);
2742 mutex_unlock(&fs_info->qgroup_rescan_lock);
2743 goto err;
2744 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002745 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2746 }
2747
2748 memset(&fs_info->qgroup_rescan_progress, 0,
2749 sizeof(fs_info->qgroup_rescan_progress));
2750 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00002751 init_completion(&fs_info->qgroup_rescan_completion);
Filipe Manana8d9edda2016-11-24 02:09:04 +00002752 fs_info->qgroup_rescan_running = true;
Jan Schmidtb382a322013-05-28 15:47:24 +00002753
2754 spin_unlock(&fs_info->qgroup_lock);
2755 mutex_unlock(&fs_info->qgroup_rescan_lock);
2756
Jan Schmidtb382a322013-05-28 15:47:24 +00002757 memset(&fs_info->qgroup_rescan_work, 0,
2758 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002759 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002760 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002761 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002762
Jan Schmidt2f232032013-04-25 16:04:51 +00002763 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002764err:
Frank Holtonefe120a2013-12-20 11:37:06 -05002765 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002766 return ret;
2767 }
2768
Jan Schmidtb382a322013-05-28 15:47:24 +00002769 return 0;
2770}
Jan Schmidt2f232032013-04-25 16:04:51 +00002771
Jan Schmidtb382a322013-05-28 15:47:24 +00002772static void
2773qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2774{
2775 struct rb_node *n;
2776 struct btrfs_qgroup *qgroup;
2777
2778 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002779 /* clear all current qgroup tracking information */
2780 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2781 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2782 qgroup->rfer = 0;
2783 qgroup->rfer_cmpr = 0;
2784 qgroup->excl = 0;
2785 qgroup->excl_cmpr = 0;
2786 }
2787 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002788}
Jan Schmidt2f232032013-04-25 16:04:51 +00002789
Jan Schmidtb382a322013-05-28 15:47:24 +00002790int
2791btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2792{
2793 int ret = 0;
2794 struct btrfs_trans_handle *trans;
2795
2796 ret = qgroup_rescan_init(fs_info, 0, 1);
2797 if (ret)
2798 return ret;
2799
2800 /*
2801 * We have set the rescan_progress to 0, which means no more
2802 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2803 * However, btrfs_qgroup_account_ref may be right after its call
2804 * to btrfs_find_all_roots, in which case it would still do the
2805 * accounting.
2806 * To solve this, we're committing the transaction, which will
2807 * ensure we run all delayed refs and only after that, we are
2808 * going to clear all tracking information for a clean start.
2809 */
2810
2811 trans = btrfs_join_transaction(fs_info->fs_root);
2812 if (IS_ERR(trans)) {
2813 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2814 return PTR_ERR(trans);
2815 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002816 ret = btrfs_commit_transaction(trans);
Jan Schmidtb382a322013-05-28 15:47:24 +00002817 if (ret) {
2818 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2819 return ret;
2820 }
2821
2822 qgroup_rescan_zero_tracking(fs_info);
2823
Qu Wenruofc97fab2014-02-28 10:46:16 +08002824 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2825 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002826
2827 return 0;
2828}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002829
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002830int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2831 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002832{
2833 int running;
2834 int ret = 0;
2835
2836 mutex_lock(&fs_info->qgroup_rescan_lock);
2837 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002838 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002839 spin_unlock(&fs_info->qgroup_lock);
2840 mutex_unlock(&fs_info->qgroup_rescan_lock);
2841
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002842 if (!running)
2843 return 0;
2844
2845 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002846 ret = wait_for_completion_interruptible(
2847 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002848 else
2849 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002850
2851 return ret;
2852}
Jan Schmidtb382a322013-05-28 15:47:24 +00002853
2854/*
2855 * this is only called from open_ctree where we're still single threaded, thus
2856 * locking is omitted here.
2857 */
2858void
2859btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2860{
2861 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002862 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2863 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002864}
Qu Wenruo52472552015-10-12 16:05:40 +08002865
2866/*
2867 * Reserve qgroup space for range [start, start + len).
2868 *
2869 * This function will either reserve space from related qgroups or doing
2870 * nothing if the range is already reserved.
2871 *
2872 * Return 0 for successful reserve
2873 * Return <0 for error (including -EQUOT)
2874 *
2875 * NOTE: this function may sleep for memory allocation.
Qu Wenruo364ecf32017-02-27 15:10:38 +08002876 * if btrfs_qgroup_reserve_data() is called multiple times with
2877 * same @reserved, caller must ensure when error happens it's OK
2878 * to free *ALL* reserved space.
Qu Wenruo52472552015-10-12 16:05:40 +08002879 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08002880int btrfs_qgroup_reserve_data(struct inode *inode,
2881 struct extent_changeset **reserved_ret, u64 start,
2882 u64 len)
Qu Wenruo52472552015-10-12 16:05:40 +08002883{
2884 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo52472552015-10-12 16:05:40 +08002885 struct ulist_node *unode;
2886 struct ulist_iterator uiter;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002887 struct extent_changeset *reserved;
2888 u64 orig_reserved;
2889 u64 to_reserve;
Qu Wenruo52472552015-10-12 16:05:40 +08002890 int ret;
2891
Josef Bacikafcdd122016-09-02 15:40:02 -04002892 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2893 !is_fstree(root->objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08002894 return 0;
2895
Qu Wenruo364ecf32017-02-27 15:10:38 +08002896 /* @reserved parameter is mandatory for qgroup */
2897 if (WARN_ON(!reserved_ret))
2898 return -EINVAL;
2899 if (!*reserved_ret) {
2900 *reserved_ret = extent_changeset_alloc();
2901 if (!*reserved_ret)
2902 return -ENOMEM;
2903 }
2904 reserved = *reserved_ret;
2905 /* Record already reserved space */
2906 orig_reserved = reserved->bytes_changed;
Qu Wenruo52472552015-10-12 16:05:40 +08002907 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002908 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2909
2910 /* Newly reserved space */
2911 to_reserve = reserved->bytes_changed - orig_reserved;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002912 trace_btrfs_qgroup_reserve_data(inode, start, len,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002913 to_reserve, QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08002914 if (ret < 0)
2915 goto cleanup;
Qu Wenruodba21322017-12-12 15:34:25 +08002916 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo52472552015-10-12 16:05:40 +08002917 if (ret < 0)
2918 goto cleanup;
2919
Qu Wenruo52472552015-10-12 16:05:40 +08002920 return ret;
2921
2922cleanup:
Qu Wenruo364ecf32017-02-27 15:10:38 +08002923 /* cleanup *ALL* already reserved ranges */
Qu Wenruo52472552015-10-12 16:05:40 +08002924 ULIST_ITER_INIT(&uiter);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002925 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
Qu Wenruo52472552015-10-12 16:05:40 +08002926 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
David Sterbaae0f1622017-10-31 16:37:52 +01002927 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002928 extent_changeset_release(reserved);
Qu Wenruo52472552015-10-12 16:05:40 +08002929 return ret;
2930}
Qu Wenruof695fdc2015-10-12 16:28:06 +08002931
Qu Wenruobc42bda2017-02-27 15:10:39 +08002932/* Free ranges specified by @reserved, normally in error path */
2933static int qgroup_free_reserved_data(struct inode *inode,
2934 struct extent_changeset *reserved, u64 start, u64 len)
2935{
2936 struct btrfs_root *root = BTRFS_I(inode)->root;
2937 struct ulist_node *unode;
2938 struct ulist_iterator uiter;
2939 struct extent_changeset changeset;
2940 int freed = 0;
2941 int ret;
2942
2943 extent_changeset_init(&changeset);
2944 len = round_up(start + len, root->fs_info->sectorsize);
2945 start = round_down(start, root->fs_info->sectorsize);
2946
2947 ULIST_ITER_INIT(&uiter);
2948 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
2949 u64 range_start = unode->val;
2950 /* unode->aux is the inclusive end */
2951 u64 range_len = unode->aux - range_start + 1;
2952 u64 free_start;
2953 u64 free_len;
2954
2955 extent_changeset_release(&changeset);
2956
2957 /* Only free range in range [start, start + len) */
2958 if (range_start >= start + len ||
2959 range_start + range_len <= start)
2960 continue;
2961 free_start = max(range_start, start);
2962 free_len = min(start + len, range_start + range_len) -
2963 free_start;
2964 /*
2965 * TODO: To also modify reserved->ranges_reserved to reflect
2966 * the modification.
2967 *
2968 * However as long as we free qgroup reserved according to
2969 * EXTENT_QGROUP_RESERVED, we won't double free.
2970 * So not need to rush.
2971 */
2972 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
2973 free_start, free_start + free_len - 1,
2974 EXTENT_QGROUP_RESERVED, &changeset);
2975 if (ret < 0)
2976 goto out;
2977 freed += changeset.bytes_changed;
2978 }
Qu Wenruod4e5c922017-12-12 15:34:23 +08002979 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
2980 BTRFS_QGROUP_RSV_DATA);
Qu Wenruobc42bda2017-02-27 15:10:39 +08002981 ret = freed;
2982out:
2983 extent_changeset_release(&changeset);
2984 return ret;
2985}
2986
2987static int __btrfs_qgroup_release_data(struct inode *inode,
2988 struct extent_changeset *reserved, u64 start, u64 len,
2989 int free)
Qu Wenruof695fdc2015-10-12 16:28:06 +08002990{
2991 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002992 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08002993 int ret;
2994
Qu Wenruobc42bda2017-02-27 15:10:39 +08002995 /* In release case, we shouldn't have @reserved */
2996 WARN_ON(!free && reserved);
2997 if (free && reserved)
2998 return qgroup_free_reserved_data(inode, reserved, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002999 extent_changeset_init(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003000 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterbaf734c442016-04-26 23:54:39 +02003001 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003002 if (ret < 0)
3003 goto out;
3004
Qu Wenruod51ea5d2017-03-13 15:52:09 +08003005 if (free)
3006 trace_op = QGROUP_FREE;
3007 trace_btrfs_qgroup_release_data(inode, start, len,
3008 changeset.bytes_changed, trace_op);
3009 if (free)
David Sterba0b08e1f2017-02-13 14:24:35 +01003010 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3011 BTRFS_I(inode)->root->objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003012 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08003013 ret = changeset.bytes_changed;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003014out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08003015 extent_changeset_release(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003016 return ret;
3017}
3018
3019/*
3020 * Free a reserved space range from io_tree and related qgroups
3021 *
3022 * Should be called when a range of pages get invalidated before reaching disk.
3023 * Or for error cleanup case.
Qu Wenruobc42bda2017-02-27 15:10:39 +08003024 * if @reserved is given, only reserved range in [@start, @start + @len) will
3025 * be freed.
Qu Wenruof695fdc2015-10-12 16:28:06 +08003026 *
3027 * For data written to disk, use btrfs_qgroup_release_data().
3028 *
3029 * NOTE: This function may sleep for memory allocation.
3030 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003031int btrfs_qgroup_free_data(struct inode *inode,
3032 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003033{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003034 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003035}
3036
3037/*
3038 * Release a reserved space range from io_tree only.
3039 *
3040 * Should be called when a range of pages get written to disk and corresponding
3041 * FILE_EXTENT is inserted into corresponding root.
3042 *
3043 * Since new qgroup accounting framework will only update qgroup numbers at
3044 * commit_transaction() time, its reserved space shouldn't be freed from
3045 * related qgroups.
3046 *
3047 * But we should release the range from io_tree, to allow further write to be
3048 * COWed.
3049 *
3050 * NOTE: This function may sleep for memory allocation.
3051 */
3052int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3053{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003054 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003055}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003056
Qu Wenruo82874752017-12-12 15:34:34 +08003057static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3058 enum btrfs_qgroup_rsv_type type)
3059{
3060 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3061 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3062 return;
3063 if (num_bytes == 0)
3064 return;
3065
3066 spin_lock(&root->qgroup_meta_rsv_lock);
3067 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3068 root->qgroup_meta_rsv_prealloc += num_bytes;
3069 else
3070 root->qgroup_meta_rsv_pertrans += num_bytes;
3071 spin_unlock(&root->qgroup_meta_rsv_lock);
3072}
3073
3074static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3075 enum btrfs_qgroup_rsv_type type)
3076{
3077 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3078 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3079 return 0;
3080 if (num_bytes == 0)
3081 return 0;
3082
3083 spin_lock(&root->qgroup_meta_rsv_lock);
3084 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3085 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3086 num_bytes);
3087 root->qgroup_meta_rsv_prealloc -= num_bytes;
3088 } else {
3089 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3090 num_bytes);
3091 root->qgroup_meta_rsv_pertrans -= num_bytes;
3092 }
3093 spin_unlock(&root->qgroup_meta_rsv_lock);
3094 return num_bytes;
3095}
3096
Qu Wenruo733e03a2017-12-12 15:34:29 +08003097int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3098 enum btrfs_qgroup_rsv_type type, bool enforce)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003099{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003100 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003101 int ret;
3102
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003103 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003104 !is_fstree(root->objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003105 return 0;
3106
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003107 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003108 trace_qgroup_meta_reserve(root, type, (s64)num_bytes);
Qu Wenruo733e03a2017-12-12 15:34:29 +08003109 ret = qgroup_reserve(root, num_bytes, enforce, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003110 if (ret < 0)
3111 return ret;
Qu Wenruo82874752017-12-12 15:34:34 +08003112 /*
3113 * Record what we have reserved into root.
3114 *
3115 * To avoid quota disabled->enabled underflow.
3116 * In that case, we may try to free space we haven't reserved
3117 * (since quota was disabled), so record what we reserved into root.
3118 * And ensure later release won't underflow this number.
3119 */
3120 add_root_meta_rsv(root, num_bytes, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003121 return ret;
3122}
3123
Qu Wenruo733e03a2017-12-12 15:34:29 +08003124void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003125{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003126 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003127
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003129 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003130 return;
3131
Qu Wenruoe1211d02017-12-12 15:34:30 +08003132 /* TODO: Update trace point to handle such free */
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003133 trace_qgroup_meta_free_all_pertrans(root);
Qu Wenruoe1211d02017-12-12 15:34:30 +08003134 /* Special value -1 means to free all reserved space */
3135 btrfs_qgroup_free_refroot(fs_info, root->objectid, (u64)-1,
Qu Wenruo733e03a2017-12-12 15:34:29 +08003136 BTRFS_QGROUP_RSV_META_PERTRANS);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003137}
3138
Qu Wenruo733e03a2017-12-12 15:34:29 +08003139void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3140 enum btrfs_qgroup_rsv_type type)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 struct btrfs_fs_info *fs_info = root->fs_info;
3143
3144 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003145 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003146 return;
3147
Qu Wenruo82874752017-12-12 15:34:34 +08003148 /*
3149 * reservation for META_PREALLOC can happen before quota is enabled,
3150 * which can lead to underflow.
3151 * Here ensure we will only free what we really have reserved.
3152 */
3153 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003155 trace_qgroup_meta_reserve(root, type, -(s64)num_bytes);
Qu Wenruo733e03a2017-12-12 15:34:29 +08003156 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003157}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003158
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003159static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3160 int num_bytes)
3161{
3162 struct btrfs_root *quota_root = fs_info->quota_root;
3163 struct btrfs_qgroup *qgroup;
3164 struct ulist_node *unode;
3165 struct ulist_iterator uiter;
3166 int ret = 0;
3167
3168 if (num_bytes == 0)
3169 return;
3170 if (!quota_root)
3171 return;
3172
3173 spin_lock(&fs_info->qgroup_lock);
3174 qgroup = find_qgroup_rb(fs_info, ref_root);
3175 if (!qgroup)
3176 goto out;
3177 ulist_reinit(fs_info->qgroup_ulist);
3178 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003179 qgroup_to_aux(qgroup), GFP_ATOMIC);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003180 if (ret < 0)
3181 goto out;
3182 ULIST_ITER_INIT(&uiter);
3183 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3184 struct btrfs_qgroup *qg;
3185 struct btrfs_qgroup_list *glist;
3186
3187 qg = unode_aux_to_qgroup(unode);
3188
3189 qgroup_rsv_release(fs_info, qg, num_bytes,
3190 BTRFS_QGROUP_RSV_META_PREALLOC);
3191 qgroup_rsv_add(fs_info, qg, num_bytes,
3192 BTRFS_QGROUP_RSV_META_PERTRANS);
3193 list_for_each_entry(glist, &qg->groups, next_group) {
3194 ret = ulist_add(fs_info->qgroup_ulist,
3195 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003196 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003197 if (ret < 0)
3198 goto out;
3199 }
3200 }
3201out:
3202 spin_unlock(&fs_info->qgroup_lock);
3203}
3204
3205void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3206{
3207 struct btrfs_fs_info *fs_info = root->fs_info;
3208
3209 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3210 !is_fstree(root->objectid))
3211 return;
Qu Wenruo82874752017-12-12 15:34:34 +08003212 /* Same as btrfs_qgroup_free_meta_prealloc() */
3213 num_bytes = sub_root_meta_rsv(root, num_bytes,
3214 BTRFS_QGROUP_RSV_META_PREALLOC);
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003215 trace_qgroup_meta_convert(root, num_bytes);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003216 qgroup_convert_meta(fs_info, root->objectid, num_bytes);
3217}
3218
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003219/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003220 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003221 * time
3222 */
3223void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3224{
3225 struct extent_changeset changeset;
3226 struct ulist_node *unode;
3227 struct ulist_iterator iter;
3228 int ret;
3229
Qu Wenruo364ecf32017-02-27 15:10:38 +08003230 extent_changeset_init(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003231 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02003232 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003233
3234 WARN_ON(ret < 0);
3235 if (WARN_ON(changeset.bytes_changed)) {
3236 ULIST_ITER_INIT(&iter);
David Sterba53d32352017-02-13 13:42:29 +01003237 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003238 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3239 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3240 inode->i_ino, unode->val, unode->aux);
3241 }
David Sterba0b08e1f2017-02-13 14:24:35 +01003242 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3243 BTRFS_I(inode)->root->objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003244 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
David Sterba0b08e1f2017-02-13 14:24:35 +01003245
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003246 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08003247 extent_changeset_release(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003248}