blob: e172d4843eae2d8eb6f0d29dce38fb7f693f4ed0 [file] [log] [blame]
Arne Jansenbed92ea2012-06-28 18:03:02 +02001/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
20#include <linux/pagemap.h>
21#include <linux/writeback.h>
22#include <linux/blkdev.h>
23#include <linux/rbtree.h>
24#include <linux/slab.h>
25#include <linux/workqueue.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Arne Jansenbed92ea2012-06-28 18:03:02 +020027
28#include "ctree.h"
29#include "transaction.h"
30#include "disk-io.h"
31#include "locking.h"
32#include "ulist.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020033#include "backref.h"
Jan Schmidt2f232032013-04-25 16:04:51 +000034#include "extent_io.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070035#include "qgroup.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020036
Qu Wenruoe69bcee2015-04-17 10:23:16 +080037
Arne Jansenbed92ea2012-06-28 18:03:02 +020038/* TODO XXX FIXME
39 * - subvol delete -> delete when ref goes to 0? delete limits also?
40 * - reorganize keys
41 * - compressed
42 * - sync
Arne Jansenbed92ea2012-06-28 18:03:02 +020043 * - copy also limits on subvol creation
44 * - limit
45 * - caches fuer ulists
46 * - performance benchmarks
47 * - check all ioctl parameters
48 */
49
Qu Wenruo9c542132015-03-12 16:10:13 +080050static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
51 int mod)
52{
53 if (qg->old_refcnt < seq)
54 qg->old_refcnt = seq;
55 qg->old_refcnt += mod;
56}
57
58static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
59 int mod)
60{
61 if (qg->new_refcnt < seq)
62 qg->new_refcnt = seq;
63 qg->new_refcnt += mod;
64}
65
66static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
67{
68 if (qg->old_refcnt < seq)
69 return 0;
70 return qg->old_refcnt - seq;
71}
72
73static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
74{
75 if (qg->new_refcnt < seq)
76 return 0;
77 return qg->new_refcnt - seq;
78}
79
Arne Jansenbed92ea2012-06-28 18:03:02 +020080/*
81 * glue structure to represent the relations between qgroups.
82 */
83struct btrfs_qgroup_list {
84 struct list_head next_group;
85 struct list_head next_member;
86 struct btrfs_qgroup *group;
87 struct btrfs_qgroup *member;
88};
89
David Sterbaef2fff62016-10-26 16:23:50 +020090static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
91{
92 return (u64)(uintptr_t)qg;
93}
94
95static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
96{
97 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
98}
Josef Bacikfcebe452014-05-13 17:30:47 -070099
Jan Schmidtb382a322013-05-28 15:47:24 +0000100static int
101qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
102 int init_flags);
103static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000104
Wang Shilong58400fc2013-04-07 10:50:17 +0000105/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200106static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
107 u64 qgroupid)
108{
109 struct rb_node *n = fs_info->qgroup_tree.rb_node;
110 struct btrfs_qgroup *qgroup;
111
112 while (n) {
113 qgroup = rb_entry(n, struct btrfs_qgroup, node);
114 if (qgroup->qgroupid < qgroupid)
115 n = n->rb_left;
116 else if (qgroup->qgroupid > qgroupid)
117 n = n->rb_right;
118 else
119 return qgroup;
120 }
121 return NULL;
122}
123
124/* must be called with qgroup_lock held */
125static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
126 u64 qgroupid)
127{
128 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
129 struct rb_node *parent = NULL;
130 struct btrfs_qgroup *qgroup;
131
132 while (*p) {
133 parent = *p;
134 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
135
136 if (qgroup->qgroupid < qgroupid)
137 p = &(*p)->rb_left;
138 else if (qgroup->qgroupid > qgroupid)
139 p = &(*p)->rb_right;
140 else
141 return qgroup;
142 }
143
144 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
145 if (!qgroup)
146 return ERR_PTR(-ENOMEM);
147
148 qgroup->qgroupid = qgroupid;
149 INIT_LIST_HEAD(&qgroup->groups);
150 INIT_LIST_HEAD(&qgroup->members);
151 INIT_LIST_HEAD(&qgroup->dirty);
152
153 rb_link_node(&qgroup->node, parent, p);
154 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
155
156 return qgroup;
157}
158
Wang Shilong4082bd32013-08-14 09:13:36 +0800159static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200160{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200161 struct btrfs_qgroup_list *list;
162
Arne Jansenbed92ea2012-06-28 18:03:02 +0200163 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200164 while (!list_empty(&qgroup->groups)) {
165 list = list_first_entry(&qgroup->groups,
166 struct btrfs_qgroup_list, next_group);
167 list_del(&list->next_group);
168 list_del(&list->next_member);
169 kfree(list);
170 }
171
172 while (!list_empty(&qgroup->members)) {
173 list = list_first_entry(&qgroup->members,
174 struct btrfs_qgroup_list, next_member);
175 list_del(&list->next_group);
176 list_del(&list->next_member);
177 kfree(list);
178 }
179 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800180}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200181
Wang Shilong4082bd32013-08-14 09:13:36 +0800182/* must be called with qgroup_lock held */
183static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
184{
185 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
186
187 if (!qgroup)
188 return -ENOENT;
189
190 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
191 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200192 return 0;
193}
194
195/* must be called with qgroup_lock held */
196static int add_relation_rb(struct btrfs_fs_info *fs_info,
197 u64 memberid, u64 parentid)
198{
199 struct btrfs_qgroup *member;
200 struct btrfs_qgroup *parent;
201 struct btrfs_qgroup_list *list;
202
203 member = find_qgroup_rb(fs_info, memberid);
204 parent = find_qgroup_rb(fs_info, parentid);
205 if (!member || !parent)
206 return -ENOENT;
207
208 list = kzalloc(sizeof(*list), GFP_ATOMIC);
209 if (!list)
210 return -ENOMEM;
211
212 list->group = parent;
213 list->member = member;
214 list_add_tail(&list->next_group, &member->groups);
215 list_add_tail(&list->next_member, &parent->members);
216
217 return 0;
218}
219
220/* must be called with qgroup_lock held */
221static int del_relation_rb(struct btrfs_fs_info *fs_info,
222 u64 memberid, u64 parentid)
223{
224 struct btrfs_qgroup *member;
225 struct btrfs_qgroup *parent;
226 struct btrfs_qgroup_list *list;
227
228 member = find_qgroup_rb(fs_info, memberid);
229 parent = find_qgroup_rb(fs_info, parentid);
230 if (!member || !parent)
231 return -ENOENT;
232
233 list_for_each_entry(list, &member->groups, next_group) {
234 if (list->group == parent) {
235 list_del(&list->next_group);
236 list_del(&list->next_member);
237 kfree(list);
238 return 0;
239 }
240 }
241 return -ENOENT;
242}
243
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400244#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
245int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
246 u64 rfer, u64 excl)
247{
248 struct btrfs_qgroup *qgroup;
249
250 qgroup = find_qgroup_rb(fs_info, qgroupid);
251 if (!qgroup)
252 return -EINVAL;
253 if (qgroup->rfer != rfer || qgroup->excl != excl)
254 return -EINVAL;
255 return 0;
256}
257#endif
258
Arne Jansenbed92ea2012-06-28 18:03:02 +0200259/*
260 * The full config is read in one go, only called from open_ctree()
261 * It doesn't use any locking, as at this point we're still single-threaded
262 */
263int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
264{
265 struct btrfs_key key;
266 struct btrfs_key found_key;
267 struct btrfs_root *quota_root = fs_info->quota_root;
268 struct btrfs_path *path = NULL;
269 struct extent_buffer *l;
270 int slot;
271 int ret = 0;
272 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000273 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200274
Josef Bacikafcdd122016-09-02 15:40:02 -0400275 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +0200276 return 0;
277
David Sterba323b88f2017-02-13 12:10:20 +0100278 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000279 if (!fs_info->qgroup_ulist) {
280 ret = -ENOMEM;
281 goto out;
282 }
283
Arne Jansenbed92ea2012-06-28 18:03:02 +0200284 path = btrfs_alloc_path();
285 if (!path) {
286 ret = -ENOMEM;
287 goto out;
288 }
289
290 /* default this to quota off, in case no status key is found */
291 fs_info->qgroup_flags = 0;
292
293 /*
294 * pass 1: read status, all qgroup infos and limits
295 */
296 key.objectid = 0;
297 key.type = 0;
298 key.offset = 0;
299 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
300 if (ret)
301 goto out;
302
303 while (1) {
304 struct btrfs_qgroup *qgroup;
305
306 slot = path->slots[0];
307 l = path->nodes[0];
308 btrfs_item_key_to_cpu(l, &found_key, slot);
309
310 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
311 struct btrfs_qgroup_status_item *ptr;
312
313 ptr = btrfs_item_ptr(l, slot,
314 struct btrfs_qgroup_status_item);
315
316 if (btrfs_qgroup_status_version(l, ptr) !=
317 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500318 btrfs_err(fs_info,
319 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200320 goto out;
321 }
322 if (btrfs_qgroup_status_generation(l, ptr) !=
323 fs_info->generation) {
324 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500325 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400326 "qgroup generation mismatch, marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200327 }
328 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
329 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000330 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200331 goto next1;
332 }
333
334 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
335 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
336 goto next1;
337
338 qgroup = find_qgroup_rb(fs_info, found_key.offset);
339 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
340 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Geert Uytterhoevend41e36a2015-07-06 15:38:11 +0200341 btrfs_err(fs_info, "inconsistent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200342 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
343 }
344 if (!qgroup) {
345 qgroup = add_qgroup_rb(fs_info, found_key.offset);
346 if (IS_ERR(qgroup)) {
347 ret = PTR_ERR(qgroup);
348 goto out;
349 }
350 }
351 switch (found_key.type) {
352 case BTRFS_QGROUP_INFO_KEY: {
353 struct btrfs_qgroup_info_item *ptr;
354
355 ptr = btrfs_item_ptr(l, slot,
356 struct btrfs_qgroup_info_item);
357 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
358 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
359 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
360 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
361 /* generation currently unused */
362 break;
363 }
364 case BTRFS_QGROUP_LIMIT_KEY: {
365 struct btrfs_qgroup_limit_item *ptr;
366
367 ptr = btrfs_item_ptr(l, slot,
368 struct btrfs_qgroup_limit_item);
369 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
370 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
371 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
372 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
373 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
374 break;
375 }
376 }
377next1:
378 ret = btrfs_next_item(quota_root, path);
379 if (ret < 0)
380 goto out;
381 if (ret)
382 break;
383 }
384 btrfs_release_path(path);
385
386 /*
387 * pass 2: read all qgroup relations
388 */
389 key.objectid = 0;
390 key.type = BTRFS_QGROUP_RELATION_KEY;
391 key.offset = 0;
392 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
393 if (ret)
394 goto out;
395 while (1) {
396 slot = path->slots[0];
397 l = path->nodes[0];
398 btrfs_item_key_to_cpu(l, &found_key, slot);
399
400 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
401 goto next2;
402
403 if (found_key.objectid > found_key.offset) {
404 /* parent <- member, not needed to build config */
405 /* FIXME should we omit the key completely? */
406 goto next2;
407 }
408
409 ret = add_relation_rb(fs_info, found_key.objectid,
410 found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700411 if (ret == -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500412 btrfs_warn(fs_info,
413 "orphan qgroup relation 0x%llx->0x%llx",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200414 found_key.objectid, found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700415 ret = 0; /* ignore the error */
416 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200417 if (ret)
418 goto out;
419next2:
420 ret = btrfs_next_item(quota_root, path);
421 if (ret < 0)
422 goto out;
423 if (ret)
424 break;
425 }
426out:
427 fs_info->qgroup_flags |= flags;
Josef Bacikafcdd122016-09-02 15:40:02 -0400428 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
429 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
430 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
431 ret >= 0)
Jan Schmidtb382a322013-05-28 15:47:24 +0000432 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200433 btrfs_free_path(path);
434
Jan Schmidteb1716a2013-05-28 15:47:23 +0000435 if (ret < 0) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000436 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000437 fs_info->qgroup_ulist = NULL;
Jan Schmidtb382a322013-05-28 15:47:24 +0000438 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidteb1716a2013-05-28 15:47:23 +0000439 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000440
Arne Jansenbed92ea2012-06-28 18:03:02 +0200441 return ret < 0 ? ret : 0;
442}
443
444/*
Wang Shilonge685da12013-08-14 09:13:37 +0800445 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
446 * first two are in single-threaded paths.And for the third one, we have set
447 * quota_root to be null with qgroup_lock held before, so it is safe to clean
448 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200449 */
450void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
451{
452 struct rb_node *n;
453 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200454
455 while ((n = rb_first(&fs_info->qgroup_tree))) {
456 qgroup = rb_entry(n, struct btrfs_qgroup, node);
457 rb_erase(n, &fs_info->qgroup_tree);
Wang Shilong4082bd32013-08-14 09:13:36 +0800458 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200459 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800460 /*
461 * we call btrfs_free_qgroup_config() when umounting
Nicholas D Steeves01327612016-05-19 21:18:45 -0400462 * filesystem and disabling quota, so we set qgroup_ulist
Wang Shilong1e7bac12013-07-13 21:02:54 +0800463 * to be null here to avoid double free.
464 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000465 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800466 fs_info->qgroup_ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200467}
468
469static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
470 struct btrfs_root *quota_root,
471 u64 src, u64 dst)
472{
473 int ret;
474 struct btrfs_path *path;
475 struct btrfs_key key;
476
477 path = btrfs_alloc_path();
478 if (!path)
479 return -ENOMEM;
480
481 key.objectid = src;
482 key.type = BTRFS_QGROUP_RELATION_KEY;
483 key.offset = dst;
484
485 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
486
487 btrfs_mark_buffer_dirty(path->nodes[0]);
488
489 btrfs_free_path(path);
490 return ret;
491}
492
493static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
494 struct btrfs_root *quota_root,
495 u64 src, u64 dst)
496{
497 int ret;
498 struct btrfs_path *path;
499 struct btrfs_key key;
500
501 path = btrfs_alloc_path();
502 if (!path)
503 return -ENOMEM;
504
505 key.objectid = src;
506 key.type = BTRFS_QGROUP_RELATION_KEY;
507 key.offset = dst;
508
509 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
510 if (ret < 0)
511 goto out;
512
513 if (ret > 0) {
514 ret = -ENOENT;
515 goto out;
516 }
517
518 ret = btrfs_del_item(trans, quota_root, path);
519out:
520 btrfs_free_path(path);
521 return ret;
522}
523
524static int add_qgroup_item(struct btrfs_trans_handle *trans,
525 struct btrfs_root *quota_root, u64 qgroupid)
526{
527 int ret;
528 struct btrfs_path *path;
529 struct btrfs_qgroup_info_item *qgroup_info;
530 struct btrfs_qgroup_limit_item *qgroup_limit;
531 struct extent_buffer *leaf;
532 struct btrfs_key key;
533
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400534 if (btrfs_is_testing(quota_root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400535 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200536
Arne Jansenbed92ea2012-06-28 18:03:02 +0200537 path = btrfs_alloc_path();
538 if (!path)
539 return -ENOMEM;
540
541 key.objectid = 0;
542 key.type = BTRFS_QGROUP_INFO_KEY;
543 key.offset = qgroupid;
544
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700545 /*
546 * Avoid a transaction abort by catching -EEXIST here. In that
547 * case, we proceed by re-initializing the existing structure
548 * on disk.
549 */
550
Arne Jansenbed92ea2012-06-28 18:03:02 +0200551 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
552 sizeof(*qgroup_info));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700553 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200554 goto out;
555
556 leaf = path->nodes[0];
557 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
558 struct btrfs_qgroup_info_item);
559 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
560 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
561 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
562 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
563 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
564
565 btrfs_mark_buffer_dirty(leaf);
566
567 btrfs_release_path(path);
568
569 key.type = BTRFS_QGROUP_LIMIT_KEY;
570 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
571 sizeof(*qgroup_limit));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700572 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200573 goto out;
574
575 leaf = path->nodes[0];
576 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
577 struct btrfs_qgroup_limit_item);
578 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
579 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
580 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
581 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
582 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
583
584 btrfs_mark_buffer_dirty(leaf);
585
586 ret = 0;
587out:
588 btrfs_free_path(path);
589 return ret;
590}
591
592static int del_qgroup_item(struct btrfs_trans_handle *trans,
593 struct btrfs_root *quota_root, u64 qgroupid)
594{
595 int ret;
596 struct btrfs_path *path;
597 struct btrfs_key key;
598
599 path = btrfs_alloc_path();
600 if (!path)
601 return -ENOMEM;
602
603 key.objectid = 0;
604 key.type = BTRFS_QGROUP_INFO_KEY;
605 key.offset = qgroupid;
606 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
607 if (ret < 0)
608 goto out;
609
610 if (ret > 0) {
611 ret = -ENOENT;
612 goto out;
613 }
614
615 ret = btrfs_del_item(trans, quota_root, path);
616 if (ret)
617 goto out;
618
619 btrfs_release_path(path);
620
621 key.type = BTRFS_QGROUP_LIMIT_KEY;
622 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
623 if (ret < 0)
624 goto out;
625
626 if (ret > 0) {
627 ret = -ENOENT;
628 goto out;
629 }
630
631 ret = btrfs_del_item(trans, quota_root, path);
632
633out:
634 btrfs_free_path(path);
635 return ret;
636}
637
638static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
Dongsheng Yang1510e712014-11-20 21:01:41 -0500639 struct btrfs_root *root,
640 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200641{
642 struct btrfs_path *path;
643 struct btrfs_key key;
644 struct extent_buffer *l;
645 struct btrfs_qgroup_limit_item *qgroup_limit;
646 int ret;
647 int slot;
648
649 key.objectid = 0;
650 key.type = BTRFS_QGROUP_LIMIT_KEY;
Dongsheng Yang1510e712014-11-20 21:01:41 -0500651 key.offset = qgroup->qgroupid;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200652
653 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000654 if (!path)
655 return -ENOMEM;
656
Arne Jansenbed92ea2012-06-28 18:03:02 +0200657 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
658 if (ret > 0)
659 ret = -ENOENT;
660
661 if (ret)
662 goto out;
663
664 l = path->nodes[0];
665 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100666 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Dongsheng Yang1510e712014-11-20 21:01:41 -0500667 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
668 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
669 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
670 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
671 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200672
673 btrfs_mark_buffer_dirty(l);
674
675out:
676 btrfs_free_path(path);
677 return ret;
678}
679
680static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
681 struct btrfs_root *root,
682 struct btrfs_qgroup *qgroup)
683{
684 struct btrfs_path *path;
685 struct btrfs_key key;
686 struct extent_buffer *l;
687 struct btrfs_qgroup_info_item *qgroup_info;
688 int ret;
689 int slot;
690
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400691 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400692 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200693
Arne Jansenbed92ea2012-06-28 18:03:02 +0200694 key.objectid = 0;
695 key.type = BTRFS_QGROUP_INFO_KEY;
696 key.offset = qgroup->qgroupid;
697
698 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000699 if (!path)
700 return -ENOMEM;
701
Arne Jansenbed92ea2012-06-28 18:03:02 +0200702 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
703 if (ret > 0)
704 ret = -ENOENT;
705
706 if (ret)
707 goto out;
708
709 l = path->nodes[0];
710 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100711 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200712 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
713 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
714 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
715 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
716 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
717
718 btrfs_mark_buffer_dirty(l);
719
720out:
721 btrfs_free_path(path);
722 return ret;
723}
724
725static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
726 struct btrfs_fs_info *fs_info,
727 struct btrfs_root *root)
728{
729 struct btrfs_path *path;
730 struct btrfs_key key;
731 struct extent_buffer *l;
732 struct btrfs_qgroup_status_item *ptr;
733 int ret;
734 int slot;
735
736 key.objectid = 0;
737 key.type = BTRFS_QGROUP_STATUS_KEY;
738 key.offset = 0;
739
740 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000741 if (!path)
742 return -ENOMEM;
743
Arne Jansenbed92ea2012-06-28 18:03:02 +0200744 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
745 if (ret > 0)
746 ret = -ENOENT;
747
748 if (ret)
749 goto out;
750
751 l = path->nodes[0];
752 slot = path->slots[0];
753 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
754 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
755 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000756 btrfs_set_qgroup_status_rescan(l, ptr,
757 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200758
759 btrfs_mark_buffer_dirty(l);
760
761out:
762 btrfs_free_path(path);
763 return ret;
764}
765
766/*
767 * called with qgroup_lock held
768 */
769static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
770 struct btrfs_root *root)
771{
772 struct btrfs_path *path;
773 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000774 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200775 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000776 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200777
Arne Jansenbed92ea2012-06-28 18:03:02 +0200778 path = btrfs_alloc_path();
779 if (!path)
780 return -ENOMEM;
781
Wang Shilong06b3a862013-02-27 11:16:57 +0000782 path->leave_spinning = 1;
783
784 key.objectid = 0;
785 key.offset = 0;
786 key.type = 0;
787
Arne Jansenbed92ea2012-06-28 18:03:02 +0200788 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200789 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000790 if (ret < 0)
791 goto out;
792 leaf = path->nodes[0];
793 nr = btrfs_header_nritems(leaf);
794 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200795 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000796 /*
797 * delete the leaf one by one
798 * since the whole tree is going
799 * to be deleted.
800 */
801 path->slots[0] = 0;
802 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200803 if (ret)
804 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000805
Arne Jansenbed92ea2012-06-28 18:03:02 +0200806 btrfs_release_path(path);
807 }
808 ret = 0;
809out:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200810 btrfs_free_path(path);
811 return ret;
812}
813
814int btrfs_quota_enable(struct btrfs_trans_handle *trans,
815 struct btrfs_fs_info *fs_info)
816{
817 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000818 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200819 struct btrfs_path *path = NULL;
820 struct btrfs_qgroup_status_item *ptr;
821 struct extent_buffer *leaf;
822 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000823 struct btrfs_key found_key;
824 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200825 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000826 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200827
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000828 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200829 if (fs_info->quota_root) {
Josef Bacikafcdd122016-09-02 15:40:02 -0400830 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200831 goto out;
832 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200833
David Sterba52bf8e72017-02-13 11:03:44 +0100834 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000835 if (!fs_info->qgroup_ulist) {
836 ret = -ENOMEM;
837 goto out;
838 }
839
Arne Jansenbed92ea2012-06-28 18:03:02 +0200840 /*
841 * initially create the quota tree
842 */
843 quota_root = btrfs_create_tree(trans, fs_info,
844 BTRFS_QUOTA_TREE_OBJECTID);
845 if (IS_ERR(quota_root)) {
846 ret = PTR_ERR(quota_root);
847 goto out;
848 }
849
850 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000851 if (!path) {
852 ret = -ENOMEM;
853 goto out_free_root;
854 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200855
856 key.objectid = 0;
857 key.type = BTRFS_QGROUP_STATUS_KEY;
858 key.offset = 0;
859
860 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
861 sizeof(*ptr));
862 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000863 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200864
865 leaf = path->nodes[0];
866 ptr = btrfs_item_ptr(leaf, path->slots[0],
867 struct btrfs_qgroup_status_item);
868 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
869 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
870 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
871 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
872 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000873 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200874
875 btrfs_mark_buffer_dirty(leaf);
876
Wang Shilong7708f022013-04-07 10:24:57 +0000877 key.objectid = 0;
878 key.type = BTRFS_ROOT_REF_KEY;
879 key.offset = 0;
880
881 btrfs_release_path(path);
882 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
883 if (ret > 0)
884 goto out_add_root;
885 if (ret < 0)
886 goto out_free_path;
887
888
889 while (1) {
890 slot = path->slots[0];
891 leaf = path->nodes[0];
892 btrfs_item_key_to_cpu(leaf, &found_key, slot);
893
894 if (found_key.type == BTRFS_ROOT_REF_KEY) {
895 ret = add_qgroup_item(trans, quota_root,
896 found_key.offset);
897 if (ret)
898 goto out_free_path;
899
Wang Shilong7708f022013-04-07 10:24:57 +0000900 qgroup = add_qgroup_rb(fs_info, found_key.offset);
901 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000902 ret = PTR_ERR(qgroup);
903 goto out_free_path;
904 }
Wang Shilong7708f022013-04-07 10:24:57 +0000905 }
906 ret = btrfs_next_item(tree_root, path);
907 if (ret < 0)
908 goto out_free_path;
909 if (ret)
910 break;
911 }
912
913out_add_root:
914 btrfs_release_path(path);
915 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
916 if (ret)
917 goto out_free_path;
918
Wang Shilong7708f022013-04-07 10:24:57 +0000919 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
920 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000921 ret = PTR_ERR(qgroup);
922 goto out_free_path;
923 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000924 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200925 fs_info->quota_root = quota_root;
Josef Bacikafcdd122016-09-02 15:40:02 -0400926 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200927 spin_unlock(&fs_info->qgroup_lock);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000928out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200929 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000930out_free_root:
931 if (ret) {
932 free_extent_buffer(quota_root->node);
933 free_extent_buffer(quota_root->commit_root);
934 kfree(quota_root);
935 }
936out:
Jan Schmidteb1716a2013-05-28 15:47:23 +0000937 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000938 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000939 fs_info->qgroup_ulist = NULL;
940 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000941 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200942 return ret;
943}
944
945int btrfs_quota_disable(struct btrfs_trans_handle *trans,
946 struct btrfs_fs_info *fs_info)
947{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200948 struct btrfs_root *quota_root;
949 int ret = 0;
950
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000951 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +0000952 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000953 goto out;
Josef Bacikafcdd122016-09-02 15:40:02 -0400954 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -0400955 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -0800956 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200957 quota_root = fs_info->quota_root;
958 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +0800959 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200960 spin_unlock(&fs_info->qgroup_lock);
961
Wang Shilonge685da12013-08-14 09:13:37 +0800962 btrfs_free_qgroup_config(fs_info);
963
Arne Jansenbed92ea2012-06-28 18:03:02 +0200964 ret = btrfs_clean_quota_tree(trans, quota_root);
965 if (ret)
966 goto out;
967
Jeff Mahoney1cd54472017-08-17 10:25:11 -0400968 ret = btrfs_del_root(trans, fs_info, &quota_root->root_key);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200969 if (ret)
970 goto out;
971
972 list_del(&quota_root->dirty_list);
973
974 btrfs_tree_lock(quota_root->node);
David Sterba7c302b42017-02-10 18:47:57 +0100975 clean_tree_block(fs_info, quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200976 btrfs_tree_unlock(quota_root->node);
977 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
978
979 free_extent_buffer(quota_root->node);
980 free_extent_buffer(quota_root->commit_root);
981 kfree(quota_root);
982out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000983 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200984 return ret;
985}
986
Jan Schmidt2f232032013-04-25 16:04:51 +0000987static void qgroup_dirty(struct btrfs_fs_info *fs_info,
988 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200989{
Jan Schmidt2f232032013-04-25 16:04:51 +0000990 if (list_empty(&qgroup->dirty))
991 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200992}
993
Qu Wenruo18dc22c2016-10-20 10:28:41 +0800994static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
995 struct btrfs_qgroup *qgroup,
996 u64 num_bytes)
997{
David Sterba338bd522017-04-18 17:00:12 +0200998#ifdef CONFIG_BTRFS_DEBUG
999 WARN_ON(qgroup->reserved < num_bytes);
1000 btrfs_debug(fs_info,
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001001 "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
1002 qgroup->qgroupid, qgroup->reserved, num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001003#endif
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001004 qgroup->reserved = 0;
1005}
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001006/*
1007 * The easy accounting, if we are adding/removing the only ref for an extent
Nicholas D Steeves01327612016-05-19 21:18:45 -04001008 * then this qgroup and all of the parent qgroups get their reference and
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001009 * exclusive counts adjusted.
1010 *
1011 * Caller should hold fs_info->qgroup_lock.
1012 */
1013static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1014 struct ulist *tmp, u64 ref_root,
1015 u64 num_bytes, int sign)
1016{
1017 struct btrfs_qgroup *qgroup;
1018 struct btrfs_qgroup_list *glist;
1019 struct ulist_node *unode;
1020 struct ulist_iterator uiter;
1021 int ret = 0;
1022
1023 qgroup = find_qgroup_rb(fs_info, ref_root);
1024 if (!qgroup)
1025 goto out;
1026
1027 qgroup->rfer += sign * num_bytes;
1028 qgroup->rfer_cmpr += sign * num_bytes;
1029
1030 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1031 qgroup->excl += sign * num_bytes;
1032 qgroup->excl_cmpr += sign * num_bytes;
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001033 if (sign > 0) {
Qu Wenruo3159fe72017-03-13 15:52:08 +08001034 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001035 if (qgroup->reserved < num_bytes)
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001036 report_reserved_underflow(fs_info, qgroup, num_bytes);
1037 else
1038 qgroup->reserved -= num_bytes;
1039 }
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001040
1041 qgroup_dirty(fs_info, qgroup);
1042
1043 /* Get all of the parent groups that contain this qgroup */
1044 list_for_each_entry(glist, &qgroup->groups, next_group) {
1045 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001046 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001047 if (ret < 0)
1048 goto out;
1049 }
1050
1051 /* Iterate all of the parents and adjust their reference counts */
1052 ULIST_ITER_INIT(&uiter);
1053 while ((unode = ulist_next(tmp, &uiter))) {
David Sterbaef2fff62016-10-26 16:23:50 +02001054 qgroup = unode_aux_to_qgroup(unode);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001055 qgroup->rfer += sign * num_bytes;
1056 qgroup->rfer_cmpr += sign * num_bytes;
1057 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1058 qgroup->excl += sign * num_bytes;
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001059 if (sign > 0) {
Qu Wenruo3159fe72017-03-13 15:52:08 +08001060 trace_qgroup_update_reserve(fs_info, qgroup,
1061 -(s64)num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001062 if (qgroup->reserved < num_bytes)
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001063 report_reserved_underflow(fs_info, qgroup,
1064 num_bytes);
1065 else
1066 qgroup->reserved -= num_bytes;
1067 }
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001068 qgroup->excl_cmpr += sign * num_bytes;
1069 qgroup_dirty(fs_info, qgroup);
1070
1071 /* Add any parents of the parents */
1072 list_for_each_entry(glist, &qgroup->groups, next_group) {
1073 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001074 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001075 if (ret < 0)
1076 goto out;
1077 }
1078 }
1079 ret = 0;
1080out:
1081 return ret;
1082}
1083
1084
1085/*
1086 * Quick path for updating qgroup with only excl refs.
1087 *
1088 * In that case, just update all parent will be enough.
1089 * Or we needs to do a full rescan.
1090 * Caller should also hold fs_info->qgroup_lock.
1091 *
1092 * Return 0 for quick update, return >0 for need to full rescan
1093 * and mark INCONSISTENT flag.
1094 * Return < 0 for other error.
1095 */
1096static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1097 struct ulist *tmp, u64 src, u64 dst,
1098 int sign)
1099{
1100 struct btrfs_qgroup *qgroup;
1101 int ret = 1;
1102 int err = 0;
1103
1104 qgroup = find_qgroup_rb(fs_info, src);
1105 if (!qgroup)
1106 goto out;
1107 if (qgroup->excl == qgroup->rfer) {
1108 ret = 0;
1109 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1110 qgroup->excl, sign);
1111 if (err < 0) {
1112 ret = err;
1113 goto out;
1114 }
1115 }
1116out:
1117 if (ret)
1118 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1119 return ret;
1120}
1121
Arne Jansenbed92ea2012-06-28 18:03:02 +02001122int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1123 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1124{
1125 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001126 struct btrfs_qgroup *parent;
1127 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001128 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001129 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001130 int ret = 0;
1131
Qu Wenruo8465ece2015-02-27 16:24:22 +08001132 /* Check the level of src and dst first */
1133 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1134 return -EINVAL;
1135
David Sterba6602caf2017-02-13 12:41:02 +01001136 tmp = ulist_alloc(GFP_KERNEL);
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001137 if (!tmp)
1138 return -ENOMEM;
1139
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001140 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001141 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001142 if (!quota_root) {
1143 ret = -EINVAL;
1144 goto out;
1145 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001146 member = find_qgroup_rb(fs_info, src);
1147 parent = find_qgroup_rb(fs_info, dst);
1148 if (!member || !parent) {
1149 ret = -EINVAL;
1150 goto out;
1151 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001152
Wang Shilong534e6622013-04-17 14:49:51 +00001153 /* check if such qgroup relation exist firstly */
1154 list_for_each_entry(list, &member->groups, next_group) {
1155 if (list->group == parent) {
1156 ret = -EEXIST;
1157 goto out;
1158 }
1159 }
1160
Arne Jansenbed92ea2012-06-28 18:03:02 +02001161 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1162 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001163 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001164
1165 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1166 if (ret) {
1167 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001168 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001169 }
1170
1171 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001172 ret = add_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001173 if (ret < 0) {
1174 spin_unlock(&fs_info->qgroup_lock);
1175 goto out;
1176 }
1177 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001178 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001179out:
1180 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001181 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001182 return ret;
1183}
1184
David Sterba025db912017-02-13 13:00:51 +01001185static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001186 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1187{
1188 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001189 struct btrfs_qgroup *parent;
1190 struct btrfs_qgroup *member;
1191 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001192 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001193 int ret = 0;
1194 int err;
1195
David Sterba6602caf2017-02-13 12:41:02 +01001196 tmp = ulist_alloc(GFP_KERNEL);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001197 if (!tmp)
1198 return -ENOMEM;
1199
Arne Jansenbed92ea2012-06-28 18:03:02 +02001200 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001201 if (!quota_root) {
1202 ret = -EINVAL;
1203 goto out;
1204 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001205
Wang Shilong534e6622013-04-17 14:49:51 +00001206 member = find_qgroup_rb(fs_info, src);
1207 parent = find_qgroup_rb(fs_info, dst);
1208 if (!member || !parent) {
1209 ret = -EINVAL;
1210 goto out;
1211 }
1212
1213 /* check if such qgroup relation exist firstly */
1214 list_for_each_entry(list, &member->groups, next_group) {
1215 if (list->group == parent)
1216 goto exist;
1217 }
1218 ret = -ENOENT;
1219 goto out;
1220exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001221 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1222 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1223 if (err && !ret)
1224 ret = err;
1225
1226 spin_lock(&fs_info->qgroup_lock);
1227 del_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001228 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001229 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001230out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001231 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001232 return ret;
1233}
1234
1235int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1236 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1237{
1238 int ret = 0;
1239
1240 mutex_lock(&fs_info->qgroup_ioctl_lock);
1241 ret = __del_qgroup_relation(trans, fs_info, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001242 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001243
Arne Jansenbed92ea2012-06-28 18:03:02 +02001244 return ret;
1245}
1246
1247int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -05001248 struct btrfs_fs_info *fs_info, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001249{
1250 struct btrfs_root *quota_root;
1251 struct btrfs_qgroup *qgroup;
1252 int ret = 0;
1253
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001254 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001255 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001256 if (!quota_root) {
1257 ret = -EINVAL;
1258 goto out;
1259 }
Wang Shilong534e6622013-04-17 14:49:51 +00001260 qgroup = find_qgroup_rb(fs_info, qgroupid);
1261 if (qgroup) {
1262 ret = -EEXIST;
1263 goto out;
1264 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001265
1266 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001267 if (ret)
1268 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001269
1270 spin_lock(&fs_info->qgroup_lock);
1271 qgroup = add_qgroup_rb(fs_info, qgroupid);
1272 spin_unlock(&fs_info->qgroup_lock);
1273
1274 if (IS_ERR(qgroup))
1275 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001276out:
1277 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001278 return ret;
1279}
1280
1281int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1282 struct btrfs_fs_info *fs_info, u64 qgroupid)
1283{
1284 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001285 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001286 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001287 int ret = 0;
1288
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001289 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001290 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001291 if (!quota_root) {
1292 ret = -EINVAL;
1293 goto out;
1294 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001295
Arne Jansen2cf68702013-01-17 01:22:09 -07001296 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001297 if (!qgroup) {
1298 ret = -ENOENT;
1299 goto out;
1300 } else {
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001301 /* check if there are no children of this qgroup */
1302 if (!list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001303 ret = -EBUSY;
1304 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001305 }
1306 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001307 ret = del_qgroup_item(trans, quota_root, qgroupid);
Sargun Dhillon36b96fd2017-09-17 09:02:29 +00001308 if (ret && ret != -ENOENT)
1309 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001310
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001311 while (!list_empty(&qgroup->groups)) {
1312 list = list_first_entry(&qgroup->groups,
1313 struct btrfs_qgroup_list, next_group);
1314 ret = __del_qgroup_relation(trans, fs_info,
1315 qgroupid,
1316 list->group->qgroupid);
1317 if (ret)
1318 goto out;
1319 }
1320
Arne Jansenbed92ea2012-06-28 18:03:02 +02001321 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001322 del_qgroup_rb(fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001323 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001324out:
1325 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001326 return ret;
1327}
1328
1329int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1330 struct btrfs_fs_info *fs_info, u64 qgroupid,
1331 struct btrfs_qgroup_limit *limit)
1332{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001333 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001334 struct btrfs_qgroup *qgroup;
1335 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001336 /* Sometimes we would want to clear the limit on this qgroup.
1337 * To meet this requirement, we treat the -1 as a special value
1338 * which tell kernel to clear the limit on this qgroup.
1339 */
1340 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001341
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001342 mutex_lock(&fs_info->qgroup_ioctl_lock);
1343 quota_root = fs_info->quota_root;
1344 if (!quota_root) {
1345 ret = -EINVAL;
1346 goto out;
1347 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001348
Wang Shilongddb47af2013-04-07 10:50:20 +00001349 qgroup = find_qgroup_rb(fs_info, qgroupid);
1350 if (!qgroup) {
1351 ret = -ENOENT;
1352 goto out;
1353 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001354
Wang Shilong58400fc2013-04-07 10:50:17 +00001355 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001356 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1357 if (limit->max_rfer == CLEAR_VALUE) {
1358 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1359 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1360 qgroup->max_rfer = 0;
1361 } else {
1362 qgroup->max_rfer = limit->max_rfer;
1363 }
1364 }
1365 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1366 if (limit->max_excl == CLEAR_VALUE) {
1367 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1368 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1369 qgroup->max_excl = 0;
1370 } else {
1371 qgroup->max_excl = limit->max_excl;
1372 }
1373 }
1374 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1375 if (limit->rsv_rfer == CLEAR_VALUE) {
1376 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1377 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1378 qgroup->rsv_rfer = 0;
1379 } else {
1380 qgroup->rsv_rfer = limit->rsv_rfer;
1381 }
1382 }
1383 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1384 if (limit->rsv_excl == CLEAR_VALUE) {
1385 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1386 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1387 qgroup->rsv_excl = 0;
1388 } else {
1389 qgroup->rsv_excl = limit->rsv_excl;
1390 }
1391 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001392 qgroup->lim_flags |= limit->flags;
1393
Arne Jansenbed92ea2012-06-28 18:03:02 +02001394 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001395
1396 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1397 if (ret) {
1398 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1399 btrfs_info(fs_info, "unable to update quota limit for %llu",
1400 qgroupid);
1401 }
1402
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001403out:
1404 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001405 return ret;
1406}
Mark Fasheh11526512014-07-17 12:39:01 -07001407
Qu Wenruo50b3e042016-10-18 09:31:27 +08001408int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +08001409 struct btrfs_delayed_ref_root *delayed_refs,
1410 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001411{
1412 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1413 struct rb_node *parent_node = NULL;
1414 struct btrfs_qgroup_extent_record *entry;
1415 u64 bytenr = record->bytenr;
1416
Mark Fasheh82bd1012015-11-05 14:38:00 -08001417 assert_spin_locked(&delayed_refs->lock);
Qu Wenruo50b3e042016-10-18 09:31:27 +08001418 trace_btrfs_qgroup_trace_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001419
Qu Wenruo3368d002015-04-16 14:34:17 +08001420 while (*p) {
1421 parent_node = *p;
1422 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1423 node);
1424 if (bytenr < entry->bytenr)
1425 p = &(*p)->rb_left;
1426 else if (bytenr > entry->bytenr)
1427 p = &(*p)->rb_right;
1428 else
Qu Wenruocb93b522016-08-15 10:36:50 +08001429 return 1;
Qu Wenruo3368d002015-04-16 14:34:17 +08001430 }
1431
1432 rb_link_node(&record->node, parent_node, p);
1433 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001434 return 0;
1435}
1436
Qu Wenruofb235dc2017-02-15 10:43:03 +08001437int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1438 struct btrfs_qgroup_extent_record *qrecord)
1439{
1440 struct ulist *old_root;
1441 u64 bytenr = qrecord->bytenr;
1442 int ret;
1443
1444 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root);
1445 if (ret < 0)
1446 return ret;
1447
1448 /*
1449 * Here we don't need to get the lock of
1450 * trans->transaction->delayed_refs, since inserted qrecord won't
1451 * be deleted, only qrecord->node may be modified (new qrecord insert)
1452 *
1453 * So modifying qrecord->old_roots is safe here
1454 */
1455 qrecord->old_roots = old_root;
1456 return 0;
1457}
1458
Qu Wenruo50b3e042016-10-18 09:31:27 +08001459int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
Qu Wenruocb93b522016-08-15 10:36:50 +08001460 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1461 gfp_t gfp_flag)
1462{
1463 struct btrfs_qgroup_extent_record *record;
1464 struct btrfs_delayed_ref_root *delayed_refs;
1465 int ret;
1466
Josef Bacikafcdd122016-09-02 15:40:02 -04001467 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1468 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001469 return 0;
1470 if (WARN_ON(trans == NULL))
1471 return -EINVAL;
1472 record = kmalloc(sizeof(*record), gfp_flag);
1473 if (!record)
1474 return -ENOMEM;
1475
1476 delayed_refs = &trans->transaction->delayed_refs;
1477 record->bytenr = bytenr;
1478 record->num_bytes = num_bytes;
1479 record->old_roots = NULL;
1480
1481 spin_lock(&delayed_refs->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001482 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
Qu Wenruocb93b522016-08-15 10:36:50 +08001483 spin_unlock(&delayed_refs->lock);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001484 if (ret > 0) {
Qu Wenruocb93b522016-08-15 10:36:50 +08001485 kfree(record);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001486 return 0;
1487 }
1488 return btrfs_qgroup_trace_extent_post(fs_info, record);
Qu Wenruo3368d002015-04-16 14:34:17 +08001489}
1490
Qu Wenruo33d1f052016-10-18 09:31:28 +08001491int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001492 struct btrfs_fs_info *fs_info,
Qu Wenruo33d1f052016-10-18 09:31:28 +08001493 struct extent_buffer *eb)
1494{
1495 int nr = btrfs_header_nritems(eb);
1496 int i, extent_type, ret;
1497 struct btrfs_key key;
1498 struct btrfs_file_extent_item *fi;
1499 u64 bytenr, num_bytes;
1500
1501 /* We can be called directly from walk_up_proc() */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001503 return 0;
1504
1505 for (i = 0; i < nr; i++) {
1506 btrfs_item_key_to_cpu(eb, &key, i);
1507
1508 if (key.type != BTRFS_EXTENT_DATA_KEY)
1509 continue;
1510
1511 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1512 /* filter out non qgroup-accountable extents */
1513 extent_type = btrfs_file_extent_type(eb, fi);
1514
1515 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1516 continue;
1517
1518 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1519 if (!bytenr)
1520 continue;
1521
1522 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1523
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001524 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1525 num_bytes, GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001526 if (ret)
1527 return ret;
1528 }
Jeff Mahoneycddf3b22017-06-20 08:15:26 -04001529 cond_resched();
Qu Wenruo33d1f052016-10-18 09:31:28 +08001530 return 0;
1531}
1532
1533/*
1534 * Walk up the tree from the bottom, freeing leaves and any interior
1535 * nodes which have had all slots visited. If a node (leaf or
1536 * interior) is freed, the node above it will have it's slot
1537 * incremented. The root node will never be freed.
1538 *
1539 * At the end of this function, we should have a path which has all
1540 * slots incremented to the next position for a search. If we need to
1541 * read a new node it will be NULL and the node above it will have the
1542 * correct slot selected for a later read.
1543 *
1544 * If we increment the root nodes slot counter past the number of
1545 * elements, 1 is returned to signal completion of the search.
1546 */
David Sterba15b34512017-02-10 20:30:23 +01001547static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
Qu Wenruo33d1f052016-10-18 09:31:28 +08001548{
1549 int level = 0;
1550 int nr, slot;
1551 struct extent_buffer *eb;
1552
1553 if (root_level == 0)
1554 return 1;
1555
1556 while (level <= root_level) {
1557 eb = path->nodes[level];
1558 nr = btrfs_header_nritems(eb);
1559 path->slots[level]++;
1560 slot = path->slots[level];
1561 if (slot >= nr || level == 0) {
1562 /*
1563 * Don't free the root - we will detect this
1564 * condition after our loop and return a
1565 * positive value for caller to stop walking the tree.
1566 */
1567 if (level != root_level) {
1568 btrfs_tree_unlock_rw(eb, path->locks[level]);
1569 path->locks[level] = 0;
1570
1571 free_extent_buffer(eb);
1572 path->nodes[level] = NULL;
1573 path->slots[level] = 0;
1574 }
1575 } else {
1576 /*
1577 * We have a valid slot to walk back down
1578 * from. Stop here so caller can process these
1579 * new nodes.
1580 */
1581 break;
1582 }
1583
1584 level++;
1585 }
1586
1587 eb = path->nodes[root_level];
1588 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1589 return 1;
1590
1591 return 0;
1592}
1593
1594int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1595 struct btrfs_root *root,
1596 struct extent_buffer *root_eb,
1597 u64 root_gen, int root_level)
1598{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001599 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001600 int ret = 0;
1601 int level;
1602 struct extent_buffer *eb = root_eb;
1603 struct btrfs_path *path = NULL;
1604
Nikolay Borisovb6e6bca2017-07-12 09:42:19 +03001605 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001606 BUG_ON(root_eb == NULL);
1607
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001609 return 0;
1610
1611 if (!extent_buffer_uptodate(root_eb)) {
1612 ret = btrfs_read_buffer(root_eb, root_gen);
1613 if (ret)
1614 goto out;
1615 }
1616
1617 if (root_level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001618 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001619 goto out;
1620 }
1621
1622 path = btrfs_alloc_path();
1623 if (!path)
1624 return -ENOMEM;
1625
1626 /*
1627 * Walk down the tree. Missing extent blocks are filled in as
1628 * we go. Metadata is accounted every time we read a new
1629 * extent block.
1630 *
1631 * When we reach a leaf, we account for file extent items in it,
1632 * walk back up the tree (adjusting slot pointers as we go)
1633 * and restart the search process.
1634 */
1635 extent_buffer_get(root_eb); /* For path */
1636 path->nodes[root_level] = root_eb;
1637 path->slots[root_level] = 0;
1638 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1639walk_down:
1640 level = root_level;
1641 while (level >= 0) {
1642 if (path->nodes[level] == NULL) {
1643 int parent_slot;
1644 u64 child_gen;
1645 u64 child_bytenr;
1646
1647 /*
1648 * We need to get child blockptr/gen from parent before
1649 * we can read it.
1650 */
1651 eb = path->nodes[level + 1];
1652 parent_slot = path->slots[level + 1];
1653 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1654 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1655
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001656 eb = read_tree_block(fs_info, child_bytenr, child_gen);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001657 if (IS_ERR(eb)) {
1658 ret = PTR_ERR(eb);
1659 goto out;
1660 } else if (!extent_buffer_uptodate(eb)) {
1661 free_extent_buffer(eb);
1662 ret = -EIO;
1663 goto out;
1664 }
1665
1666 path->nodes[level] = eb;
1667 path->slots[level] = 0;
1668
1669 btrfs_tree_read_lock(eb);
1670 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1671 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1672
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001673 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1674 child_bytenr,
1675 fs_info->nodesize,
1676 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001677 if (ret)
1678 goto out;
1679 }
1680
1681 if (level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001682 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1683 path->nodes[level]);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001684 if (ret)
1685 goto out;
1686
1687 /* Nonzero return here means we completed our search */
David Sterba15b34512017-02-10 20:30:23 +01001688 ret = adjust_slots_upwards(path, root_level);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001689 if (ret)
1690 break;
1691
1692 /* Restart search with new slots */
1693 goto walk_down;
1694 }
1695
1696 level--;
1697 }
1698
1699 ret = 0;
1700out:
1701 btrfs_free_path(path);
1702
1703 return ret;
1704}
1705
Qu Wenruod810ef22015-04-12 16:52:34 +08001706#define UPDATE_NEW 0
1707#define UPDATE_OLD 1
1708/*
1709 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1710 */
1711static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1712 struct ulist *roots, struct ulist *tmp,
1713 struct ulist *qgroups, u64 seq, int update_old)
1714{
1715 struct ulist_node *unode;
1716 struct ulist_iterator uiter;
1717 struct ulist_node *tmp_unode;
1718 struct ulist_iterator tmp_uiter;
1719 struct btrfs_qgroup *qg;
1720 int ret = 0;
1721
1722 if (!roots)
1723 return 0;
1724 ULIST_ITER_INIT(&uiter);
1725 while ((unode = ulist_next(roots, &uiter))) {
1726 qg = find_qgroup_rb(fs_info, unode->val);
1727 if (!qg)
1728 continue;
1729
1730 ulist_reinit(tmp);
David Sterbaef2fff62016-10-26 16:23:50 +02001731 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
Qu Wenruod810ef22015-04-12 16:52:34 +08001732 GFP_ATOMIC);
1733 if (ret < 0)
1734 return ret;
David Sterbaef2fff62016-10-26 16:23:50 +02001735 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
Qu Wenruod810ef22015-04-12 16:52:34 +08001736 if (ret < 0)
1737 return ret;
1738 ULIST_ITER_INIT(&tmp_uiter);
1739 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1740 struct btrfs_qgroup_list *glist;
1741
David Sterbaef2fff62016-10-26 16:23:50 +02001742 qg = unode_aux_to_qgroup(tmp_unode);
Qu Wenruod810ef22015-04-12 16:52:34 +08001743 if (update_old)
1744 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1745 else
1746 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1747 list_for_each_entry(glist, &qg->groups, next_group) {
1748 ret = ulist_add(qgroups, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001749 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001750 GFP_ATOMIC);
1751 if (ret < 0)
1752 return ret;
1753 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001754 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001755 GFP_ATOMIC);
1756 if (ret < 0)
1757 return ret;
1758 }
1759 }
1760 }
1761 return 0;
1762}
1763
Josef Bacikfcebe452014-05-13 17:30:47 -07001764/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001765 * Update qgroup rfer/excl counters.
1766 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08001767 *
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001768 * Excl update is tricky, the update is split into 2 part.
1769 * Part 1: Possible exclusive <-> sharing detect:
1770 * | A | !A |
1771 * -------------------------------------
1772 * B | * | - |
1773 * -------------------------------------
1774 * !B | + | ** |
1775 * -------------------------------------
1776 *
1777 * Conditions:
1778 * A: cur_old_roots < nr_old_roots (not exclusive before)
1779 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1780 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04001781 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001782 *
1783 * Results:
1784 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1785 * *: Definitely not changed. **: Possible unchanged.
1786 *
1787 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1788 *
1789 * To make the logic clear, we first use condition A and B to split
1790 * combination into 4 results.
1791 *
1792 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1793 * only on variant maybe 0.
1794 *
1795 * Lastly, check result **, since there are 2 variants maybe 0, split them
1796 * again(2x2).
1797 * But this time we don't need to consider other things, the codes and logic
1798 * is easy to understand now.
1799 */
1800static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1801 struct ulist *qgroups,
1802 u64 nr_old_roots,
1803 u64 nr_new_roots,
1804 u64 num_bytes, u64 seq)
1805{
1806 struct ulist_node *unode;
1807 struct ulist_iterator uiter;
1808 struct btrfs_qgroup *qg;
1809 u64 cur_new_count, cur_old_count;
1810
1811 ULIST_ITER_INIT(&uiter);
1812 while ((unode = ulist_next(qgroups, &uiter))) {
1813 bool dirty = false;
1814
David Sterbaef2fff62016-10-26 16:23:50 +02001815 qg = unode_aux_to_qgroup(unode);
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001816 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1817 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1818
Jeff Mahoneybc074522016-06-09 17:27:55 -04001819 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1820 cur_old_count, cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001821
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001822 /* Rfer update part */
1823 if (cur_old_count == 0 && cur_new_count > 0) {
1824 qg->rfer += num_bytes;
1825 qg->rfer_cmpr += num_bytes;
1826 dirty = true;
1827 }
1828 if (cur_old_count > 0 && cur_new_count == 0) {
1829 qg->rfer -= num_bytes;
1830 qg->rfer_cmpr -= num_bytes;
1831 dirty = true;
1832 }
1833
1834 /* Excl update part */
1835 /* Exclusive/none -> shared case */
1836 if (cur_old_count == nr_old_roots &&
1837 cur_new_count < nr_new_roots) {
1838 /* Exclusive -> shared */
1839 if (cur_old_count != 0) {
1840 qg->excl -= num_bytes;
1841 qg->excl_cmpr -= num_bytes;
1842 dirty = true;
1843 }
1844 }
1845
1846 /* Shared -> exclusive/none case */
1847 if (cur_old_count < nr_old_roots &&
1848 cur_new_count == nr_new_roots) {
1849 /* Shared->exclusive */
1850 if (cur_new_count != 0) {
1851 qg->excl += num_bytes;
1852 qg->excl_cmpr += num_bytes;
1853 dirty = true;
1854 }
1855 }
1856
1857 /* Exclusive/none -> exclusive/none case */
1858 if (cur_old_count == nr_old_roots &&
1859 cur_new_count == nr_new_roots) {
1860 if (cur_old_count == 0) {
1861 /* None -> exclusive/none */
1862
1863 if (cur_new_count != 0) {
1864 /* None -> exclusive */
1865 qg->excl += num_bytes;
1866 qg->excl_cmpr += num_bytes;
1867 dirty = true;
1868 }
1869 /* None -> none, nothing changed */
1870 } else {
1871 /* Exclusive -> exclusive/none */
1872
1873 if (cur_new_count == 0) {
1874 /* Exclusive -> none */
1875 qg->excl -= num_bytes;
1876 qg->excl_cmpr -= num_bytes;
1877 dirty = true;
1878 }
1879 /* Exclusive -> exclusive, nothing changed */
1880 }
1881 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08001882
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001883 if (dirty)
1884 qgroup_dirty(fs_info, qg);
1885 }
1886 return 0;
1887}
1888
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001889/*
1890 * Check if the @roots potentially is a list of fs tree roots
1891 *
1892 * Return 0 for definitely not a fs/subvol tree roots ulist
1893 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1894 * one as well)
1895 */
1896static int maybe_fs_roots(struct ulist *roots)
1897{
1898 struct ulist_node *unode;
1899 struct ulist_iterator uiter;
1900
1901 /* Empty one, still possible for fs roots */
1902 if (!roots || roots->nnodes == 0)
1903 return 1;
1904
1905 ULIST_ITER_INIT(&uiter);
1906 unode = ulist_next(roots, &uiter);
1907 if (!unode)
1908 return 1;
1909
1910 /*
1911 * If it contains fs tree roots, then it must belong to fs/subvol
1912 * trees.
1913 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
1914 */
1915 return is_fstree(unode->val);
1916}
1917
Qu Wenruo442244c2015-04-16 17:18:36 +08001918int
Qu Wenruo550d7a22015-04-16 15:37:33 +08001919btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1920 struct btrfs_fs_info *fs_info,
1921 u64 bytenr, u64 num_bytes,
1922 struct ulist *old_roots, struct ulist *new_roots)
1923{
1924 struct ulist *qgroups = NULL;
1925 struct ulist *tmp = NULL;
1926 u64 seq;
1927 u64 nr_new_roots = 0;
1928 u64 nr_old_roots = 0;
1929 int ret = 0;
1930
David Sterba81353d52017-02-13 14:05:24 +01001931 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1932 return 0;
1933
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001934 if (new_roots) {
1935 if (!maybe_fs_roots(new_roots))
1936 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08001937 nr_new_roots = new_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001938 }
1939 if (old_roots) {
1940 if (!maybe_fs_roots(old_roots))
1941 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08001942 nr_old_roots = old_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001943 }
1944
1945 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
1946 if (nr_old_roots == 0 && nr_new_roots == 0)
1947 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08001948
Qu Wenruo550d7a22015-04-16 15:37:33 +08001949 BUG_ON(!fs_info->quota_root);
1950
Jeff Mahoneybc074522016-06-09 17:27:55 -04001951 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
1952 nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001953
Qu Wenruo550d7a22015-04-16 15:37:33 +08001954 qgroups = ulist_alloc(GFP_NOFS);
1955 if (!qgroups) {
1956 ret = -ENOMEM;
1957 goto out_free;
1958 }
1959 tmp = ulist_alloc(GFP_NOFS);
1960 if (!tmp) {
1961 ret = -ENOMEM;
1962 goto out_free;
1963 }
1964
1965 mutex_lock(&fs_info->qgroup_rescan_lock);
1966 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1967 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
1968 mutex_unlock(&fs_info->qgroup_rescan_lock);
1969 ret = 0;
1970 goto out_free;
1971 }
1972 }
1973 mutex_unlock(&fs_info->qgroup_rescan_lock);
1974
1975 spin_lock(&fs_info->qgroup_lock);
1976 seq = fs_info->qgroup_seq;
1977
1978 /* Update old refcnts using old_roots */
1979 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
1980 UPDATE_OLD);
1981 if (ret < 0)
1982 goto out;
1983
1984 /* Update new refcnts using new_roots */
1985 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
1986 UPDATE_NEW);
1987 if (ret < 0)
1988 goto out;
1989
1990 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
1991 num_bytes, seq);
1992
1993 /*
1994 * Bump qgroup_seq to avoid seq overlap
1995 */
1996 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
1997out:
1998 spin_unlock(&fs_info->qgroup_lock);
1999out_free:
2000 ulist_free(tmp);
2001 ulist_free(qgroups);
2002 ulist_free(old_roots);
2003 ulist_free(new_roots);
2004 return ret;
2005}
2006
2007int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
2008 struct btrfs_fs_info *fs_info)
2009{
2010 struct btrfs_qgroup_extent_record *record;
2011 struct btrfs_delayed_ref_root *delayed_refs;
2012 struct ulist *new_roots = NULL;
2013 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08002014 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002015 int ret = 0;
2016
2017 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08002018 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002019 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2020 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2021 node);
2022
Jeff Mahoneybc074522016-06-09 17:27:55 -04002023 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002024
Qu Wenruo550d7a22015-04-16 15:37:33 +08002025 if (!ret) {
2026 /*
Qu Wenruod1b8b942017-02-27 15:10:35 +08002027 * Old roots should be searched when inserting qgroup
2028 * extent record
2029 */
2030 if (WARN_ON(!record->old_roots)) {
2031 /* Search commit root to find old_roots */
2032 ret = btrfs_find_all_roots(NULL, fs_info,
2033 record->bytenr, 0,
2034 &record->old_roots);
2035 if (ret < 0)
2036 goto cleanup;
2037 }
2038
2039 /*
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002040 * Use SEQ_LAST as time_seq to do special search, which
Qu Wenruo550d7a22015-04-16 15:37:33 +08002041 * doesn't lock tree or delayed_refs and search current
2042 * root. It's safe inside commit_transaction().
2043 */
2044 ret = btrfs_find_all_roots(trans, fs_info,
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002045 record->bytenr, SEQ_LAST, &new_roots);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002046 if (ret < 0)
2047 goto cleanup;
Qu Wenruod1b8b942017-02-27 15:10:35 +08002048 if (qgroup_to_skip) {
Qu Wenruo9086db82015-04-20 09:53:50 +08002049 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002050 ulist_del(record->old_roots, qgroup_to_skip,
2051 0);
2052 }
Qu Wenruo550d7a22015-04-16 15:37:33 +08002053 ret = btrfs_qgroup_account_extent(trans, fs_info,
2054 record->bytenr, record->num_bytes,
2055 record->old_roots, new_roots);
2056 record->old_roots = NULL;
2057 new_roots = NULL;
2058 }
2059cleanup:
2060 ulist_free(record->old_roots);
2061 ulist_free(new_roots);
2062 new_roots = NULL;
2063 rb_erase(node, &delayed_refs->dirty_extent_root);
2064 kfree(record);
2065
2066 }
2067 return ret;
2068}
2069
Josef Bacikfcebe452014-05-13 17:30:47 -07002070/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002071 * called from commit_transaction. Writes all changed qgroups to disk.
2072 */
2073int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2074 struct btrfs_fs_info *fs_info)
2075{
2076 struct btrfs_root *quota_root = fs_info->quota_root;
2077 int ret = 0;
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002078 int start_rescan_worker = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002079
2080 if (!quota_root)
2081 goto out;
2082
Josef Bacikafcdd122016-09-02 15:40:02 -04002083 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
2084 test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002085 start_rescan_worker = 1;
2086
Josef Bacikafcdd122016-09-02 15:40:02 -04002087 if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
2088 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002089
2090 spin_lock(&fs_info->qgroup_lock);
2091 while (!list_empty(&fs_info->dirty_qgroups)) {
2092 struct btrfs_qgroup *qgroup;
2093 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2094 struct btrfs_qgroup, dirty);
2095 list_del_init(&qgroup->dirty);
2096 spin_unlock(&fs_info->qgroup_lock);
2097 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2098 if (ret)
2099 fs_info->qgroup_flags |=
2100 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05002101 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2102 if (ret)
2103 fs_info->qgroup_flags |=
2104 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002105 spin_lock(&fs_info->qgroup_lock);
2106 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002107 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02002108 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2109 else
2110 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2111 spin_unlock(&fs_info->qgroup_lock);
2112
2113 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2114 if (ret)
2115 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2116
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002117 if (!ret && start_rescan_worker) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002118 ret = qgroup_rescan_init(fs_info, 0, 1);
2119 if (!ret) {
2120 qgroup_rescan_zero_tracking(fs_info);
Qu Wenruofc97fab2014-02-28 10:46:16 +08002121 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2122 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002123 }
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002124 ret = 0;
2125 }
2126
Arne Jansenbed92ea2012-06-28 18:03:02 +02002127out:
2128
2129 return ret;
2130}
2131
2132/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002133 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002134 * when a snapshot or a subvolume is created. Throwing an error will
2135 * cause a transaction abort so we take extra care here to only error
2136 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02002137 */
2138int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2139 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2140 struct btrfs_qgroup_inherit *inherit)
2141{
2142 int ret = 0;
2143 int i;
2144 u64 *i_qgroups;
2145 struct btrfs_root *quota_root = fs_info->quota_root;
2146 struct btrfs_qgroup *srcgroup;
2147 struct btrfs_qgroup *dstgroup;
2148 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002149 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002150
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002151 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04002152 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002153 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002154
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002155 if (!quota_root) {
2156 ret = -EINVAL;
2157 goto out;
2158 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002159
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002160 if (inherit) {
2161 i_qgroups = (u64 *)(inherit + 1);
2162 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2163 2 * inherit->num_excl_copies;
2164 for (i = 0; i < nums; ++i) {
2165 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05002166
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002167 /*
2168 * Zero out invalid groups so we can ignore
2169 * them later.
2170 */
2171 if (!srcgroup ||
2172 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2173 *i_qgroups = 0ULL;
2174
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002175 ++i_qgroups;
2176 }
2177 }
2178
Arne Jansenbed92ea2012-06-28 18:03:02 +02002179 /*
2180 * create a tracking group for the subvol itself
2181 */
2182 ret = add_qgroup_item(trans, quota_root, objectid);
2183 if (ret)
2184 goto out;
2185
Arne Jansenbed92ea2012-06-28 18:03:02 +02002186 if (srcid) {
2187 struct btrfs_root *srcroot;
2188 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002189
2190 srckey.objectid = srcid;
2191 srckey.type = BTRFS_ROOT_ITEM_KEY;
2192 srckey.offset = (u64)-1;
2193 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2194 if (IS_ERR(srcroot)) {
2195 ret = PTR_ERR(srcroot);
2196 goto out;
2197 }
2198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002199 level_size = fs_info->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002200 }
2201
2202 /*
2203 * add qgroup to all inherited groups
2204 */
2205 if (inherit) {
2206 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002207 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2208 if (*i_qgroups == 0)
2209 continue;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002210 ret = add_qgroup_relation_item(trans, quota_root,
2211 objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002212 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002213 goto out;
2214 ret = add_qgroup_relation_item(trans, quota_root,
2215 *i_qgroups, objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002216 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002217 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002218 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002219 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002220 }
2221
2222
2223 spin_lock(&fs_info->qgroup_lock);
2224
2225 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002226 if (IS_ERR(dstgroup)) {
2227 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002228 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002229 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002230
Dongsheng Yange8c85412014-11-20 20:58:34 -05002231 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05002232 dstgroup->lim_flags = inherit->lim.flags;
2233 dstgroup->max_rfer = inherit->lim.max_rfer;
2234 dstgroup->max_excl = inherit->lim.max_excl;
2235 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2236 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05002237
2238 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2239 if (ret) {
2240 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002241 btrfs_info(fs_info,
2242 "unable to update quota limit for %llu",
2243 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002244 goto unlock;
2245 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002246 }
2247
Arne Jansenbed92ea2012-06-28 18:03:02 +02002248 if (srcid) {
2249 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002250 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002251 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002252
2253 /*
2254 * We call inherit after we clone the root in order to make sure
2255 * our counts don't go crazy, so at this point the only
2256 * difference between the two roots should be the root node.
2257 */
2258 dstgroup->rfer = srcgroup->rfer;
2259 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2260 dstgroup->excl = level_size;
2261 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002262 srcgroup->excl = level_size;
2263 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002264
2265 /* inherit the limit info */
2266 dstgroup->lim_flags = srcgroup->lim_flags;
2267 dstgroup->max_rfer = srcgroup->max_rfer;
2268 dstgroup->max_excl = srcgroup->max_excl;
2269 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2270 dstgroup->rsv_excl = srcgroup->rsv_excl;
2271
Arne Jansenbed92ea2012-06-28 18:03:02 +02002272 qgroup_dirty(fs_info, dstgroup);
2273 qgroup_dirty(fs_info, srcgroup);
2274 }
2275
Chris Masonf3a87f12012-09-14 20:06:30 -04002276 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002277 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002278
2279 i_qgroups = (u64 *)(inherit + 1);
2280 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002281 if (*i_qgroups) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002282 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002283 if (ret)
2284 goto unlock;
2285 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002286 ++i_qgroups;
2287 }
2288
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002289 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002290 struct btrfs_qgroup *src;
2291 struct btrfs_qgroup *dst;
2292
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002293 if (!i_qgroups[0] || !i_qgroups[1])
2294 continue;
2295
Arne Jansenbed92ea2012-06-28 18:03:02 +02002296 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2297 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2298
2299 if (!src || !dst) {
2300 ret = -EINVAL;
2301 goto unlock;
2302 }
2303
2304 dst->rfer = src->rfer - level_size;
2305 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002306 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002307 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002308 struct btrfs_qgroup *src;
2309 struct btrfs_qgroup *dst;
2310
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002311 if (!i_qgroups[0] || !i_qgroups[1])
2312 continue;
2313
Arne Jansenbed92ea2012-06-28 18:03:02 +02002314 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2315 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2316
2317 if (!src || !dst) {
2318 ret = -EINVAL;
2319 goto unlock;
2320 }
2321
2322 dst->excl = src->excl + level_size;
2323 dst->excl_cmpr = src->excl_cmpr + level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002324 }
2325
2326unlock:
2327 spin_unlock(&fs_info->qgroup_lock);
2328out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002329 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002330 return ret;
2331}
2332
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002333static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2334{
2335 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2336 qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
2337 return false;
2338
2339 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2340 qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
2341 return false;
2342
2343 return true;
2344}
2345
2346static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002347{
2348 struct btrfs_root *quota_root;
2349 struct btrfs_qgroup *qgroup;
2350 struct btrfs_fs_info *fs_info = root->fs_info;
2351 u64 ref_root = root->root_key.objectid;
2352 int ret = 0;
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002353 int retried = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002354 struct ulist_node *unode;
2355 struct ulist_iterator uiter;
2356
2357 if (!is_fstree(ref_root))
2358 return 0;
2359
2360 if (num_bytes == 0)
2361 return 0;
Sargun Dhillonf29efe22017-05-11 21:17:33 +00002362
2363 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2364 capable(CAP_SYS_RESOURCE))
2365 enforce = false;
2366
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002367retry:
Arne Jansenbed92ea2012-06-28 18:03:02 +02002368 spin_lock(&fs_info->qgroup_lock);
2369 quota_root = fs_info->quota_root;
2370 if (!quota_root)
2371 goto out;
2372
2373 qgroup = find_qgroup_rb(fs_info, ref_root);
2374 if (!qgroup)
2375 goto out;
2376
2377 /*
2378 * in a first step, we check all affected qgroups if any limits would
2379 * be exceeded
2380 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002381 ulist_reinit(fs_info->qgroup_ulist);
2382 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002383 (uintptr_t)qgroup, GFP_ATOMIC);
2384 if (ret < 0)
2385 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002386 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002387 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002388 struct btrfs_qgroup *qg;
2389 struct btrfs_qgroup_list *glist;
2390
David Sterbaef2fff62016-10-26 16:23:50 +02002391 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002392
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002393 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002394 /*
2395 * Commit the tree and retry, since we may have
2396 * deletions which would free up space.
2397 */
2398 if (!retried && qg->reserved > 0) {
2399 struct btrfs_trans_handle *trans;
2400
2401 spin_unlock(&fs_info->qgroup_lock);
2402 ret = btrfs_start_delalloc_inodes(root, 0);
2403 if (ret)
2404 return ret;
Chris Mason6374e57a2017-06-23 09:48:21 -07002405 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002406 trans = btrfs_join_transaction(root);
2407 if (IS_ERR(trans))
2408 return PTR_ERR(trans);
2409 ret = btrfs_commit_transaction(trans);
2410 if (ret)
2411 return ret;
2412 retried++;
2413 goto retry;
2414 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002415 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002416 goto out;
2417 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002418
2419 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002420 ret = ulist_add(fs_info->qgroup_ulist,
2421 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002422 (uintptr_t)glist->group, GFP_ATOMIC);
2423 if (ret < 0)
2424 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002425 }
2426 }
Wang Shilong3c971852013-04-17 14:00:36 +00002427 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002428 /*
2429 * no limits exceeded, now record the reservation into all qgroups
2430 */
2431 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002432 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002433 struct btrfs_qgroup *qg;
2434
David Sterbaef2fff62016-10-26 16:23:50 +02002435 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002436
Qu Wenruo3159fe72017-03-13 15:52:08 +08002437 trace_qgroup_update_reserve(fs_info, qg, num_bytes);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05002438 qg->reserved += num_bytes;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002439 }
2440
2441out:
2442 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002443 return ret;
2444}
2445
Qu Wenruo297d7502015-09-08 17:08:37 +08002446void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2447 u64 ref_root, u64 num_bytes)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002448{
2449 struct btrfs_root *quota_root;
2450 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002451 struct ulist_node *unode;
2452 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00002453 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002454
2455 if (!is_fstree(ref_root))
2456 return;
2457
2458 if (num_bytes == 0)
2459 return;
2460
2461 spin_lock(&fs_info->qgroup_lock);
2462
2463 quota_root = fs_info->quota_root;
2464 if (!quota_root)
2465 goto out;
2466
2467 qgroup = find_qgroup_rb(fs_info, ref_root);
2468 if (!qgroup)
2469 goto out;
2470
Wang Shilong1e8f9152013-05-06 11:03:27 +00002471 ulist_reinit(fs_info->qgroup_ulist);
2472 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002473 (uintptr_t)qgroup, GFP_ATOMIC);
2474 if (ret < 0)
2475 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002476 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002477 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002478 struct btrfs_qgroup *qg;
2479 struct btrfs_qgroup_list *glist;
2480
David Sterbaef2fff62016-10-26 16:23:50 +02002481 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002482
Qu Wenruo3159fe72017-03-13 15:52:08 +08002483 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02002484 if (qg->reserved < num_bytes)
Qu Wenruo18dc22c2016-10-20 10:28:41 +08002485 report_reserved_underflow(fs_info, qg, num_bytes);
2486 else
2487 qg->reserved -= num_bytes;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002488
2489 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002490 ret = ulist_add(fs_info->qgroup_ulist,
2491 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002492 (uintptr_t)glist->group, GFP_ATOMIC);
2493 if (ret < 0)
2494 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002495 }
2496 }
2497
2498out:
2499 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002500}
2501
Jan Schmidt2f232032013-04-25 16:04:51 +00002502/*
2503 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08002504 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00002505 */
2506static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002507qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002508 struct btrfs_trans_handle *trans)
Jan Schmidt2f232032013-04-25 16:04:51 +00002509{
2510 struct btrfs_key found;
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002511 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002512 struct ulist *roots = NULL;
David Sterba3284da72015-02-25 15:47:32 +01002513 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Josef Bacikfcebe452014-05-13 17:30:47 -07002514 u64 num_bytes;
Jan Schmidt2f232032013-04-25 16:04:51 +00002515 int slot;
2516 int ret;
2517
Jan Schmidt2f232032013-04-25 16:04:51 +00002518 mutex_lock(&fs_info->qgroup_rescan_lock);
2519 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2520 &fs_info->qgroup_rescan_progress,
2521 path, 1, 0);
2522
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002523 btrfs_debug(fs_info,
2524 "current progress key (%llu %u %llu), search_slot ret %d",
2525 fs_info->qgroup_rescan_progress.objectid,
2526 fs_info->qgroup_rescan_progress.type,
2527 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002528
2529 if (ret) {
2530 /*
2531 * The rescan is about to end, we will not be scanning any
2532 * further blocks. We cannot unset the RESCAN flag here, because
2533 * we want to commit the transaction if everything went well.
2534 * To make the live accounting work in this phase, we set our
2535 * scan progress pointer such that every real extent objectid
2536 * will be smaller.
2537 */
2538 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2539 btrfs_release_path(path);
2540 mutex_unlock(&fs_info->qgroup_rescan_lock);
2541 return ret;
2542 }
2543
2544 btrfs_item_key_to_cpu(path->nodes[0], &found,
2545 btrfs_header_nritems(path->nodes[0]) - 1);
2546 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2547
2548 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002549 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2550 if (!scratch_leaf) {
2551 ret = -ENOMEM;
2552 mutex_unlock(&fs_info->qgroup_rescan_lock);
2553 goto out;
2554 }
2555 extent_buffer_get(scratch_leaf);
2556 btrfs_tree_read_lock(scratch_leaf);
2557 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
Jan Schmidt2f232032013-04-25 16:04:51 +00002558 slot = path->slots[0];
2559 btrfs_release_path(path);
2560 mutex_unlock(&fs_info->qgroup_rescan_lock);
2561
2562 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2563 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002564 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2565 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002566 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002567 if (found.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04002568 num_bytes = fs_info->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002569 else
2570 num_bytes = found.offset;
2571
Josef Bacikfcebe452014-05-13 17:30:47 -07002572 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2573 &roots);
Jan Schmidt2f232032013-04-25 16:04:51 +00002574 if (ret < 0)
2575 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08002576 /* For rescan, just pass old_roots as NULL */
2577 ret = btrfs_qgroup_account_extent(trans, fs_info,
2578 found.objectid, num_bytes, NULL, roots);
2579 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00002580 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002581 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002582out:
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002583 if (scratch_leaf) {
2584 btrfs_tree_read_unlock_blocking(scratch_leaf);
2585 free_extent_buffer(scratch_leaf);
2586 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002587 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2588
2589 return ret;
2590}
2591
Qu Wenruod458b052014-02-28 10:46:19 +08002592static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002593{
Jan Schmidtb382a322013-05-28 15:47:24 +00002594 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2595 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002596 struct btrfs_path *path;
2597 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002598 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002599 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002600
2601 path = btrfs_alloc_path();
2602 if (!path)
2603 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002604
2605 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08002606 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002607 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2608 if (IS_ERR(trans)) {
2609 err = PTR_ERR(trans);
2610 break;
2611 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002612 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002613 err = -EINTR;
2614 } else {
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002615 err = qgroup_rescan_leaf(fs_info, path, trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002616 }
2617 if (err > 0)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002618 btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002619 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002620 btrfs_end_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002621 }
2622
2623out:
Jan Schmidt2f232032013-04-25 16:04:51 +00002624 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002625
2626 mutex_lock(&fs_info->qgroup_rescan_lock);
Justin Maggard7343dd62015-11-04 15:56:16 -08002627 if (!btrfs_fs_closing(fs_info))
2628 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidt2f232032013-04-25 16:04:51 +00002629
Qu Wenruo33931682015-02-27 16:24:24 +08002630 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00002631 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2632 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2633 } else if (err < 0) {
2634 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2635 }
2636 mutex_unlock(&fs_info->qgroup_rescan_lock);
2637
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002638 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002639 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002640 * qgroup info.
2641 */
2642 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2643 if (IS_ERR(trans)) {
2644 err = PTR_ERR(trans);
2645 btrfs_err(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002646 "fail to start transaction for status update: %d",
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002647 err);
2648 goto done;
2649 }
2650 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2651 if (ret < 0) {
2652 err = ret;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002653 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002654 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002655 btrfs_end_transaction(trans);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002656
Justin Maggard7343dd62015-11-04 15:56:16 -08002657 if (btrfs_fs_closing(fs_info)) {
2658 btrfs_info(fs_info, "qgroup scan paused");
2659 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002660 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08002661 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00002662 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002663 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002664 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002665
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002666done:
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002667 mutex_lock(&fs_info->qgroup_rescan_lock);
2668 fs_info->qgroup_rescan_running = false;
2669 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002670 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002671}
2672
Jan Schmidtb382a322013-05-28 15:47:24 +00002673/*
2674 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2675 * memory required for the rescan context.
2676 */
2677static int
2678qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2679 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002680{
2681 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002682
Jan Schmidtb382a322013-05-28 15:47:24 +00002683 if (!init_flags &&
2684 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2685 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2686 ret = -EINVAL;
2687 goto err;
2688 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002689
2690 mutex_lock(&fs_info->qgroup_rescan_lock);
2691 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002692
2693 if (init_flags) {
2694 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2695 ret = -EINPROGRESS;
2696 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2697 ret = -EINVAL;
2698
2699 if (ret) {
2700 spin_unlock(&fs_info->qgroup_lock);
2701 mutex_unlock(&fs_info->qgroup_rescan_lock);
2702 goto err;
2703 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002704 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2705 }
2706
2707 memset(&fs_info->qgroup_rescan_progress, 0,
2708 sizeof(fs_info->qgroup_rescan_progress));
2709 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00002710 init_completion(&fs_info->qgroup_rescan_completion);
Filipe Manana8d9edda2016-11-24 02:09:04 +00002711 fs_info->qgroup_rescan_running = true;
Jan Schmidtb382a322013-05-28 15:47:24 +00002712
2713 spin_unlock(&fs_info->qgroup_lock);
2714 mutex_unlock(&fs_info->qgroup_rescan_lock);
2715
Jan Schmidtb382a322013-05-28 15:47:24 +00002716 memset(&fs_info->qgroup_rescan_work, 0,
2717 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002718 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002719 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002720 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002721
Jan Schmidt2f232032013-04-25 16:04:51 +00002722 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002723err:
Frank Holtonefe120a2013-12-20 11:37:06 -05002724 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002725 return ret;
2726 }
2727
Jan Schmidtb382a322013-05-28 15:47:24 +00002728 return 0;
2729}
Jan Schmidt2f232032013-04-25 16:04:51 +00002730
Jan Schmidtb382a322013-05-28 15:47:24 +00002731static void
2732qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2733{
2734 struct rb_node *n;
2735 struct btrfs_qgroup *qgroup;
2736
2737 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002738 /* clear all current qgroup tracking information */
2739 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2740 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2741 qgroup->rfer = 0;
2742 qgroup->rfer_cmpr = 0;
2743 qgroup->excl = 0;
2744 qgroup->excl_cmpr = 0;
2745 }
2746 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002747}
Jan Schmidt2f232032013-04-25 16:04:51 +00002748
Jan Schmidtb382a322013-05-28 15:47:24 +00002749int
2750btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2751{
2752 int ret = 0;
2753 struct btrfs_trans_handle *trans;
2754
2755 ret = qgroup_rescan_init(fs_info, 0, 1);
2756 if (ret)
2757 return ret;
2758
2759 /*
2760 * We have set the rescan_progress to 0, which means no more
2761 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2762 * However, btrfs_qgroup_account_ref may be right after its call
2763 * to btrfs_find_all_roots, in which case it would still do the
2764 * accounting.
2765 * To solve this, we're committing the transaction, which will
2766 * ensure we run all delayed refs and only after that, we are
2767 * going to clear all tracking information for a clean start.
2768 */
2769
2770 trans = btrfs_join_transaction(fs_info->fs_root);
2771 if (IS_ERR(trans)) {
2772 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2773 return PTR_ERR(trans);
2774 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002775 ret = btrfs_commit_transaction(trans);
Jan Schmidtb382a322013-05-28 15:47:24 +00002776 if (ret) {
2777 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2778 return ret;
2779 }
2780
2781 qgroup_rescan_zero_tracking(fs_info);
2782
Qu Wenruofc97fab2014-02-28 10:46:16 +08002783 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2784 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002785
2786 return 0;
2787}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002788
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002789int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2790 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002791{
2792 int running;
2793 int ret = 0;
2794
2795 mutex_lock(&fs_info->qgroup_rescan_lock);
2796 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002797 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002798 spin_unlock(&fs_info->qgroup_lock);
2799 mutex_unlock(&fs_info->qgroup_rescan_lock);
2800
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002801 if (!running)
2802 return 0;
2803
2804 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002805 ret = wait_for_completion_interruptible(
2806 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002807 else
2808 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002809
2810 return ret;
2811}
Jan Schmidtb382a322013-05-28 15:47:24 +00002812
2813/*
2814 * this is only called from open_ctree where we're still single threaded, thus
2815 * locking is omitted here.
2816 */
2817void
2818btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2819{
2820 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002821 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2822 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002823}
Qu Wenruo52472552015-10-12 16:05:40 +08002824
2825/*
2826 * Reserve qgroup space for range [start, start + len).
2827 *
2828 * This function will either reserve space from related qgroups or doing
2829 * nothing if the range is already reserved.
2830 *
2831 * Return 0 for successful reserve
2832 * Return <0 for error (including -EQUOT)
2833 *
2834 * NOTE: this function may sleep for memory allocation.
Qu Wenruo364ecf32017-02-27 15:10:38 +08002835 * if btrfs_qgroup_reserve_data() is called multiple times with
2836 * same @reserved, caller must ensure when error happens it's OK
2837 * to free *ALL* reserved space.
Qu Wenruo52472552015-10-12 16:05:40 +08002838 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08002839int btrfs_qgroup_reserve_data(struct inode *inode,
2840 struct extent_changeset **reserved_ret, u64 start,
2841 u64 len)
Qu Wenruo52472552015-10-12 16:05:40 +08002842{
2843 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo52472552015-10-12 16:05:40 +08002844 struct ulist_node *unode;
2845 struct ulist_iterator uiter;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002846 struct extent_changeset *reserved;
2847 u64 orig_reserved;
2848 u64 to_reserve;
Qu Wenruo52472552015-10-12 16:05:40 +08002849 int ret;
2850
Josef Bacikafcdd122016-09-02 15:40:02 -04002851 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2852 !is_fstree(root->objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08002853 return 0;
2854
Qu Wenruo364ecf32017-02-27 15:10:38 +08002855 /* @reserved parameter is mandatory for qgroup */
2856 if (WARN_ON(!reserved_ret))
2857 return -EINVAL;
2858 if (!*reserved_ret) {
2859 *reserved_ret = extent_changeset_alloc();
2860 if (!*reserved_ret)
2861 return -ENOMEM;
2862 }
2863 reserved = *reserved_ret;
2864 /* Record already reserved space */
2865 orig_reserved = reserved->bytes_changed;
Qu Wenruo52472552015-10-12 16:05:40 +08002866 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002867 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2868
2869 /* Newly reserved space */
2870 to_reserve = reserved->bytes_changed - orig_reserved;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002871 trace_btrfs_qgroup_reserve_data(inode, start, len,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002872 to_reserve, QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08002873 if (ret < 0)
2874 goto cleanup;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002875 ret = qgroup_reserve(root, to_reserve, true);
Qu Wenruo52472552015-10-12 16:05:40 +08002876 if (ret < 0)
2877 goto cleanup;
2878
Qu Wenruo52472552015-10-12 16:05:40 +08002879 return ret;
2880
2881cleanup:
Qu Wenruo364ecf32017-02-27 15:10:38 +08002882 /* cleanup *ALL* already reserved ranges */
Qu Wenruo52472552015-10-12 16:05:40 +08002883 ULIST_ITER_INIT(&uiter);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002884 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
Qu Wenruo52472552015-10-12 16:05:40 +08002885 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2886 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
2887 GFP_NOFS);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002888 extent_changeset_release(reserved);
Qu Wenruo52472552015-10-12 16:05:40 +08002889 return ret;
2890}
Qu Wenruof695fdc2015-10-12 16:28:06 +08002891
Qu Wenruobc42bda2017-02-27 15:10:39 +08002892/* Free ranges specified by @reserved, normally in error path */
2893static int qgroup_free_reserved_data(struct inode *inode,
2894 struct extent_changeset *reserved, u64 start, u64 len)
2895{
2896 struct btrfs_root *root = BTRFS_I(inode)->root;
2897 struct ulist_node *unode;
2898 struct ulist_iterator uiter;
2899 struct extent_changeset changeset;
2900 int freed = 0;
2901 int ret;
2902
2903 extent_changeset_init(&changeset);
2904 len = round_up(start + len, root->fs_info->sectorsize);
2905 start = round_down(start, root->fs_info->sectorsize);
2906
2907 ULIST_ITER_INIT(&uiter);
2908 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
2909 u64 range_start = unode->val;
2910 /* unode->aux is the inclusive end */
2911 u64 range_len = unode->aux - range_start + 1;
2912 u64 free_start;
2913 u64 free_len;
2914
2915 extent_changeset_release(&changeset);
2916
2917 /* Only free range in range [start, start + len) */
2918 if (range_start >= start + len ||
2919 range_start + range_len <= start)
2920 continue;
2921 free_start = max(range_start, start);
2922 free_len = min(start + len, range_start + range_len) -
2923 free_start;
2924 /*
2925 * TODO: To also modify reserved->ranges_reserved to reflect
2926 * the modification.
2927 *
2928 * However as long as we free qgroup reserved according to
2929 * EXTENT_QGROUP_RESERVED, we won't double free.
2930 * So not need to rush.
2931 */
2932 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
2933 free_start, free_start + free_len - 1,
2934 EXTENT_QGROUP_RESERVED, &changeset);
2935 if (ret < 0)
2936 goto out;
2937 freed += changeset.bytes_changed;
2938 }
2939 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed);
2940 ret = freed;
2941out:
2942 extent_changeset_release(&changeset);
2943 return ret;
2944}
2945
2946static int __btrfs_qgroup_release_data(struct inode *inode,
2947 struct extent_changeset *reserved, u64 start, u64 len,
2948 int free)
Qu Wenruof695fdc2015-10-12 16:28:06 +08002949{
2950 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002951 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08002952 int ret;
2953
Qu Wenruobc42bda2017-02-27 15:10:39 +08002954 /* In release case, we shouldn't have @reserved */
2955 WARN_ON(!free && reserved);
2956 if (free && reserved)
2957 return qgroup_free_reserved_data(inode, reserved, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002958 extent_changeset_init(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002959 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterbaf734c442016-04-26 23:54:39 +02002960 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002961 if (ret < 0)
2962 goto out;
2963
Qu Wenruod51ea5d2017-03-13 15:52:09 +08002964 if (free)
2965 trace_op = QGROUP_FREE;
2966 trace_btrfs_qgroup_release_data(inode, start, len,
2967 changeset.bytes_changed, trace_op);
2968 if (free)
David Sterba0b08e1f2017-02-13 14:24:35 +01002969 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
2970 BTRFS_I(inode)->root->objectid,
2971 changeset.bytes_changed);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08002972 ret = changeset.bytes_changed;
Qu Wenruof695fdc2015-10-12 16:28:06 +08002973out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08002974 extent_changeset_release(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002975 return ret;
2976}
2977
2978/*
2979 * Free a reserved space range from io_tree and related qgroups
2980 *
2981 * Should be called when a range of pages get invalidated before reaching disk.
2982 * Or for error cleanup case.
Qu Wenruobc42bda2017-02-27 15:10:39 +08002983 * if @reserved is given, only reserved range in [@start, @start + @len) will
2984 * be freed.
Qu Wenruof695fdc2015-10-12 16:28:06 +08002985 *
2986 * For data written to disk, use btrfs_qgroup_release_data().
2987 *
2988 * NOTE: This function may sleep for memory allocation.
2989 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002990int btrfs_qgroup_free_data(struct inode *inode,
2991 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruof695fdc2015-10-12 16:28:06 +08002992{
Qu Wenruobc42bda2017-02-27 15:10:39 +08002993 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002994}
2995
2996/*
2997 * Release a reserved space range from io_tree only.
2998 *
2999 * Should be called when a range of pages get written to disk and corresponding
3000 * FILE_EXTENT is inserted into corresponding root.
3001 *
3002 * Since new qgroup accounting framework will only update qgroup numbers at
3003 * commit_transaction() time, its reserved space shouldn't be freed from
3004 * related qgroups.
3005 *
3006 * But we should release the range from io_tree, to allow further write to be
3007 * COWed.
3008 *
3009 * NOTE: This function may sleep for memory allocation.
3010 */
3011int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3012{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003013 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003014}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003015
Jeff Mahoney003d7c52017-01-25 09:50:33 -05003016int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3017 bool enforce)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003018{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003019 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003020 int ret;
3021
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003022 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003023 !is_fstree(root->objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003024 return 0;
3025
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003026 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo3159fe72017-03-13 15:52:08 +08003027 trace_qgroup_meta_reserve(root, (s64)num_bytes);
Jeff Mahoney003d7c52017-01-25 09:50:33 -05003028 ret = qgroup_reserve(root, num_bytes, enforce);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003029 if (ret < 0)
3030 return ret;
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003031 atomic64_add(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003032 return ret;
3033}
3034
3035void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
3036{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 struct btrfs_fs_info *fs_info = root->fs_info;
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003038 u64 reserved;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003039
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003040 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003041 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003042 return;
3043
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003044 reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003045 if (reserved == 0)
3046 return;
Qu Wenruo3159fe72017-03-13 15:52:08 +08003047 trace_qgroup_meta_reserve(root, -(s64)reserved);
David Sterba0b08e1f2017-02-13 14:24:35 +01003048 btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003049}
3050
3051void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
3052{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003053 struct btrfs_fs_info *fs_info = root->fs_info;
3054
3055 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003056 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003057 return;
3058
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003059 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003060 WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
3061 atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo3159fe72017-03-13 15:52:08 +08003062 trace_qgroup_meta_reserve(root, -(s64)num_bytes);
David Sterba0b08e1f2017-02-13 14:24:35 +01003063 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003064}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003065
3066/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003067 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003068 * time
3069 */
3070void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3071{
3072 struct extent_changeset changeset;
3073 struct ulist_node *unode;
3074 struct ulist_iterator iter;
3075 int ret;
3076
Qu Wenruo364ecf32017-02-27 15:10:38 +08003077 extent_changeset_init(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003078 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02003079 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003080
3081 WARN_ON(ret < 0);
3082 if (WARN_ON(changeset.bytes_changed)) {
3083 ULIST_ITER_INIT(&iter);
David Sterba53d32352017-02-13 13:42:29 +01003084 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003085 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3086 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3087 inode->i_ino, unode->val, unode->aux);
3088 }
David Sterba0b08e1f2017-02-13 14:24:35 +01003089 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3090 BTRFS_I(inode)->root->objectid,
3091 changeset.bytes_changed);
3092
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003093 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08003094 extent_changeset_release(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003095}