]>
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 AJ |
36 | |
37 | /* TODO XXX FIXME | |
38 | * - subvol delete -> delete when ref goes to 0? delete limits also? | |
39 | * - reorganize keys | |
40 | * - compressed | |
41 | * - sync | |
bed92eae AJ |
42 | * - copy also limits on subvol creation |
43 | * - limit | |
44 | * - caches fuer ulists | |
45 | * - performance benchmarks | |
46 | * - check all ioctl parameters | |
47 | */ | |
48 | ||
49 | /* | |
50 | * one struct for each qgroup, organized in fs_info->qgroup_tree. | |
51 | */ | |
52 | struct btrfs_qgroup { | |
53 | u64 qgroupid; | |
54 | ||
55 | /* | |
56 | * state | |
57 | */ | |
58 | u64 rfer; /* referenced */ | |
59 | u64 rfer_cmpr; /* referenced compressed */ | |
60 | u64 excl; /* exclusive */ | |
61 | u64 excl_cmpr; /* exclusive compressed */ | |
62 | ||
63 | /* | |
64 | * limits | |
65 | */ | |
66 | u64 lim_flags; /* which limits are set */ | |
67 | u64 max_rfer; | |
68 | u64 max_excl; | |
69 | u64 rsv_rfer; | |
70 | u64 rsv_excl; | |
71 | ||
72 | /* | |
73 | * reservation tracking | |
74 | */ | |
31193213 | 75 | u64 may_use; |
bed92eae AJ |
76 | u64 reserved; |
77 | ||
78 | /* | |
79 | * lists | |
80 | */ | |
81 | struct list_head groups; /* groups this group is member of */ | |
82 | struct list_head members; /* groups that are members of this group */ | |
83 | struct list_head dirty; /* dirty groups */ | |
84 | struct rb_node node; /* tree of qgroups */ | |
85 | ||
86 | /* | |
87 | * temp variables for accounting operations | |
88 | */ | |
fcebe456 JB |
89 | u64 old_refcnt; |
90 | u64 new_refcnt; | |
bed92eae AJ |
91 | }; |
92 | ||
93 | /* | |
94 | * glue structure to represent the relations between qgroups. | |
95 | */ | |
96 | struct btrfs_qgroup_list { | |
97 | struct list_head next_group; | |
98 | struct list_head next_member; | |
99 | struct btrfs_qgroup *group; | |
100 | struct btrfs_qgroup *member; | |
101 | }; | |
102 | ||
fcebe456 JB |
103 | #define ptr_to_u64(x) ((u64)(uintptr_t)x) |
104 | #define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x) | |
105 | ||
b382a324 JS |
106 | static int |
107 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, | |
108 | int init_flags); | |
109 | static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info); | |
2f232036 | 110 | |
58400fce | 111 | /* must be called with qgroup_ioctl_lock held */ |
bed92eae AJ |
112 | static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info, |
113 | u64 qgroupid) | |
114 | { | |
115 | struct rb_node *n = fs_info->qgroup_tree.rb_node; | |
116 | struct btrfs_qgroup *qgroup; | |
117 | ||
118 | while (n) { | |
119 | qgroup = rb_entry(n, struct btrfs_qgroup, node); | |
120 | if (qgroup->qgroupid < qgroupid) | |
121 | n = n->rb_left; | |
122 | else if (qgroup->qgroupid > qgroupid) | |
123 | n = n->rb_right; | |
124 | else | |
125 | return qgroup; | |
126 | } | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | /* must be called with qgroup_lock held */ | |
131 | static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info, | |
132 | u64 qgroupid) | |
133 | { | |
134 | struct rb_node **p = &fs_info->qgroup_tree.rb_node; | |
135 | struct rb_node *parent = NULL; | |
136 | struct btrfs_qgroup *qgroup; | |
137 | ||
138 | while (*p) { | |
139 | parent = *p; | |
140 | qgroup = rb_entry(parent, struct btrfs_qgroup, node); | |
141 | ||
142 | if (qgroup->qgroupid < qgroupid) | |
143 | p = &(*p)->rb_left; | |
144 | else if (qgroup->qgroupid > qgroupid) | |
145 | p = &(*p)->rb_right; | |
146 | else | |
147 | return qgroup; | |
148 | } | |
149 | ||
150 | qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC); | |
151 | if (!qgroup) | |
152 | return ERR_PTR(-ENOMEM); | |
153 | ||
154 | qgroup->qgroupid = qgroupid; | |
155 | INIT_LIST_HEAD(&qgroup->groups); | |
156 | INIT_LIST_HEAD(&qgroup->members); | |
157 | INIT_LIST_HEAD(&qgroup->dirty); | |
158 | ||
159 | rb_link_node(&qgroup->node, parent, p); | |
160 | rb_insert_color(&qgroup->node, &fs_info->qgroup_tree); | |
161 | ||
162 | return qgroup; | |
163 | } | |
164 | ||
4082bd3d | 165 | static void __del_qgroup_rb(struct btrfs_qgroup *qgroup) |
bed92eae | 166 | { |
bed92eae AJ |
167 | struct btrfs_qgroup_list *list; |
168 | ||
bed92eae | 169 | list_del(&qgroup->dirty); |
bed92eae AJ |
170 | while (!list_empty(&qgroup->groups)) { |
171 | list = list_first_entry(&qgroup->groups, | |
172 | struct btrfs_qgroup_list, next_group); | |
173 | list_del(&list->next_group); | |
174 | list_del(&list->next_member); | |
175 | kfree(list); | |
176 | } | |
177 | ||
178 | while (!list_empty(&qgroup->members)) { | |
179 | list = list_first_entry(&qgroup->members, | |
180 | struct btrfs_qgroup_list, next_member); | |
181 | list_del(&list->next_group); | |
182 | list_del(&list->next_member); | |
183 | kfree(list); | |
184 | } | |
185 | kfree(qgroup); | |
4082bd3d | 186 | } |
bed92eae | 187 | |
4082bd3d WS |
188 | /* must be called with qgroup_lock held */ |
189 | static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid) | |
190 | { | |
191 | struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid); | |
192 | ||
193 | if (!qgroup) | |
194 | return -ENOENT; | |
195 | ||
196 | rb_erase(&qgroup->node, &fs_info->qgroup_tree); | |
197 | __del_qgroup_rb(qgroup); | |
bed92eae AJ |
198 | return 0; |
199 | } | |
200 | ||
201 | /* must be called with qgroup_lock held */ | |
202 | static int add_relation_rb(struct btrfs_fs_info *fs_info, | |
203 | u64 memberid, u64 parentid) | |
204 | { | |
205 | struct btrfs_qgroup *member; | |
206 | struct btrfs_qgroup *parent; | |
207 | struct btrfs_qgroup_list *list; | |
208 | ||
209 | member = find_qgroup_rb(fs_info, memberid); | |
210 | parent = find_qgroup_rb(fs_info, parentid); | |
211 | if (!member || !parent) | |
212 | return -ENOENT; | |
213 | ||
214 | list = kzalloc(sizeof(*list), GFP_ATOMIC); | |
215 | if (!list) | |
216 | return -ENOMEM; | |
217 | ||
218 | list->group = parent; | |
219 | list->member = member; | |
220 | list_add_tail(&list->next_group, &member->groups); | |
221 | list_add_tail(&list->next_member, &parent->members); | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | /* must be called with qgroup_lock held */ | |
227 | static int del_relation_rb(struct btrfs_fs_info *fs_info, | |
228 | u64 memberid, u64 parentid) | |
229 | { | |
230 | struct btrfs_qgroup *member; | |
231 | struct btrfs_qgroup *parent; | |
232 | struct btrfs_qgroup_list *list; | |
233 | ||
234 | member = find_qgroup_rb(fs_info, memberid); | |
235 | parent = find_qgroup_rb(fs_info, parentid); | |
236 | if (!member || !parent) | |
237 | return -ENOENT; | |
238 | ||
239 | list_for_each_entry(list, &member->groups, next_group) { | |
240 | if (list->group == parent) { | |
241 | list_del(&list->next_group); | |
242 | list_del(&list->next_member); | |
243 | kfree(list); | |
244 | return 0; | |
245 | } | |
246 | } | |
247 | return -ENOENT; | |
248 | } | |
249 | ||
faa2dbf0 JB |
250 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
251 | int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid, | |
252 | u64 rfer, u64 excl) | |
253 | { | |
254 | struct btrfs_qgroup *qgroup; | |
255 | ||
256 | qgroup = find_qgroup_rb(fs_info, qgroupid); | |
257 | if (!qgroup) | |
258 | return -EINVAL; | |
259 | if (qgroup->rfer != rfer || qgroup->excl != excl) | |
260 | return -EINVAL; | |
261 | return 0; | |
262 | } | |
263 | #endif | |
264 | ||
bed92eae AJ |
265 | /* |
266 | * The full config is read in one go, only called from open_ctree() | |
267 | * It doesn't use any locking, as at this point we're still single-threaded | |
268 | */ | |
269 | int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) | |
270 | { | |
271 | struct btrfs_key key; | |
272 | struct btrfs_key found_key; | |
273 | struct btrfs_root *quota_root = fs_info->quota_root; | |
274 | struct btrfs_path *path = NULL; | |
275 | struct extent_buffer *l; | |
276 | int slot; | |
277 | int ret = 0; | |
278 | u64 flags = 0; | |
b382a324 | 279 | u64 rescan_progress = 0; |
bed92eae AJ |
280 | |
281 | if (!fs_info->quota_enabled) | |
282 | return 0; | |
283 | ||
1e8f9158 WS |
284 | fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS); |
285 | if (!fs_info->qgroup_ulist) { | |
286 | ret = -ENOMEM; | |
287 | goto out; | |
288 | } | |
289 | ||
bed92eae AJ |
290 | path = btrfs_alloc_path(); |
291 | if (!path) { | |
292 | ret = -ENOMEM; | |
293 | goto out; | |
294 | } | |
295 | ||
296 | /* default this to quota off, in case no status key is found */ | |
297 | fs_info->qgroup_flags = 0; | |
298 | ||
299 | /* | |
300 | * pass 1: read status, all qgroup infos and limits | |
301 | */ | |
302 | key.objectid = 0; | |
303 | key.type = 0; | |
304 | key.offset = 0; | |
305 | ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1); | |
306 | if (ret) | |
307 | goto out; | |
308 | ||
309 | while (1) { | |
310 | struct btrfs_qgroup *qgroup; | |
311 | ||
312 | slot = path->slots[0]; | |
313 | l = path->nodes[0]; | |
314 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
315 | ||
316 | if (found_key.type == BTRFS_QGROUP_STATUS_KEY) { | |
317 | struct btrfs_qgroup_status_item *ptr; | |
318 | ||
319 | ptr = btrfs_item_ptr(l, slot, | |
320 | struct btrfs_qgroup_status_item); | |
321 | ||
322 | if (btrfs_qgroup_status_version(l, ptr) != | |
323 | BTRFS_QGROUP_STATUS_VERSION) { | |
efe120a0 FH |
324 | btrfs_err(fs_info, |
325 | "old qgroup version, quota disabled"); | |
bed92eae AJ |
326 | goto out; |
327 | } | |
328 | if (btrfs_qgroup_status_generation(l, ptr) != | |
329 | fs_info->generation) { | |
330 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
efe120a0 FH |
331 | btrfs_err(fs_info, |
332 | "qgroup generation mismatch, " | |
333 | "marked as inconsistent"); | |
bed92eae AJ |
334 | } |
335 | fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, | |
336 | ptr); | |
b382a324 | 337 | rescan_progress = btrfs_qgroup_status_rescan(l, ptr); |
bed92eae AJ |
338 | goto next1; |
339 | } | |
340 | ||
341 | if (found_key.type != BTRFS_QGROUP_INFO_KEY && | |
342 | found_key.type != BTRFS_QGROUP_LIMIT_KEY) | |
343 | goto next1; | |
344 | ||
345 | qgroup = find_qgroup_rb(fs_info, found_key.offset); | |
346 | if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) || | |
347 | (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) { | |
efe120a0 | 348 | btrfs_err(fs_info, "inconsitent qgroup config"); |
bed92eae AJ |
349 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
350 | } | |
351 | if (!qgroup) { | |
352 | qgroup = add_qgroup_rb(fs_info, found_key.offset); | |
353 | if (IS_ERR(qgroup)) { | |
354 | ret = PTR_ERR(qgroup); | |
355 | goto out; | |
356 | } | |
357 | } | |
358 | switch (found_key.type) { | |
359 | case BTRFS_QGROUP_INFO_KEY: { | |
360 | struct btrfs_qgroup_info_item *ptr; | |
361 | ||
362 | ptr = btrfs_item_ptr(l, slot, | |
363 | struct btrfs_qgroup_info_item); | |
364 | qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr); | |
365 | qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr); | |
366 | qgroup->excl = btrfs_qgroup_info_excl(l, ptr); | |
367 | qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr); | |
368 | /* generation currently unused */ | |
369 | break; | |
370 | } | |
371 | case BTRFS_QGROUP_LIMIT_KEY: { | |
372 | struct btrfs_qgroup_limit_item *ptr; | |
373 | ||
374 | ptr = btrfs_item_ptr(l, slot, | |
375 | struct btrfs_qgroup_limit_item); | |
376 | qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr); | |
377 | qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr); | |
378 | qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr); | |
379 | qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr); | |
380 | qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr); | |
381 | break; | |
382 | } | |
383 | } | |
384 | next1: | |
385 | ret = btrfs_next_item(quota_root, path); | |
386 | if (ret < 0) | |
387 | goto out; | |
388 | if (ret) | |
389 | break; | |
390 | } | |
391 | btrfs_release_path(path); | |
392 | ||
393 | /* | |
394 | * pass 2: read all qgroup relations | |
395 | */ | |
396 | key.objectid = 0; | |
397 | key.type = BTRFS_QGROUP_RELATION_KEY; | |
398 | key.offset = 0; | |
399 | ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0); | |
400 | if (ret) | |
401 | goto out; | |
402 | while (1) { | |
403 | slot = path->slots[0]; | |
404 | l = path->nodes[0]; | |
405 | btrfs_item_key_to_cpu(l, &found_key, slot); | |
406 | ||
407 | if (found_key.type != BTRFS_QGROUP_RELATION_KEY) | |
408 | goto next2; | |
409 | ||
410 | if (found_key.objectid > found_key.offset) { | |
411 | /* parent <- member, not needed to build config */ | |
412 | /* FIXME should we omit the key completely? */ | |
413 | goto next2; | |
414 | } | |
415 | ||
416 | ret = add_relation_rb(fs_info, found_key.objectid, | |
417 | found_key.offset); | |
ff24858c | 418 | if (ret == -ENOENT) { |
efe120a0 FH |
419 | btrfs_warn(fs_info, |
420 | "orphan qgroup relation 0x%llx->0x%llx", | |
c1c9ff7c | 421 | found_key.objectid, found_key.offset); |
ff24858c AJ |
422 | ret = 0; /* ignore the error */ |
423 | } | |
bed92eae AJ |
424 | if (ret) |
425 | goto out; | |
426 | next2: | |
427 | ret = btrfs_next_item(quota_root, path); | |
428 | if (ret < 0) | |
429 | goto out; | |
430 | if (ret) | |
431 | break; | |
432 | } | |
433 | out: | |
434 | fs_info->qgroup_flags |= flags; | |
435 | if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) { | |
436 | fs_info->quota_enabled = 0; | |
437 | fs_info->pending_quota_state = 0; | |
b382a324 JS |
438 | } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN && |
439 | ret >= 0) { | |
440 | ret = qgroup_rescan_init(fs_info, rescan_progress, 0); | |
bed92eae AJ |
441 | } |
442 | btrfs_free_path(path); | |
443 | ||
eb1716af | 444 | if (ret < 0) { |
1e8f9158 | 445 | ulist_free(fs_info->qgroup_ulist); |
eb1716af | 446 | fs_info->qgroup_ulist = NULL; |
b382a324 | 447 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
eb1716af | 448 | } |
1e8f9158 | 449 | |
bed92eae AJ |
450 | return ret < 0 ? ret : 0; |
451 | } | |
452 | ||
453 | /* | |
e685da14 WS |
454 | * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(), |
455 | * first two are in single-threaded paths.And for the third one, we have set | |
456 | * quota_root to be null with qgroup_lock held before, so it is safe to clean | |
457 | * up the in-memory structures without qgroup_lock held. | |
bed92eae AJ |
458 | */ |
459 | void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info) | |
460 | { | |
461 | struct rb_node *n; | |
462 | struct btrfs_qgroup *qgroup; | |
bed92eae AJ |
463 | |
464 | while ((n = rb_first(&fs_info->qgroup_tree))) { | |
465 | qgroup = rb_entry(n, struct btrfs_qgroup, node); | |
466 | rb_erase(n, &fs_info->qgroup_tree); | |
4082bd3d | 467 | __del_qgroup_rb(qgroup); |
bed92eae | 468 | } |
1e7bac1e WS |
469 | /* |
470 | * we call btrfs_free_qgroup_config() when umounting | |
471 | * filesystem and disabling quota, so we set qgroup_ulit | |
472 | * to be null here to avoid double free. | |
473 | */ | |
1e8f9158 | 474 | ulist_free(fs_info->qgroup_ulist); |
1e7bac1e | 475 | fs_info->qgroup_ulist = NULL; |
bed92eae AJ |
476 | } |
477 | ||
478 | static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, | |
479 | struct btrfs_root *quota_root, | |
480 | u64 src, u64 dst) | |
481 | { | |
482 | int ret; | |
483 | struct btrfs_path *path; | |
484 | struct btrfs_key key; | |
485 | ||
486 | path = btrfs_alloc_path(); | |
487 | if (!path) | |
488 | return -ENOMEM; | |
489 | ||
490 | key.objectid = src; | |
491 | key.type = BTRFS_QGROUP_RELATION_KEY; | |
492 | key.offset = dst; | |
493 | ||
494 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0); | |
495 | ||
496 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
497 | ||
498 | btrfs_free_path(path); | |
499 | return ret; | |
500 | } | |
501 | ||
502 | static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, | |
503 | struct btrfs_root *quota_root, | |
504 | u64 src, u64 dst) | |
505 | { | |
506 | int ret; | |
507 | struct btrfs_path *path; | |
508 | struct btrfs_key key; | |
509 | ||
510 | path = btrfs_alloc_path(); | |
511 | if (!path) | |
512 | return -ENOMEM; | |
513 | ||
514 | key.objectid = src; | |
515 | key.type = BTRFS_QGROUP_RELATION_KEY; | |
516 | key.offset = dst; | |
517 | ||
518 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); | |
519 | if (ret < 0) | |
520 | goto out; | |
521 | ||
522 | if (ret > 0) { | |
523 | ret = -ENOENT; | |
524 | goto out; | |
525 | } | |
526 | ||
527 | ret = btrfs_del_item(trans, quota_root, path); | |
528 | out: | |
529 | btrfs_free_path(path); | |
530 | return ret; | |
531 | } | |
532 | ||
533 | static int add_qgroup_item(struct btrfs_trans_handle *trans, | |
534 | struct btrfs_root *quota_root, u64 qgroupid) | |
535 | { | |
536 | int ret; | |
537 | struct btrfs_path *path; | |
538 | struct btrfs_qgroup_info_item *qgroup_info; | |
539 | struct btrfs_qgroup_limit_item *qgroup_limit; | |
540 | struct extent_buffer *leaf; | |
541 | struct btrfs_key key; | |
542 | ||
fccb84c9 | 543 | if (btrfs_test_is_dummy_root(quota_root)) |
faa2dbf0 | 544 | return 0; |
fccb84c9 | 545 | |
bed92eae AJ |
546 | path = btrfs_alloc_path(); |
547 | if (!path) | |
548 | return -ENOMEM; | |
549 | ||
550 | key.objectid = 0; | |
551 | key.type = BTRFS_QGROUP_INFO_KEY; | |
552 | key.offset = qgroupid; | |
553 | ||
0b4699dc MF |
554 | /* |
555 | * Avoid a transaction abort by catching -EEXIST here. In that | |
556 | * case, we proceed by re-initializing the existing structure | |
557 | * on disk. | |
558 | */ | |
559 | ||
bed92eae AJ |
560 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
561 | sizeof(*qgroup_info)); | |
0b4699dc | 562 | if (ret && ret != -EEXIST) |
bed92eae AJ |
563 | goto out; |
564 | ||
565 | leaf = path->nodes[0]; | |
566 | qgroup_info = btrfs_item_ptr(leaf, path->slots[0], | |
567 | struct btrfs_qgroup_info_item); | |
568 | btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid); | |
569 | btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0); | |
570 | btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0); | |
571 | btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0); | |
572 | btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0); | |
573 | ||
574 | btrfs_mark_buffer_dirty(leaf); | |
575 | ||
576 | btrfs_release_path(path); | |
577 | ||
578 | key.type = BTRFS_QGROUP_LIMIT_KEY; | |
579 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, | |
580 | sizeof(*qgroup_limit)); | |
0b4699dc | 581 | if (ret && ret != -EEXIST) |
bed92eae AJ |
582 | goto out; |
583 | ||
584 | leaf = path->nodes[0]; | |
585 | qgroup_limit = btrfs_item_ptr(leaf, path->slots[0], | |
586 | struct btrfs_qgroup_limit_item); | |
587 | btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0); | |
588 | btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0); | |
589 | btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0); | |
590 | btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0); | |
591 | btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0); | |
592 | ||
593 | btrfs_mark_buffer_dirty(leaf); | |
594 | ||
595 | ret = 0; | |
596 | out: | |
597 | btrfs_free_path(path); | |
598 | return ret; | |
599 | } | |
600 | ||
601 | static int del_qgroup_item(struct btrfs_trans_handle *trans, | |
602 | struct btrfs_root *quota_root, u64 qgroupid) | |
603 | { | |
604 | int ret; | |
605 | struct btrfs_path *path; | |
606 | struct btrfs_key key; | |
607 | ||
608 | path = btrfs_alloc_path(); | |
609 | if (!path) | |
610 | return -ENOMEM; | |
611 | ||
612 | key.objectid = 0; | |
613 | key.type = BTRFS_QGROUP_INFO_KEY; | |
614 | key.offset = qgroupid; | |
615 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); | |
616 | if (ret < 0) | |
617 | goto out; | |
618 | ||
619 | if (ret > 0) { | |
620 | ret = -ENOENT; | |
621 | goto out; | |
622 | } | |
623 | ||
624 | ret = btrfs_del_item(trans, quota_root, path); | |
625 | if (ret) | |
626 | goto out; | |
627 | ||
628 | btrfs_release_path(path); | |
629 | ||
630 | key.type = BTRFS_QGROUP_LIMIT_KEY; | |
631 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); | |
632 | if (ret < 0) | |
633 | goto out; | |
634 | ||
635 | if (ret > 0) { | |
636 | ret = -ENOENT; | |
637 | goto out; | |
638 | } | |
639 | ||
640 | ret = btrfs_del_item(trans, quota_root, path); | |
641 | ||
642 | out: | |
643 | btrfs_free_path(path); | |
644 | return ret; | |
645 | } | |
646 | ||
647 | static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, | |
1510e71c DY |
648 | struct btrfs_root *root, |
649 | struct btrfs_qgroup *qgroup) | |
bed92eae AJ |
650 | { |
651 | struct btrfs_path *path; | |
652 | struct btrfs_key key; | |
653 | struct extent_buffer *l; | |
654 | struct btrfs_qgroup_limit_item *qgroup_limit; | |
655 | int ret; | |
656 | int slot; | |
657 | ||
658 | key.objectid = 0; | |
659 | key.type = BTRFS_QGROUP_LIMIT_KEY; | |
1510e71c | 660 | key.offset = qgroup->qgroupid; |
bed92eae AJ |
661 | |
662 | path = btrfs_alloc_path(); | |
84cbe2f7 WS |
663 | if (!path) |
664 | return -ENOMEM; | |
665 | ||
bed92eae AJ |
666 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
667 | if (ret > 0) | |
668 | ret = -ENOENT; | |
669 | ||
670 | if (ret) | |
671 | goto out; | |
672 | ||
673 | l = path->nodes[0]; | |
674 | slot = path->slots[0]; | |
a3df41ee | 675 | qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item); |
1510e71c DY |
676 | btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags); |
677 | btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer); | |
678 | btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl); | |
679 | btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer); | |
680 | btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl); | |
bed92eae AJ |
681 | |
682 | btrfs_mark_buffer_dirty(l); | |
683 | ||
684 | out: | |
685 | btrfs_free_path(path); | |
686 | return ret; | |
687 | } | |
688 | ||
689 | static int update_qgroup_info_item(struct btrfs_trans_handle *trans, | |
690 | struct btrfs_root *root, | |
691 | struct btrfs_qgroup *qgroup) | |
692 | { | |
693 | struct btrfs_path *path; | |
694 | struct btrfs_key key; | |
695 | struct extent_buffer *l; | |
696 | struct btrfs_qgroup_info_item *qgroup_info; | |
697 | int ret; | |
698 | int slot; | |
699 | ||
fccb84c9 | 700 | if (btrfs_test_is_dummy_root(root)) |
faa2dbf0 | 701 | return 0; |
fccb84c9 | 702 | |
bed92eae AJ |
703 | key.objectid = 0; |
704 | key.type = BTRFS_QGROUP_INFO_KEY; | |
705 | key.offset = qgroup->qgroupid; | |
706 | ||
707 | path = btrfs_alloc_path(); | |
84cbe2f7 WS |
708 | if (!path) |
709 | return -ENOMEM; | |
710 | ||
bed92eae AJ |
711 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
712 | if (ret > 0) | |
713 | ret = -ENOENT; | |
714 | ||
715 | if (ret) | |
716 | goto out; | |
717 | ||
718 | l = path->nodes[0]; | |
719 | slot = path->slots[0]; | |
a3df41ee | 720 | qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item); |
bed92eae AJ |
721 | btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid); |
722 | btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer); | |
723 | btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr); | |
724 | btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl); | |
725 | btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr); | |
726 | ||
727 | btrfs_mark_buffer_dirty(l); | |
728 | ||
729 | out: | |
730 | btrfs_free_path(path); | |
731 | return ret; | |
732 | } | |
733 | ||
734 | static int update_qgroup_status_item(struct btrfs_trans_handle *trans, | |
735 | struct btrfs_fs_info *fs_info, | |
736 | struct btrfs_root *root) | |
737 | { | |
738 | struct btrfs_path *path; | |
739 | struct btrfs_key key; | |
740 | struct extent_buffer *l; | |
741 | struct btrfs_qgroup_status_item *ptr; | |
742 | int ret; | |
743 | int slot; | |
744 | ||
745 | key.objectid = 0; | |
746 | key.type = BTRFS_QGROUP_STATUS_KEY; | |
747 | key.offset = 0; | |
748 | ||
749 | path = btrfs_alloc_path(); | |
84cbe2f7 WS |
750 | if (!path) |
751 | return -ENOMEM; | |
752 | ||
bed92eae AJ |
753 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
754 | if (ret > 0) | |
755 | ret = -ENOENT; | |
756 | ||
757 | if (ret) | |
758 | goto out; | |
759 | ||
760 | l = path->nodes[0]; | |
761 | slot = path->slots[0]; | |
762 | ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item); | |
763 | btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags); | |
764 | btrfs_set_qgroup_status_generation(l, ptr, trans->transid); | |
2f232036 JS |
765 | btrfs_set_qgroup_status_rescan(l, ptr, |
766 | fs_info->qgroup_rescan_progress.objectid); | |
bed92eae AJ |
767 | |
768 | btrfs_mark_buffer_dirty(l); | |
769 | ||
770 | out: | |
771 | btrfs_free_path(path); | |
772 | return ret; | |
773 | } | |
774 | ||
775 | /* | |
776 | * called with qgroup_lock held | |
777 | */ | |
778 | static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans, | |
779 | struct btrfs_root *root) | |
780 | { | |
781 | struct btrfs_path *path; | |
782 | struct btrfs_key key; | |
06b3a860 | 783 | struct extent_buffer *leaf = NULL; |
bed92eae | 784 | int ret; |
06b3a860 | 785 | int nr = 0; |
bed92eae | 786 | |
bed92eae AJ |
787 | path = btrfs_alloc_path(); |
788 | if (!path) | |
789 | return -ENOMEM; | |
790 | ||
06b3a860 WS |
791 | path->leave_spinning = 1; |
792 | ||
793 | key.objectid = 0; | |
794 | key.offset = 0; | |
795 | key.type = 0; | |
bed92eae | 796 | |
06b3a860 | 797 | while (1) { |
bed92eae | 798 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
06b3a860 WS |
799 | if (ret < 0) |
800 | goto out; | |
801 | leaf = path->nodes[0]; | |
802 | nr = btrfs_header_nritems(leaf); | |
803 | if (!nr) | |
bed92eae | 804 | break; |
06b3a860 WS |
805 | /* |
806 | * delete the leaf one by one | |
807 | * since the whole tree is going | |
808 | * to be deleted. | |
809 | */ | |
810 | path->slots[0] = 0; | |
811 | ret = btrfs_del_items(trans, root, path, 0, nr); | |
bed92eae AJ |
812 | if (ret) |
813 | goto out; | |
06b3a860 | 814 | |
bed92eae AJ |
815 | btrfs_release_path(path); |
816 | } | |
817 | ret = 0; | |
818 | out: | |
819 | root->fs_info->pending_quota_state = 0; | |
820 | btrfs_free_path(path); | |
821 | return ret; | |
822 | } | |
823 | ||
824 | int btrfs_quota_enable(struct btrfs_trans_handle *trans, | |
825 | struct btrfs_fs_info *fs_info) | |
826 | { | |
827 | struct btrfs_root *quota_root; | |
7708f029 | 828 | struct btrfs_root *tree_root = fs_info->tree_root; |
bed92eae AJ |
829 | struct btrfs_path *path = NULL; |
830 | struct btrfs_qgroup_status_item *ptr; | |
831 | struct extent_buffer *leaf; | |
832 | struct btrfs_key key; | |
7708f029 WS |
833 | struct btrfs_key found_key; |
834 | struct btrfs_qgroup *qgroup = NULL; | |
bed92eae | 835 | int ret = 0; |
7708f029 | 836 | int slot; |
bed92eae | 837 | |
f2f6ed3d | 838 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
839 | if (fs_info->quota_root) { |
840 | fs_info->pending_quota_state = 1; | |
bed92eae AJ |
841 | goto out; |
842 | } | |
bed92eae | 843 | |
1e8f9158 WS |
844 | fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS); |
845 | if (!fs_info->qgroup_ulist) { | |
846 | ret = -ENOMEM; | |
847 | goto out; | |
848 | } | |
849 | ||
bed92eae AJ |
850 | /* |
851 | * initially create the quota tree | |
852 | */ | |
853 | quota_root = btrfs_create_tree(trans, fs_info, | |
854 | BTRFS_QUOTA_TREE_OBJECTID); | |
855 | if (IS_ERR(quota_root)) { | |
856 | ret = PTR_ERR(quota_root); | |
857 | goto out; | |
858 | } | |
859 | ||
860 | path = btrfs_alloc_path(); | |
5b7ff5b3 TI |
861 | if (!path) { |
862 | ret = -ENOMEM; | |
863 | goto out_free_root; | |
864 | } | |
bed92eae AJ |
865 | |
866 | key.objectid = 0; | |
867 | key.type = BTRFS_QGROUP_STATUS_KEY; | |
868 | key.offset = 0; | |
869 | ||
870 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, | |
871 | sizeof(*ptr)); | |
872 | if (ret) | |
5b7ff5b3 | 873 | goto out_free_path; |
bed92eae AJ |
874 | |
875 | leaf = path->nodes[0]; | |
876 | ptr = btrfs_item_ptr(leaf, path->slots[0], | |
877 | struct btrfs_qgroup_status_item); | |
878 | btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid); | |
879 | btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION); | |
880 | fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON | | |
881 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
882 | btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags); | |
2f232036 | 883 | btrfs_set_qgroup_status_rescan(leaf, ptr, 0); |
bed92eae AJ |
884 | |
885 | btrfs_mark_buffer_dirty(leaf); | |
886 | ||
7708f029 WS |
887 | key.objectid = 0; |
888 | key.type = BTRFS_ROOT_REF_KEY; | |
889 | key.offset = 0; | |
890 | ||
891 | btrfs_release_path(path); | |
892 | ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0); | |
893 | if (ret > 0) | |
894 | goto out_add_root; | |
895 | if (ret < 0) | |
896 | goto out_free_path; | |
897 | ||
898 | ||
899 | while (1) { | |
900 | slot = path->slots[0]; | |
901 | leaf = path->nodes[0]; | |
902 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
903 | ||
904 | if (found_key.type == BTRFS_ROOT_REF_KEY) { | |
905 | ret = add_qgroup_item(trans, quota_root, | |
906 | found_key.offset); | |
907 | if (ret) | |
908 | goto out_free_path; | |
909 | ||
7708f029 WS |
910 | qgroup = add_qgroup_rb(fs_info, found_key.offset); |
911 | if (IS_ERR(qgroup)) { | |
7708f029 WS |
912 | ret = PTR_ERR(qgroup); |
913 | goto out_free_path; | |
914 | } | |
7708f029 WS |
915 | } |
916 | ret = btrfs_next_item(tree_root, path); | |
917 | if (ret < 0) | |
918 | goto out_free_path; | |
919 | if (ret) | |
920 | break; | |
921 | } | |
922 | ||
923 | out_add_root: | |
924 | btrfs_release_path(path); | |
925 | ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID); | |
926 | if (ret) | |
927 | goto out_free_path; | |
928 | ||
7708f029 WS |
929 | qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID); |
930 | if (IS_ERR(qgroup)) { | |
7708f029 WS |
931 | ret = PTR_ERR(qgroup); |
932 | goto out_free_path; | |
933 | } | |
58400fce | 934 | spin_lock(&fs_info->qgroup_lock); |
bed92eae AJ |
935 | fs_info->quota_root = quota_root; |
936 | fs_info->pending_quota_state = 1; | |
937 | spin_unlock(&fs_info->qgroup_lock); | |
5b7ff5b3 | 938 | out_free_path: |
bed92eae | 939 | btrfs_free_path(path); |
5b7ff5b3 TI |
940 | out_free_root: |
941 | if (ret) { | |
942 | free_extent_buffer(quota_root->node); | |
943 | free_extent_buffer(quota_root->commit_root); | |
944 | kfree(quota_root); | |
945 | } | |
946 | out: | |
eb1716af | 947 | if (ret) { |
1e8f9158 | 948 | ulist_free(fs_info->qgroup_ulist); |
eb1716af JS |
949 | fs_info->qgroup_ulist = NULL; |
950 | } | |
f2f6ed3d | 951 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
952 | return ret; |
953 | } | |
954 | ||
955 | int btrfs_quota_disable(struct btrfs_trans_handle *trans, | |
956 | struct btrfs_fs_info *fs_info) | |
957 | { | |
958 | struct btrfs_root *tree_root = fs_info->tree_root; | |
959 | struct btrfs_root *quota_root; | |
960 | int ret = 0; | |
961 | ||
f2f6ed3d | 962 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
58400fce | 963 | if (!fs_info->quota_root) |
f2f6ed3d | 964 | goto out; |
58400fce | 965 | spin_lock(&fs_info->qgroup_lock); |
bed92eae AJ |
966 | fs_info->quota_enabled = 0; |
967 | fs_info->pending_quota_state = 0; | |
968 | quota_root = fs_info->quota_root; | |
969 | fs_info->quota_root = NULL; | |
bed92eae AJ |
970 | spin_unlock(&fs_info->qgroup_lock); |
971 | ||
e685da14 WS |
972 | btrfs_free_qgroup_config(fs_info); |
973 | ||
bed92eae AJ |
974 | ret = btrfs_clean_quota_tree(trans, quota_root); |
975 | if (ret) | |
976 | goto out; | |
977 | ||
978 | ret = btrfs_del_root(trans, tree_root, "a_root->root_key); | |
979 | if (ret) | |
980 | goto out; | |
981 | ||
982 | list_del("a_root->dirty_list); | |
983 | ||
984 | btrfs_tree_lock(quota_root->node); | |
01d58472 | 985 | clean_tree_block(trans, tree_root->fs_info, quota_root->node); |
bed92eae AJ |
986 | btrfs_tree_unlock(quota_root->node); |
987 | btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1); | |
988 | ||
989 | free_extent_buffer(quota_root->node); | |
990 | free_extent_buffer(quota_root->commit_root); | |
991 | kfree(quota_root); | |
992 | out: | |
f2f6ed3d | 993 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
994 | return ret; |
995 | } | |
996 | ||
2f232036 JS |
997 | static void qgroup_dirty(struct btrfs_fs_info *fs_info, |
998 | struct btrfs_qgroup *qgroup) | |
bed92eae | 999 | { |
2f232036 JS |
1000 | if (list_empty(&qgroup->dirty)) |
1001 | list_add(&qgroup->dirty, &fs_info->dirty_qgroups); | |
bed92eae AJ |
1002 | } |
1003 | ||
1004 | int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, | |
1005 | struct btrfs_fs_info *fs_info, u64 src, u64 dst) | |
1006 | { | |
1007 | struct btrfs_root *quota_root; | |
b7fef4f5 WS |
1008 | struct btrfs_qgroup *parent; |
1009 | struct btrfs_qgroup *member; | |
534e6623 | 1010 | struct btrfs_qgroup_list *list; |
bed92eae AJ |
1011 | int ret = 0; |
1012 | ||
f2f6ed3d | 1013 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1014 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1015 | if (!quota_root) { |
1016 | ret = -EINVAL; | |
1017 | goto out; | |
1018 | } | |
b7fef4f5 WS |
1019 | member = find_qgroup_rb(fs_info, src); |
1020 | parent = find_qgroup_rb(fs_info, dst); | |
1021 | if (!member || !parent) { | |
1022 | ret = -EINVAL; | |
1023 | goto out; | |
1024 | } | |
bed92eae | 1025 | |
534e6623 WS |
1026 | /* check if such qgroup relation exist firstly */ |
1027 | list_for_each_entry(list, &member->groups, next_group) { | |
1028 | if (list->group == parent) { | |
1029 | ret = -EEXIST; | |
1030 | goto out; | |
1031 | } | |
1032 | } | |
1033 | ||
bed92eae AJ |
1034 | ret = add_qgroup_relation_item(trans, quota_root, src, dst); |
1035 | if (ret) | |
f2f6ed3d | 1036 | goto out; |
bed92eae AJ |
1037 | |
1038 | ret = add_qgroup_relation_item(trans, quota_root, dst, src); | |
1039 | if (ret) { | |
1040 | del_qgroup_relation_item(trans, quota_root, src, dst); | |
f2f6ed3d | 1041 | goto out; |
bed92eae AJ |
1042 | } |
1043 | ||
1044 | spin_lock(&fs_info->qgroup_lock); | |
1045 | ret = add_relation_rb(quota_root->fs_info, src, dst); | |
1046 | spin_unlock(&fs_info->qgroup_lock); | |
f2f6ed3d WS |
1047 | out: |
1048 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1049 | return ret; |
1050 | } | |
1051 | ||
1052 | int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, | |
1053 | struct btrfs_fs_info *fs_info, u64 src, u64 dst) | |
1054 | { | |
1055 | struct btrfs_root *quota_root; | |
534e6623 WS |
1056 | struct btrfs_qgroup *parent; |
1057 | struct btrfs_qgroup *member; | |
1058 | struct btrfs_qgroup_list *list; | |
bed92eae AJ |
1059 | int ret = 0; |
1060 | int err; | |
1061 | ||
f2f6ed3d | 1062 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1063 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1064 | if (!quota_root) { |
1065 | ret = -EINVAL; | |
1066 | goto out; | |
1067 | } | |
bed92eae | 1068 | |
534e6623 WS |
1069 | member = find_qgroup_rb(fs_info, src); |
1070 | parent = find_qgroup_rb(fs_info, dst); | |
1071 | if (!member || !parent) { | |
1072 | ret = -EINVAL; | |
1073 | goto out; | |
1074 | } | |
1075 | ||
1076 | /* check if such qgroup relation exist firstly */ | |
1077 | list_for_each_entry(list, &member->groups, next_group) { | |
1078 | if (list->group == parent) | |
1079 | goto exist; | |
1080 | } | |
1081 | ret = -ENOENT; | |
1082 | goto out; | |
1083 | exist: | |
bed92eae AJ |
1084 | ret = del_qgroup_relation_item(trans, quota_root, src, dst); |
1085 | err = del_qgroup_relation_item(trans, quota_root, dst, src); | |
1086 | if (err && !ret) | |
1087 | ret = err; | |
1088 | ||
1089 | spin_lock(&fs_info->qgroup_lock); | |
1090 | del_relation_rb(fs_info, src, dst); | |
bed92eae | 1091 | spin_unlock(&fs_info->qgroup_lock); |
f2f6ed3d WS |
1092 | out: |
1093 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1094 | return ret; |
1095 | } | |
1096 | ||
1097 | int btrfs_create_qgroup(struct btrfs_trans_handle *trans, | |
4087cf24 | 1098 | struct btrfs_fs_info *fs_info, u64 qgroupid) |
bed92eae AJ |
1099 | { |
1100 | struct btrfs_root *quota_root; | |
1101 | struct btrfs_qgroup *qgroup; | |
1102 | int ret = 0; | |
1103 | ||
f2f6ed3d | 1104 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1105 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1106 | if (!quota_root) { |
1107 | ret = -EINVAL; | |
1108 | goto out; | |
1109 | } | |
534e6623 WS |
1110 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
1111 | if (qgroup) { | |
1112 | ret = -EEXIST; | |
1113 | goto out; | |
1114 | } | |
bed92eae AJ |
1115 | |
1116 | ret = add_qgroup_item(trans, quota_root, qgroupid); | |
534e6623 WS |
1117 | if (ret) |
1118 | goto out; | |
bed92eae AJ |
1119 | |
1120 | spin_lock(&fs_info->qgroup_lock); | |
1121 | qgroup = add_qgroup_rb(fs_info, qgroupid); | |
1122 | spin_unlock(&fs_info->qgroup_lock); | |
1123 | ||
1124 | if (IS_ERR(qgroup)) | |
1125 | ret = PTR_ERR(qgroup); | |
f2f6ed3d WS |
1126 | out: |
1127 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1128 | return ret; |
1129 | } | |
1130 | ||
1131 | int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, | |
1132 | struct btrfs_fs_info *fs_info, u64 qgroupid) | |
1133 | { | |
1134 | struct btrfs_root *quota_root; | |
2cf68703 | 1135 | struct btrfs_qgroup *qgroup; |
bed92eae AJ |
1136 | int ret = 0; |
1137 | ||
f2f6ed3d | 1138 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 1139 | quota_root = fs_info->quota_root; |
f2f6ed3d WS |
1140 | if (!quota_root) { |
1141 | ret = -EINVAL; | |
1142 | goto out; | |
1143 | } | |
bed92eae | 1144 | |
2cf68703 | 1145 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
534e6623 WS |
1146 | if (!qgroup) { |
1147 | ret = -ENOENT; | |
1148 | goto out; | |
1149 | } else { | |
1150 | /* check if there are no relations to this qgroup */ | |
1151 | if (!list_empty(&qgroup->groups) || | |
1152 | !list_empty(&qgroup->members)) { | |
f2f6ed3d WS |
1153 | ret = -EBUSY; |
1154 | goto out; | |
2cf68703 AJ |
1155 | } |
1156 | } | |
bed92eae AJ |
1157 | ret = del_qgroup_item(trans, quota_root, qgroupid); |
1158 | ||
1159 | spin_lock(&fs_info->qgroup_lock); | |
1160 | del_qgroup_rb(quota_root->fs_info, qgroupid); | |
bed92eae | 1161 | spin_unlock(&fs_info->qgroup_lock); |
f2f6ed3d WS |
1162 | out: |
1163 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1164 | return ret; |
1165 | } | |
1166 | ||
1167 | int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, | |
1168 | struct btrfs_fs_info *fs_info, u64 qgroupid, | |
1169 | struct btrfs_qgroup_limit *limit) | |
1170 | { | |
f2f6ed3d | 1171 | struct btrfs_root *quota_root; |
bed92eae AJ |
1172 | struct btrfs_qgroup *qgroup; |
1173 | int ret = 0; | |
1174 | ||
f2f6ed3d WS |
1175 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
1176 | quota_root = fs_info->quota_root; | |
1177 | if (!quota_root) { | |
1178 | ret = -EINVAL; | |
1179 | goto out; | |
1180 | } | |
bed92eae | 1181 | |
ddb47afa WS |
1182 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
1183 | if (!qgroup) { | |
1184 | ret = -ENOENT; | |
1185 | goto out; | |
1186 | } | |
bed92eae | 1187 | |
58400fce | 1188 | spin_lock(&fs_info->qgroup_lock); |
03477d94 DY |
1189 | if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) |
1190 | qgroup->max_rfer = limit->max_rfer; | |
1191 | if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) | |
1192 | qgroup->max_excl = limit->max_excl; | |
1193 | if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) | |
1194 | qgroup->rsv_rfer = limit->rsv_rfer; | |
1195 | if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) | |
1196 | qgroup->rsv_excl = limit->rsv_excl; | |
1197 | qgroup->lim_flags |= limit->flags; | |
1198 | ||
bed92eae | 1199 | spin_unlock(&fs_info->qgroup_lock); |
1510e71c DY |
1200 | |
1201 | ret = update_qgroup_limit_item(trans, quota_root, qgroup); | |
1202 | if (ret) { | |
1203 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
1204 | btrfs_info(fs_info, "unable to update quota limit for %llu", | |
1205 | qgroupid); | |
1206 | } | |
1207 | ||
f2f6ed3d WS |
1208 | out: |
1209 | mutex_unlock(&fs_info->qgroup_ioctl_lock); | |
bed92eae AJ |
1210 | return ret; |
1211 | } | |
1152651a MF |
1212 | |
1213 | static int comp_oper_exist(struct btrfs_qgroup_operation *oper1, | |
1214 | struct btrfs_qgroup_operation *oper2) | |
1215 | { | |
1216 | /* | |
1217 | * Ignore seq and type here, we're looking for any operation | |
1218 | * at all related to this extent on that root. | |
1219 | */ | |
1220 | if (oper1->bytenr < oper2->bytenr) | |
1221 | return -1; | |
1222 | if (oper1->bytenr > oper2->bytenr) | |
1223 | return 1; | |
1224 | if (oper1->ref_root < oper2->ref_root) | |
1225 | return -1; | |
1226 | if (oper1->ref_root > oper2->ref_root) | |
1227 | return 1; | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | static int qgroup_oper_exists(struct btrfs_fs_info *fs_info, | |
1232 | struct btrfs_qgroup_operation *oper) | |
1233 | { | |
1234 | struct rb_node *n; | |
1235 | struct btrfs_qgroup_operation *cur; | |
1236 | int cmp; | |
1237 | ||
1238 | spin_lock(&fs_info->qgroup_op_lock); | |
1239 | n = fs_info->qgroup_op_tree.rb_node; | |
1240 | while (n) { | |
1241 | cur = rb_entry(n, struct btrfs_qgroup_operation, n); | |
1242 | cmp = comp_oper_exist(cur, oper); | |
1243 | if (cmp < 0) { | |
1244 | n = n->rb_right; | |
1245 | } else if (cmp) { | |
1246 | n = n->rb_left; | |
1247 | } else { | |
1248 | spin_unlock(&fs_info->qgroup_op_lock); | |
1249 | return -EEXIST; | |
1250 | } | |
1251 | } | |
1252 | spin_unlock(&fs_info->qgroup_op_lock); | |
1253 | return 0; | |
1254 | } | |
1255 | ||
fcebe456 JB |
1256 | static int comp_oper(struct btrfs_qgroup_operation *oper1, |
1257 | struct btrfs_qgroup_operation *oper2) | |
1258 | { | |
1259 | if (oper1->bytenr < oper2->bytenr) | |
1260 | return -1; | |
1261 | if (oper1->bytenr > oper2->bytenr) | |
1262 | return 1; | |
fcebe456 JB |
1263 | if (oper1->ref_root < oper2->ref_root) |
1264 | return -1; | |
1265 | if (oper1->ref_root > oper2->ref_root) | |
1266 | return 1; | |
bf691960 FM |
1267 | if (oper1->seq < oper2->seq) |
1268 | return -1; | |
1269 | if (oper1->seq > oper2->seq) | |
1270 | return 1; | |
fcebe456 JB |
1271 | if (oper1->type < oper2->type) |
1272 | return -1; | |
1273 | if (oper1->type > oper2->type) | |
1274 | return 1; | |
1275 | return 0; | |
1276 | } | |
1277 | ||
1278 | static int insert_qgroup_oper(struct btrfs_fs_info *fs_info, | |
1279 | struct btrfs_qgroup_operation *oper) | |
1280 | { | |
1281 | struct rb_node **p; | |
1282 | struct rb_node *parent = NULL; | |
1283 | struct btrfs_qgroup_operation *cur; | |
1284 | int cmp; | |
1285 | ||
1286 | spin_lock(&fs_info->qgroup_op_lock); | |
1287 | p = &fs_info->qgroup_op_tree.rb_node; | |
1288 | while (*p) { | |
1289 | parent = *p; | |
1290 | cur = rb_entry(parent, struct btrfs_qgroup_operation, n); | |
1291 | cmp = comp_oper(cur, oper); | |
1292 | if (cmp < 0) { | |
1293 | p = &(*p)->rb_right; | |
1294 | } else if (cmp) { | |
1295 | p = &(*p)->rb_left; | |
1296 | } else { | |
1297 | spin_unlock(&fs_info->qgroup_op_lock); | |
1298 | return -EEXIST; | |
1299 | } | |
1300 | } | |
1301 | rb_link_node(&oper->n, parent, p); | |
1302 | rb_insert_color(&oper->n, &fs_info->qgroup_op_tree); | |
1303 | spin_unlock(&fs_info->qgroup_op_lock); | |
1304 | return 0; | |
1305 | } | |
bed92eae | 1306 | |
bed92eae | 1307 | /* |
fcebe456 JB |
1308 | * Record a quota operation for processing later on. |
1309 | * @trans: the transaction we are adding the delayed op to. | |
1310 | * @fs_info: the fs_info for this fs. | |
1311 | * @ref_root: the root of the reference we are acting on, | |
1312 | * @bytenr: the bytenr we are acting on. | |
1313 | * @num_bytes: the number of bytes in the reference. | |
1314 | * @type: the type of operation this is. | |
1315 | * @mod_seq: do we need to get a sequence number for looking up roots. | |
1316 | * | |
1317 | * We just add it to our trans qgroup_ref_list and carry on and process these | |
1318 | * operations in order at some later point. If the reference root isn't a fs | |
1319 | * root then we don't bother with doing anything. | |
1320 | * | |
1321 | * MUST BE HOLDING THE REF LOCK. | |
bed92eae AJ |
1322 | */ |
1323 | int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans, | |
fcebe456 JB |
1324 | struct btrfs_fs_info *fs_info, u64 ref_root, |
1325 | u64 bytenr, u64 num_bytes, | |
1326 | enum btrfs_qgroup_operation_type type, int mod_seq) | |
bed92eae | 1327 | { |
fcebe456 JB |
1328 | struct btrfs_qgroup_operation *oper; |
1329 | int ret; | |
bed92eae | 1330 | |
fcebe456 JB |
1331 | if (!is_fstree(ref_root) || !fs_info->quota_enabled) |
1332 | return 0; | |
1333 | ||
1334 | oper = kmalloc(sizeof(*oper), GFP_NOFS); | |
1335 | if (!oper) | |
bed92eae AJ |
1336 | return -ENOMEM; |
1337 | ||
fcebe456 JB |
1338 | oper->ref_root = ref_root; |
1339 | oper->bytenr = bytenr; | |
1340 | oper->num_bytes = num_bytes; | |
1341 | oper->type = type; | |
1342 | oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq); | |
1343 | INIT_LIST_HEAD(&oper->elem.list); | |
1344 | oper->elem.seq = 0; | |
1152651a | 1345 | |
d3982100 MF |
1346 | trace_btrfs_qgroup_record_ref(oper); |
1347 | ||
1152651a MF |
1348 | if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) { |
1349 | /* | |
1350 | * If any operation for this bytenr/ref_root combo | |
1351 | * exists, then we know it's not exclusively owned and | |
1352 | * shouldn't be queued up. | |
1353 | * | |
1354 | * This also catches the case where we have a cloned | |
1355 | * extent that gets queued up multiple times during | |
1356 | * drop snapshot. | |
1357 | */ | |
1358 | if (qgroup_oper_exists(fs_info, oper)) { | |
1359 | kfree(oper); | |
1360 | return 0; | |
1361 | } | |
1362 | } | |
1363 | ||
fcebe456 JB |
1364 | ret = insert_qgroup_oper(fs_info, oper); |
1365 | if (ret) { | |
1366 | /* Shouldn't happen so have an assert for developers */ | |
1367 | ASSERT(0); | |
1368 | kfree(oper); | |
1369 | return ret; | |
1370 | } | |
1371 | list_add_tail(&oper->list, &trans->qgroup_ref_list); | |
1372 | ||
1373 | if (mod_seq) | |
1374 | btrfs_get_tree_mod_seq(fs_info, &oper->elem); | |
bed92eae AJ |
1375 | |
1376 | return 0; | |
1377 | } | |
1378 | ||
fcebe456 JB |
1379 | /* |
1380 | * The easy accounting, if we are adding/removing the only ref for an extent | |
1381 | * then this qgroup and all of the parent qgroups get their refrence and | |
1382 | * exclusive counts adjusted. | |
1383 | */ | |
1384 | static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info, | |
1385 | struct btrfs_qgroup_operation *oper) | |
1386 | { | |
1387 | struct btrfs_qgroup *qgroup; | |
1388 | struct ulist *tmp; | |
1389 | struct btrfs_qgroup_list *glist; | |
1390 | struct ulist_node *unode; | |
1391 | struct ulist_iterator uiter; | |
1392 | int sign = 0; | |
1393 | int ret = 0; | |
1394 | ||
1395 | tmp = ulist_alloc(GFP_NOFS); | |
1396 | if (!tmp) | |
1397 | return -ENOMEM; | |
1398 | ||
1399 | spin_lock(&fs_info->qgroup_lock); | |
1400 | if (!fs_info->quota_root) | |
1401 | goto out; | |
1402 | qgroup = find_qgroup_rb(fs_info, oper->ref_root); | |
1403 | if (!qgroup) | |
1404 | goto out; | |
1405 | switch (oper->type) { | |
1406 | case BTRFS_QGROUP_OPER_ADD_EXCL: | |
1407 | sign = 1; | |
1408 | break; | |
1409 | case BTRFS_QGROUP_OPER_SUB_EXCL: | |
1410 | sign = -1; | |
1411 | break; | |
1412 | default: | |
1413 | ASSERT(0); | |
1414 | } | |
1415 | qgroup->rfer += sign * oper->num_bytes; | |
1416 | qgroup->rfer_cmpr += sign * oper->num_bytes; | |
1417 | ||
1418 | WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes); | |
1419 | qgroup->excl += sign * oper->num_bytes; | |
1420 | qgroup->excl_cmpr += sign * oper->num_bytes; | |
31193213 DY |
1421 | if (sign > 0) |
1422 | qgroup->reserved -= oper->num_bytes; | |
fcebe456 JB |
1423 | |
1424 | qgroup_dirty(fs_info, qgroup); | |
1425 | ||
1426 | /* Get all of the parent groups that contain this qgroup */ | |
1427 | list_for_each_entry(glist, &qgroup->groups, next_group) { | |
1428 | ret = ulist_add(tmp, glist->group->qgroupid, | |
1429 | ptr_to_u64(glist->group), GFP_ATOMIC); | |
1430 | if (ret < 0) | |
1431 | goto out; | |
1432 | } | |
1433 | ||
1434 | /* Iterate all of the parents and adjust their reference counts */ | |
1435 | ULIST_ITER_INIT(&uiter); | |
1436 | while ((unode = ulist_next(tmp, &uiter))) { | |
1437 | qgroup = u64_to_ptr(unode->aux); | |
1438 | qgroup->rfer += sign * oper->num_bytes; | |
1439 | qgroup->rfer_cmpr += sign * oper->num_bytes; | |
0ee13fe2 | 1440 | WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes); |
fcebe456 | 1441 | qgroup->excl += sign * oper->num_bytes; |
31193213 DY |
1442 | if (sign > 0) |
1443 | qgroup->reserved -= oper->num_bytes; | |
fcebe456 JB |
1444 | qgroup->excl_cmpr += sign * oper->num_bytes; |
1445 | qgroup_dirty(fs_info, qgroup); | |
1446 | ||
1447 | /* Add any parents of the parents */ | |
1448 | list_for_each_entry(glist, &qgroup->groups, next_group) { | |
1449 | ret = ulist_add(tmp, glist->group->qgroupid, | |
1450 | ptr_to_u64(glist->group), GFP_ATOMIC); | |
1451 | if (ret < 0) | |
1452 | goto out; | |
1453 | } | |
1454 | } | |
1455 | ret = 0; | |
1456 | out: | |
1457 | spin_unlock(&fs_info->qgroup_lock); | |
1458 | ulist_free(tmp); | |
1459 | return ret; | |
1460 | } | |
1461 | ||
1462 | /* | |
1463 | * Walk all of the roots that pointed to our bytenr and adjust their refcnts as | |
1464 | * properly. | |
1465 | */ | |
1466 | static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info, | |
1467 | u64 root_to_skip, struct ulist *tmp, | |
1468 | struct ulist *roots, struct ulist *qgroups, | |
1469 | u64 seq, int *old_roots, int rescan) | |
46b665ce JS |
1470 | { |
1471 | struct ulist_node *unode; | |
1472 | struct ulist_iterator uiter; | |
1473 | struct ulist_node *tmp_unode; | |
1474 | struct ulist_iterator tmp_uiter; | |
1475 | struct btrfs_qgroup *qg; | |
1476 | int ret; | |
1477 | ||
1478 | ULIST_ITER_INIT(&uiter); | |
1479 | while ((unode = ulist_next(roots, &uiter))) { | |
fcebe456 JB |
1480 | /* We don't count our current root here */ |
1481 | if (unode->val == root_to_skip) | |
1482 | continue; | |
46b665ce JS |
1483 | qg = find_qgroup_rb(fs_info, unode->val); |
1484 | if (!qg) | |
1485 | continue; | |
fcebe456 JB |
1486 | /* |
1487 | * We could have a pending removal of this same ref so we may | |
1488 | * not have actually found our ref root when doing | |
1489 | * btrfs_find_all_roots, so we need to keep track of how many | |
1490 | * old roots we find in case we removed ours and added a | |
1491 | * different one at the same time. I don't think this could | |
1492 | * happen in practice but that sort of thinking leads to pain | |
1493 | * and suffering and to the dark side. | |
1494 | */ | |
1495 | (*old_roots)++; | |
46b665ce JS |
1496 | |
1497 | ulist_reinit(tmp); | |
fcebe456 JB |
1498 | ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg), |
1499 | GFP_ATOMIC); | |
1500 | if (ret < 0) | |
1501 | return ret; | |
1502 | ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC); | |
46b665ce JS |
1503 | if (ret < 0) |
1504 | return ret; | |
1505 | ULIST_ITER_INIT(&tmp_uiter); | |
1506 | while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) { | |
1507 | struct btrfs_qgroup_list *glist; | |
1508 | ||
fcebe456 JB |
1509 | qg = u64_to_ptr(tmp_unode->aux); |
1510 | /* | |
1511 | * We use this sequence number to keep from having to | |
1512 | * run the whole list and 0 out the refcnt every time. | |
1513 | * We basically use sequnce as the known 0 count and | |
1514 | * then add 1 everytime we see a qgroup. This is how we | |
1515 | * get how many of the roots actually point up to the | |
1516 | * upper level qgroups in order to determine exclusive | |
1517 | * counts. | |
1518 | * | |
1519 | * For rescan we want to set old_refcnt to seq so our | |
1520 | * exclusive calculations end up correct. | |
1521 | */ | |
1522 | if (rescan) | |
1523 | qg->old_refcnt = seq; | |
1524 | else if (qg->old_refcnt < seq) | |
1525 | qg->old_refcnt = seq + 1; | |
46b665ce | 1526 | else |
fcebe456 | 1527 | qg->old_refcnt++; |
46b665ce | 1528 | |
fcebe456 JB |
1529 | if (qg->new_refcnt < seq) |
1530 | qg->new_refcnt = seq + 1; | |
1531 | else | |
1532 | qg->new_refcnt++; | |
46b665ce | 1533 | list_for_each_entry(glist, &qg->groups, next_group) { |
fcebe456 JB |
1534 | ret = ulist_add(qgroups, glist->group->qgroupid, |
1535 | ptr_to_u64(glist->group), | |
1536 | GFP_ATOMIC); | |
1537 | if (ret < 0) | |
1538 | return ret; | |
46b665ce | 1539 | ret = ulist_add(tmp, glist->group->qgroupid, |
fcebe456 | 1540 | ptr_to_u64(glist->group), |
46b665ce JS |
1541 | GFP_ATOMIC); |
1542 | if (ret < 0) | |
1543 | return ret; | |
1544 | } | |
1545 | } | |
1546 | } | |
fcebe456 JB |
1547 | return 0; |
1548 | } | |
46b665ce | 1549 | |
fcebe456 JB |
1550 | /* |
1551 | * We need to walk forward in our operation tree and account for any roots that | |
1552 | * were deleted after we made this operation. | |
1553 | */ | |
1554 | static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info, | |
1555 | struct btrfs_qgroup_operation *oper, | |
1556 | struct ulist *tmp, | |
1557 | struct ulist *qgroups, u64 seq, | |
1558 | int *old_roots) | |
1559 | { | |
1560 | struct ulist_node *unode; | |
1561 | struct ulist_iterator uiter; | |
1562 | struct btrfs_qgroup *qg; | |
1563 | struct btrfs_qgroup_operation *tmp_oper; | |
1564 | struct rb_node *n; | |
1565 | int ret; | |
1566 | ||
1567 | ulist_reinit(tmp); | |
1568 | ||
1569 | /* | |
1570 | * We only walk forward in the tree since we're only interested in | |
1571 | * removals that happened _after_ our operation. | |
1572 | */ | |
1573 | spin_lock(&fs_info->qgroup_op_lock); | |
1574 | n = rb_next(&oper->n); | |
1575 | spin_unlock(&fs_info->qgroup_op_lock); | |
1576 | if (!n) | |
1577 | return 0; | |
1578 | tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n); | |
1579 | while (tmp_oper->bytenr == oper->bytenr) { | |
1580 | /* | |
1581 | * If it's not a removal we don't care, additions work out | |
1582 | * properly with our refcnt tracking. | |
1583 | */ | |
1584 | if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED && | |
1585 | tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL) | |
1586 | goto next; | |
1587 | qg = find_qgroup_rb(fs_info, tmp_oper->ref_root); | |
1588 | if (!qg) | |
1589 | goto next; | |
1590 | ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg), | |
1591 | GFP_ATOMIC); | |
1592 | if (ret) { | |
1593 | if (ret < 0) | |
1594 | return ret; | |
1595 | /* | |
1596 | * We only want to increase old_roots if this qgroup is | |
1597 | * not already in the list of qgroups. If it is already | |
1598 | * there then that means it must have been re-added or | |
1599 | * the delete will be discarded because we had an | |
1600 | * existing ref that we haven't looked up yet. In this | |
1601 | * case we don't want to increase old_roots. So if ret | |
1602 | * == 1 then we know that this is the first time we've | |
1603 | * seen this qgroup and we can bump the old_roots. | |
1604 | */ | |
1605 | (*old_roots)++; | |
1606 | ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), | |
1607 | GFP_ATOMIC); | |
1608 | if (ret < 0) | |
1609 | return ret; | |
1610 | } | |
1611 | next: | |
1612 | spin_lock(&fs_info->qgroup_op_lock); | |
1613 | n = rb_next(&tmp_oper->n); | |
1614 | spin_unlock(&fs_info->qgroup_op_lock); | |
1615 | if (!n) | |
1616 | break; | |
1617 | tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n); | |
1618 | } | |
1619 | ||
1620 | /* Ok now process the qgroups we found */ | |
1621 | ULIST_ITER_INIT(&uiter); | |
1622 | while ((unode = ulist_next(tmp, &uiter))) { | |
1623 | struct btrfs_qgroup_list *glist; | |
1624 | ||
1625 | qg = u64_to_ptr(unode->aux); | |
1626 | if (qg->old_refcnt < seq) | |
1627 | qg->old_refcnt = seq + 1; | |
1628 | else | |
1629 | qg->old_refcnt++; | |
1630 | if (qg->new_refcnt < seq) | |
1631 | qg->new_refcnt = seq + 1; | |
1632 | else | |
1633 | qg->new_refcnt++; | |
1634 | list_for_each_entry(glist, &qg->groups, next_group) { | |
1635 | ret = ulist_add(qgroups, glist->group->qgroupid, | |
1636 | ptr_to_u64(glist->group), GFP_ATOMIC); | |
1637 | if (ret < 0) | |
1638 | return ret; | |
1639 | ret = ulist_add(tmp, glist->group->qgroupid, | |
1640 | ptr_to_u64(glist->group), GFP_ATOMIC); | |
1641 | if (ret < 0) | |
1642 | return ret; | |
1643 | } | |
1644 | } | |
46b665ce JS |
1645 | return 0; |
1646 | } | |
1647 | ||
fcebe456 JB |
1648 | /* Add refcnt for the newly added reference. */ |
1649 | static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info, | |
1650 | struct btrfs_qgroup_operation *oper, | |
1651 | struct btrfs_qgroup *qgroup, | |
1652 | struct ulist *tmp, struct ulist *qgroups, | |
1653 | u64 seq) | |
46b665ce JS |
1654 | { |
1655 | struct ulist_node *unode; | |
1656 | struct ulist_iterator uiter; | |
1657 | struct btrfs_qgroup *qg; | |
46b665ce JS |
1658 | int ret; |
1659 | ||
1660 | ulist_reinit(tmp); | |
fcebe456 JB |
1661 | ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup), |
1662 | GFP_ATOMIC); | |
1663 | if (ret < 0) | |
1664 | return ret; | |
1665 | ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup), | |
1666 | GFP_ATOMIC); | |
46b665ce JS |
1667 | if (ret < 0) |
1668 | return ret; | |
46b665ce JS |
1669 | ULIST_ITER_INIT(&uiter); |
1670 | while ((unode = ulist_next(tmp, &uiter))) { | |
fcebe456 | 1671 | struct btrfs_qgroup_list *glist; |
46b665ce | 1672 | |
fcebe456 JB |
1673 | qg = u64_to_ptr(unode->aux); |
1674 | if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) { | |
1675 | if (qg->new_refcnt < seq) | |
1676 | qg->new_refcnt = seq + 1; | |
1677 | else | |
1678 | qg->new_refcnt++; | |
1679 | } else { | |
1680 | if (qg->old_refcnt < seq) | |
1681 | qg->old_refcnt = seq + 1; | |
1682 | else | |
1683 | qg->old_refcnt++; | |
1684 | } | |
46b665ce JS |
1685 | list_for_each_entry(glist, &qg->groups, next_group) { |
1686 | ret = ulist_add(tmp, glist->group->qgroupid, | |
fcebe456 JB |
1687 | ptr_to_u64(glist->group), GFP_ATOMIC); |
1688 | if (ret < 0) | |
1689 | return ret; | |
1690 | ret = ulist_add(qgroups, glist->group->qgroupid, | |
1691 | ptr_to_u64(glist->group), GFP_ATOMIC); | |
46b665ce JS |
1692 | if (ret < 0) |
1693 | return ret; | |
1694 | } | |
1695 | } | |
46b665ce JS |
1696 | return 0; |
1697 | } | |
1698 | ||
fcebe456 JB |
1699 | /* |
1700 | * This adjusts the counters for all referenced qgroups if need be. | |
1701 | */ | |
1702 | static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info, | |
1703 | u64 root_to_skip, u64 num_bytes, | |
1704 | struct ulist *qgroups, u64 seq, | |
1705 | int old_roots, int new_roots, int rescan) | |
46b665ce JS |
1706 | { |
1707 | struct ulist_node *unode; | |
1708 | struct ulist_iterator uiter; | |
1709 | struct btrfs_qgroup *qg; | |
fcebe456 | 1710 | u64 cur_new_count, cur_old_count; |
46b665ce JS |
1711 | |
1712 | ULIST_ITER_INIT(&uiter); | |
fcebe456 JB |
1713 | while ((unode = ulist_next(qgroups, &uiter))) { |
1714 | bool dirty = false; | |
46b665ce | 1715 | |
fcebe456 JB |
1716 | qg = u64_to_ptr(unode->aux); |
1717 | /* | |
1718 | * Wasn't referenced before but is now, add to the reference | |
1719 | * counters. | |
1720 | */ | |
1721 | if (qg->old_refcnt <= seq && qg->new_refcnt > seq) { | |
1722 | qg->rfer += num_bytes; | |
1723 | qg->rfer_cmpr += num_bytes; | |
1724 | dirty = true; | |
1725 | } | |
46b665ce | 1726 | |
fcebe456 JB |
1727 | /* |
1728 | * Was referenced before but isn't now, subtract from the | |
1729 | * reference counters. | |
1730 | */ | |
1731 | if (qg->old_refcnt > seq && qg->new_refcnt <= seq) { | |
1732 | qg->rfer -= num_bytes; | |
1733 | qg->rfer_cmpr -= num_bytes; | |
1734 | dirty = true; | |
1735 | } | |
46b665ce | 1736 | |
fcebe456 JB |
1737 | if (qg->old_refcnt < seq) |
1738 | cur_old_count = 0; | |
1739 | else | |
1740 | cur_old_count = qg->old_refcnt - seq; | |
1741 | if (qg->new_refcnt < seq) | |
1742 | cur_new_count = 0; | |
1743 | else | |
1744 | cur_new_count = qg->new_refcnt - seq; | |
46b665ce | 1745 | |
fcebe456 JB |
1746 | /* |
1747 | * If our refcount was the same as the roots previously but our | |
1748 | * new count isn't the same as the number of roots now then we | |
1749 | * went from having a exclusive reference on this range to not. | |
1750 | */ | |
1751 | if (old_roots && cur_old_count == old_roots && | |
1752 | (cur_new_count != new_roots || new_roots == 0)) { | |
1753 | WARN_ON(cur_new_count != new_roots && new_roots == 0); | |
1754 | qg->excl -= num_bytes; | |
1755 | qg->excl_cmpr -= num_bytes; | |
1756 | dirty = true; | |
1757 | } | |
46b665ce | 1758 | |
fcebe456 JB |
1759 | /* |
1760 | * If we didn't reference all the roots before but now we do we | |
1761 | * have an exclusive reference to this range. | |
1762 | */ | |
1763 | if ((!old_roots || (old_roots && cur_old_count != old_roots)) | |
1764 | && cur_new_count == new_roots) { | |
1765 | qg->excl += num_bytes; | |
1766 | qg->excl_cmpr += num_bytes; | |
1767 | dirty = true; | |
46b665ce | 1768 | } |
46b665ce | 1769 | |
fcebe456 JB |
1770 | if (dirty) |
1771 | qgroup_dirty(fs_info, qg); | |
1772 | } | |
46b665ce JS |
1773 | return 0; |
1774 | } | |
1775 | ||
bed92eae | 1776 | /* |
fcebe456 JB |
1777 | * If we removed a data extent and there were other references for that bytenr |
1778 | * then we need to lookup all referenced roots to make sure we still don't | |
1779 | * reference this bytenr. If we do then we can just discard this operation. | |
bed92eae | 1780 | */ |
fcebe456 JB |
1781 | static int check_existing_refs(struct btrfs_trans_handle *trans, |
1782 | struct btrfs_fs_info *fs_info, | |
1783 | struct btrfs_qgroup_operation *oper) | |
bed92eae | 1784 | { |
bed92eae | 1785 | struct ulist *roots = NULL; |
fcebe456 JB |
1786 | struct ulist_node *unode; |
1787 | struct ulist_iterator uiter; | |
bed92eae | 1788 | int ret = 0; |
bed92eae | 1789 | |
fcebe456 JB |
1790 | ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, |
1791 | oper->elem.seq, &roots); | |
1792 | if (ret < 0) | |
1793 | return ret; | |
1794 | ret = 0; | |
bed92eae | 1795 | |
fcebe456 JB |
1796 | ULIST_ITER_INIT(&uiter); |
1797 | while ((unode = ulist_next(roots, &uiter))) { | |
1798 | if (unode->val == oper->ref_root) { | |
1799 | ret = 1; | |
1800 | break; | |
1801 | } | |
bed92eae | 1802 | } |
fcebe456 JB |
1803 | ulist_free(roots); |
1804 | btrfs_put_tree_mod_seq(fs_info, &oper->elem); | |
bed92eae | 1805 | |
fcebe456 JB |
1806 | return ret; |
1807 | } | |
bed92eae | 1808 | |
fcebe456 JB |
1809 | /* |
1810 | * If we share a reference across multiple roots then we may need to adjust | |
1811 | * various qgroups referenced and exclusive counters. The basic premise is this | |
1812 | * | |
1813 | * 1) We have seq to represent a 0 count. Instead of looping through all of the | |
1814 | * qgroups and resetting their refcount to 0 we just constantly bump this | |
1815 | * sequence number to act as the base reference count. This means that if | |
1816 | * anybody is equal to or below this sequence they were never referenced. We | |
1817 | * jack this sequence up by the number of roots we found each time in order to | |
1818 | * make sure we don't have any overlap. | |
1819 | * | |
1820 | * 2) We first search all the roots that reference the area _except_ the root | |
1821 | * we're acting on currently. This makes up the old_refcnt of all the qgroups | |
1822 | * before. | |
1823 | * | |
1824 | * 3) We walk all of the qgroups referenced by the root we are currently acting | |
1825 | * on, and will either adjust old_refcnt in the case of a removal or the | |
1826 | * new_refcnt in the case of an addition. | |
1827 | * | |
1828 | * 4) Finally we walk all the qgroups that are referenced by this range | |
1829 | * including the root we are acting on currently. We will adjust the counters | |
1830 | * based on the number of roots we had and will have after this operation. | |
1831 | * | |
1832 | * Take this example as an illustration | |
1833 | * | |
1834 | * [qgroup 1/0] | |
1835 | * / | \ | |
1836 | * [qg 0/0] [qg 0/1] [qg 0/2] | |
1837 | * \ | / | |
1838 | * [ extent ] | |
1839 | * | |
1840 | * Say we are adding a reference that is covered by qg 0/0. The first step | |
1841 | * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with | |
1842 | * old_roots being 2. Because it is adding new_roots will be 1. We then go | |
1843 | * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's | |
1844 | * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we | |
1845 | * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a | |
1846 | * reference and thus must add the size to the referenced bytes. Everything | |
1847 | * else is the same so nothing else changes. | |
1848 | */ | |
1849 | static int qgroup_shared_accounting(struct btrfs_trans_handle *trans, | |
1850 | struct btrfs_fs_info *fs_info, | |
1851 | struct btrfs_qgroup_operation *oper) | |
1852 | { | |
1853 | struct ulist *roots = NULL; | |
1854 | struct ulist *qgroups, *tmp; | |
1855 | struct btrfs_qgroup *qgroup; | |
3284da7b | 1856 | struct seq_list elem = SEQ_LIST_INIT(elem); |
fcebe456 JB |
1857 | u64 seq; |
1858 | int old_roots = 0; | |
1859 | int new_roots = 0; | |
1860 | int ret = 0; | |
bed92eae | 1861 | |
fcebe456 JB |
1862 | if (oper->elem.seq) { |
1863 | ret = check_existing_refs(trans, fs_info, oper); | |
1864 | if (ret < 0) | |
1865 | return ret; | |
1866 | if (ret) | |
2f232036 | 1867 | return 0; |
2f232036 | 1868 | } |
2f232036 | 1869 | |
fcebe456 JB |
1870 | qgroups = ulist_alloc(GFP_NOFS); |
1871 | if (!qgroups) | |
1872 | return -ENOMEM; | |
2f232036 | 1873 | |
fcebe456 | 1874 | tmp = ulist_alloc(GFP_NOFS); |
d7372780 ES |
1875 | if (!tmp) { |
1876 | ulist_free(qgroups); | |
fcebe456 | 1877 | return -ENOMEM; |
d7372780 | 1878 | } |
bed92eae | 1879 | |
fcebe456 JB |
1880 | btrfs_get_tree_mod_seq(fs_info, &elem); |
1881 | ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq, | |
1882 | &roots); | |
1883 | btrfs_put_tree_mod_seq(fs_info, &elem); | |
1884 | if (ret < 0) { | |
1885 | ulist_free(qgroups); | |
1886 | ulist_free(tmp); | |
1887 | return ret; | |
1888 | } | |
1889 | spin_lock(&fs_info->qgroup_lock); | |
1890 | qgroup = find_qgroup_rb(fs_info, oper->ref_root); | |
bed92eae | 1891 | if (!qgroup) |
fcebe456 JB |
1892 | goto out; |
1893 | seq = fs_info->qgroup_seq; | |
bed92eae AJ |
1894 | |
1895 | /* | |
fcebe456 JB |
1896 | * So roots is the list of all the roots currently pointing at the |
1897 | * bytenr, including the ref we are adding if we are adding, or not if | |
1898 | * we are removing a ref. So we pass in the ref_root to skip that root | |
1899 | * in our calculations. We set old_refnct and new_refcnt cause who the | |
1900 | * hell knows what everything looked like before, and it doesn't matter | |
1901 | * except... | |
bed92eae | 1902 | */ |
fcebe456 JB |
1903 | ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups, |
1904 | seq, &old_roots, 0); | |
1905 | if (ret < 0) | |
1906 | goto out; | |
bed92eae | 1907 | |
fcebe456 JB |
1908 | /* |
1909 | * Now adjust the refcounts of the qgroups that care about this | |
1910 | * reference, either the old_count in the case of removal or new_count | |
1911 | * in the case of an addition. | |
1912 | */ | |
1913 | ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups, | |
1914 | seq); | |
1915 | if (ret < 0) | |
1916 | goto out; | |
bed92eae AJ |
1917 | |
1918 | /* | |
fcebe456 JB |
1919 | * ...in the case of removals. If we had a removal before we got around |
1920 | * to processing this operation then we need to find that guy and count | |
1921 | * his references as if they really existed so we don't end up screwing | |
1922 | * up the exclusive counts. Then whenever we go to process the delete | |
1923 | * everything will be grand and we can account for whatever exclusive | |
1924 | * changes need to be made there. We also have to pass in old_roots so | |
1925 | * we have an accurate count of the roots as it pertains to this | |
1926 | * operations view of the world. | |
bed92eae | 1927 | */ |
fcebe456 JB |
1928 | ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq, |
1929 | &old_roots); | |
1930 | if (ret < 0) | |
1931 | goto out; | |
bed92eae AJ |
1932 | |
1933 | /* | |
fcebe456 JB |
1934 | * We are adding our root, need to adjust up the number of roots, |
1935 | * otherwise old_roots is the number of roots we want. | |
bed92eae | 1936 | */ |
fcebe456 JB |
1937 | if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) { |
1938 | new_roots = old_roots + 1; | |
1939 | } else { | |
1940 | new_roots = old_roots; | |
1941 | old_roots++; | |
1942 | } | |
1943 | fs_info->qgroup_seq += old_roots + 1; | |
bed92eae | 1944 | |
fcebe456 JB |
1945 | |
1946 | /* | |
1947 | * And now the magic happens, bless Arne for having a pretty elegant | |
1948 | * solution for this. | |
1949 | */ | |
1950 | qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes, | |
1951 | qgroups, seq, old_roots, new_roots, 0); | |
1952 | out: | |
bed92eae | 1953 | spin_unlock(&fs_info->qgroup_lock); |
fcebe456 | 1954 | ulist_free(qgroups); |
bed92eae | 1955 | ulist_free(roots); |
fcebe456 JB |
1956 | ulist_free(tmp); |
1957 | return ret; | |
1958 | } | |
1959 | ||
1152651a MF |
1960 | /* |
1961 | * Process a reference to a shared subtree. This type of operation is | |
1962 | * queued during snapshot removal when we encounter extents which are | |
1963 | * shared between more than one root. | |
1964 | */ | |
1965 | static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans, | |
1966 | struct btrfs_fs_info *fs_info, | |
1967 | struct btrfs_qgroup_operation *oper) | |
1968 | { | |
1969 | struct ulist *roots = NULL; | |
1970 | struct ulist_node *unode; | |
1971 | struct ulist_iterator uiter; | |
1972 | struct btrfs_qgroup_list *glist; | |
1973 | struct ulist *parents; | |
1974 | int ret = 0; | |
f90e579c | 1975 | int err; |
1152651a MF |
1976 | struct btrfs_qgroup *qg; |
1977 | u64 root_obj = 0; | |
3284da7b | 1978 | struct seq_list elem = SEQ_LIST_INIT(elem); |
1152651a MF |
1979 | |
1980 | parents = ulist_alloc(GFP_NOFS); | |
1981 | if (!parents) | |
1982 | return -ENOMEM; | |
1983 | ||
1984 | btrfs_get_tree_mod_seq(fs_info, &elem); | |
1985 | ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, | |
1986 | elem.seq, &roots); | |
1987 | btrfs_put_tree_mod_seq(fs_info, &elem); | |
1988 | if (ret < 0) | |
a3c10895 | 1989 | goto out; |
1152651a MF |
1990 | |
1991 | if (roots->nnodes != 1) | |
1992 | goto out; | |
1993 | ||
1994 | ULIST_ITER_INIT(&uiter); | |
1995 | unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */ | |
1996 | /* | |
1997 | * If we find our ref root then that means all refs | |
1998 | * this extent has to the root have not yet been | |
1999 | * deleted. In that case, we do nothing and let the | |
2000 | * last ref for this bytenr drive our update. | |
2001 | * | |
2002 | * This can happen for example if an extent is | |
2003 | * referenced multiple times in a snapshot (clone, | |
2004 | * etc). If we are in the middle of snapshot removal, | |
2005 | * queued updates for such an extent will find the | |
2006 | * root if we have not yet finished removing the | |
2007 | * snapshot. | |
2008 | */ | |
2009 | if (unode->val == oper->ref_root) | |
2010 | goto out; | |
2011 | ||
2012 | root_obj = unode->val; | |
2013 | BUG_ON(!root_obj); | |
2014 | ||
2015 | spin_lock(&fs_info->qgroup_lock); | |
2016 | qg = find_qgroup_rb(fs_info, root_obj); | |
2017 | if (!qg) | |
2018 | goto out_unlock; | |
2019 | ||
2020 | qg->excl += oper->num_bytes; | |
2021 | qg->excl_cmpr += oper->num_bytes; | |
2022 | qgroup_dirty(fs_info, qg); | |
2023 | ||
2024 | /* | |
2025 | * Adjust counts for parent groups. First we find all | |
2026 | * parents, then in the 2nd loop we do the adjustment | |
2027 | * while adding parents of the parents to our ulist. | |
2028 | */ | |
2029 | list_for_each_entry(glist, &qg->groups, next_group) { | |
f90e579c | 2030 | err = ulist_add(parents, glist->group->qgroupid, |
1152651a | 2031 | ptr_to_u64(glist->group), GFP_ATOMIC); |
f90e579c MF |
2032 | if (err < 0) { |
2033 | ret = err; | |
1152651a | 2034 | goto out_unlock; |
f90e579c | 2035 | } |
1152651a MF |
2036 | } |
2037 | ||
2038 | ULIST_ITER_INIT(&uiter); | |
2039 | while ((unode = ulist_next(parents, &uiter))) { | |
2040 | qg = u64_to_ptr(unode->aux); | |
2041 | qg->excl += oper->num_bytes; | |
2042 | qg->excl_cmpr += oper->num_bytes; | |
2043 | qgroup_dirty(fs_info, qg); | |
2044 | ||
2045 | /* Add any parents of the parents */ | |
2046 | list_for_each_entry(glist, &qg->groups, next_group) { | |
f90e579c | 2047 | err = ulist_add(parents, glist->group->qgroupid, |
1152651a | 2048 | ptr_to_u64(glist->group), GFP_ATOMIC); |
f90e579c MF |
2049 | if (err < 0) { |
2050 | ret = err; | |
1152651a | 2051 | goto out_unlock; |
f90e579c | 2052 | } |
1152651a MF |
2053 | } |
2054 | } | |
2055 | ||
2056 | out_unlock: | |
2057 | spin_unlock(&fs_info->qgroup_lock); | |
2058 | ||
2059 | out: | |
2060 | ulist_free(roots); | |
2061 | ulist_free(parents); | |
2062 | return ret; | |
2063 | } | |
2064 | ||
fcebe456 JB |
2065 | /* |
2066 | * btrfs_qgroup_account_ref is called for every ref that is added to or deleted | |
2067 | * from the fs. First, all roots referencing the extent are searched, and | |
2068 | * then the space is accounted accordingly to the different roots. The | |
2069 | * accounting algorithm works in 3 steps documented inline. | |
2070 | */ | |
2071 | static int btrfs_qgroup_account(struct btrfs_trans_handle *trans, | |
2072 | struct btrfs_fs_info *fs_info, | |
2073 | struct btrfs_qgroup_operation *oper) | |
2074 | { | |
2075 | int ret = 0; | |
2076 | ||
2077 | if (!fs_info->quota_enabled) | |
2078 | return 0; | |
2079 | ||
2080 | BUG_ON(!fs_info->quota_root); | |
2081 | ||
2082 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2083 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { | |
2084 | if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) { | |
2085 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2086 | return 0; | |
2087 | } | |
2088 | } | |
2089 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2090 | ||
2091 | ASSERT(is_fstree(oper->ref_root)); | |
2092 | ||
d3982100 MF |
2093 | trace_btrfs_qgroup_account(oper); |
2094 | ||
fcebe456 JB |
2095 | switch (oper->type) { |
2096 | case BTRFS_QGROUP_OPER_ADD_EXCL: | |
2097 | case BTRFS_QGROUP_OPER_SUB_EXCL: | |
2098 | ret = qgroup_excl_accounting(fs_info, oper); | |
2099 | break; | |
2100 | case BTRFS_QGROUP_OPER_ADD_SHARED: | |
2101 | case BTRFS_QGROUP_OPER_SUB_SHARED: | |
2102 | ret = qgroup_shared_accounting(trans, fs_info, oper); | |
2103 | break; | |
1152651a MF |
2104 | case BTRFS_QGROUP_OPER_SUB_SUBTREE: |
2105 | ret = qgroup_subtree_accounting(trans, fs_info, oper); | |
2106 | break; | |
fcebe456 JB |
2107 | default: |
2108 | ASSERT(0); | |
2109 | } | |
2110 | return ret; | |
2111 | } | |
bed92eae | 2112 | |
fcebe456 JB |
2113 | /* |
2114 | * Needs to be called everytime we run delayed refs, even if there is an error | |
2115 | * in order to cleanup outstanding operations. | |
2116 | */ | |
2117 | int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans, | |
2118 | struct btrfs_fs_info *fs_info) | |
2119 | { | |
2120 | struct btrfs_qgroup_operation *oper; | |
2121 | int ret = 0; | |
2122 | ||
2123 | while (!list_empty(&trans->qgroup_ref_list)) { | |
2124 | oper = list_first_entry(&trans->qgroup_ref_list, | |
2125 | struct btrfs_qgroup_operation, list); | |
2126 | list_del_init(&oper->list); | |
2127 | if (!ret || !trans->aborted) | |
2128 | ret = btrfs_qgroup_account(trans, fs_info, oper); | |
2129 | spin_lock(&fs_info->qgroup_op_lock); | |
2130 | rb_erase(&oper->n, &fs_info->qgroup_op_tree); | |
2131 | spin_unlock(&fs_info->qgroup_op_lock); | |
2132 | btrfs_put_tree_mod_seq(fs_info, &oper->elem); | |
2133 | kfree(oper); | |
2134 | } | |
bed92eae AJ |
2135 | return ret; |
2136 | } | |
2137 | ||
2138 | /* | |
2139 | * called from commit_transaction. Writes all changed qgroups to disk. | |
2140 | */ | |
2141 | int btrfs_run_qgroups(struct btrfs_trans_handle *trans, | |
2142 | struct btrfs_fs_info *fs_info) | |
2143 | { | |
2144 | struct btrfs_root *quota_root = fs_info->quota_root; | |
2145 | int ret = 0; | |
3d7b5a28 | 2146 | int start_rescan_worker = 0; |
bed92eae AJ |
2147 | |
2148 | if (!quota_root) | |
2149 | goto out; | |
2150 | ||
3d7b5a28 JS |
2151 | if (!fs_info->quota_enabled && fs_info->pending_quota_state) |
2152 | start_rescan_worker = 1; | |
2153 | ||
bed92eae AJ |
2154 | fs_info->quota_enabled = fs_info->pending_quota_state; |
2155 | ||
2156 | spin_lock(&fs_info->qgroup_lock); | |
2157 | while (!list_empty(&fs_info->dirty_qgroups)) { | |
2158 | struct btrfs_qgroup *qgroup; | |
2159 | qgroup = list_first_entry(&fs_info->dirty_qgroups, | |
2160 | struct btrfs_qgroup, dirty); | |
2161 | list_del_init(&qgroup->dirty); | |
2162 | spin_unlock(&fs_info->qgroup_lock); | |
2163 | ret = update_qgroup_info_item(trans, quota_root, qgroup); | |
d3001ed3 DY |
2164 | if (ret) |
2165 | fs_info->qgroup_flags |= | |
2166 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2167 | ret = update_qgroup_limit_item(trans, quota_root, qgroup); | |
bed92eae AJ |
2168 | if (ret) |
2169 | fs_info->qgroup_flags |= | |
2170 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2171 | spin_lock(&fs_info->qgroup_lock); | |
2172 | } | |
2173 | if (fs_info->quota_enabled) | |
2174 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON; | |
2175 | else | |
2176 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; | |
2177 | spin_unlock(&fs_info->qgroup_lock); | |
2178 | ||
2179 | ret = update_qgroup_status_item(trans, fs_info, quota_root); | |
2180 | if (ret) | |
2181 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2182 | ||
3d7b5a28 | 2183 | if (!ret && start_rescan_worker) { |
b382a324 JS |
2184 | ret = qgroup_rescan_init(fs_info, 0, 1); |
2185 | if (!ret) { | |
2186 | qgroup_rescan_zero_tracking(fs_info); | |
fc97fab0 QW |
2187 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
2188 | &fs_info->qgroup_rescan_work); | |
b382a324 | 2189 | } |
3d7b5a28 JS |
2190 | ret = 0; |
2191 | } | |
2192 | ||
bed92eae AJ |
2193 | out: |
2194 | ||
2195 | return ret; | |
2196 | } | |
2197 | ||
2198 | /* | |
2199 | * copy the acounting information between qgroups. This is necessary when a | |
2200 | * snapshot or a subvolume is created | |
2201 | */ | |
2202 | int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, | |
2203 | struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid, | |
2204 | struct btrfs_qgroup_inherit *inherit) | |
2205 | { | |
2206 | int ret = 0; | |
2207 | int i; | |
2208 | u64 *i_qgroups; | |
2209 | struct btrfs_root *quota_root = fs_info->quota_root; | |
2210 | struct btrfs_qgroup *srcgroup; | |
2211 | struct btrfs_qgroup *dstgroup; | |
2212 | u32 level_size = 0; | |
3f5e2d3b | 2213 | u64 nums; |
bed92eae | 2214 | |
f2f6ed3d | 2215 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
bed92eae | 2216 | if (!fs_info->quota_enabled) |
f2f6ed3d | 2217 | goto out; |
bed92eae | 2218 | |
f2f6ed3d WS |
2219 | if (!quota_root) { |
2220 | ret = -EINVAL; | |
2221 | goto out; | |
2222 | } | |
bed92eae | 2223 | |
3f5e2d3b WS |
2224 | if (inherit) { |
2225 | i_qgroups = (u64 *)(inherit + 1); | |
2226 | nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + | |
2227 | 2 * inherit->num_excl_copies; | |
2228 | for (i = 0; i < nums; ++i) { | |
2229 | srcgroup = find_qgroup_rb(fs_info, *i_qgroups); | |
2230 | if (!srcgroup) { | |
2231 | ret = -EINVAL; | |
2232 | goto out; | |
2233 | } | |
2234 | ++i_qgroups; | |
2235 | } | |
2236 | } | |
2237 | ||
bed92eae AJ |
2238 | /* |
2239 | * create a tracking group for the subvol itself | |
2240 | */ | |
2241 | ret = add_qgroup_item(trans, quota_root, objectid); | |
2242 | if (ret) | |
2243 | goto out; | |
2244 | ||
bed92eae AJ |
2245 | if (srcid) { |
2246 | struct btrfs_root *srcroot; | |
2247 | struct btrfs_key srckey; | |
bed92eae AJ |
2248 | |
2249 | srckey.objectid = srcid; | |
2250 | srckey.type = BTRFS_ROOT_ITEM_KEY; | |
2251 | srckey.offset = (u64)-1; | |
2252 | srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey); | |
2253 | if (IS_ERR(srcroot)) { | |
2254 | ret = PTR_ERR(srcroot); | |
2255 | goto out; | |
2256 | } | |
2257 | ||
2258 | rcu_read_lock(); | |
707e8a07 | 2259 | level_size = srcroot->nodesize; |
bed92eae AJ |
2260 | rcu_read_unlock(); |
2261 | } | |
2262 | ||
2263 | /* | |
2264 | * add qgroup to all inherited groups | |
2265 | */ | |
2266 | if (inherit) { | |
2267 | i_qgroups = (u64 *)(inherit + 1); | |
2268 | for (i = 0; i < inherit->num_qgroups; ++i) { | |
2269 | ret = add_qgroup_relation_item(trans, quota_root, | |
2270 | objectid, *i_qgroups); | |
2271 | if (ret) | |
2272 | goto out; | |
2273 | ret = add_qgroup_relation_item(trans, quota_root, | |
2274 | *i_qgroups, objectid); | |
2275 | if (ret) | |
2276 | goto out; | |
2277 | ++i_qgroups; | |
2278 | } | |
2279 | } | |
2280 | ||
2281 | ||
2282 | spin_lock(&fs_info->qgroup_lock); | |
2283 | ||
2284 | dstgroup = add_qgroup_rb(fs_info, objectid); | |
57a5a882 DC |
2285 | if (IS_ERR(dstgroup)) { |
2286 | ret = PTR_ERR(dstgroup); | |
bed92eae | 2287 | goto unlock; |
57a5a882 | 2288 | } |
bed92eae | 2289 | |
e8c8541a | 2290 | if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) { |
e8c8541a DY |
2291 | dstgroup->lim_flags = inherit->lim.flags; |
2292 | dstgroup->max_rfer = inherit->lim.max_rfer; | |
2293 | dstgroup->max_excl = inherit->lim.max_excl; | |
2294 | dstgroup->rsv_rfer = inherit->lim.rsv_rfer; | |
2295 | dstgroup->rsv_excl = inherit->lim.rsv_excl; | |
1510e71c DY |
2296 | |
2297 | ret = update_qgroup_limit_item(trans, quota_root, dstgroup); | |
2298 | if (ret) { | |
2299 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2300 | btrfs_info(fs_info, "unable to update quota limit for %llu", | |
2301 | dstgroup->qgroupid); | |
2302 | goto unlock; | |
2303 | } | |
e8c8541a DY |
2304 | } |
2305 | ||
bed92eae AJ |
2306 | if (srcid) { |
2307 | srcgroup = find_qgroup_rb(fs_info, srcid); | |
f3a87f1b | 2308 | if (!srcgroup) |
bed92eae | 2309 | goto unlock; |
fcebe456 JB |
2310 | |
2311 | /* | |
2312 | * We call inherit after we clone the root in order to make sure | |
2313 | * our counts don't go crazy, so at this point the only | |
2314 | * difference between the two roots should be the root node. | |
2315 | */ | |
2316 | dstgroup->rfer = srcgroup->rfer; | |
2317 | dstgroup->rfer_cmpr = srcgroup->rfer_cmpr; | |
2318 | dstgroup->excl = level_size; | |
2319 | dstgroup->excl_cmpr = level_size; | |
bed92eae AJ |
2320 | srcgroup->excl = level_size; |
2321 | srcgroup->excl_cmpr = level_size; | |
3eeb4d59 DY |
2322 | |
2323 | /* inherit the limit info */ | |
2324 | dstgroup->lim_flags = srcgroup->lim_flags; | |
2325 | dstgroup->max_rfer = srcgroup->max_rfer; | |
2326 | dstgroup->max_excl = srcgroup->max_excl; | |
2327 | dstgroup->rsv_rfer = srcgroup->rsv_rfer; | |
2328 | dstgroup->rsv_excl = srcgroup->rsv_excl; | |
2329 | ||
bed92eae AJ |
2330 | qgroup_dirty(fs_info, dstgroup); |
2331 | qgroup_dirty(fs_info, srcgroup); | |
2332 | } | |
2333 | ||
f3a87f1b | 2334 | if (!inherit) |
bed92eae AJ |
2335 | goto unlock; |
2336 | ||
2337 | i_qgroups = (u64 *)(inherit + 1); | |
2338 | for (i = 0; i < inherit->num_qgroups; ++i) { | |
2339 | ret = add_relation_rb(quota_root->fs_info, objectid, | |
2340 | *i_qgroups); | |
2341 | if (ret) | |
2342 | goto unlock; | |
2343 | ++i_qgroups; | |
2344 | } | |
2345 | ||
2346 | for (i = 0; i < inherit->num_ref_copies; ++i) { | |
2347 | struct btrfs_qgroup *src; | |
2348 | struct btrfs_qgroup *dst; | |
2349 | ||
2350 | src = find_qgroup_rb(fs_info, i_qgroups[0]); | |
2351 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); | |
2352 | ||
2353 | if (!src || !dst) { | |
2354 | ret = -EINVAL; | |
2355 | goto unlock; | |
2356 | } | |
2357 | ||
2358 | dst->rfer = src->rfer - level_size; | |
2359 | dst->rfer_cmpr = src->rfer_cmpr - level_size; | |
2360 | i_qgroups += 2; | |
2361 | } | |
2362 | for (i = 0; i < inherit->num_excl_copies; ++i) { | |
2363 | struct btrfs_qgroup *src; | |
2364 | struct btrfs_qgroup *dst; | |
2365 | ||
2366 | src = find_qgroup_rb(fs_info, i_qgroups[0]); | |
2367 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); | |
2368 | ||
2369 | if (!src || !dst) { | |
2370 | ret = -EINVAL; | |
2371 | goto unlock; | |
2372 | } | |
2373 | ||
2374 | dst->excl = src->excl + level_size; | |
2375 | dst->excl_cmpr = src->excl_cmpr + level_size; | |
2376 | i_qgroups += 2; | |
2377 | } | |
2378 | ||
2379 | unlock: | |
2380 | spin_unlock(&fs_info->qgroup_lock); | |
2381 | out: | |
f2f6ed3d | 2382 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
bed92eae AJ |
2383 | return ret; |
2384 | } | |
2385 | ||
31193213 DY |
2386 | int btrfs_qgroup_update_reserved_bytes(struct btrfs_fs_info *fs_info, |
2387 | u64 ref_root, | |
2388 | u64 num_bytes, | |
2389 | int sign) | |
2390 | { | |
2391 | struct btrfs_root *quota_root; | |
2392 | struct btrfs_qgroup *qgroup; | |
2393 | int ret = 0; | |
2394 | struct ulist_node *unode; | |
2395 | struct ulist_iterator uiter; | |
2396 | ||
2397 | if (!is_fstree(ref_root) || !fs_info->quota_enabled) | |
2398 | return 0; | |
2399 | ||
2400 | if (num_bytes == 0) | |
2401 | return 0; | |
2402 | ||
2403 | spin_lock(&fs_info->qgroup_lock); | |
2404 | quota_root = fs_info->quota_root; | |
2405 | if (!quota_root) | |
2406 | goto out; | |
2407 | ||
2408 | qgroup = find_qgroup_rb(fs_info, ref_root); | |
2409 | if (!qgroup) | |
2410 | goto out; | |
2411 | ||
2412 | ulist_reinit(fs_info->qgroup_ulist); | |
2413 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, | |
2414 | (uintptr_t)qgroup, GFP_ATOMIC); | |
2415 | if (ret < 0) | |
2416 | goto out; | |
2417 | ||
2418 | ULIST_ITER_INIT(&uiter); | |
2419 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { | |
2420 | struct btrfs_qgroup *qg; | |
2421 | struct btrfs_qgroup_list *glist; | |
2422 | ||
2423 | qg = u64_to_ptr(unode->aux); | |
2424 | ||
2425 | qg->reserved += sign * num_bytes; | |
2426 | ||
2427 | list_for_each_entry(glist, &qg->groups, next_group) { | |
2428 | ret = ulist_add(fs_info->qgroup_ulist, | |
2429 | glist->group->qgroupid, | |
2430 | (uintptr_t)glist->group, GFP_ATOMIC); | |
2431 | if (ret < 0) | |
2432 | goto out; | |
2433 | } | |
2434 | } | |
2435 | ||
2436 | out: | |
2437 | spin_unlock(&fs_info->qgroup_lock); | |
2438 | return ret; | |
2439 | } | |
2440 | ||
bed92eae AJ |
2441 | /* |
2442 | * reserve some space for a qgroup and all its parents. The reservation takes | |
2443 | * place with start_transaction or dealloc_reserve, similar to ENOSPC | |
2444 | * accounting. If not enough space is available, EDQUOT is returned. | |
2445 | * We assume that the requested space is new for all qgroups. | |
2446 | */ | |
2447 | int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes) | |
2448 | { | |
2449 | struct btrfs_root *quota_root; | |
2450 | struct btrfs_qgroup *qgroup; | |
2451 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2452 | u64 ref_root = root->root_key.objectid; | |
2453 | int ret = 0; | |
bed92eae AJ |
2454 | struct ulist_node *unode; |
2455 | struct ulist_iterator uiter; | |
2456 | ||
2457 | if (!is_fstree(ref_root)) | |
2458 | return 0; | |
2459 | ||
2460 | if (num_bytes == 0) | |
2461 | return 0; | |
2462 | ||
2463 | spin_lock(&fs_info->qgroup_lock); | |
2464 | quota_root = fs_info->quota_root; | |
2465 | if (!quota_root) | |
2466 | goto out; | |
2467 | ||
2468 | qgroup = find_qgroup_rb(fs_info, ref_root); | |
2469 | if (!qgroup) | |
2470 | goto out; | |
2471 | ||
2472 | /* | |
2473 | * in a first step, we check all affected qgroups if any limits would | |
2474 | * be exceeded | |
2475 | */ | |
1e8f9158 WS |
2476 | ulist_reinit(fs_info->qgroup_ulist); |
2477 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, | |
3c97185c WS |
2478 | (uintptr_t)qgroup, GFP_ATOMIC); |
2479 | if (ret < 0) | |
2480 | goto out; | |
bed92eae | 2481 | ULIST_ITER_INIT(&uiter); |
1e8f9158 | 2482 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
bed92eae AJ |
2483 | struct btrfs_qgroup *qg; |
2484 | struct btrfs_qgroup_list *glist; | |
2485 | ||
fcebe456 | 2486 | qg = u64_to_ptr(unode->aux); |
bed92eae AJ |
2487 | |
2488 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) && | |
31193213 | 2489 | qg->reserved + qg->may_use + (s64)qg->rfer + num_bytes > |
720f1e20 | 2490 | qg->max_rfer) { |
bed92eae | 2491 | ret = -EDQUOT; |
720f1e20 WS |
2492 | goto out; |
2493 | } | |
bed92eae AJ |
2494 | |
2495 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) && | |
31193213 | 2496 | qg->reserved + qg->may_use + (s64)qg->excl + num_bytes > |
720f1e20 | 2497 | qg->max_excl) { |
bed92eae | 2498 | ret = -EDQUOT; |
720f1e20 WS |
2499 | goto out; |
2500 | } | |
bed92eae AJ |
2501 | |
2502 | list_for_each_entry(glist, &qg->groups, next_group) { | |
1e8f9158 WS |
2503 | ret = ulist_add(fs_info->qgroup_ulist, |
2504 | glist->group->qgroupid, | |
3c97185c WS |
2505 | (uintptr_t)glist->group, GFP_ATOMIC); |
2506 | if (ret < 0) | |
2507 | goto out; | |
bed92eae AJ |
2508 | } |
2509 | } | |
3c97185c | 2510 | ret = 0; |
bed92eae AJ |
2511 | /* |
2512 | * no limits exceeded, now record the reservation into all qgroups | |
2513 | */ | |
2514 | ULIST_ITER_INIT(&uiter); | |
1e8f9158 | 2515 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
bed92eae AJ |
2516 | struct btrfs_qgroup *qg; |
2517 | ||
fcebe456 | 2518 | qg = u64_to_ptr(unode->aux); |
bed92eae | 2519 | |
31193213 | 2520 | qg->may_use += num_bytes; |
bed92eae AJ |
2521 | } |
2522 | ||
2523 | out: | |
2524 | spin_unlock(&fs_info->qgroup_lock); | |
bed92eae AJ |
2525 | return ret; |
2526 | } | |
2527 | ||
2528 | void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes) | |
2529 | { | |
2530 | struct btrfs_root *quota_root; | |
2531 | struct btrfs_qgroup *qgroup; | |
2532 | struct btrfs_fs_info *fs_info = root->fs_info; | |
bed92eae AJ |
2533 | struct ulist_node *unode; |
2534 | struct ulist_iterator uiter; | |
2535 | u64 ref_root = root->root_key.objectid; | |
3c97185c | 2536 | int ret = 0; |
bed92eae AJ |
2537 | |
2538 | if (!is_fstree(ref_root)) | |
2539 | return; | |
2540 | ||
2541 | if (num_bytes == 0) | |
2542 | return; | |
2543 | ||
2544 | spin_lock(&fs_info->qgroup_lock); | |
2545 | ||
2546 | quota_root = fs_info->quota_root; | |
2547 | if (!quota_root) | |
2548 | goto out; | |
2549 | ||
2550 | qgroup = find_qgroup_rb(fs_info, ref_root); | |
2551 | if (!qgroup) | |
2552 | goto out; | |
2553 | ||
1e8f9158 WS |
2554 | ulist_reinit(fs_info->qgroup_ulist); |
2555 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, | |
3c97185c WS |
2556 | (uintptr_t)qgroup, GFP_ATOMIC); |
2557 | if (ret < 0) | |
2558 | goto out; | |
bed92eae | 2559 | ULIST_ITER_INIT(&uiter); |
1e8f9158 | 2560 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
bed92eae AJ |
2561 | struct btrfs_qgroup *qg; |
2562 | struct btrfs_qgroup_list *glist; | |
2563 | ||
fcebe456 | 2564 | qg = u64_to_ptr(unode->aux); |
bed92eae | 2565 | |
31193213 | 2566 | qg->may_use -= num_bytes; |
bed92eae AJ |
2567 | |
2568 | list_for_each_entry(glist, &qg->groups, next_group) { | |
1e8f9158 WS |
2569 | ret = ulist_add(fs_info->qgroup_ulist, |
2570 | glist->group->qgroupid, | |
3c97185c WS |
2571 | (uintptr_t)glist->group, GFP_ATOMIC); |
2572 | if (ret < 0) | |
2573 | goto out; | |
bed92eae AJ |
2574 | } |
2575 | } | |
2576 | ||
2577 | out: | |
2578 | spin_unlock(&fs_info->qgroup_lock); | |
bed92eae AJ |
2579 | } |
2580 | ||
2581 | void assert_qgroups_uptodate(struct btrfs_trans_handle *trans) | |
2582 | { | |
2583 | if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq) | |
2584 | return; | |
efe120a0 FH |
2585 | btrfs_err(trans->root->fs_info, |
2586 | "qgroups not uptodate in trans handle %p: list is%s empty, " | |
2587 | "seq is %#x.%x", | |
bed92eae | 2588 | trans, list_empty(&trans->qgroup_ref_list) ? "" : " not", |
fc36ed7e JS |
2589 | (u32)(trans->delayed_ref_elem.seq >> 32), |
2590 | (u32)trans->delayed_ref_elem.seq); | |
bed92eae AJ |
2591 | BUG(); |
2592 | } | |
2f232036 JS |
2593 | |
2594 | /* | |
2595 | * returns < 0 on error, 0 when more leafs are to be scanned. | |
2596 | * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared. | |
2597 | */ | |
2598 | static int | |
b382a324 | 2599 | qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
fcebe456 JB |
2600 | struct btrfs_trans_handle *trans, struct ulist *qgroups, |
2601 | struct ulist *tmp, struct extent_buffer *scratch_leaf) | |
2f232036 JS |
2602 | { |
2603 | struct btrfs_key found; | |
2f232036 | 2604 | struct ulist *roots = NULL; |
3284da7b | 2605 | struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem); |
fcebe456 | 2606 | u64 num_bytes; |
2f232036 | 2607 | u64 seq; |
fcebe456 | 2608 | int new_roots; |
2f232036 JS |
2609 | int slot; |
2610 | int ret; | |
2611 | ||
2612 | path->leave_spinning = 1; | |
2613 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2614 | ret = btrfs_search_slot_for_read(fs_info->extent_root, | |
2615 | &fs_info->qgroup_rescan_progress, | |
2616 | path, 1, 0); | |
2617 | ||
2618 | pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n", | |
c1c9ff7c | 2619 | fs_info->qgroup_rescan_progress.objectid, |
2f232036 | 2620 | fs_info->qgroup_rescan_progress.type, |
c1c9ff7c | 2621 | fs_info->qgroup_rescan_progress.offset, ret); |
2f232036 JS |
2622 | |
2623 | if (ret) { | |
2624 | /* | |
2625 | * The rescan is about to end, we will not be scanning any | |
2626 | * further blocks. We cannot unset the RESCAN flag here, because | |
2627 | * we want to commit the transaction if everything went well. | |
2628 | * To make the live accounting work in this phase, we set our | |
2629 | * scan progress pointer such that every real extent objectid | |
2630 | * will be smaller. | |
2631 | */ | |
2632 | fs_info->qgroup_rescan_progress.objectid = (u64)-1; | |
2633 | btrfs_release_path(path); | |
2634 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2635 | return ret; | |
2636 | } | |
2637 | ||
2638 | btrfs_item_key_to_cpu(path->nodes[0], &found, | |
2639 | btrfs_header_nritems(path->nodes[0]) - 1); | |
2640 | fs_info->qgroup_rescan_progress.objectid = found.objectid + 1; | |
2641 | ||
2642 | btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem); | |
2643 | memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf)); | |
2644 | slot = path->slots[0]; | |
2645 | btrfs_release_path(path); | |
2646 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2647 | ||
2648 | for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) { | |
2649 | btrfs_item_key_to_cpu(scratch_leaf, &found, slot); | |
3a6d75e8 JB |
2650 | if (found.type != BTRFS_EXTENT_ITEM_KEY && |
2651 | found.type != BTRFS_METADATA_ITEM_KEY) | |
2f232036 | 2652 | continue; |
3a6d75e8 | 2653 | if (found.type == BTRFS_METADATA_ITEM_KEY) |
707e8a07 | 2654 | num_bytes = fs_info->extent_root->nodesize; |
3a6d75e8 JB |
2655 | else |
2656 | num_bytes = found.offset; | |
2657 | ||
fcebe456 JB |
2658 | ulist_reinit(qgroups); |
2659 | ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0, | |
2660 | &roots); | |
2f232036 JS |
2661 | if (ret < 0) |
2662 | goto out; | |
2663 | spin_lock(&fs_info->qgroup_lock); | |
2664 | seq = fs_info->qgroup_seq; | |
2665 | fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */ | |
2666 | ||
fcebe456 JB |
2667 | new_roots = 0; |
2668 | ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups, | |
2669 | seq, &new_roots, 1); | |
2670 | if (ret < 0) { | |
2f232036 JS |
2671 | spin_unlock(&fs_info->qgroup_lock); |
2672 | ulist_free(roots); | |
2673 | goto out; | |
2674 | } | |
2675 | ||
fcebe456 JB |
2676 | ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups, |
2677 | seq, 0, new_roots, 1); | |
2678 | if (ret < 0) { | |
2679 | spin_unlock(&fs_info->qgroup_lock); | |
2680 | ulist_free(roots); | |
2681 | goto out; | |
2f232036 | 2682 | } |
2f232036 JS |
2683 | spin_unlock(&fs_info->qgroup_lock); |
2684 | ulist_free(roots); | |
2f232036 | 2685 | } |
2f232036 JS |
2686 | out: |
2687 | btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem); | |
2688 | ||
2689 | return ret; | |
2690 | } | |
2691 | ||
d458b054 | 2692 | static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) |
2f232036 | 2693 | { |
b382a324 JS |
2694 | struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info, |
2695 | qgroup_rescan_work); | |
2f232036 JS |
2696 | struct btrfs_path *path; |
2697 | struct btrfs_trans_handle *trans = NULL; | |
fcebe456 | 2698 | struct ulist *tmp = NULL, *qgroups = NULL; |
2f232036 JS |
2699 | struct extent_buffer *scratch_leaf = NULL; |
2700 | int err = -ENOMEM; | |
2701 | ||
2702 | path = btrfs_alloc_path(); | |
2703 | if (!path) | |
2704 | goto out; | |
fcebe456 JB |
2705 | qgroups = ulist_alloc(GFP_NOFS); |
2706 | if (!qgroups) | |
2707 | goto out; | |
2f232036 JS |
2708 | tmp = ulist_alloc(GFP_NOFS); |
2709 | if (!tmp) | |
2710 | goto out; | |
2711 | scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS); | |
2712 | if (!scratch_leaf) | |
2713 | goto out; | |
2714 | ||
2715 | err = 0; | |
2716 | while (!err) { | |
2717 | trans = btrfs_start_transaction(fs_info->fs_root, 0); | |
2718 | if (IS_ERR(trans)) { | |
2719 | err = PTR_ERR(trans); | |
2720 | break; | |
2721 | } | |
2722 | if (!fs_info->quota_enabled) { | |
2723 | err = -EINTR; | |
2724 | } else { | |
b382a324 | 2725 | err = qgroup_rescan_leaf(fs_info, path, trans, |
fcebe456 | 2726 | qgroups, tmp, scratch_leaf); |
2f232036 JS |
2727 | } |
2728 | if (err > 0) | |
2729 | btrfs_commit_transaction(trans, fs_info->fs_root); | |
2730 | else | |
2731 | btrfs_end_transaction(trans, fs_info->fs_root); | |
2732 | } | |
2733 | ||
2734 | out: | |
2735 | kfree(scratch_leaf); | |
fcebe456 | 2736 | ulist_free(qgroups); |
2a108409 | 2737 | ulist_free(tmp); |
2f232036 | 2738 | btrfs_free_path(path); |
2f232036 JS |
2739 | |
2740 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2741 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2742 | ||
2743 | if (err == 2 && | |
2744 | fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) { | |
2745 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2746 | } else if (err < 0) { | |
2747 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; | |
2748 | } | |
2749 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2750 | ||
2751 | if (err >= 0) { | |
efe120a0 | 2752 | btrfs_info(fs_info, "qgroup scan completed%s", |
2f232036 JS |
2753 | err == 2 ? " (inconsistency flag cleared)" : ""); |
2754 | } else { | |
efe120a0 | 2755 | btrfs_err(fs_info, "qgroup scan failed with %d", err); |
2f232036 | 2756 | } |
57254b6e JS |
2757 | |
2758 | complete_all(&fs_info->qgroup_rescan_completion); | |
2f232036 JS |
2759 | } |
2760 | ||
b382a324 JS |
2761 | /* |
2762 | * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all | |
2763 | * memory required for the rescan context. | |
2764 | */ | |
2765 | static int | |
2766 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, | |
2767 | int init_flags) | |
2f232036 JS |
2768 | { |
2769 | int ret = 0; | |
2f232036 | 2770 | |
b382a324 JS |
2771 | if (!init_flags && |
2772 | (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) || | |
2773 | !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) { | |
2774 | ret = -EINVAL; | |
2775 | goto err; | |
2776 | } | |
2f232036 JS |
2777 | |
2778 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2779 | spin_lock(&fs_info->qgroup_lock); | |
b382a324 JS |
2780 | |
2781 | if (init_flags) { | |
2782 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) | |
2783 | ret = -EINPROGRESS; | |
2784 | else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) | |
2785 | ret = -EINVAL; | |
2786 | ||
2787 | if (ret) { | |
2788 | spin_unlock(&fs_info->qgroup_lock); | |
2789 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2790 | goto err; | |
2791 | } | |
2792 | ||
2793 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2f232036 JS |
2794 | } |
2795 | ||
2f232036 JS |
2796 | memset(&fs_info->qgroup_rescan_progress, 0, |
2797 | sizeof(fs_info->qgroup_rescan_progress)); | |
b382a324 JS |
2798 | fs_info->qgroup_rescan_progress.objectid = progress_objectid; |
2799 | ||
2800 | spin_unlock(&fs_info->qgroup_lock); | |
2801 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2802 | ||
57254b6e | 2803 | init_completion(&fs_info->qgroup_rescan_completion); |
2f232036 | 2804 | |
b382a324 JS |
2805 | memset(&fs_info->qgroup_rescan_work, 0, |
2806 | sizeof(fs_info->qgroup_rescan_work)); | |
fc97fab0 | 2807 | btrfs_init_work(&fs_info->qgroup_rescan_work, |
9e0af237 | 2808 | btrfs_qgroup_rescan_helper, |
fc97fab0 | 2809 | btrfs_qgroup_rescan_worker, NULL, NULL); |
b382a324 JS |
2810 | |
2811 | if (ret) { | |
2812 | err: | |
efe120a0 | 2813 | btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret); |
b382a324 JS |
2814 | return ret; |
2815 | } | |
2816 | ||
2817 | return 0; | |
2818 | } | |
2819 | ||
2820 | static void | |
2821 | qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info) | |
2822 | { | |
2823 | struct rb_node *n; | |
2824 | struct btrfs_qgroup *qgroup; | |
2825 | ||
2826 | spin_lock(&fs_info->qgroup_lock); | |
2f232036 JS |
2827 | /* clear all current qgroup tracking information */ |
2828 | for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) { | |
2829 | qgroup = rb_entry(n, struct btrfs_qgroup, node); | |
2830 | qgroup->rfer = 0; | |
2831 | qgroup->rfer_cmpr = 0; | |
2832 | qgroup->excl = 0; | |
2833 | qgroup->excl_cmpr = 0; | |
2834 | } | |
2835 | spin_unlock(&fs_info->qgroup_lock); | |
b382a324 | 2836 | } |
2f232036 | 2837 | |
b382a324 JS |
2838 | int |
2839 | btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info) | |
2840 | { | |
2841 | int ret = 0; | |
2842 | struct btrfs_trans_handle *trans; | |
2843 | ||
2844 | ret = qgroup_rescan_init(fs_info, 0, 1); | |
2845 | if (ret) | |
2846 | return ret; | |
2847 | ||
2848 | /* | |
2849 | * We have set the rescan_progress to 0, which means no more | |
2850 | * delayed refs will be accounted by btrfs_qgroup_account_ref. | |
2851 | * However, btrfs_qgroup_account_ref may be right after its call | |
2852 | * to btrfs_find_all_roots, in which case it would still do the | |
2853 | * accounting. | |
2854 | * To solve this, we're committing the transaction, which will | |
2855 | * ensure we run all delayed refs and only after that, we are | |
2856 | * going to clear all tracking information for a clean start. | |
2857 | */ | |
2858 | ||
2859 | trans = btrfs_join_transaction(fs_info->fs_root); | |
2860 | if (IS_ERR(trans)) { | |
2861 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2862 | return PTR_ERR(trans); | |
2863 | } | |
2864 | ret = btrfs_commit_transaction(trans, fs_info->fs_root); | |
2865 | if (ret) { | |
2866 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2867 | return ret; | |
2868 | } | |
2869 | ||
2870 | qgroup_rescan_zero_tracking(fs_info); | |
2871 | ||
fc97fab0 QW |
2872 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
2873 | &fs_info->qgroup_rescan_work); | |
2f232036 JS |
2874 | |
2875 | return 0; | |
2876 | } | |
57254b6e JS |
2877 | |
2878 | int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info) | |
2879 | { | |
2880 | int running; | |
2881 | int ret = 0; | |
2882 | ||
2883 | mutex_lock(&fs_info->qgroup_rescan_lock); | |
2884 | spin_lock(&fs_info->qgroup_lock); | |
2885 | running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN; | |
2886 | spin_unlock(&fs_info->qgroup_lock); | |
2887 | mutex_unlock(&fs_info->qgroup_rescan_lock); | |
2888 | ||
2889 | if (running) | |
2890 | ret = wait_for_completion_interruptible( | |
2891 | &fs_info->qgroup_rescan_completion); | |
2892 | ||
2893 | return ret; | |
2894 | } | |
b382a324 JS |
2895 | |
2896 | /* | |
2897 | * this is only called from open_ctree where we're still single threaded, thus | |
2898 | * locking is omitted here. | |
2899 | */ | |
2900 | void | |
2901 | btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info) | |
2902 | { | |
2903 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) | |
fc97fab0 QW |
2904 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
2905 | &fs_info->qgroup_rescan_work); | |
b382a324 | 2906 | } |