blob: 7d16f0692d73745e5e86f8aa77226d806da171dd [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:
Josef Bacikafcdd122016-09-02 15:40:02 -0400810 set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200811 btrfs_free_path(path);
812 return ret;
813}
814
815int btrfs_quota_enable(struct btrfs_trans_handle *trans,
816 struct btrfs_fs_info *fs_info)
817{
818 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000819 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200820 struct btrfs_path *path = NULL;
821 struct btrfs_qgroup_status_item *ptr;
822 struct extent_buffer *leaf;
823 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000824 struct btrfs_key found_key;
825 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200826 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000827 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200828
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000829 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200830 if (fs_info->quota_root) {
Josef Bacikafcdd122016-09-02 15:40:02 -0400831 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200832 goto out;
833 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200834
David Sterba52bf8e72017-02-13 11:03:44 +0100835 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000836 if (!fs_info->qgroup_ulist) {
837 ret = -ENOMEM;
838 goto out;
839 }
840
Arne Jansenbed92ea2012-06-28 18:03:02 +0200841 /*
842 * initially create the quota tree
843 */
844 quota_root = btrfs_create_tree(trans, fs_info,
845 BTRFS_QUOTA_TREE_OBJECTID);
846 if (IS_ERR(quota_root)) {
847 ret = PTR_ERR(quota_root);
848 goto out;
849 }
850
851 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000852 if (!path) {
853 ret = -ENOMEM;
854 goto out_free_root;
855 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200856
857 key.objectid = 0;
858 key.type = BTRFS_QGROUP_STATUS_KEY;
859 key.offset = 0;
860
861 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
862 sizeof(*ptr));
863 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000864 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200865
866 leaf = path->nodes[0];
867 ptr = btrfs_item_ptr(leaf, path->slots[0],
868 struct btrfs_qgroup_status_item);
869 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
870 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
871 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
872 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
873 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000874 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200875
876 btrfs_mark_buffer_dirty(leaf);
877
Wang Shilong7708f022013-04-07 10:24:57 +0000878 key.objectid = 0;
879 key.type = BTRFS_ROOT_REF_KEY;
880 key.offset = 0;
881
882 btrfs_release_path(path);
883 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
884 if (ret > 0)
885 goto out_add_root;
886 if (ret < 0)
887 goto out_free_path;
888
889
890 while (1) {
891 slot = path->slots[0];
892 leaf = path->nodes[0];
893 btrfs_item_key_to_cpu(leaf, &found_key, slot);
894
895 if (found_key.type == BTRFS_ROOT_REF_KEY) {
896 ret = add_qgroup_item(trans, quota_root,
897 found_key.offset);
898 if (ret)
899 goto out_free_path;
900
Wang Shilong7708f022013-04-07 10:24:57 +0000901 qgroup = add_qgroup_rb(fs_info, found_key.offset);
902 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000903 ret = PTR_ERR(qgroup);
904 goto out_free_path;
905 }
Wang Shilong7708f022013-04-07 10:24:57 +0000906 }
907 ret = btrfs_next_item(tree_root, path);
908 if (ret < 0)
909 goto out_free_path;
910 if (ret)
911 break;
912 }
913
914out_add_root:
915 btrfs_release_path(path);
916 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
917 if (ret)
918 goto out_free_path;
919
Wang Shilong7708f022013-04-07 10:24:57 +0000920 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
921 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000922 ret = PTR_ERR(qgroup);
923 goto out_free_path;
924 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000925 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200926 fs_info->quota_root = quota_root;
Josef Bacikafcdd122016-09-02 15:40:02 -0400927 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200928 spin_unlock(&fs_info->qgroup_lock);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000929out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200930 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000931out_free_root:
932 if (ret) {
933 free_extent_buffer(quota_root->node);
934 free_extent_buffer(quota_root->commit_root);
935 kfree(quota_root);
936 }
937out:
Jan Schmidteb1716a2013-05-28 15:47:23 +0000938 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000939 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000940 fs_info->qgroup_ulist = NULL;
941 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000942 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200943 return ret;
944}
945
946int btrfs_quota_disable(struct btrfs_trans_handle *trans,
947 struct btrfs_fs_info *fs_info)
948{
949 struct btrfs_root *tree_root = fs_info->tree_root;
950 struct btrfs_root *quota_root;
951 int ret = 0;
952
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000953 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +0000954 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000955 goto out;
Josef Bacikafcdd122016-09-02 15:40:02 -0400956 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
957 set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -0400958 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -0800959 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200960 quota_root = fs_info->quota_root;
961 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +0800962 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200963 spin_unlock(&fs_info->qgroup_lock);
964
Wang Shilonge685da12013-08-14 09:13:37 +0800965 btrfs_free_qgroup_config(fs_info);
966
Arne Jansenbed92ea2012-06-28 18:03:02 +0200967 ret = btrfs_clean_quota_tree(trans, quota_root);
968 if (ret)
969 goto out;
970
971 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
972 if (ret)
973 goto out;
974
975 list_del(&quota_root->dirty_list);
976
977 btrfs_tree_lock(quota_root->node);
David Sterba7c302b42017-02-10 18:47:57 +0100978 clean_tree_block(fs_info, quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200979 btrfs_tree_unlock(quota_root->node);
980 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
981
982 free_extent_buffer(quota_root->node);
983 free_extent_buffer(quota_root->commit_root);
984 kfree(quota_root);
985out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000986 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200987 return ret;
988}
989
Jan Schmidt2f232032013-04-25 16:04:51 +0000990static void qgroup_dirty(struct btrfs_fs_info *fs_info,
991 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200992{
Jan Schmidt2f232032013-04-25 16:04:51 +0000993 if (list_empty(&qgroup->dirty))
994 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200995}
996
Qu Wenruo18dc22c2016-10-20 10:28:41 +0800997static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
998 struct btrfs_qgroup *qgroup,
999 u64 num_bytes)
1000{
David Sterba338bd522017-04-18 17:00:12 +02001001#ifdef CONFIG_BTRFS_DEBUG
1002 WARN_ON(qgroup->reserved < num_bytes);
1003 btrfs_debug(fs_info,
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001004 "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
1005 qgroup->qgroupid, qgroup->reserved, num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001006#endif
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001007 qgroup->reserved = 0;
1008}
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001009/*
1010 * The easy accounting, if we are adding/removing the only ref for an extent
Nicholas D Steeves01327612016-05-19 21:18:45 -04001011 * then this qgroup and all of the parent qgroups get their reference and
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001012 * exclusive counts adjusted.
1013 *
1014 * Caller should hold fs_info->qgroup_lock.
1015 */
1016static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1017 struct ulist *tmp, u64 ref_root,
1018 u64 num_bytes, int sign)
1019{
1020 struct btrfs_qgroup *qgroup;
1021 struct btrfs_qgroup_list *glist;
1022 struct ulist_node *unode;
1023 struct ulist_iterator uiter;
1024 int ret = 0;
1025
1026 qgroup = find_qgroup_rb(fs_info, ref_root);
1027 if (!qgroup)
1028 goto out;
1029
1030 qgroup->rfer += sign * num_bytes;
1031 qgroup->rfer_cmpr += sign * num_bytes;
1032
1033 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1034 qgroup->excl += sign * num_bytes;
1035 qgroup->excl_cmpr += sign * num_bytes;
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001036 if (sign > 0) {
Qu Wenruo3159fe72017-03-13 15:52:08 +08001037 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001038 if (qgroup->reserved < num_bytes)
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001039 report_reserved_underflow(fs_info, qgroup, num_bytes);
1040 else
1041 qgroup->reserved -= num_bytes;
1042 }
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001043
1044 qgroup_dirty(fs_info, qgroup);
1045
1046 /* Get all of the parent groups that contain this qgroup */
1047 list_for_each_entry(glist, &qgroup->groups, next_group) {
1048 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001049 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001050 if (ret < 0)
1051 goto out;
1052 }
1053
1054 /* Iterate all of the parents and adjust their reference counts */
1055 ULIST_ITER_INIT(&uiter);
1056 while ((unode = ulist_next(tmp, &uiter))) {
David Sterbaef2fff62016-10-26 16:23:50 +02001057 qgroup = unode_aux_to_qgroup(unode);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001058 qgroup->rfer += sign * num_bytes;
1059 qgroup->rfer_cmpr += sign * num_bytes;
1060 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1061 qgroup->excl += sign * num_bytes;
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001062 if (sign > 0) {
Qu Wenruo3159fe72017-03-13 15:52:08 +08001063 trace_qgroup_update_reserve(fs_info, qgroup,
1064 -(s64)num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001065 if (qgroup->reserved < num_bytes)
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001066 report_reserved_underflow(fs_info, qgroup,
1067 num_bytes);
1068 else
1069 qgroup->reserved -= num_bytes;
1070 }
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001071 qgroup->excl_cmpr += sign * num_bytes;
1072 qgroup_dirty(fs_info, qgroup);
1073
1074 /* Add any parents of the parents */
1075 list_for_each_entry(glist, &qgroup->groups, next_group) {
1076 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001077 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001078 if (ret < 0)
1079 goto out;
1080 }
1081 }
1082 ret = 0;
1083out:
1084 return ret;
1085}
1086
1087
1088/*
1089 * Quick path for updating qgroup with only excl refs.
1090 *
1091 * In that case, just update all parent will be enough.
1092 * Or we needs to do a full rescan.
1093 * Caller should also hold fs_info->qgroup_lock.
1094 *
1095 * Return 0 for quick update, return >0 for need to full rescan
1096 * and mark INCONSISTENT flag.
1097 * Return < 0 for other error.
1098 */
1099static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1100 struct ulist *tmp, u64 src, u64 dst,
1101 int sign)
1102{
1103 struct btrfs_qgroup *qgroup;
1104 int ret = 1;
1105 int err = 0;
1106
1107 qgroup = find_qgroup_rb(fs_info, src);
1108 if (!qgroup)
1109 goto out;
1110 if (qgroup->excl == qgroup->rfer) {
1111 ret = 0;
1112 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1113 qgroup->excl, sign);
1114 if (err < 0) {
1115 ret = err;
1116 goto out;
1117 }
1118 }
1119out:
1120 if (ret)
1121 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1122 return ret;
1123}
1124
Arne Jansenbed92ea2012-06-28 18:03:02 +02001125int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1126 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1127{
1128 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001129 struct btrfs_qgroup *parent;
1130 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001131 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001132 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001133 int ret = 0;
1134
Qu Wenruo8465ece2015-02-27 16:24:22 +08001135 /* Check the level of src and dst first */
1136 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1137 return -EINVAL;
1138
David Sterba6602caf2017-02-13 12:41:02 +01001139 tmp = ulist_alloc(GFP_KERNEL);
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001140 if (!tmp)
1141 return -ENOMEM;
1142
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001143 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001144 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001145 if (!quota_root) {
1146 ret = -EINVAL;
1147 goto out;
1148 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001149 member = find_qgroup_rb(fs_info, src);
1150 parent = find_qgroup_rb(fs_info, dst);
1151 if (!member || !parent) {
1152 ret = -EINVAL;
1153 goto out;
1154 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001155
Wang Shilong534e6622013-04-17 14:49:51 +00001156 /* check if such qgroup relation exist firstly */
1157 list_for_each_entry(list, &member->groups, next_group) {
1158 if (list->group == parent) {
1159 ret = -EEXIST;
1160 goto out;
1161 }
1162 }
1163
Arne Jansenbed92ea2012-06-28 18:03:02 +02001164 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1165 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001166 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001167
1168 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1169 if (ret) {
1170 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001171 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001172 }
1173
1174 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001175 ret = add_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001176 if (ret < 0) {
1177 spin_unlock(&fs_info->qgroup_lock);
1178 goto out;
1179 }
1180 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001181 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001182out:
1183 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001184 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001185 return ret;
1186}
1187
David Sterba025db912017-02-13 13:00:51 +01001188static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001189 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1190{
1191 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001192 struct btrfs_qgroup *parent;
1193 struct btrfs_qgroup *member;
1194 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001195 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001196 int ret = 0;
1197 int err;
1198
David Sterba6602caf2017-02-13 12:41:02 +01001199 tmp = ulist_alloc(GFP_KERNEL);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001200 if (!tmp)
1201 return -ENOMEM;
1202
Arne Jansenbed92ea2012-06-28 18:03:02 +02001203 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001204 if (!quota_root) {
1205 ret = -EINVAL;
1206 goto out;
1207 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001208
Wang Shilong534e6622013-04-17 14:49:51 +00001209 member = find_qgroup_rb(fs_info, src);
1210 parent = find_qgroup_rb(fs_info, dst);
1211 if (!member || !parent) {
1212 ret = -EINVAL;
1213 goto out;
1214 }
1215
1216 /* check if such qgroup relation exist firstly */
1217 list_for_each_entry(list, &member->groups, next_group) {
1218 if (list->group == parent)
1219 goto exist;
1220 }
1221 ret = -ENOENT;
1222 goto out;
1223exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001224 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1225 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1226 if (err && !ret)
1227 ret = err;
1228
1229 spin_lock(&fs_info->qgroup_lock);
1230 del_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001231 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001232 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001233out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001234 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001235 return ret;
1236}
1237
1238int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1239 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1240{
1241 int ret = 0;
1242
1243 mutex_lock(&fs_info->qgroup_ioctl_lock);
1244 ret = __del_qgroup_relation(trans, fs_info, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001245 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001246
Arne Jansenbed92ea2012-06-28 18:03:02 +02001247 return ret;
1248}
1249
1250int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -05001251 struct btrfs_fs_info *fs_info, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001252{
1253 struct btrfs_root *quota_root;
1254 struct btrfs_qgroup *qgroup;
1255 int ret = 0;
1256
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001257 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001258 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001259 if (!quota_root) {
1260 ret = -EINVAL;
1261 goto out;
1262 }
Wang Shilong534e6622013-04-17 14:49:51 +00001263 qgroup = find_qgroup_rb(fs_info, qgroupid);
1264 if (qgroup) {
1265 ret = -EEXIST;
1266 goto out;
1267 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001268
1269 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001270 if (ret)
1271 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001272
1273 spin_lock(&fs_info->qgroup_lock);
1274 qgroup = add_qgroup_rb(fs_info, qgroupid);
1275 spin_unlock(&fs_info->qgroup_lock);
1276
1277 if (IS_ERR(qgroup))
1278 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001279out:
1280 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001281 return ret;
1282}
1283
1284int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1285 struct btrfs_fs_info *fs_info, u64 qgroupid)
1286{
1287 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001288 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001289 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001290 int ret = 0;
1291
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001292 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001293 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001294 if (!quota_root) {
1295 ret = -EINVAL;
1296 goto out;
1297 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001298
Arne Jansen2cf68702013-01-17 01:22:09 -07001299 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001300 if (!qgroup) {
1301 ret = -ENOENT;
1302 goto out;
1303 } else {
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001304 /* check if there are no children of this qgroup */
1305 if (!list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001306 ret = -EBUSY;
1307 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001308 }
1309 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001310 ret = del_qgroup_item(trans, quota_root, qgroupid);
1311
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001312 while (!list_empty(&qgroup->groups)) {
1313 list = list_first_entry(&qgroup->groups,
1314 struct btrfs_qgroup_list, next_group);
1315 ret = __del_qgroup_relation(trans, fs_info,
1316 qgroupid,
1317 list->group->qgroupid);
1318 if (ret)
1319 goto out;
1320 }
1321
Arne Jansenbed92ea2012-06-28 18:03:02 +02001322 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001323 del_qgroup_rb(fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001324 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001325out:
1326 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001327 return ret;
1328}
1329
1330int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1331 struct btrfs_fs_info *fs_info, u64 qgroupid,
1332 struct btrfs_qgroup_limit *limit)
1333{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001334 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001335 struct btrfs_qgroup *qgroup;
1336 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001337 /* Sometimes we would want to clear the limit on this qgroup.
1338 * To meet this requirement, we treat the -1 as a special value
1339 * which tell kernel to clear the limit on this qgroup.
1340 */
1341 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001342
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001343 mutex_lock(&fs_info->qgroup_ioctl_lock);
1344 quota_root = fs_info->quota_root;
1345 if (!quota_root) {
1346 ret = -EINVAL;
1347 goto out;
1348 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001349
Wang Shilongddb47af2013-04-07 10:50:20 +00001350 qgroup = find_qgroup_rb(fs_info, qgroupid);
1351 if (!qgroup) {
1352 ret = -ENOENT;
1353 goto out;
1354 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001355
Wang Shilong58400fc2013-04-07 10:50:17 +00001356 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001357 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1358 if (limit->max_rfer == CLEAR_VALUE) {
1359 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1360 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1361 qgroup->max_rfer = 0;
1362 } else {
1363 qgroup->max_rfer = limit->max_rfer;
1364 }
1365 }
1366 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1367 if (limit->max_excl == CLEAR_VALUE) {
1368 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1369 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1370 qgroup->max_excl = 0;
1371 } else {
1372 qgroup->max_excl = limit->max_excl;
1373 }
1374 }
1375 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1376 if (limit->rsv_rfer == CLEAR_VALUE) {
1377 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1378 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1379 qgroup->rsv_rfer = 0;
1380 } else {
1381 qgroup->rsv_rfer = limit->rsv_rfer;
1382 }
1383 }
1384 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1385 if (limit->rsv_excl == CLEAR_VALUE) {
1386 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1387 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1388 qgroup->rsv_excl = 0;
1389 } else {
1390 qgroup->rsv_excl = limit->rsv_excl;
1391 }
1392 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001393 qgroup->lim_flags |= limit->flags;
1394
Arne Jansenbed92ea2012-06-28 18:03:02 +02001395 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001396
1397 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1398 if (ret) {
1399 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1400 btrfs_info(fs_info, "unable to update quota limit for %llu",
1401 qgroupid);
1402 }
1403
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001404out:
1405 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001406 return ret;
1407}
Mark Fasheh11526512014-07-17 12:39:01 -07001408
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001409int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
1410 struct btrfs_fs_info *fs_info)
1411{
1412 struct btrfs_qgroup_extent_record *record;
1413 struct btrfs_delayed_ref_root *delayed_refs;
1414 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08001415 u64 qgroup_to_skip;
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001416 int ret = 0;
1417
1418 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08001419 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001420
1421 /*
1422 * No need to do lock, since this function will only be called in
Nicholas D Steeves01327612016-05-19 21:18:45 -04001423 * btrfs_commit_transaction().
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001424 */
1425 node = rb_first(&delayed_refs->dirty_extent_root);
1426 while (node) {
1427 record = rb_entry(node, struct btrfs_qgroup_extent_record,
1428 node);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001429 if (WARN_ON(!record->old_roots))
1430 ret = btrfs_find_all_roots(NULL, fs_info,
1431 record->bytenr, 0, &record->old_roots);
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001432 if (ret < 0)
1433 break;
Qu Wenruo9086db82015-04-20 09:53:50 +08001434 if (qgroup_to_skip)
1435 ulist_del(record->old_roots, qgroup_to_skip, 0);
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001436 node = rb_next(node);
1437 }
1438 return ret;
1439}
1440
Qu Wenruo50b3e042016-10-18 09:31:27 +08001441int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +08001442 struct btrfs_delayed_ref_root *delayed_refs,
1443 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001444{
1445 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1446 struct rb_node *parent_node = NULL;
1447 struct btrfs_qgroup_extent_record *entry;
1448 u64 bytenr = record->bytenr;
1449
Mark Fasheh82bd1012015-11-05 14:38:00 -08001450 assert_spin_locked(&delayed_refs->lock);
Qu Wenruo50b3e042016-10-18 09:31:27 +08001451 trace_btrfs_qgroup_trace_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001452
Qu Wenruo3368d002015-04-16 14:34:17 +08001453 while (*p) {
1454 parent_node = *p;
1455 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1456 node);
1457 if (bytenr < entry->bytenr)
1458 p = &(*p)->rb_left;
1459 else if (bytenr > entry->bytenr)
1460 p = &(*p)->rb_right;
1461 else
Qu Wenruocb93b522016-08-15 10:36:50 +08001462 return 1;
Qu Wenruo3368d002015-04-16 14:34:17 +08001463 }
1464
1465 rb_link_node(&record->node, parent_node, p);
1466 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001467 return 0;
1468}
1469
Qu Wenruofb235dc2017-02-15 10:43:03 +08001470int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1471 struct btrfs_qgroup_extent_record *qrecord)
1472{
1473 struct ulist *old_root;
1474 u64 bytenr = qrecord->bytenr;
1475 int ret;
1476
1477 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root);
1478 if (ret < 0)
1479 return ret;
1480
1481 /*
1482 * Here we don't need to get the lock of
1483 * trans->transaction->delayed_refs, since inserted qrecord won't
1484 * be deleted, only qrecord->node may be modified (new qrecord insert)
1485 *
1486 * So modifying qrecord->old_roots is safe here
1487 */
1488 qrecord->old_roots = old_root;
1489 return 0;
1490}
1491
Qu Wenruo50b3e042016-10-18 09:31:27 +08001492int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
Qu Wenruocb93b522016-08-15 10:36:50 +08001493 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1494 gfp_t gfp_flag)
1495{
1496 struct btrfs_qgroup_extent_record *record;
1497 struct btrfs_delayed_ref_root *delayed_refs;
1498 int ret;
1499
Josef Bacikafcdd122016-09-02 15:40:02 -04001500 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1501 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001502 return 0;
1503 if (WARN_ON(trans == NULL))
1504 return -EINVAL;
1505 record = kmalloc(sizeof(*record), gfp_flag);
1506 if (!record)
1507 return -ENOMEM;
1508
1509 delayed_refs = &trans->transaction->delayed_refs;
1510 record->bytenr = bytenr;
1511 record->num_bytes = num_bytes;
1512 record->old_roots = NULL;
1513
1514 spin_lock(&delayed_refs->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001515 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
Qu Wenruocb93b522016-08-15 10:36:50 +08001516 spin_unlock(&delayed_refs->lock);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001517 if (ret > 0) {
Qu Wenruocb93b522016-08-15 10:36:50 +08001518 kfree(record);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001519 return 0;
1520 }
1521 return btrfs_qgroup_trace_extent_post(fs_info, record);
Qu Wenruo3368d002015-04-16 14:34:17 +08001522}
1523
Qu Wenruo33d1f052016-10-18 09:31:28 +08001524int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001525 struct btrfs_fs_info *fs_info,
Qu Wenruo33d1f052016-10-18 09:31:28 +08001526 struct extent_buffer *eb)
1527{
1528 int nr = btrfs_header_nritems(eb);
1529 int i, extent_type, ret;
1530 struct btrfs_key key;
1531 struct btrfs_file_extent_item *fi;
1532 u64 bytenr, num_bytes;
1533
1534 /* We can be called directly from walk_up_proc() */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001535 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001536 return 0;
1537
1538 for (i = 0; i < nr; i++) {
1539 btrfs_item_key_to_cpu(eb, &key, i);
1540
1541 if (key.type != BTRFS_EXTENT_DATA_KEY)
1542 continue;
1543
1544 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1545 /* filter out non qgroup-accountable extents */
1546 extent_type = btrfs_file_extent_type(eb, fi);
1547
1548 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1549 continue;
1550
1551 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1552 if (!bytenr)
1553 continue;
1554
1555 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1556
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001557 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1558 num_bytes, GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001559 if (ret)
1560 return ret;
1561 }
Jeff Mahoneycddf3b22017-06-20 08:15:26 -04001562 cond_resched();
Qu Wenruo33d1f052016-10-18 09:31:28 +08001563 return 0;
1564}
1565
1566/*
1567 * Walk up the tree from the bottom, freeing leaves and any interior
1568 * nodes which have had all slots visited. If a node (leaf or
1569 * interior) is freed, the node above it will have it's slot
1570 * incremented. The root node will never be freed.
1571 *
1572 * At the end of this function, we should have a path which has all
1573 * slots incremented to the next position for a search. If we need to
1574 * read a new node it will be NULL and the node above it will have the
1575 * correct slot selected for a later read.
1576 *
1577 * If we increment the root nodes slot counter past the number of
1578 * elements, 1 is returned to signal completion of the search.
1579 */
David Sterba15b34512017-02-10 20:30:23 +01001580static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
Qu Wenruo33d1f052016-10-18 09:31:28 +08001581{
1582 int level = 0;
1583 int nr, slot;
1584 struct extent_buffer *eb;
1585
1586 if (root_level == 0)
1587 return 1;
1588
1589 while (level <= root_level) {
1590 eb = path->nodes[level];
1591 nr = btrfs_header_nritems(eb);
1592 path->slots[level]++;
1593 slot = path->slots[level];
1594 if (slot >= nr || level == 0) {
1595 /*
1596 * Don't free the root - we will detect this
1597 * condition after our loop and return a
1598 * positive value for caller to stop walking the tree.
1599 */
1600 if (level != root_level) {
1601 btrfs_tree_unlock_rw(eb, path->locks[level]);
1602 path->locks[level] = 0;
1603
1604 free_extent_buffer(eb);
1605 path->nodes[level] = NULL;
1606 path->slots[level] = 0;
1607 }
1608 } else {
1609 /*
1610 * We have a valid slot to walk back down
1611 * from. Stop here so caller can process these
1612 * new nodes.
1613 */
1614 break;
1615 }
1616
1617 level++;
1618 }
1619
1620 eb = path->nodes[root_level];
1621 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1622 return 1;
1623
1624 return 0;
1625}
1626
1627int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1628 struct btrfs_root *root,
1629 struct extent_buffer *root_eb,
1630 u64 root_gen, int root_level)
1631{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001632 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001633 int ret = 0;
1634 int level;
1635 struct extent_buffer *eb = root_eb;
1636 struct btrfs_path *path = NULL;
1637
1638 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
1639 BUG_ON(root_eb == NULL);
1640
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001641 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001642 return 0;
1643
1644 if (!extent_buffer_uptodate(root_eb)) {
1645 ret = btrfs_read_buffer(root_eb, root_gen);
1646 if (ret)
1647 goto out;
1648 }
1649
1650 if (root_level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001651 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001652 goto out;
1653 }
1654
1655 path = btrfs_alloc_path();
1656 if (!path)
1657 return -ENOMEM;
1658
1659 /*
1660 * Walk down the tree. Missing extent blocks are filled in as
1661 * we go. Metadata is accounted every time we read a new
1662 * extent block.
1663 *
1664 * When we reach a leaf, we account for file extent items in it,
1665 * walk back up the tree (adjusting slot pointers as we go)
1666 * and restart the search process.
1667 */
1668 extent_buffer_get(root_eb); /* For path */
1669 path->nodes[root_level] = root_eb;
1670 path->slots[root_level] = 0;
1671 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1672walk_down:
1673 level = root_level;
1674 while (level >= 0) {
1675 if (path->nodes[level] == NULL) {
1676 int parent_slot;
1677 u64 child_gen;
1678 u64 child_bytenr;
1679
1680 /*
1681 * We need to get child blockptr/gen from parent before
1682 * we can read it.
1683 */
1684 eb = path->nodes[level + 1];
1685 parent_slot = path->slots[level + 1];
1686 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1687 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1688
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001689 eb = read_tree_block(fs_info, child_bytenr, child_gen);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001690 if (IS_ERR(eb)) {
1691 ret = PTR_ERR(eb);
1692 goto out;
1693 } else if (!extent_buffer_uptodate(eb)) {
1694 free_extent_buffer(eb);
1695 ret = -EIO;
1696 goto out;
1697 }
1698
1699 path->nodes[level] = eb;
1700 path->slots[level] = 0;
1701
1702 btrfs_tree_read_lock(eb);
1703 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1704 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1705
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001706 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1707 child_bytenr,
1708 fs_info->nodesize,
1709 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001710 if (ret)
1711 goto out;
1712 }
1713
1714 if (level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001715 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1716 path->nodes[level]);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001717 if (ret)
1718 goto out;
1719
1720 /* Nonzero return here means we completed our search */
David Sterba15b34512017-02-10 20:30:23 +01001721 ret = adjust_slots_upwards(path, root_level);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001722 if (ret)
1723 break;
1724
1725 /* Restart search with new slots */
1726 goto walk_down;
1727 }
1728
1729 level--;
1730 }
1731
1732 ret = 0;
1733out:
1734 btrfs_free_path(path);
1735
1736 return ret;
1737}
1738
Qu Wenruod810ef22015-04-12 16:52:34 +08001739#define UPDATE_NEW 0
1740#define UPDATE_OLD 1
1741/*
1742 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1743 */
1744static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1745 struct ulist *roots, struct ulist *tmp,
1746 struct ulist *qgroups, u64 seq, int update_old)
1747{
1748 struct ulist_node *unode;
1749 struct ulist_iterator uiter;
1750 struct ulist_node *tmp_unode;
1751 struct ulist_iterator tmp_uiter;
1752 struct btrfs_qgroup *qg;
1753 int ret = 0;
1754
1755 if (!roots)
1756 return 0;
1757 ULIST_ITER_INIT(&uiter);
1758 while ((unode = ulist_next(roots, &uiter))) {
1759 qg = find_qgroup_rb(fs_info, unode->val);
1760 if (!qg)
1761 continue;
1762
1763 ulist_reinit(tmp);
David Sterbaef2fff62016-10-26 16:23:50 +02001764 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
Qu Wenruod810ef22015-04-12 16:52:34 +08001765 GFP_ATOMIC);
1766 if (ret < 0)
1767 return ret;
David Sterbaef2fff62016-10-26 16:23:50 +02001768 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
Qu Wenruod810ef22015-04-12 16:52:34 +08001769 if (ret < 0)
1770 return ret;
1771 ULIST_ITER_INIT(&tmp_uiter);
1772 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1773 struct btrfs_qgroup_list *glist;
1774
David Sterbaef2fff62016-10-26 16:23:50 +02001775 qg = unode_aux_to_qgroup(tmp_unode);
Qu Wenruod810ef22015-04-12 16:52:34 +08001776 if (update_old)
1777 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1778 else
1779 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1780 list_for_each_entry(glist, &qg->groups, next_group) {
1781 ret = ulist_add(qgroups, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001782 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001783 GFP_ATOMIC);
1784 if (ret < 0)
1785 return ret;
1786 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001787 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001788 GFP_ATOMIC);
1789 if (ret < 0)
1790 return ret;
1791 }
1792 }
1793 }
1794 return 0;
1795}
1796
Josef Bacikfcebe452014-05-13 17:30:47 -07001797/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001798 * Update qgroup rfer/excl counters.
1799 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08001800 *
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001801 * Excl update is tricky, the update is split into 2 part.
1802 * Part 1: Possible exclusive <-> sharing detect:
1803 * | A | !A |
1804 * -------------------------------------
1805 * B | * | - |
1806 * -------------------------------------
1807 * !B | + | ** |
1808 * -------------------------------------
1809 *
1810 * Conditions:
1811 * A: cur_old_roots < nr_old_roots (not exclusive before)
1812 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1813 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04001814 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001815 *
1816 * Results:
1817 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1818 * *: Definitely not changed. **: Possible unchanged.
1819 *
1820 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1821 *
1822 * To make the logic clear, we first use condition A and B to split
1823 * combination into 4 results.
1824 *
1825 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1826 * only on variant maybe 0.
1827 *
1828 * Lastly, check result **, since there are 2 variants maybe 0, split them
1829 * again(2x2).
1830 * But this time we don't need to consider other things, the codes and logic
1831 * is easy to understand now.
1832 */
1833static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1834 struct ulist *qgroups,
1835 u64 nr_old_roots,
1836 u64 nr_new_roots,
1837 u64 num_bytes, u64 seq)
1838{
1839 struct ulist_node *unode;
1840 struct ulist_iterator uiter;
1841 struct btrfs_qgroup *qg;
1842 u64 cur_new_count, cur_old_count;
1843
1844 ULIST_ITER_INIT(&uiter);
1845 while ((unode = ulist_next(qgroups, &uiter))) {
1846 bool dirty = false;
1847
David Sterbaef2fff62016-10-26 16:23:50 +02001848 qg = unode_aux_to_qgroup(unode);
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001849 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1850 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1851
Jeff Mahoneybc074522016-06-09 17:27:55 -04001852 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1853 cur_old_count, cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001854
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001855 /* Rfer update part */
1856 if (cur_old_count == 0 && cur_new_count > 0) {
1857 qg->rfer += num_bytes;
1858 qg->rfer_cmpr += num_bytes;
1859 dirty = true;
1860 }
1861 if (cur_old_count > 0 && cur_new_count == 0) {
1862 qg->rfer -= num_bytes;
1863 qg->rfer_cmpr -= num_bytes;
1864 dirty = true;
1865 }
1866
1867 /* Excl update part */
1868 /* Exclusive/none -> shared case */
1869 if (cur_old_count == nr_old_roots &&
1870 cur_new_count < nr_new_roots) {
1871 /* Exclusive -> shared */
1872 if (cur_old_count != 0) {
1873 qg->excl -= num_bytes;
1874 qg->excl_cmpr -= num_bytes;
1875 dirty = true;
1876 }
1877 }
1878
1879 /* Shared -> exclusive/none case */
1880 if (cur_old_count < nr_old_roots &&
1881 cur_new_count == nr_new_roots) {
1882 /* Shared->exclusive */
1883 if (cur_new_count != 0) {
1884 qg->excl += num_bytes;
1885 qg->excl_cmpr += num_bytes;
1886 dirty = true;
1887 }
1888 }
1889
1890 /* Exclusive/none -> exclusive/none case */
1891 if (cur_old_count == nr_old_roots &&
1892 cur_new_count == nr_new_roots) {
1893 if (cur_old_count == 0) {
1894 /* None -> exclusive/none */
1895
1896 if (cur_new_count != 0) {
1897 /* None -> exclusive */
1898 qg->excl += num_bytes;
1899 qg->excl_cmpr += num_bytes;
1900 dirty = true;
1901 }
1902 /* None -> none, nothing changed */
1903 } else {
1904 /* Exclusive -> exclusive/none */
1905
1906 if (cur_new_count == 0) {
1907 /* Exclusive -> none */
1908 qg->excl -= num_bytes;
1909 qg->excl_cmpr -= num_bytes;
1910 dirty = true;
1911 }
1912 /* Exclusive -> exclusive, nothing changed */
1913 }
1914 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08001915
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001916 if (dirty)
1917 qgroup_dirty(fs_info, qg);
1918 }
1919 return 0;
1920}
1921
Qu Wenruo442244c2015-04-16 17:18:36 +08001922int
Qu Wenruo550d7a22015-04-16 15:37:33 +08001923btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1924 struct btrfs_fs_info *fs_info,
1925 u64 bytenr, u64 num_bytes,
1926 struct ulist *old_roots, struct ulist *new_roots)
1927{
1928 struct ulist *qgroups = NULL;
1929 struct ulist *tmp = NULL;
1930 u64 seq;
1931 u64 nr_new_roots = 0;
1932 u64 nr_old_roots = 0;
1933 int ret = 0;
1934
David Sterba81353d52017-02-13 14:05:24 +01001935 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1936 return 0;
1937
Qu Wenruo550d7a22015-04-16 15:37:33 +08001938 if (new_roots)
1939 nr_new_roots = new_roots->nnodes;
1940 if (old_roots)
1941 nr_old_roots = old_roots->nnodes;
1942
Qu Wenruo550d7a22015-04-16 15:37:33 +08001943 BUG_ON(!fs_info->quota_root);
1944
Jeff Mahoneybc074522016-06-09 17:27:55 -04001945 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
1946 nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001947
Qu Wenruo550d7a22015-04-16 15:37:33 +08001948 qgroups = ulist_alloc(GFP_NOFS);
1949 if (!qgroups) {
1950 ret = -ENOMEM;
1951 goto out_free;
1952 }
1953 tmp = ulist_alloc(GFP_NOFS);
1954 if (!tmp) {
1955 ret = -ENOMEM;
1956 goto out_free;
1957 }
1958
1959 mutex_lock(&fs_info->qgroup_rescan_lock);
1960 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1961 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
1962 mutex_unlock(&fs_info->qgroup_rescan_lock);
1963 ret = 0;
1964 goto out_free;
1965 }
1966 }
1967 mutex_unlock(&fs_info->qgroup_rescan_lock);
1968
1969 spin_lock(&fs_info->qgroup_lock);
1970 seq = fs_info->qgroup_seq;
1971
1972 /* Update old refcnts using old_roots */
1973 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
1974 UPDATE_OLD);
1975 if (ret < 0)
1976 goto out;
1977
1978 /* Update new refcnts using new_roots */
1979 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
1980 UPDATE_NEW);
1981 if (ret < 0)
1982 goto out;
1983
1984 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
1985 num_bytes, seq);
1986
1987 /*
1988 * Bump qgroup_seq to avoid seq overlap
1989 */
1990 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
1991out:
1992 spin_unlock(&fs_info->qgroup_lock);
1993out_free:
1994 ulist_free(tmp);
1995 ulist_free(qgroups);
1996 ulist_free(old_roots);
1997 ulist_free(new_roots);
1998 return ret;
1999}
2000
2001int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
2002 struct btrfs_fs_info *fs_info)
2003{
2004 struct btrfs_qgroup_extent_record *record;
2005 struct btrfs_delayed_ref_root *delayed_refs;
2006 struct ulist *new_roots = NULL;
2007 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08002008 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002009 int ret = 0;
2010
2011 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08002012 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002013 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2014 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2015 node);
2016
Jeff Mahoneybc074522016-06-09 17:27:55 -04002017 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002018
Qu Wenruo550d7a22015-04-16 15:37:33 +08002019 if (!ret) {
2020 /*
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002021 * Use SEQ_LAST as time_seq to do special search, which
Qu Wenruo550d7a22015-04-16 15:37:33 +08002022 * doesn't lock tree or delayed_refs and search current
2023 * root. It's safe inside commit_transaction().
2024 */
2025 ret = btrfs_find_all_roots(trans, fs_info,
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002026 record->bytenr, SEQ_LAST, &new_roots);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002027 if (ret < 0)
2028 goto cleanup;
Qu Wenruo9086db82015-04-20 09:53:50 +08002029 if (qgroup_to_skip)
2030 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002031 ret = btrfs_qgroup_account_extent(trans, fs_info,
2032 record->bytenr, record->num_bytes,
2033 record->old_roots, new_roots);
2034 record->old_roots = NULL;
2035 new_roots = NULL;
2036 }
2037cleanup:
2038 ulist_free(record->old_roots);
2039 ulist_free(new_roots);
2040 new_roots = NULL;
2041 rb_erase(node, &delayed_refs->dirty_extent_root);
2042 kfree(record);
2043
2044 }
2045 return ret;
2046}
2047
Josef Bacikfcebe452014-05-13 17:30:47 -07002048/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002049 * called from commit_transaction. Writes all changed qgroups to disk.
2050 */
2051int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2052 struct btrfs_fs_info *fs_info)
2053{
2054 struct btrfs_root *quota_root = fs_info->quota_root;
2055 int ret = 0;
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002056 int start_rescan_worker = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002057
2058 if (!quota_root)
2059 goto out;
2060
Josef Bacikafcdd122016-09-02 15:40:02 -04002061 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
2062 test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002063 start_rescan_worker = 1;
2064
Josef Bacikafcdd122016-09-02 15:40:02 -04002065 if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
2066 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
2067 if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
2068 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002069
2070 spin_lock(&fs_info->qgroup_lock);
2071 while (!list_empty(&fs_info->dirty_qgroups)) {
2072 struct btrfs_qgroup *qgroup;
2073 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2074 struct btrfs_qgroup, dirty);
2075 list_del_init(&qgroup->dirty);
2076 spin_unlock(&fs_info->qgroup_lock);
2077 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2078 if (ret)
2079 fs_info->qgroup_flags |=
2080 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05002081 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2082 if (ret)
2083 fs_info->qgroup_flags |=
2084 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002085 spin_lock(&fs_info->qgroup_lock);
2086 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002087 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02002088 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2089 else
2090 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2091 spin_unlock(&fs_info->qgroup_lock);
2092
2093 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2094 if (ret)
2095 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2096
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002097 if (!ret && start_rescan_worker) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002098 ret = qgroup_rescan_init(fs_info, 0, 1);
2099 if (!ret) {
2100 qgroup_rescan_zero_tracking(fs_info);
Qu Wenruofc97fab2014-02-28 10:46:16 +08002101 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2102 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002103 }
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002104 ret = 0;
2105 }
2106
Arne Jansenbed92ea2012-06-28 18:03:02 +02002107out:
2108
2109 return ret;
2110}
2111
2112/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002113 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002114 * when a snapshot or a subvolume is created. Throwing an error will
2115 * cause a transaction abort so we take extra care here to only error
2116 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02002117 */
2118int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2119 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2120 struct btrfs_qgroup_inherit *inherit)
2121{
2122 int ret = 0;
2123 int i;
2124 u64 *i_qgroups;
2125 struct btrfs_root *quota_root = fs_info->quota_root;
2126 struct btrfs_qgroup *srcgroup;
2127 struct btrfs_qgroup *dstgroup;
2128 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002129 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002130
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002131 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04002132 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002133 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002134
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002135 if (!quota_root) {
2136 ret = -EINVAL;
2137 goto out;
2138 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002139
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002140 if (inherit) {
2141 i_qgroups = (u64 *)(inherit + 1);
2142 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2143 2 * inherit->num_excl_copies;
2144 for (i = 0; i < nums; ++i) {
2145 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05002146
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002147 /*
2148 * Zero out invalid groups so we can ignore
2149 * them later.
2150 */
2151 if (!srcgroup ||
2152 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2153 *i_qgroups = 0ULL;
2154
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002155 ++i_qgroups;
2156 }
2157 }
2158
Arne Jansenbed92ea2012-06-28 18:03:02 +02002159 /*
2160 * create a tracking group for the subvol itself
2161 */
2162 ret = add_qgroup_item(trans, quota_root, objectid);
2163 if (ret)
2164 goto out;
2165
Arne Jansenbed92ea2012-06-28 18:03:02 +02002166 if (srcid) {
2167 struct btrfs_root *srcroot;
2168 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002169
2170 srckey.objectid = srcid;
2171 srckey.type = BTRFS_ROOT_ITEM_KEY;
2172 srckey.offset = (u64)-1;
2173 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2174 if (IS_ERR(srcroot)) {
2175 ret = PTR_ERR(srcroot);
2176 goto out;
2177 }
2178
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002179 level_size = fs_info->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002180 }
2181
2182 /*
2183 * add qgroup to all inherited groups
2184 */
2185 if (inherit) {
2186 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002187 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2188 if (*i_qgroups == 0)
2189 continue;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002190 ret = add_qgroup_relation_item(trans, quota_root,
2191 objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002192 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002193 goto out;
2194 ret = add_qgroup_relation_item(trans, quota_root,
2195 *i_qgroups, objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002196 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002197 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002198 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002199 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002200 }
2201
2202
2203 spin_lock(&fs_info->qgroup_lock);
2204
2205 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002206 if (IS_ERR(dstgroup)) {
2207 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002208 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002209 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002210
Dongsheng Yange8c85412014-11-20 20:58:34 -05002211 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05002212 dstgroup->lim_flags = inherit->lim.flags;
2213 dstgroup->max_rfer = inherit->lim.max_rfer;
2214 dstgroup->max_excl = inherit->lim.max_excl;
2215 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2216 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05002217
2218 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2219 if (ret) {
2220 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002221 btrfs_info(fs_info,
2222 "unable to update quota limit for %llu",
2223 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002224 goto unlock;
2225 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002226 }
2227
Arne Jansenbed92ea2012-06-28 18:03:02 +02002228 if (srcid) {
2229 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002230 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002231 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002232
2233 /*
2234 * We call inherit after we clone the root in order to make sure
2235 * our counts don't go crazy, so at this point the only
2236 * difference between the two roots should be the root node.
2237 */
2238 dstgroup->rfer = srcgroup->rfer;
2239 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2240 dstgroup->excl = level_size;
2241 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002242 srcgroup->excl = level_size;
2243 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002244
2245 /* inherit the limit info */
2246 dstgroup->lim_flags = srcgroup->lim_flags;
2247 dstgroup->max_rfer = srcgroup->max_rfer;
2248 dstgroup->max_excl = srcgroup->max_excl;
2249 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2250 dstgroup->rsv_excl = srcgroup->rsv_excl;
2251
Arne Jansenbed92ea2012-06-28 18:03:02 +02002252 qgroup_dirty(fs_info, dstgroup);
2253 qgroup_dirty(fs_info, srcgroup);
2254 }
2255
Chris Masonf3a87f12012-09-14 20:06:30 -04002256 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002257 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002258
2259 i_qgroups = (u64 *)(inherit + 1);
2260 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002261 if (*i_qgroups) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002262 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002263 if (ret)
2264 goto unlock;
2265 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002266 ++i_qgroups;
2267 }
2268
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002269 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002270 struct btrfs_qgroup *src;
2271 struct btrfs_qgroup *dst;
2272
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002273 if (!i_qgroups[0] || !i_qgroups[1])
2274 continue;
2275
Arne Jansenbed92ea2012-06-28 18:03:02 +02002276 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2277 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2278
2279 if (!src || !dst) {
2280 ret = -EINVAL;
2281 goto unlock;
2282 }
2283
2284 dst->rfer = src->rfer - level_size;
2285 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002286 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002287 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002288 struct btrfs_qgroup *src;
2289 struct btrfs_qgroup *dst;
2290
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002291 if (!i_qgroups[0] || !i_qgroups[1])
2292 continue;
2293
Arne Jansenbed92ea2012-06-28 18:03:02 +02002294 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2295 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2296
2297 if (!src || !dst) {
2298 ret = -EINVAL;
2299 goto unlock;
2300 }
2301
2302 dst->excl = src->excl + level_size;
2303 dst->excl_cmpr = src->excl_cmpr + level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002304 }
2305
2306unlock:
2307 spin_unlock(&fs_info->qgroup_lock);
2308out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002309 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002310 return ret;
2311}
2312
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002313static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2314{
2315 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2316 qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
2317 return false;
2318
2319 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2320 qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
2321 return false;
2322
2323 return true;
2324}
2325
2326static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002327{
2328 struct btrfs_root *quota_root;
2329 struct btrfs_qgroup *qgroup;
2330 struct btrfs_fs_info *fs_info = root->fs_info;
2331 u64 ref_root = root->root_key.objectid;
2332 int ret = 0;
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002333 int retried = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002334 struct ulist_node *unode;
2335 struct ulist_iterator uiter;
2336
2337 if (!is_fstree(ref_root))
2338 return 0;
2339
2340 if (num_bytes == 0)
2341 return 0;
Sargun Dhillonf29efe22017-05-11 21:17:33 +00002342
2343 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2344 capable(CAP_SYS_RESOURCE))
2345 enforce = false;
2346
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002347retry:
Arne Jansenbed92ea2012-06-28 18:03:02 +02002348 spin_lock(&fs_info->qgroup_lock);
2349 quota_root = fs_info->quota_root;
2350 if (!quota_root)
2351 goto out;
2352
2353 qgroup = find_qgroup_rb(fs_info, ref_root);
2354 if (!qgroup)
2355 goto out;
2356
2357 /*
2358 * in a first step, we check all affected qgroups if any limits would
2359 * be exceeded
2360 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002361 ulist_reinit(fs_info->qgroup_ulist);
2362 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002363 (uintptr_t)qgroup, GFP_ATOMIC);
2364 if (ret < 0)
2365 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002366 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002367 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002368 struct btrfs_qgroup *qg;
2369 struct btrfs_qgroup_list *glist;
2370
David Sterbaef2fff62016-10-26 16:23:50 +02002371 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002372
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002373 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002374 /*
2375 * Commit the tree and retry, since we may have
2376 * deletions which would free up space.
2377 */
2378 if (!retried && qg->reserved > 0) {
2379 struct btrfs_trans_handle *trans;
2380
2381 spin_unlock(&fs_info->qgroup_lock);
2382 ret = btrfs_start_delalloc_inodes(root, 0);
2383 if (ret)
2384 return ret;
2385 btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
2386 trans = btrfs_join_transaction(root);
2387 if (IS_ERR(trans))
2388 return PTR_ERR(trans);
2389 ret = btrfs_commit_transaction(trans);
2390 if (ret)
2391 return ret;
2392 retried++;
2393 goto retry;
2394 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002395 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002396 goto out;
2397 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002398
2399 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002400 ret = ulist_add(fs_info->qgroup_ulist,
2401 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002402 (uintptr_t)glist->group, GFP_ATOMIC);
2403 if (ret < 0)
2404 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002405 }
2406 }
Wang Shilong3c971852013-04-17 14:00:36 +00002407 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002408 /*
2409 * no limits exceeded, now record the reservation into all qgroups
2410 */
2411 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002412 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002413 struct btrfs_qgroup *qg;
2414
David Sterbaef2fff62016-10-26 16:23:50 +02002415 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002416
Qu Wenruo3159fe72017-03-13 15:52:08 +08002417 trace_qgroup_update_reserve(fs_info, qg, num_bytes);
Dongsheng Yange2d1f922015-02-06 10:26:52 -05002418 qg->reserved += num_bytes;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002419 }
2420
2421out:
2422 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002423 return ret;
2424}
2425
Qu Wenruo297d7502015-09-08 17:08:37 +08002426void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2427 u64 ref_root, u64 num_bytes)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002428{
2429 struct btrfs_root *quota_root;
2430 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002431 struct ulist_node *unode;
2432 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00002433 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002434
2435 if (!is_fstree(ref_root))
2436 return;
2437
2438 if (num_bytes == 0)
2439 return;
2440
2441 spin_lock(&fs_info->qgroup_lock);
2442
2443 quota_root = fs_info->quota_root;
2444 if (!quota_root)
2445 goto out;
2446
2447 qgroup = find_qgroup_rb(fs_info, ref_root);
2448 if (!qgroup)
2449 goto out;
2450
Wang Shilong1e8f9152013-05-06 11:03:27 +00002451 ulist_reinit(fs_info->qgroup_ulist);
2452 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002453 (uintptr_t)qgroup, GFP_ATOMIC);
2454 if (ret < 0)
2455 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002456 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002457 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002458 struct btrfs_qgroup *qg;
2459 struct btrfs_qgroup_list *glist;
2460
David Sterbaef2fff62016-10-26 16:23:50 +02002461 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002462
Qu Wenruo3159fe72017-03-13 15:52:08 +08002463 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02002464 if (qg->reserved < num_bytes)
Qu Wenruo18dc22c2016-10-20 10:28:41 +08002465 report_reserved_underflow(fs_info, qg, num_bytes);
2466 else
2467 qg->reserved -= num_bytes;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002468
2469 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002470 ret = ulist_add(fs_info->qgroup_ulist,
2471 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002472 (uintptr_t)glist->group, GFP_ATOMIC);
2473 if (ret < 0)
2474 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002475 }
2476 }
2477
2478out:
2479 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002480}
2481
Jan Schmidt2f232032013-04-25 16:04:51 +00002482/*
2483 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08002484 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00002485 */
2486static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002487qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002488 struct btrfs_trans_handle *trans)
Jan Schmidt2f232032013-04-25 16:04:51 +00002489{
2490 struct btrfs_key found;
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002491 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002492 struct ulist *roots = NULL;
David Sterba3284da72015-02-25 15:47:32 +01002493 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Josef Bacikfcebe452014-05-13 17:30:47 -07002494 u64 num_bytes;
Jan Schmidt2f232032013-04-25 16:04:51 +00002495 int slot;
2496 int ret;
2497
Jan Schmidt2f232032013-04-25 16:04:51 +00002498 mutex_lock(&fs_info->qgroup_rescan_lock);
2499 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2500 &fs_info->qgroup_rescan_progress,
2501 path, 1, 0);
2502
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002503 btrfs_debug(fs_info,
2504 "current progress key (%llu %u %llu), search_slot ret %d",
2505 fs_info->qgroup_rescan_progress.objectid,
2506 fs_info->qgroup_rescan_progress.type,
2507 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002508
2509 if (ret) {
2510 /*
2511 * The rescan is about to end, we will not be scanning any
2512 * further blocks. We cannot unset the RESCAN flag here, because
2513 * we want to commit the transaction if everything went well.
2514 * To make the live accounting work in this phase, we set our
2515 * scan progress pointer such that every real extent objectid
2516 * will be smaller.
2517 */
2518 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2519 btrfs_release_path(path);
2520 mutex_unlock(&fs_info->qgroup_rescan_lock);
2521 return ret;
2522 }
2523
2524 btrfs_item_key_to_cpu(path->nodes[0], &found,
2525 btrfs_header_nritems(path->nodes[0]) - 1);
2526 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2527
2528 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002529 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2530 if (!scratch_leaf) {
2531 ret = -ENOMEM;
2532 mutex_unlock(&fs_info->qgroup_rescan_lock);
2533 goto out;
2534 }
2535 extent_buffer_get(scratch_leaf);
2536 btrfs_tree_read_lock(scratch_leaf);
2537 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
Jan Schmidt2f232032013-04-25 16:04:51 +00002538 slot = path->slots[0];
2539 btrfs_release_path(path);
2540 mutex_unlock(&fs_info->qgroup_rescan_lock);
2541
2542 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2543 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002544 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2545 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002546 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002547 if (found.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04002548 num_bytes = fs_info->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002549 else
2550 num_bytes = found.offset;
2551
Josef Bacikfcebe452014-05-13 17:30:47 -07002552 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2553 &roots);
Jan Schmidt2f232032013-04-25 16:04:51 +00002554 if (ret < 0)
2555 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08002556 /* For rescan, just pass old_roots as NULL */
2557 ret = btrfs_qgroup_account_extent(trans, fs_info,
2558 found.objectid, num_bytes, NULL, roots);
2559 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00002560 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002561 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002562out:
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002563 if (scratch_leaf) {
2564 btrfs_tree_read_unlock_blocking(scratch_leaf);
2565 free_extent_buffer(scratch_leaf);
2566 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002567 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2568
2569 return ret;
2570}
2571
Qu Wenruod458b052014-02-28 10:46:19 +08002572static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002573{
Jan Schmidtb382a322013-05-28 15:47:24 +00002574 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2575 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002576 struct btrfs_path *path;
2577 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002578 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002579 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002580
2581 path = btrfs_alloc_path();
2582 if (!path)
2583 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002584
2585 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08002586 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002587 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2588 if (IS_ERR(trans)) {
2589 err = PTR_ERR(trans);
2590 break;
2591 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002592 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002593 err = -EINTR;
2594 } else {
Qu Wenruo0a0e8b892015-10-26 09:19:43 +08002595 err = qgroup_rescan_leaf(fs_info, path, trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002596 }
2597 if (err > 0)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002598 btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002599 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002600 btrfs_end_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002601 }
2602
2603out:
Jan Schmidt2f232032013-04-25 16:04:51 +00002604 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002605
2606 mutex_lock(&fs_info->qgroup_rescan_lock);
Justin Maggard7343dd62015-11-04 15:56:16 -08002607 if (!btrfs_fs_closing(fs_info))
2608 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidt2f232032013-04-25 16:04:51 +00002609
Qu Wenruo33931682015-02-27 16:24:24 +08002610 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00002611 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2612 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2613 } else if (err < 0) {
2614 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2615 }
2616 mutex_unlock(&fs_info->qgroup_rescan_lock);
2617
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002618 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002619 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002620 * qgroup info.
2621 */
2622 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2623 if (IS_ERR(trans)) {
2624 err = PTR_ERR(trans);
2625 btrfs_err(fs_info,
2626 "fail to start transaction for status update: %d\n",
2627 err);
2628 goto done;
2629 }
2630 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2631 if (ret < 0) {
2632 err = ret;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002633 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002634 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002635 btrfs_end_transaction(trans);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002636
Justin Maggard7343dd62015-11-04 15:56:16 -08002637 if (btrfs_fs_closing(fs_info)) {
2638 btrfs_info(fs_info, "qgroup scan paused");
2639 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002640 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08002641 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00002642 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002643 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002644 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002645
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002646done:
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002647 mutex_lock(&fs_info->qgroup_rescan_lock);
2648 fs_info->qgroup_rescan_running = false;
2649 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002650 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002651}
2652
Jan Schmidtb382a322013-05-28 15:47:24 +00002653/*
2654 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2655 * memory required for the rescan context.
2656 */
2657static int
2658qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2659 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002660{
2661 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002662
Jan Schmidtb382a322013-05-28 15:47:24 +00002663 if (!init_flags &&
2664 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2665 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2666 ret = -EINVAL;
2667 goto err;
2668 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002669
2670 mutex_lock(&fs_info->qgroup_rescan_lock);
2671 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002672
2673 if (init_flags) {
2674 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2675 ret = -EINPROGRESS;
2676 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2677 ret = -EINVAL;
2678
2679 if (ret) {
2680 spin_unlock(&fs_info->qgroup_lock);
2681 mutex_unlock(&fs_info->qgroup_rescan_lock);
2682 goto err;
2683 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002684 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2685 }
2686
2687 memset(&fs_info->qgroup_rescan_progress, 0,
2688 sizeof(fs_info->qgroup_rescan_progress));
2689 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00002690 init_completion(&fs_info->qgroup_rescan_completion);
Filipe Manana8d9edda2016-11-24 02:09:04 +00002691 fs_info->qgroup_rescan_running = true;
Jan Schmidtb382a322013-05-28 15:47:24 +00002692
2693 spin_unlock(&fs_info->qgroup_lock);
2694 mutex_unlock(&fs_info->qgroup_rescan_lock);
2695
Jan Schmidtb382a322013-05-28 15:47:24 +00002696 memset(&fs_info->qgroup_rescan_work, 0,
2697 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002698 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002699 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002700 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002701
Jan Schmidt2f232032013-04-25 16:04:51 +00002702 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002703err:
Frank Holtonefe120a2013-12-20 11:37:06 -05002704 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002705 return ret;
2706 }
2707
Jan Schmidtb382a322013-05-28 15:47:24 +00002708 return 0;
2709}
Jan Schmidt2f232032013-04-25 16:04:51 +00002710
Jan Schmidtb382a322013-05-28 15:47:24 +00002711static void
2712qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2713{
2714 struct rb_node *n;
2715 struct btrfs_qgroup *qgroup;
2716
2717 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002718 /* clear all current qgroup tracking information */
2719 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2720 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2721 qgroup->rfer = 0;
2722 qgroup->rfer_cmpr = 0;
2723 qgroup->excl = 0;
2724 qgroup->excl_cmpr = 0;
2725 }
2726 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002727}
Jan Schmidt2f232032013-04-25 16:04:51 +00002728
Jan Schmidtb382a322013-05-28 15:47:24 +00002729int
2730btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2731{
2732 int ret = 0;
2733 struct btrfs_trans_handle *trans;
2734
2735 ret = qgroup_rescan_init(fs_info, 0, 1);
2736 if (ret)
2737 return ret;
2738
2739 /*
2740 * We have set the rescan_progress to 0, which means no more
2741 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2742 * However, btrfs_qgroup_account_ref may be right after its call
2743 * to btrfs_find_all_roots, in which case it would still do the
2744 * accounting.
2745 * To solve this, we're committing the transaction, which will
2746 * ensure we run all delayed refs and only after that, we are
2747 * going to clear all tracking information for a clean start.
2748 */
2749
2750 trans = btrfs_join_transaction(fs_info->fs_root);
2751 if (IS_ERR(trans)) {
2752 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2753 return PTR_ERR(trans);
2754 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002755 ret = btrfs_commit_transaction(trans);
Jan Schmidtb382a322013-05-28 15:47:24 +00002756 if (ret) {
2757 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2758 return ret;
2759 }
2760
2761 qgroup_rescan_zero_tracking(fs_info);
2762
Qu Wenruofc97fab2014-02-28 10:46:16 +08002763 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2764 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002765
2766 return 0;
2767}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002768
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002769int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2770 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002771{
2772 int running;
2773 int ret = 0;
2774
2775 mutex_lock(&fs_info->qgroup_rescan_lock);
2776 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002777 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002778 spin_unlock(&fs_info->qgroup_lock);
2779 mutex_unlock(&fs_info->qgroup_rescan_lock);
2780
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002781 if (!running)
2782 return 0;
2783
2784 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002785 ret = wait_for_completion_interruptible(
2786 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002787 else
2788 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002789
2790 return ret;
2791}
Jan Schmidtb382a322013-05-28 15:47:24 +00002792
2793/*
2794 * this is only called from open_ctree where we're still single threaded, thus
2795 * locking is omitted here.
2796 */
2797void
2798btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2799{
2800 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002801 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2802 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002803}
Qu Wenruo52472552015-10-12 16:05:40 +08002804
2805/*
2806 * Reserve qgroup space for range [start, start + len).
2807 *
2808 * This function will either reserve space from related qgroups or doing
2809 * nothing if the range is already reserved.
2810 *
2811 * Return 0 for successful reserve
2812 * Return <0 for error (including -EQUOT)
2813 *
2814 * NOTE: this function may sleep for memory allocation.
2815 */
2816int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len)
2817{
2818 struct btrfs_root *root = BTRFS_I(inode)->root;
2819 struct extent_changeset changeset;
2820 struct ulist_node *unode;
2821 struct ulist_iterator uiter;
2822 int ret;
2823
Josef Bacikafcdd122016-09-02 15:40:02 -04002824 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2825 !is_fstree(root->objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08002826 return 0;
2827
2828 changeset.bytes_changed = 0;
David Sterba53d32352017-02-13 13:42:29 +01002829 ulist_init(&changeset.range_changed);
Qu Wenruo52472552015-10-12 16:05:40 +08002830 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterba2c53b912016-04-26 23:54:39 +02002831 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002832 trace_btrfs_qgroup_reserve_data(inode, start, len,
2833 changeset.bytes_changed,
2834 QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08002835 if (ret < 0)
2836 goto cleanup;
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002837 ret = qgroup_reserve(root, changeset.bytes_changed, true);
Qu Wenruo52472552015-10-12 16:05:40 +08002838 if (ret < 0)
2839 goto cleanup;
2840
David Sterba6655bc32017-02-15 16:47:36 +01002841 ulist_release(&changeset.range_changed);
Qu Wenruo52472552015-10-12 16:05:40 +08002842 return ret;
2843
2844cleanup:
2845 /* cleanup already reserved ranges */
2846 ULIST_ITER_INIT(&uiter);
David Sterba53d32352017-02-13 13:42:29 +01002847 while ((unode = ulist_next(&changeset.range_changed, &uiter)))
Qu Wenruo52472552015-10-12 16:05:40 +08002848 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2849 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
2850 GFP_NOFS);
David Sterba6655bc32017-02-15 16:47:36 +01002851 ulist_release(&changeset.range_changed);
Qu Wenruo52472552015-10-12 16:05:40 +08002852 return ret;
2853}
Qu Wenruof695fdc2015-10-12 16:28:06 +08002854
2855static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len,
2856 int free)
2857{
2858 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002859 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08002860 int ret;
2861
2862 changeset.bytes_changed = 0;
David Sterba53d32352017-02-13 13:42:29 +01002863 ulist_init(&changeset.range_changed);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002864 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterbaf734c442016-04-26 23:54:39 +02002865 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002866 if (ret < 0)
2867 goto out;
2868
Qu Wenruod51ea5d2017-03-13 15:52:09 +08002869 if (free)
2870 trace_op = QGROUP_FREE;
2871 trace_btrfs_qgroup_release_data(inode, start, len,
2872 changeset.bytes_changed, trace_op);
2873 if (free)
David Sterba0b08e1f2017-02-13 14:24:35 +01002874 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
2875 BTRFS_I(inode)->root->objectid,
2876 changeset.bytes_changed);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002877out:
David Sterba6655bc32017-02-15 16:47:36 +01002878 ulist_release(&changeset.range_changed);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002879 return ret;
2880}
2881
2882/*
2883 * Free a reserved space range from io_tree and related qgroups
2884 *
2885 * Should be called when a range of pages get invalidated before reaching disk.
2886 * Or for error cleanup case.
2887 *
2888 * For data written to disk, use btrfs_qgroup_release_data().
2889 *
2890 * NOTE: This function may sleep for memory allocation.
2891 */
2892int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len)
2893{
2894 return __btrfs_qgroup_release_data(inode, start, len, 1);
2895}
2896
2897/*
2898 * Release a reserved space range from io_tree only.
2899 *
2900 * Should be called when a range of pages get written to disk and corresponding
2901 * FILE_EXTENT is inserted into corresponding root.
2902 *
2903 * Since new qgroup accounting framework will only update qgroup numbers at
2904 * commit_transaction() time, its reserved space shouldn't be freed from
2905 * related qgroups.
2906 *
2907 * But we should release the range from io_tree, to allow further write to be
2908 * COWed.
2909 *
2910 * NOTE: This function may sleep for memory allocation.
2911 */
2912int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
2913{
2914 return __btrfs_qgroup_release_data(inode, start, len, 0);
2915}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002916
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002917int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
2918 bool enforce)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002919{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002921 int ret;
2922
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002923 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04002924 !is_fstree(root->objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002925 return 0;
2926
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002927 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo3159fe72017-03-13 15:52:08 +08002928 trace_qgroup_meta_reserve(root, (s64)num_bytes);
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002929 ret = qgroup_reserve(root, num_bytes, enforce);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002930 if (ret < 0)
2931 return ret;
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05002932 atomic64_add(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002933 return ret;
2934}
2935
2936void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
2937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 struct btrfs_fs_info *fs_info = root->fs_info;
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05002939 u64 reserved;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002940
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002941 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04002942 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002943 return;
2944
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05002945 reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002946 if (reserved == 0)
2947 return;
Qu Wenruo3159fe72017-03-13 15:52:08 +08002948 trace_qgroup_meta_reserve(root, -(s64)reserved);
David Sterba0b08e1f2017-02-13 14:24:35 +01002949 btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002950}
2951
2952void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
2953{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002954 struct btrfs_fs_info *fs_info = root->fs_info;
2955
2956 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04002957 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002958 return;
2959
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002960 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05002961 WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
2962 atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo3159fe72017-03-13 15:52:08 +08002963 trace_qgroup_meta_reserve(root, -(s64)num_bytes);
David Sterba0b08e1f2017-02-13 14:24:35 +01002964 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002965}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002966
2967/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002968 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002969 * time
2970 */
2971void btrfs_qgroup_check_reserved_leak(struct inode *inode)
2972{
2973 struct extent_changeset changeset;
2974 struct ulist_node *unode;
2975 struct ulist_iterator iter;
2976 int ret;
2977
2978 changeset.bytes_changed = 0;
David Sterba53d32352017-02-13 13:42:29 +01002979 ulist_init(&changeset.range_changed);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002980 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02002981 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002982
2983 WARN_ON(ret < 0);
2984 if (WARN_ON(changeset.bytes_changed)) {
2985 ULIST_ITER_INIT(&iter);
David Sterba53d32352017-02-13 13:42:29 +01002986 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002987 btrfs_warn(BTRFS_I(inode)->root->fs_info,
2988 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
2989 inode->i_ino, unode->val, unode->aux);
2990 }
David Sterba0b08e1f2017-02-13 14:24:35 +01002991 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
2992 BTRFS_I(inode)->root->objectid,
2993 changeset.bytes_changed);
2994
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002995 }
David Sterba6655bc32017-02-15 16:47:36 +01002996 ulist_release(&changeset.range_changed);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002997}