Commit | Line | Data |
---|---|---|
bed92eae AJ |
1 | /* |
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> | |
55e301fd | 26 | #include <linux/btrfs.h> |
bed92eae AJ |
27 | |
28 | #include "ctree.h" | |
29 | #include "transaction.h" | |
30 | #include "disk-io.h" | |
31 | #include "locking.h" | |
32 | #include "ulist.h" | |
bed92eae | 33 | #include "backref.h" |
2f232036 | 34 | #include "extent_io.h" |
fcebe456 | 35 | #include "qgroup.h" |
bed92eae | 36 | |
e69bcee3 | 37 | |
bed92eae AJ |
38 | /* TODO XXX FIXME |
39 | * - subvol delete -> delete when ref goes to 0? delete limits also? | |
40 | * - reorganize keys | |
41 | * - compressed | |
42 | * - sync | |
bed92eae AJ |
43 | * - copy also limits on subvol creation |
44 | * - limit | |
45 | * - caches fuer ulists | |
46 | * - performance benchmarks | |
47 | * - check all ioctl parameters | |
48 | */ | |
49 | ||
9c542136 QW |
50 | static 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 | ||
58 | static 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 | ||
66 | static 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 | ||
73 | static 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 | ||
bed92eae AJ |
80 | /* |
81 | * glue structure to represent the relations between qgroups. | |
82 | */ | |
83 | struct 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 | ||
ef2fff64 DS |
90 | static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg) |
91 | { | |
92 | return (u64)(uintptr_t)qg; | |
93 | } | |
94 | ||
95 | static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n) | |
96 | { | |
97 | return (struct btrfs_qgroup *)(uintptr_t)n->aux; | |
98 | } | |
fcebe456 | 99 | |
b382a324 JS |
100 | static int |
101 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, | |
102 | int init_flags); | |
103 | static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info); | |
2f232036 | 104 | |
58400fce | 105 | /* must be called with qgroup_ioctl_lock held */ |
bed92eae AJ |
106 | static 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 */ | |
125 | static 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 | ||
4082bd3d | 159 | static void __del_qgroup_rb(struct btrfs_qgroup *qgroup) |
bed92eae | 160 | { |
bed92eae AJ |
161 | struct btrfs_qgroup_list *list; |
162 | ||
bed92eae | 163 | list_del(&qgroup->dirty); |
bed92eae AJ |
164 | 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); | |
4082bd3d | 180 | } |
bed92eae | 181 | |
4082bd3d WS |
182 | /* must be called with qgroup_lock held */ |
183 | static 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); | |
bed92eae AJ |
192 | return 0; |
193 | } | |
194 | ||
195 | /* must be called with qgroup_lock held */ | |
196 | static 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 */ | |
221 | static 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 | ||
faa2dbf0 JB |
244 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
245 | int 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 | ||
bed92eae AJ |
259 | /* |
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 | */ | |
263 | int 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; | |
b382a324 | 273 | u64 rescan_progress = 0; |
bed92eae | 274 | |
afcdd129 | 275 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
bed92eae AJ |
276 | return 0; |
277 | ||
323b88f4 | 278 | fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL); |
1e8f9158 WS |
279 | if (!fs_info->qgroup_ulist) { |
280 | ret = -ENOMEM; | |
281 | goto out; | |
282 | } | |
283 | ||
bed92eae AJ |
284 | 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) { | |
efe120a0 FH |
318 | btrfs_err(fs_info, |
319 | "old qgroup version, quota disabled"); | |
bed92eae AJ |
320 | goto out; |
321 | } | |
322 | if (btrfs_qgroup_status_generation(l, ptr) != | |
323 | fs_info->generation) { | |
324 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
efe120a0 | 325 | btrfs_err(fs_info, |
5d163e0e | 326 | "qgroup generation mismatch, marked as inconsistent"); |
bed92eae AJ |
327 | } |
328 | fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, | |
329 | ptr); | |
b382a324 | 330 | rescan_progress = btrfs_qgroup_status_rescan(l, ptr); |
bed92eae AJ |
331 | 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)) { | |
d41e36a0 | 341 | btrfs_err(fs_info, "inconsistent qgroup config"); |
bed92eae AJ |
342 | 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 | } | |
377 | next1: | |
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); | |
ff24858c | 411 | if (ret == -ENOENT) { |
efe120a0 FH |
412 | btrfs_warn(fs_info, |
413 | "orphan qgroup relation 0x%llx->0x%llx", | |
c1c9ff7c | 414 | found_key.objectid, found_key.offset); |
ff24858c AJ |
415 | ret = 0; /* ignore the error */ |
416 | } | |
bed92eae AJ |
417 | if (ret) |
418 | goto out; | |
419 | next2: | |
420 | ret = btrfs_next_item(quota_root, path); | |
421 | if (ret < 0) | |
422 | goto out; | |
423 | if (ret) | |
424 | break; | |
425 | } | |
426 | out: | |
427 | fs_info->qgroup_flags |= flags; | |
afcdd129 JB |
428 | 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) | |
b382a324 | 432 | ret = qgroup_rescan_init(fs_info, rescan_progress, 0); |
bed92eae AJ |
433 | btrfs_free_path(path); |
434 | ||
eb1716af | 435 | if (ret < 0) { |
1e8f9158 | 436 | ulist_free(fs_info->qgroup_ulist); |
eb1716af | 437 | fs_info->qgroup_ulist = NULL; |
b382a324 | 438 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
eb1716af | 439 | } |
1e8f9158 | 440 | |
bed92eae AJ |
441 | return ret < 0 ? ret : 0; |
442 | } | |
443 | ||
444 | /* | |
e685da14 WS |
445 | * 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. | |
bed92eae AJ |
449 | */ |
450 | void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info) | |
451 | { | |
452 | struct rb_node *n; | |
453 | struct btrfs_qgroup *qgroup; | |
bed92eae AJ |
454 | |
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); | |
4082bd3d | 458 | __del_qgroup_rb(qgroup); |
bed92eae | 459 | } |
1e7bac1e WS |
460 | /* |
461 | * we call btrfs_free_qgroup_config() when umounting | |
01327610 | 462 | * filesystem and disabling quota, so we set qgroup_ulist |
1e7bac1e WS |
463 | * to be null here to avoid double free. |
464 | */ | |
1e8f9158 | 465 | ulist_free(fs_info->qgroup_ulist); |
1e7bac1e | 466 | fs_info->qgroup_ulist = NULL; |
bed92eae AJ |
467 | } |
468 | ||
469 | static 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 | ||
493 | static 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); | |
519 | out: | |
520 | btrfs_free_path(path); | |
521 | return ret; | |
522 | } | |
523 | ||
524 | static 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 | ||
f5ee5c9a | 534 | if (btrfs_is_testing(quota_root->fs_info)) |
faa2dbf0 | 535 | return 0; |
fccb84c9 | 536 | |
bed92eae AJ |
537 | 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 | ||
0b4699dc MF |
545 | /* |
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 | ||
bed92eae AJ |
551 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
552 | sizeof(*qgroup_info)); | |
0b4699dc | 553 | if (ret && ret != -EEXIST) |
bed92eae AJ |
554 | 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)); | |
0b4699dc | 572 | if (ret && ret != -EEXIST) |
bed92eae AJ |
573 | 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; | |
587 | out: | |
588 | btrfs_free_path(path); | |
589 | return ret; | |
590 | } | |
591 | ||
592 | static 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 | ||
633 | out: | |
634 | btrfs_free_path(path); | |
635 | return ret; | |
636 | } | |
637 | ||
638 | static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, | |
1510e71c DY |
639 | struct btrfs_root *root, |
640 | struct btrfs_qgroup *qgroup) | |
bed92eae AJ |
641 | { |
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; | |
1510e71c | 651 | key.offset = qgroup->qgroupid; |
bed92eae AJ |
652 | |
653 | path = btrfs_alloc_path(); | |
84cbe2f7 WS |
654 | if (!path) |
655 | return -ENOMEM; | |
656 | ||
bed92eae AJ |
657 | 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]; | |
a3df41ee | 666 | qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item); |
1510e71c DY |
667 | 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); | |
bed92eae AJ |
672 | |
673 | btrfs_mark_buffer_dirty(l); | |
674 | ||
675 | out: | |
676 | btrfs_free_path(path); | |
677 | return ret; | |
678 | } | |
679 | ||
680 | static 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 | ||
f5ee5c9a | 691 | if (btrfs_is_testing(root->fs_info)) |
faa2dbf0 | 692 | return 0; |
fccb84c9 | 693 | |
bed92eae AJ |
694 | key.objectid = 0; |
695 | key.type = BTRFS_QGROUP_INFO_KEY; | |
696 | key.offset = qgroup->qgroupid; | |
697 | ||
698 | path = btrfs_alloc_path(); | |
84cbe2f7 WS |
699 | if (!path) |
700 | return -ENOMEM; | |
701 | ||
bed92eae AJ |
702 | 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]; | |
a3df41ee | 711 | qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item); |
bed92eae AJ |
712 | 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 | ||
720 | out: | |
721 | btrfs_free_path(path); | |
722 | return ret; | |
723 | } | |
724 | ||
725 | static 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(); | |
84cbe2f7 WS |
741 | if (!path) |
742 | return -ENOMEM; | |
743 | ||
bed92eae AJ |
744 | 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); | |
2f232036 JS |
756 | btrfs_set_qgroup_status_rescan(l, ptr, |
757 | fs_info->qgroup_rescan_progress.objectid); | |
bed92eae AJ |
758 | |
759 | btrfs_mark_buffer_dirty(l); | |
760 | ||
761 | out: | |
762 | btrfs_free_path(path); | |
763 | return ret; | |
764 | } | |
765 | ||
766 | /* | |
767 | * called with qgroup_lock held | |
768 | */ | |
769 | static 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; | |
06b3a860 | 774 | struct extent_buffer *leaf = NULL; |
bed92eae | 775 | int ret; |
06b3a860 | 776 | int nr = 0; |
bed92eae | 777 | |
bed92eae AJ |
778 | path = btrfs_alloc_path(); |
779 | if (!path) | |
780 | return -ENOMEM; | |
781 | ||
06b3a860 WS |
782 | path->leave_spinning = 1; |
783 | ||
784 | key.objectid = 0; | |
785 | key.offset = 0; | |
786 | key.type = 0; | |
bed92eae | 787 | |
06b3a860 | 788 | while (1) { |
bed92eae | 789 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
06b3a860 WS |
790 | if (ret < 0) |
791 | goto out; | |
792 | leaf = path->nodes[0]; | |
793 | nr = btrfs_header_nritems(leaf); | |
794 | if (!nr) | |
bed92eae | 795 | break; |
06b3a860 WS |
796 | /* |
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); | |
bed92eae AJ |
803 | if (ret) |
804 | goto out; | |
06b3a860 | 805 | |
bed92eae AJ |
806 | btrfs_release_path(path); |
807 | } | |
808 | ret = 0; | |
809 | out: | |
afcdd129 | 810 | set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags); |
bed92eae AJ |
811 | btrfs_free_path(path); |
812 | return ret; | |
813 | } | |
814 | ||
815 | int btrfs_quota_enable(struct btrfs_trans_handle *trans, | |
816 | struct btrfs_fs_info *fs_info) | |
817 | { | |
818 | struct btrfs_root *quota_root; | |
7708f029 | 819 | struct btrfs_root *tree_root = fs_info->tree_root; |
bed92eae AJ |
820 | struct btrfs_path *path = NULL; |
821 | struct btrfs_qgroup_status_item *ptr; | |
822 | struct extent_buffer *leaf; | |
823 | struct btrfs_key key; | |
7708f029 WS |
824 | struct btrfs_key found_key; |
825 | struct btrfs_qgroup *qgroup = NULL; | |
bed92eae | 826 | int ret = 0; |
7708f029 | 827 | int slot; |
bed92eae | 828 | |
f2f6ed3d | 829 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 830 | if (fs_info->quota_root) { |
afcdd129 | 831 | set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags); |
bed92eae AJ |
832 | goto out; |
833 | } | |
bed92eae | 834 | |
52bf8e7a | 835 | fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL); |
1e8f9158 WS |
836 | if (!fs_info->qgroup_ulist) { |
837 | ret = -ENOMEM; | |
838 | goto out; | |
839 | } | |
840 | ||
bed92eae AJ |
841 | /* |
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(); | |
5b7ff5b3 TI |
852 | if (!path) { |
853 | ret = -ENOMEM; | |
854 | goto out_free_root; | |
855 | } | |
bed92eae AJ |
856 | |
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) | |
5b7ff5b3 | 864 | goto out_free_path; |
bed92eae AJ |
865 | |
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); | |
2f232036 | 874 | btrfs_set_qgroup_status_rescan(leaf, ptr, 0); |
bed92eae AJ |
875 | |
876 | btrfs_mark_buffer_dirty(leaf); | |
877 | ||
7708f029 WS |
878 | 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 | ||
7708f029 WS |
901 | qgroup = add_qgroup_rb(fs_info, found_key.offset); |
902 | if (IS_ERR(qgroup)) { | |
7708f029 WS |
903 | ret = PTR_ERR(qgroup); |
904 | goto out_free_path; | |
905 | } | |
7708f029 WS |
906 | } |
907 | ret = btrfs_next_item(tree_root, path); | |
908 | if (ret < 0) | |
909 | goto out_free_path; | |
910 | if (ret) | |
911 | break; | |
912 | } | |
913 | ||
914 | out_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 | ||
7708f029 WS |
920 | qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID); |
921 | if (IS_ERR(qgroup)) { | |
7708f029 WS |
922 | ret = PTR_ERR(qgroup); |
923 | goto out_free_path; | |
924 | } | |
58400fce | 925 | spin_lock(&fs_info->qgroup_lock); |
bed92eae | 926 | fs_info->quota_root = quota_root; |
afcdd129 | 927 | set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags); |
bed92eae | 928 | spin_unlock(&fs_info->qgroup_lock); |
5b7ff5b3 | 929 | out_free_path: |
bed92eae | 930 | btrfs_free_path(path); |
5b7ff5b3 TI |
931 | out_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 | } | |
937 | out: | |
eb1716af | 938 | if (ret) { |
1e8f9158 | 939 | ulist_free(fs_info->qgroup_ulist); |
eb1716af JS |
940 | fs_info->qgroup_ulist = NULL; |
941 | } | |
f2f6ed3d | 942 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
943 | return ret; |
944 | } | |
945 | ||
946 | int 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 | ||
f2f6ed3d | 953 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
58400fce | 954 | if (!fs_info->quota_root) |
f2f6ed3d | 955 | goto out; |
afcdd129 JB |
956 | clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); |
957 | set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags); | |
d06f23d6 | 958 | btrfs_qgroup_wait_for_completion(fs_info, false); |
967ef513 | 959 | spin_lock(&fs_info->qgroup_lock); |
bed92eae AJ |
960 | quota_root = fs_info->quota_root; |
961 | fs_info->quota_root = NULL; | |
8ea0ec9e | 962 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; |
bed92eae AJ |
963 | spin_unlock(&fs_info->qgroup_lock); |
964 | ||
e685da14 WS |
965 | btrfs_free_qgroup_config(fs_info); |
966 | ||
bed92eae AJ |
967 | ret = btrfs_clean_quota_tree(trans, quota_root); |
968 | if (ret) | |
969 | goto out; | |
970 | ||
971 | ret = btrfs_del_root(trans, tree_root, "a_root->root_key); | |
972 | if (ret) | |
973 | goto out; | |
974 | ||
975 | list_del("a_root->dirty_list); | |
976 | ||
977 | btrfs_tree_lock(quota_root->node); | |
7c302b49 | 978 | clean_tree_block(fs_info, quota_root->node); |
bed92eae AJ |
979 | 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); | |
985 | out: | |
f2f6ed3d | 986 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
987 | return ret; |
988 | } | |
989 | ||
2f232036 JS |
990 | static void qgroup_dirty(struct btrfs_fs_info *fs_info, |
991 | struct btrfs_qgroup *qgroup) | |
bed92eae | 992 | { |
2f232036 JS |
993 | if (list_empty(&qgroup->dirty)) |
994 | list_add(&qgroup->dirty, &fs_info->dirty_qgroups); | |
bed92eae AJ |
995 | } |
996 | ||
18dc22c1 QW |
997 | static void report_reserved_underflow(struct btrfs_fs_info *fs_info, |
998 | struct btrfs_qgroup *qgroup, | |
999 | u64 num_bytes) | |
1000 | { | |
338bd52f DS |
1001 | #ifdef CONFIG_BTRFS_DEBUG |
1002 | WARN_ON(qgroup->reserved < num_bytes); | |
1003 | btrfs_debug(fs_info, | |
18dc22c1 QW |
1004 | "qgroup %llu reserved space underflow, have: %llu, to free: %llu", |
1005 | qgroup->qgroupid, qgroup->reserved, num_bytes); | |
338bd52f | 1006 | #endif |
18dc22c1 QW |
1007 | qgroup->reserved = 0; |
1008 | } | |
9c8b35b1 QW |
1009 | /* |
1010 | * The easy accounting, if we are adding/removing the only ref for an extent | |
01327610 | 1011 | * then this qgroup and all of the parent qgroups get their reference and |
9c8b35b1 QW |
1012 | * exclusive counts adjusted. |
1013 | * | |
1014 | * Caller should hold fs_info->qgroup_lock. | |
1015 | */ | |
1016 | static 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; | |
18dc22c1 | 1036 | if (sign > 0) { |
3159fe7b | 1037 | trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes); |
338bd52f | 1038 | if (qgroup->reserved < num_bytes) |
18dc22c1 QW |
1039 | report_reserved_underflow(fs_info, qgroup, num_bytes); |
1040 | else | |
1041 | qgroup->reserved -= num_bytes; | |
1042 | } | |
9c8b35b1 QW |
1043 | |
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, | |
ef2fff64 | 1049 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
9c8b35b1 QW |
1050 | 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))) { | |
ef2fff64 | 1057 | qgroup = unode_aux_to_qgroup(unode); |
9c8b35b1 QW |
1058 | 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; | |
18dc22c1 | 1062 | if (sign > 0) { |
3159fe7b QW |
1063 | trace_qgroup_update_reserve(fs_info, qgroup, |
1064 | -(s64)num_bytes); | |
338bd52f | 1065 | if (qgroup->reserved < num_bytes) |
18dc22c1 QW |
1066 | report_reserved_underflow(fs_info, qgroup, |
1067 | num_bytes); | |
1068 | else | |
1069 | qgroup->reserved -= num_bytes; | |
1070 | } | |
9c8b35b1 QW |
1071 | 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, | |
ef2fff64 | 1077 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
9c8b35b1 QW |
1078 | if (ret < 0) |
1079 | goto out; | |
1080 | } | |
1081 | } | |
1082 | ret = 0; | |
1083 | out: | |
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 | */ | |
1099 | static 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 | } | |
1119 | out: | |
1120 | if (ret) | |
1121 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
1122 | return ret; | |
1123 | } | |
1124 | ||
bed92eae AJ |
1125 | int 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; | |
b7fef4f5 WS |
1129 | struct btrfs_qgroup *parent; |
1130 | struct btrfs_qgroup *member; | |
534e6623 | 1131 | struct btrfs_qgroup_list *list; |
9c8b35b1 | 1132 | struct ulist *tmp; |
bed92eae AJ |
1133 | int ret = 0; |
1134 | ||
8465ecec QW |
1135 | /* Check the level of src and dst first */ |
1136 | if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst)) | |
1137 | return -EINVAL; | |
1138 | ||
6602caf1 | 1139 | tmp = ulist_alloc(GFP_KERNEL); |
ab3680dd CE |
1140 | if (!tmp) |
1141 | return -ENOMEM; | |
1142 | ||
f2f6ed3d | 1143 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1144 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1145 | if (!quota_root) { |
1146 | ret = -EINVAL; | |
1147 | goto out; | |
1148 | } | |
b7fef4f5 WS |
1149 | 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 | } | |
bed92eae | 1155 | |
534e6623 WS |
1156 | /* 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 | ||
bed92eae AJ |
1164 | ret = add_qgroup_relation_item(trans, quota_root, src, dst); |
1165 | if (ret) | |
f2f6ed3d | 1166 | goto out; |
bed92eae AJ |
1167 | |
1168 | ret = add_qgroup_relation_item(trans, quota_root, dst, src); | |
1169 | if (ret) { | |
1170 | del_qgroup_relation_item(trans, quota_root, src, dst); | |
f2f6ed3d | 1171 | goto out; |
bed92eae AJ |
1172 | } |
1173 | ||
1174 | spin_lock(&fs_info->qgroup_lock); | |
0b246afa | 1175 | ret = add_relation_rb(fs_info, src, dst); |
9c8b35b1 QW |
1176 | 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); | |
bed92eae | 1181 | spin_unlock(&fs_info->qgroup_lock); |
f2f6ed3d WS |
1182 | out: |
1183 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
9c8b35b1 | 1184 | ulist_free(tmp); |
bed92eae AJ |
1185 | return ret; |
1186 | } | |
1187 | ||
025db916 | 1188 | static int __del_qgroup_relation(struct btrfs_trans_handle *trans, |
bed92eae AJ |
1189 | struct btrfs_fs_info *fs_info, u64 src, u64 dst) |
1190 | { | |
1191 | struct btrfs_root *quota_root; | |
534e6623 WS |
1192 | struct btrfs_qgroup *parent; |
1193 | struct btrfs_qgroup *member; | |
1194 | struct btrfs_qgroup_list *list; | |
9c8b35b1 | 1195 | struct ulist *tmp; |
bed92eae AJ |
1196 | int ret = 0; |
1197 | int err; | |
1198 | ||
6602caf1 | 1199 | tmp = ulist_alloc(GFP_KERNEL); |
9c8b35b1 QW |
1200 | if (!tmp) |
1201 | return -ENOMEM; | |
1202 | ||
bed92eae | 1203 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1204 | if (!quota_root) { |
1205 | ret = -EINVAL; | |
1206 | goto out; | |
1207 | } | |
bed92eae | 1208 | |
534e6623 WS |
1209 | 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; | |
1223 | exist: | |
bed92eae AJ |
1224 | 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); | |
9c8b35b1 | 1231 | ret = quick_update_accounting(fs_info, tmp, src, dst, -1); |
bed92eae | 1232 | spin_unlock(&fs_info->qgroup_lock); |
f2f6ed3d | 1233 | out: |
9c8b35b1 | 1234 | ulist_free(tmp); |
f5a6b1c5 DY |
1235 | return ret; |
1236 | } | |
1237 | ||
1238 | int 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); | |
f2f6ed3d | 1245 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
f5a6b1c5 | 1246 | |
bed92eae AJ |
1247 | return ret; |
1248 | } | |
1249 | ||
1250 | int btrfs_create_qgroup(struct btrfs_trans_handle *trans, | |
4087cf24 | 1251 | struct btrfs_fs_info *fs_info, u64 qgroupid) |
bed92eae AJ |
1252 | { |
1253 | struct btrfs_root *quota_root; | |
1254 | struct btrfs_qgroup *qgroup; | |
1255 | int ret = 0; | |
1256 | ||
f2f6ed3d | 1257 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1258 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1259 | if (!quota_root) { |
1260 | ret = -EINVAL; | |
1261 | goto out; | |
1262 | } | |
534e6623 WS |
1263 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
1264 | if (qgroup) { | |
1265 | ret = -EEXIST; | |
1266 | goto out; | |
1267 | } | |
bed92eae AJ |
1268 | |
1269 | ret = add_qgroup_item(trans, quota_root, qgroupid); | |
534e6623 WS |
1270 | if (ret) |
1271 | goto out; | |
bed92eae AJ |
1272 | |
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); | |
f2f6ed3d WS |
1279 | out: |
1280 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1281 | return ret; |
1282 | } | |
1283 | ||
1284 | int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, | |
1285 | struct btrfs_fs_info *fs_info, u64 qgroupid) | |
1286 | { | |
1287 | struct btrfs_root *quota_root; | |
2cf68703 | 1288 | struct btrfs_qgroup *qgroup; |
f5a6b1c5 | 1289 | struct btrfs_qgroup_list *list; |
bed92eae AJ |
1290 | int ret = 0; |
1291 | ||
f2f6ed3d | 1292 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1293 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1294 | if (!quota_root) { |
1295 | ret = -EINVAL; | |
1296 | goto out; | |
1297 | } | |
bed92eae | 1298 | |
2cf68703 | 1299 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
534e6623 WS |
1300 | if (!qgroup) { |
1301 | ret = -ENOENT; | |
1302 | goto out; | |
1303 | } else { | |
f5a6b1c5 DY |
1304 | /* check if there are no children of this qgroup */ |
1305 | if (!list_empty(&qgroup->members)) { | |
f2f6ed3d WS |
1306 | ret = -EBUSY; |
1307 | goto out; | |
2cf68703 AJ |
1308 | } |
1309 | } | |
bed92eae AJ |
1310 | ret = del_qgroup_item(trans, quota_root, qgroupid); |
1311 | ||
f5a6b1c5 DY |
1312 | 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 | ||
bed92eae | 1322 | spin_lock(&fs_info->qgroup_lock); |
0b246afa | 1323 | del_qgroup_rb(fs_info, qgroupid); |
bed92eae | 1324 | spin_unlock(&fs_info->qgroup_lock); |
f2f6ed3d WS |
1325 | out: |
1326 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1327 | return ret; |
1328 | } | |
1329 | ||
1330 | int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, | |
1331 | struct btrfs_fs_info *fs_info, u64 qgroupid, | |
1332 | struct btrfs_qgroup_limit *limit) | |
1333 | { | |
f2f6ed3d | 1334 | struct btrfs_root *quota_root; |
bed92eae AJ |
1335 | struct btrfs_qgroup *qgroup; |
1336 | int ret = 0; | |
fe759907 YD |
1337 | /* 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; | |
bed92eae | 1342 | |
f2f6ed3d WS |
1343 | 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 | } | |
bed92eae | 1349 | |
ddb47afa WS |
1350 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
1351 | if (!qgroup) { | |
1352 | ret = -ENOENT; | |
1353 | goto out; | |
1354 | } | |
bed92eae | 1355 | |
58400fce | 1356 | spin_lock(&fs_info->qgroup_lock); |
fe759907 YD |
1357 | 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 | } | |
03477d94 DY |
1393 | qgroup->lim_flags |= limit->flags; |
1394 | ||
bed92eae | 1395 | spin_unlock(&fs_info->qgroup_lock); |
1510e71c DY |
1396 | |
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 | ||
f2f6ed3d WS |
1404 | out: |
1405 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1406 | return ret; |
1407 | } | |
1152651a | 1408 | |
3b7d00f9 QW |
1409 | int 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; | |
9086db86 | 1415 | u64 qgroup_to_skip; |
3b7d00f9 QW |
1416 | int ret = 0; |
1417 | ||
1418 | delayed_refs = &trans->transaction->delayed_refs; | |
9086db86 | 1419 | qgroup_to_skip = delayed_refs->qgroup_to_skip; |
3b7d00f9 QW |
1420 | |
1421 | /* | |
1422 | * No need to do lock, since this function will only be called in | |
01327610 | 1423 | * btrfs_commit_transaction(). |
3b7d00f9 QW |
1424 | */ |
1425 | node = rb_first(&delayed_refs->dirty_extent_root); | |
1426 | while (node) { | |
1427 | record = rb_entry(node, struct btrfs_qgroup_extent_record, | |
1428 | node); | |
fb235dc0 QW |
1429 | if (WARN_ON(!record->old_roots)) |
1430 | ret = btrfs_find_all_roots(NULL, fs_info, | |
1431 | record->bytenr, 0, &record->old_roots); | |
3b7d00f9 QW |
1432 | if (ret < 0) |
1433 | break; | |
9086db86 QW |
1434 | if (qgroup_to_skip) |
1435 | ulist_del(record->old_roots, qgroup_to_skip, 0); | |
3b7d00f9 QW |
1436 | node = rb_next(node); |
1437 | } | |
1438 | return ret; | |
1439 | } | |
1440 | ||
50b3e040 | 1441 | int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info, |
cb93b52c QW |
1442 | struct btrfs_delayed_ref_root *delayed_refs, |
1443 | struct btrfs_qgroup_extent_record *record) | |
3368d001 QW |
1444 | { |
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 | ||
82bd101b | 1450 | assert_spin_locked(&delayed_refs->lock); |
50b3e040 | 1451 | trace_btrfs_qgroup_trace_extent(fs_info, record); |
82bd101b | 1452 | |
3368d001 QW |
1453 | 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 | |
cb93b52c | 1462 | return 1; |
3368d001 QW |
1463 | } |
1464 | ||
1465 | rb_link_node(&record->node, parent_node, p); | |
1466 | rb_insert_color(&record->node, &delayed_refs->dirty_extent_root); | |
cb93b52c QW |
1467 | return 0; |
1468 | } | |
1469 | ||
fb235dc0 QW |
1470 | int 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 | ||
50b3e040 | 1492 | int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, |
cb93b52c QW |
1493 | 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 | ||
afcdd129 JB |
1500 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) |
1501 | || bytenr == 0 || num_bytes == 0) | |
cb93b52c QW |
1502 | 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); | |
2ff7e61e | 1515 | ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record); |
cb93b52c | 1516 | spin_unlock(&delayed_refs->lock); |
fb235dc0 | 1517 | if (ret > 0) { |
cb93b52c | 1518 | kfree(record); |
fb235dc0 QW |
1519 | return 0; |
1520 | } | |
1521 | return btrfs_qgroup_trace_extent_post(fs_info, record); | |
3368d001 QW |
1522 | } |
1523 | ||
33d1f05c | 1524 | int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans, |
2ff7e61e | 1525 | struct btrfs_fs_info *fs_info, |
33d1f05c QW |
1526 | 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() */ | |
0b246afa | 1535 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
33d1f05c QW |
1536 | 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 | ||
0b246afa JM |
1557 | ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr, |
1558 | num_bytes, GFP_NOFS); | |
33d1f05c QW |
1559 | if (ret) |
1560 | return ret; | |
1561 | } | |
cddf3b2c | 1562 | cond_resched(); |
33d1f05c QW |
1563 | 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 | */ | |
15b34517 | 1580 | static int adjust_slots_upwards(struct btrfs_path *path, int root_level) |
33d1f05c QW |
1581 | { |
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 | ||
1627 | int 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 | { | |
0b246afa | 1632 | struct btrfs_fs_info *fs_info = root->fs_info; |
33d1f05c QW |
1633 | 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 | ||
0b246afa | 1641 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
33d1f05c QW |
1642 | 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) { | |
2ff7e61e | 1651 | ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb); |
33d1f05c QW |
1652 | 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 */ | |
1672 | walk_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 | ||
2ff7e61e | 1689 | eb = read_tree_block(fs_info, child_bytenr, child_gen); |
33d1f05c QW |
1690 | 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 | ||
0b246afa JM |
1706 | ret = btrfs_qgroup_trace_extent(trans, fs_info, |
1707 | child_bytenr, | |
1708 | fs_info->nodesize, | |
1709 | GFP_NOFS); | |
33d1f05c QW |
1710 | if (ret) |
1711 | goto out; | |
1712 | } | |
1713 | ||
1714 | if (level == 0) { | |
2ff7e61e JM |
1715 | ret = btrfs_qgroup_trace_leaf_items(trans,fs_info, |
1716 | path->nodes[level]); | |
33d1f05c QW |
1717 | if (ret) |
1718 | goto out; | |
1719 | ||
1720 | /* Nonzero return here means we completed our search */ | |
15b34517 | 1721 | ret = adjust_slots_upwards(path, root_level); |
33d1f05c QW |
1722 | if (ret) |
1723 | break; | |
1724 | ||
1725 | /* Restart search with new slots */ | |
1726 | goto walk_down; | |
1727 | } | |
1728 | ||
1729 | level--; | |
1730 | } | |
1731 | ||
1732 | ret = 0; | |
1733 | out: | |
1734 | btrfs_free_path(path); | |
1735 | ||
1736 | return ret; | |
1737 | } | |
1738 | ||
d810ef2b QW |
1739 | #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 | */ | |
1744 | static 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); | |
ef2fff64 | 1764 | ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg), |
d810ef2b QW |
1765 | GFP_ATOMIC); |
1766 | if (ret < 0) | |
1767 | return ret; | |
ef2fff64 | 1768 | ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC); |
d810ef2b QW |
1769 | 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 | ||
ef2fff64 | 1775 | qg = unode_aux_to_qgroup(tmp_unode); |
d810ef2b QW |
1776 | 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, | |
ef2fff64 | 1782 | qgroup_to_aux(glist->group), |
d810ef2b QW |
1783 | GFP_ATOMIC); |
1784 | if (ret < 0) | |
1785 | return ret; | |
1786 | ret = ulist_add(tmp, glist->group->qgroupid, | |
ef2fff64 | 1787 | qgroup_to_aux(glist->group), |
d810ef2b QW |
1788 | GFP_ATOMIC); |
1789 | if (ret < 0) | |
1790 | return ret; | |
1791 | } | |
1792 | } | |
1793 | } | |
1794 | return 0; | |
1795 | } | |
1796 | ||
823ae5b8 QW |
1797 | /* |
1798 | * Update qgroup rfer/excl counters. | |
1799 | * Rfer update is easy, codes can explain themselves. | |
e69bcee3 | 1800 | * |
823ae5b8 QW |
1801 | * 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) | |
01327610 | 1814 | * !B: cur_new_roots == nr_new_roots (possible exclusive now) |
823ae5b8 QW |
1815 | * |
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 | */ | |
1833 | static 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 | ||
ef2fff64 | 1848 | qg = unode_aux_to_qgroup(unode); |
823ae5b8 QW |
1849 | cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq); |
1850 | cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq); | |
1851 | ||
bc074524 JM |
1852 | trace_qgroup_update_counters(fs_info, qg->qgroupid, |
1853 | cur_old_count, cur_new_count); | |
0f5dcf8d | 1854 | |
823ae5b8 QW |
1855 | /* 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 | } | |
c05f9429 | 1915 | |
823ae5b8 QW |
1916 | if (dirty) |
1917 | qgroup_dirty(fs_info, qg); | |
1918 | } | |
1919 | return 0; | |
1920 | } | |
1921 | ||
5edfd9fd QW |
1922 | /* |
1923 | * Check if the @roots potentially is a list of fs tree roots | |
1924 | * | |
1925 | * Return 0 for definitely not a fs/subvol tree roots ulist | |
1926 | * Return 1 for possible fs/subvol tree roots in the list (considering an empty | |
1927 | * one as well) | |
1928 | */ | |
1929 | static int maybe_fs_roots(struct ulist *roots) | |
1930 | { | |
1931 | struct ulist_node *unode; | |
1932 | struct ulist_iterator uiter; | |
1933 | ||
1934 | /* Empty one, still possible for fs roots */ | |
1935 | if (!roots || roots->nnodes == 0) | |
1936 | return 1; | |
1937 | ||
1938 | ULIST_ITER_INIT(&uiter); | |
1939 | unode = ulist_next(roots, &uiter); | |
1940 | if (!unode) | |
1941 | return 1; | |
1942 | ||
1943 | /* | |
1944 | * If it contains fs tree roots, then it must belong to fs/subvol | |
1945 | * trees. | |
1946 | * If it contains a non-fs tree, it won't be shared with fs/subvol trees. | |
1947 | */ | |
1948 | return is_fstree(unode->val); | |
1949 | } | |
1950 | ||
442244c9 | 1951 | int |
550d7a2e QW |
1952 | btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, |
1953 | struct btrfs_fs_info *fs_info, | |
1954 | u64 bytenr, u64 num_bytes, | |
1955 | struct ulist *old_roots, struct ulist *new_roots) | |
1956 | { | |
1957 | struct ulist *qgroups = NULL; | |
1958 | struct ulist *tmp = NULL; | |
1959 | u64 seq; | |
1960 | u64 nr_new_roots = 0; | |
1961 | u64 nr_old_roots = 0; | |
1962 | int ret = 0; | |
1963 | ||
81353d50 DS |
1964 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
1965 | return 0; | |
1966 | ||
5edfd9fd QW |
1967 | if (new_roots) { |
1968 | if (!maybe_fs_roots(new_roots)) | |
1969 | goto out_free; | |
550d7a2e | 1970 | nr_new_roots = new_roots->nnodes; |
5edfd9fd QW |
1971 | } |
1972 | if (old_roots) { | |
1973 | if (!maybe_fs_roots(old_roots)) | |
1974 | goto out_free; | |
550d7a2e | 1975 | nr_old_roots = old_roots->nnodes; |
5edfd9fd QW |
1976 | } |
1977 | ||
1978 | /* Quick exit, either not fs tree roots, or won't affect any qgroup */ | |
1979 | if (nr_old_roots == 0 && nr_new_roots == 0) | |
1980 | goto out_free; | |
550d7a2e | 1981 | |
550d7a2e QW |
1982 | BUG_ON(!fs_info->quota_root); |
1983 | ||
bc074524 JM |
1984 | trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes, |
1985 | nr_old_roots, nr_new_roots); | |
0f5dcf8d | 1986 | |
550d7a2e QW |
1987 | qgroups = ulist_alloc(GFP_NOFS); |
1988 | if (!qgroups) { | |
1989 | ret = -ENOMEM; | |
1990 | goto out_free; | |
1991 | } | |
1992 | tmp = ulist_alloc(GFP_NOFS); | |
1993 | if (!tmp) { | |
1994 | ret = -ENOMEM; | |
1995 | goto out_free; | |
1996 | } | |
1997 | ||
1998 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
1999 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { | |
2000 | if (fs_info->qgroup_rescan_progress.objectid <= bytenr) { | |
2001 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2002 | ret = 0; | |
2003 | goto out_free; | |
2004 | } | |
2005 | } | |
2006 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2007 | ||
2008 | spin_lock(&fs_info->qgroup_lock); | |
2009 | seq = fs_info->qgroup_seq; | |
2010 | ||
2011 | /* Update old refcnts using old_roots */ | |
2012 | ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq, | |
2013 | UPDATE_OLD); | |
2014 | if (ret < 0) | |
2015 | goto out; | |
2016 | ||
2017 | /* Update new refcnts using new_roots */ | |
2018 | ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq, | |
2019 | UPDATE_NEW); | |
2020 | if (ret < 0) | |
2021 | goto out; | |
2022 | ||
2023 | qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots, | |
2024 | num_bytes, seq); | |
2025 | ||
2026 | /* | |
2027 | * Bump qgroup_seq to avoid seq overlap | |
2028 | */ | |
2029 | fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1; | |
2030 | out: | |
2031 | spin_unlock(&fs_info->qgroup_lock); | |
2032 | out_free: | |
2033 | ulist_free(tmp); | |
2034 | ulist_free(qgroups); | |
2035 | ulist_free(old_roots); | |
2036 | ulist_free(new_roots); | |
2037 | return ret; | |
2038 | } | |
2039 | ||
2040 | int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans, | |
2041 | struct btrfs_fs_info *fs_info) | |
2042 | { | |
2043 | struct btrfs_qgroup_extent_record *record; | |
2044 | struct btrfs_delayed_ref_root *delayed_refs; | |
2045 | struct ulist *new_roots = NULL; | |
2046 | struct rb_node *node; | |
9086db86 | 2047 | u64 qgroup_to_skip; |
550d7a2e QW |
2048 | int ret = 0; |
2049 | ||
2050 | delayed_refs = &trans->transaction->delayed_refs; | |
9086db86 | 2051 | qgroup_to_skip = delayed_refs->qgroup_to_skip; |
550d7a2e QW |
2052 | while ((node = rb_first(&delayed_refs->dirty_extent_root))) { |
2053 | record = rb_entry(node, struct btrfs_qgroup_extent_record, | |
2054 | node); | |
2055 | ||
bc074524 | 2056 | trace_btrfs_qgroup_account_extents(fs_info, record); |
0f5dcf8d | 2057 | |
550d7a2e QW |
2058 | if (!ret) { |
2059 | /* | |
de47c9d3 | 2060 | * Use SEQ_LAST as time_seq to do special search, which |
550d7a2e QW |
2061 | * doesn't lock tree or delayed_refs and search current |
2062 | * root. It's safe inside commit_transaction(). | |
2063 | */ | |
2064 | ret = btrfs_find_all_roots(trans, fs_info, | |
de47c9d3 | 2065 | record->bytenr, SEQ_LAST, &new_roots); |
550d7a2e QW |
2066 | if (ret < 0) |
2067 | goto cleanup; | |
9086db86 QW |
2068 | if (qgroup_to_skip) |
2069 | ulist_del(new_roots, qgroup_to_skip, 0); | |
550d7a2e QW |
2070 | ret = btrfs_qgroup_account_extent(trans, fs_info, |
2071 | record->bytenr, record->num_bytes, | |
2072 | record->old_roots, new_roots); | |
2073 | record->old_roots = NULL; | |
2074 | new_roots = NULL; | |
2075 | } | |
2076 | cleanup: | |
2077 | ulist_free(record->old_roots); | |
2078 | ulist_free(new_roots); | |
2079 | new_roots = NULL; | |
2080 | rb_erase(node, &delayed_refs->dirty_extent_root); | |
2081 | kfree(record); | |
2082 | ||
2083 | } | |
2084 | return ret; | |
2085 | } | |
2086 | ||
bed92eae AJ |
2087 | /* |
2088 | * called from commit_transaction. Writes all changed qgroups to disk. | |
2089 | */ | |
2090 | int btrfs_run_qgroups(struct btrfs_trans_handle *trans, | |
2091 | struct btrfs_fs_info *fs_info) | |
2092 | { | |
2093 | struct btrfs_root *quota_root = fs_info->quota_root; | |
2094 | int ret = 0; | |
3d7b5a28 | 2095 | int start_rescan_worker = 0; |
bed92eae AJ |
2096 | |
2097 | if (!quota_root) | |
2098 | goto out; | |
2099 | ||
afcdd129 JB |
2100 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) && |
2101 | test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags)) | |
3d7b5a28 JS |
2102 | start_rescan_worker = 1; |
2103 | ||
afcdd129 JB |
2104 | if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags)) |
2105 | set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); | |
2106 | if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags)) | |
2107 | clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); | |
bed92eae AJ |
2108 | |
2109 | spin_lock(&fs_info->qgroup_lock); | |
2110 | while (!list_empty(&fs_info->dirty_qgroups)) { | |
2111 | struct btrfs_qgroup *qgroup; | |
2112 | qgroup = list_first_entry(&fs_info->dirty_qgroups, | |
2113 | struct btrfs_qgroup, dirty); | |
2114 | list_del_init(&qgroup->dirty); | |
2115 | spin_unlock(&fs_info->qgroup_lock); | |
2116 | ret = update_qgroup_info_item(trans, quota_root, qgroup); | |
d3001ed3 DY |
2117 | if (ret) |
2118 | fs_info->qgroup_flags |= | |
2119 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2120 | ret = update_qgroup_limit_item(trans, quota_root, qgroup); | |
bed92eae AJ |
2121 | if (ret) |
2122 | fs_info->qgroup_flags |= | |
2123 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2124 | spin_lock(&fs_info->qgroup_lock); | |
2125 | } | |
afcdd129 | 2126 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
bed92eae AJ |
2127 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON; |
2128 | else | |
2129 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; | |
2130 | spin_unlock(&fs_info->qgroup_lock); | |
2131 | ||
2132 | ret = update_qgroup_status_item(trans, fs_info, quota_root); | |
2133 | if (ret) | |
2134 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2135 | ||
3d7b5a28 | 2136 | if (!ret && start_rescan_worker) { |
b382a324 JS |
2137 | ret = qgroup_rescan_init(fs_info, 0, 1); |
2138 | if (!ret) { | |
2139 | qgroup_rescan_zero_tracking(fs_info); | |
fc97fab0 QW |
2140 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
2141 | &fs_info->qgroup_rescan_work); | |
b382a324 | 2142 | } |
3d7b5a28 JS |
2143 | ret = 0; |
2144 | } | |
2145 | ||
bed92eae AJ |
2146 | out: |
2147 | ||
2148 | return ret; | |
2149 | } | |
2150 | ||
2151 | /* | |
01327610 | 2152 | * Copy the accounting information between qgroups. This is necessary |
918c2ee1 MF |
2153 | * when a snapshot or a subvolume is created. Throwing an error will |
2154 | * cause a transaction abort so we take extra care here to only error | |
2155 | * when a readonly fs is a reasonable outcome. | |
bed92eae AJ |
2156 | */ |
2157 | int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, | |
2158 | struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid, | |
2159 | struct btrfs_qgroup_inherit *inherit) | |
2160 | { | |
2161 | int ret = 0; | |
2162 | int i; | |
2163 | u64 *i_qgroups; | |
2164 | struct btrfs_root *quota_root = fs_info->quota_root; | |
2165 | struct btrfs_qgroup *srcgroup; | |
2166 | struct btrfs_qgroup *dstgroup; | |
2167 | u32 level_size = 0; | |
3f5e2d3b | 2168 | u64 nums; |
bed92eae | 2169 | |
f2f6ed3d | 2170 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
afcdd129 | 2171 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
f2f6ed3d | 2172 | goto out; |
bed92eae | 2173 | |
f2f6ed3d WS |
2174 | if (!quota_root) { |
2175 | ret = -EINVAL; | |
2176 | goto out; | |
2177 | } | |
bed92eae | 2178 | |
3f5e2d3b WS |
2179 | if (inherit) { |
2180 | i_qgroups = (u64 *)(inherit + 1); | |
2181 | nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + | |
2182 | 2 * inherit->num_excl_copies; | |
2183 | for (i = 0; i < nums; ++i) { | |
2184 | srcgroup = find_qgroup_rb(fs_info, *i_qgroups); | |
09870d27 | 2185 | |
918c2ee1 MF |
2186 | /* |
2187 | * Zero out invalid groups so we can ignore | |
2188 | * them later. | |
2189 | */ | |
2190 | if (!srcgroup || | |
2191 | ((srcgroup->qgroupid >> 48) <= (objectid >> 48))) | |
2192 | *i_qgroups = 0ULL; | |
2193 | ||
3f5e2d3b WS |
2194 | ++i_qgroups; |
2195 | } | |
2196 | } | |
2197 | ||
bed92eae AJ |
2198 | /* |
2199 | * create a tracking group for the subvol itself | |
2200 | */ | |
2201 | ret = add_qgroup_item(trans, quota_root, objectid); | |
2202 | if (ret) | |
2203 | goto out; | |
2204 | ||
bed92eae AJ |
2205 | if (srcid) { |
2206 | struct btrfs_root *srcroot; | |
2207 | struct btrfs_key srckey; | |
bed92eae AJ |
2208 | |
2209 | srckey.objectid = srcid; | |
2210 | srckey.type = BTRFS_ROOT_ITEM_KEY; | |
2211 | srckey.offset = (u64)-1; | |
2212 | srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey); | |
2213 | if (IS_ERR(srcroot)) { | |
2214 | ret = PTR_ERR(srcroot); | |
2215 | goto out; | |
2216 | } | |
2217 | ||
0b246afa | 2218 | level_size = fs_info->nodesize; |
bed92eae AJ |
2219 | } |
2220 | ||
2221 | /* | |
2222 | * add qgroup to all inherited groups | |
2223 | */ | |
2224 | if (inherit) { | |
2225 | i_qgroups = (u64 *)(inherit + 1); | |
918c2ee1 MF |
2226 | for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) { |
2227 | if (*i_qgroups == 0) | |
2228 | continue; | |
bed92eae AJ |
2229 | ret = add_qgroup_relation_item(trans, quota_root, |
2230 | objectid, *i_qgroups); | |
918c2ee1 | 2231 | if (ret && ret != -EEXIST) |
bed92eae AJ |
2232 | goto out; |
2233 | ret = add_qgroup_relation_item(trans, quota_root, | |
2234 | *i_qgroups, objectid); | |
918c2ee1 | 2235 | if (ret && ret != -EEXIST) |
bed92eae | 2236 | goto out; |
bed92eae | 2237 | } |
918c2ee1 | 2238 | ret = 0; |
bed92eae AJ |
2239 | } |
2240 | ||
2241 | ||
2242 | spin_lock(&fs_info->qgroup_lock); | |
2243 | ||
2244 | dstgroup = add_qgroup_rb(fs_info, objectid); | |
57a5a882 DC |
2245 | if (IS_ERR(dstgroup)) { |
2246 | ret = PTR_ERR(dstgroup); | |
bed92eae | 2247 | goto unlock; |
57a5a882 | 2248 | } |
bed92eae | 2249 | |
e8c8541a | 2250 | if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) { |
e8c8541a DY |
2251 | dstgroup->lim_flags = inherit->lim.flags; |
2252 | dstgroup->max_rfer = inherit->lim.max_rfer; | |
2253 | dstgroup->max_excl = inherit->lim.max_excl; | |
2254 | dstgroup->rsv_rfer = inherit->lim.rsv_rfer; | |
2255 | dstgroup->rsv_excl = inherit->lim.rsv_excl; | |
1510e71c DY |
2256 | |
2257 | ret = update_qgroup_limit_item(trans, quota_root, dstgroup); | |
2258 | if (ret) { | |
2259 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
5d163e0e JM |
2260 | btrfs_info(fs_info, |
2261 | "unable to update quota limit for %llu", | |
2262 | dstgroup->qgroupid); | |
1510e71c DY |
2263 | goto unlock; |
2264 | } | |
e8c8541a DY |
2265 | } |
2266 | ||
bed92eae AJ |
2267 | if (srcid) { |
2268 | srcgroup = find_qgroup_rb(fs_info, srcid); | |
f3a87f1b | 2269 | if (!srcgroup) |
bed92eae | 2270 | goto unlock; |
fcebe456 JB |
2271 | |
2272 | /* | |
2273 | * We call inherit after we clone the root in order to make sure | |
2274 | * our counts don't go crazy, so at this point the only | |
2275 | * difference between the two roots should be the root node. | |
2276 | */ | |
2277 | dstgroup->rfer = srcgroup->rfer; | |
2278 | dstgroup->rfer_cmpr = srcgroup->rfer_cmpr; | |
2279 | dstgroup->excl = level_size; | |
2280 | dstgroup->excl_cmpr = level_size; | |
bed92eae AJ |
2281 | srcgroup->excl = level_size; |
2282 | srcgroup->excl_cmpr = level_size; | |
3eeb4d59 DY |
2283 | |
2284 | /* inherit the limit info */ | |
2285 | dstgroup->lim_flags = srcgroup->lim_flags; | |
2286 | dstgroup->max_rfer = srcgroup->max_rfer; | |
2287 | dstgroup->max_excl = srcgroup->max_excl; | |
2288 | dstgroup->rsv_rfer = srcgroup->rsv_rfer; | |
2289 | dstgroup->rsv_excl = srcgroup->rsv_excl; | |
2290 | ||
bed92eae AJ |
2291 | qgroup_dirty(fs_info, dstgroup); |
2292 | qgroup_dirty(fs_info, srcgroup); | |
2293 | } | |
2294 | ||
f3a87f1b | 2295 | if (!inherit) |
bed92eae AJ |
2296 | goto unlock; |
2297 | ||
2298 | i_qgroups = (u64 *)(inherit + 1); | |
2299 | for (i = 0; i < inherit->num_qgroups; ++i) { | |
918c2ee1 | 2300 | if (*i_qgroups) { |
0b246afa | 2301 | ret = add_relation_rb(fs_info, objectid, *i_qgroups); |
918c2ee1 MF |
2302 | if (ret) |
2303 | goto unlock; | |
2304 | } | |
bed92eae AJ |
2305 | ++i_qgroups; |
2306 | } | |
2307 | ||
918c2ee1 | 2308 | for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) { |
bed92eae AJ |
2309 | struct btrfs_qgroup *src; |
2310 | struct btrfs_qgroup *dst; | |
2311 | ||
918c2ee1 MF |
2312 | if (!i_qgroups[0] || !i_qgroups[1]) |
2313 | continue; | |
2314 | ||
bed92eae AJ |
2315 | src = find_qgroup_rb(fs_info, i_qgroups[0]); |
2316 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); | |
2317 | ||
2318 | if (!src || !dst) { | |
2319 | ret = -EINVAL; | |
2320 | goto unlock; | |
2321 | } | |
2322 | ||
2323 | dst->rfer = src->rfer - level_size; | |
2324 | dst->rfer_cmpr = src->rfer_cmpr - level_size; | |
bed92eae | 2325 | } |
918c2ee1 | 2326 | for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) { |
bed92eae AJ |
2327 | struct btrfs_qgroup *src; |
2328 | struct btrfs_qgroup *dst; | |
2329 | ||
918c2ee1 MF |
2330 | if (!i_qgroups[0] || !i_qgroups[1]) |
2331 | continue; | |
2332 | ||
bed92eae AJ |
2333 | src = find_qgroup_rb(fs_info, i_qgroups[0]); |
2334 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); | |
2335 | ||
2336 | if (!src || !dst) { | |
2337 | ret = -EINVAL; | |
2338 | goto unlock; | |
2339 | } | |
2340 | ||
2341 | dst->excl = src->excl + level_size; | |
2342 | dst->excl_cmpr = src->excl_cmpr + level_size; | |
bed92eae AJ |
2343 | } |
2344 | ||
2345 | unlock: | |
2346 | spin_unlock(&fs_info->qgroup_lock); | |
2347 | out: | |
f2f6ed3d | 2348 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
2349 | return ret; |
2350 | } | |
2351 | ||
003d7c59 JM |
2352 | static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes) |
2353 | { | |
2354 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) && | |
2355 | qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer) | |
2356 | return false; | |
2357 | ||
2358 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) && | |
2359 | qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl) | |
2360 | return false; | |
2361 | ||
2362 | return true; | |
2363 | } | |
2364 | ||
2365 | static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce) | |
bed92eae AJ |
2366 | { |
2367 | struct btrfs_root *quota_root; | |
2368 | struct btrfs_qgroup *qgroup; | |
2369 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2370 | u64 ref_root = root->root_key.objectid; | |
2371 | int ret = 0; | |
48a89bc4 | 2372 | int retried = 0; |
bed92eae AJ |
2373 | struct ulist_node *unode; |
2374 | struct ulist_iterator uiter; | |
2375 | ||
2376 | if (!is_fstree(ref_root)) | |
2377 | return 0; | |
2378 | ||
2379 | if (num_bytes == 0) | |
2380 | return 0; | |
f29efe29 SD |
2381 | |
2382 | if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) && | |
2383 | capable(CAP_SYS_RESOURCE)) | |
2384 | enforce = false; | |
2385 | ||
48a89bc4 | 2386 | retry: |
bed92eae AJ |
2387 | spin_lock(&fs_info->qgroup_lock); |
2388 | quota_root = fs_info->quota_root; | |
2389 | if (!quota_root) | |
2390 | goto out; | |
2391 | ||
2392 | qgroup = find_qgroup_rb(fs_info, ref_root); | |
2393 | if (!qgroup) | |
2394 | goto out; | |
2395 | ||
2396 | /* | |
2397 | * in a first step, we check all affected qgroups if any limits would | |
2398 | * be exceeded | |
2399 | */ | |
1e8f9158 WS |
2400 | ulist_reinit(fs_info->qgroup_ulist); |
2401 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, | |
3c97185c WS |
2402 | (uintptr_t)qgroup, GFP_ATOMIC); |
2403 | if (ret < 0) | |
2404 | goto out; | |
bed92eae | 2405 | ULIST_ITER_INIT(&uiter); |
1e8f9158 | 2406 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
bed92eae AJ |
2407 | struct btrfs_qgroup *qg; |
2408 | struct btrfs_qgroup_list *glist; | |
2409 | ||
ef2fff64 | 2410 | qg = unode_aux_to_qgroup(unode); |
bed92eae | 2411 | |
003d7c59 | 2412 | if (enforce && !qgroup_check_limits(qg, num_bytes)) { |
48a89bc4 GR |
2413 | /* |
2414 | * Commit the tree and retry, since we may have | |
2415 | * deletions which would free up space. | |
2416 | */ | |
2417 | if (!retried && qg->reserved > 0) { | |
2418 | struct btrfs_trans_handle *trans; | |
2419 | ||
2420 | spin_unlock(&fs_info->qgroup_lock); | |
2421 | ret = btrfs_start_delalloc_inodes(root, 0); | |
2422 | if (ret) | |
2423 | return ret; | |
2424 | btrfs_wait_ordered_extents(root, -1, 0, (u64)-1); | |
2425 | trans = btrfs_join_transaction(root); | |
2426 | if (IS_ERR(trans)) | |
2427 | return PTR_ERR(trans); | |
2428 | ret = btrfs_commit_transaction(trans); | |
2429 | if (ret) | |
2430 | return ret; | |
2431 | retried++; | |
2432 | goto retry; | |
2433 | } | |
bed92eae | 2434 | ret = -EDQUOT; |
720f1e20 WS |
2435 | goto out; |
2436 | } | |
bed92eae AJ |
2437 | |
2438 | list_for_each_entry(glist, &qg->groups, next_group) { | |
1e8f9158 WS |
2439 | ret = ulist_add(fs_info->qgroup_ulist, |
2440 | glist->group->qgroupid, | |
3c97185c WS |
2441 | (uintptr_t)glist->group, GFP_ATOMIC); |
2442 | if (ret < 0) | |
2443 | goto out; | |
bed92eae AJ |
2444 | } |
2445 | } | |
3c97185c | 2446 | ret = 0; |
bed92eae AJ |
2447 | /* |
2448 | * no limits exceeded, now record the reservation into all qgroups | |
2449 | */ | |
2450 | ULIST_ITER_INIT(&uiter); | |
1e8f9158 | 2451 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
bed92eae AJ |
2452 | struct btrfs_qgroup *qg; |
2453 | ||
ef2fff64 | 2454 | qg = unode_aux_to_qgroup(unode); |
bed92eae | 2455 | |
3159fe7b | 2456 | trace_qgroup_update_reserve(fs_info, qg, num_bytes); |
e2d1f923 | 2457 | qg->reserved += num_bytes; |
bed92eae AJ |
2458 | } |
2459 | ||
2460 | out: | |
2461 | spin_unlock(&fs_info->qgroup_lock); | |
bed92eae AJ |
2462 | return ret; |
2463 | } | |
2464 | ||
297d750b QW |
2465 | void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info, |
2466 | u64 ref_root, u64 num_bytes) | |
bed92eae AJ |
2467 | { |
2468 | struct btrfs_root *quota_root; | |
2469 | struct btrfs_qgroup *qgroup; | |
bed92eae AJ |
2470 | struct ulist_node *unode; |
2471 | struct ulist_iterator uiter; | |
3c97185c | 2472 | int ret = 0; |
bed92eae AJ |
2473 | |
2474 | if (!is_fstree(ref_root)) | |
2475 | return; | |
2476 | ||
2477 | if (num_bytes == 0) | |
2478 | return; | |
2479 | ||
2480 | spin_lock(&fs_info->qgroup_lock); | |
2481 | ||
2482 | quota_root = fs_info->quota_root; | |
2483 | if (!quota_root) | |
2484 | goto out; | |
2485 | ||
2486 | qgroup = find_qgroup_rb(fs_info, ref_root); | |
2487 | if (!qgroup) | |
2488 | goto out; | |
2489 | ||
1e8f9158 WS |
2490 | ulist_reinit(fs_info->qgroup_ulist); |
2491 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, | |
3c97185c WS |
2492 | (uintptr_t)qgroup, GFP_ATOMIC); |
2493 | if (ret < 0) | |
2494 | goto out; | |
bed92eae | 2495 | ULIST_ITER_INIT(&uiter); |
1e8f9158 | 2496 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
bed92eae AJ |
2497 | struct btrfs_qgroup *qg; |
2498 | struct btrfs_qgroup_list *glist; | |
2499 | ||
ef2fff64 | 2500 | qg = unode_aux_to_qgroup(unode); |
bed92eae | 2501 | |
3159fe7b | 2502 | trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes); |
338bd52f | 2503 | if (qg->reserved < num_bytes) |
18dc22c1 QW |
2504 | report_reserved_underflow(fs_info, qg, num_bytes); |
2505 | else | |
2506 | qg->reserved -= num_bytes; | |
bed92eae AJ |
2507 | |
2508 | list_for_each_entry(glist, &qg->groups, next_group) { | |
1e8f9158 WS |
2509 | ret = ulist_add(fs_info->qgroup_ulist, |
2510 | glist->group->qgroupid, | |
3c97185c WS |
2511 | (uintptr_t)glist->group, GFP_ATOMIC); |
2512 | if (ret < 0) | |
2513 | goto out; | |
bed92eae AJ |
2514 | } |
2515 | } | |
2516 | ||
2517 | out: | |
2518 | spin_unlock(&fs_info->qgroup_lock); | |
bed92eae AJ |
2519 | } |
2520 | ||
2f232036 JS |
2521 | /* |
2522 | * returns < 0 on error, 0 when more leafs are to be scanned. | |
3393168d | 2523 | * returns 1 when done. |
2f232036 JS |
2524 | */ |
2525 | static int | |
b382a324 | 2526 | qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
0a0e8b89 | 2527 | struct btrfs_trans_handle *trans) |
2f232036 JS |
2528 | { |
2529 | struct btrfs_key found; | |
0a0e8b89 | 2530 | struct extent_buffer *scratch_leaf = NULL; |
2f232036 | 2531 | struct ulist *roots = NULL; |
3284da7b | 2532 | struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); |
fcebe456 | 2533 | u64 num_bytes; |
2f232036 JS |
2534 | int slot; |
2535 | int ret; | |
2536 | ||
2f232036 JS |
2537 | mutex_lock(&fs_info->qgroup_rescan_lock); |
2538 | ret = btrfs_search_slot_for_read(fs_info->extent_root, | |
2539 | &fs_info->qgroup_rescan_progress, | |
2540 | path, 1, 0); | |
2541 | ||
ab8d0fc4 JM |
2542 | btrfs_debug(fs_info, |
2543 | "current progress key (%llu %u %llu), search_slot ret %d", | |
2544 | fs_info->qgroup_rescan_progress.objectid, | |
2545 | fs_info->qgroup_rescan_progress.type, | |
2546 | fs_info->qgroup_rescan_progress.offset, ret); | |
2f232036 JS |
2547 | |
2548 | if (ret) { | |
2549 | /* | |
2550 | * The rescan is about to end, we will not be scanning any | |
2551 | * further blocks. We cannot unset the RESCAN flag here, because | |
2552 | * we want to commit the transaction if everything went well. | |
2553 | * To make the live accounting work in this phase, we set our | |
2554 | * scan progress pointer such that every real extent objectid | |
2555 | * will be smaller. | |
2556 | */ | |
2557 | fs_info->qgroup_rescan_progress.objectid = (u64)-1; | |
2558 | btrfs_release_path(path); | |
2559 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2560 | return ret; | |
2561 | } | |
2562 | ||
2563 | btrfs_item_key_to_cpu(path->nodes[0], &found, | |
2564 | btrfs_header_nritems(path->nodes[0]) - 1); | |
2565 | fs_info->qgroup_rescan_progress.objectid = found.objectid + 1; | |
2566 | ||
2567 | btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); | |
0a0e8b89 QW |
2568 | scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]); |
2569 | if (!scratch_leaf) { | |
2570 | ret = -ENOMEM; | |
2571 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2572 | goto out; | |
2573 | } | |
2574 | extent_buffer_get(scratch_leaf); | |
2575 | btrfs_tree_read_lock(scratch_leaf); | |
2576 | btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK); | |
2f232036 JS |
2577 | slot = path->slots[0]; |
2578 | btrfs_release_path(path); | |
2579 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2580 | ||
2581 | for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) { | |
2582 | btrfs_item_key_to_cpu(scratch_leaf, &found, slot); | |
3a6d75e8 JB |
2583 | if (found.type != BTRFS_EXTENT_ITEM_KEY && |
2584 | found.type != BTRFS_METADATA_ITEM_KEY) | |
2f232036 | 2585 | continue; |
3a6d75e8 | 2586 | if (found.type == BTRFS_METADATA_ITEM_KEY) |
da17066c | 2587 | num_bytes = fs_info->nodesize; |
3a6d75e8 JB |
2588 | else |
2589 | num_bytes = found.offset; | |
2590 | ||
fcebe456 JB |
2591 | ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0, |
2592 | &roots); | |
2f232036 JS |
2593 | if (ret < 0) |
2594 | goto out; | |
9d220c95 QW |
2595 | /* For rescan, just pass old_roots as NULL */ |
2596 | ret = btrfs_qgroup_account_extent(trans, fs_info, | |
2597 | found.objectid, num_bytes, NULL, roots); | |
2598 | if (ret < 0) | |
fcebe456 | 2599 | goto out; |
2f232036 | 2600 | } |
2f232036 | 2601 | out: |
0a0e8b89 QW |
2602 | if (scratch_leaf) { |
2603 | btrfs_tree_read_unlock_blocking(scratch_leaf); | |
2604 | free_extent_buffer(scratch_leaf); | |
2605 | } | |
2f232036 JS |
2606 | btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); |
2607 | ||
2608 | return ret; | |
2609 | } | |
2610 | ||
d458b054 | 2611 | static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) |
2f232036 | 2612 | { |
b382a324 JS |
2613 | struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info, |
2614 | qgroup_rescan_work); | |
2f232036 JS |
2615 | struct btrfs_path *path; |
2616 | struct btrfs_trans_handle *trans = NULL; | |
2f232036 | 2617 | int err = -ENOMEM; |
53b7cde9 | 2618 | int ret = 0; |
2f232036 JS |
2619 | |
2620 | path = btrfs_alloc_path(); | |
2621 | if (!path) | |
2622 | goto out; | |
2f232036 JS |
2623 | |
2624 | err = 0; | |
7343dd61 | 2625 | while (!err && !btrfs_fs_closing(fs_info)) { |
2f232036 JS |
2626 | trans = btrfs_start_transaction(fs_info->fs_root, 0); |
2627 | if (IS_ERR(trans)) { | |
2628 | err = PTR_ERR(trans); | |
2629 | break; | |
2630 | } | |
afcdd129 | 2631 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
2f232036 JS |
2632 | err = -EINTR; |
2633 | } else { | |
0a0e8b89 | 2634 | err = qgroup_rescan_leaf(fs_info, path, trans); |
2f232036 JS |
2635 | } |
2636 | if (err > 0) | |
3a45bb20 | 2637 | btrfs_commit_transaction(trans); |
2f232036 | 2638 | else |
3a45bb20 | 2639 | btrfs_end_transaction(trans); |
2f232036 JS |
2640 | } |
2641 | ||
2642 | out: | |
2f232036 | 2643 | btrfs_free_path(path); |
2f232036 JS |
2644 | |
2645 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
7343dd61 JM |
2646 | if (!btrfs_fs_closing(fs_info)) |
2647 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2f232036 | 2648 | |
3393168d | 2649 | if (err > 0 && |
2f232036 JS |
2650 | fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) { |
2651 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2652 | } else if (err < 0) { | |
2653 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2654 | } | |
2655 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2656 | ||
53b7cde9 | 2657 | /* |
01327610 | 2658 | * only update status, since the previous part has already updated the |
53b7cde9 QW |
2659 | * qgroup info. |
2660 | */ | |
2661 | trans = btrfs_start_transaction(fs_info->quota_root, 1); | |
2662 | if (IS_ERR(trans)) { | |
2663 | err = PTR_ERR(trans); | |
2664 | btrfs_err(fs_info, | |
2665 | "fail to start transaction for status update: %d\n", | |
2666 | err); | |
2667 | goto done; | |
2668 | } | |
2669 | ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root); | |
2670 | if (ret < 0) { | |
2671 | err = ret; | |
ab8d0fc4 | 2672 | btrfs_err(fs_info, "fail to update qgroup status: %d", err); |
53b7cde9 | 2673 | } |
3a45bb20 | 2674 | btrfs_end_transaction(trans); |
53b7cde9 | 2675 | |
7343dd61 JM |
2676 | if (btrfs_fs_closing(fs_info)) { |
2677 | btrfs_info(fs_info, "qgroup scan paused"); | |
2678 | } else if (err >= 0) { | |
efe120a0 | 2679 | btrfs_info(fs_info, "qgroup scan completed%s", |
3393168d | 2680 | err > 0 ? " (inconsistency flag cleared)" : ""); |
2f232036 | 2681 | } else { |
efe120a0 | 2682 | btrfs_err(fs_info, "qgroup scan failed with %d", err); |
2f232036 | 2683 | } |
57254b6e | 2684 | |
53b7cde9 | 2685 | done: |
d2c609b8 JM |
2686 | mutex_lock(&fs_info->qgroup_rescan_lock); |
2687 | fs_info->qgroup_rescan_running = false; | |
2688 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
57254b6e | 2689 | complete_all(&fs_info->qgroup_rescan_completion); |
2f232036 JS |
2690 | } |
2691 | ||
b382a324 JS |
2692 | /* |
2693 | * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all | |
2694 | * memory required for the rescan context. | |
2695 | */ | |
2696 | static int | |
2697 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, | |
2698 | int init_flags) | |
2f232036 JS |
2699 | { |
2700 | int ret = 0; | |
2f232036 | 2701 | |
b382a324 JS |
2702 | if (!init_flags && |
2703 | (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) || | |
2704 | !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) { | |
2705 | ret = -EINVAL; | |
2706 | goto err; | |
2707 | } | |
2f232036 JS |
2708 | |
2709 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2710 | spin_lock(&fs_info->qgroup_lock); | |
b382a324 JS |
2711 | |
2712 | if (init_flags) { | |
2713 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) | |
2714 | ret = -EINPROGRESS; | |
2715 | else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) | |
2716 | ret = -EINVAL; | |
2717 | ||
2718 | if (ret) { | |
2719 | spin_unlock(&fs_info->qgroup_lock); | |
2720 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2721 | goto err; | |
2722 | } | |
b382a324 | 2723 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
2f232036 JS |
2724 | } |
2725 | ||
2f232036 JS |
2726 | memset(&fs_info->qgroup_rescan_progress, 0, |
2727 | sizeof(fs_info->qgroup_rescan_progress)); | |
b382a324 | 2728 | fs_info->qgroup_rescan_progress.objectid = progress_objectid; |
190631f1 | 2729 | init_completion(&fs_info->qgroup_rescan_completion); |
8d9eddad | 2730 | fs_info->qgroup_rescan_running = true; |
b382a324 JS |
2731 | |
2732 | spin_unlock(&fs_info->qgroup_lock); | |
2733 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2734 | ||
b382a324 JS |
2735 | memset(&fs_info->qgroup_rescan_work, 0, |
2736 | sizeof(fs_info->qgroup_rescan_work)); | |
fc97fab0 | 2737 | btrfs_init_work(&fs_info->qgroup_rescan_work, |
9e0af237 | 2738 | btrfs_qgroup_rescan_helper, |
fc97fab0 | 2739 | btrfs_qgroup_rescan_worker, NULL, NULL); |
b382a324 JS |
2740 | |
2741 | if (ret) { | |
2742 | err: | |
efe120a0 | 2743 | btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret); |
b382a324 JS |
2744 | return ret; |
2745 | } | |
2746 | ||
2747 | return 0; | |
2748 | } | |
2749 | ||
2750 | static void | |
2751 | qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info) | |
2752 | { | |
2753 | struct rb_node *n; | |
2754 | struct btrfs_qgroup *qgroup; | |
2755 | ||
2756 | spin_lock(&fs_info->qgroup_lock); | |
2f232036 JS |
2757 | /* clear all current qgroup tracking information */ |
2758 | for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) { | |
2759 | qgroup = rb_entry(n, struct btrfs_qgroup, node); | |
2760 | qgroup->rfer = 0; | |
2761 | qgroup->rfer_cmpr = 0; | |
2762 | qgroup->excl = 0; | |
2763 | qgroup->excl_cmpr = 0; | |
2764 | } | |
2765 | spin_unlock(&fs_info->qgroup_lock); | |
b382a324 | 2766 | } |
2f232036 | 2767 | |
b382a324 JS |
2768 | int |
2769 | btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info) | |
2770 | { | |
2771 | int ret = 0; | |
2772 | struct btrfs_trans_handle *trans; | |
2773 | ||
2774 | ret = qgroup_rescan_init(fs_info, 0, 1); | |
2775 | if (ret) | |
2776 | return ret; | |
2777 | ||
2778 | /* | |
2779 | * We have set the rescan_progress to 0, which means no more | |
2780 | * delayed refs will be accounted by btrfs_qgroup_account_ref. | |
2781 | * However, btrfs_qgroup_account_ref may be right after its call | |
2782 | * to btrfs_find_all_roots, in which case it would still do the | |
2783 | * accounting. | |
2784 | * To solve this, we're committing the transaction, which will | |
2785 | * ensure we run all delayed refs and only after that, we are | |
2786 | * going to clear all tracking information for a clean start. | |
2787 | */ | |
2788 | ||
2789 | trans = btrfs_join_transaction(fs_info->fs_root); | |
2790 | if (IS_ERR(trans)) { | |
2791 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2792 | return PTR_ERR(trans); | |
2793 | } | |
3a45bb20 | 2794 | ret = btrfs_commit_transaction(trans); |
b382a324 JS |
2795 | if (ret) { |
2796 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2797 | return ret; | |
2798 | } | |
2799 | ||
2800 | qgroup_rescan_zero_tracking(fs_info); | |
2801 | ||
fc97fab0 QW |
2802 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
2803 | &fs_info->qgroup_rescan_work); | |
2f232036 JS |
2804 | |
2805 | return 0; | |
2806 | } | |
57254b6e | 2807 | |
d06f23d6 JM |
2808 | int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info, |
2809 | bool interruptible) | |
57254b6e JS |
2810 | { |
2811 | int running; | |
2812 | int ret = 0; | |
2813 | ||
2814 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2815 | spin_lock(&fs_info->qgroup_lock); | |
d2c609b8 | 2816 | running = fs_info->qgroup_rescan_running; |
57254b6e JS |
2817 | spin_unlock(&fs_info->qgroup_lock); |
2818 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2819 | ||
d06f23d6 JM |
2820 | if (!running) |
2821 | return 0; | |
2822 | ||
2823 | if (interruptible) | |
57254b6e JS |
2824 | ret = wait_for_completion_interruptible( |
2825 | &fs_info->qgroup_rescan_completion); | |
d06f23d6 JM |
2826 | else |
2827 | wait_for_completion(&fs_info->qgroup_rescan_completion); | |
57254b6e JS |
2828 | |
2829 | return ret; | |
2830 | } | |
b382a324 JS |
2831 | |
2832 | /* | |
2833 | * this is only called from open_ctree where we're still single threaded, thus | |
2834 | * locking is omitted here. | |
2835 | */ | |
2836 | void | |
2837 | btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info) | |
2838 | { | |
2839 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) | |
fc97fab0 QW |
2840 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
2841 | &fs_info->qgroup_rescan_work); | |
b382a324 | 2842 | } |
52472553 QW |
2843 | |
2844 | /* | |
2845 | * Reserve qgroup space for range [start, start + len). | |
2846 | * | |
2847 | * This function will either reserve space from related qgroups or doing | |
2848 | * nothing if the range is already reserved. | |
2849 | * | |
2850 | * Return 0 for successful reserve | |
2851 | * Return <0 for error (including -EQUOT) | |
2852 | * | |
2853 | * NOTE: this function may sleep for memory allocation. | |
2854 | */ | |
2855 | int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len) | |
2856 | { | |
2857 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2858 | struct extent_changeset changeset; | |
2859 | struct ulist_node *unode; | |
2860 | struct ulist_iterator uiter; | |
2861 | int ret; | |
2862 | ||
afcdd129 JB |
2863 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) || |
2864 | !is_fstree(root->objectid) || len == 0) | |
52472553 QW |
2865 | return 0; |
2866 | ||
2867 | changeset.bytes_changed = 0; | |
53d32359 | 2868 | ulist_init(&changeset.range_changed); |
52472553 | 2869 | ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start, |
2c53b912 | 2870 | start + len -1, EXTENT_QGROUP_RESERVED, &changeset); |
81fb6f77 QW |
2871 | trace_btrfs_qgroup_reserve_data(inode, start, len, |
2872 | changeset.bytes_changed, | |
2873 | QGROUP_RESERVE); | |
52472553 QW |
2874 | if (ret < 0) |
2875 | goto cleanup; | |
003d7c59 | 2876 | ret = qgroup_reserve(root, changeset.bytes_changed, true); |
52472553 QW |
2877 | if (ret < 0) |
2878 | goto cleanup; | |
2879 | ||
6655bc3d | 2880 | ulist_release(&changeset.range_changed); |
52472553 QW |
2881 | return ret; |
2882 | ||
2883 | cleanup: | |
2884 | /* cleanup already reserved ranges */ | |
2885 | ULIST_ITER_INIT(&uiter); | |
53d32359 | 2886 | while ((unode = ulist_next(&changeset.range_changed, &uiter))) |
52472553 QW |
2887 | clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val, |
2888 | unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL, | |
2889 | GFP_NOFS); | |
6655bc3d | 2890 | ulist_release(&changeset.range_changed); |
52472553 QW |
2891 | return ret; |
2892 | } | |
f695fdce QW |
2893 | |
2894 | static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len, | |
2895 | int free) | |
2896 | { | |
2897 | struct extent_changeset changeset; | |
81fb6f77 | 2898 | int trace_op = QGROUP_RELEASE; |
f695fdce QW |
2899 | int ret; |
2900 | ||
2901 | changeset.bytes_changed = 0; | |
53d32359 | 2902 | ulist_init(&changeset.range_changed); |
f695fdce | 2903 | ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start, |
f734c44a | 2904 | start + len -1, EXTENT_QGROUP_RESERVED, &changeset); |
f695fdce QW |
2905 | if (ret < 0) |
2906 | goto out; | |
2907 | ||
d51ea5dd | 2908 | if (free) |
81fb6f77 | 2909 | trace_op = QGROUP_FREE; |
81fb6f77 QW |
2910 | trace_btrfs_qgroup_release_data(inode, start, len, |
2911 | changeset.bytes_changed, trace_op); | |
d51ea5dd QW |
2912 | if (free) |
2913 | btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info, | |
2914 | BTRFS_I(inode)->root->objectid, | |
2915 | changeset.bytes_changed); | |
f695fdce | 2916 | out: |
6655bc3d | 2917 | ulist_release(&changeset.range_changed); |
f695fdce QW |
2918 | return ret; |
2919 | } | |
2920 | ||
2921 | /* | |
2922 | * Free a reserved space range from io_tree and related qgroups | |
2923 | * | |
2924 | * Should be called when a range of pages get invalidated before reaching disk. | |
2925 | * Or for error cleanup case. | |
2926 | * | |
2927 | * For data written to disk, use btrfs_qgroup_release_data(). | |
2928 | * | |
2929 | * NOTE: This function may sleep for memory allocation. | |
2930 | */ | |
2931 | int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len) | |
2932 | { | |
2933 | return __btrfs_qgroup_release_data(inode, start, len, 1); | |
2934 | } | |
2935 | ||
2936 | /* | |
2937 | * Release a reserved space range from io_tree only. | |
2938 | * | |
2939 | * Should be called when a range of pages get written to disk and corresponding | |
2940 | * FILE_EXTENT is inserted into corresponding root. | |
2941 | * | |
2942 | * Since new qgroup accounting framework will only update qgroup numbers at | |
2943 | * commit_transaction() time, its reserved space shouldn't be freed from | |
2944 | * related qgroups. | |
2945 | * | |
2946 | * But we should release the range from io_tree, to allow further write to be | |
2947 | * COWed. | |
2948 | * | |
2949 | * NOTE: This function may sleep for memory allocation. | |
2950 | */ | |
2951 | int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len) | |
2952 | { | |
2953 | return __btrfs_qgroup_release_data(inode, start, len, 0); | |
2954 | } | |
55eeaf05 | 2955 | |
003d7c59 JM |
2956 | int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, |
2957 | bool enforce) | |
55eeaf05 | 2958 | { |
0b246afa | 2959 | struct btrfs_fs_info *fs_info = root->fs_info; |
55eeaf05 QW |
2960 | int ret; |
2961 | ||
0b246afa | 2962 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || |
afcdd129 | 2963 | !is_fstree(root->objectid) || num_bytes == 0) |
55eeaf05 QW |
2964 | return 0; |
2965 | ||
0b246afa | 2966 | BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize)); |
3159fe7b | 2967 | trace_qgroup_meta_reserve(root, (s64)num_bytes); |
003d7c59 | 2968 | ret = qgroup_reserve(root, num_bytes, enforce); |
55eeaf05 QW |
2969 | if (ret < 0) |
2970 | return ret; | |
ce0dcee6 | 2971 | atomic64_add(num_bytes, &root->qgroup_meta_rsv); |
55eeaf05 QW |
2972 | return ret; |
2973 | } | |
2974 | ||
2975 | void btrfs_qgroup_free_meta_all(struct btrfs_root *root) | |
2976 | { | |
0b246afa | 2977 | struct btrfs_fs_info *fs_info = root->fs_info; |
ce0dcee6 | 2978 | u64 reserved; |
55eeaf05 | 2979 | |
0b246afa | 2980 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || |
afcdd129 | 2981 | !is_fstree(root->objectid)) |
55eeaf05 QW |
2982 | return; |
2983 | ||
ce0dcee6 | 2984 | reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0); |
55eeaf05 QW |
2985 | if (reserved == 0) |
2986 | return; | |
3159fe7b | 2987 | trace_qgroup_meta_reserve(root, -(s64)reserved); |
0b08e1f4 | 2988 | btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved); |
55eeaf05 QW |
2989 | } |
2990 | ||
2991 | void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes) | |
2992 | { | |
0b246afa JM |
2993 | struct btrfs_fs_info *fs_info = root->fs_info; |
2994 | ||
2995 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || | |
afcdd129 | 2996 | !is_fstree(root->objectid)) |
55eeaf05 QW |
2997 | return; |
2998 | ||
0b246afa | 2999 | BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize)); |
ce0dcee6 GR |
3000 | WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes); |
3001 | atomic64_sub(num_bytes, &root->qgroup_meta_rsv); | |
3159fe7b | 3002 | trace_qgroup_meta_reserve(root, -(s64)num_bytes); |
0b08e1f4 | 3003 | btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes); |
55eeaf05 | 3004 | } |
56fa9d07 QW |
3005 | |
3006 | /* | |
01327610 | 3007 | * Check qgroup reserved space leaking, normally at destroy inode |
56fa9d07 QW |
3008 | * time |
3009 | */ | |
3010 | void btrfs_qgroup_check_reserved_leak(struct inode *inode) | |
3011 | { | |
3012 | struct extent_changeset changeset; | |
3013 | struct ulist_node *unode; | |
3014 | struct ulist_iterator iter; | |
3015 | int ret; | |
3016 | ||
3017 | changeset.bytes_changed = 0; | |
53d32359 | 3018 | ulist_init(&changeset.range_changed); |
56fa9d07 | 3019 | ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1, |
f734c44a | 3020 | EXTENT_QGROUP_RESERVED, &changeset); |
56fa9d07 QW |
3021 | |
3022 | WARN_ON(ret < 0); | |
3023 | if (WARN_ON(changeset.bytes_changed)) { | |
3024 | ULIST_ITER_INIT(&iter); | |
53d32359 | 3025 | while ((unode = ulist_next(&changeset.range_changed, &iter))) { |
56fa9d07 QW |
3026 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
3027 | "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu", | |
3028 | inode->i_ino, unode->val, unode->aux); | |
3029 | } | |
0b08e1f4 DS |
3030 | btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info, |
3031 | BTRFS_I(inode)->root->objectid, | |
3032 | changeset.bytes_changed); | |
3033 | ||
56fa9d07 | 3034 | } |
6655bc3d | 3035 | ulist_release(&changeset.range_changed); |
56fa9d07 | 3036 | } |