David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 STRATO. All rights reserved. |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/sched.h> |
| 7 | #include <linux/pagemap.h> |
| 8 | #include <linux/writeback.h> |
| 9 | #include <linux/blkdev.h> |
| 10 | #include <linux/rbtree.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/workqueue.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 13 | #include <linux/btrfs.h> |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 14 | #include <linux/sched/mm.h> |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 15 | |
| 16 | #include "ctree.h" |
| 17 | #include "transaction.h" |
| 18 | #include "disk-io.h" |
| 19 | #include "locking.h" |
| 20 | #include "ulist.h" |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 21 | #include "backref.h" |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 22 | #include "extent_io.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 23 | #include "qgroup.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 24 | #include "block-group.h" |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 25 | #include "sysfs.h" |
Qu Wenruo | e69bcee | 2015-04-17 10:23:16 +0800 | [diff] [blame] | 26 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 27 | /* TODO XXX FIXME |
| 28 | * - subvol delete -> delete when ref goes to 0? delete limits also? |
| 29 | * - reorganize keys |
| 30 | * - compressed |
| 31 | * - sync |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 32 | * - copy also limits on subvol creation |
| 33 | * - limit |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 34 | * - caches for ulists |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 35 | * - performance benchmarks |
| 36 | * - check all ioctl parameters |
| 37 | */ |
| 38 | |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 39 | /* |
| 40 | * Helpers to access qgroup reservation |
| 41 | * |
| 42 | * Callers should ensure the lock context and type are valid |
| 43 | */ |
| 44 | |
| 45 | static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup) |
| 46 | { |
| 47 | u64 ret = 0; |
| 48 | int i; |
| 49 | |
| 50 | for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) |
| 51 | ret += qgroup->rsv.values[i]; |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | #ifdef CONFIG_BTRFS_DEBUG |
| 57 | static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type) |
| 58 | { |
| 59 | if (type == BTRFS_QGROUP_RSV_DATA) |
| 60 | return "data"; |
Qu Wenruo | 733e03a | 2017-12-12 15:34:29 +0800 | [diff] [blame] | 61 | if (type == BTRFS_QGROUP_RSV_META_PERTRANS) |
| 62 | return "meta_pertrans"; |
| 63 | if (type == BTRFS_QGROUP_RSV_META_PREALLOC) |
| 64 | return "meta_prealloc"; |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
| 67 | #endif |
| 68 | |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 69 | static void qgroup_rsv_add(struct btrfs_fs_info *fs_info, |
| 70 | struct btrfs_qgroup *qgroup, u64 num_bytes, |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 71 | enum btrfs_qgroup_rsv_type type) |
| 72 | { |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 73 | trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type); |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 74 | qgroup->rsv.values[type] += num_bytes; |
| 75 | } |
| 76 | |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 77 | static void qgroup_rsv_release(struct btrfs_fs_info *fs_info, |
| 78 | struct btrfs_qgroup *qgroup, u64 num_bytes, |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 79 | enum btrfs_qgroup_rsv_type type) |
| 80 | { |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 81 | trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type); |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 82 | if (qgroup->rsv.values[type] >= num_bytes) { |
| 83 | qgroup->rsv.values[type] -= num_bytes; |
| 84 | return; |
| 85 | } |
| 86 | #ifdef CONFIG_BTRFS_DEBUG |
| 87 | WARN_RATELIMIT(1, |
| 88 | "qgroup %llu %s reserved space underflow, have %llu to free %llu", |
| 89 | qgroup->qgroupid, qgroup_rsv_type_str(type), |
| 90 | qgroup->rsv.values[type], num_bytes); |
| 91 | #endif |
| 92 | qgroup->rsv.values[type] = 0; |
| 93 | } |
| 94 | |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 95 | static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info, |
| 96 | struct btrfs_qgroup *dest, |
| 97 | struct btrfs_qgroup *src) |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 98 | { |
| 99 | int i; |
| 100 | |
| 101 | for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 102 | qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i); |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 103 | } |
| 104 | |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 105 | static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info, |
| 106 | struct btrfs_qgroup *dest, |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 107 | struct btrfs_qgroup *src) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 111 | for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 112 | qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i); |
Qu Wenruo | f59c034 | 2017-12-12 15:34:24 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Qu Wenruo | 9c54213 | 2015-03-12 16:10:13 +0800 | [diff] [blame] | 115 | static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq, |
| 116 | int mod) |
| 117 | { |
| 118 | if (qg->old_refcnt < seq) |
| 119 | qg->old_refcnt = seq; |
| 120 | qg->old_refcnt += mod; |
| 121 | } |
| 122 | |
| 123 | static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq, |
| 124 | int mod) |
| 125 | { |
| 126 | if (qg->new_refcnt < seq) |
| 127 | qg->new_refcnt = seq; |
| 128 | qg->new_refcnt += mod; |
| 129 | } |
| 130 | |
| 131 | static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq) |
| 132 | { |
| 133 | if (qg->old_refcnt < seq) |
| 134 | return 0; |
| 135 | return qg->old_refcnt - seq; |
| 136 | } |
| 137 | |
| 138 | static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq) |
| 139 | { |
| 140 | if (qg->new_refcnt < seq) |
| 141 | return 0; |
| 142 | return qg->new_refcnt - seq; |
| 143 | } |
| 144 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 145 | /* |
| 146 | * glue structure to represent the relations between qgroups. |
| 147 | */ |
| 148 | struct btrfs_qgroup_list { |
| 149 | struct list_head next_group; |
| 150 | struct list_head next_member; |
| 151 | struct btrfs_qgroup *group; |
| 152 | struct btrfs_qgroup *member; |
| 153 | }; |
| 154 | |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 155 | static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg) |
| 156 | { |
| 157 | return (u64)(uintptr_t)qg; |
| 158 | } |
| 159 | |
| 160 | static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n) |
| 161 | { |
| 162 | return (struct btrfs_qgroup *)(uintptr_t)n->aux; |
| 163 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 164 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 165 | static int |
| 166 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, |
| 167 | int init_flags); |
| 168 | static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 169 | |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 170 | /* must be called with qgroup_ioctl_lock held */ |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 171 | static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info, |
| 172 | u64 qgroupid) |
| 173 | { |
| 174 | struct rb_node *n = fs_info->qgroup_tree.rb_node; |
| 175 | struct btrfs_qgroup *qgroup; |
| 176 | |
| 177 | while (n) { |
| 178 | qgroup = rb_entry(n, struct btrfs_qgroup, node); |
| 179 | if (qgroup->qgroupid < qgroupid) |
| 180 | n = n->rb_left; |
| 181 | else if (qgroup->qgroupid > qgroupid) |
| 182 | n = n->rb_right; |
| 183 | else |
| 184 | return qgroup; |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | /* must be called with qgroup_lock held */ |
| 190 | static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info, |
| 191 | u64 qgroupid) |
| 192 | { |
| 193 | struct rb_node **p = &fs_info->qgroup_tree.rb_node; |
| 194 | struct rb_node *parent = NULL; |
| 195 | struct btrfs_qgroup *qgroup; |
| 196 | |
| 197 | while (*p) { |
| 198 | parent = *p; |
| 199 | qgroup = rb_entry(parent, struct btrfs_qgroup, node); |
| 200 | |
| 201 | if (qgroup->qgroupid < qgroupid) |
| 202 | p = &(*p)->rb_left; |
| 203 | else if (qgroup->qgroupid > qgroupid) |
| 204 | p = &(*p)->rb_right; |
| 205 | else |
| 206 | return qgroup; |
| 207 | } |
| 208 | |
| 209 | qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC); |
| 210 | if (!qgroup) |
| 211 | return ERR_PTR(-ENOMEM); |
| 212 | |
| 213 | qgroup->qgroupid = qgroupid; |
| 214 | INIT_LIST_HEAD(&qgroup->groups); |
| 215 | INIT_LIST_HEAD(&qgroup->members); |
| 216 | INIT_LIST_HEAD(&qgroup->dirty); |
| 217 | |
| 218 | rb_link_node(&qgroup->node, parent, p); |
| 219 | rb_insert_color(&qgroup->node, &fs_info->qgroup_tree); |
| 220 | |
| 221 | return qgroup; |
| 222 | } |
| 223 | |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 224 | static void __del_qgroup_rb(struct btrfs_fs_info *fs_info, |
| 225 | struct btrfs_qgroup *qgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 226 | { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 227 | struct btrfs_qgroup_list *list; |
| 228 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 229 | list_del(&qgroup->dirty); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 230 | while (!list_empty(&qgroup->groups)) { |
| 231 | list = list_first_entry(&qgroup->groups, |
| 232 | struct btrfs_qgroup_list, next_group); |
| 233 | list_del(&list->next_group); |
| 234 | list_del(&list->next_member); |
| 235 | kfree(list); |
| 236 | } |
| 237 | |
| 238 | while (!list_empty(&qgroup->members)) { |
| 239 | list = list_first_entry(&qgroup->members, |
| 240 | struct btrfs_qgroup_list, next_member); |
| 241 | list_del(&list->next_group); |
| 242 | list_del(&list->next_member); |
| 243 | kfree(list); |
| 244 | } |
Wang Shilong | 4082bd3 | 2013-08-14 09:13:36 +0800 | [diff] [blame] | 245 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 246 | |
Wang Shilong | 4082bd3 | 2013-08-14 09:13:36 +0800 | [diff] [blame] | 247 | /* must be called with qgroup_lock held */ |
| 248 | static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid) |
| 249 | { |
| 250 | struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 251 | |
| 252 | if (!qgroup) |
| 253 | return -ENOENT; |
| 254 | |
| 255 | rb_erase(&qgroup->node, &fs_info->qgroup_tree); |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 256 | __del_qgroup_rb(fs_info, qgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* must be called with qgroup_lock held */ |
| 261 | static int add_relation_rb(struct btrfs_fs_info *fs_info, |
| 262 | u64 memberid, u64 parentid) |
| 263 | { |
| 264 | struct btrfs_qgroup *member; |
| 265 | struct btrfs_qgroup *parent; |
| 266 | struct btrfs_qgroup_list *list; |
| 267 | |
| 268 | member = find_qgroup_rb(fs_info, memberid); |
| 269 | parent = find_qgroup_rb(fs_info, parentid); |
| 270 | if (!member || !parent) |
| 271 | return -ENOENT; |
| 272 | |
| 273 | list = kzalloc(sizeof(*list), GFP_ATOMIC); |
| 274 | if (!list) |
| 275 | return -ENOMEM; |
| 276 | |
| 277 | list->group = parent; |
| 278 | list->member = member; |
| 279 | list_add_tail(&list->next_group, &member->groups); |
| 280 | list_add_tail(&list->next_member, &parent->members); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* must be called with qgroup_lock held */ |
| 286 | static int del_relation_rb(struct btrfs_fs_info *fs_info, |
| 287 | u64 memberid, u64 parentid) |
| 288 | { |
| 289 | struct btrfs_qgroup *member; |
| 290 | struct btrfs_qgroup *parent; |
| 291 | struct btrfs_qgroup_list *list; |
| 292 | |
| 293 | member = find_qgroup_rb(fs_info, memberid); |
| 294 | parent = find_qgroup_rb(fs_info, parentid); |
| 295 | if (!member || !parent) |
| 296 | return -ENOENT; |
| 297 | |
| 298 | list_for_each_entry(list, &member->groups, next_group) { |
| 299 | if (list->group == parent) { |
| 300 | list_del(&list->next_group); |
| 301 | list_del(&list->next_member); |
| 302 | kfree(list); |
| 303 | return 0; |
| 304 | } |
| 305 | } |
| 306 | return -ENOENT; |
| 307 | } |
| 308 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 309 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 310 | int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid, |
| 311 | u64 rfer, u64 excl) |
| 312 | { |
| 313 | struct btrfs_qgroup *qgroup; |
| 314 | |
| 315 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 316 | if (!qgroup) |
| 317 | return -EINVAL; |
| 318 | if (qgroup->rfer != rfer || qgroup->excl != excl) |
| 319 | return -EINVAL; |
| 320 | return 0; |
| 321 | } |
| 322 | #endif |
| 323 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 324 | /* |
| 325 | * The full config is read in one go, only called from open_ctree() |
| 326 | * It doesn't use any locking, as at this point we're still single-threaded |
| 327 | */ |
| 328 | int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) |
| 329 | { |
| 330 | struct btrfs_key key; |
| 331 | struct btrfs_key found_key; |
| 332 | struct btrfs_root *quota_root = fs_info->quota_root; |
| 333 | struct btrfs_path *path = NULL; |
| 334 | struct extent_buffer *l; |
| 335 | int slot; |
| 336 | int ret = 0; |
| 337 | u64 flags = 0; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 338 | u64 rescan_progress = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 339 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 340 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 341 | return 0; |
| 342 | |
David Sterba | 323b88f | 2017-02-13 12:10:20 +0100 | [diff] [blame] | 343 | fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 344 | if (!fs_info->qgroup_ulist) { |
| 345 | ret = -ENOMEM; |
| 346 | goto out; |
| 347 | } |
| 348 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 349 | path = btrfs_alloc_path(); |
| 350 | if (!path) { |
| 351 | ret = -ENOMEM; |
| 352 | goto out; |
| 353 | } |
| 354 | |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 355 | ret = btrfs_sysfs_add_qgroups(fs_info); |
| 356 | if (ret < 0) |
| 357 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 358 | /* default this to quota off, in case no status key is found */ |
| 359 | fs_info->qgroup_flags = 0; |
| 360 | |
| 361 | /* |
| 362 | * pass 1: read status, all qgroup infos and limits |
| 363 | */ |
| 364 | key.objectid = 0; |
| 365 | key.type = 0; |
| 366 | key.offset = 0; |
| 367 | ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1); |
| 368 | if (ret) |
| 369 | goto out; |
| 370 | |
| 371 | while (1) { |
| 372 | struct btrfs_qgroup *qgroup; |
| 373 | |
| 374 | slot = path->slots[0]; |
| 375 | l = path->nodes[0]; |
| 376 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 377 | |
| 378 | if (found_key.type == BTRFS_QGROUP_STATUS_KEY) { |
| 379 | struct btrfs_qgroup_status_item *ptr; |
| 380 | |
| 381 | ptr = btrfs_item_ptr(l, slot, |
| 382 | struct btrfs_qgroup_status_item); |
| 383 | |
| 384 | if (btrfs_qgroup_status_version(l, ptr) != |
| 385 | BTRFS_QGROUP_STATUS_VERSION) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 386 | btrfs_err(fs_info, |
| 387 | "old qgroup version, quota disabled"); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 388 | goto out; |
| 389 | } |
| 390 | if (btrfs_qgroup_status_generation(l, ptr) != |
| 391 | fs_info->generation) { |
| 392 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 393 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 394 | "qgroup generation mismatch, marked as inconsistent"); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 395 | } |
| 396 | fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, |
| 397 | ptr); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 398 | rescan_progress = btrfs_qgroup_status_rescan(l, ptr); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 399 | goto next1; |
| 400 | } |
| 401 | |
| 402 | if (found_key.type != BTRFS_QGROUP_INFO_KEY && |
| 403 | found_key.type != BTRFS_QGROUP_LIMIT_KEY) |
| 404 | goto next1; |
| 405 | |
| 406 | qgroup = find_qgroup_rb(fs_info, found_key.offset); |
| 407 | if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) || |
| 408 | (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) { |
Geert Uytterhoeven | d41e36a | 2015-07-06 15:38:11 +0200 | [diff] [blame] | 409 | btrfs_err(fs_info, "inconsistent qgroup config"); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 410 | flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 411 | } |
| 412 | if (!qgroup) { |
| 413 | qgroup = add_qgroup_rb(fs_info, found_key.offset); |
| 414 | if (IS_ERR(qgroup)) { |
| 415 | ret = PTR_ERR(qgroup); |
| 416 | goto out; |
| 417 | } |
| 418 | } |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 419 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
| 420 | if (ret < 0) |
| 421 | goto out; |
| 422 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 423 | switch (found_key.type) { |
| 424 | case BTRFS_QGROUP_INFO_KEY: { |
| 425 | struct btrfs_qgroup_info_item *ptr; |
| 426 | |
| 427 | ptr = btrfs_item_ptr(l, slot, |
| 428 | struct btrfs_qgroup_info_item); |
| 429 | qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr); |
| 430 | qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr); |
| 431 | qgroup->excl = btrfs_qgroup_info_excl(l, ptr); |
| 432 | qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr); |
| 433 | /* generation currently unused */ |
| 434 | break; |
| 435 | } |
| 436 | case BTRFS_QGROUP_LIMIT_KEY: { |
| 437 | struct btrfs_qgroup_limit_item *ptr; |
| 438 | |
| 439 | ptr = btrfs_item_ptr(l, slot, |
| 440 | struct btrfs_qgroup_limit_item); |
| 441 | qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr); |
| 442 | qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr); |
| 443 | qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr); |
| 444 | qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr); |
| 445 | qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | next1: |
| 450 | ret = btrfs_next_item(quota_root, path); |
| 451 | if (ret < 0) |
| 452 | goto out; |
| 453 | if (ret) |
| 454 | break; |
| 455 | } |
| 456 | btrfs_release_path(path); |
| 457 | |
| 458 | /* |
| 459 | * pass 2: read all qgroup relations |
| 460 | */ |
| 461 | key.objectid = 0; |
| 462 | key.type = BTRFS_QGROUP_RELATION_KEY; |
| 463 | key.offset = 0; |
| 464 | ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0); |
| 465 | if (ret) |
| 466 | goto out; |
| 467 | while (1) { |
| 468 | slot = path->slots[0]; |
| 469 | l = path->nodes[0]; |
| 470 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 471 | |
| 472 | if (found_key.type != BTRFS_QGROUP_RELATION_KEY) |
| 473 | goto next2; |
| 474 | |
| 475 | if (found_key.objectid > found_key.offset) { |
| 476 | /* parent <- member, not needed to build config */ |
| 477 | /* FIXME should we omit the key completely? */ |
| 478 | goto next2; |
| 479 | } |
| 480 | |
| 481 | ret = add_relation_rb(fs_info, found_key.objectid, |
| 482 | found_key.offset); |
Arne Jansen | ff24858 | 2013-01-17 01:22:08 -0700 | [diff] [blame] | 483 | if (ret == -ENOENT) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 484 | btrfs_warn(fs_info, |
| 485 | "orphan qgroup relation 0x%llx->0x%llx", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 486 | found_key.objectid, found_key.offset); |
Arne Jansen | ff24858 | 2013-01-17 01:22:08 -0700 | [diff] [blame] | 487 | ret = 0; /* ignore the error */ |
| 488 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 489 | if (ret) |
| 490 | goto out; |
| 491 | next2: |
| 492 | ret = btrfs_next_item(quota_root, path); |
| 493 | if (ret < 0) |
| 494 | goto out; |
| 495 | if (ret) |
| 496 | break; |
| 497 | } |
| 498 | out: |
Filipe Manana | 3d05cad | 2020-11-23 14:28:44 +0000 | [diff] [blame] | 499 | btrfs_free_path(path); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 500 | fs_info->qgroup_flags |= flags; |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 501 | if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) |
| 502 | clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); |
| 503 | else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN && |
| 504 | ret >= 0) |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 505 | ret = qgroup_rescan_init(fs_info, rescan_progress, 0); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 506 | |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 507 | if (ret < 0) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 508 | ulist_free(fs_info->qgroup_ulist); |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 509 | fs_info->qgroup_ulist = NULL; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 510 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 511 | btrfs_sysfs_del_qgroups(fs_info); |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 512 | } |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 513 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 514 | return ret < 0 ? ret : 0; |
| 515 | } |
| 516 | |
Qu Wenruo | 5958253 | 2020-06-10 09:04:44 +0800 | [diff] [blame] | 517 | /* |
| 518 | * Called in close_ctree() when quota is still enabled. This verifies we don't |
| 519 | * leak some reserved space. |
| 520 | * |
| 521 | * Return false if no reserved space is left. |
| 522 | * Return true if some reserved space is leaked. |
| 523 | */ |
| 524 | bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info) |
| 525 | { |
| 526 | struct rb_node *node; |
| 527 | bool ret = false; |
| 528 | |
| 529 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
| 530 | return ret; |
| 531 | /* |
| 532 | * Since we're unmounting, there is no race and no need to grab qgroup |
| 533 | * lock. And here we don't go post-order to provide a more user |
| 534 | * friendly sorted result. |
| 535 | */ |
| 536 | for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) { |
| 537 | struct btrfs_qgroup *qgroup; |
| 538 | int i; |
| 539 | |
| 540 | qgroup = rb_entry(node, struct btrfs_qgroup, node); |
| 541 | for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) { |
| 542 | if (qgroup->rsv.values[i]) { |
| 543 | ret = true; |
| 544 | btrfs_warn(fs_info, |
Qu Wenruo | 06f67c4 | 2020-06-28 13:07:14 +0800 | [diff] [blame] | 545 | "qgroup %hu/%llu has unreleased space, type %d rsv %llu", |
Qu Wenruo | 5958253 | 2020-06-10 09:04:44 +0800 | [diff] [blame] | 546 | btrfs_qgroup_level(qgroup->qgroupid), |
| 547 | btrfs_qgroup_subvolid(qgroup->qgroupid), |
| 548 | i, qgroup->rsv.values[i]); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | return ret; |
| 553 | } |
| 554 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 555 | /* |
Wang Shilong | e685da1 | 2013-08-14 09:13:37 +0800 | [diff] [blame] | 556 | * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(), |
| 557 | * first two are in single-threaded paths.And for the third one, we have set |
| 558 | * quota_root to be null with qgroup_lock held before, so it is safe to clean |
| 559 | * up the in-memory structures without qgroup_lock held. |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 560 | */ |
| 561 | void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info) |
| 562 | { |
| 563 | struct rb_node *n; |
| 564 | struct btrfs_qgroup *qgroup; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 565 | |
| 566 | while ((n = rb_first(&fs_info->qgroup_tree))) { |
| 567 | qgroup = rb_entry(n, struct btrfs_qgroup, node); |
| 568 | rb_erase(n, &fs_info->qgroup_tree); |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 569 | __del_qgroup_rb(fs_info, qgroup); |
Filipe Manana | 0bb7883 | 2021-03-18 11:22:05 +0000 | [diff] [blame] | 570 | btrfs_sysfs_del_one_qgroup(fs_info, qgroup); |
| 571 | kfree(qgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 572 | } |
Wang Shilong | 1e7bac1 | 2013-07-13 21:02:54 +0800 | [diff] [blame] | 573 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 574 | * We call btrfs_free_qgroup_config() when unmounting |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 575 | * filesystem and disabling quota, so we set qgroup_ulist |
Wang Shilong | 1e7bac1 | 2013-07-13 21:02:54 +0800 | [diff] [blame] | 576 | * to be null here to avoid double free. |
| 577 | */ |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 578 | ulist_free(fs_info->qgroup_ulist); |
Wang Shilong | 1e7bac1 | 2013-07-13 21:02:54 +0800 | [diff] [blame] | 579 | fs_info->qgroup_ulist = NULL; |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 580 | btrfs_sysfs_del_qgroups(fs_info); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Lu Fengqi | 711169c | 2018-07-18 14:45:24 +0800 | [diff] [blame] | 583 | static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src, |
| 584 | u64 dst) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 585 | { |
| 586 | int ret; |
Lu Fengqi | 711169c | 2018-07-18 14:45:24 +0800 | [diff] [blame] | 587 | struct btrfs_root *quota_root = trans->fs_info->quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 588 | struct btrfs_path *path; |
| 589 | struct btrfs_key key; |
| 590 | |
| 591 | path = btrfs_alloc_path(); |
| 592 | if (!path) |
| 593 | return -ENOMEM; |
| 594 | |
| 595 | key.objectid = src; |
| 596 | key.type = BTRFS_QGROUP_RELATION_KEY; |
| 597 | key.offset = dst; |
| 598 | |
| 599 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0); |
| 600 | |
| 601 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 602 | |
| 603 | btrfs_free_path(path); |
| 604 | return ret; |
| 605 | } |
| 606 | |
Lu Fengqi | 99d7f09 | 2018-07-18 14:45:25 +0800 | [diff] [blame] | 607 | static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src, |
| 608 | u64 dst) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 609 | { |
| 610 | int ret; |
Lu Fengqi | 99d7f09 | 2018-07-18 14:45:25 +0800 | [diff] [blame] | 611 | struct btrfs_root *quota_root = trans->fs_info->quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 612 | struct btrfs_path *path; |
| 613 | struct btrfs_key key; |
| 614 | |
| 615 | path = btrfs_alloc_path(); |
| 616 | if (!path) |
| 617 | return -ENOMEM; |
| 618 | |
| 619 | key.objectid = src; |
| 620 | key.type = BTRFS_QGROUP_RELATION_KEY; |
| 621 | key.offset = dst; |
| 622 | |
| 623 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); |
| 624 | if (ret < 0) |
| 625 | goto out; |
| 626 | |
| 627 | if (ret > 0) { |
| 628 | ret = -ENOENT; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | ret = btrfs_del_item(trans, quota_root, path); |
| 633 | out: |
| 634 | btrfs_free_path(path); |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int add_qgroup_item(struct btrfs_trans_handle *trans, |
| 639 | struct btrfs_root *quota_root, u64 qgroupid) |
| 640 | { |
| 641 | int ret; |
| 642 | struct btrfs_path *path; |
| 643 | struct btrfs_qgroup_info_item *qgroup_info; |
| 644 | struct btrfs_qgroup_limit_item *qgroup_limit; |
| 645 | struct extent_buffer *leaf; |
| 646 | struct btrfs_key key; |
| 647 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 648 | if (btrfs_is_testing(quota_root->fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 649 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 650 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 651 | path = btrfs_alloc_path(); |
| 652 | if (!path) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | key.objectid = 0; |
| 656 | key.type = BTRFS_QGROUP_INFO_KEY; |
| 657 | key.offset = qgroupid; |
| 658 | |
Mark Fasheh | 0b4699d | 2014-08-18 14:01:17 -0700 | [diff] [blame] | 659 | /* |
| 660 | * Avoid a transaction abort by catching -EEXIST here. In that |
| 661 | * case, we proceed by re-initializing the existing structure |
| 662 | * on disk. |
| 663 | */ |
| 664 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 665 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
| 666 | sizeof(*qgroup_info)); |
Mark Fasheh | 0b4699d | 2014-08-18 14:01:17 -0700 | [diff] [blame] | 667 | if (ret && ret != -EEXIST) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 668 | goto out; |
| 669 | |
| 670 | leaf = path->nodes[0]; |
| 671 | qgroup_info = btrfs_item_ptr(leaf, path->slots[0], |
| 672 | struct btrfs_qgroup_info_item); |
| 673 | btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid); |
| 674 | btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0); |
| 675 | btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0); |
| 676 | btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0); |
| 677 | btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0); |
| 678 | |
| 679 | btrfs_mark_buffer_dirty(leaf); |
| 680 | |
| 681 | btrfs_release_path(path); |
| 682 | |
| 683 | key.type = BTRFS_QGROUP_LIMIT_KEY; |
| 684 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
| 685 | sizeof(*qgroup_limit)); |
Mark Fasheh | 0b4699d | 2014-08-18 14:01:17 -0700 | [diff] [blame] | 686 | if (ret && ret != -EEXIST) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 687 | goto out; |
| 688 | |
| 689 | leaf = path->nodes[0]; |
| 690 | qgroup_limit = btrfs_item_ptr(leaf, path->slots[0], |
| 691 | struct btrfs_qgroup_limit_item); |
| 692 | btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0); |
| 693 | btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0); |
| 694 | btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0); |
| 695 | btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0); |
| 696 | btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0); |
| 697 | |
| 698 | btrfs_mark_buffer_dirty(leaf); |
| 699 | |
| 700 | ret = 0; |
| 701 | out: |
| 702 | btrfs_free_path(path); |
| 703 | return ret; |
| 704 | } |
| 705 | |
Lu Fengqi | 6910461 | 2018-07-18 14:45:26 +0800 | [diff] [blame] | 706 | static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 707 | { |
| 708 | int ret; |
Lu Fengqi | 6910461 | 2018-07-18 14:45:26 +0800 | [diff] [blame] | 709 | struct btrfs_root *quota_root = trans->fs_info->quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 710 | struct btrfs_path *path; |
| 711 | struct btrfs_key key; |
| 712 | |
| 713 | path = btrfs_alloc_path(); |
| 714 | if (!path) |
| 715 | return -ENOMEM; |
| 716 | |
| 717 | key.objectid = 0; |
| 718 | key.type = BTRFS_QGROUP_INFO_KEY; |
| 719 | key.offset = qgroupid; |
| 720 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); |
| 721 | if (ret < 0) |
| 722 | goto out; |
| 723 | |
| 724 | if (ret > 0) { |
| 725 | ret = -ENOENT; |
| 726 | goto out; |
| 727 | } |
| 728 | |
| 729 | ret = btrfs_del_item(trans, quota_root, path); |
| 730 | if (ret) |
| 731 | goto out; |
| 732 | |
| 733 | btrfs_release_path(path); |
| 734 | |
| 735 | key.type = BTRFS_QGROUP_LIMIT_KEY; |
| 736 | ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1); |
| 737 | if (ret < 0) |
| 738 | goto out; |
| 739 | |
| 740 | if (ret > 0) { |
| 741 | ret = -ENOENT; |
| 742 | goto out; |
| 743 | } |
| 744 | |
| 745 | ret = btrfs_del_item(trans, quota_root, path); |
| 746 | |
| 747 | out: |
| 748 | btrfs_free_path(path); |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 753 | struct btrfs_qgroup *qgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 754 | { |
Lu Fengqi | ac8a866 | 2018-07-18 14:45:27 +0800 | [diff] [blame] | 755 | struct btrfs_root *quota_root = trans->fs_info->quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 756 | struct btrfs_path *path; |
| 757 | struct btrfs_key key; |
| 758 | struct extent_buffer *l; |
| 759 | struct btrfs_qgroup_limit_item *qgroup_limit; |
| 760 | int ret; |
| 761 | int slot; |
| 762 | |
| 763 | key.objectid = 0; |
| 764 | key.type = BTRFS_QGROUP_LIMIT_KEY; |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 765 | key.offset = qgroup->qgroupid; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 766 | |
| 767 | path = btrfs_alloc_path(); |
Wang Shilong | 84cbe2f | 2013-02-27 11:20:56 +0000 | [diff] [blame] | 768 | if (!path) |
| 769 | return -ENOMEM; |
| 770 | |
Lu Fengqi | ac8a866 | 2018-07-18 14:45:27 +0800 | [diff] [blame] | 771 | ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 772 | if (ret > 0) |
| 773 | ret = -ENOENT; |
| 774 | |
| 775 | if (ret) |
| 776 | goto out; |
| 777 | |
| 778 | l = path->nodes[0]; |
| 779 | slot = path->slots[0]; |
Valentina Giusti | a3df41e | 2013-11-04 22:34:29 +0100 | [diff] [blame] | 780 | qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 781 | btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags); |
| 782 | btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer); |
| 783 | btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl); |
| 784 | btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer); |
| 785 | btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 786 | |
| 787 | btrfs_mark_buffer_dirty(l); |
| 788 | |
| 789 | out: |
| 790 | btrfs_free_path(path); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static int update_qgroup_info_item(struct btrfs_trans_handle *trans, |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 795 | struct btrfs_qgroup *qgroup) |
| 796 | { |
Lu Fengqi | 3e07e9a | 2018-07-18 14:45:28 +0800 | [diff] [blame] | 797 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 798 | struct btrfs_root *quota_root = fs_info->quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 799 | struct btrfs_path *path; |
| 800 | struct btrfs_key key; |
| 801 | struct extent_buffer *l; |
| 802 | struct btrfs_qgroup_info_item *qgroup_info; |
| 803 | int ret; |
| 804 | int slot; |
| 805 | |
Lu Fengqi | 3e07e9a | 2018-07-18 14:45:28 +0800 | [diff] [blame] | 806 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 807 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 808 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 809 | key.objectid = 0; |
| 810 | key.type = BTRFS_QGROUP_INFO_KEY; |
| 811 | key.offset = qgroup->qgroupid; |
| 812 | |
| 813 | path = btrfs_alloc_path(); |
Wang Shilong | 84cbe2f | 2013-02-27 11:20:56 +0000 | [diff] [blame] | 814 | if (!path) |
| 815 | return -ENOMEM; |
| 816 | |
Lu Fengqi | 3e07e9a | 2018-07-18 14:45:28 +0800 | [diff] [blame] | 817 | ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 818 | if (ret > 0) |
| 819 | ret = -ENOENT; |
| 820 | |
| 821 | if (ret) |
| 822 | goto out; |
| 823 | |
| 824 | l = path->nodes[0]; |
| 825 | slot = path->slots[0]; |
Valentina Giusti | a3df41e | 2013-11-04 22:34:29 +0100 | [diff] [blame] | 826 | qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 827 | btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid); |
| 828 | btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer); |
| 829 | btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr); |
| 830 | btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl); |
| 831 | btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr); |
| 832 | |
| 833 | btrfs_mark_buffer_dirty(l); |
| 834 | |
| 835 | out: |
| 836 | btrfs_free_path(path); |
| 837 | return ret; |
| 838 | } |
| 839 | |
Lu Fengqi | 2e980ac | 2018-07-18 14:45:29 +0800 | [diff] [blame] | 840 | static int update_qgroup_status_item(struct btrfs_trans_handle *trans) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 841 | { |
Lu Fengqi | 2e980ac | 2018-07-18 14:45:29 +0800 | [diff] [blame] | 842 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 843 | struct btrfs_root *quota_root = fs_info->quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 844 | struct btrfs_path *path; |
| 845 | struct btrfs_key key; |
| 846 | struct extent_buffer *l; |
| 847 | struct btrfs_qgroup_status_item *ptr; |
| 848 | int ret; |
| 849 | int slot; |
| 850 | |
| 851 | key.objectid = 0; |
| 852 | key.type = BTRFS_QGROUP_STATUS_KEY; |
| 853 | key.offset = 0; |
| 854 | |
| 855 | path = btrfs_alloc_path(); |
Wang Shilong | 84cbe2f | 2013-02-27 11:20:56 +0000 | [diff] [blame] | 856 | if (!path) |
| 857 | return -ENOMEM; |
| 858 | |
Lu Fengqi | 2e980ac | 2018-07-18 14:45:29 +0800 | [diff] [blame] | 859 | ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 860 | if (ret > 0) |
| 861 | ret = -ENOENT; |
| 862 | |
| 863 | if (ret) |
| 864 | goto out; |
| 865 | |
| 866 | l = path->nodes[0]; |
| 867 | slot = path->slots[0]; |
| 868 | ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item); |
| 869 | btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags); |
| 870 | btrfs_set_qgroup_status_generation(l, ptr, trans->transid); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 871 | btrfs_set_qgroup_status_rescan(l, ptr, |
| 872 | fs_info->qgroup_rescan_progress.objectid); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 873 | |
| 874 | btrfs_mark_buffer_dirty(l); |
| 875 | |
| 876 | out: |
| 877 | btrfs_free_path(path); |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * called with qgroup_lock held |
| 883 | */ |
| 884 | static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans, |
| 885 | struct btrfs_root *root) |
| 886 | { |
| 887 | struct btrfs_path *path; |
| 888 | struct btrfs_key key; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 889 | struct extent_buffer *leaf = NULL; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 890 | int ret; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 891 | int nr = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 892 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 893 | path = btrfs_alloc_path(); |
| 894 | if (!path) |
| 895 | return -ENOMEM; |
| 896 | |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 897 | key.objectid = 0; |
| 898 | key.offset = 0; |
| 899 | key.type = 0; |
| 900 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 901 | while (1) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 902 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 903 | if (ret < 0) |
| 904 | goto out; |
| 905 | leaf = path->nodes[0]; |
| 906 | nr = btrfs_header_nritems(leaf); |
| 907 | if (!nr) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 908 | break; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 909 | /* |
| 910 | * delete the leaf one by one |
| 911 | * since the whole tree is going |
| 912 | * to be deleted. |
| 913 | */ |
| 914 | path->slots[0] = 0; |
| 915 | ret = btrfs_del_items(trans, root, path, 0, nr); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 916 | if (ret) |
| 917 | goto out; |
Wang Shilong | 06b3a86 | 2013-02-27 11:16:57 +0000 | [diff] [blame] | 918 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 919 | btrfs_release_path(path); |
| 920 | } |
| 921 | ret = 0; |
| 922 | out: |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 923 | btrfs_free_path(path); |
| 924 | return ret; |
| 925 | } |
| 926 | |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 927 | int btrfs_quota_enable(struct btrfs_fs_info *fs_info) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 928 | { |
| 929 | struct btrfs_root *quota_root; |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 930 | struct btrfs_root *tree_root = fs_info->tree_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 931 | struct btrfs_path *path = NULL; |
| 932 | struct btrfs_qgroup_status_item *ptr; |
| 933 | struct extent_buffer *leaf; |
| 934 | struct btrfs_key key; |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 935 | struct btrfs_key found_key; |
| 936 | struct btrfs_qgroup *qgroup = NULL; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 937 | struct btrfs_trans_handle *trans = NULL; |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 938 | struct ulist *ulist = NULL; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 939 | int ret = 0; |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 940 | int slot; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 941 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 942 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Nikolay Borisov | 5d23515 | 2018-01-31 10:52:04 +0200 | [diff] [blame] | 943 | if (fs_info->quota_root) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 944 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 945 | |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 946 | ulist = ulist_alloc(GFP_KERNEL); |
| 947 | if (!ulist) { |
David Sterba | 7503b83 | 2018-12-19 19:47:37 +0100 | [diff] [blame] | 948 | ret = -ENOMEM; |
| 949 | goto out; |
| 950 | } |
| 951 | |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 952 | ret = btrfs_sysfs_add_qgroups(fs_info); |
| 953 | if (ret < 0) |
| 954 | goto out; |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 955 | |
| 956 | /* |
| 957 | * Unlock qgroup_ioctl_lock before starting the transaction. This is to |
| 958 | * avoid lock acquisition inversion problems (reported by lockdep) between |
| 959 | * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we |
| 960 | * start a transaction. |
| 961 | * After we started the transaction lock qgroup_ioctl_lock again and |
| 962 | * check if someone else created the quota root in the meanwhile. If so, |
| 963 | * just return success and release the transaction handle. |
| 964 | * |
| 965 | * Also we don't need to worry about someone else calling |
| 966 | * btrfs_sysfs_add_qgroups() after we unlock and getting an error because |
| 967 | * that function returns 0 (success) when the sysfs entries already exist. |
| 968 | */ |
| 969 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
| 970 | |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 971 | /* |
| 972 | * 1 for quota root item |
| 973 | * 1 for BTRFS_QGROUP_STATUS item |
| 974 | * |
| 975 | * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items |
| 976 | * per subvolume. However those are not currently reserved since it |
| 977 | * would be a lot of overkill. |
| 978 | */ |
| 979 | trans = btrfs_start_transaction(tree_root, 2); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 980 | |
| 981 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 982 | if (IS_ERR(trans)) { |
| 983 | ret = PTR_ERR(trans); |
| 984 | trans = NULL; |
| 985 | goto out; |
| 986 | } |
| 987 | |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 988 | if (fs_info->quota_root) |
| 989 | goto out; |
| 990 | |
| 991 | fs_info->qgroup_ulist = ulist; |
| 992 | ulist = NULL; |
| 993 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 994 | /* |
| 995 | * initially create the quota tree |
| 996 | */ |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 997 | quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 998 | if (IS_ERR(quota_root)) { |
| 999 | ret = PTR_ERR(quota_root); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1000 | btrfs_abort_transaction(trans, ret); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1001 | goto out; |
| 1002 | } |
| 1003 | |
| 1004 | path = btrfs_alloc_path(); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 1005 | if (!path) { |
| 1006 | ret = -ENOMEM; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1007 | btrfs_abort_transaction(trans, ret); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 1008 | goto out_free_root; |
| 1009 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1010 | |
| 1011 | key.objectid = 0; |
| 1012 | key.type = BTRFS_QGROUP_STATUS_KEY; |
| 1013 | key.offset = 0; |
| 1014 | |
| 1015 | ret = btrfs_insert_empty_item(trans, quota_root, path, &key, |
| 1016 | sizeof(*ptr)); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1017 | if (ret) { |
| 1018 | btrfs_abort_transaction(trans, ret); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 1019 | goto out_free_path; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1020 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1021 | |
| 1022 | leaf = path->nodes[0]; |
| 1023 | ptr = btrfs_item_ptr(leaf, path->slots[0], |
| 1024 | struct btrfs_qgroup_status_item); |
| 1025 | btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid); |
| 1026 | btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION); |
| 1027 | fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON | |
| 1028 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 1029 | btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 1030 | btrfs_set_qgroup_status_rescan(leaf, ptr, 0); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1031 | |
| 1032 | btrfs_mark_buffer_dirty(leaf); |
| 1033 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1034 | key.objectid = 0; |
| 1035 | key.type = BTRFS_ROOT_REF_KEY; |
| 1036 | key.offset = 0; |
| 1037 | |
| 1038 | btrfs_release_path(path); |
| 1039 | ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0); |
| 1040 | if (ret > 0) |
| 1041 | goto out_add_root; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1042 | if (ret < 0) { |
| 1043 | btrfs_abort_transaction(trans, ret); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1044 | goto out_free_path; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1045 | } |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1046 | |
| 1047 | while (1) { |
| 1048 | slot = path->slots[0]; |
| 1049 | leaf = path->nodes[0]; |
| 1050 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 1051 | |
| 1052 | if (found_key.type == BTRFS_ROOT_REF_KEY) { |
Josef Bacik | 5223cc6 | 2020-10-19 16:02:29 -0400 | [diff] [blame] | 1053 | |
| 1054 | /* Release locks on tree_root before we access quota_root */ |
| 1055 | btrfs_release_path(path); |
| 1056 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1057 | ret = add_qgroup_item(trans, quota_root, |
| 1058 | found_key.offset); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1059 | if (ret) { |
| 1060 | btrfs_abort_transaction(trans, ret); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1061 | goto out_free_path; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1062 | } |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1063 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1064 | qgroup = add_qgroup_rb(fs_info, found_key.offset); |
| 1065 | if (IS_ERR(qgroup)) { |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1066 | ret = PTR_ERR(qgroup); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1067 | btrfs_abort_transaction(trans, ret); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1068 | goto out_free_path; |
| 1069 | } |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1070 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
| 1071 | if (ret < 0) { |
| 1072 | btrfs_abort_transaction(trans, ret); |
| 1073 | goto out_free_path; |
| 1074 | } |
Josef Bacik | 5223cc6 | 2020-10-19 16:02:29 -0400 | [diff] [blame] | 1075 | ret = btrfs_search_slot_for_read(tree_root, &found_key, |
| 1076 | path, 1, 0); |
| 1077 | if (ret < 0) { |
| 1078 | btrfs_abort_transaction(trans, ret); |
| 1079 | goto out_free_path; |
| 1080 | } |
| 1081 | if (ret > 0) { |
| 1082 | /* |
| 1083 | * Shouldn't happen, but in case it does we |
| 1084 | * don't need to do the btrfs_next_item, just |
| 1085 | * continue. |
| 1086 | */ |
| 1087 | continue; |
| 1088 | } |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1089 | } |
| 1090 | ret = btrfs_next_item(tree_root, path); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1091 | if (ret < 0) { |
| 1092 | btrfs_abort_transaction(trans, ret); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1093 | goto out_free_path; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1094 | } |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1095 | if (ret) |
| 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | out_add_root: |
| 1100 | btrfs_release_path(path); |
| 1101 | ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1102 | if (ret) { |
| 1103 | btrfs_abort_transaction(trans, ret); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1104 | goto out_free_path; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1105 | } |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1106 | |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1107 | qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID); |
| 1108 | if (IS_ERR(qgroup)) { |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1109 | ret = PTR_ERR(qgroup); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1110 | btrfs_abort_transaction(trans, ret); |
Wang Shilong | 7708f02 | 2013-04-07 10:24:57 +0000 | [diff] [blame] | 1111 | goto out_free_path; |
| 1112 | } |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1113 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
| 1114 | if (ret < 0) { |
| 1115 | btrfs_abort_transaction(trans, ret); |
| 1116 | goto out_free_path; |
| 1117 | } |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1118 | |
| 1119 | ret = btrfs_commit_transaction(trans); |
Dan Carpenter | b9b8a41 | 2018-08-20 11:25:33 +0300 | [diff] [blame] | 1120 | trans = NULL; |
| 1121 | if (ret) |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1122 | goto out_free_path; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1123 | |
Filipe Manana | 9a6f209 | 2018-11-19 14:15:36 +0000 | [diff] [blame] | 1124 | /* |
| 1125 | * Set quota enabled flag after committing the transaction, to avoid |
| 1126 | * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot |
| 1127 | * creation. |
| 1128 | */ |
| 1129 | spin_lock(&fs_info->qgroup_lock); |
| 1130 | fs_info->quota_root = quota_root; |
| 1131 | set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); |
| 1132 | spin_unlock(&fs_info->qgroup_lock); |
| 1133 | |
Nikolay Borisov | 5d23515 | 2018-01-31 10:52:04 +0200 | [diff] [blame] | 1134 | ret = qgroup_rescan_init(fs_info, 0, 1); |
| 1135 | if (!ret) { |
| 1136 | qgroup_rescan_zero_tracking(fs_info); |
Qu Wenruo | d61acbb | 2020-02-07 13:38:20 +0800 | [diff] [blame] | 1137 | fs_info->qgroup_rescan_running = true; |
Nikolay Borisov | 5d23515 | 2018-01-31 10:52:04 +0200 | [diff] [blame] | 1138 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
| 1139 | &fs_info->qgroup_rescan_work); |
| 1140 | } |
| 1141 | |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 1142 | out_free_path: |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1143 | btrfs_free_path(path); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 1144 | out_free_root: |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 1145 | if (ret) |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 1146 | btrfs_put_root(quota_root); |
Tsutomu Itoh | 5b7ff5b | 2012-10-16 05:44:21 +0000 | [diff] [blame] | 1147 | out: |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 1148 | if (ret) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 1149 | ulist_free(fs_info->qgroup_ulist); |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 1150 | fs_info->qgroup_ulist = NULL; |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1151 | btrfs_sysfs_del_qgroups(fs_info); |
Jan Schmidt | eb1716a | 2013-05-28 15:47:23 +0000 | [diff] [blame] | 1152 | } |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1153 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1154 | if (ret && trans) |
| 1155 | btrfs_end_transaction(trans); |
| 1156 | else if (trans) |
| 1157 | ret = btrfs_end_transaction(trans); |
| 1158 | ulist_free(ulist); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1159 | return ret; |
| 1160 | } |
| 1161 | |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1162 | int btrfs_quota_disable(struct btrfs_fs_info *fs_info) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1163 | { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1164 | struct btrfs_root *quota_root; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1165 | struct btrfs_trans_handle *trans = NULL; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1166 | int ret = 0; |
| 1167 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1168 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 1169 | if (!fs_info->quota_root) |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1170 | goto out; |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1171 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * 1 For the root item |
| 1175 | * |
| 1176 | * We should also reserve enough items for the quota tree deletion in |
| 1177 | * btrfs_clean_quota_tree but this is not done. |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1178 | * |
| 1179 | * Also, we must always start a transaction without holding the mutex |
| 1180 | * qgroup_ioctl_lock, see btrfs_quota_enable(). |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1181 | */ |
| 1182 | trans = btrfs_start_transaction(fs_info->tree_root, 1); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1183 | |
| 1184 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1185 | if (IS_ERR(trans)) { |
| 1186 | ret = PTR_ERR(trans); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1187 | trans = NULL; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1188 | goto out; |
| 1189 | } |
| 1190 | |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1191 | if (!fs_info->quota_root) |
| 1192 | goto out; |
| 1193 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 1194 | clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags); |
Jeff Mahoney | d06f23d | 2016-08-08 22:08:06 -0400 | [diff] [blame] | 1195 | btrfs_qgroup_wait_for_completion(fs_info, false); |
Justin Maggard | 967ef51 | 2015-11-06 10:36:42 -0800 | [diff] [blame] | 1196 | spin_lock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1197 | quota_root = fs_info->quota_root; |
| 1198 | fs_info->quota_root = NULL; |
Dongsheng Yang | 8ea0ec9 | 2015-02-27 16:24:26 +0800 | [diff] [blame] | 1199 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1200 | spin_unlock(&fs_info->qgroup_lock); |
| 1201 | |
Wang Shilong | e685da1 | 2013-08-14 09:13:37 +0800 | [diff] [blame] | 1202 | btrfs_free_qgroup_config(fs_info); |
| 1203 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1204 | ret = btrfs_clean_quota_tree(trans, quota_root); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1205 | if (ret) { |
| 1206 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1207 | goto out; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1208 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1209 | |
Lu Fengqi | ab9ce7d | 2018-08-01 11:32:27 +0800 | [diff] [blame] | 1210 | ret = btrfs_del_root(trans, "a_root->root_key); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1211 | if (ret) { |
| 1212 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1213 | goto out; |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1214 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1215 | |
| 1216 | list_del("a_root->dirty_list); |
| 1217 | |
| 1218 | btrfs_tree_lock(quota_root->node); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1219 | btrfs_clean_tree_block(quota_root->node); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1220 | btrfs_tree_unlock(quota_root->node); |
| 1221 | btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1); |
| 1222 | |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 1223 | btrfs_put_root(quota_root); |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 1224 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1225 | out: |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1226 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Filipe Manana | a855fbe | 2020-11-23 18:31:02 +0000 | [diff] [blame] | 1227 | if (ret && trans) |
| 1228 | btrfs_end_transaction(trans); |
| 1229 | else if (trans) |
| 1230 | ret = btrfs_end_transaction(trans); |
| 1231 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1232 | return ret; |
| 1233 | } |
| 1234 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 1235 | static void qgroup_dirty(struct btrfs_fs_info *fs_info, |
| 1236 | struct btrfs_qgroup *qgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1237 | { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 1238 | if (list_empty(&qgroup->dirty)) |
| 1239 | list_add(&qgroup->dirty, &fs_info->dirty_qgroups); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1242 | /* |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1243 | * The easy accounting, we're updating qgroup relationship whose child qgroup |
| 1244 | * only has exclusive extents. |
| 1245 | * |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1246 | * In this case, all exclusive extents will also be exclusive for parent, so |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1247 | * excl/rfer just get added/removed. |
| 1248 | * |
| 1249 | * So is qgroup reservation space, which should also be added/removed to |
| 1250 | * parent. |
| 1251 | * Or when child tries to release reservation space, parent will underflow its |
| 1252 | * reservation (for relationship adding case). |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1253 | * |
| 1254 | * Caller should hold fs_info->qgroup_lock. |
| 1255 | */ |
| 1256 | static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info, |
| 1257 | struct ulist *tmp, u64 ref_root, |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1258 | struct btrfs_qgroup *src, int sign) |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1259 | { |
| 1260 | struct btrfs_qgroup *qgroup; |
| 1261 | struct btrfs_qgroup_list *glist; |
| 1262 | struct ulist_node *unode; |
| 1263 | struct ulist_iterator uiter; |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1264 | u64 num_bytes = src->excl; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1265 | int ret = 0; |
| 1266 | |
| 1267 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 1268 | if (!qgroup) |
| 1269 | goto out; |
| 1270 | |
| 1271 | qgroup->rfer += sign * num_bytes; |
| 1272 | qgroup->rfer_cmpr += sign * num_bytes; |
| 1273 | |
| 1274 | WARN_ON(sign < 0 && qgroup->excl < num_bytes); |
| 1275 | qgroup->excl += sign * num_bytes; |
| 1276 | qgroup->excl_cmpr += sign * num_bytes; |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1277 | |
| 1278 | if (sign > 0) |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 1279 | qgroup_rsv_add_by_qgroup(fs_info, qgroup, src); |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1280 | else |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 1281 | qgroup_rsv_release_by_qgroup(fs_info, qgroup, src); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1282 | |
| 1283 | qgroup_dirty(fs_info, qgroup); |
| 1284 | |
| 1285 | /* Get all of the parent groups that contain this qgroup */ |
| 1286 | list_for_each_entry(glist, &qgroup->groups, next_group) { |
| 1287 | ret = ulist_add(tmp, glist->group->qgroupid, |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 1288 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1289 | if (ret < 0) |
| 1290 | goto out; |
| 1291 | } |
| 1292 | |
| 1293 | /* Iterate all of the parents and adjust their reference counts */ |
| 1294 | ULIST_ITER_INIT(&uiter); |
| 1295 | while ((unode = ulist_next(tmp, &uiter))) { |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 1296 | qgroup = unode_aux_to_qgroup(unode); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1297 | qgroup->rfer += sign * num_bytes; |
| 1298 | qgroup->rfer_cmpr += sign * num_bytes; |
| 1299 | WARN_ON(sign < 0 && qgroup->excl < num_bytes); |
| 1300 | qgroup->excl += sign * num_bytes; |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1301 | if (sign > 0) |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 1302 | qgroup_rsv_add_by_qgroup(fs_info, qgroup, src); |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1303 | else |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 1304 | qgroup_rsv_release_by_qgroup(fs_info, qgroup, src); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1305 | qgroup->excl_cmpr += sign * num_bytes; |
| 1306 | qgroup_dirty(fs_info, qgroup); |
| 1307 | |
| 1308 | /* Add any parents of the parents */ |
| 1309 | list_for_each_entry(glist, &qgroup->groups, next_group) { |
| 1310 | ret = ulist_add(tmp, glist->group->qgroupid, |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 1311 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1312 | if (ret < 0) |
| 1313 | goto out; |
| 1314 | } |
| 1315 | } |
| 1316 | ret = 0; |
| 1317 | out: |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | /* |
| 1323 | * Quick path for updating qgroup with only excl refs. |
| 1324 | * |
| 1325 | * In that case, just update all parent will be enough. |
| 1326 | * Or we needs to do a full rescan. |
| 1327 | * Caller should also hold fs_info->qgroup_lock. |
| 1328 | * |
| 1329 | * Return 0 for quick update, return >0 for need to full rescan |
| 1330 | * and mark INCONSISTENT flag. |
| 1331 | * Return < 0 for other error. |
| 1332 | */ |
| 1333 | static int quick_update_accounting(struct btrfs_fs_info *fs_info, |
| 1334 | struct ulist *tmp, u64 src, u64 dst, |
| 1335 | int sign) |
| 1336 | { |
| 1337 | struct btrfs_qgroup *qgroup; |
| 1338 | int ret = 1; |
| 1339 | int err = 0; |
| 1340 | |
| 1341 | qgroup = find_qgroup_rb(fs_info, src); |
| 1342 | if (!qgroup) |
| 1343 | goto out; |
| 1344 | if (qgroup->excl == qgroup->rfer) { |
| 1345 | ret = 0; |
| 1346 | err = __qgroup_excl_accounting(fs_info, tmp, dst, |
Qu Wenruo | 429d627 | 2017-12-12 15:34:26 +0800 | [diff] [blame] | 1347 | qgroup, sign); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1348 | if (err < 0) { |
| 1349 | ret = err; |
| 1350 | goto out; |
| 1351 | } |
| 1352 | } |
| 1353 | out: |
| 1354 | if (ret) |
| 1355 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 1356 | return ret; |
| 1357 | } |
| 1358 | |
Lu Fengqi | 9f8a6ce | 2018-07-18 14:45:30 +0800 | [diff] [blame] | 1359 | int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, |
| 1360 | u64 dst) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1361 | { |
Lu Fengqi | 9f8a6ce | 2018-07-18 14:45:30 +0800 | [diff] [blame] | 1362 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Wang Shilong | b7fef4f | 2013-04-07 10:50:18 +0000 | [diff] [blame] | 1363 | struct btrfs_qgroup *parent; |
| 1364 | struct btrfs_qgroup *member; |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1365 | struct btrfs_qgroup_list *list; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1366 | struct ulist *tmp; |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 1367 | unsigned int nofs_flag; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1368 | int ret = 0; |
| 1369 | |
Qu Wenruo | 8465ece | 2015-02-27 16:24:22 +0800 | [diff] [blame] | 1370 | /* Check the level of src and dst first */ |
| 1371 | if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst)) |
| 1372 | return -EINVAL; |
| 1373 | |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 1374 | /* We hold a transaction handle open, must do a NOFS allocation. */ |
| 1375 | nofs_flag = memalloc_nofs_save(); |
David Sterba | 6602caf | 2017-02-13 12:41:02 +0100 | [diff] [blame] | 1376 | tmp = ulist_alloc(GFP_KERNEL); |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 1377 | memalloc_nofs_restore(nofs_flag); |
Christian Engelmayer | ab3680d | 2015-05-02 17:19:55 +0200 | [diff] [blame] | 1378 | if (!tmp) |
| 1379 | return -ENOMEM; |
| 1380 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1381 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 1382 | if (!fs_info->quota_root) { |
Marcos Paulo de Souza | 8a36e40 | 2019-11-25 21:58:51 -0300 | [diff] [blame] | 1383 | ret = -ENOTCONN; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1384 | goto out; |
| 1385 | } |
Wang Shilong | b7fef4f | 2013-04-07 10:50:18 +0000 | [diff] [blame] | 1386 | member = find_qgroup_rb(fs_info, src); |
| 1387 | parent = find_qgroup_rb(fs_info, dst); |
| 1388 | if (!member || !parent) { |
| 1389 | ret = -EINVAL; |
| 1390 | goto out; |
| 1391 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1392 | |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1393 | /* check if such qgroup relation exist firstly */ |
| 1394 | list_for_each_entry(list, &member->groups, next_group) { |
| 1395 | if (list->group == parent) { |
| 1396 | ret = -EEXIST; |
| 1397 | goto out; |
| 1398 | } |
| 1399 | } |
| 1400 | |
Lu Fengqi | 711169c | 2018-07-18 14:45:24 +0800 | [diff] [blame] | 1401 | ret = add_qgroup_relation_item(trans, src, dst); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1402 | if (ret) |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1403 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1404 | |
Lu Fengqi | 711169c | 2018-07-18 14:45:24 +0800 | [diff] [blame] | 1405 | ret = add_qgroup_relation_item(trans, dst, src); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1406 | if (ret) { |
Lu Fengqi | 99d7f09 | 2018-07-18 14:45:25 +0800 | [diff] [blame] | 1407 | del_qgroup_relation_item(trans, src, dst); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1408 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | spin_lock(&fs_info->qgroup_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1412 | ret = add_relation_rb(fs_info, src, dst); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1413 | if (ret < 0) { |
| 1414 | spin_unlock(&fs_info->qgroup_lock); |
| 1415 | goto out; |
| 1416 | } |
| 1417 | ret = quick_update_accounting(fs_info, tmp, src, dst, 1); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1418 | spin_unlock(&fs_info->qgroup_lock); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1419 | out: |
| 1420 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1421 | ulist_free(tmp); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1422 | return ret; |
| 1423 | } |
| 1424 | |
Lu Fengqi | 6b36f1a | 2018-07-18 14:45:31 +0800 | [diff] [blame] | 1425 | static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, |
| 1426 | u64 dst) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1427 | { |
Lu Fengqi | 6b36f1a | 2018-07-18 14:45:31 +0800 | [diff] [blame] | 1428 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1429 | struct btrfs_qgroup *parent; |
| 1430 | struct btrfs_qgroup *member; |
| 1431 | struct btrfs_qgroup_list *list; |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1432 | struct ulist *tmp; |
Qu Wenruo | 73798c4 | 2019-08-06 22:05:07 +0800 | [diff] [blame] | 1433 | bool found = false; |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 1434 | unsigned int nofs_flag; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1435 | int ret = 0; |
Qu Wenruo | 73798c4 | 2019-08-06 22:05:07 +0800 | [diff] [blame] | 1436 | int ret2; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1437 | |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 1438 | /* We hold a transaction handle open, must do a NOFS allocation. */ |
| 1439 | nofs_flag = memalloc_nofs_save(); |
David Sterba | 6602caf | 2017-02-13 12:41:02 +0100 | [diff] [blame] | 1440 | tmp = ulist_alloc(GFP_KERNEL); |
Filipe Manana | 7aa6d35 | 2020-11-23 18:30:54 +0000 | [diff] [blame] | 1441 | memalloc_nofs_restore(nofs_flag); |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1442 | if (!tmp) |
| 1443 | return -ENOMEM; |
| 1444 | |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 1445 | if (!fs_info->quota_root) { |
Marcos Paulo de Souza | 8a36e40 | 2019-11-25 21:58:51 -0300 | [diff] [blame] | 1446 | ret = -ENOTCONN; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1447 | goto out; |
| 1448 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1449 | |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1450 | member = find_qgroup_rb(fs_info, src); |
| 1451 | parent = find_qgroup_rb(fs_info, dst); |
Qu Wenruo | 73798c4 | 2019-08-06 22:05:07 +0800 | [diff] [blame] | 1452 | /* |
| 1453 | * The parent/member pair doesn't exist, then try to delete the dead |
| 1454 | * relation items only. |
| 1455 | */ |
| 1456 | if (!member || !parent) |
| 1457 | goto delete_item; |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1458 | |
| 1459 | /* check if such qgroup relation exist firstly */ |
| 1460 | list_for_each_entry(list, &member->groups, next_group) { |
Qu Wenruo | 73798c4 | 2019-08-06 22:05:07 +0800 | [diff] [blame] | 1461 | if (list->group == parent) { |
| 1462 | found = true; |
| 1463 | break; |
| 1464 | } |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1465 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1466 | |
Qu Wenruo | 73798c4 | 2019-08-06 22:05:07 +0800 | [diff] [blame] | 1467 | delete_item: |
| 1468 | ret = del_qgroup_relation_item(trans, src, dst); |
| 1469 | if (ret < 0 && ret != -ENOENT) |
| 1470 | goto out; |
| 1471 | ret2 = del_qgroup_relation_item(trans, dst, src); |
| 1472 | if (ret2 < 0 && ret2 != -ENOENT) |
| 1473 | goto out; |
| 1474 | |
| 1475 | /* At least one deletion succeeded, return 0 */ |
| 1476 | if (!ret || !ret2) |
| 1477 | ret = 0; |
| 1478 | |
| 1479 | if (found) { |
| 1480 | spin_lock(&fs_info->qgroup_lock); |
| 1481 | del_relation_rb(fs_info, src, dst); |
| 1482 | ret = quick_update_accounting(fs_info, tmp, src, dst, -1); |
| 1483 | spin_unlock(&fs_info->qgroup_lock); |
| 1484 | } |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1485 | out: |
Qu Wenruo | 9c8b35b | 2015-02-27 16:24:27 +0800 | [diff] [blame] | 1486 | ulist_free(tmp); |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1487 | return ret; |
| 1488 | } |
| 1489 | |
Lu Fengqi | 39616c2 | 2018-07-18 14:45:32 +0800 | [diff] [blame] | 1490 | int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src, |
| 1491 | u64 dst) |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1492 | { |
Lu Fengqi | 39616c2 | 2018-07-18 14:45:32 +0800 | [diff] [blame] | 1493 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1494 | int ret = 0; |
| 1495 | |
| 1496 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Lu Fengqi | 6b36f1a | 2018-07-18 14:45:31 +0800 | [diff] [blame] | 1497 | ret = __del_qgroup_relation(trans, src, dst); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1498 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1499 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1500 | return ret; |
| 1501 | } |
| 1502 | |
Lu Fengqi | 49a05ec | 2018-07-18 14:45:33 +0800 | [diff] [blame] | 1503 | int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1504 | { |
Lu Fengqi | 49a05ec | 2018-07-18 14:45:33 +0800 | [diff] [blame] | 1505 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1506 | struct btrfs_root *quota_root; |
| 1507 | struct btrfs_qgroup *qgroup; |
| 1508 | int ret = 0; |
| 1509 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1510 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 1511 | if (!fs_info->quota_root) { |
Marcos Paulo de Souza | 8a36e40 | 2019-11-25 21:58:51 -0300 | [diff] [blame] | 1512 | ret = -ENOTCONN; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1513 | goto out; |
| 1514 | } |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 1515 | quota_root = fs_info->quota_root; |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1516 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 1517 | if (qgroup) { |
| 1518 | ret = -EEXIST; |
| 1519 | goto out; |
| 1520 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1521 | |
| 1522 | ret = add_qgroup_item(trans, quota_root, qgroupid); |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1523 | if (ret) |
| 1524 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1525 | |
| 1526 | spin_lock(&fs_info->qgroup_lock); |
| 1527 | qgroup = add_qgroup_rb(fs_info, qgroupid); |
| 1528 | spin_unlock(&fs_info->qgroup_lock); |
| 1529 | |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1530 | if (IS_ERR(qgroup)) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1531 | ret = PTR_ERR(qgroup); |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1532 | goto out; |
| 1533 | } |
| 1534 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1535 | out: |
| 1536 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1537 | return ret; |
| 1538 | } |
| 1539 | |
Lu Fengqi | 3efbee1 | 2018-07-18 14:45:34 +0800 | [diff] [blame] | 1540 | int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1541 | { |
Lu Fengqi | 3efbee1 | 2018-07-18 14:45:34 +0800 | [diff] [blame] | 1542 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Arne Jansen | 2cf6870 | 2013-01-17 01:22:09 -0700 | [diff] [blame] | 1543 | struct btrfs_qgroup *qgroup; |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1544 | struct btrfs_qgroup_list *list; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1545 | int ret = 0; |
| 1546 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1547 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 1548 | if (!fs_info->quota_root) { |
Marcos Paulo de Souza | 8a36e40 | 2019-11-25 21:58:51 -0300 | [diff] [blame] | 1549 | ret = -ENOTCONN; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1550 | goto out; |
| 1551 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1552 | |
Arne Jansen | 2cf6870 | 2013-01-17 01:22:09 -0700 | [diff] [blame] | 1553 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
Wang Shilong | 534e662 | 2013-04-17 14:49:51 +0000 | [diff] [blame] | 1554 | if (!qgroup) { |
| 1555 | ret = -ENOENT; |
| 1556 | goto out; |
Arne Jansen | 2cf6870 | 2013-01-17 01:22:09 -0700 | [diff] [blame] | 1557 | } |
Lu Fengqi | b90e22b | 2018-10-11 13:42:56 +0800 | [diff] [blame] | 1558 | |
| 1559 | /* Check if there are no children of this qgroup */ |
| 1560 | if (!list_empty(&qgroup->members)) { |
| 1561 | ret = -EBUSY; |
| 1562 | goto out; |
| 1563 | } |
| 1564 | |
Lu Fengqi | 6910461 | 2018-07-18 14:45:26 +0800 | [diff] [blame] | 1565 | ret = del_qgroup_item(trans, qgroupid); |
Sargun Dhillon | 36b96fd | 2017-09-17 09:02:29 +0000 | [diff] [blame] | 1566 | if (ret && ret != -ENOENT) |
| 1567 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1568 | |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1569 | while (!list_empty(&qgroup->groups)) { |
| 1570 | list = list_first_entry(&qgroup->groups, |
| 1571 | struct btrfs_qgroup_list, next_group); |
Lu Fengqi | 6b36f1a | 2018-07-18 14:45:31 +0800 | [diff] [blame] | 1572 | ret = __del_qgroup_relation(trans, qgroupid, |
| 1573 | list->group->qgroupid); |
Dongsheng Yang | f5a6b1c | 2014-11-24 10:27:09 -0500 | [diff] [blame] | 1574 | if (ret) |
| 1575 | goto out; |
| 1576 | } |
| 1577 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1578 | spin_lock(&fs_info->qgroup_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1579 | del_qgroup_rb(fs_info, qgroupid); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1580 | spin_unlock(&fs_info->qgroup_lock); |
Filipe Manana | 0bb7883 | 2021-03-18 11:22:05 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* |
| 1583 | * Remove the qgroup from sysfs now without holding the qgroup_lock |
| 1584 | * spinlock, since the sysfs_remove_group() function needs to take |
| 1585 | * the mutex kernfs_mutex through kernfs_remove_by_name_ns(). |
| 1586 | */ |
| 1587 | btrfs_sysfs_del_one_qgroup(fs_info, qgroup); |
| 1588 | kfree(qgroup); |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1589 | out: |
| 1590 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1591 | return ret; |
| 1592 | } |
| 1593 | |
Lu Fengqi | f0042d5 | 2018-07-18 14:45:35 +0800 | [diff] [blame] | 1594 | int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid, |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1595 | struct btrfs_qgroup_limit *limit) |
| 1596 | { |
Lu Fengqi | f0042d5 | 2018-07-18 14:45:35 +0800 | [diff] [blame] | 1597 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1598 | struct btrfs_qgroup *qgroup; |
| 1599 | int ret = 0; |
Yang Dongsheng | fe75990 | 2015-06-03 14:57:32 +0800 | [diff] [blame] | 1600 | /* Sometimes we would want to clear the limit on this qgroup. |
| 1601 | * To meet this requirement, we treat the -1 as a special value |
| 1602 | * which tell kernel to clear the limit on this qgroup. |
| 1603 | */ |
| 1604 | const u64 CLEAR_VALUE = -1; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1605 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1606 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 1607 | if (!fs_info->quota_root) { |
Marcos Paulo de Souza | 8a36e40 | 2019-11-25 21:58:51 -0300 | [diff] [blame] | 1608 | ret = -ENOTCONN; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1609 | goto out; |
| 1610 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1611 | |
Wang Shilong | ddb47af | 2013-04-07 10:50:20 +0000 | [diff] [blame] | 1612 | qgroup = find_qgroup_rb(fs_info, qgroupid); |
| 1613 | if (!qgroup) { |
| 1614 | ret = -ENOENT; |
| 1615 | goto out; |
| 1616 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1617 | |
Wang Shilong | 58400fc | 2013-04-07 10:50:17 +0000 | [diff] [blame] | 1618 | spin_lock(&fs_info->qgroup_lock); |
Yang Dongsheng | fe75990 | 2015-06-03 14:57:32 +0800 | [diff] [blame] | 1619 | if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) { |
| 1620 | if (limit->max_rfer == CLEAR_VALUE) { |
| 1621 | qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER; |
| 1622 | limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER; |
| 1623 | qgroup->max_rfer = 0; |
| 1624 | } else { |
| 1625 | qgroup->max_rfer = limit->max_rfer; |
| 1626 | } |
| 1627 | } |
| 1628 | if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) { |
| 1629 | if (limit->max_excl == CLEAR_VALUE) { |
| 1630 | qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL; |
| 1631 | limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL; |
| 1632 | qgroup->max_excl = 0; |
| 1633 | } else { |
| 1634 | qgroup->max_excl = limit->max_excl; |
| 1635 | } |
| 1636 | } |
| 1637 | if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) { |
| 1638 | if (limit->rsv_rfer == CLEAR_VALUE) { |
| 1639 | qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER; |
| 1640 | limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER; |
| 1641 | qgroup->rsv_rfer = 0; |
| 1642 | } else { |
| 1643 | qgroup->rsv_rfer = limit->rsv_rfer; |
| 1644 | } |
| 1645 | } |
| 1646 | if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) { |
| 1647 | if (limit->rsv_excl == CLEAR_VALUE) { |
| 1648 | qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL; |
| 1649 | limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL; |
| 1650 | qgroup->rsv_excl = 0; |
| 1651 | } else { |
| 1652 | qgroup->rsv_excl = limit->rsv_excl; |
| 1653 | } |
| 1654 | } |
Dongsheng Yang | 03477d9 | 2015-02-06 11:06:25 -0500 | [diff] [blame] | 1655 | qgroup->lim_flags |= limit->flags; |
| 1656 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1657 | spin_unlock(&fs_info->qgroup_lock); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 1658 | |
Lu Fengqi | ac8a866 | 2018-07-18 14:45:27 +0800 | [diff] [blame] | 1659 | ret = update_qgroup_limit_item(trans, qgroup); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 1660 | if (ret) { |
| 1661 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 1662 | btrfs_info(fs_info, "unable to update quota limit for %llu", |
| 1663 | qgroupid); |
| 1664 | } |
| 1665 | |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 1666 | out: |
| 1667 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 1668 | return ret; |
| 1669 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 1670 | |
Qu Wenruo | 50b3e04 | 2016-10-18 09:31:27 +0800 | [diff] [blame] | 1671 | int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info, |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1672 | struct btrfs_delayed_ref_root *delayed_refs, |
| 1673 | struct btrfs_qgroup_extent_record *record) |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1674 | { |
| 1675 | struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node; |
| 1676 | struct rb_node *parent_node = NULL; |
| 1677 | struct btrfs_qgroup_extent_record *entry; |
| 1678 | u64 bytenr = record->bytenr; |
| 1679 | |
David Sterba | a4666e6 | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 1680 | lockdep_assert_held(&delayed_refs->lock); |
Qu Wenruo | 50b3e04 | 2016-10-18 09:31:27 +0800 | [diff] [blame] | 1681 | trace_btrfs_qgroup_trace_extent(fs_info, record); |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 1682 | |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1683 | while (*p) { |
| 1684 | parent_node = *p; |
| 1685 | entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record, |
| 1686 | node); |
Qu Wenruo | 1418bae | 2019-01-23 15:15:12 +0800 | [diff] [blame] | 1687 | if (bytenr < entry->bytenr) { |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1688 | p = &(*p)->rb_left; |
Qu Wenruo | 1418bae | 2019-01-23 15:15:12 +0800 | [diff] [blame] | 1689 | } else if (bytenr > entry->bytenr) { |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1690 | p = &(*p)->rb_right; |
Qu Wenruo | 1418bae | 2019-01-23 15:15:12 +0800 | [diff] [blame] | 1691 | } else { |
| 1692 | if (record->data_rsv && !entry->data_rsv) { |
| 1693 | entry->data_rsv = record->data_rsv; |
| 1694 | entry->data_rsv_refroot = |
| 1695 | record->data_rsv_refroot; |
| 1696 | } |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1697 | return 1; |
Qu Wenruo | 1418bae | 2019-01-23 15:15:12 +0800 | [diff] [blame] | 1698 | } |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | rb_link_node(&record->node, parent_node, p); |
| 1702 | rb_insert_color(&record->node, &delayed_refs->dirty_extent_root); |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1703 | return 0; |
| 1704 | } |
| 1705 | |
Qu Wenruo | fb235dc | 2017-02-15 10:43:03 +0800 | [diff] [blame] | 1706 | int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info, |
| 1707 | struct btrfs_qgroup_extent_record *qrecord) |
| 1708 | { |
| 1709 | struct ulist *old_root; |
| 1710 | u64 bytenr = qrecord->bytenr; |
| 1711 | int ret; |
| 1712 | |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 1713 | ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false); |
Nikolay Borisov | 952bd3db | 2018-01-29 15:53:01 +0200 | [diff] [blame] | 1714 | if (ret < 0) { |
| 1715 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 1716 | btrfs_warn(fs_info, |
| 1717 | "error accounting new delayed refs extent (err code: %d), quota inconsistent", |
| 1718 | ret); |
| 1719 | return 0; |
| 1720 | } |
Qu Wenruo | fb235dc | 2017-02-15 10:43:03 +0800 | [diff] [blame] | 1721 | |
| 1722 | /* |
| 1723 | * Here we don't need to get the lock of |
| 1724 | * trans->transaction->delayed_refs, since inserted qrecord won't |
| 1725 | * be deleted, only qrecord->node may be modified (new qrecord insert) |
| 1726 | * |
| 1727 | * So modifying qrecord->old_roots is safe here |
| 1728 | */ |
| 1729 | qrecord->old_roots = old_root; |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 1733 | int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr, |
| 1734 | u64 num_bytes, gfp_t gfp_flag) |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1735 | { |
Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 1736 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1737 | struct btrfs_qgroup_extent_record *record; |
| 1738 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1739 | int ret; |
| 1740 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 1741 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) |
| 1742 | || bytenr == 0 || num_bytes == 0) |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1743 | return 0; |
Qu Wenruo | 1418bae | 2019-01-23 15:15:12 +0800 | [diff] [blame] | 1744 | record = kzalloc(sizeof(*record), gfp_flag); |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1745 | if (!record) |
| 1746 | return -ENOMEM; |
| 1747 | |
| 1748 | delayed_refs = &trans->transaction->delayed_refs; |
| 1749 | record->bytenr = bytenr; |
| 1750 | record->num_bytes = num_bytes; |
| 1751 | record->old_roots = NULL; |
| 1752 | |
| 1753 | spin_lock(&delayed_refs->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1754 | ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record); |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1755 | spin_unlock(&delayed_refs->lock); |
Qu Wenruo | fb235dc | 2017-02-15 10:43:03 +0800 | [diff] [blame] | 1756 | if (ret > 0) { |
Qu Wenruo | cb93b52 | 2016-08-15 10:36:50 +0800 | [diff] [blame] | 1757 | kfree(record); |
Qu Wenruo | fb235dc | 2017-02-15 10:43:03 +0800 | [diff] [blame] | 1758 | return 0; |
| 1759 | } |
| 1760 | return btrfs_qgroup_trace_extent_post(fs_info, record); |
Qu Wenruo | 3368d00 | 2015-04-16 14:34:17 +0800 | [diff] [blame] | 1761 | } |
| 1762 | |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1763 | int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1764 | struct extent_buffer *eb) |
| 1765 | { |
Lu Fengqi | 8d38d7e | 2018-07-18 14:45:37 +0800 | [diff] [blame] | 1766 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1767 | int nr = btrfs_header_nritems(eb); |
| 1768 | int i, extent_type, ret; |
| 1769 | struct btrfs_key key; |
| 1770 | struct btrfs_file_extent_item *fi; |
| 1771 | u64 bytenr, num_bytes; |
| 1772 | |
| 1773 | /* We can be called directly from walk_up_proc() */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1774 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1775 | return 0; |
| 1776 | |
| 1777 | for (i = 0; i < nr; i++) { |
| 1778 | btrfs_item_key_to_cpu(eb, &key, i); |
| 1779 | |
| 1780 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 1781 | continue; |
| 1782 | |
| 1783 | fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 1784 | /* filter out non qgroup-accountable extents */ |
| 1785 | extent_type = btrfs_file_extent_type(eb, fi); |
| 1786 | |
| 1787 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) |
| 1788 | continue; |
| 1789 | |
| 1790 | bytenr = btrfs_file_extent_disk_bytenr(eb, fi); |
| 1791 | if (!bytenr) |
| 1792 | continue; |
| 1793 | |
| 1794 | num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi); |
| 1795 | |
Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 1796 | ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes, |
| 1797 | GFP_NOFS); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1798 | if (ret) |
| 1799 | return ret; |
| 1800 | } |
Jeff Mahoney | cddf3b2 | 2017-06-20 08:15:26 -0400 | [diff] [blame] | 1801 | cond_resched(); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | /* |
| 1806 | * Walk up the tree from the bottom, freeing leaves and any interior |
| 1807 | * nodes which have had all slots visited. If a node (leaf or |
| 1808 | * interior) is freed, the node above it will have it's slot |
| 1809 | * incremented. The root node will never be freed. |
| 1810 | * |
| 1811 | * At the end of this function, we should have a path which has all |
| 1812 | * slots incremented to the next position for a search. If we need to |
| 1813 | * read a new node it will be NULL and the node above it will have the |
| 1814 | * correct slot selected for a later read. |
| 1815 | * |
| 1816 | * If we increment the root nodes slot counter past the number of |
| 1817 | * elements, 1 is returned to signal completion of the search. |
| 1818 | */ |
David Sterba | 15b3451 | 2017-02-10 20:30:23 +0100 | [diff] [blame] | 1819 | static int adjust_slots_upwards(struct btrfs_path *path, int root_level) |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 1820 | { |
| 1821 | int level = 0; |
| 1822 | int nr, slot; |
| 1823 | struct extent_buffer *eb; |
| 1824 | |
| 1825 | if (root_level == 0) |
| 1826 | return 1; |
| 1827 | |
| 1828 | while (level <= root_level) { |
| 1829 | eb = path->nodes[level]; |
| 1830 | nr = btrfs_header_nritems(eb); |
| 1831 | path->slots[level]++; |
| 1832 | slot = path->slots[level]; |
| 1833 | if (slot >= nr || level == 0) { |
| 1834 | /* |
| 1835 | * Don't free the root - we will detect this |
| 1836 | * condition after our loop and return a |
| 1837 | * positive value for caller to stop walking the tree. |
| 1838 | */ |
| 1839 | if (level != root_level) { |
| 1840 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
| 1841 | path->locks[level] = 0; |
| 1842 | |
| 1843 | free_extent_buffer(eb); |
| 1844 | path->nodes[level] = NULL; |
| 1845 | path->slots[level] = 0; |
| 1846 | } |
| 1847 | } else { |
| 1848 | /* |
| 1849 | * We have a valid slot to walk back down |
| 1850 | * from. Stop here so caller can process these |
| 1851 | * new nodes. |
| 1852 | */ |
| 1853 | break; |
| 1854 | } |
| 1855 | |
| 1856 | level++; |
| 1857 | } |
| 1858 | |
| 1859 | eb = path->nodes[root_level]; |
| 1860 | if (path->slots[root_level] >= btrfs_header_nritems(eb)) |
| 1861 | return 1; |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1866 | /* |
| 1867 | * Helper function to trace a subtree tree block swap. |
| 1868 | * |
| 1869 | * The swap will happen in highest tree block, but there may be a lot of |
| 1870 | * tree blocks involved. |
| 1871 | * |
| 1872 | * For example: |
| 1873 | * OO = Old tree blocks |
| 1874 | * NN = New tree blocks allocated during balance |
| 1875 | * |
| 1876 | * File tree (257) Reloc tree for 257 |
| 1877 | * L2 OO NN |
| 1878 | * / \ / \ |
| 1879 | * L1 OO OO (a) OO NN (a) |
| 1880 | * / \ / \ / \ / \ |
| 1881 | * L0 OO OO OO OO OO OO NN NN |
| 1882 | * (b) (c) (b) (c) |
| 1883 | * |
| 1884 | * When calling qgroup_trace_extent_swap(), we will pass: |
| 1885 | * @src_eb = OO(a) |
| 1886 | * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ] |
| 1887 | * @dst_level = 0 |
| 1888 | * @root_level = 1 |
| 1889 | * |
| 1890 | * In that case, qgroup_trace_extent_swap() will search from OO(a) to |
| 1891 | * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty. |
| 1892 | * |
| 1893 | * The main work of qgroup_trace_extent_swap() can be split into 3 parts: |
| 1894 | * |
| 1895 | * 1) Tree search from @src_eb |
| 1896 | * It should acts as a simplified btrfs_search_slot(). |
| 1897 | * The key for search can be extracted from @dst_path->nodes[dst_level] |
| 1898 | * (first key). |
| 1899 | * |
| 1900 | * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty |
| 1901 | * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty. |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1902 | * They should be marked during previous (@dst_level = 1) iteration. |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1903 | * |
| 1904 | * 3) Mark file extents in leaves dirty |
| 1905 | * We don't have good way to pick out new file extents only. |
| 1906 | * So we still follow the old method by scanning all file extents in |
| 1907 | * the leave. |
| 1908 | * |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1909 | * This function can free us from keeping two paths, thus later we only need |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1910 | * to care about how to iterate all new tree blocks in reloc tree. |
| 1911 | */ |
| 1912 | static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans, |
| 1913 | struct extent_buffer *src_eb, |
| 1914 | struct btrfs_path *dst_path, |
Qu Wenruo | 3d0174f | 2018-09-27 14:42:35 +0800 | [diff] [blame] | 1915 | int dst_level, int root_level, |
| 1916 | bool trace_leaf) |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1917 | { |
| 1918 | struct btrfs_key key; |
| 1919 | struct btrfs_path *src_path; |
| 1920 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1921 | u32 nodesize = fs_info->nodesize; |
| 1922 | int cur_level = root_level; |
| 1923 | int ret; |
| 1924 | |
| 1925 | BUG_ON(dst_level > root_level); |
| 1926 | /* Level mismatch */ |
| 1927 | if (btrfs_header_level(src_eb) != root_level) |
| 1928 | return -EINVAL; |
| 1929 | |
| 1930 | src_path = btrfs_alloc_path(); |
| 1931 | if (!src_path) { |
| 1932 | ret = -ENOMEM; |
| 1933 | goto out; |
| 1934 | } |
| 1935 | |
| 1936 | if (dst_level) |
| 1937 | btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0); |
| 1938 | else |
| 1939 | btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0); |
| 1940 | |
| 1941 | /* For src_path */ |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 1942 | atomic_inc(&src_eb->refs); |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1943 | src_path->nodes[root_level] = src_eb; |
| 1944 | src_path->slots[root_level] = dst_path->slots[root_level]; |
| 1945 | src_path->locks[root_level] = 0; |
| 1946 | |
| 1947 | /* A simplified version of btrfs_search_slot() */ |
| 1948 | while (cur_level >= dst_level) { |
| 1949 | struct btrfs_key src_key; |
| 1950 | struct btrfs_key dst_key; |
| 1951 | |
| 1952 | if (src_path->nodes[cur_level] == NULL) { |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1953 | struct extent_buffer *eb; |
| 1954 | int parent_slot; |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1955 | |
| 1956 | eb = src_path->nodes[cur_level + 1]; |
| 1957 | parent_slot = src_path->slots[cur_level + 1]; |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1958 | |
Josef Bacik | 6b2cb7c | 2020-11-05 10:45:15 -0500 | [diff] [blame] | 1959 | eb = btrfs_read_node_slot(eb, parent_slot); |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1960 | if (IS_ERR(eb)) { |
| 1961 | ret = PTR_ERR(eb); |
| 1962 | goto out; |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | src_path->nodes[cur_level] = eb; |
| 1966 | |
| 1967 | btrfs_tree_read_lock(eb); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 1968 | src_path->locks[cur_level] = BTRFS_READ_LOCK; |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | src_path->slots[cur_level] = dst_path->slots[cur_level]; |
| 1972 | if (cur_level) { |
| 1973 | btrfs_node_key_to_cpu(dst_path->nodes[cur_level], |
| 1974 | &dst_key, dst_path->slots[cur_level]); |
| 1975 | btrfs_node_key_to_cpu(src_path->nodes[cur_level], |
| 1976 | &src_key, src_path->slots[cur_level]); |
| 1977 | } else { |
| 1978 | btrfs_item_key_to_cpu(dst_path->nodes[cur_level], |
| 1979 | &dst_key, dst_path->slots[cur_level]); |
| 1980 | btrfs_item_key_to_cpu(src_path->nodes[cur_level], |
| 1981 | &src_key, src_path->slots[cur_level]); |
| 1982 | } |
| 1983 | /* Content mismatch, something went wrong */ |
| 1984 | if (btrfs_comp_cpu_keys(&dst_key, &src_key)) { |
| 1985 | ret = -ENOENT; |
| 1986 | goto out; |
| 1987 | } |
| 1988 | cur_level--; |
| 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * Now both @dst_path and @src_path have been populated, record the tree |
| 1993 | * blocks for qgroup accounting. |
| 1994 | */ |
| 1995 | ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start, |
| 1996 | nodesize, GFP_NOFS); |
| 1997 | if (ret < 0) |
| 1998 | goto out; |
| 1999 | ret = btrfs_qgroup_trace_extent(trans, |
| 2000 | dst_path->nodes[dst_level]->start, |
| 2001 | nodesize, GFP_NOFS); |
| 2002 | if (ret < 0) |
| 2003 | goto out; |
| 2004 | |
| 2005 | /* Record leaf file extents */ |
Qu Wenruo | 3d0174f | 2018-09-27 14:42:35 +0800 | [diff] [blame] | 2006 | if (dst_level == 0 && trace_leaf) { |
Qu Wenruo | 2598256 | 2018-09-27 14:42:30 +0800 | [diff] [blame] | 2007 | ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]); |
| 2008 | if (ret < 0) |
| 2009 | goto out; |
| 2010 | ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]); |
| 2011 | } |
| 2012 | out: |
| 2013 | btrfs_free_path(src_path); |
| 2014 | return ret; |
| 2015 | } |
| 2016 | |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2017 | /* |
| 2018 | * Helper function to do recursive generation-aware depth-first search, to |
| 2019 | * locate all new tree blocks in a subtree of reloc tree. |
| 2020 | * |
| 2021 | * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot) |
| 2022 | * reloc tree |
| 2023 | * L2 NN (a) |
| 2024 | * / \ |
| 2025 | * L1 OO NN (b) |
| 2026 | * / \ / \ |
| 2027 | * L0 OO OO OO NN |
| 2028 | * (c) (d) |
| 2029 | * If we pass: |
| 2030 | * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ], |
| 2031 | * @cur_level = 1 |
| 2032 | * @root_level = 1 |
| 2033 | * |
| 2034 | * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace |
| 2035 | * above tree blocks along with their counter parts in file tree. |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 2036 | * While during search, old tree blocks OO(c) will be skipped as tree block swap |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2037 | * won't affect OO(c). |
| 2038 | */ |
| 2039 | static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans, |
| 2040 | struct extent_buffer *src_eb, |
| 2041 | struct btrfs_path *dst_path, |
| 2042 | int cur_level, int root_level, |
Qu Wenruo | 3d0174f | 2018-09-27 14:42:35 +0800 | [diff] [blame] | 2043 | u64 last_snapshot, bool trace_leaf) |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2044 | { |
| 2045 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2046 | struct extent_buffer *eb; |
| 2047 | bool need_cleanup = false; |
| 2048 | int ret = 0; |
| 2049 | int i; |
| 2050 | |
| 2051 | /* Level sanity check */ |
Nikolay Borisov | 7ff2c2a | 2019-03-18 17:45:19 +0200 | [diff] [blame] | 2052 | if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 || |
| 2053 | root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 || |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2054 | root_level < cur_level) { |
| 2055 | btrfs_err_rl(fs_info, |
| 2056 | "%s: bad levels, cur_level=%d root_level=%d", |
| 2057 | __func__, cur_level, root_level); |
| 2058 | return -EUCLEAN; |
| 2059 | } |
| 2060 | |
| 2061 | /* Read the tree block if needed */ |
| 2062 | if (dst_path->nodes[cur_level] == NULL) { |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2063 | int parent_slot; |
| 2064 | u64 child_gen; |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2065 | |
| 2066 | /* |
| 2067 | * dst_path->nodes[root_level] must be initialized before |
| 2068 | * calling this function. |
| 2069 | */ |
| 2070 | if (cur_level == root_level) { |
| 2071 | btrfs_err_rl(fs_info, |
| 2072 | "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d", |
| 2073 | __func__, root_level, root_level, cur_level); |
| 2074 | return -EUCLEAN; |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * We need to get child blockptr/gen from parent before we can |
| 2079 | * read it. |
| 2080 | */ |
| 2081 | eb = dst_path->nodes[cur_level + 1]; |
| 2082 | parent_slot = dst_path->slots[cur_level + 1]; |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2083 | child_gen = btrfs_node_ptr_generation(eb, parent_slot); |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2084 | |
| 2085 | /* This node is old, no need to trace */ |
| 2086 | if (child_gen < last_snapshot) |
| 2087 | goto out; |
| 2088 | |
Josef Bacik | 3acfbd6 | 2020-11-05 10:45:16 -0500 | [diff] [blame] | 2089 | eb = btrfs_read_node_slot(eb, parent_slot); |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2090 | if (IS_ERR(eb)) { |
| 2091 | ret = PTR_ERR(eb); |
| 2092 | goto out; |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | dst_path->nodes[cur_level] = eb; |
| 2096 | dst_path->slots[cur_level] = 0; |
| 2097 | |
| 2098 | btrfs_tree_read_lock(eb); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 2099 | dst_path->locks[cur_level] = BTRFS_READ_LOCK; |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2100 | need_cleanup = true; |
| 2101 | } |
| 2102 | |
| 2103 | /* Now record this tree block and its counter part for qgroups */ |
| 2104 | ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level, |
Qu Wenruo | 3d0174f | 2018-09-27 14:42:35 +0800 | [diff] [blame] | 2105 | root_level, trace_leaf); |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2106 | if (ret < 0) |
| 2107 | goto cleanup; |
| 2108 | |
| 2109 | eb = dst_path->nodes[cur_level]; |
| 2110 | |
| 2111 | if (cur_level > 0) { |
| 2112 | /* Iterate all child tree blocks */ |
| 2113 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 2114 | /* Skip old tree blocks as they won't be swapped */ |
| 2115 | if (btrfs_node_ptr_generation(eb, i) < last_snapshot) |
| 2116 | continue; |
| 2117 | dst_path->slots[cur_level] = i; |
| 2118 | |
| 2119 | /* Recursive call (at most 7 times) */ |
| 2120 | ret = qgroup_trace_new_subtree_blocks(trans, src_eb, |
| 2121 | dst_path, cur_level - 1, root_level, |
Qu Wenruo | 3d0174f | 2018-09-27 14:42:35 +0800 | [diff] [blame] | 2122 | last_snapshot, trace_leaf); |
Qu Wenruo | ea49f3e | 2018-09-27 14:42:31 +0800 | [diff] [blame] | 2123 | if (ret < 0) |
| 2124 | goto cleanup; |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | cleanup: |
| 2129 | if (need_cleanup) { |
| 2130 | /* Clean up */ |
| 2131 | btrfs_tree_unlock_rw(dst_path->nodes[cur_level], |
| 2132 | dst_path->locks[cur_level]); |
| 2133 | free_extent_buffer(dst_path->nodes[cur_level]); |
| 2134 | dst_path->nodes[cur_level] = NULL; |
| 2135 | dst_path->slots[cur_level] = 0; |
| 2136 | dst_path->locks[cur_level] = 0; |
| 2137 | } |
| 2138 | out: |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
Qu Wenruo | 5aea1a4 | 2019-01-23 15:15:15 +0800 | [diff] [blame] | 2142 | static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans, |
| 2143 | struct extent_buffer *src_eb, |
| 2144 | struct extent_buffer *dst_eb, |
| 2145 | u64 last_snapshot, bool trace_leaf) |
| 2146 | { |
| 2147 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 2148 | struct btrfs_path *dst_path = NULL; |
| 2149 | int level; |
| 2150 | int ret; |
| 2151 | |
| 2152 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
| 2153 | return 0; |
| 2154 | |
| 2155 | /* Wrong parameter order */ |
| 2156 | if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) { |
| 2157 | btrfs_err_rl(fs_info, |
| 2158 | "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__, |
| 2159 | btrfs_header_generation(src_eb), |
| 2160 | btrfs_header_generation(dst_eb)); |
| 2161 | return -EUCLEAN; |
| 2162 | } |
| 2163 | |
| 2164 | if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) { |
| 2165 | ret = -EIO; |
| 2166 | goto out; |
| 2167 | } |
| 2168 | |
| 2169 | level = btrfs_header_level(dst_eb); |
| 2170 | dst_path = btrfs_alloc_path(); |
| 2171 | if (!dst_path) { |
| 2172 | ret = -ENOMEM; |
| 2173 | goto out; |
| 2174 | } |
| 2175 | /* For dst_path */ |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 2176 | atomic_inc(&dst_eb->refs); |
Qu Wenruo | 5aea1a4 | 2019-01-23 15:15:15 +0800 | [diff] [blame] | 2177 | dst_path->nodes[level] = dst_eb; |
| 2178 | dst_path->slots[level] = 0; |
| 2179 | dst_path->locks[level] = 0; |
| 2180 | |
| 2181 | /* Do the generation aware breadth-first search */ |
| 2182 | ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level, |
| 2183 | level, last_snapshot, trace_leaf); |
| 2184 | if (ret < 0) |
| 2185 | goto out; |
| 2186 | ret = 0; |
| 2187 | |
| 2188 | out: |
| 2189 | btrfs_free_path(dst_path); |
| 2190 | if (ret < 0) |
| 2191 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2195 | int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2196 | struct extent_buffer *root_eb, |
| 2197 | u64 root_gen, int root_level) |
| 2198 | { |
Lu Fengqi | deb4062 | 2018-07-18 14:45:38 +0800 | [diff] [blame] | 2199 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2200 | int ret = 0; |
| 2201 | int level; |
| 2202 | struct extent_buffer *eb = root_eb; |
| 2203 | struct btrfs_path *path = NULL; |
| 2204 | |
Nikolay Borisov | b6e6bca | 2017-07-12 09:42:19 +0300 | [diff] [blame] | 2205 | BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2206 | BUG_ON(root_eb == NULL); |
| 2207 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2208 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2209 | return 0; |
| 2210 | |
| 2211 | if (!extent_buffer_uptodate(root_eb)) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2212 | ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2213 | if (ret) |
| 2214 | goto out; |
| 2215 | } |
| 2216 | |
| 2217 | if (root_level == 0) { |
Lu Fengqi | 8d38d7e | 2018-07-18 14:45:37 +0800 | [diff] [blame] | 2218 | ret = btrfs_qgroup_trace_leaf_items(trans, root_eb); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2219 | goto out; |
| 2220 | } |
| 2221 | |
| 2222 | path = btrfs_alloc_path(); |
| 2223 | if (!path) |
| 2224 | return -ENOMEM; |
| 2225 | |
| 2226 | /* |
| 2227 | * Walk down the tree. Missing extent blocks are filled in as |
| 2228 | * we go. Metadata is accounted every time we read a new |
| 2229 | * extent block. |
| 2230 | * |
| 2231 | * When we reach a leaf, we account for file extent items in it, |
| 2232 | * walk back up the tree (adjusting slot pointers as we go) |
| 2233 | * and restart the search process. |
| 2234 | */ |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 2235 | atomic_inc(&root_eb->refs); /* For path */ |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2236 | path->nodes[root_level] = root_eb; |
| 2237 | path->slots[root_level] = 0; |
| 2238 | path->locks[root_level] = 0; /* so release_path doesn't try to unlock */ |
| 2239 | walk_down: |
| 2240 | level = root_level; |
| 2241 | while (level >= 0) { |
| 2242 | if (path->nodes[level] == NULL) { |
| 2243 | int parent_slot; |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2244 | u64 child_bytenr; |
| 2245 | |
| 2246 | /* |
Josef Bacik | 182c79f | 2020-11-05 10:45:17 -0500 | [diff] [blame] | 2247 | * We need to get child blockptr from parent before we |
| 2248 | * can read it. |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2249 | */ |
| 2250 | eb = path->nodes[level + 1]; |
| 2251 | parent_slot = path->slots[level + 1]; |
| 2252 | child_bytenr = btrfs_node_blockptr(eb, parent_slot); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2253 | |
Josef Bacik | 182c79f | 2020-11-05 10:45:17 -0500 | [diff] [blame] | 2254 | eb = btrfs_read_node_slot(eb, parent_slot); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2255 | if (IS_ERR(eb)) { |
| 2256 | ret = PTR_ERR(eb); |
| 2257 | goto out; |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | path->nodes[level] = eb; |
| 2261 | path->slots[level] = 0; |
| 2262 | |
| 2263 | btrfs_tree_read_lock(eb); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 2264 | path->locks[level] = BTRFS_READ_LOCK; |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2265 | |
Lu Fengqi | a95f3aa | 2018-07-18 16:28:03 +0800 | [diff] [blame] | 2266 | ret = btrfs_qgroup_trace_extent(trans, child_bytenr, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2267 | fs_info->nodesize, |
| 2268 | GFP_NOFS); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2269 | if (ret) |
| 2270 | goto out; |
| 2271 | } |
| 2272 | |
| 2273 | if (level == 0) { |
Lu Fengqi | 8d38d7e | 2018-07-18 14:45:37 +0800 | [diff] [blame] | 2274 | ret = btrfs_qgroup_trace_leaf_items(trans, |
| 2275 | path->nodes[level]); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2276 | if (ret) |
| 2277 | goto out; |
| 2278 | |
| 2279 | /* Nonzero return here means we completed our search */ |
David Sterba | 15b3451 | 2017-02-10 20:30:23 +0100 | [diff] [blame] | 2280 | ret = adjust_slots_upwards(path, root_level); |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 2281 | if (ret) |
| 2282 | break; |
| 2283 | |
| 2284 | /* Restart search with new slots */ |
| 2285 | goto walk_down; |
| 2286 | } |
| 2287 | |
| 2288 | level--; |
| 2289 | } |
| 2290 | |
| 2291 | ret = 0; |
| 2292 | out: |
| 2293 | btrfs_free_path(path); |
| 2294 | |
| 2295 | return ret; |
| 2296 | } |
| 2297 | |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 2298 | #define UPDATE_NEW 0 |
| 2299 | #define UPDATE_OLD 1 |
| 2300 | /* |
| 2301 | * Walk all of the roots that points to the bytenr and adjust their refcnts. |
| 2302 | */ |
| 2303 | static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info, |
| 2304 | struct ulist *roots, struct ulist *tmp, |
| 2305 | struct ulist *qgroups, u64 seq, int update_old) |
| 2306 | { |
| 2307 | struct ulist_node *unode; |
| 2308 | struct ulist_iterator uiter; |
| 2309 | struct ulist_node *tmp_unode; |
| 2310 | struct ulist_iterator tmp_uiter; |
| 2311 | struct btrfs_qgroup *qg; |
| 2312 | int ret = 0; |
| 2313 | |
| 2314 | if (!roots) |
| 2315 | return 0; |
| 2316 | ULIST_ITER_INIT(&uiter); |
| 2317 | while ((unode = ulist_next(roots, &uiter))) { |
| 2318 | qg = find_qgroup_rb(fs_info, unode->val); |
| 2319 | if (!qg) |
| 2320 | continue; |
| 2321 | |
| 2322 | ulist_reinit(tmp); |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2323 | ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg), |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 2324 | GFP_ATOMIC); |
| 2325 | if (ret < 0) |
| 2326 | return ret; |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2327 | ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC); |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 2328 | if (ret < 0) |
| 2329 | return ret; |
| 2330 | ULIST_ITER_INIT(&tmp_uiter); |
| 2331 | while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) { |
| 2332 | struct btrfs_qgroup_list *glist; |
| 2333 | |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2334 | qg = unode_aux_to_qgroup(tmp_unode); |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 2335 | if (update_old) |
| 2336 | btrfs_qgroup_update_old_refcnt(qg, seq, 1); |
| 2337 | else |
| 2338 | btrfs_qgroup_update_new_refcnt(qg, seq, 1); |
| 2339 | list_for_each_entry(glist, &qg->groups, next_group) { |
| 2340 | ret = ulist_add(qgroups, glist->group->qgroupid, |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2341 | qgroup_to_aux(glist->group), |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 2342 | GFP_ATOMIC); |
| 2343 | if (ret < 0) |
| 2344 | return ret; |
| 2345 | ret = ulist_add(tmp, glist->group->qgroupid, |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2346 | qgroup_to_aux(glist->group), |
Qu Wenruo | d810ef2 | 2015-04-12 16:52:34 +0800 | [diff] [blame] | 2347 | GFP_ATOMIC); |
| 2348 | if (ret < 0) |
| 2349 | return ret; |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2356 | /* |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 2357 | * Update qgroup rfer/excl counters. |
| 2358 | * Rfer update is easy, codes can explain themselves. |
Qu Wenruo | e69bcee | 2015-04-17 10:23:16 +0800 | [diff] [blame] | 2359 | * |
Randy Dunlap | 260db43 | 2020-08-04 19:48:34 -0700 | [diff] [blame] | 2360 | * Excl update is tricky, the update is split into 2 parts. |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 2361 | * Part 1: Possible exclusive <-> sharing detect: |
| 2362 | * | A | !A | |
| 2363 | * ------------------------------------- |
| 2364 | * B | * | - | |
| 2365 | * ------------------------------------- |
| 2366 | * !B | + | ** | |
| 2367 | * ------------------------------------- |
| 2368 | * |
| 2369 | * Conditions: |
| 2370 | * A: cur_old_roots < nr_old_roots (not exclusive before) |
| 2371 | * !A: cur_old_roots == nr_old_roots (possible exclusive before) |
| 2372 | * B: cur_new_roots < nr_new_roots (not exclusive now) |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2373 | * !B: cur_new_roots == nr_new_roots (possible exclusive now) |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 2374 | * |
| 2375 | * Results: |
| 2376 | * +: Possible sharing -> exclusive -: Possible exclusive -> sharing |
| 2377 | * *: Definitely not changed. **: Possible unchanged. |
| 2378 | * |
| 2379 | * For !A and !B condition, the exception is cur_old/new_roots == 0 case. |
| 2380 | * |
| 2381 | * To make the logic clear, we first use condition A and B to split |
| 2382 | * combination into 4 results. |
| 2383 | * |
| 2384 | * Then, for result "+" and "-", check old/new_roots == 0 case, as in them |
| 2385 | * only on variant maybe 0. |
| 2386 | * |
| 2387 | * Lastly, check result **, since there are 2 variants maybe 0, split them |
| 2388 | * again(2x2). |
| 2389 | * But this time we don't need to consider other things, the codes and logic |
| 2390 | * is easy to understand now. |
| 2391 | */ |
| 2392 | static int qgroup_update_counters(struct btrfs_fs_info *fs_info, |
| 2393 | struct ulist *qgroups, |
| 2394 | u64 nr_old_roots, |
| 2395 | u64 nr_new_roots, |
| 2396 | u64 num_bytes, u64 seq) |
| 2397 | { |
| 2398 | struct ulist_node *unode; |
| 2399 | struct ulist_iterator uiter; |
| 2400 | struct btrfs_qgroup *qg; |
| 2401 | u64 cur_new_count, cur_old_count; |
| 2402 | |
| 2403 | ULIST_ITER_INIT(&uiter); |
| 2404 | while ((unode = ulist_next(qgroups, &uiter))) { |
| 2405 | bool dirty = false; |
| 2406 | |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2407 | qg = unode_aux_to_qgroup(unode); |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 2408 | cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq); |
| 2409 | cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq); |
| 2410 | |
Qu Wenruo | 8b31790 | 2018-04-30 15:04:44 +0800 | [diff] [blame] | 2411 | trace_qgroup_update_counters(fs_info, qg, cur_old_count, |
| 2412 | cur_new_count); |
Mark Fasheh | 0f5dcf8 | 2016-03-29 17:19:55 -0700 | [diff] [blame] | 2413 | |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 2414 | /* Rfer update part */ |
| 2415 | if (cur_old_count == 0 && cur_new_count > 0) { |
| 2416 | qg->rfer += num_bytes; |
| 2417 | qg->rfer_cmpr += num_bytes; |
| 2418 | dirty = true; |
| 2419 | } |
| 2420 | if (cur_old_count > 0 && cur_new_count == 0) { |
| 2421 | qg->rfer -= num_bytes; |
| 2422 | qg->rfer_cmpr -= num_bytes; |
| 2423 | dirty = true; |
| 2424 | } |
| 2425 | |
| 2426 | /* Excl update part */ |
| 2427 | /* Exclusive/none -> shared case */ |
| 2428 | if (cur_old_count == nr_old_roots && |
| 2429 | cur_new_count < nr_new_roots) { |
| 2430 | /* Exclusive -> shared */ |
| 2431 | if (cur_old_count != 0) { |
| 2432 | qg->excl -= num_bytes; |
| 2433 | qg->excl_cmpr -= num_bytes; |
| 2434 | dirty = true; |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | /* Shared -> exclusive/none case */ |
| 2439 | if (cur_old_count < nr_old_roots && |
| 2440 | cur_new_count == nr_new_roots) { |
| 2441 | /* Shared->exclusive */ |
| 2442 | if (cur_new_count != 0) { |
| 2443 | qg->excl += num_bytes; |
| 2444 | qg->excl_cmpr += num_bytes; |
| 2445 | dirty = true; |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | /* Exclusive/none -> exclusive/none case */ |
| 2450 | if (cur_old_count == nr_old_roots && |
| 2451 | cur_new_count == nr_new_roots) { |
| 2452 | if (cur_old_count == 0) { |
| 2453 | /* None -> exclusive/none */ |
| 2454 | |
| 2455 | if (cur_new_count != 0) { |
| 2456 | /* None -> exclusive */ |
| 2457 | qg->excl += num_bytes; |
| 2458 | qg->excl_cmpr += num_bytes; |
| 2459 | dirty = true; |
| 2460 | } |
| 2461 | /* None -> none, nothing changed */ |
| 2462 | } else { |
| 2463 | /* Exclusive -> exclusive/none */ |
| 2464 | |
| 2465 | if (cur_new_count == 0) { |
| 2466 | /* Exclusive -> none */ |
| 2467 | qg->excl -= num_bytes; |
| 2468 | qg->excl_cmpr -= num_bytes; |
| 2469 | dirty = true; |
| 2470 | } |
| 2471 | /* Exclusive -> exclusive, nothing changed */ |
| 2472 | } |
| 2473 | } |
Qu Wenruo | c05f942 | 2015-08-03 14:44:29 +0800 | [diff] [blame] | 2474 | |
Qu Wenruo | 823ae5b | 2015-04-12 16:59:57 +0800 | [diff] [blame] | 2475 | if (dirty) |
| 2476 | qgroup_dirty(fs_info, qg); |
| 2477 | } |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
Qu Wenruo | 5edfd9f | 2017-02-27 15:10:34 +0800 | [diff] [blame] | 2481 | /* |
| 2482 | * Check if the @roots potentially is a list of fs tree roots |
| 2483 | * |
| 2484 | * Return 0 for definitely not a fs/subvol tree roots ulist |
| 2485 | * Return 1 for possible fs/subvol tree roots in the list (considering an empty |
| 2486 | * one as well) |
| 2487 | */ |
| 2488 | static int maybe_fs_roots(struct ulist *roots) |
| 2489 | { |
| 2490 | struct ulist_node *unode; |
| 2491 | struct ulist_iterator uiter; |
| 2492 | |
| 2493 | /* Empty one, still possible for fs roots */ |
| 2494 | if (!roots || roots->nnodes == 0) |
| 2495 | return 1; |
| 2496 | |
| 2497 | ULIST_ITER_INIT(&uiter); |
| 2498 | unode = ulist_next(roots, &uiter); |
| 2499 | if (!unode) |
| 2500 | return 1; |
| 2501 | |
| 2502 | /* |
| 2503 | * If it contains fs tree roots, then it must belong to fs/subvol |
| 2504 | * trees. |
| 2505 | * If it contains a non-fs tree, it won't be shared with fs/subvol trees. |
| 2506 | */ |
| 2507 | return is_fstree(unode->val); |
| 2508 | } |
| 2509 | |
Lu Fengqi | 8696d76 | 2018-07-18 14:45:39 +0800 | [diff] [blame] | 2510 | int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr, |
| 2511 | u64 num_bytes, struct ulist *old_roots, |
| 2512 | struct ulist *new_roots) |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2513 | { |
Lu Fengqi | 8696d76 | 2018-07-18 14:45:39 +0800 | [diff] [blame] | 2514 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2515 | struct ulist *qgroups = NULL; |
| 2516 | struct ulist *tmp = NULL; |
| 2517 | u64 seq; |
| 2518 | u64 nr_new_roots = 0; |
| 2519 | u64 nr_old_roots = 0; |
| 2520 | int ret = 0; |
| 2521 | |
Johannes Thumshirn | 26ef849 | 2020-01-08 21:07:32 +0900 | [diff] [blame] | 2522 | /* |
| 2523 | * If quotas get disabled meanwhile, the resouces need to be freed and |
| 2524 | * we can't just exit here. |
| 2525 | */ |
David Sterba | 81353d5 | 2017-02-13 14:05:24 +0100 | [diff] [blame] | 2526 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
Johannes Thumshirn | 26ef849 | 2020-01-08 21:07:32 +0900 | [diff] [blame] | 2527 | goto out_free; |
David Sterba | 81353d5 | 2017-02-13 14:05:24 +0100 | [diff] [blame] | 2528 | |
Qu Wenruo | 5edfd9f | 2017-02-27 15:10:34 +0800 | [diff] [blame] | 2529 | if (new_roots) { |
| 2530 | if (!maybe_fs_roots(new_roots)) |
| 2531 | goto out_free; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2532 | nr_new_roots = new_roots->nnodes; |
Qu Wenruo | 5edfd9f | 2017-02-27 15:10:34 +0800 | [diff] [blame] | 2533 | } |
| 2534 | if (old_roots) { |
| 2535 | if (!maybe_fs_roots(old_roots)) |
| 2536 | goto out_free; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2537 | nr_old_roots = old_roots->nnodes; |
Qu Wenruo | 5edfd9f | 2017-02-27 15:10:34 +0800 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | /* Quick exit, either not fs tree roots, or won't affect any qgroup */ |
| 2541 | if (nr_old_roots == 0 && nr_new_roots == 0) |
| 2542 | goto out_free; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2543 | |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2544 | BUG_ON(!fs_info->quota_root); |
| 2545 | |
Qu Wenruo | c9f6f3c | 2018-05-03 09:59:02 +0800 | [diff] [blame] | 2546 | trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr, |
| 2547 | num_bytes, nr_old_roots, nr_new_roots); |
Mark Fasheh | 0f5dcf8 | 2016-03-29 17:19:55 -0700 | [diff] [blame] | 2548 | |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2549 | qgroups = ulist_alloc(GFP_NOFS); |
| 2550 | if (!qgroups) { |
| 2551 | ret = -ENOMEM; |
| 2552 | goto out_free; |
| 2553 | } |
| 2554 | tmp = ulist_alloc(GFP_NOFS); |
| 2555 | if (!tmp) { |
| 2556 | ret = -ENOMEM; |
| 2557 | goto out_free; |
| 2558 | } |
| 2559 | |
| 2560 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 2561 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
| 2562 | if (fs_info->qgroup_rescan_progress.objectid <= bytenr) { |
| 2563 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 2564 | ret = 0; |
| 2565 | goto out_free; |
| 2566 | } |
| 2567 | } |
| 2568 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 2569 | |
| 2570 | spin_lock(&fs_info->qgroup_lock); |
| 2571 | seq = fs_info->qgroup_seq; |
| 2572 | |
| 2573 | /* Update old refcnts using old_roots */ |
| 2574 | ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq, |
| 2575 | UPDATE_OLD); |
| 2576 | if (ret < 0) |
| 2577 | goto out; |
| 2578 | |
| 2579 | /* Update new refcnts using new_roots */ |
| 2580 | ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq, |
| 2581 | UPDATE_NEW); |
| 2582 | if (ret < 0) |
| 2583 | goto out; |
| 2584 | |
| 2585 | qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots, |
| 2586 | num_bytes, seq); |
| 2587 | |
| 2588 | /* |
| 2589 | * Bump qgroup_seq to avoid seq overlap |
| 2590 | */ |
| 2591 | fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1; |
| 2592 | out: |
| 2593 | spin_unlock(&fs_info->qgroup_lock); |
| 2594 | out_free: |
| 2595 | ulist_free(tmp); |
| 2596 | ulist_free(qgroups); |
| 2597 | ulist_free(old_roots); |
| 2598 | ulist_free(new_roots); |
| 2599 | return ret; |
| 2600 | } |
| 2601 | |
Nikolay Borisov | 460fb20 | 2018-03-15 16:00:25 +0200 | [diff] [blame] | 2602 | int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans) |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2603 | { |
Nikolay Borisov | 460fb20 | 2018-03-15 16:00:25 +0200 | [diff] [blame] | 2604 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2605 | struct btrfs_qgroup_extent_record *record; |
| 2606 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2607 | struct ulist *new_roots = NULL; |
| 2608 | struct rb_node *node; |
Qu Wenruo | c337e7b | 2018-09-27 14:42:29 +0800 | [diff] [blame] | 2609 | u64 num_dirty_extents = 0; |
Qu Wenruo | 9086db8 | 2015-04-20 09:53:50 +0800 | [diff] [blame] | 2610 | u64 qgroup_to_skip; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2611 | int ret = 0; |
| 2612 | |
| 2613 | delayed_refs = &trans->transaction->delayed_refs; |
Qu Wenruo | 9086db8 | 2015-04-20 09:53:50 +0800 | [diff] [blame] | 2614 | qgroup_to_skip = delayed_refs->qgroup_to_skip; |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2615 | while ((node = rb_first(&delayed_refs->dirty_extent_root))) { |
| 2616 | record = rb_entry(node, struct btrfs_qgroup_extent_record, |
| 2617 | node); |
| 2618 | |
Qu Wenruo | c337e7b | 2018-09-27 14:42:29 +0800 | [diff] [blame] | 2619 | num_dirty_extents++; |
Jeff Mahoney | bc07452 | 2016-06-09 17:27:55 -0400 | [diff] [blame] | 2620 | trace_btrfs_qgroup_account_extents(fs_info, record); |
Mark Fasheh | 0f5dcf8 | 2016-03-29 17:19:55 -0700 | [diff] [blame] | 2621 | |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2622 | if (!ret) { |
| 2623 | /* |
Qu Wenruo | d1b8b94 | 2017-02-27 15:10:35 +0800 | [diff] [blame] | 2624 | * Old roots should be searched when inserting qgroup |
| 2625 | * extent record |
| 2626 | */ |
| 2627 | if (WARN_ON(!record->old_roots)) { |
| 2628 | /* Search commit root to find old_roots */ |
| 2629 | ret = btrfs_find_all_roots(NULL, fs_info, |
| 2630 | record->bytenr, 0, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 2631 | &record->old_roots, false); |
Qu Wenruo | d1b8b94 | 2017-02-27 15:10:35 +0800 | [diff] [blame] | 2632 | if (ret < 0) |
| 2633 | goto cleanup; |
| 2634 | } |
| 2635 | |
Qu Wenruo | 1418bae | 2019-01-23 15:15:12 +0800 | [diff] [blame] | 2636 | /* Free the reserved data space */ |
| 2637 | btrfs_qgroup_free_refroot(fs_info, |
| 2638 | record->data_rsv_refroot, |
| 2639 | record->data_rsv, |
| 2640 | BTRFS_QGROUP_RSV_DATA); |
Qu Wenruo | d1b8b94 | 2017-02-27 15:10:35 +0800 | [diff] [blame] | 2641 | /* |
Edmund Nadolski | de47c9d | 2017-03-16 10:04:34 -0600 | [diff] [blame] | 2642 | * Use SEQ_LAST as time_seq to do special search, which |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2643 | * doesn't lock tree or delayed_refs and search current |
| 2644 | * root. It's safe inside commit_transaction(). |
| 2645 | */ |
| 2646 | ret = btrfs_find_all_roots(trans, fs_info, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 2647 | record->bytenr, SEQ_LAST, &new_roots, false); |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2648 | if (ret < 0) |
| 2649 | goto cleanup; |
Qu Wenruo | d1b8b94 | 2017-02-27 15:10:35 +0800 | [diff] [blame] | 2650 | if (qgroup_to_skip) { |
Qu Wenruo | 9086db8 | 2015-04-20 09:53:50 +0800 | [diff] [blame] | 2651 | ulist_del(new_roots, qgroup_to_skip, 0); |
Qu Wenruo | d1b8b94 | 2017-02-27 15:10:35 +0800 | [diff] [blame] | 2652 | ulist_del(record->old_roots, qgroup_to_skip, |
| 2653 | 0); |
| 2654 | } |
Lu Fengqi | 8696d76 | 2018-07-18 14:45:39 +0800 | [diff] [blame] | 2655 | ret = btrfs_qgroup_account_extent(trans, record->bytenr, |
| 2656 | record->num_bytes, |
| 2657 | record->old_roots, |
| 2658 | new_roots); |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2659 | record->old_roots = NULL; |
| 2660 | new_roots = NULL; |
| 2661 | } |
| 2662 | cleanup: |
| 2663 | ulist_free(record->old_roots); |
| 2664 | ulist_free(new_roots); |
| 2665 | new_roots = NULL; |
| 2666 | rb_erase(node, &delayed_refs->dirty_extent_root); |
| 2667 | kfree(record); |
| 2668 | |
| 2669 | } |
Qu Wenruo | c337e7b | 2018-09-27 14:42:29 +0800 | [diff] [blame] | 2670 | trace_qgroup_num_dirty_extents(fs_info, trans->transid, |
| 2671 | num_dirty_extents); |
Qu Wenruo | 550d7a2 | 2015-04-16 15:37:33 +0800 | [diff] [blame] | 2672 | return ret; |
| 2673 | } |
| 2674 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2675 | /* |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2676 | * called from commit_transaction. Writes all changed qgroups to disk. |
| 2677 | */ |
Lu Fengqi | 280f8bd | 2018-07-18 14:45:40 +0800 | [diff] [blame] | 2678 | int btrfs_run_qgroups(struct btrfs_trans_handle *trans) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2679 | { |
Lu Fengqi | 280f8bd | 2018-07-18 14:45:40 +0800 | [diff] [blame] | 2680 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2681 | int ret = 0; |
| 2682 | |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 2683 | if (!fs_info->quota_root) |
Nikolay Borisov | 5d23515 | 2018-01-31 10:52:04 +0200 | [diff] [blame] | 2684 | return ret; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2685 | |
| 2686 | spin_lock(&fs_info->qgroup_lock); |
| 2687 | while (!list_empty(&fs_info->dirty_qgroups)) { |
| 2688 | struct btrfs_qgroup *qgroup; |
| 2689 | qgroup = list_first_entry(&fs_info->dirty_qgroups, |
| 2690 | struct btrfs_qgroup, dirty); |
| 2691 | list_del_init(&qgroup->dirty); |
| 2692 | spin_unlock(&fs_info->qgroup_lock); |
Lu Fengqi | 3e07e9a | 2018-07-18 14:45:28 +0800 | [diff] [blame] | 2693 | ret = update_qgroup_info_item(trans, qgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2694 | if (ret) |
| 2695 | fs_info->qgroup_flags |= |
| 2696 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Lu Fengqi | ac8a866 | 2018-07-18 14:45:27 +0800 | [diff] [blame] | 2697 | ret = update_qgroup_limit_item(trans, qgroup); |
Dongsheng Yang | d3001ed | 2014-11-20 21:04:56 -0500 | [diff] [blame] | 2698 | if (ret) |
| 2699 | fs_info->qgroup_flags |= |
| 2700 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2701 | spin_lock(&fs_info->qgroup_lock); |
| 2702 | } |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 2703 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2704 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON; |
| 2705 | else |
| 2706 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON; |
| 2707 | spin_unlock(&fs_info->qgroup_lock); |
| 2708 | |
Lu Fengqi | 2e980ac | 2018-07-18 14:45:29 +0800 | [diff] [blame] | 2709 | ret = update_qgroup_status_item(trans); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2710 | if (ret) |
| 2711 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 2712 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2713 | return ret; |
| 2714 | } |
| 2715 | |
| 2716 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2717 | * Copy the accounting information between qgroups. This is necessary |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2718 | * when a snapshot or a subvolume is created. Throwing an error will |
| 2719 | * cause a transaction abort so we take extra care here to only error |
| 2720 | * when a readonly fs is a reasonable outcome. |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2721 | */ |
Lu Fengqi | a9377422 | 2018-07-18 14:45:41 +0800 | [diff] [blame] | 2722 | int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, |
| 2723 | u64 objectid, struct btrfs_qgroup_inherit *inherit) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2724 | { |
| 2725 | int ret = 0; |
| 2726 | int i; |
| 2727 | u64 *i_qgroups; |
Qu Wenruo | e88439d | 2019-06-13 17:31:24 +0800 | [diff] [blame] | 2728 | bool committing = false; |
Lu Fengqi | a9377422 | 2018-07-18 14:45:41 +0800 | [diff] [blame] | 2729 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 552f032 | 2018-11-19 16:20:34 +0000 | [diff] [blame] | 2730 | struct btrfs_root *quota_root; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2731 | struct btrfs_qgroup *srcgroup; |
| 2732 | struct btrfs_qgroup *dstgroup; |
Qu Wenruo | cbab8ad | 2020-04-02 14:37:35 +0800 | [diff] [blame] | 2733 | bool need_rescan = false; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2734 | u32 level_size = 0; |
Wang Shilong | 3f5e2d3 | 2013-04-07 10:50:19 +0000 | [diff] [blame] | 2735 | u64 nums; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2736 | |
Qu Wenruo | e88439d | 2019-06-13 17:31:24 +0800 | [diff] [blame] | 2737 | /* |
| 2738 | * There are only two callers of this function. |
| 2739 | * |
| 2740 | * One in create_subvol() in the ioctl context, which needs to hold |
| 2741 | * the qgroup_ioctl_lock. |
| 2742 | * |
| 2743 | * The other one in create_pending_snapshot() where no other qgroup |
| 2744 | * code can modify the fs as they all need to either start a new trans |
| 2745 | * or hold a trans handler, thus we don't need to hold |
| 2746 | * qgroup_ioctl_lock. |
| 2747 | * This would avoid long and complex lock chain and make lockdep happy. |
| 2748 | */ |
| 2749 | spin_lock(&fs_info->trans_lock); |
| 2750 | if (trans->transaction->state == TRANS_STATE_COMMIT_DOING) |
| 2751 | committing = true; |
| 2752 | spin_unlock(&fs_info->trans_lock); |
| 2753 | |
| 2754 | if (!committing) |
| 2755 | mutex_lock(&fs_info->qgroup_ioctl_lock); |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 2756 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 2757 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2758 | |
Filipe Manana | 552f032 | 2018-11-19 16:20:34 +0000 | [diff] [blame] | 2759 | quota_root = fs_info->quota_root; |
Wang Shilong | f2f6ed3 | 2013-04-07 10:50:16 +0000 | [diff] [blame] | 2760 | if (!quota_root) { |
| 2761 | ret = -EINVAL; |
| 2762 | goto out; |
| 2763 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2764 | |
Wang Shilong | 3f5e2d3 | 2013-04-07 10:50:19 +0000 | [diff] [blame] | 2765 | if (inherit) { |
| 2766 | i_qgroups = (u64 *)(inherit + 1); |
| 2767 | nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + |
| 2768 | 2 * inherit->num_excl_copies; |
| 2769 | for (i = 0; i < nums; ++i) { |
| 2770 | srcgroup = find_qgroup_rb(fs_info, *i_qgroups); |
Dongsheng Yang | 09870d2 | 2014-11-11 07:18:22 -0500 | [diff] [blame] | 2771 | |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2772 | /* |
| 2773 | * Zero out invalid groups so we can ignore |
| 2774 | * them later. |
| 2775 | */ |
| 2776 | if (!srcgroup || |
| 2777 | ((srcgroup->qgroupid >> 48) <= (objectid >> 48))) |
| 2778 | *i_qgroups = 0ULL; |
| 2779 | |
Wang Shilong | 3f5e2d3 | 2013-04-07 10:50:19 +0000 | [diff] [blame] | 2780 | ++i_qgroups; |
| 2781 | } |
| 2782 | } |
| 2783 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2784 | /* |
| 2785 | * create a tracking group for the subvol itself |
| 2786 | */ |
| 2787 | ret = add_qgroup_item(trans, quota_root, objectid); |
| 2788 | if (ret) |
| 2789 | goto out; |
| 2790 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2791 | /* |
| 2792 | * add qgroup to all inherited groups |
| 2793 | */ |
| 2794 | if (inherit) { |
| 2795 | i_qgroups = (u64 *)(inherit + 1); |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2796 | for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) { |
| 2797 | if (*i_qgroups == 0) |
| 2798 | continue; |
Lu Fengqi | 711169c | 2018-07-18 14:45:24 +0800 | [diff] [blame] | 2799 | ret = add_qgroup_relation_item(trans, objectid, |
| 2800 | *i_qgroups); |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2801 | if (ret && ret != -EEXIST) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2802 | goto out; |
Lu Fengqi | 711169c | 2018-07-18 14:45:24 +0800 | [diff] [blame] | 2803 | ret = add_qgroup_relation_item(trans, *i_qgroups, |
| 2804 | objectid); |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2805 | if (ret && ret != -EEXIST) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2806 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2807 | } |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2808 | ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | |
| 2812 | spin_lock(&fs_info->qgroup_lock); |
| 2813 | |
| 2814 | dstgroup = add_qgroup_rb(fs_info, objectid); |
Dan Carpenter | 57a5a88 | 2012-07-30 02:15:43 -0600 | [diff] [blame] | 2815 | if (IS_ERR(dstgroup)) { |
| 2816 | ret = PTR_ERR(dstgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2817 | goto unlock; |
Dan Carpenter | 57a5a88 | 2012-07-30 02:15:43 -0600 | [diff] [blame] | 2818 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2819 | |
Dongsheng Yang | e8c8541 | 2014-11-20 20:58:34 -0500 | [diff] [blame] | 2820 | if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) { |
Dongsheng Yang | e8c8541 | 2014-11-20 20:58:34 -0500 | [diff] [blame] | 2821 | dstgroup->lim_flags = inherit->lim.flags; |
| 2822 | dstgroup->max_rfer = inherit->lim.max_rfer; |
| 2823 | dstgroup->max_excl = inherit->lim.max_excl; |
| 2824 | dstgroup->rsv_rfer = inherit->lim.rsv_rfer; |
| 2825 | dstgroup->rsv_excl = inherit->lim.rsv_excl; |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 2826 | |
Lu Fengqi | ac8a866 | 2018-07-18 14:45:27 +0800 | [diff] [blame] | 2827 | ret = update_qgroup_limit_item(trans, dstgroup); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 2828 | if (ret) { |
| 2829 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2830 | btrfs_info(fs_info, |
| 2831 | "unable to update quota limit for %llu", |
| 2832 | dstgroup->qgroupid); |
Dongsheng Yang | 1510e71 | 2014-11-20 21:01:41 -0500 | [diff] [blame] | 2833 | goto unlock; |
| 2834 | } |
Dongsheng Yang | e8c8541 | 2014-11-20 20:58:34 -0500 | [diff] [blame] | 2835 | } |
| 2836 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2837 | if (srcid) { |
| 2838 | srcgroup = find_qgroup_rb(fs_info, srcid); |
Chris Mason | f3a87f1 | 2012-09-14 20:06:30 -0400 | [diff] [blame] | 2839 | if (!srcgroup) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2840 | goto unlock; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2841 | |
| 2842 | /* |
| 2843 | * We call inherit after we clone the root in order to make sure |
| 2844 | * our counts don't go crazy, so at this point the only |
| 2845 | * difference between the two roots should be the root node. |
| 2846 | */ |
Lu Fengqi | c8389d4 | 2018-07-17 16:58:22 +0800 | [diff] [blame] | 2847 | level_size = fs_info->nodesize; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2848 | dstgroup->rfer = srcgroup->rfer; |
| 2849 | dstgroup->rfer_cmpr = srcgroup->rfer_cmpr; |
| 2850 | dstgroup->excl = level_size; |
| 2851 | dstgroup->excl_cmpr = level_size; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2852 | srcgroup->excl = level_size; |
| 2853 | srcgroup->excl_cmpr = level_size; |
Dongsheng Yang | 3eeb4d5 | 2014-11-20 20:14:38 -0500 | [diff] [blame] | 2854 | |
| 2855 | /* inherit the limit info */ |
| 2856 | dstgroup->lim_flags = srcgroup->lim_flags; |
| 2857 | dstgroup->max_rfer = srcgroup->max_rfer; |
| 2858 | dstgroup->max_excl = srcgroup->max_excl; |
| 2859 | dstgroup->rsv_rfer = srcgroup->rsv_rfer; |
| 2860 | dstgroup->rsv_excl = srcgroup->rsv_excl; |
| 2861 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2862 | qgroup_dirty(fs_info, dstgroup); |
| 2863 | qgroup_dirty(fs_info, srcgroup); |
| 2864 | } |
| 2865 | |
Chris Mason | f3a87f1 | 2012-09-14 20:06:30 -0400 | [diff] [blame] | 2866 | if (!inherit) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2867 | goto unlock; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2868 | |
| 2869 | i_qgroups = (u64 *)(inherit + 1); |
| 2870 | for (i = 0; i < inherit->num_qgroups; ++i) { |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2871 | if (*i_qgroups) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2872 | ret = add_relation_rb(fs_info, objectid, *i_qgroups); |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2873 | if (ret) |
| 2874 | goto unlock; |
| 2875 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2876 | ++i_qgroups; |
Qu Wenruo | cbab8ad | 2020-04-02 14:37:35 +0800 | [diff] [blame] | 2877 | |
| 2878 | /* |
| 2879 | * If we're doing a snapshot, and adding the snapshot to a new |
| 2880 | * qgroup, the numbers are guaranteed to be incorrect. |
| 2881 | */ |
| 2882 | if (srcid) |
| 2883 | need_rescan = true; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2884 | } |
| 2885 | |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2886 | for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2887 | struct btrfs_qgroup *src; |
| 2888 | struct btrfs_qgroup *dst; |
| 2889 | |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2890 | if (!i_qgroups[0] || !i_qgroups[1]) |
| 2891 | continue; |
| 2892 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2893 | src = find_qgroup_rb(fs_info, i_qgroups[0]); |
| 2894 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); |
| 2895 | |
| 2896 | if (!src || !dst) { |
| 2897 | ret = -EINVAL; |
| 2898 | goto unlock; |
| 2899 | } |
| 2900 | |
| 2901 | dst->rfer = src->rfer - level_size; |
| 2902 | dst->rfer_cmpr = src->rfer_cmpr - level_size; |
Qu Wenruo | cbab8ad | 2020-04-02 14:37:35 +0800 | [diff] [blame] | 2903 | |
| 2904 | /* Manually tweaking numbers certainly needs a rescan */ |
| 2905 | need_rescan = true; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2906 | } |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2907 | for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2908 | struct btrfs_qgroup *src; |
| 2909 | struct btrfs_qgroup *dst; |
| 2910 | |
Mark Fasheh | 918c2ee | 2016-03-30 17:57:48 -0700 | [diff] [blame] | 2911 | if (!i_qgroups[0] || !i_qgroups[1]) |
| 2912 | continue; |
| 2913 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2914 | src = find_qgroup_rb(fs_info, i_qgroups[0]); |
| 2915 | dst = find_qgroup_rb(fs_info, i_qgroups[1]); |
| 2916 | |
| 2917 | if (!src || !dst) { |
| 2918 | ret = -EINVAL; |
| 2919 | goto unlock; |
| 2920 | } |
| 2921 | |
| 2922 | dst->excl = src->excl + level_size; |
| 2923 | dst->excl_cmpr = src->excl_cmpr + level_size; |
Qu Wenruo | cbab8ad | 2020-04-02 14:37:35 +0800 | [diff] [blame] | 2924 | need_rescan = true; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | unlock: |
| 2928 | spin_unlock(&fs_info->qgroup_lock); |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 2929 | if (!ret) |
| 2930 | ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2931 | out: |
Qu Wenruo | e88439d | 2019-06-13 17:31:24 +0800 | [diff] [blame] | 2932 | if (!committing) |
| 2933 | mutex_unlock(&fs_info->qgroup_ioctl_lock); |
Qu Wenruo | cbab8ad | 2020-04-02 14:37:35 +0800 | [diff] [blame] | 2934 | if (need_rescan) |
| 2935 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2936 | return ret; |
| 2937 | } |
| 2938 | |
Qu Wenruo | adca4d9 | 2020-07-13 18:50:49 +0800 | [diff] [blame] | 2939 | static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes) |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 2940 | { |
| 2941 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) && |
Qu Wenruo | dba2132 | 2017-12-12 15:34:25 +0800 | [diff] [blame] | 2942 | qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer) |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 2943 | return false; |
| 2944 | |
| 2945 | if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) && |
Qu Wenruo | dba2132 | 2017-12-12 15:34:25 +0800 | [diff] [blame] | 2946 | qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl) |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 2947 | return false; |
| 2948 | |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
Qu Wenruo | dba2132 | 2017-12-12 15:34:25 +0800 | [diff] [blame] | 2952 | static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce, |
| 2953 | enum btrfs_qgroup_rsv_type type) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2954 | { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2955 | struct btrfs_qgroup *qgroup; |
| 2956 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2957 | u64 ref_root = root->root_key.objectid; |
| 2958 | int ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2959 | struct ulist_node *unode; |
| 2960 | struct ulist_iterator uiter; |
| 2961 | |
| 2962 | if (!is_fstree(ref_root)) |
| 2963 | return 0; |
| 2964 | |
| 2965 | if (num_bytes == 0) |
| 2966 | return 0; |
Sargun Dhillon | f29efe2 | 2017-05-11 21:17:33 +0000 | [diff] [blame] | 2967 | |
| 2968 | if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) && |
| 2969 | capable(CAP_SYS_RESOURCE)) |
| 2970 | enforce = false; |
| 2971 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2972 | spin_lock(&fs_info->qgroup_lock); |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 2973 | if (!fs_info->quota_root) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2974 | goto out; |
| 2975 | |
| 2976 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 2977 | if (!qgroup) |
| 2978 | goto out; |
| 2979 | |
| 2980 | /* |
| 2981 | * in a first step, we check all affected qgroups if any limits would |
| 2982 | * be exceeded |
| 2983 | */ |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2984 | ulist_reinit(fs_info->qgroup_ulist); |
| 2985 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, |
David Sterba | a1840b5 | 2018-03-27 19:04:50 +0200 | [diff] [blame] | 2986 | qgroup_to_aux(qgroup), GFP_ATOMIC); |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 2987 | if (ret < 0) |
| 2988 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2989 | ULIST_ITER_INIT(&uiter); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 2990 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2991 | struct btrfs_qgroup *qg; |
| 2992 | struct btrfs_qgroup_list *glist; |
| 2993 | |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 2994 | qg = unode_aux_to_qgroup(unode); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2995 | |
Qu Wenruo | adca4d9 | 2020-07-13 18:50:49 +0800 | [diff] [blame] | 2996 | if (enforce && !qgroup_check_limits(qg, num_bytes)) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2997 | ret = -EDQUOT; |
Wang Shilong | 720f1e2 | 2013-03-06 11:51:47 +0000 | [diff] [blame] | 2998 | goto out; |
| 2999 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3000 | |
| 3001 | list_for_each_entry(glist, &qg->groups, next_group) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 3002 | ret = ulist_add(fs_info->qgroup_ulist, |
| 3003 | glist->group->qgroupid, |
David Sterba | a1840b5 | 2018-03-27 19:04:50 +0200 | [diff] [blame] | 3004 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 3005 | if (ret < 0) |
| 3006 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3007 | } |
| 3008 | } |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 3009 | ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3010 | /* |
| 3011 | * no limits exceeded, now record the reservation into all qgroups |
| 3012 | */ |
| 3013 | ULIST_ITER_INIT(&uiter); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 3014 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3015 | struct btrfs_qgroup *qg; |
| 3016 | |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 3017 | qg = unode_aux_to_qgroup(unode); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3018 | |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 3019 | qgroup_rsv_add(fs_info, qg, num_bytes, type); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | out: |
| 3023 | spin_unlock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3024 | return ret; |
| 3025 | } |
| 3026 | |
Qu Wenruo | e1211d0 | 2017-12-12 15:34:30 +0800 | [diff] [blame] | 3027 | /* |
| 3028 | * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0 |
| 3029 | * qgroup). |
| 3030 | * |
| 3031 | * Will handle all higher level qgroup too. |
| 3032 | * |
| 3033 | * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup. |
| 3034 | * This special case is only used for META_PERTRANS type. |
| 3035 | */ |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 3036 | void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info, |
Qu Wenruo | d4e5c92 | 2017-12-12 15:34:23 +0800 | [diff] [blame] | 3037 | u64 ref_root, u64 num_bytes, |
| 3038 | enum btrfs_qgroup_rsv_type type) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3039 | { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3040 | struct btrfs_qgroup *qgroup; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3041 | struct ulist_node *unode; |
| 3042 | struct ulist_iterator uiter; |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 3043 | int ret = 0; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3044 | |
| 3045 | if (!is_fstree(ref_root)) |
| 3046 | return; |
| 3047 | |
| 3048 | if (num_bytes == 0) |
| 3049 | return; |
| 3050 | |
Qu Wenruo | e1211d0 | 2017-12-12 15:34:30 +0800 | [diff] [blame] | 3051 | if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) { |
| 3052 | WARN(1, "%s: Invalid type to free", __func__); |
| 3053 | return; |
| 3054 | } |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3055 | spin_lock(&fs_info->qgroup_lock); |
| 3056 | |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 3057 | if (!fs_info->quota_root) |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3058 | goto out; |
| 3059 | |
| 3060 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 3061 | if (!qgroup) |
| 3062 | goto out; |
| 3063 | |
Qu Wenruo | e1211d0 | 2017-12-12 15:34:30 +0800 | [diff] [blame] | 3064 | if (num_bytes == (u64)-1) |
Qu Wenruo | 8287475 | 2017-12-12 15:34:34 +0800 | [diff] [blame] | 3065 | /* |
| 3066 | * We're freeing all pertrans rsv, get reserved value from |
| 3067 | * level 0 qgroup as real num_bytes to free. |
| 3068 | */ |
Qu Wenruo | e1211d0 | 2017-12-12 15:34:30 +0800 | [diff] [blame] | 3069 | num_bytes = qgroup->rsv.values[type]; |
| 3070 | |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 3071 | ulist_reinit(fs_info->qgroup_ulist); |
| 3072 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, |
David Sterba | a1840b5 | 2018-03-27 19:04:50 +0200 | [diff] [blame] | 3073 | qgroup_to_aux(qgroup), GFP_ATOMIC); |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 3074 | if (ret < 0) |
| 3075 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3076 | ULIST_ITER_INIT(&uiter); |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 3077 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3078 | struct btrfs_qgroup *qg; |
| 3079 | struct btrfs_qgroup_list *glist; |
| 3080 | |
David Sterba | ef2fff6 | 2016-10-26 16:23:50 +0200 | [diff] [blame] | 3081 | qg = unode_aux_to_qgroup(unode); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3082 | |
Qu Wenruo | 64ee4e7 | 2017-12-12 15:34:27 +0800 | [diff] [blame] | 3083 | qgroup_rsv_release(fs_info, qg, num_bytes, type); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3084 | |
| 3085 | list_for_each_entry(glist, &qg->groups, next_group) { |
Wang Shilong | 1e8f915 | 2013-05-06 11:03:27 +0000 | [diff] [blame] | 3086 | ret = ulist_add(fs_info->qgroup_ulist, |
| 3087 | glist->group->qgroupid, |
David Sterba | a1840b5 | 2018-03-27 19:04:50 +0200 | [diff] [blame] | 3088 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
Wang Shilong | 3c97185 | 2013-04-17 14:00:36 +0000 | [diff] [blame] | 3089 | if (ret < 0) |
| 3090 | goto out; |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | out: |
| 3095 | spin_unlock(&fs_info->qgroup_lock); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3098 | /* |
Qu Wenruo | ff3d27a0 | 2018-05-14 09:38:13 +0800 | [diff] [blame] | 3099 | * Check if the leaf is the last leaf. Which means all node pointers |
| 3100 | * are at their last position. |
| 3101 | */ |
| 3102 | static bool is_last_leaf(struct btrfs_path *path) |
| 3103 | { |
| 3104 | int i; |
| 3105 | |
| 3106 | for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) { |
| 3107 | if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1) |
| 3108 | return false; |
| 3109 | } |
| 3110 | return true; |
| 3111 | } |
| 3112 | |
| 3113 | /* |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3114 | * returns < 0 on error, 0 when more leafs are to be scanned. |
Qu Wenruo | 3393168 | 2015-02-27 16:24:24 +0800 | [diff] [blame] | 3115 | * returns 1 when done. |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3116 | */ |
Lu Fengqi | 62088ca | 2018-07-18 14:45:42 +0800 | [diff] [blame] | 3117 | static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans, |
| 3118 | struct btrfs_path *path) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3119 | { |
Lu Fengqi | 62088ca | 2018-07-18 14:45:42 +0800 | [diff] [blame] | 3120 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3121 | struct btrfs_key found; |
Qu Wenruo | 0a0e8b89 | 2015-10-26 09:19:43 +0800 | [diff] [blame] | 3122 | struct extent_buffer *scratch_leaf = NULL; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3123 | struct ulist *roots = NULL; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3124 | u64 num_bytes; |
Qu Wenruo | ff3d27a0 | 2018-05-14 09:38:13 +0800 | [diff] [blame] | 3125 | bool done; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3126 | int slot; |
| 3127 | int ret; |
| 3128 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3129 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3130 | ret = btrfs_search_slot_for_read(fs_info->extent_root, |
| 3131 | &fs_info->qgroup_rescan_progress, |
| 3132 | path, 1, 0); |
| 3133 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 3134 | btrfs_debug(fs_info, |
| 3135 | "current progress key (%llu %u %llu), search_slot ret %d", |
| 3136 | fs_info->qgroup_rescan_progress.objectid, |
| 3137 | fs_info->qgroup_rescan_progress.type, |
| 3138 | fs_info->qgroup_rescan_progress.offset, ret); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3139 | |
| 3140 | if (ret) { |
| 3141 | /* |
| 3142 | * The rescan is about to end, we will not be scanning any |
| 3143 | * further blocks. We cannot unset the RESCAN flag here, because |
| 3144 | * we want to commit the transaction if everything went well. |
| 3145 | * To make the live accounting work in this phase, we set our |
| 3146 | * scan progress pointer such that every real extent objectid |
| 3147 | * will be smaller. |
| 3148 | */ |
| 3149 | fs_info->qgroup_rescan_progress.objectid = (u64)-1; |
| 3150 | btrfs_release_path(path); |
| 3151 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3152 | return ret; |
| 3153 | } |
Qu Wenruo | ff3d27a0 | 2018-05-14 09:38:13 +0800 | [diff] [blame] | 3154 | done = is_last_leaf(path); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3155 | |
| 3156 | btrfs_item_key_to_cpu(path->nodes[0], &found, |
| 3157 | btrfs_header_nritems(path->nodes[0]) - 1); |
| 3158 | fs_info->qgroup_rescan_progress.objectid = found.objectid + 1; |
| 3159 | |
Qu Wenruo | 0a0e8b89 | 2015-10-26 09:19:43 +0800 | [diff] [blame] | 3160 | scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]); |
| 3161 | if (!scratch_leaf) { |
| 3162 | ret = -ENOMEM; |
| 3163 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3164 | goto out; |
| 3165 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3166 | slot = path->slots[0]; |
| 3167 | btrfs_release_path(path); |
| 3168 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3169 | |
| 3170 | for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) { |
| 3171 | btrfs_item_key_to_cpu(scratch_leaf, &found, slot); |
Josef Bacik | 3a6d75e | 2014-01-23 16:45:10 -0500 | [diff] [blame] | 3172 | if (found.type != BTRFS_EXTENT_ITEM_KEY && |
| 3173 | found.type != BTRFS_METADATA_ITEM_KEY) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3174 | continue; |
Josef Bacik | 3a6d75e | 2014-01-23 16:45:10 -0500 | [diff] [blame] | 3175 | if (found.type == BTRFS_METADATA_ITEM_KEY) |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 3176 | num_bytes = fs_info->nodesize; |
Josef Bacik | 3a6d75e | 2014-01-23 16:45:10 -0500 | [diff] [blame] | 3177 | else |
| 3178 | num_bytes = found.offset; |
| 3179 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3180 | ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 3181 | &roots, false); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3182 | if (ret < 0) |
| 3183 | goto out; |
Qu Wenruo | 9d220c9 | 2015-04-13 11:02:16 +0800 | [diff] [blame] | 3184 | /* For rescan, just pass old_roots as NULL */ |
Lu Fengqi | 8696d76 | 2018-07-18 14:45:39 +0800 | [diff] [blame] | 3185 | ret = btrfs_qgroup_account_extent(trans, found.objectid, |
| 3186 | num_bytes, NULL, roots); |
Qu Wenruo | 9d220c9 | 2015-04-13 11:02:16 +0800 | [diff] [blame] | 3187 | if (ret < 0) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3188 | goto out; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3189 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3190 | out: |
Nikolay Borisov | df44971 | 2018-08-15 18:26:56 +0300 | [diff] [blame] | 3191 | if (scratch_leaf) |
Qu Wenruo | 0a0e8b89 | 2015-10-26 09:19:43 +0800 | [diff] [blame] | 3192 | free_extent_buffer(scratch_leaf); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3193 | |
Qu Wenruo | 6f7de19 | 2018-06-27 18:19:55 +0800 | [diff] [blame] | 3194 | if (done && !ret) { |
Qu Wenruo | ff3d27a0 | 2018-05-14 09:38:13 +0800 | [diff] [blame] | 3195 | ret = 1; |
Qu Wenruo | 6f7de19 | 2018-06-27 18:19:55 +0800 | [diff] [blame] | 3196 | fs_info->qgroup_rescan_progress.objectid = (u64)-1; |
| 3197 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3198 | return ret; |
| 3199 | } |
| 3200 | |
Filipe Manana | cb13eea | 2020-12-14 10:10:45 +0000 | [diff] [blame] | 3201 | static bool rescan_should_stop(struct btrfs_fs_info *fs_info) |
| 3202 | { |
| 3203 | return btrfs_fs_closing(fs_info) || |
| 3204 | test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); |
| 3205 | } |
| 3206 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 3207 | static void btrfs_qgroup_rescan_worker(struct btrfs_work *work) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3208 | { |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3209 | struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info, |
| 3210 | qgroup_rescan_work); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3211 | struct btrfs_path *path; |
| 3212 | struct btrfs_trans_handle *trans = NULL; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3213 | int err = -ENOMEM; |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3214 | int ret = 0; |
Filipe Manana | cb13eea | 2020-12-14 10:10:45 +0000 | [diff] [blame] | 3215 | bool stopped = false; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3216 | |
| 3217 | path = btrfs_alloc_path(); |
| 3218 | if (!path) |
| 3219 | goto out; |
Qu Wenruo | b6debf1 | 2018-05-14 09:38:12 +0800 | [diff] [blame] | 3220 | /* |
| 3221 | * Rescan should only search for commit root, and any later difference |
| 3222 | * should be recorded by qgroup |
| 3223 | */ |
| 3224 | path->search_commit_root = 1; |
| 3225 | path->skip_locking = 1; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3226 | |
| 3227 | err = 0; |
Filipe Manana | cb13eea | 2020-12-14 10:10:45 +0000 | [diff] [blame] | 3228 | while (!err && !(stopped = rescan_should_stop(fs_info))) { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3229 | trans = btrfs_start_transaction(fs_info->fs_root, 0); |
| 3230 | if (IS_ERR(trans)) { |
| 3231 | err = PTR_ERR(trans); |
| 3232 | break; |
| 3233 | } |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3234 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3235 | err = -EINTR; |
| 3236 | } else { |
Lu Fengqi | 62088ca | 2018-07-18 14:45:42 +0800 | [diff] [blame] | 3237 | err = qgroup_rescan_leaf(trans, path); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3238 | } |
| 3239 | if (err > 0) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3240 | btrfs_commit_transaction(trans); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3241 | else |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3242 | btrfs_end_transaction(trans); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | out: |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3246 | btrfs_free_path(path); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3247 | |
| 3248 | mutex_lock(&fs_info->qgroup_rescan_lock); |
Qu Wenruo | 3393168 | 2015-02-27 16:24:24 +0800 | [diff] [blame] | 3249 | if (err > 0 && |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3250 | fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) { |
| 3251 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 3252 | } else if (err < 0) { |
| 3253 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 3254 | } |
| 3255 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3256 | |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3257 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3258 | * only update status, since the previous part has already updated the |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3259 | * qgroup info. |
| 3260 | */ |
| 3261 | trans = btrfs_start_transaction(fs_info->quota_root, 1); |
| 3262 | if (IS_ERR(trans)) { |
| 3263 | err = PTR_ERR(trans); |
Filipe Manana | 13fc1d2 | 2019-09-24 10:49:54 +0100 | [diff] [blame] | 3264 | trans = NULL; |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3265 | btrfs_err(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 3266 | "fail to start transaction for status update: %d", |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3267 | err); |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3268 | } |
Filipe Manana | 13fc1d2 | 2019-09-24 10:49:54 +0100 | [diff] [blame] | 3269 | |
| 3270 | mutex_lock(&fs_info->qgroup_rescan_lock); |
Filipe Manana | cb13eea | 2020-12-14 10:10:45 +0000 | [diff] [blame] | 3271 | if (!stopped) |
Filipe Manana | 13fc1d2 | 2019-09-24 10:49:54 +0100 | [diff] [blame] | 3272 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3273 | if (trans) { |
| 3274 | ret = update_qgroup_status_item(trans); |
| 3275 | if (ret < 0) { |
| 3276 | err = ret; |
| 3277 | btrfs_err(fs_info, "fail to update qgroup status: %d", |
| 3278 | err); |
| 3279 | } |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3280 | } |
Filipe Manana | 13fc1d2 | 2019-09-24 10:49:54 +0100 | [diff] [blame] | 3281 | fs_info->qgroup_rescan_running = false; |
| 3282 | complete_all(&fs_info->qgroup_rescan_completion); |
| 3283 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3284 | |
| 3285 | if (!trans) |
| 3286 | return; |
| 3287 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3288 | btrfs_end_transaction(trans); |
Qu Wenruo | 53b7cde | 2015-02-27 16:24:25 +0800 | [diff] [blame] | 3289 | |
Filipe Manana | cb13eea | 2020-12-14 10:10:45 +0000 | [diff] [blame] | 3290 | if (stopped) { |
Justin Maggard | 7343dd6 | 2015-11-04 15:56:16 -0800 | [diff] [blame] | 3291 | btrfs_info(fs_info, "qgroup scan paused"); |
| 3292 | } else if (err >= 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3293 | btrfs_info(fs_info, "qgroup scan completed%s", |
Qu Wenruo | 3393168 | 2015-02-27 16:24:24 +0800 | [diff] [blame] | 3294 | err > 0 ? " (inconsistency flag cleared)" : ""); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3295 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3296 | btrfs_err(fs_info, "qgroup scan failed with %d", err); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3300 | /* |
| 3301 | * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all |
| 3302 | * memory required for the rescan context. |
| 3303 | */ |
| 3304 | static int |
| 3305 | qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid, |
| 3306 | int init_flags) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3307 | { |
| 3308 | int ret = 0; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3309 | |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3310 | if (!init_flags) { |
| 3311 | /* we're resuming qgroup rescan at mount time */ |
Filipe Manana | e4e7ede | 2018-06-27 00:43:15 +0100 | [diff] [blame] | 3312 | if (!(fs_info->qgroup_flags & |
| 3313 | BTRFS_QGROUP_STATUS_FLAG_RESCAN)) { |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3314 | btrfs_warn(fs_info, |
Nikolay Borisov | 37d0259 | 2019-11-18 14:16:44 +0200 | [diff] [blame] | 3315 | "qgroup rescan init failed, qgroup rescan is not queued"); |
Filipe Manana | e4e7ede | 2018-06-27 00:43:15 +0100 | [diff] [blame] | 3316 | ret = -EINVAL; |
| 3317 | } else if (!(fs_info->qgroup_flags & |
| 3318 | BTRFS_QGROUP_STATUS_FLAG_ON)) { |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3319 | btrfs_warn(fs_info, |
Nikolay Borisov | 37d0259 | 2019-11-18 14:16:44 +0200 | [diff] [blame] | 3320 | "qgroup rescan init failed, qgroup is not enabled"); |
Filipe Manana | e4e7ede | 2018-06-27 00:43:15 +0100 | [diff] [blame] | 3321 | ret = -EINVAL; |
| 3322 | } |
| 3323 | |
| 3324 | if (ret) |
| 3325 | return ret; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3326 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3327 | |
| 3328 | mutex_lock(&fs_info->qgroup_rescan_lock); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3329 | |
| 3330 | if (init_flags) { |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3331 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
| 3332 | btrfs_warn(fs_info, |
| 3333 | "qgroup rescan is already in progress"); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3334 | ret = -EINPROGRESS; |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3335 | } else if (!(fs_info->qgroup_flags & |
| 3336 | BTRFS_QGROUP_STATUS_FLAG_ON)) { |
| 3337 | btrfs_warn(fs_info, |
| 3338 | "qgroup rescan init failed, qgroup is not enabled"); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3339 | ret = -EINVAL; |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3340 | } |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3341 | |
| 3342 | if (ret) { |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3343 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
Qu Wenruo | 9593bf49 | 2018-05-02 13:28:03 +0800 | [diff] [blame] | 3344 | return ret; |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3345 | } |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3346 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3347 | } |
| 3348 | |
| 3349 | memset(&fs_info->qgroup_rescan_progress, 0, |
| 3350 | sizeof(fs_info->qgroup_rescan_progress)); |
| 3351 | fs_info->qgroup_rescan_progress.objectid = progress_objectid; |
Filipe Manana | 190631f | 2015-11-05 10:06:23 +0000 | [diff] [blame] | 3352 | init_completion(&fs_info->qgroup_rescan_completion); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3353 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3354 | |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3355 | btrfs_init_work(&fs_info->qgroup_rescan_work, |
| 3356 | btrfs_qgroup_rescan_worker, NULL, NULL); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3357 | return 0; |
| 3358 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3359 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3360 | static void |
| 3361 | qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info) |
| 3362 | { |
| 3363 | struct rb_node *n; |
| 3364 | struct btrfs_qgroup *qgroup; |
| 3365 | |
| 3366 | spin_lock(&fs_info->qgroup_lock); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3367 | /* clear all current qgroup tracking information */ |
| 3368 | for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) { |
| 3369 | qgroup = rb_entry(n, struct btrfs_qgroup, node); |
| 3370 | qgroup->rfer = 0; |
| 3371 | qgroup->rfer_cmpr = 0; |
| 3372 | qgroup->excl = 0; |
| 3373 | qgroup->excl_cmpr = 0; |
Qu Wenruo | 9c7b0c2 | 2018-08-10 10:20:26 +0800 | [diff] [blame] | 3374 | qgroup_dirty(fs_info, qgroup); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3375 | } |
| 3376 | spin_unlock(&fs_info->qgroup_lock); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3377 | } |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3378 | |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3379 | int |
| 3380 | btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info) |
| 3381 | { |
| 3382 | int ret = 0; |
| 3383 | struct btrfs_trans_handle *trans; |
| 3384 | |
| 3385 | ret = qgroup_rescan_init(fs_info, 0, 1); |
| 3386 | if (ret) |
| 3387 | return ret; |
| 3388 | |
| 3389 | /* |
| 3390 | * We have set the rescan_progress to 0, which means no more |
| 3391 | * delayed refs will be accounted by btrfs_qgroup_account_ref. |
| 3392 | * However, btrfs_qgroup_account_ref may be right after its call |
| 3393 | * to btrfs_find_all_roots, in which case it would still do the |
| 3394 | * accounting. |
| 3395 | * To solve this, we're committing the transaction, which will |
| 3396 | * ensure we run all delayed refs and only after that, we are |
| 3397 | * going to clear all tracking information for a clean start. |
| 3398 | */ |
| 3399 | |
| 3400 | trans = btrfs_join_transaction(fs_info->fs_root); |
| 3401 | if (IS_ERR(trans)) { |
| 3402 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3403 | return PTR_ERR(trans); |
| 3404 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3405 | ret = btrfs_commit_transaction(trans); |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3406 | if (ret) { |
| 3407 | fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN; |
| 3408 | return ret; |
| 3409 | } |
| 3410 | |
| 3411 | qgroup_rescan_zero_tracking(fs_info); |
| 3412 | |
Qu Wenruo | d61acbb | 2020-02-07 13:38:20 +0800 | [diff] [blame] | 3413 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3414 | fs_info->qgroup_rescan_running = true; |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3415 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
| 3416 | &fs_info->qgroup_rescan_work); |
Qu Wenruo | d61acbb | 2020-02-07 13:38:20 +0800 | [diff] [blame] | 3417 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 3418 | |
| 3419 | return 0; |
| 3420 | } |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3421 | |
Jeff Mahoney | d06f23d | 2016-08-08 22:08:06 -0400 | [diff] [blame] | 3422 | int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info, |
| 3423 | bool interruptible) |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3424 | { |
| 3425 | int running; |
| 3426 | int ret = 0; |
| 3427 | |
| 3428 | mutex_lock(&fs_info->qgroup_rescan_lock); |
Jeff Mahoney | d2c609b | 2016-08-15 12:10:33 -0400 | [diff] [blame] | 3429 | running = fs_info->qgroup_rescan_running; |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3430 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3431 | |
Jeff Mahoney | d06f23d | 2016-08-08 22:08:06 -0400 | [diff] [blame] | 3432 | if (!running) |
| 3433 | return 0; |
| 3434 | |
| 3435 | if (interruptible) |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3436 | ret = wait_for_completion_interruptible( |
| 3437 | &fs_info->qgroup_rescan_completion); |
Jeff Mahoney | d06f23d | 2016-08-08 22:08:06 -0400 | [diff] [blame] | 3438 | else |
| 3439 | wait_for_completion(&fs_info->qgroup_rescan_completion); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 3440 | |
| 3441 | return ret; |
| 3442 | } |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3443 | |
| 3444 | /* |
| 3445 | * this is only called from open_ctree where we're still single threaded, thus |
| 3446 | * locking is omitted here. |
| 3447 | */ |
| 3448 | void |
| 3449 | btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info) |
| 3450 | { |
Qu Wenruo | d61acbb | 2020-02-07 13:38:20 +0800 | [diff] [blame] | 3451 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
| 3452 | mutex_lock(&fs_info->qgroup_rescan_lock); |
| 3453 | fs_info->qgroup_rescan_running = true; |
Qu Wenruo | fc97fab | 2014-02-28 10:46:16 +0800 | [diff] [blame] | 3454 | btrfs_queue_work(fs_info->qgroup_rescan_workers, |
| 3455 | &fs_info->qgroup_rescan_work); |
Qu Wenruo | d61acbb | 2020-02-07 13:38:20 +0800 | [diff] [blame] | 3456 | mutex_unlock(&fs_info->qgroup_rescan_lock); |
| 3457 | } |
Jan Schmidt | b382a32 | 2013-05-28 15:47:24 +0000 | [diff] [blame] | 3458 | } |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3459 | |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3460 | #define rbtree_iterate_from_safe(node, next, start) \ |
| 3461 | for (node = start; node && ({ next = rb_next(node); 1;}); node = next) |
| 3462 | |
| 3463 | static int qgroup_unreserve_range(struct btrfs_inode *inode, |
| 3464 | struct extent_changeset *reserved, u64 start, |
| 3465 | u64 len) |
| 3466 | { |
| 3467 | struct rb_node *node; |
| 3468 | struct rb_node *next; |
Dan Carpenter | f07728d | 2020-10-23 14:26:33 +0300 | [diff] [blame] | 3469 | struct ulist_node *entry; |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3470 | int ret = 0; |
| 3471 | |
| 3472 | node = reserved->range_changed.root.rb_node; |
Dan Carpenter | f07728d | 2020-10-23 14:26:33 +0300 | [diff] [blame] | 3473 | if (!node) |
| 3474 | return 0; |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3475 | while (node) { |
| 3476 | entry = rb_entry(node, struct ulist_node, rb_node); |
| 3477 | if (entry->val < start) |
| 3478 | node = node->rb_right; |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3479 | else |
Dan Carpenter | f07728d | 2020-10-23 14:26:33 +0300 | [diff] [blame] | 3480 | node = node->rb_left; |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3481 | } |
| 3482 | |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3483 | if (entry->val > start && rb_prev(&entry->rb_node)) |
| 3484 | entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node, |
| 3485 | rb_node); |
| 3486 | |
| 3487 | rbtree_iterate_from_safe(node, next, &entry->rb_node) { |
| 3488 | u64 entry_start; |
| 3489 | u64 entry_end; |
| 3490 | u64 entry_len; |
| 3491 | int clear_ret; |
| 3492 | |
| 3493 | entry = rb_entry(node, struct ulist_node, rb_node); |
| 3494 | entry_start = entry->val; |
| 3495 | entry_end = entry->aux; |
| 3496 | entry_len = entry_end - entry_start + 1; |
| 3497 | |
| 3498 | if (entry_start >= start + len) |
| 3499 | break; |
| 3500 | if (entry_start + entry_len <= start) |
| 3501 | continue; |
| 3502 | /* |
| 3503 | * Now the entry is in [start, start + len), revert the |
| 3504 | * EXTENT_QGROUP_RESERVED bit. |
| 3505 | */ |
| 3506 | clear_ret = clear_extent_bits(&inode->io_tree, entry_start, |
| 3507 | entry_end, EXTENT_QGROUP_RESERVED); |
| 3508 | if (!ret && clear_ret < 0) |
| 3509 | ret = clear_ret; |
| 3510 | |
| 3511 | ulist_del(&reserved->range_changed, entry->val, entry->aux); |
| 3512 | if (likely(reserved->bytes_changed >= entry_len)) { |
| 3513 | reserved->bytes_changed -= entry_len; |
| 3514 | } else { |
| 3515 | WARN_ON(1); |
| 3516 | reserved->bytes_changed = 0; |
| 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | return ret; |
| 3521 | } |
| 3522 | |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3523 | /* |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3524 | * Try to free some space for qgroup. |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3525 | * |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3526 | * For qgroup, there are only 3 ways to free qgroup space: |
| 3527 | * - Flush nodatacow write |
| 3528 | * Any nodatacow write will free its reserved data space at run_delalloc_range(). |
| 3529 | * In theory, we should only flush nodatacow inodes, but it's not yet |
| 3530 | * possible, so we need to flush the whole root. |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3531 | * |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3532 | * - Wait for ordered extents |
| 3533 | * When ordered extents are finished, their reserved metadata is finally |
| 3534 | * converted to per_trans status, which can be freed by later commit |
| 3535 | * transaction. |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3536 | * |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3537 | * - Commit transaction |
| 3538 | * This would free the meta_per_trans space. |
| 3539 | * In theory this shouldn't provide much space, but any more qgroup space |
| 3540 | * is needed. |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3541 | */ |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3542 | static int try_flush_qgroup(struct btrfs_root *root) |
| 3543 | { |
| 3544 | struct btrfs_trans_handle *trans; |
| 3545 | int ret; |
| 3546 | |
Nikolay Borisov | ae396a3 | 2021-02-22 18:40:45 +0200 | [diff] [blame] | 3547 | /* Can't hold an open transaction or we run the risk of deadlocking */ |
| 3548 | ASSERT(current->journal_info == NULL || |
| 3549 | current->journal_info == BTRFS_SEND_TRANS_STUB); |
| 3550 | if (WARN_ON(current->journal_info && |
| 3551 | current->journal_info != BTRFS_SEND_TRANS_STUB)) |
| 3552 | return 0; |
Qu Wenruo | 6f23277 | 2020-11-11 19:38:18 +0800 | [diff] [blame] | 3553 | |
Qu Wenruo | ae5e070 | 2020-12-04 09:24:47 +0800 | [diff] [blame] | 3554 | /* |
| 3555 | * We don't want to run flush again and again, so if there is a running |
| 3556 | * one, we won't try to start a new flush, but exit directly. |
| 3557 | */ |
| 3558 | if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) { |
Qu Wenruo | ae5e070 | 2020-12-04 09:24:47 +0800 | [diff] [blame] | 3559 | wait_event(root->qgroup_flush_wait, |
| 3560 | !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)); |
| 3561 | return 0; |
| 3562 | } |
| 3563 | |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3564 | ret = btrfs_start_delalloc_snapshot(root); |
| 3565 | if (ret < 0) |
| 3566 | goto out; |
| 3567 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); |
| 3568 | |
| 3569 | trans = btrfs_join_transaction(root); |
| 3570 | if (IS_ERR(trans)) { |
| 3571 | ret = PTR_ERR(trans); |
| 3572 | goto out; |
| 3573 | } |
| 3574 | |
Nikolay Borisov | ae396a3 | 2021-02-22 18:40:45 +0200 | [diff] [blame] | 3575 | ret = btrfs_commit_transaction(trans); |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3576 | out: |
| 3577 | clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state); |
| 3578 | wake_up(&root->qgroup_flush_wait); |
| 3579 | return ret; |
| 3580 | } |
| 3581 | |
| 3582 | static int qgroup_reserve_data(struct btrfs_inode *inode, |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3583 | struct extent_changeset **reserved_ret, u64 start, |
| 3584 | u64 len) |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3585 | { |
Nikolay Borisov | 7661a3e | 2020-06-03 08:55:37 +0300 | [diff] [blame] | 3586 | struct btrfs_root *root = inode->root; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3587 | struct extent_changeset *reserved; |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3588 | bool new_reserved = false; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3589 | u64 orig_reserved; |
| 3590 | u64 to_reserve; |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3591 | int ret; |
| 3592 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 3593 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3594 | !is_fstree(root->root_key.objectid) || len == 0) |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3595 | return 0; |
| 3596 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3597 | /* @reserved parameter is mandatory for qgroup */ |
| 3598 | if (WARN_ON(!reserved_ret)) |
| 3599 | return -EINVAL; |
| 3600 | if (!*reserved_ret) { |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3601 | new_reserved = true; |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3602 | *reserved_ret = extent_changeset_alloc(); |
| 3603 | if (!*reserved_ret) |
| 3604 | return -ENOMEM; |
| 3605 | } |
| 3606 | reserved = *reserved_ret; |
| 3607 | /* Record already reserved space */ |
| 3608 | orig_reserved = reserved->bytes_changed; |
Nikolay Borisov | 7661a3e | 2020-06-03 08:55:37 +0300 | [diff] [blame] | 3609 | ret = set_record_extent_bits(&inode->io_tree, start, |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3610 | start + len -1, EXTENT_QGROUP_RESERVED, reserved); |
| 3611 | |
| 3612 | /* Newly reserved space */ |
| 3613 | to_reserve = reserved->bytes_changed - orig_reserved; |
Nikolay Borisov | 7661a3e | 2020-06-03 08:55:37 +0300 | [diff] [blame] | 3614 | trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len, |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3615 | to_reserve, QGROUP_RESERVE); |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3616 | if (ret < 0) |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3617 | goto out; |
Qu Wenruo | dba2132 | 2017-12-12 15:34:25 +0800 | [diff] [blame] | 3618 | ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA); |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3619 | if (ret < 0) |
| 3620 | goto cleanup; |
| 3621 | |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3622 | return ret; |
| 3623 | |
| 3624 | cleanup: |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3625 | qgroup_unreserve_range(inode, reserved, start, len); |
| 3626 | out: |
| 3627 | if (new_reserved) { |
Nikolay Borisov | d6ade68 | 2021-03-02 12:44:40 +0200 | [diff] [blame^] | 3628 | extent_changeset_free(reserved); |
Qu Wenruo | 263da81 | 2020-07-08 14:24:45 +0800 | [diff] [blame] | 3629 | *reserved_ret = NULL; |
| 3630 | } |
Qu Wenruo | 5247255 | 2015-10-12 16:05:40 +0800 | [diff] [blame] | 3631 | return ret; |
| 3632 | } |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3633 | |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3634 | /* |
| 3635 | * Reserve qgroup space for range [start, start + len). |
| 3636 | * |
| 3637 | * This function will either reserve space from related qgroups or do nothing |
| 3638 | * if the range is already reserved. |
| 3639 | * |
| 3640 | * Return 0 for successful reservation |
| 3641 | * Return <0 for error (including -EQUOT) |
| 3642 | * |
| 3643 | * NOTE: This function may sleep for memory allocation, dirty page flushing and |
| 3644 | * commit transaction. So caller should not hold any dirty page locked. |
| 3645 | */ |
| 3646 | int btrfs_qgroup_reserve_data(struct btrfs_inode *inode, |
| 3647 | struct extent_changeset **reserved_ret, u64 start, |
| 3648 | u64 len) |
| 3649 | { |
| 3650 | int ret; |
| 3651 | |
| 3652 | ret = qgroup_reserve_data(inode, reserved_ret, start, len); |
| 3653 | if (ret <= 0 && ret != -EDQUOT) |
| 3654 | return ret; |
| 3655 | |
| 3656 | ret = try_flush_qgroup(inode->root); |
| 3657 | if (ret < 0) |
| 3658 | return ret; |
| 3659 | return qgroup_reserve_data(inode, reserved_ret, start, len); |
| 3660 | } |
| 3661 | |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3662 | /* Free ranges specified by @reserved, normally in error path */ |
Nikolay Borisov | df2cfd1 | 2020-06-03 08:55:09 +0300 | [diff] [blame] | 3663 | static int qgroup_free_reserved_data(struct btrfs_inode *inode, |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3664 | struct extent_changeset *reserved, u64 start, u64 len) |
| 3665 | { |
Nikolay Borisov | df2cfd1 | 2020-06-03 08:55:09 +0300 | [diff] [blame] | 3666 | struct btrfs_root *root = inode->root; |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3667 | struct ulist_node *unode; |
| 3668 | struct ulist_iterator uiter; |
| 3669 | struct extent_changeset changeset; |
| 3670 | int freed = 0; |
| 3671 | int ret; |
| 3672 | |
| 3673 | extent_changeset_init(&changeset); |
| 3674 | len = round_up(start + len, root->fs_info->sectorsize); |
| 3675 | start = round_down(start, root->fs_info->sectorsize); |
| 3676 | |
| 3677 | ULIST_ITER_INIT(&uiter); |
| 3678 | while ((unode = ulist_next(&reserved->range_changed, &uiter))) { |
| 3679 | u64 range_start = unode->val; |
| 3680 | /* unode->aux is the inclusive end */ |
| 3681 | u64 range_len = unode->aux - range_start + 1; |
| 3682 | u64 free_start; |
| 3683 | u64 free_len; |
| 3684 | |
| 3685 | extent_changeset_release(&changeset); |
| 3686 | |
| 3687 | /* Only free range in range [start, start + len) */ |
| 3688 | if (range_start >= start + len || |
| 3689 | range_start + range_len <= start) |
| 3690 | continue; |
| 3691 | free_start = max(range_start, start); |
| 3692 | free_len = min(start + len, range_start + range_len) - |
| 3693 | free_start; |
| 3694 | /* |
| 3695 | * TODO: To also modify reserved->ranges_reserved to reflect |
| 3696 | * the modification. |
| 3697 | * |
| 3698 | * However as long as we free qgroup reserved according to |
| 3699 | * EXTENT_QGROUP_RESERVED, we won't double free. |
| 3700 | * So not need to rush. |
| 3701 | */ |
Nikolay Borisov | df2cfd1 | 2020-06-03 08:55:09 +0300 | [diff] [blame] | 3702 | ret = clear_record_extent_bits(&inode->io_tree, free_start, |
| 3703 | free_start + free_len - 1, |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3704 | EXTENT_QGROUP_RESERVED, &changeset); |
| 3705 | if (ret < 0) |
| 3706 | goto out; |
| 3707 | freed += changeset.bytes_changed; |
| 3708 | } |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3709 | btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed, |
Qu Wenruo | d4e5c92 | 2017-12-12 15:34:23 +0800 | [diff] [blame] | 3710 | BTRFS_QGROUP_RSV_DATA); |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3711 | ret = freed; |
| 3712 | out: |
| 3713 | extent_changeset_release(&changeset); |
| 3714 | return ret; |
| 3715 | } |
| 3716 | |
Nikolay Borisov | 8769af9 | 2020-06-03 08:55:10 +0300 | [diff] [blame] | 3717 | static int __btrfs_qgroup_release_data(struct btrfs_inode *inode, |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3718 | struct extent_changeset *reserved, u64 start, u64 len, |
| 3719 | int free) |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3720 | { |
| 3721 | struct extent_changeset changeset; |
Qu Wenruo | 81fb6f7 | 2015-09-28 16:57:53 +0800 | [diff] [blame] | 3722 | int trace_op = QGROUP_RELEASE; |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3723 | int ret; |
| 3724 | |
Nikolay Borisov | 8769af9 | 2020-06-03 08:55:10 +0300 | [diff] [blame] | 3725 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags)) |
Qu Wenruo | 3628b4c | 2018-10-09 14:36:45 +0800 | [diff] [blame] | 3726 | return 0; |
| 3727 | |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3728 | /* In release case, we shouldn't have @reserved */ |
| 3729 | WARN_ON(!free && reserved); |
| 3730 | if (free && reserved) |
Nikolay Borisov | 8769af9 | 2020-06-03 08:55:10 +0300 | [diff] [blame] | 3731 | return qgroup_free_reserved_data(inode, reserved, start, len); |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3732 | extent_changeset_init(&changeset); |
Nikolay Borisov | 8769af9 | 2020-06-03 08:55:10 +0300 | [diff] [blame] | 3733 | ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1, |
| 3734 | EXTENT_QGROUP_RESERVED, &changeset); |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3735 | if (ret < 0) |
| 3736 | goto out; |
| 3737 | |
Qu Wenruo | d51ea5d | 2017-03-13 15:52:09 +0800 | [diff] [blame] | 3738 | if (free) |
| 3739 | trace_op = QGROUP_FREE; |
Nikolay Borisov | 8769af9 | 2020-06-03 08:55:10 +0300 | [diff] [blame] | 3740 | trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len, |
Qu Wenruo | d51ea5d | 2017-03-13 15:52:09 +0800 | [diff] [blame] | 3741 | changeset.bytes_changed, trace_op); |
| 3742 | if (free) |
Nikolay Borisov | 8769af9 | 2020-06-03 08:55:10 +0300 | [diff] [blame] | 3743 | btrfs_qgroup_free_refroot(inode->root->fs_info, |
| 3744 | inode->root->root_key.objectid, |
Qu Wenruo | d4e5c92 | 2017-12-12 15:34:23 +0800 | [diff] [blame] | 3745 | changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA); |
Qu Wenruo | 7bc329c | 2017-02-27 15:10:36 +0800 | [diff] [blame] | 3746 | ret = changeset.bytes_changed; |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3747 | out: |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3748 | extent_changeset_release(&changeset); |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3749 | return ret; |
| 3750 | } |
| 3751 | |
| 3752 | /* |
| 3753 | * Free a reserved space range from io_tree and related qgroups |
| 3754 | * |
| 3755 | * Should be called when a range of pages get invalidated before reaching disk. |
| 3756 | * Or for error cleanup case. |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3757 | * if @reserved is given, only reserved range in [@start, @start + @len) will |
| 3758 | * be freed. |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3759 | * |
| 3760 | * For data written to disk, use btrfs_qgroup_release_data(). |
| 3761 | * |
| 3762 | * NOTE: This function may sleep for memory allocation. |
| 3763 | */ |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 3764 | int btrfs_qgroup_free_data(struct btrfs_inode *inode, |
Qu Wenruo | bc42bda | 2017-02-27 15:10:39 +0800 | [diff] [blame] | 3765 | struct extent_changeset *reserved, u64 start, u64 len) |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3766 | { |
Nikolay Borisov | 8b8a979 | 2020-06-03 08:55:11 +0300 | [diff] [blame] | 3767 | return __btrfs_qgroup_release_data(inode, reserved, start, len, 1); |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | /* |
| 3771 | * Release a reserved space range from io_tree only. |
| 3772 | * |
| 3773 | * Should be called when a range of pages get written to disk and corresponding |
| 3774 | * FILE_EXTENT is inserted into corresponding root. |
| 3775 | * |
| 3776 | * Since new qgroup accounting framework will only update qgroup numbers at |
| 3777 | * commit_transaction() time, its reserved space shouldn't be freed from |
| 3778 | * related qgroups. |
| 3779 | * |
| 3780 | * But we should release the range from io_tree, to allow further write to be |
| 3781 | * COWed. |
| 3782 | * |
| 3783 | * NOTE: This function may sleep for memory allocation. |
| 3784 | */ |
Nikolay Borisov | 72b7d15 | 2020-06-03 08:55:18 +0300 | [diff] [blame] | 3785 | int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len) |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3786 | { |
Nikolay Borisov | 72b7d15 | 2020-06-03 08:55:18 +0300 | [diff] [blame] | 3787 | return __btrfs_qgroup_release_data(inode, NULL, start, len, 0); |
Qu Wenruo | f695fdc | 2015-10-12 16:28:06 +0800 | [diff] [blame] | 3788 | } |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3789 | |
Qu Wenruo | 8287475 | 2017-12-12 15:34:34 +0800 | [diff] [blame] | 3790 | static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes, |
| 3791 | enum btrfs_qgroup_rsv_type type) |
| 3792 | { |
| 3793 | if (type != BTRFS_QGROUP_RSV_META_PREALLOC && |
| 3794 | type != BTRFS_QGROUP_RSV_META_PERTRANS) |
| 3795 | return; |
| 3796 | if (num_bytes == 0) |
| 3797 | return; |
| 3798 | |
| 3799 | spin_lock(&root->qgroup_meta_rsv_lock); |
| 3800 | if (type == BTRFS_QGROUP_RSV_META_PREALLOC) |
| 3801 | root->qgroup_meta_rsv_prealloc += num_bytes; |
| 3802 | else |
| 3803 | root->qgroup_meta_rsv_pertrans += num_bytes; |
| 3804 | spin_unlock(&root->qgroup_meta_rsv_lock); |
| 3805 | } |
| 3806 | |
| 3807 | static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes, |
| 3808 | enum btrfs_qgroup_rsv_type type) |
| 3809 | { |
| 3810 | if (type != BTRFS_QGROUP_RSV_META_PREALLOC && |
| 3811 | type != BTRFS_QGROUP_RSV_META_PERTRANS) |
| 3812 | return 0; |
| 3813 | if (num_bytes == 0) |
| 3814 | return 0; |
| 3815 | |
| 3816 | spin_lock(&root->qgroup_meta_rsv_lock); |
| 3817 | if (type == BTRFS_QGROUP_RSV_META_PREALLOC) { |
| 3818 | num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc, |
| 3819 | num_bytes); |
| 3820 | root->qgroup_meta_rsv_prealloc -= num_bytes; |
| 3821 | } else { |
| 3822 | num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans, |
| 3823 | num_bytes); |
| 3824 | root->qgroup_meta_rsv_pertrans -= num_bytes; |
| 3825 | } |
| 3826 | spin_unlock(&root->qgroup_meta_rsv_lock); |
| 3827 | return num_bytes; |
| 3828 | } |
| 3829 | |
Nikolay Borisov | 80e9bae | 2021-02-22 18:40:43 +0200 | [diff] [blame] | 3830 | int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, |
| 3831 | enum btrfs_qgroup_rsv_type type, bool enforce) |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3832 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3833 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3834 | int ret; |
| 3835 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3836 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3837 | !is_fstree(root->root_key.objectid) || num_bytes == 0) |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3838 | return 0; |
| 3839 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3840 | BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize)); |
Qu Wenruo | fd2b007 | 2019-10-17 10:38:36 +0800 | [diff] [blame] | 3841 | trace_qgroup_meta_reserve(root, (s64)num_bytes, type); |
Qu Wenruo | 733e03a | 2017-12-12 15:34:29 +0800 | [diff] [blame] | 3842 | ret = qgroup_reserve(root, num_bytes, enforce, type); |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3843 | if (ret < 0) |
| 3844 | return ret; |
Qu Wenruo | 8287475 | 2017-12-12 15:34:34 +0800 | [diff] [blame] | 3845 | /* |
| 3846 | * Record what we have reserved into root. |
| 3847 | * |
| 3848 | * To avoid quota disabled->enabled underflow. |
| 3849 | * In that case, we may try to free space we haven't reserved |
| 3850 | * (since quota was disabled), so record what we reserved into root. |
| 3851 | * And ensure later release won't underflow this number. |
| 3852 | */ |
| 3853 | add_root_meta_rsv(root, num_bytes, type); |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3854 | return ret; |
| 3855 | } |
| 3856 | |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3857 | int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes, |
| 3858 | enum btrfs_qgroup_rsv_type type, bool enforce) |
| 3859 | { |
| 3860 | int ret; |
| 3861 | |
Nikolay Borisov | 80e9bae | 2021-02-22 18:40:43 +0200 | [diff] [blame] | 3862 | ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce); |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3863 | if (ret <= 0 && ret != -EDQUOT) |
| 3864 | return ret; |
| 3865 | |
| 3866 | ret = try_flush_qgroup(root); |
| 3867 | if (ret < 0) |
| 3868 | return ret; |
Nikolay Borisov | 80e9bae | 2021-02-22 18:40:43 +0200 | [diff] [blame] | 3869 | return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce); |
Qu Wenruo | c53e965 | 2020-07-13 18:50:48 +0800 | [diff] [blame] | 3870 | } |
| 3871 | |
Qu Wenruo | 733e03a | 2017-12-12 15:34:29 +0800 | [diff] [blame] | 3872 | void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root) |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3873 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3874 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3875 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3876 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3877 | !is_fstree(root->root_key.objectid)) |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3878 | return; |
| 3879 | |
Qu Wenruo | e1211d0 | 2017-12-12 15:34:30 +0800 | [diff] [blame] | 3880 | /* TODO: Update trace point to handle such free */ |
Qu Wenruo | 4ee0d88 | 2017-12-12 15:34:35 +0800 | [diff] [blame] | 3881 | trace_qgroup_meta_free_all_pertrans(root); |
Qu Wenruo | e1211d0 | 2017-12-12 15:34:30 +0800 | [diff] [blame] | 3882 | /* Special value -1 means to free all reserved space */ |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3883 | btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1, |
Qu Wenruo | 733e03a | 2017-12-12 15:34:29 +0800 | [diff] [blame] | 3884 | BTRFS_QGROUP_RSV_META_PERTRANS); |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3885 | } |
| 3886 | |
Qu Wenruo | 733e03a | 2017-12-12 15:34:29 +0800 | [diff] [blame] | 3887 | void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes, |
| 3888 | enum btrfs_qgroup_rsv_type type) |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3889 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3890 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3891 | |
| 3892 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3893 | !is_fstree(root->root_key.objectid)) |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3894 | return; |
| 3895 | |
Qu Wenruo | 8287475 | 2017-12-12 15:34:34 +0800 | [diff] [blame] | 3896 | /* |
| 3897 | * reservation for META_PREALLOC can happen before quota is enabled, |
| 3898 | * which can lead to underflow. |
| 3899 | * Here ensure we will only free what we really have reserved. |
| 3900 | */ |
| 3901 | num_bytes = sub_root_meta_rsv(root, num_bytes, type); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3902 | BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize)); |
Qu Wenruo | fd2b007 | 2019-10-17 10:38:36 +0800 | [diff] [blame] | 3903 | trace_qgroup_meta_reserve(root, -(s64)num_bytes, type); |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3904 | btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, |
| 3905 | num_bytes, type); |
Qu Wenruo | 55eeaf0 | 2015-09-08 17:08:38 +0800 | [diff] [blame] | 3906 | } |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3907 | |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3908 | static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root, |
| 3909 | int num_bytes) |
| 3910 | { |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3911 | struct btrfs_qgroup *qgroup; |
| 3912 | struct ulist_node *unode; |
| 3913 | struct ulist_iterator uiter; |
| 3914 | int ret = 0; |
| 3915 | |
| 3916 | if (num_bytes == 0) |
| 3917 | return; |
Marcos Paulo de Souza | e3b0edd | 2019-11-25 21:58:50 -0300 | [diff] [blame] | 3918 | if (!fs_info->quota_root) |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3919 | return; |
| 3920 | |
| 3921 | spin_lock(&fs_info->qgroup_lock); |
| 3922 | qgroup = find_qgroup_rb(fs_info, ref_root); |
| 3923 | if (!qgroup) |
| 3924 | goto out; |
| 3925 | ulist_reinit(fs_info->qgroup_ulist); |
| 3926 | ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid, |
David Sterba | a1840b5 | 2018-03-27 19:04:50 +0200 | [diff] [blame] | 3927 | qgroup_to_aux(qgroup), GFP_ATOMIC); |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3928 | if (ret < 0) |
| 3929 | goto out; |
| 3930 | ULIST_ITER_INIT(&uiter); |
| 3931 | while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) { |
| 3932 | struct btrfs_qgroup *qg; |
| 3933 | struct btrfs_qgroup_list *glist; |
| 3934 | |
| 3935 | qg = unode_aux_to_qgroup(unode); |
| 3936 | |
| 3937 | qgroup_rsv_release(fs_info, qg, num_bytes, |
| 3938 | BTRFS_QGROUP_RSV_META_PREALLOC); |
| 3939 | qgroup_rsv_add(fs_info, qg, num_bytes, |
| 3940 | BTRFS_QGROUP_RSV_META_PERTRANS); |
| 3941 | list_for_each_entry(glist, &qg->groups, next_group) { |
| 3942 | ret = ulist_add(fs_info->qgroup_ulist, |
| 3943 | glist->group->qgroupid, |
David Sterba | a1840b5 | 2018-03-27 19:04:50 +0200 | [diff] [blame] | 3944 | qgroup_to_aux(glist->group), GFP_ATOMIC); |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3945 | if (ret < 0) |
| 3946 | goto out; |
| 3947 | } |
| 3948 | } |
| 3949 | out: |
| 3950 | spin_unlock(&fs_info->qgroup_lock); |
| 3951 | } |
| 3952 | |
| 3953 | void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes) |
| 3954 | { |
| 3955 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3956 | |
| 3957 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) || |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3958 | !is_fstree(root->root_key.objectid)) |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3959 | return; |
Qu Wenruo | 8287475 | 2017-12-12 15:34:34 +0800 | [diff] [blame] | 3960 | /* Same as btrfs_qgroup_free_meta_prealloc() */ |
| 3961 | num_bytes = sub_root_meta_rsv(root, num_bytes, |
| 3962 | BTRFS_QGROUP_RSV_META_PREALLOC); |
Qu Wenruo | 4ee0d88 | 2017-12-12 15:34:35 +0800 | [diff] [blame] | 3963 | trace_qgroup_meta_convert(root, num_bytes); |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 3964 | qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes); |
Qu Wenruo | 64cfaef | 2017-12-12 15:34:31 +0800 | [diff] [blame] | 3965 | } |
| 3966 | |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3967 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3968 | * Check qgroup reserved space leaking, normally at destroy inode |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3969 | * time |
| 3970 | */ |
Nikolay Borisov | cfdd459 | 2020-06-03 08:55:46 +0300 | [diff] [blame] | 3971 | void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode) |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3972 | { |
| 3973 | struct extent_changeset changeset; |
| 3974 | struct ulist_node *unode; |
| 3975 | struct ulist_iterator iter; |
| 3976 | int ret; |
| 3977 | |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3978 | extent_changeset_init(&changeset); |
Nikolay Borisov | cfdd459 | 2020-06-03 08:55:46 +0300 | [diff] [blame] | 3979 | ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1, |
David Sterba | f734c44 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 3980 | EXTENT_QGROUP_RESERVED, &changeset); |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3981 | |
| 3982 | WARN_ON(ret < 0); |
| 3983 | if (WARN_ON(changeset.bytes_changed)) { |
| 3984 | ULIST_ITER_INIT(&iter); |
David Sterba | 53d3235 | 2017-02-13 13:42:29 +0100 | [diff] [blame] | 3985 | while ((unode = ulist_next(&changeset.range_changed, &iter))) { |
Nikolay Borisov | cfdd459 | 2020-06-03 08:55:46 +0300 | [diff] [blame] | 3986 | btrfs_warn(inode->root->fs_info, |
| 3987 | "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu", |
| 3988 | btrfs_ino(inode), unode->val, unode->aux); |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3989 | } |
Nikolay Borisov | cfdd459 | 2020-06-03 08:55:46 +0300 | [diff] [blame] | 3990 | btrfs_qgroup_free_refroot(inode->root->fs_info, |
| 3991 | inode->root->root_key.objectid, |
Qu Wenruo | d4e5c92 | 2017-12-12 15:34:23 +0800 | [diff] [blame] | 3992 | changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA); |
David Sterba | 0b08e1f | 2017-02-13 14:24:35 +0100 | [diff] [blame] | 3993 | |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3994 | } |
Qu Wenruo | 364ecf3 | 2017-02-27 15:10:38 +0800 | [diff] [blame] | 3995 | extent_changeset_release(&changeset); |
Qu Wenruo | 56fa9d0 | 2015-10-13 09:53:10 +0800 | [diff] [blame] | 3996 | } |
Qu Wenruo | 370a11b | 2019-01-23 15:15:16 +0800 | [diff] [blame] | 3997 | |
| 3998 | void btrfs_qgroup_init_swapped_blocks( |
| 3999 | struct btrfs_qgroup_swapped_blocks *swapped_blocks) |
| 4000 | { |
| 4001 | int i; |
| 4002 | |
| 4003 | spin_lock_init(&swapped_blocks->lock); |
| 4004 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) |
| 4005 | swapped_blocks->blocks[i] = RB_ROOT; |
| 4006 | swapped_blocks->swapped = false; |
| 4007 | } |
| 4008 | |
| 4009 | /* |
| 4010 | * Delete all swapped blocks record of @root. |
| 4011 | * Every record here means we skipped a full subtree scan for qgroup. |
| 4012 | * |
| 4013 | * Gets called when committing one transaction. |
| 4014 | */ |
| 4015 | void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root) |
| 4016 | { |
| 4017 | struct btrfs_qgroup_swapped_blocks *swapped_blocks; |
| 4018 | int i; |
| 4019 | |
| 4020 | swapped_blocks = &root->swapped_blocks; |
| 4021 | |
| 4022 | spin_lock(&swapped_blocks->lock); |
| 4023 | if (!swapped_blocks->swapped) |
| 4024 | goto out; |
| 4025 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 4026 | struct rb_root *cur_root = &swapped_blocks->blocks[i]; |
| 4027 | struct btrfs_qgroup_swapped_block *entry; |
| 4028 | struct btrfs_qgroup_swapped_block *next; |
| 4029 | |
| 4030 | rbtree_postorder_for_each_entry_safe(entry, next, cur_root, |
| 4031 | node) |
| 4032 | kfree(entry); |
| 4033 | swapped_blocks->blocks[i] = RB_ROOT; |
| 4034 | } |
| 4035 | swapped_blocks->swapped = false; |
| 4036 | out: |
| 4037 | spin_unlock(&swapped_blocks->lock); |
| 4038 | } |
| 4039 | |
| 4040 | /* |
| 4041 | * Add subtree roots record into @subvol_root. |
| 4042 | * |
| 4043 | * @subvol_root: tree root of the subvolume tree get swapped |
| 4044 | * @bg: block group under balance |
| 4045 | * @subvol_parent/slot: pointer to the subtree root in subvolume tree |
| 4046 | * @reloc_parent/slot: pointer to the subtree root in reloc tree |
| 4047 | * BOTH POINTERS ARE BEFORE TREE SWAP |
| 4048 | * @last_snapshot: last snapshot generation of the subvolume tree |
| 4049 | */ |
| 4050 | int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans, |
| 4051 | struct btrfs_root *subvol_root, |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4052 | struct btrfs_block_group *bg, |
Qu Wenruo | 370a11b | 2019-01-23 15:15:16 +0800 | [diff] [blame] | 4053 | struct extent_buffer *subvol_parent, int subvol_slot, |
| 4054 | struct extent_buffer *reloc_parent, int reloc_slot, |
| 4055 | u64 last_snapshot) |
| 4056 | { |
| 4057 | struct btrfs_fs_info *fs_info = subvol_root->fs_info; |
| 4058 | struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks; |
| 4059 | struct btrfs_qgroup_swapped_block *block; |
| 4060 | struct rb_node **cur; |
| 4061 | struct rb_node *parent = NULL; |
| 4062 | int level = btrfs_header_level(subvol_parent) - 1; |
| 4063 | int ret = 0; |
| 4064 | |
| 4065 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
| 4066 | return 0; |
| 4067 | |
| 4068 | if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) > |
| 4069 | btrfs_node_ptr_generation(reloc_parent, reloc_slot)) { |
| 4070 | btrfs_err_rl(fs_info, |
| 4071 | "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu", |
| 4072 | __func__, |
| 4073 | btrfs_node_ptr_generation(subvol_parent, subvol_slot), |
| 4074 | btrfs_node_ptr_generation(reloc_parent, reloc_slot)); |
| 4075 | return -EUCLEAN; |
| 4076 | } |
| 4077 | |
| 4078 | block = kmalloc(sizeof(*block), GFP_NOFS); |
| 4079 | if (!block) { |
| 4080 | ret = -ENOMEM; |
| 4081 | goto out; |
| 4082 | } |
| 4083 | |
| 4084 | /* |
| 4085 | * @reloc_parent/slot is still before swap, while @block is going to |
| 4086 | * record the bytenr after swap, so we do the swap here. |
| 4087 | */ |
| 4088 | block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot); |
| 4089 | block->subvol_generation = btrfs_node_ptr_generation(reloc_parent, |
| 4090 | reloc_slot); |
| 4091 | block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot); |
| 4092 | block->reloc_generation = btrfs_node_ptr_generation(subvol_parent, |
| 4093 | subvol_slot); |
| 4094 | block->last_snapshot = last_snapshot; |
| 4095 | block->level = level; |
Qu Wenruo | 57949d0 | 2019-05-21 19:28:08 +0800 | [diff] [blame] | 4096 | |
| 4097 | /* |
| 4098 | * If we have bg == NULL, we're called from btrfs_recover_relocation(), |
| 4099 | * no one else can modify tree blocks thus we qgroup will not change |
| 4100 | * no matter the value of trace_leaf. |
| 4101 | */ |
| 4102 | if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA) |
Qu Wenruo | 370a11b | 2019-01-23 15:15:16 +0800 | [diff] [blame] | 4103 | block->trace_leaf = true; |
| 4104 | else |
| 4105 | block->trace_leaf = false; |
| 4106 | btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot); |
| 4107 | |
| 4108 | /* Insert @block into @blocks */ |
| 4109 | spin_lock(&blocks->lock); |
| 4110 | cur = &blocks->blocks[level].rb_node; |
| 4111 | while (*cur) { |
| 4112 | struct btrfs_qgroup_swapped_block *entry; |
| 4113 | |
| 4114 | parent = *cur; |
| 4115 | entry = rb_entry(parent, struct btrfs_qgroup_swapped_block, |
| 4116 | node); |
| 4117 | |
| 4118 | if (entry->subvol_bytenr < block->subvol_bytenr) { |
| 4119 | cur = &(*cur)->rb_left; |
| 4120 | } else if (entry->subvol_bytenr > block->subvol_bytenr) { |
| 4121 | cur = &(*cur)->rb_right; |
| 4122 | } else { |
| 4123 | if (entry->subvol_generation != |
| 4124 | block->subvol_generation || |
| 4125 | entry->reloc_bytenr != block->reloc_bytenr || |
| 4126 | entry->reloc_generation != |
| 4127 | block->reloc_generation) { |
| 4128 | /* |
| 4129 | * Duplicated but mismatch entry found. |
| 4130 | * Shouldn't happen. |
| 4131 | * |
| 4132 | * Marking qgroup inconsistent should be enough |
| 4133 | * for end users. |
| 4134 | */ |
| 4135 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); |
| 4136 | ret = -EEXIST; |
| 4137 | } |
| 4138 | kfree(block); |
| 4139 | goto out_unlock; |
| 4140 | } |
| 4141 | } |
| 4142 | rb_link_node(&block->node, parent, cur); |
| 4143 | rb_insert_color(&block->node, &blocks->blocks[level]); |
| 4144 | blocks->swapped = true; |
| 4145 | out_unlock: |
| 4146 | spin_unlock(&blocks->lock); |
| 4147 | out: |
| 4148 | if (ret < 0) |
| 4149 | fs_info->qgroup_flags |= |
| 4150 | BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 4151 | return ret; |
| 4152 | } |
Qu Wenruo | f616f5c | 2019-01-23 15:15:17 +0800 | [diff] [blame] | 4153 | |
| 4154 | /* |
| 4155 | * Check if the tree block is a subtree root, and if so do the needed |
| 4156 | * delayed subtree trace for qgroup. |
| 4157 | * |
| 4158 | * This is called during btrfs_cow_block(). |
| 4159 | */ |
| 4160 | int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans, |
| 4161 | struct btrfs_root *root, |
| 4162 | struct extent_buffer *subvol_eb) |
| 4163 | { |
| 4164 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4165 | struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks; |
| 4166 | struct btrfs_qgroup_swapped_block *block; |
| 4167 | struct extent_buffer *reloc_eb = NULL; |
| 4168 | struct rb_node *node; |
| 4169 | bool found = false; |
| 4170 | bool swapped = false; |
| 4171 | int level = btrfs_header_level(subvol_eb); |
| 4172 | int ret = 0; |
| 4173 | int i; |
| 4174 | |
| 4175 | if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) |
| 4176 | return 0; |
| 4177 | if (!is_fstree(root->root_key.objectid) || !root->reloc_root) |
| 4178 | return 0; |
| 4179 | |
| 4180 | spin_lock(&blocks->lock); |
| 4181 | if (!blocks->swapped) { |
| 4182 | spin_unlock(&blocks->lock); |
| 4183 | return 0; |
| 4184 | } |
| 4185 | node = blocks->blocks[level].rb_node; |
| 4186 | |
| 4187 | while (node) { |
| 4188 | block = rb_entry(node, struct btrfs_qgroup_swapped_block, node); |
| 4189 | if (block->subvol_bytenr < subvol_eb->start) { |
| 4190 | node = node->rb_left; |
| 4191 | } else if (block->subvol_bytenr > subvol_eb->start) { |
| 4192 | node = node->rb_right; |
| 4193 | } else { |
| 4194 | found = true; |
| 4195 | break; |
| 4196 | } |
| 4197 | } |
| 4198 | if (!found) { |
| 4199 | spin_unlock(&blocks->lock); |
| 4200 | goto out; |
| 4201 | } |
| 4202 | /* Found one, remove it from @blocks first and update blocks->swapped */ |
| 4203 | rb_erase(&block->node, &blocks->blocks[level]); |
| 4204 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 4205 | if (RB_EMPTY_ROOT(&blocks->blocks[i])) { |
| 4206 | swapped = true; |
| 4207 | break; |
| 4208 | } |
| 4209 | } |
| 4210 | blocks->swapped = swapped; |
| 4211 | spin_unlock(&blocks->lock); |
| 4212 | |
| 4213 | /* Read out reloc subtree root */ |
Josef Bacik | 1b7ec85 | 2020-11-05 10:45:18 -0500 | [diff] [blame] | 4214 | reloc_eb = read_tree_block(fs_info, block->reloc_bytenr, 0, |
Qu Wenruo | f616f5c | 2019-01-23 15:15:17 +0800 | [diff] [blame] | 4215 | block->reloc_generation, block->level, |
| 4216 | &block->first_key); |
| 4217 | if (IS_ERR(reloc_eb)) { |
| 4218 | ret = PTR_ERR(reloc_eb); |
| 4219 | reloc_eb = NULL; |
| 4220 | goto free_out; |
| 4221 | } |
| 4222 | if (!extent_buffer_uptodate(reloc_eb)) { |
| 4223 | ret = -EIO; |
| 4224 | goto free_out; |
| 4225 | } |
| 4226 | |
| 4227 | ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb, |
| 4228 | block->last_snapshot, block->trace_leaf); |
| 4229 | free_out: |
| 4230 | kfree(block); |
| 4231 | free_extent_buffer(reloc_eb); |
| 4232 | out: |
| 4233 | if (ret < 0) { |
| 4234 | btrfs_err_rl(fs_info, |
| 4235 | "failed to account subtree at bytenr %llu: %d", |
| 4236 | subvol_eb->start, ret); |
| 4237 | fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT; |
| 4238 | } |
| 4239 | return ret; |
| 4240 | } |
Jeff Mahoney | 81f7eb0 | 2020-02-11 15:25:37 +0800 | [diff] [blame] | 4241 | |
| 4242 | void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans) |
| 4243 | { |
| 4244 | struct btrfs_qgroup_extent_record *entry; |
| 4245 | struct btrfs_qgroup_extent_record *next; |
| 4246 | struct rb_root *root; |
| 4247 | |
| 4248 | root = &trans->delayed_refs.dirty_extent_root; |
| 4249 | rbtree_postorder_for_each_entry_safe(entry, next, root, node) { |
| 4250 | ulist_free(entry->old_roots); |
| 4251 | kfree(entry); |
| 4252 | } |
| 4253 | } |