blob: 99f2b9ce0f15ce9b3d3ff4ad6aa441bf0b784cc4 [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>
Qu Wenruoa514d632017-12-22 16:06:39 +080014#include <linux/sizes.h>
Arne Jansenbed92ea2012-06-28 18:03:02 +020015
16#include "ctree.h"
17#include "transaction.h"
18#include "disk-io.h"
19#include "locking.h"
20#include "ulist.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020021#include "backref.h"
Jan Schmidt2f232032013-04-25 16:04:51 +000022#include "extent_io.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070023#include "qgroup.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020024
Qu Wenruoe69bcee2015-04-17 10:23:16 +080025
Arne Jansenbed92ea2012-06-28 18:03:02 +020026/* TODO XXX FIXME
27 * - subvol delete -> delete when ref goes to 0? delete limits also?
28 * - reorganize keys
29 * - compressed
30 * - sync
Arne Jansenbed92ea2012-06-28 18:03:02 +020031 * - copy also limits on subvol creation
32 * - limit
33 * - caches fuer ulists
34 * - performance benchmarks
35 * - check all ioctl parameters
36 */
37
Qu Wenruof59c0342017-12-12 15:34:24 +080038/*
39 * Helpers to access qgroup reservation
40 *
41 * Callers should ensure the lock context and type are valid
42 */
43
44static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
45{
46 u64 ret = 0;
47 int i;
48
49 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
50 ret += qgroup->rsv.values[i];
51
52 return ret;
53}
54
55#ifdef CONFIG_BTRFS_DEBUG
56static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
57{
58 if (type == BTRFS_QGROUP_RSV_DATA)
59 return "data";
Qu Wenruo733e03a2017-12-12 15:34:29 +080060 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
61 return "meta_pertrans";
62 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
63 return "meta_prealloc";
Qu Wenruof59c0342017-12-12 15:34:24 +080064 return NULL;
65}
66#endif
67
Qu Wenruo64ee4e72017-12-12 15:34:27 +080068static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
69 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080070 enum btrfs_qgroup_rsv_type type)
71{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080072 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080073 qgroup->rsv.values[type] += num_bytes;
74}
75
Qu Wenruo64ee4e72017-12-12 15:34:27 +080076static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
77 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080078 enum btrfs_qgroup_rsv_type type)
79{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080080 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080081 if (qgroup->rsv.values[type] >= num_bytes) {
82 qgroup->rsv.values[type] -= num_bytes;
83 return;
84 }
85#ifdef CONFIG_BTRFS_DEBUG
86 WARN_RATELIMIT(1,
87 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
88 qgroup->qgroupid, qgroup_rsv_type_str(type),
89 qgroup->rsv.values[type], num_bytes);
90#endif
91 qgroup->rsv.values[type] = 0;
92}
93
Qu Wenruo64ee4e72017-12-12 15:34:27 +080094static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
95 struct btrfs_qgroup *dest,
96 struct btrfs_qgroup *src)
Qu Wenruof59c0342017-12-12 15:34:24 +080097{
98 int i;
99
100 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800101 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800102}
103
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800104static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
105 struct btrfs_qgroup *dest,
Qu Wenruof59c0342017-12-12 15:34:24 +0800106 struct btrfs_qgroup *src)
107{
108 int i;
109
110 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800111 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800112}
113
Qu Wenruo9c542132015-03-12 16:10:13 +0800114static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
115 int mod)
116{
117 if (qg->old_refcnt < seq)
118 qg->old_refcnt = seq;
119 qg->old_refcnt += mod;
120}
121
122static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
123 int mod)
124{
125 if (qg->new_refcnt < seq)
126 qg->new_refcnt = seq;
127 qg->new_refcnt += mod;
128}
129
130static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
131{
132 if (qg->old_refcnt < seq)
133 return 0;
134 return qg->old_refcnt - seq;
135}
136
137static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
138{
139 if (qg->new_refcnt < seq)
140 return 0;
141 return qg->new_refcnt - seq;
142}
143
Arne Jansenbed92ea2012-06-28 18:03:02 +0200144/*
145 * glue structure to represent the relations between qgroups.
146 */
147struct btrfs_qgroup_list {
148 struct list_head next_group;
149 struct list_head next_member;
150 struct btrfs_qgroup *group;
151 struct btrfs_qgroup *member;
152};
153
David Sterbaef2fff62016-10-26 16:23:50 +0200154static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
155{
156 return (u64)(uintptr_t)qg;
157}
158
159static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
160{
161 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
162}
Josef Bacikfcebe452014-05-13 17:30:47 -0700163
Jan Schmidtb382a322013-05-28 15:47:24 +0000164static int
165qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
166 int init_flags);
167static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000168
Wang Shilong58400fc2013-04-07 10:50:17 +0000169/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200170static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
171 u64 qgroupid)
172{
173 struct rb_node *n = fs_info->qgroup_tree.rb_node;
174 struct btrfs_qgroup *qgroup;
175
176 while (n) {
177 qgroup = rb_entry(n, struct btrfs_qgroup, node);
178 if (qgroup->qgroupid < qgroupid)
179 n = n->rb_left;
180 else if (qgroup->qgroupid > qgroupid)
181 n = n->rb_right;
182 else
183 return qgroup;
184 }
185 return NULL;
186}
187
188/* must be called with qgroup_lock held */
189static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
190 u64 qgroupid)
191{
192 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
193 struct rb_node *parent = NULL;
194 struct btrfs_qgroup *qgroup;
195
196 while (*p) {
197 parent = *p;
198 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
199
200 if (qgroup->qgroupid < qgroupid)
201 p = &(*p)->rb_left;
202 else if (qgroup->qgroupid > qgroupid)
203 p = &(*p)->rb_right;
204 else
205 return qgroup;
206 }
207
208 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
209 if (!qgroup)
210 return ERR_PTR(-ENOMEM);
211
212 qgroup->qgroupid = qgroupid;
213 INIT_LIST_HEAD(&qgroup->groups);
214 INIT_LIST_HEAD(&qgroup->members);
215 INIT_LIST_HEAD(&qgroup->dirty);
216
217 rb_link_node(&qgroup->node, parent, p);
218 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
219
220 return qgroup;
221}
222
Wang Shilong4082bd32013-08-14 09:13:36 +0800223static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200224{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200225 struct btrfs_qgroup_list *list;
226
Arne Jansenbed92ea2012-06-28 18:03:02 +0200227 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200228 while (!list_empty(&qgroup->groups)) {
229 list = list_first_entry(&qgroup->groups,
230 struct btrfs_qgroup_list, next_group);
231 list_del(&list->next_group);
232 list_del(&list->next_member);
233 kfree(list);
234 }
235
236 while (!list_empty(&qgroup->members)) {
237 list = list_first_entry(&qgroup->members,
238 struct btrfs_qgroup_list, next_member);
239 list_del(&list->next_group);
240 list_del(&list->next_member);
241 kfree(list);
242 }
243 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800244}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200245
Wang Shilong4082bd32013-08-14 09:13:36 +0800246/* must be called with qgroup_lock held */
247static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
248{
249 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
250
251 if (!qgroup)
252 return -ENOENT;
253
254 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
255 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200256 return 0;
257}
258
259/* must be called with qgroup_lock held */
260static int add_relation_rb(struct btrfs_fs_info *fs_info,
261 u64 memberid, u64 parentid)
262{
263 struct btrfs_qgroup *member;
264 struct btrfs_qgroup *parent;
265 struct btrfs_qgroup_list *list;
266
267 member = find_qgroup_rb(fs_info, memberid);
268 parent = find_qgroup_rb(fs_info, parentid);
269 if (!member || !parent)
270 return -ENOENT;
271
272 list = kzalloc(sizeof(*list), GFP_ATOMIC);
273 if (!list)
274 return -ENOMEM;
275
276 list->group = parent;
277 list->member = member;
278 list_add_tail(&list->next_group, &member->groups);
279 list_add_tail(&list->next_member, &parent->members);
280
281 return 0;
282}
283
284/* must be called with qgroup_lock held */
285static int del_relation_rb(struct btrfs_fs_info *fs_info,
286 u64 memberid, u64 parentid)
287{
288 struct btrfs_qgroup *member;
289 struct btrfs_qgroup *parent;
290 struct btrfs_qgroup_list *list;
291
292 member = find_qgroup_rb(fs_info, memberid);
293 parent = find_qgroup_rb(fs_info, parentid);
294 if (!member || !parent)
295 return -ENOENT;
296
297 list_for_each_entry(list, &member->groups, next_group) {
298 if (list->group == parent) {
299 list_del(&list->next_group);
300 list_del(&list->next_member);
301 kfree(list);
302 return 0;
303 }
304 }
305 return -ENOENT;
306}
307
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400308#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
309int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
310 u64 rfer, u64 excl)
311{
312 struct btrfs_qgroup *qgroup;
313
314 qgroup = find_qgroup_rb(fs_info, qgroupid);
315 if (!qgroup)
316 return -EINVAL;
317 if (qgroup->rfer != rfer || qgroup->excl != excl)
318 return -EINVAL;
319 return 0;
320}
321#endif
322
Arne Jansenbed92ea2012-06-28 18:03:02 +0200323/*
324 * The full config is read in one go, only called from open_ctree()
325 * It doesn't use any locking, as at this point we're still single-threaded
326 */
327int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
328{
329 struct btrfs_key key;
330 struct btrfs_key found_key;
331 struct btrfs_root *quota_root = fs_info->quota_root;
332 struct btrfs_path *path = NULL;
333 struct extent_buffer *l;
334 int slot;
335 int ret = 0;
336 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000337 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200338
Josef Bacikafcdd122016-09-02 15:40:02 -0400339 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +0200340 return 0;
341
David Sterba323b88f2017-02-13 12:10:20 +0100342 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000343 if (!fs_info->qgroup_ulist) {
344 ret = -ENOMEM;
345 goto out;
346 }
347
Arne Jansenbed92ea2012-06-28 18:03:02 +0200348 path = btrfs_alloc_path();
349 if (!path) {
350 ret = -ENOMEM;
351 goto out;
352 }
353
354 /* default this to quota off, in case no status key is found */
355 fs_info->qgroup_flags = 0;
356
357 /*
358 * pass 1: read status, all qgroup infos and limits
359 */
360 key.objectid = 0;
361 key.type = 0;
362 key.offset = 0;
363 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
364 if (ret)
365 goto out;
366
367 while (1) {
368 struct btrfs_qgroup *qgroup;
369
370 slot = path->slots[0];
371 l = path->nodes[0];
372 btrfs_item_key_to_cpu(l, &found_key, slot);
373
374 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
375 struct btrfs_qgroup_status_item *ptr;
376
377 ptr = btrfs_item_ptr(l, slot,
378 struct btrfs_qgroup_status_item);
379
380 if (btrfs_qgroup_status_version(l, ptr) !=
381 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500382 btrfs_err(fs_info,
383 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200384 goto out;
385 }
386 if (btrfs_qgroup_status_generation(l, ptr) !=
387 fs_info->generation) {
388 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500389 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400390 "qgroup generation mismatch, marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200391 }
392 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
393 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000394 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200395 goto next1;
396 }
397
398 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
399 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
400 goto next1;
401
402 qgroup = find_qgroup_rb(fs_info, found_key.offset);
403 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
404 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Geert Uytterhoevend41e36a2015-07-06 15:38:11 +0200405 btrfs_err(fs_info, "inconsistent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200406 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
407 }
408 if (!qgroup) {
409 qgroup = add_qgroup_rb(fs_info, found_key.offset);
410 if (IS_ERR(qgroup)) {
411 ret = PTR_ERR(qgroup);
412 goto out;
413 }
414 }
415 switch (found_key.type) {
416 case BTRFS_QGROUP_INFO_KEY: {
417 struct btrfs_qgroup_info_item *ptr;
418
419 ptr = btrfs_item_ptr(l, slot,
420 struct btrfs_qgroup_info_item);
421 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
422 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
423 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
424 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
425 /* generation currently unused */
426 break;
427 }
428 case BTRFS_QGROUP_LIMIT_KEY: {
429 struct btrfs_qgroup_limit_item *ptr;
430
431 ptr = btrfs_item_ptr(l, slot,
432 struct btrfs_qgroup_limit_item);
433 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
434 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
435 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
436 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
437 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
438 break;
439 }
440 }
441next1:
442 ret = btrfs_next_item(quota_root, path);
443 if (ret < 0)
444 goto out;
445 if (ret)
446 break;
447 }
448 btrfs_release_path(path);
449
450 /*
451 * pass 2: read all qgroup relations
452 */
453 key.objectid = 0;
454 key.type = BTRFS_QGROUP_RELATION_KEY;
455 key.offset = 0;
456 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
457 if (ret)
458 goto out;
459 while (1) {
460 slot = path->slots[0];
461 l = path->nodes[0];
462 btrfs_item_key_to_cpu(l, &found_key, slot);
463
464 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
465 goto next2;
466
467 if (found_key.objectid > found_key.offset) {
468 /* parent <- member, not needed to build config */
469 /* FIXME should we omit the key completely? */
470 goto next2;
471 }
472
473 ret = add_relation_rb(fs_info, found_key.objectid,
474 found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700475 if (ret == -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500476 btrfs_warn(fs_info,
477 "orphan qgroup relation 0x%llx->0x%llx",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200478 found_key.objectid, found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700479 ret = 0; /* ignore the error */
480 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200481 if (ret)
482 goto out;
483next2:
484 ret = btrfs_next_item(quota_root, path);
485 if (ret < 0)
486 goto out;
487 if (ret)
488 break;
489 }
490out:
491 fs_info->qgroup_flags |= flags;
Josef Bacikafcdd122016-09-02 15:40:02 -0400492 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
493 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
494 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
495 ret >= 0)
Jan Schmidtb382a322013-05-28 15:47:24 +0000496 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200497 btrfs_free_path(path);
498
Jan Schmidteb1716a2013-05-28 15:47:23 +0000499 if (ret < 0) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000500 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000501 fs_info->qgroup_ulist = NULL;
Jan Schmidtb382a322013-05-28 15:47:24 +0000502 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidteb1716a2013-05-28 15:47:23 +0000503 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000504
Arne Jansenbed92ea2012-06-28 18:03:02 +0200505 return ret < 0 ? ret : 0;
506}
507
508/*
Wang Shilonge685da12013-08-14 09:13:37 +0800509 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
510 * first two are in single-threaded paths.And for the third one, we have set
511 * quota_root to be null with qgroup_lock held before, so it is safe to clean
512 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200513 */
514void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
515{
516 struct rb_node *n;
517 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200518
519 while ((n = rb_first(&fs_info->qgroup_tree))) {
520 qgroup = rb_entry(n, struct btrfs_qgroup, node);
521 rb_erase(n, &fs_info->qgroup_tree);
Wang Shilong4082bd32013-08-14 09:13:36 +0800522 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200523 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800524 /*
525 * we call btrfs_free_qgroup_config() when umounting
Nicholas D Steeves01327612016-05-19 21:18:45 -0400526 * filesystem and disabling quota, so we set qgroup_ulist
Wang Shilong1e7bac12013-07-13 21:02:54 +0800527 * to be null here to avoid double free.
528 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000529 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800530 fs_info->qgroup_ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200531}
532
533static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
534 struct btrfs_root *quota_root,
535 u64 src, u64 dst)
536{
537 int ret;
538 struct btrfs_path *path;
539 struct btrfs_key key;
540
541 path = btrfs_alloc_path();
542 if (!path)
543 return -ENOMEM;
544
545 key.objectid = src;
546 key.type = BTRFS_QGROUP_RELATION_KEY;
547 key.offset = dst;
548
549 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
550
551 btrfs_mark_buffer_dirty(path->nodes[0]);
552
553 btrfs_free_path(path);
554 return ret;
555}
556
557static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
558 struct btrfs_root *quota_root,
559 u64 src, u64 dst)
560{
561 int ret;
562 struct btrfs_path *path;
563 struct btrfs_key key;
564
565 path = btrfs_alloc_path();
566 if (!path)
567 return -ENOMEM;
568
569 key.objectid = src;
570 key.type = BTRFS_QGROUP_RELATION_KEY;
571 key.offset = dst;
572
573 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
574 if (ret < 0)
575 goto out;
576
577 if (ret > 0) {
578 ret = -ENOENT;
579 goto out;
580 }
581
582 ret = btrfs_del_item(trans, quota_root, path);
583out:
584 btrfs_free_path(path);
585 return ret;
586}
587
588static int add_qgroup_item(struct btrfs_trans_handle *trans,
589 struct btrfs_root *quota_root, u64 qgroupid)
590{
591 int ret;
592 struct btrfs_path *path;
593 struct btrfs_qgroup_info_item *qgroup_info;
594 struct btrfs_qgroup_limit_item *qgroup_limit;
595 struct extent_buffer *leaf;
596 struct btrfs_key key;
597
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400598 if (btrfs_is_testing(quota_root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400599 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200600
Arne Jansenbed92ea2012-06-28 18:03:02 +0200601 path = btrfs_alloc_path();
602 if (!path)
603 return -ENOMEM;
604
605 key.objectid = 0;
606 key.type = BTRFS_QGROUP_INFO_KEY;
607 key.offset = qgroupid;
608
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700609 /*
610 * Avoid a transaction abort by catching -EEXIST here. In that
611 * case, we proceed by re-initializing the existing structure
612 * on disk.
613 */
614
Arne Jansenbed92ea2012-06-28 18:03:02 +0200615 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
616 sizeof(*qgroup_info));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700617 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200618 goto out;
619
620 leaf = path->nodes[0];
621 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
622 struct btrfs_qgroup_info_item);
623 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
624 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
625 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
626 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
627 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
628
629 btrfs_mark_buffer_dirty(leaf);
630
631 btrfs_release_path(path);
632
633 key.type = BTRFS_QGROUP_LIMIT_KEY;
634 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
635 sizeof(*qgroup_limit));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700636 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200637 goto out;
638
639 leaf = path->nodes[0];
640 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_qgroup_limit_item);
642 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
643 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
644 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
645 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
646 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
647
648 btrfs_mark_buffer_dirty(leaf);
649
650 ret = 0;
651out:
652 btrfs_free_path(path);
653 return ret;
654}
655
656static int del_qgroup_item(struct btrfs_trans_handle *trans,
657 struct btrfs_root *quota_root, u64 qgroupid)
658{
659 int ret;
660 struct btrfs_path *path;
661 struct btrfs_key key;
662
663 path = btrfs_alloc_path();
664 if (!path)
665 return -ENOMEM;
666
667 key.objectid = 0;
668 key.type = BTRFS_QGROUP_INFO_KEY;
669 key.offset = qgroupid;
670 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
671 if (ret < 0)
672 goto out;
673
674 if (ret > 0) {
675 ret = -ENOENT;
676 goto out;
677 }
678
679 ret = btrfs_del_item(trans, quota_root, path);
680 if (ret)
681 goto out;
682
683 btrfs_release_path(path);
684
685 key.type = BTRFS_QGROUP_LIMIT_KEY;
686 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
687 if (ret < 0)
688 goto out;
689
690 if (ret > 0) {
691 ret = -ENOENT;
692 goto out;
693 }
694
695 ret = btrfs_del_item(trans, quota_root, path);
696
697out:
698 btrfs_free_path(path);
699 return ret;
700}
701
702static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
Dongsheng Yang1510e712014-11-20 21:01:41 -0500703 struct btrfs_root *root,
704 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200705{
706 struct btrfs_path *path;
707 struct btrfs_key key;
708 struct extent_buffer *l;
709 struct btrfs_qgroup_limit_item *qgroup_limit;
710 int ret;
711 int slot;
712
713 key.objectid = 0;
714 key.type = BTRFS_QGROUP_LIMIT_KEY;
Dongsheng Yang1510e712014-11-20 21:01:41 -0500715 key.offset = qgroup->qgroupid;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200716
717 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000718 if (!path)
719 return -ENOMEM;
720
Arne Jansenbed92ea2012-06-28 18:03:02 +0200721 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
722 if (ret > 0)
723 ret = -ENOENT;
724
725 if (ret)
726 goto out;
727
728 l = path->nodes[0];
729 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100730 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Dongsheng Yang1510e712014-11-20 21:01:41 -0500731 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
732 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
733 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
734 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
735 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200736
737 btrfs_mark_buffer_dirty(l);
738
739out:
740 btrfs_free_path(path);
741 return ret;
742}
743
744static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
745 struct btrfs_root *root,
746 struct btrfs_qgroup *qgroup)
747{
748 struct btrfs_path *path;
749 struct btrfs_key key;
750 struct extent_buffer *l;
751 struct btrfs_qgroup_info_item *qgroup_info;
752 int ret;
753 int slot;
754
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400755 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400756 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200757
Arne Jansenbed92ea2012-06-28 18:03:02 +0200758 key.objectid = 0;
759 key.type = BTRFS_QGROUP_INFO_KEY;
760 key.offset = qgroup->qgroupid;
761
762 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000763 if (!path)
764 return -ENOMEM;
765
Arne Jansenbed92ea2012-06-28 18:03:02 +0200766 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
767 if (ret > 0)
768 ret = -ENOENT;
769
770 if (ret)
771 goto out;
772
773 l = path->nodes[0];
774 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100775 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200776 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
777 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
778 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
779 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
780 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
781
782 btrfs_mark_buffer_dirty(l);
783
784out:
785 btrfs_free_path(path);
786 return ret;
787}
788
789static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
790 struct btrfs_fs_info *fs_info,
791 struct btrfs_root *root)
792{
793 struct btrfs_path *path;
794 struct btrfs_key key;
795 struct extent_buffer *l;
796 struct btrfs_qgroup_status_item *ptr;
797 int ret;
798 int slot;
799
800 key.objectid = 0;
801 key.type = BTRFS_QGROUP_STATUS_KEY;
802 key.offset = 0;
803
804 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000805 if (!path)
806 return -ENOMEM;
807
Arne Jansenbed92ea2012-06-28 18:03:02 +0200808 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
809 if (ret > 0)
810 ret = -ENOENT;
811
812 if (ret)
813 goto out;
814
815 l = path->nodes[0];
816 slot = path->slots[0];
817 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
818 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
819 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000820 btrfs_set_qgroup_status_rescan(l, ptr,
821 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200822
823 btrfs_mark_buffer_dirty(l);
824
825out:
826 btrfs_free_path(path);
827 return ret;
828}
829
830/*
831 * called with qgroup_lock held
832 */
833static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
834 struct btrfs_root *root)
835{
836 struct btrfs_path *path;
837 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000838 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200839 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000840 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200841
Arne Jansenbed92ea2012-06-28 18:03:02 +0200842 path = btrfs_alloc_path();
843 if (!path)
844 return -ENOMEM;
845
Wang Shilong06b3a862013-02-27 11:16:57 +0000846 path->leave_spinning = 1;
847
848 key.objectid = 0;
849 key.offset = 0;
850 key.type = 0;
851
Arne Jansenbed92ea2012-06-28 18:03:02 +0200852 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200853 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000854 if (ret < 0)
855 goto out;
856 leaf = path->nodes[0];
857 nr = btrfs_header_nritems(leaf);
858 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200859 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000860 /*
861 * delete the leaf one by one
862 * since the whole tree is going
863 * to be deleted.
864 */
865 path->slots[0] = 0;
866 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200867 if (ret)
868 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000869
Arne Jansenbed92ea2012-06-28 18:03:02 +0200870 btrfs_release_path(path);
871 }
872 ret = 0;
873out:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200874 btrfs_free_path(path);
875 return ret;
876}
877
878int btrfs_quota_enable(struct btrfs_trans_handle *trans,
879 struct btrfs_fs_info *fs_info)
880{
881 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000882 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200883 struct btrfs_path *path = NULL;
884 struct btrfs_qgroup_status_item *ptr;
885 struct extent_buffer *leaf;
886 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000887 struct btrfs_key found_key;
888 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200889 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000890 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200891
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000892 mutex_lock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +0200893 if (fs_info->quota_root)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200894 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200895
David Sterba52bf8e72017-02-13 11:03:44 +0100896 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000897 if (!fs_info->qgroup_ulist) {
898 ret = -ENOMEM;
899 goto out;
900 }
901
Arne Jansenbed92ea2012-06-28 18:03:02 +0200902 /*
903 * initially create the quota tree
904 */
905 quota_root = btrfs_create_tree(trans, fs_info,
906 BTRFS_QUOTA_TREE_OBJECTID);
907 if (IS_ERR(quota_root)) {
908 ret = PTR_ERR(quota_root);
909 goto out;
910 }
911
912 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000913 if (!path) {
914 ret = -ENOMEM;
915 goto out_free_root;
916 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200917
918 key.objectid = 0;
919 key.type = BTRFS_QGROUP_STATUS_KEY;
920 key.offset = 0;
921
922 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
923 sizeof(*ptr));
924 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000925 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200926
927 leaf = path->nodes[0];
928 ptr = btrfs_item_ptr(leaf, path->slots[0],
929 struct btrfs_qgroup_status_item);
930 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
931 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
932 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
933 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
934 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000935 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200936
937 btrfs_mark_buffer_dirty(leaf);
938
Wang Shilong7708f022013-04-07 10:24:57 +0000939 key.objectid = 0;
940 key.type = BTRFS_ROOT_REF_KEY;
941 key.offset = 0;
942
943 btrfs_release_path(path);
944 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
945 if (ret > 0)
946 goto out_add_root;
947 if (ret < 0)
948 goto out_free_path;
949
950
951 while (1) {
952 slot = path->slots[0];
953 leaf = path->nodes[0];
954 btrfs_item_key_to_cpu(leaf, &found_key, slot);
955
956 if (found_key.type == BTRFS_ROOT_REF_KEY) {
957 ret = add_qgroup_item(trans, quota_root,
958 found_key.offset);
959 if (ret)
960 goto out_free_path;
961
Wang Shilong7708f022013-04-07 10:24:57 +0000962 qgroup = add_qgroup_rb(fs_info, found_key.offset);
963 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000964 ret = PTR_ERR(qgroup);
965 goto out_free_path;
966 }
Wang Shilong7708f022013-04-07 10:24:57 +0000967 }
968 ret = btrfs_next_item(tree_root, path);
969 if (ret < 0)
970 goto out_free_path;
971 if (ret)
972 break;
973 }
974
975out_add_root:
976 btrfs_release_path(path);
977 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
978 if (ret)
979 goto out_free_path;
980
Wang Shilong7708f022013-04-07 10:24:57 +0000981 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
982 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000983 ret = PTR_ERR(qgroup);
984 goto out_free_path;
985 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000986 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200987 fs_info->quota_root = quota_root;
Nikolay Borisov5d235152018-01-31 10:52:04 +0200988 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200989 spin_unlock(&fs_info->qgroup_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +0200990 ret = qgroup_rescan_init(fs_info, 0, 1);
991 if (!ret) {
992 qgroup_rescan_zero_tracking(fs_info);
993 btrfs_queue_work(fs_info->qgroup_rescan_workers,
994 &fs_info->qgroup_rescan_work);
995 }
996
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000997out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200998 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000999out_free_root:
1000 if (ret) {
1001 free_extent_buffer(quota_root->node);
1002 free_extent_buffer(quota_root->commit_root);
1003 kfree(quota_root);
1004 }
1005out:
Jan Schmidteb1716a2013-05-28 15:47:23 +00001006 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00001007 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +00001008 fs_info->qgroup_ulist = NULL;
1009 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001010 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001011 return ret;
1012}
1013
1014int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1015 struct btrfs_fs_info *fs_info)
1016{
Arne Jansenbed92ea2012-06-28 18:03:02 +02001017 struct btrfs_root *quota_root;
1018 int ret = 0;
1019
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001020 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +00001021 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001022 goto out;
Josef Bacikafcdd122016-09-02 15:40:02 -04001023 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04001024 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -08001025 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001026 quota_root = fs_info->quota_root;
1027 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +08001028 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001029 spin_unlock(&fs_info->qgroup_lock);
1030
Wang Shilonge685da12013-08-14 09:13:37 +08001031 btrfs_free_qgroup_config(fs_info);
1032
Arne Jansenbed92ea2012-06-28 18:03:02 +02001033 ret = btrfs_clean_quota_tree(trans, quota_root);
1034 if (ret)
1035 goto out;
1036
Jeff Mahoney1cd54472017-08-17 10:25:11 -04001037 ret = btrfs_del_root(trans, fs_info, &quota_root->root_key);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001038 if (ret)
1039 goto out;
1040
1041 list_del(&quota_root->dirty_list);
1042
1043 btrfs_tree_lock(quota_root->node);
David Sterba7c302b42017-02-10 18:47:57 +01001044 clean_tree_block(fs_info, quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001045 btrfs_tree_unlock(quota_root->node);
1046 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1047
1048 free_extent_buffer(quota_root->node);
1049 free_extent_buffer(quota_root->commit_root);
1050 kfree(quota_root);
1051out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001052 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001053 return ret;
1054}
1055
Jan Schmidt2f232032013-04-25 16:04:51 +00001056static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1057 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001058{
Jan Schmidt2f232032013-04-25 16:04:51 +00001059 if (list_empty(&qgroup->dirty))
1060 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001061}
1062
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001063/*
Qu Wenruo429d6272017-12-12 15:34:26 +08001064 * The easy accounting, we're updating qgroup relationship whose child qgroup
1065 * only has exclusive extents.
1066 *
1067 * In this case, all exclsuive extents will also be exlusive for parent, so
1068 * excl/rfer just get added/removed.
1069 *
1070 * So is qgroup reservation space, which should also be added/removed to
1071 * parent.
1072 * Or when child tries to release reservation space, parent will underflow its
1073 * reservation (for relationship adding case).
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001074 *
1075 * Caller should hold fs_info->qgroup_lock.
1076 */
1077static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1078 struct ulist *tmp, u64 ref_root,
Qu Wenruo429d6272017-12-12 15:34:26 +08001079 struct btrfs_qgroup *src, int sign)
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001080{
1081 struct btrfs_qgroup *qgroup;
1082 struct btrfs_qgroup_list *glist;
1083 struct ulist_node *unode;
1084 struct ulist_iterator uiter;
Qu Wenruo429d6272017-12-12 15:34:26 +08001085 u64 num_bytes = src->excl;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001086 int ret = 0;
1087
1088 qgroup = find_qgroup_rb(fs_info, ref_root);
1089 if (!qgroup)
1090 goto out;
1091
1092 qgroup->rfer += sign * num_bytes;
1093 qgroup->rfer_cmpr += sign * num_bytes;
1094
1095 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1096 qgroup->excl += sign * num_bytes;
1097 qgroup->excl_cmpr += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001098
1099 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001100 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001101 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001102 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001103
1104 qgroup_dirty(fs_info, qgroup);
1105
1106 /* Get all of the parent groups that contain this qgroup */
1107 list_for_each_entry(glist, &qgroup->groups, next_group) {
1108 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001109 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001110 if (ret < 0)
1111 goto out;
1112 }
1113
1114 /* Iterate all of the parents and adjust their reference counts */
1115 ULIST_ITER_INIT(&uiter);
1116 while ((unode = ulist_next(tmp, &uiter))) {
David Sterbaef2fff62016-10-26 16:23:50 +02001117 qgroup = unode_aux_to_qgroup(unode);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001118 qgroup->rfer += sign * num_bytes;
1119 qgroup->rfer_cmpr += sign * num_bytes;
1120 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1121 qgroup->excl += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001122 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001123 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001124 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001125 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001126 qgroup->excl_cmpr += sign * num_bytes;
1127 qgroup_dirty(fs_info, qgroup);
1128
1129 /* Add any parents of the parents */
1130 list_for_each_entry(glist, &qgroup->groups, next_group) {
1131 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001132 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001133 if (ret < 0)
1134 goto out;
1135 }
1136 }
1137 ret = 0;
1138out:
1139 return ret;
1140}
1141
1142
1143/*
1144 * Quick path for updating qgroup with only excl refs.
1145 *
1146 * In that case, just update all parent will be enough.
1147 * Or we needs to do a full rescan.
1148 * Caller should also hold fs_info->qgroup_lock.
1149 *
1150 * Return 0 for quick update, return >0 for need to full rescan
1151 * and mark INCONSISTENT flag.
1152 * Return < 0 for other error.
1153 */
1154static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1155 struct ulist *tmp, u64 src, u64 dst,
1156 int sign)
1157{
1158 struct btrfs_qgroup *qgroup;
1159 int ret = 1;
1160 int err = 0;
1161
1162 qgroup = find_qgroup_rb(fs_info, src);
1163 if (!qgroup)
1164 goto out;
1165 if (qgroup->excl == qgroup->rfer) {
1166 ret = 0;
1167 err = __qgroup_excl_accounting(fs_info, tmp, dst,
Qu Wenruo429d6272017-12-12 15:34:26 +08001168 qgroup, sign);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001169 if (err < 0) {
1170 ret = err;
1171 goto out;
1172 }
1173 }
1174out:
1175 if (ret)
1176 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1177 return ret;
1178}
1179
Arne Jansenbed92ea2012-06-28 18:03:02 +02001180int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1181 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1182{
1183 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001184 struct btrfs_qgroup *parent;
1185 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001186 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001187 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001188 int ret = 0;
1189
Qu Wenruo8465ece2015-02-27 16:24:22 +08001190 /* Check the level of src and dst first */
1191 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1192 return -EINVAL;
1193
David Sterba6602caf2017-02-13 12:41:02 +01001194 tmp = ulist_alloc(GFP_KERNEL);
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001195 if (!tmp)
1196 return -ENOMEM;
1197
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001198 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001199 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001200 if (!quota_root) {
1201 ret = -EINVAL;
1202 goto out;
1203 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001204 member = find_qgroup_rb(fs_info, src);
1205 parent = find_qgroup_rb(fs_info, dst);
1206 if (!member || !parent) {
1207 ret = -EINVAL;
1208 goto out;
1209 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001210
Wang Shilong534e6622013-04-17 14:49:51 +00001211 /* check if such qgroup relation exist firstly */
1212 list_for_each_entry(list, &member->groups, next_group) {
1213 if (list->group == parent) {
1214 ret = -EEXIST;
1215 goto out;
1216 }
1217 }
1218
Arne Jansenbed92ea2012-06-28 18:03:02 +02001219 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1220 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001221 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001222
1223 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1224 if (ret) {
1225 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001226 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001227 }
1228
1229 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001230 ret = add_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001231 if (ret < 0) {
1232 spin_unlock(&fs_info->qgroup_lock);
1233 goto out;
1234 }
1235 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001236 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001237out:
1238 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001239 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001240 return ret;
1241}
1242
David Sterba025db912017-02-13 13:00:51 +01001243static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001244 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1245{
1246 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001247 struct btrfs_qgroup *parent;
1248 struct btrfs_qgroup *member;
1249 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001250 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001251 int ret = 0;
1252 int err;
1253
David Sterba6602caf2017-02-13 12:41:02 +01001254 tmp = ulist_alloc(GFP_KERNEL);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001255 if (!tmp)
1256 return -ENOMEM;
1257
Arne Jansenbed92ea2012-06-28 18:03:02 +02001258 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001259 if (!quota_root) {
1260 ret = -EINVAL;
1261 goto out;
1262 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001263
Wang Shilong534e6622013-04-17 14:49:51 +00001264 member = find_qgroup_rb(fs_info, src);
1265 parent = find_qgroup_rb(fs_info, dst);
1266 if (!member || !parent) {
1267 ret = -EINVAL;
1268 goto out;
1269 }
1270
1271 /* check if such qgroup relation exist firstly */
1272 list_for_each_entry(list, &member->groups, next_group) {
1273 if (list->group == parent)
1274 goto exist;
1275 }
1276 ret = -ENOENT;
1277 goto out;
1278exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001279 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1280 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1281 if (err && !ret)
1282 ret = err;
1283
1284 spin_lock(&fs_info->qgroup_lock);
1285 del_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001286 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001287 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001288out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001289 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001290 return ret;
1291}
1292
1293int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1294 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1295{
1296 int ret = 0;
1297
1298 mutex_lock(&fs_info->qgroup_ioctl_lock);
1299 ret = __del_qgroup_relation(trans, fs_info, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001300 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001301
Arne Jansenbed92ea2012-06-28 18:03:02 +02001302 return ret;
1303}
1304
1305int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -05001306 struct btrfs_fs_info *fs_info, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001307{
1308 struct btrfs_root *quota_root;
1309 struct btrfs_qgroup *qgroup;
1310 int ret = 0;
1311
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001312 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001313 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001314 if (!quota_root) {
1315 ret = -EINVAL;
1316 goto out;
1317 }
Wang Shilong534e6622013-04-17 14:49:51 +00001318 qgroup = find_qgroup_rb(fs_info, qgroupid);
1319 if (qgroup) {
1320 ret = -EEXIST;
1321 goto out;
1322 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001323
1324 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001325 if (ret)
1326 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001327
1328 spin_lock(&fs_info->qgroup_lock);
1329 qgroup = add_qgroup_rb(fs_info, qgroupid);
1330 spin_unlock(&fs_info->qgroup_lock);
1331
1332 if (IS_ERR(qgroup))
1333 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001334out:
1335 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001336 return ret;
1337}
1338
1339int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1340 struct btrfs_fs_info *fs_info, u64 qgroupid)
1341{
1342 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001343 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001344 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001345 int ret = 0;
1346
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001347 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001348 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001349 if (!quota_root) {
1350 ret = -EINVAL;
1351 goto out;
1352 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001353
Arne Jansen2cf68702013-01-17 01:22:09 -07001354 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001355 if (!qgroup) {
1356 ret = -ENOENT;
1357 goto out;
1358 } else {
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001359 /* check if there are no children of this qgroup */
1360 if (!list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001361 ret = -EBUSY;
1362 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001363 }
1364 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001365 ret = del_qgroup_item(trans, quota_root, qgroupid);
Sargun Dhillon36b96fd2017-09-17 09:02:29 +00001366 if (ret && ret != -ENOENT)
1367 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001368
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001369 while (!list_empty(&qgroup->groups)) {
1370 list = list_first_entry(&qgroup->groups,
1371 struct btrfs_qgroup_list, next_group);
1372 ret = __del_qgroup_relation(trans, fs_info,
1373 qgroupid,
1374 list->group->qgroupid);
1375 if (ret)
1376 goto out;
1377 }
1378
Arne Jansenbed92ea2012-06-28 18:03:02 +02001379 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001380 del_qgroup_rb(fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001381 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001382out:
1383 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001384 return ret;
1385}
1386
1387int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1388 struct btrfs_fs_info *fs_info, u64 qgroupid,
1389 struct btrfs_qgroup_limit *limit)
1390{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001391 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001392 struct btrfs_qgroup *qgroup;
1393 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001394 /* Sometimes we would want to clear the limit on this qgroup.
1395 * To meet this requirement, we treat the -1 as a special value
1396 * which tell kernel to clear the limit on this qgroup.
1397 */
1398 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001399
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001400 mutex_lock(&fs_info->qgroup_ioctl_lock);
1401 quota_root = fs_info->quota_root;
1402 if (!quota_root) {
1403 ret = -EINVAL;
1404 goto out;
1405 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001406
Wang Shilongddb47af2013-04-07 10:50:20 +00001407 qgroup = find_qgroup_rb(fs_info, qgroupid);
1408 if (!qgroup) {
1409 ret = -ENOENT;
1410 goto out;
1411 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001412
Wang Shilong58400fc2013-04-07 10:50:17 +00001413 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001414 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1415 if (limit->max_rfer == CLEAR_VALUE) {
1416 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1417 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1418 qgroup->max_rfer = 0;
1419 } else {
1420 qgroup->max_rfer = limit->max_rfer;
1421 }
1422 }
1423 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1424 if (limit->max_excl == CLEAR_VALUE) {
1425 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1426 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1427 qgroup->max_excl = 0;
1428 } else {
1429 qgroup->max_excl = limit->max_excl;
1430 }
1431 }
1432 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1433 if (limit->rsv_rfer == CLEAR_VALUE) {
1434 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1435 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1436 qgroup->rsv_rfer = 0;
1437 } else {
1438 qgroup->rsv_rfer = limit->rsv_rfer;
1439 }
1440 }
1441 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1442 if (limit->rsv_excl == CLEAR_VALUE) {
1443 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1444 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1445 qgroup->rsv_excl = 0;
1446 } else {
1447 qgroup->rsv_excl = limit->rsv_excl;
1448 }
1449 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001450 qgroup->lim_flags |= limit->flags;
1451
Arne Jansenbed92ea2012-06-28 18:03:02 +02001452 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001453
1454 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1455 if (ret) {
1456 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1457 btrfs_info(fs_info, "unable to update quota limit for %llu",
1458 qgroupid);
1459 }
1460
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001461out:
1462 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001463 return ret;
1464}
Mark Fasheh11526512014-07-17 12:39:01 -07001465
Qu Wenruo50b3e042016-10-18 09:31:27 +08001466int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +08001467 struct btrfs_delayed_ref_root *delayed_refs,
1468 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001469{
1470 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1471 struct rb_node *parent_node = NULL;
1472 struct btrfs_qgroup_extent_record *entry;
1473 u64 bytenr = record->bytenr;
1474
David Sterbaa4666e62018-03-16 02:21:22 +01001475 lockdep_assert_held(&delayed_refs->lock);
Qu Wenruo50b3e042016-10-18 09:31:27 +08001476 trace_btrfs_qgroup_trace_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001477
Qu Wenruo3368d002015-04-16 14:34:17 +08001478 while (*p) {
1479 parent_node = *p;
1480 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1481 node);
1482 if (bytenr < entry->bytenr)
1483 p = &(*p)->rb_left;
1484 else if (bytenr > entry->bytenr)
1485 p = &(*p)->rb_right;
1486 else
Qu Wenruocb93b522016-08-15 10:36:50 +08001487 return 1;
Qu Wenruo3368d002015-04-16 14:34:17 +08001488 }
1489
1490 rb_link_node(&record->node, parent_node, p);
1491 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001492 return 0;
1493}
1494
Qu Wenruofb235dc2017-02-15 10:43:03 +08001495int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1496 struct btrfs_qgroup_extent_record *qrecord)
1497{
1498 struct ulist *old_root;
1499 u64 bytenr = qrecord->bytenr;
1500 int ret;
1501
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001502 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
Nikolay Borisov952bd3db2018-01-29 15:53:01 +02001503 if (ret < 0) {
1504 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1505 btrfs_warn(fs_info,
1506"error accounting new delayed refs extent (err code: %d), quota inconsistent",
1507 ret);
1508 return 0;
1509 }
Qu Wenruofb235dc2017-02-15 10:43:03 +08001510
1511 /*
1512 * Here we don't need to get the lock of
1513 * trans->transaction->delayed_refs, since inserted qrecord won't
1514 * be deleted, only qrecord->node may be modified (new qrecord insert)
1515 *
1516 * So modifying qrecord->old_roots is safe here
1517 */
1518 qrecord->old_roots = old_root;
1519 return 0;
1520}
1521
Qu Wenruo50b3e042016-10-18 09:31:27 +08001522int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
Qu Wenruocb93b522016-08-15 10:36:50 +08001523 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1524 gfp_t gfp_flag)
1525{
1526 struct btrfs_qgroup_extent_record *record;
1527 struct btrfs_delayed_ref_root *delayed_refs;
1528 int ret;
1529
Josef Bacikafcdd122016-09-02 15:40:02 -04001530 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1531 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001532 return 0;
1533 if (WARN_ON(trans == NULL))
1534 return -EINVAL;
1535 record = kmalloc(sizeof(*record), gfp_flag);
1536 if (!record)
1537 return -ENOMEM;
1538
1539 delayed_refs = &trans->transaction->delayed_refs;
1540 record->bytenr = bytenr;
1541 record->num_bytes = num_bytes;
1542 record->old_roots = NULL;
1543
1544 spin_lock(&delayed_refs->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001545 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
Qu Wenruocb93b522016-08-15 10:36:50 +08001546 spin_unlock(&delayed_refs->lock);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001547 if (ret > 0) {
Qu Wenruocb93b522016-08-15 10:36:50 +08001548 kfree(record);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001549 return 0;
1550 }
1551 return btrfs_qgroup_trace_extent_post(fs_info, record);
Qu Wenruo3368d002015-04-16 14:34:17 +08001552}
1553
Qu Wenruo33d1f052016-10-18 09:31:28 +08001554int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001555 struct btrfs_fs_info *fs_info,
Qu Wenruo33d1f052016-10-18 09:31:28 +08001556 struct extent_buffer *eb)
1557{
1558 int nr = btrfs_header_nritems(eb);
1559 int i, extent_type, ret;
1560 struct btrfs_key key;
1561 struct btrfs_file_extent_item *fi;
1562 u64 bytenr, num_bytes;
1563
1564 /* We can be called directly from walk_up_proc() */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001565 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001566 return 0;
1567
1568 for (i = 0; i < nr; i++) {
1569 btrfs_item_key_to_cpu(eb, &key, i);
1570
1571 if (key.type != BTRFS_EXTENT_DATA_KEY)
1572 continue;
1573
1574 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1575 /* filter out non qgroup-accountable extents */
1576 extent_type = btrfs_file_extent_type(eb, fi);
1577
1578 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1579 continue;
1580
1581 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1582 if (!bytenr)
1583 continue;
1584
1585 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1586
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001587 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1588 num_bytes, GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001589 if (ret)
1590 return ret;
1591 }
Jeff Mahoneycddf3b22017-06-20 08:15:26 -04001592 cond_resched();
Qu Wenruo33d1f052016-10-18 09:31:28 +08001593 return 0;
1594}
1595
1596/*
1597 * Walk up the tree from the bottom, freeing leaves and any interior
1598 * nodes which have had all slots visited. If a node (leaf or
1599 * interior) is freed, the node above it will have it's slot
1600 * incremented. The root node will never be freed.
1601 *
1602 * At the end of this function, we should have a path which has all
1603 * slots incremented to the next position for a search. If we need to
1604 * read a new node it will be NULL and the node above it will have the
1605 * correct slot selected for a later read.
1606 *
1607 * If we increment the root nodes slot counter past the number of
1608 * elements, 1 is returned to signal completion of the search.
1609 */
David Sterba15b34512017-02-10 20:30:23 +01001610static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
Qu Wenruo33d1f052016-10-18 09:31:28 +08001611{
1612 int level = 0;
1613 int nr, slot;
1614 struct extent_buffer *eb;
1615
1616 if (root_level == 0)
1617 return 1;
1618
1619 while (level <= root_level) {
1620 eb = path->nodes[level];
1621 nr = btrfs_header_nritems(eb);
1622 path->slots[level]++;
1623 slot = path->slots[level];
1624 if (slot >= nr || level == 0) {
1625 /*
1626 * Don't free the root - we will detect this
1627 * condition after our loop and return a
1628 * positive value for caller to stop walking the tree.
1629 */
1630 if (level != root_level) {
1631 btrfs_tree_unlock_rw(eb, path->locks[level]);
1632 path->locks[level] = 0;
1633
1634 free_extent_buffer(eb);
1635 path->nodes[level] = NULL;
1636 path->slots[level] = 0;
1637 }
1638 } else {
1639 /*
1640 * We have a valid slot to walk back down
1641 * from. Stop here so caller can process these
1642 * new nodes.
1643 */
1644 break;
1645 }
1646
1647 level++;
1648 }
1649
1650 eb = path->nodes[root_level];
1651 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1652 return 1;
1653
1654 return 0;
1655}
1656
1657int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1658 struct btrfs_root *root,
1659 struct extent_buffer *root_eb,
1660 u64 root_gen, int root_level)
1661{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001662 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001663 int ret = 0;
1664 int level;
1665 struct extent_buffer *eb = root_eb;
1666 struct btrfs_path *path = NULL;
1667
Nikolay Borisovb6e6bca2017-07-12 09:42:19 +03001668 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001669 BUG_ON(root_eb == NULL);
1670
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001671 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001672 return 0;
1673
1674 if (!extent_buffer_uptodate(root_eb)) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001675 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001676 if (ret)
1677 goto out;
1678 }
1679
1680 if (root_level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001681 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001682 goto out;
1683 }
1684
1685 path = btrfs_alloc_path();
1686 if (!path)
1687 return -ENOMEM;
1688
1689 /*
1690 * Walk down the tree. Missing extent blocks are filled in as
1691 * we go. Metadata is accounted every time we read a new
1692 * extent block.
1693 *
1694 * When we reach a leaf, we account for file extent items in it,
1695 * walk back up the tree (adjusting slot pointers as we go)
1696 * and restart the search process.
1697 */
1698 extent_buffer_get(root_eb); /* For path */
1699 path->nodes[root_level] = root_eb;
1700 path->slots[root_level] = 0;
1701 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1702walk_down:
1703 level = root_level;
1704 while (level >= 0) {
1705 if (path->nodes[level] == NULL) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001706 struct btrfs_key first_key;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001707 int parent_slot;
1708 u64 child_gen;
1709 u64 child_bytenr;
1710
1711 /*
1712 * We need to get child blockptr/gen from parent before
1713 * we can read it.
1714 */
1715 eb = path->nodes[level + 1];
1716 parent_slot = path->slots[level + 1];
1717 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1718 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08001719 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001720
Qu Wenruo581c1762018-03-29 09:08:11 +08001721 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1722 level, &first_key);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001723 if (IS_ERR(eb)) {
1724 ret = PTR_ERR(eb);
1725 goto out;
1726 } else if (!extent_buffer_uptodate(eb)) {
1727 free_extent_buffer(eb);
1728 ret = -EIO;
1729 goto out;
1730 }
1731
1732 path->nodes[level] = eb;
1733 path->slots[level] = 0;
1734
1735 btrfs_tree_read_lock(eb);
1736 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1737 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1738
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001739 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1740 child_bytenr,
1741 fs_info->nodesize,
1742 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001743 if (ret)
1744 goto out;
1745 }
1746
1747 if (level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001748 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1749 path->nodes[level]);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001750 if (ret)
1751 goto out;
1752
1753 /* Nonzero return here means we completed our search */
David Sterba15b34512017-02-10 20:30:23 +01001754 ret = adjust_slots_upwards(path, root_level);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001755 if (ret)
1756 break;
1757
1758 /* Restart search with new slots */
1759 goto walk_down;
1760 }
1761
1762 level--;
1763 }
1764
1765 ret = 0;
1766out:
1767 btrfs_free_path(path);
1768
1769 return ret;
1770}
1771
Qu Wenruod810ef22015-04-12 16:52:34 +08001772#define UPDATE_NEW 0
1773#define UPDATE_OLD 1
1774/*
1775 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1776 */
1777static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1778 struct ulist *roots, struct ulist *tmp,
1779 struct ulist *qgroups, u64 seq, int update_old)
1780{
1781 struct ulist_node *unode;
1782 struct ulist_iterator uiter;
1783 struct ulist_node *tmp_unode;
1784 struct ulist_iterator tmp_uiter;
1785 struct btrfs_qgroup *qg;
1786 int ret = 0;
1787
1788 if (!roots)
1789 return 0;
1790 ULIST_ITER_INIT(&uiter);
1791 while ((unode = ulist_next(roots, &uiter))) {
1792 qg = find_qgroup_rb(fs_info, unode->val);
1793 if (!qg)
1794 continue;
1795
1796 ulist_reinit(tmp);
David Sterbaef2fff62016-10-26 16:23:50 +02001797 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
Qu Wenruod810ef22015-04-12 16:52:34 +08001798 GFP_ATOMIC);
1799 if (ret < 0)
1800 return ret;
David Sterbaef2fff62016-10-26 16:23:50 +02001801 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
Qu Wenruod810ef22015-04-12 16:52:34 +08001802 if (ret < 0)
1803 return ret;
1804 ULIST_ITER_INIT(&tmp_uiter);
1805 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1806 struct btrfs_qgroup_list *glist;
1807
David Sterbaef2fff62016-10-26 16:23:50 +02001808 qg = unode_aux_to_qgroup(tmp_unode);
Qu Wenruod810ef22015-04-12 16:52:34 +08001809 if (update_old)
1810 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1811 else
1812 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1813 list_for_each_entry(glist, &qg->groups, next_group) {
1814 ret = ulist_add(qgroups, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001815 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001816 GFP_ATOMIC);
1817 if (ret < 0)
1818 return ret;
1819 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001820 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001821 GFP_ATOMIC);
1822 if (ret < 0)
1823 return ret;
1824 }
1825 }
1826 }
1827 return 0;
1828}
1829
Josef Bacikfcebe452014-05-13 17:30:47 -07001830/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001831 * Update qgroup rfer/excl counters.
1832 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08001833 *
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001834 * Excl update is tricky, the update is split into 2 part.
1835 * Part 1: Possible exclusive <-> sharing detect:
1836 * | A | !A |
1837 * -------------------------------------
1838 * B | * | - |
1839 * -------------------------------------
1840 * !B | + | ** |
1841 * -------------------------------------
1842 *
1843 * Conditions:
1844 * A: cur_old_roots < nr_old_roots (not exclusive before)
1845 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1846 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04001847 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001848 *
1849 * Results:
1850 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1851 * *: Definitely not changed. **: Possible unchanged.
1852 *
1853 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1854 *
1855 * To make the logic clear, we first use condition A and B to split
1856 * combination into 4 results.
1857 *
1858 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1859 * only on variant maybe 0.
1860 *
1861 * Lastly, check result **, since there are 2 variants maybe 0, split them
1862 * again(2x2).
1863 * But this time we don't need to consider other things, the codes and logic
1864 * is easy to understand now.
1865 */
1866static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1867 struct ulist *qgroups,
1868 u64 nr_old_roots,
1869 u64 nr_new_roots,
1870 u64 num_bytes, u64 seq)
1871{
1872 struct ulist_node *unode;
1873 struct ulist_iterator uiter;
1874 struct btrfs_qgroup *qg;
1875 u64 cur_new_count, cur_old_count;
1876
1877 ULIST_ITER_INIT(&uiter);
1878 while ((unode = ulist_next(qgroups, &uiter))) {
1879 bool dirty = false;
1880
David Sterbaef2fff62016-10-26 16:23:50 +02001881 qg = unode_aux_to_qgroup(unode);
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001882 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1883 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1884
Qu Wenruo8b317902018-04-30 15:04:44 +08001885 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
1886 cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001887
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001888 /* Rfer update part */
1889 if (cur_old_count == 0 && cur_new_count > 0) {
1890 qg->rfer += num_bytes;
1891 qg->rfer_cmpr += num_bytes;
1892 dirty = true;
1893 }
1894 if (cur_old_count > 0 && cur_new_count == 0) {
1895 qg->rfer -= num_bytes;
1896 qg->rfer_cmpr -= num_bytes;
1897 dirty = true;
1898 }
1899
1900 /* Excl update part */
1901 /* Exclusive/none -> shared case */
1902 if (cur_old_count == nr_old_roots &&
1903 cur_new_count < nr_new_roots) {
1904 /* Exclusive -> shared */
1905 if (cur_old_count != 0) {
1906 qg->excl -= num_bytes;
1907 qg->excl_cmpr -= num_bytes;
1908 dirty = true;
1909 }
1910 }
1911
1912 /* Shared -> exclusive/none case */
1913 if (cur_old_count < nr_old_roots &&
1914 cur_new_count == nr_new_roots) {
1915 /* Shared->exclusive */
1916 if (cur_new_count != 0) {
1917 qg->excl += num_bytes;
1918 qg->excl_cmpr += num_bytes;
1919 dirty = true;
1920 }
1921 }
1922
1923 /* Exclusive/none -> exclusive/none case */
1924 if (cur_old_count == nr_old_roots &&
1925 cur_new_count == nr_new_roots) {
1926 if (cur_old_count == 0) {
1927 /* None -> exclusive/none */
1928
1929 if (cur_new_count != 0) {
1930 /* None -> exclusive */
1931 qg->excl += num_bytes;
1932 qg->excl_cmpr += num_bytes;
1933 dirty = true;
1934 }
1935 /* None -> none, nothing changed */
1936 } else {
1937 /* Exclusive -> exclusive/none */
1938
1939 if (cur_new_count == 0) {
1940 /* Exclusive -> none */
1941 qg->excl -= num_bytes;
1942 qg->excl_cmpr -= num_bytes;
1943 dirty = true;
1944 }
1945 /* Exclusive -> exclusive, nothing changed */
1946 }
1947 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08001948
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001949 if (dirty)
1950 qgroup_dirty(fs_info, qg);
1951 }
1952 return 0;
1953}
1954
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001955/*
1956 * Check if the @roots potentially is a list of fs tree roots
1957 *
1958 * Return 0 for definitely not a fs/subvol tree roots ulist
1959 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1960 * one as well)
1961 */
1962static int maybe_fs_roots(struct ulist *roots)
1963{
1964 struct ulist_node *unode;
1965 struct ulist_iterator uiter;
1966
1967 /* Empty one, still possible for fs roots */
1968 if (!roots || roots->nnodes == 0)
1969 return 1;
1970
1971 ULIST_ITER_INIT(&uiter);
1972 unode = ulist_next(roots, &uiter);
1973 if (!unode)
1974 return 1;
1975
1976 /*
1977 * If it contains fs tree roots, then it must belong to fs/subvol
1978 * trees.
1979 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
1980 */
1981 return is_fstree(unode->val);
1982}
1983
Qu Wenruo442244c2015-04-16 17:18:36 +08001984int
Qu Wenruo550d7a22015-04-16 15:37:33 +08001985btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1986 struct btrfs_fs_info *fs_info,
1987 u64 bytenr, u64 num_bytes,
1988 struct ulist *old_roots, struct ulist *new_roots)
1989{
1990 struct ulist *qgroups = NULL;
1991 struct ulist *tmp = NULL;
1992 u64 seq;
1993 u64 nr_new_roots = 0;
1994 u64 nr_old_roots = 0;
1995 int ret = 0;
1996
David Sterba81353d52017-02-13 14:05:24 +01001997 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1998 return 0;
1999
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002000 if (new_roots) {
2001 if (!maybe_fs_roots(new_roots))
2002 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002003 nr_new_roots = new_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002004 }
2005 if (old_roots) {
2006 if (!maybe_fs_roots(old_roots))
2007 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002008 nr_old_roots = old_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002009 }
2010
2011 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2012 if (nr_old_roots == 0 && nr_new_roots == 0)
2013 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002014
Qu Wenruo550d7a22015-04-16 15:37:33 +08002015 BUG_ON(!fs_info->quota_root);
2016
Qu Wenruoc9f6f3c2018-05-03 09:59:02 +08002017 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2018 num_bytes, nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002019
Qu Wenruo550d7a22015-04-16 15:37:33 +08002020 qgroups = ulist_alloc(GFP_NOFS);
2021 if (!qgroups) {
2022 ret = -ENOMEM;
2023 goto out_free;
2024 }
2025 tmp = ulist_alloc(GFP_NOFS);
2026 if (!tmp) {
2027 ret = -ENOMEM;
2028 goto out_free;
2029 }
2030
2031 mutex_lock(&fs_info->qgroup_rescan_lock);
2032 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2033 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2034 mutex_unlock(&fs_info->qgroup_rescan_lock);
2035 ret = 0;
2036 goto out_free;
2037 }
2038 }
2039 mutex_unlock(&fs_info->qgroup_rescan_lock);
2040
2041 spin_lock(&fs_info->qgroup_lock);
2042 seq = fs_info->qgroup_seq;
2043
2044 /* Update old refcnts using old_roots */
2045 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2046 UPDATE_OLD);
2047 if (ret < 0)
2048 goto out;
2049
2050 /* Update new refcnts using new_roots */
2051 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2052 UPDATE_NEW);
2053 if (ret < 0)
2054 goto out;
2055
2056 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2057 num_bytes, seq);
2058
2059 /*
2060 * Bump qgroup_seq to avoid seq overlap
2061 */
2062 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2063out:
2064 spin_unlock(&fs_info->qgroup_lock);
2065out_free:
2066 ulist_free(tmp);
2067 ulist_free(qgroups);
2068 ulist_free(old_roots);
2069 ulist_free(new_roots);
2070 return ret;
2071}
2072
Nikolay Borisov460fb202018-03-15 16:00:25 +02002073int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
Qu Wenruo550d7a22015-04-16 15:37:33 +08002074{
Nikolay Borisov460fb202018-03-15 16:00:25 +02002075 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002076 struct btrfs_qgroup_extent_record *record;
2077 struct btrfs_delayed_ref_root *delayed_refs;
2078 struct ulist *new_roots = NULL;
2079 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08002080 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002081 int ret = 0;
2082
2083 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08002084 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002085 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2086 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2087 node);
2088
Jeff Mahoneybc074522016-06-09 17:27:55 -04002089 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002090
Qu Wenruo550d7a22015-04-16 15:37:33 +08002091 if (!ret) {
2092 /*
Qu Wenruod1b8b942017-02-27 15:10:35 +08002093 * Old roots should be searched when inserting qgroup
2094 * extent record
2095 */
2096 if (WARN_ON(!record->old_roots)) {
2097 /* Search commit root to find old_roots */
2098 ret = btrfs_find_all_roots(NULL, fs_info,
2099 record->bytenr, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002100 &record->old_roots, false);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002101 if (ret < 0)
2102 goto cleanup;
2103 }
2104
2105 /*
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002106 * Use SEQ_LAST as time_seq to do special search, which
Qu Wenruo550d7a22015-04-16 15:37:33 +08002107 * doesn't lock tree or delayed_refs and search current
2108 * root. It's safe inside commit_transaction().
2109 */
2110 ret = btrfs_find_all_roots(trans, fs_info,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002111 record->bytenr, SEQ_LAST, &new_roots, false);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002112 if (ret < 0)
2113 goto cleanup;
Qu Wenruod1b8b942017-02-27 15:10:35 +08002114 if (qgroup_to_skip) {
Qu Wenruo9086db82015-04-20 09:53:50 +08002115 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002116 ulist_del(record->old_roots, qgroup_to_skip,
2117 0);
2118 }
Qu Wenruo550d7a22015-04-16 15:37:33 +08002119 ret = btrfs_qgroup_account_extent(trans, fs_info,
2120 record->bytenr, record->num_bytes,
2121 record->old_roots, new_roots);
2122 record->old_roots = NULL;
2123 new_roots = NULL;
2124 }
2125cleanup:
2126 ulist_free(record->old_roots);
2127 ulist_free(new_roots);
2128 new_roots = NULL;
2129 rb_erase(node, &delayed_refs->dirty_extent_root);
2130 kfree(record);
2131
2132 }
2133 return ret;
2134}
2135
Josef Bacikfcebe452014-05-13 17:30:47 -07002136/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002137 * called from commit_transaction. Writes all changed qgroups to disk.
2138 */
2139int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2140 struct btrfs_fs_info *fs_info)
2141{
2142 struct btrfs_root *quota_root = fs_info->quota_root;
2143 int ret = 0;
2144
2145 if (!quota_root)
Nikolay Borisov5d235152018-01-31 10:52:04 +02002146 return ret;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002147
2148 spin_lock(&fs_info->qgroup_lock);
2149 while (!list_empty(&fs_info->dirty_qgroups)) {
2150 struct btrfs_qgroup *qgroup;
2151 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2152 struct btrfs_qgroup, dirty);
2153 list_del_init(&qgroup->dirty);
2154 spin_unlock(&fs_info->qgroup_lock);
2155 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2156 if (ret)
2157 fs_info->qgroup_flags |=
2158 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05002159 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2160 if (ret)
2161 fs_info->qgroup_flags |=
2162 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002163 spin_lock(&fs_info->qgroup_lock);
2164 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002165 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02002166 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2167 else
2168 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2169 spin_unlock(&fs_info->qgroup_lock);
2170
2171 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2172 if (ret)
2173 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2174
Arne Jansenbed92ea2012-06-28 18:03:02 +02002175 return ret;
2176}
2177
2178/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002179 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002180 * when a snapshot or a subvolume is created. Throwing an error will
2181 * cause a transaction abort so we take extra care here to only error
2182 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02002183 */
2184int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2185 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2186 struct btrfs_qgroup_inherit *inherit)
2187{
2188 int ret = 0;
2189 int i;
2190 u64 *i_qgroups;
2191 struct btrfs_root *quota_root = fs_info->quota_root;
2192 struct btrfs_qgroup *srcgroup;
2193 struct btrfs_qgroup *dstgroup;
2194 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002195 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002196
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002197 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04002198 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002199 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002200
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002201 if (!quota_root) {
2202 ret = -EINVAL;
2203 goto out;
2204 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002205
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002206 if (inherit) {
2207 i_qgroups = (u64 *)(inherit + 1);
2208 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2209 2 * inherit->num_excl_copies;
2210 for (i = 0; i < nums; ++i) {
2211 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05002212
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002213 /*
2214 * Zero out invalid groups so we can ignore
2215 * them later.
2216 */
2217 if (!srcgroup ||
2218 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2219 *i_qgroups = 0ULL;
2220
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002221 ++i_qgroups;
2222 }
2223 }
2224
Arne Jansenbed92ea2012-06-28 18:03:02 +02002225 /*
2226 * create a tracking group for the subvol itself
2227 */
2228 ret = add_qgroup_item(trans, quota_root, objectid);
2229 if (ret)
2230 goto out;
2231
Arne Jansenbed92ea2012-06-28 18:03:02 +02002232 if (srcid) {
2233 struct btrfs_root *srcroot;
2234 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002235
2236 srckey.objectid = srcid;
2237 srckey.type = BTRFS_ROOT_ITEM_KEY;
2238 srckey.offset = (u64)-1;
2239 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2240 if (IS_ERR(srcroot)) {
2241 ret = PTR_ERR(srcroot);
2242 goto out;
2243 }
2244
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002245 level_size = fs_info->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002246 }
2247
2248 /*
2249 * add qgroup to all inherited groups
2250 */
2251 if (inherit) {
2252 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002253 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2254 if (*i_qgroups == 0)
2255 continue;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002256 ret = add_qgroup_relation_item(trans, quota_root,
2257 objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002258 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002259 goto out;
2260 ret = add_qgroup_relation_item(trans, quota_root,
2261 *i_qgroups, objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002262 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002263 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002264 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002265 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002266 }
2267
2268
2269 spin_lock(&fs_info->qgroup_lock);
2270
2271 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002272 if (IS_ERR(dstgroup)) {
2273 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002274 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002275 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002276
Dongsheng Yange8c85412014-11-20 20:58:34 -05002277 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05002278 dstgroup->lim_flags = inherit->lim.flags;
2279 dstgroup->max_rfer = inherit->lim.max_rfer;
2280 dstgroup->max_excl = inherit->lim.max_excl;
2281 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2282 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05002283
2284 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2285 if (ret) {
2286 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002287 btrfs_info(fs_info,
2288 "unable to update quota limit for %llu",
2289 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002290 goto unlock;
2291 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002292 }
2293
Arne Jansenbed92ea2012-06-28 18:03:02 +02002294 if (srcid) {
2295 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002296 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002297 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002298
2299 /*
2300 * We call inherit after we clone the root in order to make sure
2301 * our counts don't go crazy, so at this point the only
2302 * difference between the two roots should be the root node.
2303 */
2304 dstgroup->rfer = srcgroup->rfer;
2305 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2306 dstgroup->excl = level_size;
2307 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002308 srcgroup->excl = level_size;
2309 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002310
2311 /* inherit the limit info */
2312 dstgroup->lim_flags = srcgroup->lim_flags;
2313 dstgroup->max_rfer = srcgroup->max_rfer;
2314 dstgroup->max_excl = srcgroup->max_excl;
2315 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2316 dstgroup->rsv_excl = srcgroup->rsv_excl;
2317
Arne Jansenbed92ea2012-06-28 18:03:02 +02002318 qgroup_dirty(fs_info, dstgroup);
2319 qgroup_dirty(fs_info, srcgroup);
2320 }
2321
Chris Masonf3a87f12012-09-14 20:06:30 -04002322 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002323 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002324
2325 i_qgroups = (u64 *)(inherit + 1);
2326 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002327 if (*i_qgroups) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002328 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002329 if (ret)
2330 goto unlock;
2331 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002332 ++i_qgroups;
2333 }
2334
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002335 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002336 struct btrfs_qgroup *src;
2337 struct btrfs_qgroup *dst;
2338
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002339 if (!i_qgroups[0] || !i_qgroups[1])
2340 continue;
2341
Arne Jansenbed92ea2012-06-28 18:03:02 +02002342 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2343 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2344
2345 if (!src || !dst) {
2346 ret = -EINVAL;
2347 goto unlock;
2348 }
2349
2350 dst->rfer = src->rfer - level_size;
2351 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002352 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002353 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002354 struct btrfs_qgroup *src;
2355 struct btrfs_qgroup *dst;
2356
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002357 if (!i_qgroups[0] || !i_qgroups[1])
2358 continue;
2359
Arne Jansenbed92ea2012-06-28 18:03:02 +02002360 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2361 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2362
2363 if (!src || !dst) {
2364 ret = -EINVAL;
2365 goto unlock;
2366 }
2367
2368 dst->excl = src->excl + level_size;
2369 dst->excl_cmpr = src->excl_cmpr + level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002370 }
2371
2372unlock:
2373 spin_unlock(&fs_info->qgroup_lock);
2374out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002375 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002376 return ret;
2377}
2378
Qu Wenruoa514d632017-12-22 16:06:39 +08002379/*
2380 * Two limits to commit transaction in advance.
2381 *
2382 * For RATIO, it will be 1/RATIO of the remaining limit
2383 * (excluding data and prealloc meta) as threshold.
2384 * For SIZE, it will be in byte unit as threshold.
2385 */
2386#define QGROUP_PERTRANS_RATIO 32
2387#define QGROUP_PERTRANS_SIZE SZ_32M
2388static bool qgroup_check_limits(struct btrfs_fs_info *fs_info,
2389 const struct btrfs_qgroup *qg, u64 num_bytes)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002390{
Qu Wenruoa514d632017-12-22 16:06:39 +08002391 u64 limit;
2392 u64 threshold;
2393
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002394 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002395 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002396 return false;
2397
2398 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002399 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002400 return false;
2401
Qu Wenruoa514d632017-12-22 16:06:39 +08002402 /*
2403 * Even if we passed the check, it's better to check if reservation
2404 * for meta_pertrans is pushing us near limit.
2405 * If there is too much pertrans reservation or it's near the limit,
2406 * let's try commit transaction to free some, using transaction_kthread
2407 */
2408 if ((qg->lim_flags & (BTRFS_QGROUP_LIMIT_MAX_RFER |
2409 BTRFS_QGROUP_LIMIT_MAX_EXCL))) {
2410 if (qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
2411 limit = qg->max_excl;
2412 else
2413 limit = qg->max_rfer;
2414 threshold = (limit - qg->rsv.values[BTRFS_QGROUP_RSV_DATA] -
2415 qg->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC]) /
2416 QGROUP_PERTRANS_RATIO;
2417 threshold = min_t(u64, threshold, QGROUP_PERTRANS_SIZE);
2418
2419 /*
2420 * Use transaction_kthread to commit transaction, so we no
2421 * longer need to bother nested transaction nor lock context.
2422 */
2423 if (qg->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS] > threshold)
2424 btrfs_commit_transaction_locksafe(fs_info);
2425 }
2426
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002427 return true;
2428}
2429
Qu Wenruodba21322017-12-12 15:34:25 +08002430static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2431 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002432{
2433 struct btrfs_root *quota_root;
2434 struct btrfs_qgroup *qgroup;
2435 struct btrfs_fs_info *fs_info = root->fs_info;
2436 u64 ref_root = root->root_key.objectid;
2437 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002438 struct ulist_node *unode;
2439 struct ulist_iterator uiter;
2440
2441 if (!is_fstree(ref_root))
2442 return 0;
2443
2444 if (num_bytes == 0)
2445 return 0;
Sargun Dhillonf29efe22017-05-11 21:17:33 +00002446
2447 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2448 capable(CAP_SYS_RESOURCE))
2449 enforce = false;
2450
Arne Jansenbed92ea2012-06-28 18:03:02 +02002451 spin_lock(&fs_info->qgroup_lock);
2452 quota_root = fs_info->quota_root;
2453 if (!quota_root)
2454 goto out;
2455
2456 qgroup = find_qgroup_rb(fs_info, ref_root);
2457 if (!qgroup)
2458 goto out;
2459
2460 /*
2461 * in a first step, we check all affected qgroups if any limits would
2462 * be exceeded
2463 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002464 ulist_reinit(fs_info->qgroup_ulist);
2465 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002466 qgroup_to_aux(qgroup), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002467 if (ret < 0)
2468 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002469 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002470 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002471 struct btrfs_qgroup *qg;
2472 struct btrfs_qgroup_list *glist;
2473
David Sterbaef2fff62016-10-26 16:23:50 +02002474 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002475
Qu Wenruoa514d632017-12-22 16:06:39 +08002476 if (enforce && !qgroup_check_limits(fs_info, qg, num_bytes)) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002477 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002478 goto out;
2479 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002480
2481 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002482 ret = ulist_add(fs_info->qgroup_ulist,
2483 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002484 qgroup_to_aux(glist->group), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002485 if (ret < 0)
2486 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002487 }
2488 }
Wang Shilong3c971852013-04-17 14:00:36 +00002489 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002490 /*
2491 * no limits exceeded, now record the reservation into all qgroups
2492 */
2493 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002494 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002495 struct btrfs_qgroup *qg;
2496
David Sterbaef2fff62016-10-26 16:23:50 +02002497 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002498
Qu Wenruo64ee4e72017-12-12 15:34:27 +08002499 trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
2500 qgroup_rsv_add(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002501 }
2502
2503out:
2504 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002505 return ret;
2506}
2507
Qu Wenruoe1211d02017-12-12 15:34:30 +08002508/*
2509 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
2510 * qgroup).
2511 *
2512 * Will handle all higher level qgroup too.
2513 *
2514 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
2515 * This special case is only used for META_PERTRANS type.
2516 */
Qu Wenruo297d7502015-09-08 17:08:37 +08002517void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
Qu Wenruod4e5c922017-12-12 15:34:23 +08002518 u64 ref_root, u64 num_bytes,
2519 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002520{
2521 struct btrfs_root *quota_root;
2522 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002523 struct ulist_node *unode;
2524 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00002525 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002526
2527 if (!is_fstree(ref_root))
2528 return;
2529
2530 if (num_bytes == 0)
2531 return;
2532
Qu Wenruoe1211d02017-12-12 15:34:30 +08002533 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
2534 WARN(1, "%s: Invalid type to free", __func__);
2535 return;
2536 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002537 spin_lock(&fs_info->qgroup_lock);
2538
2539 quota_root = fs_info->quota_root;
2540 if (!quota_root)
2541 goto out;
2542
2543 qgroup = find_qgroup_rb(fs_info, ref_root);
2544 if (!qgroup)
2545 goto out;
2546
Qu Wenruoe1211d02017-12-12 15:34:30 +08002547 if (num_bytes == (u64)-1)
Qu Wenruo82874752017-12-12 15:34:34 +08002548 /*
2549 * We're freeing all pertrans rsv, get reserved value from
2550 * level 0 qgroup as real num_bytes to free.
2551 */
Qu Wenruoe1211d02017-12-12 15:34:30 +08002552 num_bytes = qgroup->rsv.values[type];
2553
Wang Shilong1e8f9152013-05-06 11:03:27 +00002554 ulist_reinit(fs_info->qgroup_ulist);
2555 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002556 qgroup_to_aux(qgroup), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002557 if (ret < 0)
2558 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002559 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002560 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002561 struct btrfs_qgroup *qg;
2562 struct btrfs_qgroup_list *glist;
2563
David Sterbaef2fff62016-10-26 16:23:50 +02002564 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002565
Qu Wenruo64ee4e72017-12-12 15:34:27 +08002566 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
2567 qgroup_rsv_release(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002568
2569 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002570 ret = ulist_add(fs_info->qgroup_ulist,
2571 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02002572 qgroup_to_aux(glist->group), GFP_ATOMIC);
Wang Shilong3c971852013-04-17 14:00:36 +00002573 if (ret < 0)
2574 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002575 }
2576 }
2577
2578out:
2579 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002580}
2581
Jan Schmidt2f232032013-04-25 16:04:51 +00002582/*
Qu Wenruoff3d27a02018-05-14 09:38:13 +08002583 * Check if the leaf is the last leaf. Which means all node pointers
2584 * are at their last position.
2585 */
2586static bool is_last_leaf(struct btrfs_path *path)
2587{
2588 int i;
2589
2590 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
2591 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
2592 return false;
2593 }
2594 return true;
2595}
2596
2597/*
Jan Schmidt2f232032013-04-25 16:04:51 +00002598 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08002599 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00002600 */
2601static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002602qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002603 struct btrfs_trans_handle *trans)
Jan Schmidt2f232032013-04-25 16:04:51 +00002604{
2605 struct btrfs_key found;
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002606 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002607 struct ulist *roots = NULL;
Josef Bacikfcebe452014-05-13 17:30:47 -07002608 u64 num_bytes;
Qu Wenruoff3d27a02018-05-14 09:38:13 +08002609 bool done;
Jan Schmidt2f232032013-04-25 16:04:51 +00002610 int slot;
2611 int ret;
2612
Jan Schmidt2f232032013-04-25 16:04:51 +00002613 mutex_lock(&fs_info->qgroup_rescan_lock);
2614 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2615 &fs_info->qgroup_rescan_progress,
2616 path, 1, 0);
2617
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002618 btrfs_debug(fs_info,
2619 "current progress key (%llu %u %llu), search_slot ret %d",
2620 fs_info->qgroup_rescan_progress.objectid,
2621 fs_info->qgroup_rescan_progress.type,
2622 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002623
2624 if (ret) {
2625 /*
2626 * The rescan is about to end, we will not be scanning any
2627 * further blocks. We cannot unset the RESCAN flag here, because
2628 * we want to commit the transaction if everything went well.
2629 * To make the live accounting work in this phase, we set our
2630 * scan progress pointer such that every real extent objectid
2631 * will be smaller.
2632 */
2633 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2634 btrfs_release_path(path);
2635 mutex_unlock(&fs_info->qgroup_rescan_lock);
2636 return ret;
2637 }
Qu Wenruoff3d27a02018-05-14 09:38:13 +08002638 done = is_last_leaf(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002639
2640 btrfs_item_key_to_cpu(path->nodes[0], &found,
2641 btrfs_header_nritems(path->nodes[0]) - 1);
2642 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2643
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002644 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2645 if (!scratch_leaf) {
2646 ret = -ENOMEM;
2647 mutex_unlock(&fs_info->qgroup_rescan_lock);
2648 goto out;
2649 }
2650 extent_buffer_get(scratch_leaf);
2651 btrfs_tree_read_lock(scratch_leaf);
2652 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
Jan Schmidt2f232032013-04-25 16:04:51 +00002653 slot = path->slots[0];
2654 btrfs_release_path(path);
2655 mutex_unlock(&fs_info->qgroup_rescan_lock);
2656
2657 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2658 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002659 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2660 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002661 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002662 if (found.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04002663 num_bytes = fs_info->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002664 else
2665 num_bytes = found.offset;
2666
Josef Bacikfcebe452014-05-13 17:30:47 -07002667 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002668 &roots, false);
Jan Schmidt2f232032013-04-25 16:04:51 +00002669 if (ret < 0)
2670 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08002671 /* For rescan, just pass old_roots as NULL */
2672 ret = btrfs_qgroup_account_extent(trans, fs_info,
2673 found.objectid, num_bytes, NULL, roots);
2674 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00002675 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002676 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002677out:
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002678 if (scratch_leaf) {
2679 btrfs_tree_read_unlock_blocking(scratch_leaf);
2680 free_extent_buffer(scratch_leaf);
2681 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002682
Qu Wenruo6f7de192018-06-27 18:19:55 +08002683 if (done && !ret) {
Qu Wenruoff3d27a02018-05-14 09:38:13 +08002684 ret = 1;
Qu Wenruo6f7de192018-06-27 18:19:55 +08002685 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2686 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002687 return ret;
2688}
2689
Qu Wenruod458b052014-02-28 10:46:19 +08002690static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002691{
Jan Schmidtb382a322013-05-28 15:47:24 +00002692 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2693 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002694 struct btrfs_path *path;
2695 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002696 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002697 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002698
2699 path = btrfs_alloc_path();
2700 if (!path)
2701 goto out;
Qu Wenruob6debf12018-05-14 09:38:12 +08002702 /*
2703 * Rescan should only search for commit root, and any later difference
2704 * should be recorded by qgroup
2705 */
2706 path->search_commit_root = 1;
2707 path->skip_locking = 1;
Jan Schmidt2f232032013-04-25 16:04:51 +00002708
2709 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08002710 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002711 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2712 if (IS_ERR(trans)) {
2713 err = PTR_ERR(trans);
2714 break;
2715 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002716 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002717 err = -EINTR;
2718 } else {
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002719 err = qgroup_rescan_leaf(fs_info, path, trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002720 }
2721 if (err > 0)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002722 btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002723 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002724 btrfs_end_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002725 }
2726
2727out:
Jan Schmidt2f232032013-04-25 16:04:51 +00002728 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002729
2730 mutex_lock(&fs_info->qgroup_rescan_lock);
Justin Maggard7343dd62015-11-04 15:56:16 -08002731 if (!btrfs_fs_closing(fs_info))
2732 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidt2f232032013-04-25 16:04:51 +00002733
Qu Wenruo33931682015-02-27 16:24:24 +08002734 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00002735 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2736 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2737 } else if (err < 0) {
2738 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2739 }
2740 mutex_unlock(&fs_info->qgroup_rescan_lock);
2741
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002742 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002743 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002744 * qgroup info.
2745 */
2746 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2747 if (IS_ERR(trans)) {
2748 err = PTR_ERR(trans);
2749 btrfs_err(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002750 "fail to start transaction for status update: %d",
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002751 err);
2752 goto done;
2753 }
2754 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2755 if (ret < 0) {
2756 err = ret;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002757 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002758 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002759 btrfs_end_transaction(trans);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002760
Justin Maggard7343dd62015-11-04 15:56:16 -08002761 if (btrfs_fs_closing(fs_info)) {
2762 btrfs_info(fs_info, "qgroup scan paused");
2763 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002764 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08002765 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00002766 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002767 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002768 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002769
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002770done:
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002771 mutex_lock(&fs_info->qgroup_rescan_lock);
2772 fs_info->qgroup_rescan_running = false;
2773 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002774 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002775}
2776
Jan Schmidtb382a322013-05-28 15:47:24 +00002777/*
2778 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2779 * memory required for the rescan context.
2780 */
2781static int
2782qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2783 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002784{
2785 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002786
Qu Wenruo9593bf492018-05-02 13:28:03 +08002787 if (!init_flags) {
2788 /* we're resuming qgroup rescan at mount time */
2789 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN))
2790 btrfs_warn(fs_info,
2791 "qgroup rescan init failed, qgroup is not enabled");
2792 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2793 btrfs_warn(fs_info,
2794 "qgroup rescan init failed, qgroup rescan is not queued");
2795 return -EINVAL;
Jan Schmidtb382a322013-05-28 15:47:24 +00002796 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002797
2798 mutex_lock(&fs_info->qgroup_rescan_lock);
2799 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002800
2801 if (init_flags) {
Qu Wenruo9593bf492018-05-02 13:28:03 +08002802 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2803 btrfs_warn(fs_info,
2804 "qgroup rescan is already in progress");
Jan Schmidtb382a322013-05-28 15:47:24 +00002805 ret = -EINPROGRESS;
Qu Wenruo9593bf492018-05-02 13:28:03 +08002806 } else if (!(fs_info->qgroup_flags &
2807 BTRFS_QGROUP_STATUS_FLAG_ON)) {
2808 btrfs_warn(fs_info,
2809 "qgroup rescan init failed, qgroup is not enabled");
Jan Schmidtb382a322013-05-28 15:47:24 +00002810 ret = -EINVAL;
Qu Wenruo9593bf492018-05-02 13:28:03 +08002811 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002812
2813 if (ret) {
2814 spin_unlock(&fs_info->qgroup_lock);
2815 mutex_unlock(&fs_info->qgroup_rescan_lock);
Qu Wenruo9593bf492018-05-02 13:28:03 +08002816 return ret;
Jan Schmidtb382a322013-05-28 15:47:24 +00002817 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002818 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2819 }
2820
2821 memset(&fs_info->qgroup_rescan_progress, 0,
2822 sizeof(fs_info->qgroup_rescan_progress));
2823 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00002824 init_completion(&fs_info->qgroup_rescan_completion);
Filipe Manana8d9edda2016-11-24 02:09:04 +00002825 fs_info->qgroup_rescan_running = true;
Jan Schmidtb382a322013-05-28 15:47:24 +00002826
2827 spin_unlock(&fs_info->qgroup_lock);
2828 mutex_unlock(&fs_info->qgroup_rescan_lock);
2829
Jan Schmidtb382a322013-05-28 15:47:24 +00002830 memset(&fs_info->qgroup_rescan_work, 0,
2831 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002832 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002833 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002834 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002835 return 0;
2836}
Jan Schmidt2f232032013-04-25 16:04:51 +00002837
Jan Schmidtb382a322013-05-28 15:47:24 +00002838static void
2839qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2840{
2841 struct rb_node *n;
2842 struct btrfs_qgroup *qgroup;
2843
2844 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002845 /* clear all current qgroup tracking information */
2846 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2847 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2848 qgroup->rfer = 0;
2849 qgroup->rfer_cmpr = 0;
2850 qgroup->excl = 0;
2851 qgroup->excl_cmpr = 0;
2852 }
2853 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002854}
Jan Schmidt2f232032013-04-25 16:04:51 +00002855
Jan Schmidtb382a322013-05-28 15:47:24 +00002856int
2857btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2858{
2859 int ret = 0;
2860 struct btrfs_trans_handle *trans;
2861
2862 ret = qgroup_rescan_init(fs_info, 0, 1);
2863 if (ret)
2864 return ret;
2865
2866 /*
2867 * We have set the rescan_progress to 0, which means no more
2868 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2869 * However, btrfs_qgroup_account_ref may be right after its call
2870 * to btrfs_find_all_roots, in which case it would still do the
2871 * accounting.
2872 * To solve this, we're committing the transaction, which will
2873 * ensure we run all delayed refs and only after that, we are
2874 * going to clear all tracking information for a clean start.
2875 */
2876
2877 trans = btrfs_join_transaction(fs_info->fs_root);
2878 if (IS_ERR(trans)) {
2879 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2880 return PTR_ERR(trans);
2881 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002882 ret = btrfs_commit_transaction(trans);
Jan Schmidtb382a322013-05-28 15:47:24 +00002883 if (ret) {
2884 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2885 return ret;
2886 }
2887
2888 qgroup_rescan_zero_tracking(fs_info);
2889
Qu Wenruofc97fab2014-02-28 10:46:16 +08002890 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2891 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002892
2893 return 0;
2894}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002895
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002896int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2897 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002898{
2899 int running;
2900 int ret = 0;
2901
2902 mutex_lock(&fs_info->qgroup_rescan_lock);
2903 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002904 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002905 spin_unlock(&fs_info->qgroup_lock);
2906 mutex_unlock(&fs_info->qgroup_rescan_lock);
2907
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002908 if (!running)
2909 return 0;
2910
2911 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002912 ret = wait_for_completion_interruptible(
2913 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002914 else
2915 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002916
2917 return ret;
2918}
Jan Schmidtb382a322013-05-28 15:47:24 +00002919
2920/*
2921 * this is only called from open_ctree where we're still single threaded, thus
2922 * locking is omitted here.
2923 */
2924void
2925btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2926{
2927 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002928 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2929 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002930}
Qu Wenruo52472552015-10-12 16:05:40 +08002931
2932/*
2933 * Reserve qgroup space for range [start, start + len).
2934 *
2935 * This function will either reserve space from related qgroups or doing
2936 * nothing if the range is already reserved.
2937 *
2938 * Return 0 for successful reserve
2939 * Return <0 for error (including -EQUOT)
2940 *
2941 * NOTE: this function may sleep for memory allocation.
Qu Wenruo364ecf32017-02-27 15:10:38 +08002942 * if btrfs_qgroup_reserve_data() is called multiple times with
2943 * same @reserved, caller must ensure when error happens it's OK
2944 * to free *ALL* reserved space.
Qu Wenruo52472552015-10-12 16:05:40 +08002945 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08002946int btrfs_qgroup_reserve_data(struct inode *inode,
2947 struct extent_changeset **reserved_ret, u64 start,
2948 u64 len)
Qu Wenruo52472552015-10-12 16:05:40 +08002949{
2950 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo52472552015-10-12 16:05:40 +08002951 struct ulist_node *unode;
2952 struct ulist_iterator uiter;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002953 struct extent_changeset *reserved;
2954 u64 orig_reserved;
2955 u64 to_reserve;
Qu Wenruo52472552015-10-12 16:05:40 +08002956 int ret;
2957
Josef Bacikafcdd122016-09-02 15:40:02 -04002958 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2959 !is_fstree(root->objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08002960 return 0;
2961
Qu Wenruo364ecf32017-02-27 15:10:38 +08002962 /* @reserved parameter is mandatory for qgroup */
2963 if (WARN_ON(!reserved_ret))
2964 return -EINVAL;
2965 if (!*reserved_ret) {
2966 *reserved_ret = extent_changeset_alloc();
2967 if (!*reserved_ret)
2968 return -ENOMEM;
2969 }
2970 reserved = *reserved_ret;
2971 /* Record already reserved space */
2972 orig_reserved = reserved->bytes_changed;
Qu Wenruo52472552015-10-12 16:05:40 +08002973 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002974 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2975
2976 /* Newly reserved space */
2977 to_reserve = reserved->bytes_changed - orig_reserved;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002978 trace_btrfs_qgroup_reserve_data(inode, start, len,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002979 to_reserve, QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08002980 if (ret < 0)
2981 goto cleanup;
Qu Wenruodba21322017-12-12 15:34:25 +08002982 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo52472552015-10-12 16:05:40 +08002983 if (ret < 0)
2984 goto cleanup;
2985
Qu Wenruo52472552015-10-12 16:05:40 +08002986 return ret;
2987
2988cleanup:
Qu Wenruo364ecf32017-02-27 15:10:38 +08002989 /* cleanup *ALL* already reserved ranges */
Qu Wenruo52472552015-10-12 16:05:40 +08002990 ULIST_ITER_INIT(&uiter);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002991 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
Qu Wenruo52472552015-10-12 16:05:40 +08002992 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
David Sterbaae0f1622017-10-31 16:37:52 +01002993 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002994 extent_changeset_release(reserved);
Qu Wenruo52472552015-10-12 16:05:40 +08002995 return ret;
2996}
Qu Wenruof695fdc2015-10-12 16:28:06 +08002997
Qu Wenruobc42bda2017-02-27 15:10:39 +08002998/* Free ranges specified by @reserved, normally in error path */
2999static int qgroup_free_reserved_data(struct inode *inode,
3000 struct extent_changeset *reserved, u64 start, u64 len)
3001{
3002 struct btrfs_root *root = BTRFS_I(inode)->root;
3003 struct ulist_node *unode;
3004 struct ulist_iterator uiter;
3005 struct extent_changeset changeset;
3006 int freed = 0;
3007 int ret;
3008
3009 extent_changeset_init(&changeset);
3010 len = round_up(start + len, root->fs_info->sectorsize);
3011 start = round_down(start, root->fs_info->sectorsize);
3012
3013 ULIST_ITER_INIT(&uiter);
3014 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
3015 u64 range_start = unode->val;
3016 /* unode->aux is the inclusive end */
3017 u64 range_len = unode->aux - range_start + 1;
3018 u64 free_start;
3019 u64 free_len;
3020
3021 extent_changeset_release(&changeset);
3022
3023 /* Only free range in range [start, start + len) */
3024 if (range_start >= start + len ||
3025 range_start + range_len <= start)
3026 continue;
3027 free_start = max(range_start, start);
3028 free_len = min(start + len, range_start + range_len) -
3029 free_start;
3030 /*
3031 * TODO: To also modify reserved->ranges_reserved to reflect
3032 * the modification.
3033 *
3034 * However as long as we free qgroup reserved according to
3035 * EXTENT_QGROUP_RESERVED, we won't double free.
3036 * So not need to rush.
3037 */
3038 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
3039 free_start, free_start + free_len - 1,
3040 EXTENT_QGROUP_RESERVED, &changeset);
3041 if (ret < 0)
3042 goto out;
3043 freed += changeset.bytes_changed;
3044 }
Qu Wenruod4e5c922017-12-12 15:34:23 +08003045 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
3046 BTRFS_QGROUP_RSV_DATA);
Qu Wenruobc42bda2017-02-27 15:10:39 +08003047 ret = freed;
3048out:
3049 extent_changeset_release(&changeset);
3050 return ret;
3051}
3052
3053static int __btrfs_qgroup_release_data(struct inode *inode,
3054 struct extent_changeset *reserved, u64 start, u64 len,
3055 int free)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003056{
3057 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08003058 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003059 int ret;
3060
Qu Wenruobc42bda2017-02-27 15:10:39 +08003061 /* In release case, we shouldn't have @reserved */
3062 WARN_ON(!free && reserved);
3063 if (free && reserved)
3064 return qgroup_free_reserved_data(inode, reserved, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08003065 extent_changeset_init(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003066 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterbaf734c442016-04-26 23:54:39 +02003067 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003068 if (ret < 0)
3069 goto out;
3070
Qu Wenruod51ea5d2017-03-13 15:52:09 +08003071 if (free)
3072 trace_op = QGROUP_FREE;
3073 trace_btrfs_qgroup_release_data(inode, start, len,
3074 changeset.bytes_changed, trace_op);
3075 if (free)
David Sterba0b08e1f2017-02-13 14:24:35 +01003076 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3077 BTRFS_I(inode)->root->objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003078 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08003079 ret = changeset.bytes_changed;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003080out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08003081 extent_changeset_release(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003082 return ret;
3083}
3084
3085/*
3086 * Free a reserved space range from io_tree and related qgroups
3087 *
3088 * Should be called when a range of pages get invalidated before reaching disk.
3089 * Or for error cleanup case.
Qu Wenruobc42bda2017-02-27 15:10:39 +08003090 * if @reserved is given, only reserved range in [@start, @start + @len) will
3091 * be freed.
Qu Wenruof695fdc2015-10-12 16:28:06 +08003092 *
3093 * For data written to disk, use btrfs_qgroup_release_data().
3094 *
3095 * NOTE: This function may sleep for memory allocation.
3096 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003097int btrfs_qgroup_free_data(struct inode *inode,
3098 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003099{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003100 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003101}
3102
3103/*
3104 * Release a reserved space range from io_tree only.
3105 *
3106 * Should be called when a range of pages get written to disk and corresponding
3107 * FILE_EXTENT is inserted into corresponding root.
3108 *
3109 * Since new qgroup accounting framework will only update qgroup numbers at
3110 * commit_transaction() time, its reserved space shouldn't be freed from
3111 * related qgroups.
3112 *
3113 * But we should release the range from io_tree, to allow further write to be
3114 * COWed.
3115 *
3116 * NOTE: This function may sleep for memory allocation.
3117 */
3118int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3119{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003120 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003121}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003122
Qu Wenruo82874752017-12-12 15:34:34 +08003123static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3124 enum btrfs_qgroup_rsv_type type)
3125{
3126 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3127 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3128 return;
3129 if (num_bytes == 0)
3130 return;
3131
3132 spin_lock(&root->qgroup_meta_rsv_lock);
3133 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3134 root->qgroup_meta_rsv_prealloc += num_bytes;
3135 else
3136 root->qgroup_meta_rsv_pertrans += num_bytes;
3137 spin_unlock(&root->qgroup_meta_rsv_lock);
3138}
3139
3140static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3141 enum btrfs_qgroup_rsv_type type)
3142{
3143 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3144 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3145 return 0;
3146 if (num_bytes == 0)
3147 return 0;
3148
3149 spin_lock(&root->qgroup_meta_rsv_lock);
3150 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3151 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3152 num_bytes);
3153 root->qgroup_meta_rsv_prealloc -= num_bytes;
3154 } else {
3155 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3156 num_bytes);
3157 root->qgroup_meta_rsv_pertrans -= num_bytes;
3158 }
3159 spin_unlock(&root->qgroup_meta_rsv_lock);
3160 return num_bytes;
3161}
3162
Qu Wenruo733e03a2017-12-12 15:34:29 +08003163int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3164 enum btrfs_qgroup_rsv_type type, bool enforce)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003166 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003167 int ret;
3168
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003169 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003170 !is_fstree(root->objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003171 return 0;
3172
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003173 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003174 trace_qgroup_meta_reserve(root, type, (s64)num_bytes);
Qu Wenruo733e03a2017-12-12 15:34:29 +08003175 ret = qgroup_reserve(root, num_bytes, enforce, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003176 if (ret < 0)
3177 return ret;
Qu Wenruo82874752017-12-12 15:34:34 +08003178 /*
3179 * Record what we have reserved into root.
3180 *
3181 * To avoid quota disabled->enabled underflow.
3182 * In that case, we may try to free space we haven't reserved
3183 * (since quota was disabled), so record what we reserved into root.
3184 * And ensure later release won't underflow this number.
3185 */
3186 add_root_meta_rsv(root, num_bytes, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003187 return ret;
3188}
3189
Qu Wenruo733e03a2017-12-12 15:34:29 +08003190void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003192 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003193
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003194 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003195 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003196 return;
3197
Qu Wenruoe1211d02017-12-12 15:34:30 +08003198 /* TODO: Update trace point to handle such free */
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003199 trace_qgroup_meta_free_all_pertrans(root);
Qu Wenruoe1211d02017-12-12 15:34:30 +08003200 /* Special value -1 means to free all reserved space */
3201 btrfs_qgroup_free_refroot(fs_info, root->objectid, (u64)-1,
Qu Wenruo733e03a2017-12-12 15:34:29 +08003202 BTRFS_QGROUP_RSV_META_PERTRANS);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003203}
3204
Qu Wenruo733e03a2017-12-12 15:34:29 +08003205void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3206 enum btrfs_qgroup_rsv_type type)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003207{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003208 struct btrfs_fs_info *fs_info = root->fs_info;
3209
3210 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003211 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003212 return;
3213
Qu Wenruo82874752017-12-12 15:34:34 +08003214 /*
3215 * reservation for META_PREALLOC can happen before quota is enabled,
3216 * which can lead to underflow.
3217 * Here ensure we will only free what we really have reserved.
3218 */
3219 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003220 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003221 trace_qgroup_meta_reserve(root, type, -(s64)num_bytes);
Qu Wenruo733e03a2017-12-12 15:34:29 +08003222 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes, type);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003223}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003224
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003225static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3226 int num_bytes)
3227{
3228 struct btrfs_root *quota_root = fs_info->quota_root;
3229 struct btrfs_qgroup *qgroup;
3230 struct ulist_node *unode;
3231 struct ulist_iterator uiter;
3232 int ret = 0;
3233
3234 if (num_bytes == 0)
3235 return;
3236 if (!quota_root)
3237 return;
3238
3239 spin_lock(&fs_info->qgroup_lock);
3240 qgroup = find_qgroup_rb(fs_info, ref_root);
3241 if (!qgroup)
3242 goto out;
3243 ulist_reinit(fs_info->qgroup_ulist);
3244 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003245 qgroup_to_aux(qgroup), GFP_ATOMIC);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003246 if (ret < 0)
3247 goto out;
3248 ULIST_ITER_INIT(&uiter);
3249 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3250 struct btrfs_qgroup *qg;
3251 struct btrfs_qgroup_list *glist;
3252
3253 qg = unode_aux_to_qgroup(unode);
3254
3255 qgroup_rsv_release(fs_info, qg, num_bytes,
3256 BTRFS_QGROUP_RSV_META_PREALLOC);
3257 qgroup_rsv_add(fs_info, qg, num_bytes,
3258 BTRFS_QGROUP_RSV_META_PERTRANS);
3259 list_for_each_entry(glist, &qg->groups, next_group) {
3260 ret = ulist_add(fs_info->qgroup_ulist,
3261 glist->group->qgroupid,
David Sterbaa1840b52018-03-27 19:04:50 +02003262 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003263 if (ret < 0)
3264 goto out;
3265 }
3266 }
3267out:
3268 spin_unlock(&fs_info->qgroup_lock);
3269}
3270
3271void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3272{
3273 struct btrfs_fs_info *fs_info = root->fs_info;
3274
3275 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
3276 !is_fstree(root->objectid))
3277 return;
Qu Wenruo82874752017-12-12 15:34:34 +08003278 /* Same as btrfs_qgroup_free_meta_prealloc() */
3279 num_bytes = sub_root_meta_rsv(root, num_bytes,
3280 BTRFS_QGROUP_RSV_META_PREALLOC);
Qu Wenruo4ee0d882017-12-12 15:34:35 +08003281 trace_qgroup_meta_convert(root, num_bytes);
Qu Wenruo64cfaef2017-12-12 15:34:31 +08003282 qgroup_convert_meta(fs_info, root->objectid, num_bytes);
3283}
3284
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003285/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003286 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003287 * time
3288 */
3289void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3290{
3291 struct extent_changeset changeset;
3292 struct ulist_node *unode;
3293 struct ulist_iterator iter;
3294 int ret;
3295
Qu Wenruo364ecf32017-02-27 15:10:38 +08003296 extent_changeset_init(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003297 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02003298 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003299
3300 WARN_ON(ret < 0);
3301 if (WARN_ON(changeset.bytes_changed)) {
3302 ULIST_ITER_INIT(&iter);
David Sterba53d32352017-02-13 13:42:29 +01003303 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003304 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3305 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3306 inode->i_ino, unode->val, unode->aux);
3307 }
David Sterba0b08e1f2017-02-13 14:24:35 +01003308 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3309 BTRFS_I(inode)->root->objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003310 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
David Sterba0b08e1f2017-02-13 14:24:35 +01003311
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003312 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08003313 extent_changeset_release(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003314}