David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 7 | #include <linux/sched/signal.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 11 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 12 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 13 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 15 | #include <linux/ratelimit.h> |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 16 | #include <linux/percpu_counter.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 17 | #include <linux/lockdep.h> |
Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 18 | #include <linux/crc32c.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 19 | #include "misc.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 20 | #include "tree-log.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 21 | #include "disk-io.h" |
| 22 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 25 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 26 | #include "free-space-cache.h" |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 27 | #include "free-space-tree.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 28 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 29 | #include "qgroup.h" |
Josef Bacik | fd708b8 | 2017-09-29 15:43:50 -0400 | [diff] [blame] | 30 | #include "ref-verify.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | d12ffdd | 2019-06-19 13:47:17 -0400 | [diff] [blame] | 32 | #include "block-rsv.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 33 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 34 | #include "block-group.h" |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 35 | #include "discard.h" |
Qu Wenruo | c57dd1f | 2020-07-31 19:29:11 +0800 | [diff] [blame] | 36 | #include "rcu-string.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 37 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 38 | #undef SCRAMBLE_DELAYED_REFS |
| 39 | |
Qu Wenruo | 9f9b8e8 | 2018-10-24 20:24:01 +0800 | [diff] [blame] | 40 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 41 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 42 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 43 | u64 root_objectid, u64 owner_objectid, |
| 44 | u64 owner_offset, int refs_to_drop, |
| 45 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 46 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 47 | struct extent_buffer *leaf, |
| 48 | struct btrfs_extent_item *ei); |
| 49 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 50 | u64 parent, u64 root_objectid, |
| 51 | u64 flags, u64 owner, u64 offset, |
| 52 | struct btrfs_key *ins, int ref_mod); |
| 53 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 54 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 55 | struct btrfs_delayed_extent_op *extent_op); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 56 | static int find_next_key(struct btrfs_path *path, int level, |
| 57 | struct btrfs_key *key); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 58 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 59 | static int block_group_bits(struct btrfs_block_group *cache, u64 bits) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 60 | { |
| 61 | return (cache->flags & bits) == bits; |
| 62 | } |
| 63 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 64 | int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info, |
| 65 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 66 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 67 | u64 end = start + num_bytes - 1; |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 68 | set_extent_bits(&fs_info->excluded_extents, start, end, |
| 69 | EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 70 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 71 | } |
| 72 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 73 | void btrfs_free_excluded_extents(struct btrfs_block_group *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 74 | { |
Nikolay Borisov | 9e715da | 2018-06-20 15:49:08 +0300 | [diff] [blame] | 75 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 76 | u64 start, end; |
| 77 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 78 | start = cache->start; |
| 79 | end = start + cache->length - 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 80 | |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 81 | clear_extent_bits(&fs_info->excluded_extents, start, end, |
| 82 | EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 85 | /* simple helper to search for an existing data extent at a given offset */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 86 | int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 87 | { |
| 88 | int ret; |
| 89 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 90 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 91 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 92 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 93 | if (!path) |
| 94 | return -ENOMEM; |
| 95 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 96 | key.objectid = start; |
| 97 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 98 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 99 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 100 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 101 | return ret; |
| 102 | } |
| 103 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 104 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 105 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 106 | * |
| 107 | * the head node for delayed ref is used to store the sum of all the |
| 108 | * reference count modifications queued up in the rbtree. the head |
| 109 | * node may also store the extent flags to set. This way you can check |
| 110 | * to see what the reference count and extent flags would be if all of |
| 111 | * the delayed refs are not processed. |
| 112 | */ |
| 113 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 114 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 115 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 116 | { |
| 117 | struct btrfs_delayed_ref_head *head; |
| 118 | struct btrfs_delayed_ref_root *delayed_refs; |
| 119 | struct btrfs_path *path; |
| 120 | struct btrfs_extent_item *ei; |
| 121 | struct extent_buffer *leaf; |
| 122 | struct btrfs_key key; |
| 123 | u32 item_size; |
| 124 | u64 num_refs; |
| 125 | u64 extent_flags; |
| 126 | int ret; |
| 127 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 128 | /* |
| 129 | * If we don't have skinny metadata, don't bother doing anything |
| 130 | * different |
| 131 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 132 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 133 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 134 | metadata = 0; |
| 135 | } |
| 136 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 137 | path = btrfs_alloc_path(); |
| 138 | if (!path) |
| 139 | return -ENOMEM; |
| 140 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 141 | if (!trans) { |
| 142 | path->skip_locking = 1; |
| 143 | path->search_commit_root = 1; |
| 144 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 145 | |
| 146 | search_again: |
| 147 | key.objectid = bytenr; |
| 148 | key.offset = offset; |
| 149 | if (metadata) |
| 150 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 151 | else |
| 152 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 153 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 154 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 155 | if (ret < 0) |
| 156 | goto out_free; |
| 157 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 158 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 159 | if (path->slots[0]) { |
| 160 | path->slots[0]--; |
| 161 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 162 | path->slots[0]); |
| 163 | if (key.objectid == bytenr && |
| 164 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 165 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 166 | ret = 0; |
| 167 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 170 | if (ret == 0) { |
| 171 | leaf = path->nodes[0]; |
| 172 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 173 | if (item_size >= sizeof(*ei)) { |
| 174 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 175 | struct btrfs_extent_item); |
| 176 | num_refs = btrfs_extent_refs(leaf, ei); |
| 177 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 178 | } else { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 179 | ret = -EINVAL; |
| 180 | btrfs_print_v0_err(fs_info); |
| 181 | if (trans) |
| 182 | btrfs_abort_transaction(trans, ret); |
| 183 | else |
| 184 | btrfs_handle_fs_error(fs_info, ret, NULL); |
| 185 | |
| 186 | goto out_free; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 187 | } |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 188 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 189 | BUG_ON(num_refs == 0); |
| 190 | } else { |
| 191 | num_refs = 0; |
| 192 | extent_flags = 0; |
| 193 | ret = 0; |
| 194 | } |
| 195 | |
| 196 | if (!trans) |
| 197 | goto out; |
| 198 | |
| 199 | delayed_refs = &trans->transaction->delayed_refs; |
| 200 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 201 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 202 | if (head) { |
| 203 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 204 | refcount_inc(&head->refs); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 205 | spin_unlock(&delayed_refs->lock); |
| 206 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 207 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 208 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 209 | /* |
| 210 | * Mutex was contended, block until it's released and try |
| 211 | * again |
| 212 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 213 | mutex_lock(&head->mutex); |
| 214 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 215 | btrfs_put_delayed_ref_head(head); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 216 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 217 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 218 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 219 | if (head->extent_op && head->extent_op->update_flags) |
| 220 | extent_flags |= head->extent_op->flags_to_set; |
| 221 | else |
| 222 | BUG_ON(num_refs == 0); |
| 223 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 224 | num_refs += head->ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 225 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 226 | mutex_unlock(&head->mutex); |
| 227 | } |
| 228 | spin_unlock(&delayed_refs->lock); |
| 229 | out: |
| 230 | WARN_ON(num_refs == 0); |
| 231 | if (refs) |
| 232 | *refs = num_refs; |
| 233 | if (flags) |
| 234 | *flags = extent_flags; |
| 235 | out_free: |
| 236 | btrfs_free_path(path); |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 241 | * Back reference rules. Back refs have three main goals: |
| 242 | * |
| 243 | * 1) differentiate between all holders of references to an extent so that |
| 244 | * when a reference is dropped we can make sure it was a valid reference |
| 245 | * before freeing the extent. |
| 246 | * |
| 247 | * 2) Provide enough information to quickly find the holders of an extent |
| 248 | * if we notice a given block is corrupted or bad. |
| 249 | * |
| 250 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 251 | * maintenance. This is actually the same as #2, but with a slightly |
| 252 | * different use case. |
| 253 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 254 | * There are two kinds of back refs. The implicit back refs is optimized |
| 255 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 256 | * back refs of this kind provide information about the block's owner tree |
| 257 | * and the pointer's key. These information allow us to find the block by |
| 258 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 259 | * referenced by their owner trees. The location of tree block is recorded |
| 260 | * in the back refs. Actually the full back refs is generic, and can be |
| 261 | * used in all cases the implicit back refs is used. The major shortcoming |
| 262 | * of the full back refs is its overhead. Every time a tree block gets |
| 263 | * COWed, we have to update back refs entry for all pointers in it. |
| 264 | * |
| 265 | * For a newly allocated tree block, we use implicit back refs for |
| 266 | * pointers in it. This means most tree related operations only involve |
| 267 | * implicit back refs. For a tree block created in old transaction, the |
| 268 | * only way to drop a reference to it is COW it. So we can detect the |
| 269 | * event that tree block loses its owner tree's reference and do the |
| 270 | * back refs conversion. |
| 271 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 272 | * When a tree block is COWed through a tree, there are four cases: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 273 | * |
| 274 | * The reference count of the block is one and the tree is the block's |
| 275 | * owner tree. Nothing to do in this case. |
| 276 | * |
| 277 | * The reference count of the block is one and the tree is not the |
| 278 | * block's owner tree. In this case, full back refs is used for pointers |
| 279 | * in the block. Remove these full back refs, add implicit back refs for |
| 280 | * every pointers in the new block. |
| 281 | * |
| 282 | * The reference count of the block is greater than one and the tree is |
| 283 | * the block's owner tree. In this case, implicit back refs is used for |
| 284 | * pointers in the block. Add full back refs for every pointers in the |
| 285 | * block, increase lower level extents' reference counts. The original |
| 286 | * implicit back refs are entailed to the new block. |
| 287 | * |
| 288 | * The reference count of the block is greater than one and the tree is |
| 289 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 290 | * the new block, increase lower level extents' reference count. |
| 291 | * |
| 292 | * Back Reference Key composing: |
| 293 | * |
| 294 | * The key objectid corresponds to the first byte in the extent, |
| 295 | * The key type is used to differentiate between types of back refs. |
| 296 | * There are different meanings of the key offset for different types |
| 297 | * of back refs. |
| 298 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 299 | * File extents can be referenced by: |
| 300 | * |
| 301 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 302 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 303 | * - different offsets inside a file (bookend extents in file.c) |
| 304 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 305 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 306 | * |
| 307 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 308 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 309 | * - original offset in the file |
| 310 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 311 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 312 | * The key offset for the implicit back refs is hash of the first |
| 313 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 314 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 315 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 316 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 317 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 318 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 319 | * The key offset for the implicit back refs is the first byte of |
| 320 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 321 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 322 | * When a file extent is allocated, The implicit back refs is used. |
| 323 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 324 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 325 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 326 | * |
| 327 | * When a file extent is removed file truncation, we find the |
| 328 | * corresponding implicit back refs and check the following fields: |
| 329 | * |
| 330 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 331 | * |
| 332 | * Btree extents can be referenced by: |
| 333 | * |
| 334 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 335 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 336 | * Both the implicit back refs and the full back refs for tree blocks |
| 337 | * only consist of key. The key offset for the implicit back refs is |
| 338 | * objectid of block's owner tree. The key offset for the full back refs |
| 339 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 340 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 341 | * When implicit back refs is used, information about the lowest key and |
| 342 | * level of the tree block are required. These information are stored in |
| 343 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 344 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 345 | |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 346 | /* |
| 347 | * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 348 | * is_data == BTRFS_REF_TYPE_DATA, data type is requiried, |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 349 | * is_data == BTRFS_REF_TYPE_ANY, either type is OK. |
| 350 | */ |
| 351 | int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, |
| 352 | struct btrfs_extent_inline_ref *iref, |
| 353 | enum btrfs_inline_ref_type is_data) |
| 354 | { |
| 355 | int type = btrfs_extent_inline_ref_type(eb, iref); |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 356 | u64 offset = btrfs_extent_inline_ref_offset(eb, iref); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 357 | |
| 358 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 359 | type == BTRFS_SHARED_BLOCK_REF_KEY || |
| 360 | type == BTRFS_SHARED_DATA_REF_KEY || |
| 361 | type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 362 | if (is_data == BTRFS_REF_TYPE_BLOCK) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 363 | if (type == BTRFS_TREE_BLOCK_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 364 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 365 | if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 366 | ASSERT(eb->fs_info); |
| 367 | /* |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 368 | * Every shared one has parent tree block, |
| 369 | * which must be aligned to sector size. |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 370 | */ |
| 371 | if (offset && |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 372 | IS_ALIGNED(offset, eb->fs_info->sectorsize)) |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 373 | return type; |
| 374 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 375 | } else if (is_data == BTRFS_REF_TYPE_DATA) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 376 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 377 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 378 | if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 379 | ASSERT(eb->fs_info); |
| 380 | /* |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 381 | * Every shared one has parent tree block, |
| 382 | * which must be aligned to sector size. |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 383 | */ |
| 384 | if (offset && |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 385 | IS_ALIGNED(offset, eb->fs_info->sectorsize)) |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 386 | return type; |
| 387 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 388 | } else { |
| 389 | ASSERT(is_data == BTRFS_REF_TYPE_ANY); |
| 390 | return type; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | btrfs_print_leaf((struct extent_buffer *)eb); |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 395 | btrfs_err(eb->fs_info, |
| 396 | "eb %llu iref 0x%lx invalid extent inline ref type %d", |
| 397 | eb->start, (unsigned long)iref, type); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 398 | WARN_ON(1); |
| 399 | |
| 400 | return BTRFS_REF_TYPE_INVALID; |
| 401 | } |
| 402 | |
Qu Wenruo | 0785a9a | 2019-08-09 09:24:24 +0800 | [diff] [blame] | 403 | u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 404 | { |
| 405 | u32 high_crc = ~(u32)0; |
| 406 | u32 low_crc = ~(u32)0; |
| 407 | __le64 lenum; |
| 408 | |
| 409 | lenum = cpu_to_le64(root_objectid); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 410 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 411 | lenum = cpu_to_le64(owner); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 412 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 413 | lenum = cpu_to_le64(offset); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 414 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 415 | |
| 416 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 417 | } |
| 418 | |
| 419 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 420 | struct btrfs_extent_data_ref *ref) |
| 421 | { |
| 422 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 423 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 424 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 425 | } |
| 426 | |
| 427 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 428 | struct btrfs_extent_data_ref *ref, |
| 429 | u64 root_objectid, u64 owner, u64 offset) |
| 430 | { |
| 431 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 432 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 433 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 434 | return 0; |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 439 | struct btrfs_path *path, |
| 440 | u64 bytenr, u64 parent, |
| 441 | u64 root_objectid, |
| 442 | u64 owner, u64 offset) |
| 443 | { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 444 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 445 | struct btrfs_key key; |
| 446 | struct btrfs_extent_data_ref *ref; |
| 447 | struct extent_buffer *leaf; |
| 448 | u32 nritems; |
| 449 | int ret; |
| 450 | int recow; |
| 451 | int err = -ENOENT; |
| 452 | |
| 453 | key.objectid = bytenr; |
| 454 | if (parent) { |
| 455 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 456 | key.offset = parent; |
| 457 | } else { |
| 458 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 459 | key.offset = hash_extent_data_ref(root_objectid, |
| 460 | owner, offset); |
| 461 | } |
| 462 | again: |
| 463 | recow = 0; |
| 464 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 465 | if (ret < 0) { |
| 466 | err = ret; |
| 467 | goto fail; |
| 468 | } |
| 469 | |
| 470 | if (parent) { |
| 471 | if (!ret) |
| 472 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 473 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 477 | nritems = btrfs_header_nritems(leaf); |
| 478 | while (1) { |
| 479 | if (path->slots[0] >= nritems) { |
| 480 | ret = btrfs_next_leaf(root, path); |
| 481 | if (ret < 0) |
| 482 | err = ret; |
| 483 | if (ret) |
| 484 | goto fail; |
| 485 | |
| 486 | leaf = path->nodes[0]; |
| 487 | nritems = btrfs_header_nritems(leaf); |
| 488 | recow = 1; |
| 489 | } |
| 490 | |
| 491 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 492 | if (key.objectid != bytenr || |
| 493 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 494 | goto fail; |
| 495 | |
| 496 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 497 | struct btrfs_extent_data_ref); |
| 498 | |
| 499 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 500 | owner, offset)) { |
| 501 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 502 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 503 | goto again; |
| 504 | } |
| 505 | err = 0; |
| 506 | break; |
| 507 | } |
| 508 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 509 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 510 | fail: |
| 511 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 512 | } |
| 513 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 514 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 515 | struct btrfs_path *path, |
| 516 | u64 bytenr, u64 parent, |
| 517 | u64 root_objectid, u64 owner, |
| 518 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 519 | { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 520 | struct btrfs_root *root = trans->fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 521 | struct btrfs_key key; |
| 522 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 523 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 524 | u32 num_refs; |
| 525 | int ret; |
| 526 | |
| 527 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 528 | if (parent) { |
| 529 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 530 | key.offset = parent; |
| 531 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 532 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 533 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 534 | key.offset = hash_extent_data_ref(root_objectid, |
| 535 | owner, offset); |
| 536 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 537 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 538 | |
| 539 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 540 | if (ret && ret != -EEXIST) |
| 541 | goto fail; |
| 542 | |
| 543 | leaf = path->nodes[0]; |
| 544 | if (parent) { |
| 545 | struct btrfs_shared_data_ref *ref; |
| 546 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 547 | struct btrfs_shared_data_ref); |
| 548 | if (ret == 0) { |
| 549 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 550 | } else { |
| 551 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 552 | num_refs += refs_to_add; |
| 553 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 554 | } |
| 555 | } else { |
| 556 | struct btrfs_extent_data_ref *ref; |
| 557 | while (ret == -EEXIST) { |
| 558 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 559 | struct btrfs_extent_data_ref); |
| 560 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 561 | owner, offset)) |
| 562 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 563 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 564 | key.offset++; |
| 565 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 566 | size); |
| 567 | if (ret && ret != -EEXIST) |
| 568 | goto fail; |
| 569 | |
| 570 | leaf = path->nodes[0]; |
| 571 | } |
| 572 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 573 | struct btrfs_extent_data_ref); |
| 574 | if (ret == 0) { |
| 575 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 576 | root_objectid); |
| 577 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 578 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 579 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 580 | } else { |
| 581 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 582 | num_refs += refs_to_add; |
| 583 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 584 | } |
| 585 | } |
| 586 | btrfs_mark_buffer_dirty(leaf); |
| 587 | ret = 0; |
| 588 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 589 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 590 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 591 | } |
| 592 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 593 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 594 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 595 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 596 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 597 | struct btrfs_key key; |
| 598 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 599 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 600 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 601 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 602 | int ret = 0; |
| 603 | |
| 604 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 605 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 606 | |
| 607 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 608 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 609 | struct btrfs_extent_data_ref); |
| 610 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 611 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 612 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 613 | struct btrfs_shared_data_ref); |
| 614 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 615 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 616 | btrfs_print_v0_err(trans->fs_info); |
| 617 | btrfs_abort_transaction(trans, -EINVAL); |
| 618 | return -EINVAL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 619 | } else { |
| 620 | BUG(); |
| 621 | } |
| 622 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 623 | BUG_ON(num_refs < refs_to_drop); |
| 624 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 625 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 626 | if (num_refs == 0) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 627 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 628 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 629 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 630 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 631 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 632 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 633 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 634 | btrfs_mark_buffer_dirty(leaf); |
| 635 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 636 | return ret; |
| 637 | } |
| 638 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 639 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 640 | struct btrfs_extent_inline_ref *iref) |
| 641 | { |
| 642 | struct btrfs_key key; |
| 643 | struct extent_buffer *leaf; |
| 644 | struct btrfs_extent_data_ref *ref1; |
| 645 | struct btrfs_shared_data_ref *ref2; |
| 646 | u32 num_refs = 0; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 647 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 648 | |
| 649 | leaf = path->nodes[0]; |
| 650 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 651 | |
| 652 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 653 | if (iref) { |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 654 | /* |
| 655 | * If type is invalid, we should have bailed out earlier than |
| 656 | * this call. |
| 657 | */ |
| 658 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 659 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
| 660 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 661 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 662 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 663 | } else { |
| 664 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 665 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 666 | } |
| 667 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 668 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 669 | struct btrfs_extent_data_ref); |
| 670 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 671 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 672 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 673 | struct btrfs_shared_data_ref); |
| 674 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 675 | } else { |
| 676 | WARN_ON(1); |
| 677 | } |
| 678 | return num_refs; |
| 679 | } |
| 680 | |
| 681 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 682 | struct btrfs_path *path, |
| 683 | u64 bytenr, u64 parent, |
| 684 | u64 root_objectid) |
| 685 | { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 686 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 687 | struct btrfs_key key; |
| 688 | int ret; |
| 689 | |
| 690 | key.objectid = bytenr; |
| 691 | if (parent) { |
| 692 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 693 | key.offset = parent; |
| 694 | } else { |
| 695 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 696 | key.offset = root_objectid; |
| 697 | } |
| 698 | |
| 699 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 700 | if (ret > 0) |
| 701 | ret = -ENOENT; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 706 | struct btrfs_path *path, |
| 707 | u64 bytenr, u64 parent, |
| 708 | u64 root_objectid) |
| 709 | { |
| 710 | struct btrfs_key key; |
| 711 | int ret; |
| 712 | |
| 713 | key.objectid = bytenr; |
| 714 | if (parent) { |
| 715 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 716 | key.offset = parent; |
| 717 | } else { |
| 718 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 719 | key.offset = root_objectid; |
| 720 | } |
| 721 | |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 722 | ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 723 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 724 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 725 | return ret; |
| 726 | } |
| 727 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 728 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 729 | { |
| 730 | int type; |
| 731 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 732 | if (parent > 0) |
| 733 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 734 | else |
| 735 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 736 | } else { |
| 737 | if (parent > 0) |
| 738 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 739 | else |
| 740 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 741 | } |
| 742 | return type; |
| 743 | } |
| 744 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 745 | static int find_next_key(struct btrfs_path *path, int level, |
| 746 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 747 | |
| 748 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 749 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 750 | if (!path->nodes[level]) |
| 751 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 752 | if (path->slots[level] + 1 >= |
| 753 | btrfs_header_nritems(path->nodes[level])) |
| 754 | continue; |
| 755 | if (level == 0) |
| 756 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 757 | path->slots[level] + 1); |
| 758 | else |
| 759 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 760 | path->slots[level] + 1); |
| 761 | return 0; |
| 762 | } |
| 763 | return 1; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 768 | * to the address of inline back ref, and 0 is returned. |
| 769 | * |
| 770 | * if back ref isn't found, *ref_ret is set to the address where it |
| 771 | * should be inserted, and -ENOENT is returned. |
| 772 | * |
| 773 | * if insert is true and there are too many inline back refs, the path |
| 774 | * points to the extent item, and -EAGAIN is returned. |
| 775 | * |
| 776 | * NOTE: inline back refs are ordered in the same way that back ref |
| 777 | * items in the tree are ordered. |
| 778 | */ |
| 779 | static noinline_for_stack |
| 780 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 781 | struct btrfs_path *path, |
| 782 | struct btrfs_extent_inline_ref **ref_ret, |
| 783 | u64 bytenr, u64 num_bytes, |
| 784 | u64 parent, u64 root_objectid, |
| 785 | u64 owner, u64 offset, int insert) |
| 786 | { |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 787 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 788 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 789 | struct btrfs_key key; |
| 790 | struct extent_buffer *leaf; |
| 791 | struct btrfs_extent_item *ei; |
| 792 | struct btrfs_extent_inline_ref *iref; |
| 793 | u64 flags; |
| 794 | u64 item_size; |
| 795 | unsigned long ptr; |
| 796 | unsigned long end; |
| 797 | int extra_size; |
| 798 | int type; |
| 799 | int want; |
| 800 | int ret; |
| 801 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 802 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 803 | int needed; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 804 | |
| 805 | key.objectid = bytenr; |
| 806 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 807 | key.offset = num_bytes; |
| 808 | |
| 809 | want = extent_ref_type(parent, owner); |
| 810 | if (insert) { |
| 811 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 812 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 813 | } else |
| 814 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 815 | |
| 816 | /* |
Nikolay Borisov | 16d1c06 | 2018-06-18 14:59:26 +0300 | [diff] [blame] | 817 | * Owner is our level, so we can just add one to get the level for the |
| 818 | * block we are interested in. |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 819 | */ |
| 820 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 821 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 822 | key.offset = owner; |
| 823 | } |
| 824 | |
| 825 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 826 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 827 | if (ret < 0) { |
| 828 | err = ret; |
| 829 | goto out; |
| 830 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * We may be a newly converted file system which still has the old fat |
| 834 | * extent entries for metadata, so try and see if we have one of those. |
| 835 | */ |
| 836 | if (ret > 0 && skinny_metadata) { |
| 837 | skinny_metadata = false; |
| 838 | if (path->slots[0]) { |
| 839 | path->slots[0]--; |
| 840 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 841 | path->slots[0]); |
| 842 | if (key.objectid == bytenr && |
| 843 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 844 | key.offset == num_bytes) |
| 845 | ret = 0; |
| 846 | } |
| 847 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 848 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 849 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 850 | key.offset = num_bytes; |
| 851 | btrfs_release_path(path); |
| 852 | goto again; |
| 853 | } |
| 854 | } |
| 855 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 856 | if (ret && !insert) { |
| 857 | err = -ENOENT; |
| 858 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 859 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 860 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 861 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 862 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 863 | |
| 864 | leaf = path->nodes[0]; |
| 865 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 866 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 867 | err = -EINVAL; |
| 868 | btrfs_print_v0_err(fs_info); |
| 869 | btrfs_abort_transaction(trans, err); |
| 870 | goto out; |
| 871 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 872 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 873 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 874 | flags = btrfs_extent_flags(leaf, ei); |
| 875 | |
| 876 | ptr = (unsigned long)(ei + 1); |
| 877 | end = (unsigned long)ei + item_size; |
| 878 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 879 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 880 | ptr += sizeof(struct btrfs_tree_block_info); |
| 881 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 882 | } |
| 883 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 884 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) |
| 885 | needed = BTRFS_REF_TYPE_DATA; |
| 886 | else |
| 887 | needed = BTRFS_REF_TYPE_BLOCK; |
| 888 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 889 | err = -ENOENT; |
| 890 | while (1) { |
| 891 | if (ptr >= end) { |
| 892 | WARN_ON(ptr > end); |
| 893 | break; |
| 894 | } |
| 895 | iref = (struct btrfs_extent_inline_ref *)ptr; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 896 | type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); |
| 897 | if (type == BTRFS_REF_TYPE_INVALID) { |
Su Yue | af431dc | 2018-06-22 16:18:01 +0800 | [diff] [blame] | 898 | err = -EUCLEAN; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 899 | goto out; |
| 900 | } |
| 901 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 902 | if (want < type) |
| 903 | break; |
| 904 | if (want > type) { |
| 905 | ptr += btrfs_extent_inline_ref_size(type); |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 910 | struct btrfs_extent_data_ref *dref; |
| 911 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 912 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 913 | owner, offset)) { |
| 914 | err = 0; |
| 915 | break; |
| 916 | } |
| 917 | if (hash_extent_data_ref_item(leaf, dref) < |
| 918 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 919 | break; |
| 920 | } else { |
| 921 | u64 ref_offset; |
| 922 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 923 | if (parent > 0) { |
| 924 | if (parent == ref_offset) { |
| 925 | err = 0; |
| 926 | break; |
| 927 | } |
| 928 | if (ref_offset < parent) |
| 929 | break; |
| 930 | } else { |
| 931 | if (root_objectid == ref_offset) { |
| 932 | err = 0; |
| 933 | break; |
| 934 | } |
| 935 | if (ref_offset < root_objectid) |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | ptr += btrfs_extent_inline_ref_size(type); |
| 940 | } |
| 941 | if (err == -ENOENT && insert) { |
| 942 | if (item_size + extra_size >= |
| 943 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 944 | err = -EAGAIN; |
| 945 | goto out; |
| 946 | } |
| 947 | /* |
| 948 | * To add new inline back ref, we have to make sure |
| 949 | * there is no corresponding back ref item. |
| 950 | * For simplicity, we just do not add new inline back |
| 951 | * ref if there is any kind of item for this block |
| 952 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 953 | if (find_next_key(path, 0, &key) == 0 && |
| 954 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 955 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 956 | err = -EAGAIN; |
| 957 | goto out; |
| 958 | } |
| 959 | } |
| 960 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 961 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 962 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 963 | path->keep_locks = 0; |
| 964 | btrfs_unlock_up_safe(path, 1); |
| 965 | } |
| 966 | return err; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * helper to add new inline back ref |
| 971 | */ |
| 972 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 973 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 974 | struct btrfs_path *path, |
| 975 | struct btrfs_extent_inline_ref *iref, |
| 976 | u64 parent, u64 root_objectid, |
| 977 | u64 owner, u64 offset, int refs_to_add, |
| 978 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 979 | { |
| 980 | struct extent_buffer *leaf; |
| 981 | struct btrfs_extent_item *ei; |
| 982 | unsigned long ptr; |
| 983 | unsigned long end; |
| 984 | unsigned long item_offset; |
| 985 | u64 refs; |
| 986 | int size; |
| 987 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 988 | |
| 989 | leaf = path->nodes[0]; |
| 990 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 991 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 992 | |
| 993 | type = extent_ref_type(parent, owner); |
| 994 | size = btrfs_extent_inline_ref_size(type); |
| 995 | |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 996 | btrfs_extend_item(path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 997 | |
| 998 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 999 | refs = btrfs_extent_refs(leaf, ei); |
| 1000 | refs += refs_to_add; |
| 1001 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1002 | if (extent_op) |
| 1003 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1004 | |
| 1005 | ptr = (unsigned long)ei + item_offset; |
| 1006 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1007 | if (ptr < end - size) |
| 1008 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1009 | end - size - ptr); |
| 1010 | |
| 1011 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1012 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1013 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1014 | struct btrfs_extent_data_ref *dref; |
| 1015 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1016 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1017 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1018 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1019 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1020 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1021 | struct btrfs_shared_data_ref *sref; |
| 1022 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1023 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1024 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1025 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1026 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1027 | } else { |
| 1028 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1029 | } |
| 1030 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1034 | struct btrfs_path *path, |
| 1035 | struct btrfs_extent_inline_ref **ref_ret, |
| 1036 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1037 | u64 root_objectid, u64 owner, u64 offset) |
| 1038 | { |
| 1039 | int ret; |
| 1040 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1041 | ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr, |
| 1042 | num_bytes, parent, root_objectid, |
| 1043 | owner, offset, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1044 | if (ret != -ENOENT) |
| 1045 | return ret; |
| 1046 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1047 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1048 | *ref_ret = NULL; |
| 1049 | |
| 1050 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 1051 | ret = lookup_tree_block_ref(trans, path, bytenr, parent, |
| 1052 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1053 | } else { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 1054 | ret = lookup_extent_data_ref(trans, path, bytenr, parent, |
| 1055 | root_objectid, owner, offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1056 | } |
| 1057 | return ret; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * helper to update/remove inline back ref |
| 1062 | */ |
| 1063 | static noinline_for_stack |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1064 | void update_inline_extent_backref(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1065 | struct btrfs_extent_inline_ref *iref, |
| 1066 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1067 | struct btrfs_delayed_extent_op *extent_op, |
| 1068 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1069 | { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1070 | struct extent_buffer *leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1071 | struct btrfs_extent_item *ei; |
| 1072 | struct btrfs_extent_data_ref *dref = NULL; |
| 1073 | struct btrfs_shared_data_ref *sref = NULL; |
| 1074 | unsigned long ptr; |
| 1075 | unsigned long end; |
| 1076 | u32 item_size; |
| 1077 | int size; |
| 1078 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1079 | u64 refs; |
| 1080 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1081 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1082 | refs = btrfs_extent_refs(leaf, ei); |
| 1083 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1084 | refs += refs_to_mod; |
| 1085 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1086 | if (extent_op) |
| 1087 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1088 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1089 | /* |
| 1090 | * If type is invalid, we should have bailed out after |
| 1091 | * lookup_inline_extent_backref(). |
| 1092 | */ |
| 1093 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY); |
| 1094 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1095 | |
| 1096 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1097 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1098 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1099 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1100 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1101 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1102 | } else { |
| 1103 | refs = 1; |
| 1104 | BUG_ON(refs_to_mod != -1); |
| 1105 | } |
| 1106 | |
| 1107 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1108 | refs += refs_to_mod; |
| 1109 | |
| 1110 | if (refs > 0) { |
| 1111 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1112 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1113 | else |
| 1114 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1115 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1116 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1117 | size = btrfs_extent_inline_ref_size(type); |
| 1118 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1119 | ptr = (unsigned long)iref; |
| 1120 | end = (unsigned long)ei + item_size; |
| 1121 | if (ptr + size < end) |
| 1122 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1123 | end - ptr - size); |
| 1124 | item_size -= size; |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 1125 | btrfs_truncate_item(path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1126 | } |
| 1127 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static noinline_for_stack |
| 1131 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1132 | struct btrfs_path *path, |
| 1133 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1134 | u64 root_objectid, u64 owner, |
| 1135 | u64 offset, int refs_to_add, |
| 1136 | struct btrfs_delayed_extent_op *extent_op) |
| 1137 | { |
| 1138 | struct btrfs_extent_inline_ref *iref; |
| 1139 | int ret; |
| 1140 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1141 | ret = lookup_inline_extent_backref(trans, path, &iref, bytenr, |
| 1142 | num_bytes, parent, root_objectid, |
| 1143 | owner, offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1144 | if (ret == 0) { |
Qu Wenruo | 07cce5c | 2020-08-19 14:35:49 +0800 | [diff] [blame] | 1145 | /* |
| 1146 | * We're adding refs to a tree block we already own, this |
| 1147 | * should not happen at all. |
| 1148 | */ |
| 1149 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1150 | btrfs_crit(trans->fs_info, |
| 1151 | "adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu", |
| 1152 | bytenr, num_bytes, root_objectid); |
| 1153 | if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) { |
| 1154 | WARN_ON(1); |
| 1155 | btrfs_crit(trans->fs_info, |
| 1156 | "path->slots[0]=%d path->nodes[0]:", path->slots[0]); |
| 1157 | btrfs_print_leaf(path->nodes[0]); |
| 1158 | } |
| 1159 | return -EUCLEAN; |
| 1160 | } |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1161 | update_inline_extent_backref(path, iref, refs_to_add, |
| 1162 | extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1163 | } else if (ret == -ENOENT) { |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1164 | setup_inline_extent_backref(trans->fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1165 | root_objectid, owner, offset, |
| 1166 | refs_to_add, extent_op); |
| 1167 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1168 | } |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1172 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1173 | struct btrfs_path *path, |
| 1174 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1175 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1176 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1177 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1178 | |
| 1179 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1180 | if (iref) { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1181 | update_inline_extent_backref(path, iref, -refs_to_drop, NULL, |
| 1182 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1183 | } else if (is_data) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 1184 | ret = remove_extent_data_ref(trans, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1185 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1186 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1187 | *last_ref = 1; |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 1188 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1189 | } |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1193 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1194 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1195 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1196 | int j, ret = 0; |
| 1197 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1198 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1199 | |
| 1200 | if (WARN_ON(start != aligned_start)) { |
| 1201 | len -= aligned_start - start; |
| 1202 | len = round_down(len, 1 << 9); |
| 1203 | start = aligned_start; |
| 1204 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1205 | |
| 1206 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1207 | |
| 1208 | if (!len) |
| 1209 | return 0; |
| 1210 | |
| 1211 | end = start + len; |
| 1212 | bytes_left = len; |
| 1213 | |
| 1214 | /* Skip any superblocks on this device. */ |
| 1215 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1216 | u64 sb_start = btrfs_sb_offset(j); |
| 1217 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1218 | u64 size = sb_start - start; |
| 1219 | |
| 1220 | if (!in_range(sb_start, start, bytes_left) && |
| 1221 | !in_range(sb_end, start, bytes_left) && |
| 1222 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1223 | continue; |
| 1224 | |
| 1225 | /* |
| 1226 | * Superblock spans beginning of range. Adjust start and |
| 1227 | * try again. |
| 1228 | */ |
| 1229 | if (sb_start <= start) { |
| 1230 | start += sb_end - start; |
| 1231 | if (start > end) { |
| 1232 | bytes_left = 0; |
| 1233 | break; |
| 1234 | } |
| 1235 | bytes_left = end - start; |
| 1236 | continue; |
| 1237 | } |
| 1238 | |
| 1239 | if (size) { |
| 1240 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1241 | GFP_NOFS, 0); |
| 1242 | if (!ret) |
| 1243 | *discarded_bytes += size; |
| 1244 | else if (ret != -EOPNOTSUPP) |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | start = sb_end; |
| 1249 | if (start > end) { |
| 1250 | bytes_left = 0; |
| 1251 | break; |
| 1252 | } |
| 1253 | bytes_left = end - start; |
| 1254 | } |
| 1255 | |
| 1256 | if (bytes_left) { |
| 1257 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1258 | GFP_NOFS, 0); |
| 1259 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1260 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1261 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1262 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1263 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1264 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1265 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 1266 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1267 | { |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1268 | int ret = 0; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1269 | u64 discarded_bytes = 0; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1270 | u64 end = bytenr + num_bytes; |
| 1271 | u64 cur = bytenr; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1272 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1273 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1274 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 1275 | /* |
| 1276 | * Avoid races with device replace and make sure our bbio has devices |
| 1277 | * associated to its stripes that don't go away while we are discarding. |
| 1278 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1279 | btrfs_bio_counter_inc_blocked(fs_info); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1280 | while (cur < end) { |
| 1281 | struct btrfs_bio_stripe *stripe; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1282 | int i; |
| 1283 | |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1284 | num_bytes = end - cur; |
| 1285 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1286 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur, |
| 1287 | &num_bytes, &bbio, 0); |
| 1288 | /* |
| 1289 | * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or |
| 1290 | * -EOPNOTSUPP. For any such error, @num_bytes is not updated, |
| 1291 | * thus we can't continue anyway. |
| 1292 | */ |
| 1293 | if (ret < 0) |
| 1294 | goto out; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1295 | |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1296 | stripe = bbio->stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1297 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1298 | u64 bytes; |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1299 | struct request_queue *req_q; |
Anand Jain | fe910d2 | 2021-04-30 19:59:51 +0800 | [diff] [blame] | 1300 | struct btrfs_device *device = stripe->dev; |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1301 | |
Anand Jain | fe910d2 | 2021-04-30 19:59:51 +0800 | [diff] [blame] | 1302 | if (!device->bdev) { |
Filipe Manana | 627e087 | 2018-01-30 18:40:22 +0000 | [diff] [blame] | 1303 | ASSERT(btrfs_test_opt(fs_info, DEGRADED)); |
| 1304 | continue; |
| 1305 | } |
Anand Jain | fe910d2 | 2021-04-30 19:59:51 +0800 | [diff] [blame] | 1306 | req_q = bdev_get_queue(device->bdev); |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1307 | if (!blk_queue_discard(req_q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1308 | continue; |
| 1309 | |
Anand Jain | fe910d2 | 2021-04-30 19:59:51 +0800 | [diff] [blame] | 1310 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
| 1311 | continue; |
| 1312 | |
| 1313 | ret = btrfs_issue_discard(device->bdev, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1314 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1315 | stripe->length, |
| 1316 | &bytes); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1317 | if (!ret) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1318 | discarded_bytes += bytes; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1319 | } else if (ret != -EOPNOTSUPP) { |
| 1320 | /* |
| 1321 | * Logic errors or -ENOMEM, or -EIO, but |
| 1322 | * unlikely to happen. |
| 1323 | * |
| 1324 | * And since there are two loops, explicitly |
| 1325 | * go to out to avoid confusion. |
| 1326 | */ |
| 1327 | btrfs_put_bbio(bbio); |
| 1328 | goto out; |
| 1329 | } |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1330 | |
| 1331 | /* |
| 1332 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1333 | * just ignore the return value so we don't screw up |
| 1334 | * people calling discard_extent. |
| 1335 | */ |
| 1336 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1337 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1338 | btrfs_put_bbio(bbio); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1339 | cur += num_bytes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1340 | } |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1341 | out: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1342 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (actual_bytes) |
| 1345 | *actual_bytes = discarded_bytes; |
| 1346 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1347 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1348 | if (ret == -EOPNOTSUPP) |
| 1349 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1350 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1351 | } |
| 1352 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1353 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1354 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1355 | struct btrfs_ref *generic_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1356 | { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1357 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1358 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1359 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1360 | ASSERT(generic_ref->type != BTRFS_REF_NOT_SET && |
| 1361 | generic_ref->action); |
| 1362 | BUG_ON(generic_ref->type == BTRFS_REF_METADATA && |
| 1363 | generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1364 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1365 | if (generic_ref->type == BTRFS_REF_METADATA) |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 1366 | ret = btrfs_add_delayed_tree_ref(trans, generic_ref, NULL); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1367 | else |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 1368 | ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1369 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1370 | btrfs_ref_tree_mod(fs_info, generic_ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 1371 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1372 | return ret; |
| 1373 | } |
| 1374 | |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1375 | /* |
| 1376 | * __btrfs_inc_extent_ref - insert backreference for a given extent |
| 1377 | * |
Qu Wenruo | 07cce5c | 2020-08-19 14:35:49 +0800 | [diff] [blame] | 1378 | * The counterpart is in __btrfs_free_extent(), with examples and more details |
| 1379 | * how it works. |
| 1380 | * |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1381 | * @trans: Handle of transaction |
| 1382 | * |
| 1383 | * @node: The delayed ref node used to get the bytenr/length for |
| 1384 | * extent whose references are incremented. |
| 1385 | * |
| 1386 | * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/ |
| 1387 | * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical |
| 1388 | * bytenr of the parent block. Since new extents are always |
| 1389 | * created with indirect references, this will only be the case |
| 1390 | * when relocating a shared extent. In that case, root_objectid |
| 1391 | * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must |
| 1392 | * be 0 |
| 1393 | * |
| 1394 | * @root_objectid: The id of the root where this modification has originated, |
| 1395 | * this can be either one of the well-known metadata trees or |
| 1396 | * the subvolume id which references this extent. |
| 1397 | * |
| 1398 | * @owner: For data extents it is the inode number of the owning file. |
| 1399 | * For metadata extents this parameter holds the level in the |
| 1400 | * tree of the extent. |
| 1401 | * |
| 1402 | * @offset: For metadata extents the offset is ignored and is currently |
| 1403 | * always passed as 0. For data extents it is the fileoffset |
| 1404 | * this extent belongs to. |
| 1405 | * |
| 1406 | * @refs_to_add Number of references to add |
| 1407 | * |
| 1408 | * @extent_op Pointer to a structure, holding information necessary when |
| 1409 | * updating a tree block's flags |
| 1410 | * |
| 1411 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1412 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1413 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1414 | u64 parent, u64 root_objectid, |
| 1415 | u64 owner, u64 offset, int refs_to_add, |
| 1416 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1417 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1418 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1419 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1420 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1421 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1422 | u64 bytenr = node->bytenr; |
| 1423 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1424 | u64 refs; |
| 1425 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1426 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1427 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1428 | if (!path) |
| 1429 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1430 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1431 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1432 | /* this will setup the path even if it fails to insert the back ref */ |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1433 | ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, |
| 1434 | parent, root_objectid, owner, |
| 1435 | offset, refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 1436 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1437 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1438 | |
| 1439 | /* |
| 1440 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 1441 | * inline extent ref, so just update the reference count and add a |
| 1442 | * normal backref. |
| 1443 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1444 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1445 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1446 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1447 | refs = btrfs_extent_refs(leaf, item); |
| 1448 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1449 | if (extent_op) |
| 1450 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1451 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1452 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1453 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1454 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1455 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1456 | /* now insert the actual backref */ |
Nikolay Borisov | 65cd6d9 | 2020-03-04 11:33:53 +0200 | [diff] [blame] | 1457 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1458 | BUG_ON(refs_to_add != 1); |
| 1459 | ret = insert_tree_block_ref(trans, path, bytenr, parent, |
| 1460 | root_objectid); |
| 1461 | } else { |
| 1462 | ret = insert_extent_data_ref(trans, path, bytenr, parent, |
| 1463 | root_objectid, owner, offset, |
| 1464 | refs_to_add); |
| 1465 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1466 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1467 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1468 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1469 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 1470 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1471 | } |
| 1472 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1473 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1474 | struct btrfs_delayed_ref_node *node, |
| 1475 | struct btrfs_delayed_extent_op *extent_op, |
| 1476 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1477 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1478 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1479 | struct btrfs_delayed_data_ref *ref; |
| 1480 | struct btrfs_key ins; |
| 1481 | u64 parent = 0; |
| 1482 | u64 ref_root = 0; |
| 1483 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1484 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1485 | ins.objectid = node->bytenr; |
| 1486 | ins.offset = node->num_bytes; |
| 1487 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1488 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1489 | ref = btrfs_delayed_node_to_data_ref(node); |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1490 | trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1491 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1492 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1493 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1494 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1495 | |
| 1496 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1497 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1498 | flags |= extent_op->flags_to_set; |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 1499 | ret = alloc_reserved_file_extent(trans, parent, ref_root, |
| 1500 | flags, ref->objectid, |
| 1501 | ref->offset, &ins, |
| 1502 | node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1503 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1504 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1505 | ref->objectid, ref->offset, |
| 1506 | node->ref_mod, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1507 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1508 | ret = __btrfs_free_extent(trans, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1509 | ref_root, ref->objectid, |
| 1510 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1511 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1512 | } else { |
| 1513 | BUG(); |
| 1514 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1515 | return ret; |
| 1516 | } |
| 1517 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1518 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1519 | struct extent_buffer *leaf, |
| 1520 | struct btrfs_extent_item *ei) |
| 1521 | { |
| 1522 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1523 | if (extent_op->update_flags) { |
| 1524 | flags |= extent_op->flags_to_set; |
| 1525 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1526 | } |
| 1527 | |
| 1528 | if (extent_op->update_key) { |
| 1529 | struct btrfs_tree_block_info *bi; |
| 1530 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1531 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1532 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1537 | struct btrfs_delayed_ref_head *head, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1538 | struct btrfs_delayed_extent_op *extent_op) |
| 1539 | { |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1540 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1541 | struct btrfs_key key; |
| 1542 | struct btrfs_path *path; |
| 1543 | struct btrfs_extent_item *ei; |
| 1544 | struct extent_buffer *leaf; |
| 1545 | u32 item_size; |
| 1546 | int ret; |
| 1547 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1548 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1549 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 1550 | if (TRANS_ABORTED(trans)) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1551 | return 0; |
| 1552 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1553 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1554 | metadata = 0; |
| 1555 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1556 | path = btrfs_alloc_path(); |
| 1557 | if (!path) |
| 1558 | return -ENOMEM; |
| 1559 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1560 | key.objectid = head->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1561 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1562 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1563 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1564 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1565 | } else { |
| 1566 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1567 | key.offset = head->num_bytes; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1571 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1572 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1573 | if (ret < 0) { |
| 1574 | err = ret; |
| 1575 | goto out; |
| 1576 | } |
| 1577 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1578 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1579 | if (path->slots[0] > 0) { |
| 1580 | path->slots[0]--; |
| 1581 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1582 | path->slots[0]); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1583 | if (key.objectid == head->bytenr && |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1584 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1585 | key.offset == head->num_bytes) |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1586 | ret = 0; |
| 1587 | } |
| 1588 | if (ret > 0) { |
| 1589 | btrfs_release_path(path); |
| 1590 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1591 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1592 | key.objectid = head->bytenr; |
| 1593 | key.offset = head->num_bytes; |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1594 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1595 | goto again; |
| 1596 | } |
| 1597 | } else { |
| 1598 | err = -EIO; |
| 1599 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1600 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | leaf = path->nodes[0]; |
| 1604 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1605 | |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 1606 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1607 | err = -EINVAL; |
| 1608 | btrfs_print_v0_err(fs_info); |
| 1609 | btrfs_abort_transaction(trans, err); |
| 1610 | goto out; |
| 1611 | } |
| 1612 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1613 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1614 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1615 | |
| 1616 | btrfs_mark_buffer_dirty(leaf); |
| 1617 | out: |
| 1618 | btrfs_free_path(path); |
| 1619 | return err; |
| 1620 | } |
| 1621 | |
| 1622 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1623 | struct btrfs_delayed_ref_node *node, |
| 1624 | struct btrfs_delayed_extent_op *extent_op, |
| 1625 | int insert_reserved) |
| 1626 | { |
| 1627 | int ret = 0; |
| 1628 | struct btrfs_delayed_tree_ref *ref; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1629 | u64 parent = 0; |
| 1630 | u64 ref_root = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1631 | |
| 1632 | ref = btrfs_delayed_node_to_tree_ref(node); |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1633 | trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1634 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1635 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1636 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1637 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1638 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1639 | if (node->ref_mod != 1) { |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1640 | btrfs_err(trans->fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1641 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 1642 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 1643 | parent); |
| 1644 | return -EIO; |
| 1645 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1646 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1647 | BUG_ON(!extent_op || !extent_op->update_flags); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 1648 | ret = alloc_reserved_tree_block(trans, node, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1649 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1650 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1651 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1652 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1653 | ret = __btrfs_free_extent(trans, node, parent, ref_root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1654 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1655 | } else { |
| 1656 | BUG(); |
| 1657 | } |
| 1658 | return ret; |
| 1659 | } |
| 1660 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1661 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1662 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1663 | struct btrfs_delayed_ref_node *node, |
| 1664 | struct btrfs_delayed_extent_op *extent_op, |
| 1665 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1666 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1667 | int ret = 0; |
| 1668 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 1669 | if (TRANS_ABORTED(trans)) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1670 | if (insert_reserved) |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 1671 | btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1672 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1673 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1674 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1675 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1676 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1677 | ret = run_delayed_tree_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1678 | insert_reserved); |
| 1679 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1680 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1681 | ret = run_delayed_data_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1682 | insert_reserved); |
| 1683 | else |
| 1684 | BUG(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 1685 | if (ret && insert_reserved) |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 1686 | btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1687 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1688 | } |
| 1689 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1690 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1691 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1692 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1693 | struct btrfs_delayed_ref_node *ref; |
| 1694 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1695 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1696 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1697 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1698 | /* |
| 1699 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1700 | * This is to prevent a ref count from going down to zero, which deletes |
| 1701 | * the extent item from the extent tree, when there still are references |
| 1702 | * to add, which would fail because they would not find the extent item. |
| 1703 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1704 | if (!list_empty(&head->ref_add_list)) |
| 1705 | return list_first_entry(&head->ref_add_list, |
| 1706 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1707 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1708 | ref = rb_entry(rb_first_cached(&head->ref_tree), |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 1709 | struct btrfs_delayed_ref_node, ref_node); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1710 | ASSERT(list_empty(&ref->add_list)); |
| 1711 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1712 | } |
| 1713 | |
Josef Bacik | 2eadaa2 | 2017-09-29 15:43:52 -0400 | [diff] [blame] | 1714 | static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
| 1715 | struct btrfs_delayed_ref_head *head) |
| 1716 | { |
| 1717 | spin_lock(&delayed_refs->lock); |
| 1718 | head->processing = 0; |
| 1719 | delayed_refs->num_heads_ready++; |
| 1720 | spin_unlock(&delayed_refs->lock); |
| 1721 | btrfs_delayed_ref_unlock(head); |
| 1722 | } |
| 1723 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1724 | static struct btrfs_delayed_extent_op *cleanup_extent_op( |
| 1725 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1726 | { |
| 1727 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1728 | |
| 1729 | if (!extent_op) |
| 1730 | return NULL; |
| 1731 | |
| 1732 | if (head->must_insert_reserved) { |
| 1733 | head->extent_op = NULL; |
| 1734 | btrfs_free_delayed_extent_op(extent_op); |
| 1735 | return NULL; |
| 1736 | } |
| 1737 | return extent_op; |
| 1738 | } |
| 1739 | |
| 1740 | static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, |
| 1741 | struct btrfs_delayed_ref_head *head) |
| 1742 | { |
| 1743 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1744 | int ret; |
| 1745 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1746 | extent_op = cleanup_extent_op(head); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1747 | if (!extent_op) |
| 1748 | return 0; |
| 1749 | head->extent_op = NULL; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1750 | spin_unlock(&head->lock); |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1751 | ret = run_delayed_extent_op(trans, head, extent_op); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1752 | btrfs_free_delayed_extent_op(extent_op); |
| 1753 | return ret ? ret : 1; |
| 1754 | } |
| 1755 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1756 | void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, |
| 1757 | struct btrfs_delayed_ref_root *delayed_refs, |
| 1758 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1759 | { |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1760 | int nr_items = 1; /* Dropping this ref head update. */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1761 | |
Josef Bacik | 9a73991 | 2021-01-15 16:48:56 -0500 | [diff] [blame] | 1762 | /* |
| 1763 | * We had csum deletions accounted for in our delayed refs rsv, we need |
| 1764 | * to drop the csum leaves for this update from our delayed_refs_rsv. |
| 1765 | */ |
| 1766 | if (head->total_ref_mod < 0 && head->is_data) { |
| 1767 | spin_lock(&delayed_refs->lock); |
| 1768 | delayed_refs->pending_csums -= head->num_bytes; |
| 1769 | spin_unlock(&delayed_refs->lock); |
| 1770 | nr_items += btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes); |
| 1771 | } |
| 1772 | |
| 1773 | /* |
| 1774 | * We were dropping refs, or had a new ref and dropped it, and thus must |
| 1775 | * adjust down our total_bytes_pinned, the space may or may not have |
| 1776 | * been pinned and so is accounted for properly in the pinned space by |
| 1777 | * now. |
| 1778 | */ |
| 1779 | if (head->total_ref_mod < 0 || |
| 1780 | (head->total_ref_mod == 0 && head->must_insert_reserved)) { |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 1781 | u64 flags = btrfs_ref_head_to_space_flags(head); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1782 | |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 1783 | btrfs_mod_total_bytes_pinned(fs_info, flags, -head->num_bytes); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1784 | } |
| 1785 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1786 | btrfs_delayed_refs_rsv_release(fs_info, nr_items); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1787 | } |
| 1788 | |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1789 | static int cleanup_ref_head(struct btrfs_trans_handle *trans, |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1790 | struct btrfs_delayed_ref_head *head) |
| 1791 | { |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 1792 | |
| 1793 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1794 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1795 | int ret; |
| 1796 | |
| 1797 | delayed_refs = &trans->transaction->delayed_refs; |
| 1798 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1799 | ret = run_and_cleanup_extent_op(trans, head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1800 | if (ret < 0) { |
| 1801 | unselect_delayed_ref_head(delayed_refs, head); |
| 1802 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
| 1803 | return ret; |
| 1804 | } else if (ret) { |
| 1805 | return ret; |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | * Need to drop our head ref lock and re-acquire the delayed ref lock |
| 1810 | * and then re-check to make sure nobody got added. |
| 1811 | */ |
| 1812 | spin_unlock(&head->lock); |
| 1813 | spin_lock(&delayed_refs->lock); |
| 1814 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1815 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) { |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1816 | spin_unlock(&head->lock); |
| 1817 | spin_unlock(&delayed_refs->lock); |
| 1818 | return 1; |
| 1819 | } |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 1820 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1821 | spin_unlock(&head->lock); |
Nikolay Borisov | 1e7a142 | 2018-04-11 11:21:18 +0300 | [diff] [blame] | 1822 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1823 | |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1824 | if (head->must_insert_reserved) { |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 1825 | btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1826 | if (head->is_data) { |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 1827 | ret = btrfs_del_csums(trans, fs_info->csum_root, |
| 1828 | head->bytenr, head->num_bytes); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1832 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1833 | |
| 1834 | trace_run_delayed_ref_head(fs_info, head, 0); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1835 | btrfs_delayed_ref_unlock(head); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1836 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | 466d83f | 2021-05-19 10:52:46 -0400 | [diff] [blame] | 1837 | return ret; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1838 | } |
| 1839 | |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1840 | static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( |
| 1841 | struct btrfs_trans_handle *trans) |
| 1842 | { |
| 1843 | struct btrfs_delayed_ref_root *delayed_refs = |
| 1844 | &trans->transaction->delayed_refs; |
| 1845 | struct btrfs_delayed_ref_head *head = NULL; |
| 1846 | int ret; |
| 1847 | |
| 1848 | spin_lock(&delayed_refs->lock); |
Lu Fengqi | 5637c74 | 2018-10-11 13:40:33 +0800 | [diff] [blame] | 1849 | head = btrfs_select_ref_head(delayed_refs); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1850 | if (!head) { |
| 1851 | spin_unlock(&delayed_refs->lock); |
| 1852 | return head; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | * Grab the lock that says we are going to process all the refs for |
| 1857 | * this head |
| 1858 | */ |
Lu Fengqi | 9e920a6 | 2018-10-11 13:40:34 +0800 | [diff] [blame] | 1859 | ret = btrfs_delayed_ref_lock(delayed_refs, head); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1860 | spin_unlock(&delayed_refs->lock); |
| 1861 | |
| 1862 | /* |
| 1863 | * We may have dropped the spin lock to get the head mutex lock, and |
| 1864 | * that might have given someone else time to free the head. If that's |
| 1865 | * true, it has been removed from our list and we can move on. |
| 1866 | */ |
| 1867 | if (ret == -EAGAIN) |
| 1868 | head = ERR_PTR(-EAGAIN); |
| 1869 | |
| 1870 | return head; |
| 1871 | } |
| 1872 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1873 | static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, |
| 1874 | struct btrfs_delayed_ref_head *locked_ref, |
| 1875 | unsigned long *run_refs) |
| 1876 | { |
| 1877 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1878 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1879 | struct btrfs_delayed_extent_op *extent_op; |
| 1880 | struct btrfs_delayed_ref_node *ref; |
| 1881 | int must_insert_reserved = 0; |
| 1882 | int ret; |
| 1883 | |
| 1884 | delayed_refs = &trans->transaction->delayed_refs; |
| 1885 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1886 | lockdep_assert_held(&locked_ref->mutex); |
| 1887 | lockdep_assert_held(&locked_ref->lock); |
| 1888 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1889 | while ((ref = select_delayed_ref(locked_ref))) { |
| 1890 | if (ref->seq && |
| 1891 | btrfs_check_delayed_seq(fs_info, ref->seq)) { |
| 1892 | spin_unlock(&locked_ref->lock); |
| 1893 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1894 | return -EAGAIN; |
| 1895 | } |
| 1896 | |
| 1897 | (*run_refs)++; |
| 1898 | ref->in_tree = 0; |
| 1899 | rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree); |
| 1900 | RB_CLEAR_NODE(&ref->ref_node); |
| 1901 | if (!list_empty(&ref->add_list)) |
| 1902 | list_del(&ref->add_list); |
| 1903 | /* |
| 1904 | * When we play the delayed ref, also correct the ref_mod on |
| 1905 | * head |
| 1906 | */ |
| 1907 | switch (ref->action) { |
| 1908 | case BTRFS_ADD_DELAYED_REF: |
| 1909 | case BTRFS_ADD_DELAYED_EXTENT: |
| 1910 | locked_ref->ref_mod -= ref->ref_mod; |
| 1911 | break; |
| 1912 | case BTRFS_DROP_DELAYED_REF: |
| 1913 | locked_ref->ref_mod += ref->ref_mod; |
| 1914 | break; |
| 1915 | default: |
| 1916 | WARN_ON(1); |
| 1917 | } |
| 1918 | atomic_dec(&delayed_refs->num_entries); |
| 1919 | |
| 1920 | /* |
| 1921 | * Record the must_insert_reserved flag before we drop the |
| 1922 | * spin lock. |
| 1923 | */ |
| 1924 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1925 | locked_ref->must_insert_reserved = 0; |
| 1926 | |
| 1927 | extent_op = locked_ref->extent_op; |
| 1928 | locked_ref->extent_op = NULL; |
| 1929 | spin_unlock(&locked_ref->lock); |
| 1930 | |
| 1931 | ret = run_one_delayed_ref(trans, ref, extent_op, |
| 1932 | must_insert_reserved); |
| 1933 | |
| 1934 | btrfs_free_delayed_extent_op(extent_op); |
| 1935 | if (ret) { |
| 1936 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1937 | btrfs_put_delayed_ref(ref); |
| 1938 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 1939 | ret); |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
| 1943 | btrfs_put_delayed_ref(ref); |
| 1944 | cond_resched(); |
| 1945 | |
| 1946 | spin_lock(&locked_ref->lock); |
| 1947 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
| 1948 | } |
| 1949 | |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1953 | /* |
| 1954 | * Returns 0 on success or if called with an already aborted transaction. |
| 1955 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 1956 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1957 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1958 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1959 | { |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 1960 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1961 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1962 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 1963 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1964 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1965 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 1966 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1967 | |
| 1968 | delayed_refs = &trans->transaction->delayed_refs; |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1969 | do { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1970 | if (!locked_ref) { |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1971 | locked_ref = btrfs_obtain_ref_head(trans); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1972 | if (IS_ERR_OR_NULL(locked_ref)) { |
| 1973 | if (PTR_ERR(locked_ref) == -EAGAIN) { |
| 1974 | continue; |
| 1975 | } else { |
| 1976 | break; |
| 1977 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1978 | } |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1979 | count++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1980 | } |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 1981 | /* |
| 1982 | * We need to try and merge add/drops of the same ref since we |
| 1983 | * can run into issues with relocate dropping the implicit ref |
| 1984 | * and then it being added back again before the drop can |
| 1985 | * finish. If we merged anything we need to re-loop so we can |
| 1986 | * get a good ref. |
| 1987 | * Or we can get node references of the same type that weren't |
| 1988 | * merged when created due to bumps in the tree mod seq, and |
| 1989 | * we need to merge them to prevent adding an inline extent |
| 1990 | * backref before dropping it (triggering a BUG_ON at |
| 1991 | * insert_inline_extent_backref()). |
| 1992 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1993 | spin_lock(&locked_ref->lock); |
Nikolay Borisov | be97f13 | 2018-04-19 11:06:39 +0300 | [diff] [blame] | 1994 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 1995 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1996 | ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, |
| 1997 | &actual_count); |
| 1998 | if (ret < 0 && ret != -EAGAIN) { |
| 1999 | /* |
| 2000 | * Error, btrfs_run_delayed_refs_for_head already |
| 2001 | * unlocked everything so just bail out |
| 2002 | */ |
| 2003 | return ret; |
| 2004 | } else if (!ret) { |
| 2005 | /* |
| 2006 | * Success, perform the usual cleanup of a processed |
| 2007 | * head |
| 2008 | */ |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 2009 | ret = cleanup_ref_head(trans, locked_ref); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2010 | if (ret > 0 ) { |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2011 | /* We dropped our lock, we need to loop. */ |
| 2012 | ret = 0; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2013 | continue; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2014 | } else if (ret) { |
| 2015 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2016 | } |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2017 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2018 | |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2019 | /* |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2020 | * Either success case or btrfs_run_delayed_refs_for_head |
| 2021 | * returned -EAGAIN, meaning we need to select another head |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2022 | */ |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2023 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2024 | locked_ref = NULL; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2025 | cond_resched(); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2026 | } while ((nr != -1 && count < nr) || locked_ref); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2027 | |
| 2028 | /* |
| 2029 | * We don't want to include ref heads since we can have empty ref heads |
| 2030 | * and those will drastically skew our runtime down since we just do |
| 2031 | * accounting, no actual extent tree updates. |
| 2032 | */ |
| 2033 | if (actual_count > 0) { |
| 2034 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2035 | u64 avg; |
| 2036 | |
| 2037 | /* |
| 2038 | * We weigh the current average higher than our current runtime |
| 2039 | * to avoid large swings in the average. |
| 2040 | */ |
| 2041 | spin_lock(&delayed_refs->lock); |
| 2042 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2043 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2044 | spin_unlock(&delayed_refs->lock); |
| 2045 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2046 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2047 | } |
| 2048 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2049 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2050 | /* |
| 2051 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2052 | * correlates in most cases to the order added. To expose dependencies on this |
| 2053 | * order, we start to process the tree in the middle instead of the beginning |
| 2054 | */ |
| 2055 | static u64 find_middle(struct rb_root *root) |
| 2056 | { |
| 2057 | struct rb_node *n = root->rb_node; |
| 2058 | struct btrfs_delayed_ref_node *entry; |
| 2059 | int alt = 1; |
| 2060 | u64 middle; |
| 2061 | u64 first = 0, last = 0; |
| 2062 | |
| 2063 | n = rb_first(root); |
| 2064 | if (n) { |
| 2065 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2066 | first = entry->bytenr; |
| 2067 | } |
| 2068 | n = rb_last(root); |
| 2069 | if (n) { |
| 2070 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2071 | last = entry->bytenr; |
| 2072 | } |
| 2073 | n = root->rb_node; |
| 2074 | |
| 2075 | while (n) { |
| 2076 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2077 | WARN_ON(!entry->in_tree); |
| 2078 | |
| 2079 | middle = entry->bytenr; |
| 2080 | |
| 2081 | if (alt) |
| 2082 | n = n->rb_left; |
| 2083 | else |
| 2084 | n = n->rb_right; |
| 2085 | |
| 2086 | alt = 1 - alt; |
| 2087 | } |
| 2088 | return middle; |
| 2089 | } |
| 2090 | #endif |
| 2091 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2092 | /* |
| 2093 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2094 | * would require to store the csums for that many bytes. |
| 2095 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2096 | u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes) |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2097 | { |
| 2098 | u64 csum_size; |
| 2099 | u64 num_csums_per_leaf; |
| 2100 | u64 num_csums; |
| 2101 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2102 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2103 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2104 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2105 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2106 | num_csums += num_csums_per_leaf - 1; |
| 2107 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2108 | return num_csums; |
| 2109 | } |
| 2110 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2111 | /* |
| 2112 | * this starts processing the delayed reference count updates and |
| 2113 | * extent insertions we have queued up so far. count can be |
| 2114 | * 0, which means to process everything in the tree at the start |
| 2115 | * of the run (but not newly added entries), or it can be some target |
| 2116 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2117 | * |
| 2118 | * Returns 0 on success or if called with an aborted transaction |
| 2119 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2120 | */ |
| 2121 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2122 | unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2123 | { |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2124 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2125 | struct rb_node *node; |
| 2126 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2127 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2128 | int ret; |
| 2129 | int run_all = count == (unsigned long)-1; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2130 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2131 | /* We'll clean this up in btrfs_cleanup_transaction */ |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 2132 | if (TRANS_ABORTED(trans)) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2133 | return 0; |
| 2134 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2135 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2136 | return 0; |
| 2137 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2138 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2139 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2140 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2141 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2142 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2143 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2144 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2145 | #endif |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2146 | ret = __btrfs_run_delayed_refs(trans, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2147 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2148 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2149 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2150 | } |
| 2151 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2152 | if (run_all) { |
Josef Bacik | 119e80d | 2018-11-21 14:05:42 -0500 | [diff] [blame] | 2153 | btrfs_create_pending_block_groups(trans); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2154 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2155 | spin_lock(&delayed_refs->lock); |
Liu Bo | 5c9d028 | 2018-08-23 03:51:49 +0800 | [diff] [blame] | 2156 | node = rb_first_cached(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2157 | if (!node) { |
| 2158 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2159 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2160 | } |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2161 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2162 | href_node); |
| 2163 | refcount_inc(&head->refs); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2164 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2165 | |
| 2166 | /* Mutex was contended, block until it's released and retry. */ |
| 2167 | mutex_lock(&head->mutex); |
| 2168 | mutex_unlock(&head->mutex); |
| 2169 | |
| 2170 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2171 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2172 | goto again; |
| 2173 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2174 | out: |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2175 | return 0; |
| 2176 | } |
| 2177 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2178 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 2179 | struct extent_buffer *eb, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2180 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2181 | { |
| 2182 | struct btrfs_delayed_extent_op *extent_op; |
| 2183 | int ret; |
| 2184 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2185 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2186 | if (!extent_op) |
| 2187 | return -ENOMEM; |
| 2188 | |
| 2189 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 2190 | extent_op->update_flags = true; |
| 2191 | extent_op->update_key = false; |
| 2192 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2193 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2194 | |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 2195 | ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2196 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2197 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2198 | return ret; |
| 2199 | } |
| 2200 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2201 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2202 | struct btrfs_path *path, |
| 2203 | u64 objectid, u64 offset, u64 bytenr) |
| 2204 | { |
| 2205 | struct btrfs_delayed_ref_head *head; |
| 2206 | struct btrfs_delayed_ref_node *ref; |
| 2207 | struct btrfs_delayed_data_ref *data_ref; |
| 2208 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2209 | struct btrfs_transaction *cur_trans; |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2210 | struct rb_node *node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2211 | int ret = 0; |
| 2212 | |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2213 | spin_lock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2214 | cur_trans = root->fs_info->running_transaction; |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2215 | if (cur_trans) |
| 2216 | refcount_inc(&cur_trans->use_count); |
| 2217 | spin_unlock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2218 | if (!cur_trans) |
| 2219 | return 0; |
| 2220 | |
| 2221 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2222 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 2223 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2224 | if (!head) { |
| 2225 | spin_unlock(&delayed_refs->lock); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2226 | btrfs_put_transaction(cur_trans); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2227 | return 0; |
| 2228 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2229 | |
| 2230 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2231 | refcount_inc(&head->refs); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2232 | spin_unlock(&delayed_refs->lock); |
| 2233 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2234 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2235 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2236 | /* |
| 2237 | * Mutex was contended, block until it's released and let |
| 2238 | * caller try again |
| 2239 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2240 | mutex_lock(&head->mutex); |
| 2241 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2242 | btrfs_put_delayed_ref_head(head); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2243 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2244 | return -EAGAIN; |
| 2245 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2246 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2247 | |
| 2248 | spin_lock(&head->lock); |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2249 | /* |
| 2250 | * XXX: We should replace this with a proper search function in the |
| 2251 | * future. |
| 2252 | */ |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2253 | for (node = rb_first_cached(&head->ref_tree); node; |
| 2254 | node = rb_next(node)) { |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2255 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2256 | /* If it's a shared ref we know a cross reference exists */ |
| 2257 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 2258 | ret = 1; |
| 2259 | break; |
| 2260 | } |
| 2261 | |
| 2262 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2263 | |
| 2264 | /* |
| 2265 | * If our ref doesn't match the one we're currently looking at |
| 2266 | * then we have a cross reference. |
| 2267 | */ |
| 2268 | if (data_ref->root != root->root_key.objectid || |
| 2269 | data_ref->objectid != objectid || |
| 2270 | data_ref->offset != offset) { |
| 2271 | ret = 1; |
| 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | spin_unlock(&head->lock); |
| 2276 | mutex_unlock(&head->mutex); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2277 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2278 | return ret; |
| 2279 | } |
| 2280 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2281 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2282 | struct btrfs_path *path, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2283 | u64 objectid, u64 offset, u64 bytenr, |
| 2284 | bool strict) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2285 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2286 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2287 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2288 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2289 | struct btrfs_extent_data_ref *ref; |
| 2290 | struct btrfs_extent_inline_ref *iref; |
| 2291 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2292 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2293 | u32 item_size; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2294 | int type; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2295 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2296 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2297 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2298 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2299 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2300 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2301 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2302 | if (ret < 0) |
| 2303 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2304 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2305 | |
| 2306 | ret = -ENOENT; |
| 2307 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2308 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2309 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2310 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2311 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2312 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2313 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2314 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2315 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2316 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2317 | ret = 1; |
| 2318 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2319 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2320 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2321 | /* If extent item has more than 1 inline ref then it's shared */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2322 | if (item_size != sizeof(*ei) + |
| 2323 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2324 | goto out; |
| 2325 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2326 | /* |
| 2327 | * If extent created before last snapshot => it's shared unless the |
| 2328 | * snapshot has been deleted. Use the heuristic if strict is false. |
| 2329 | */ |
| 2330 | if (!strict && |
| 2331 | (btrfs_extent_generation(leaf, ei) <= |
| 2332 | btrfs_root_last_snapshot(&root->root_item))) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2333 | goto out; |
| 2334 | |
| 2335 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2336 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2337 | /* If this extent has SHARED_DATA_REF then it's shared */ |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2338 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 2339 | if (type != BTRFS_EXTENT_DATA_REF_KEY) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2340 | goto out; |
| 2341 | |
| 2342 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2343 | if (btrfs_extent_refs(leaf, ei) != |
| 2344 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2345 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2346 | root->root_key.objectid || |
| 2347 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2348 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2349 | goto out; |
| 2350 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2351 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2352 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2353 | return ret; |
| 2354 | } |
| 2355 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2356 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2357 | u64 bytenr, bool strict) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2358 | { |
| 2359 | struct btrfs_path *path; |
| 2360 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2361 | |
| 2362 | path = btrfs_alloc_path(); |
| 2363 | if (!path) |
Su Yue | 9132c4f | 2018-05-30 14:49:10 +0800 | [diff] [blame] | 2364 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2365 | |
| 2366 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2367 | ret = check_committed_ref(root, path, objectid, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2368 | offset, bytenr, strict); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2369 | if (ret && ret != -ENOENT) |
| 2370 | goto out; |
| 2371 | |
Misono Tomohiro | 380fd06 | 2018-08-30 10:59:16 +0900 | [diff] [blame] | 2372 | ret = check_delayed_ref(root, path, objectid, offset, bytenr); |
| 2373 | } while (ret == -EAGAIN); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2374 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2375 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2376 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2377 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2378 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2379 | return ret; |
| 2380 | } |
| 2381 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2382 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2383 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2384 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2385 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2386 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2387 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2388 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2389 | u64 num_bytes; |
| 2390 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2391 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2392 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2393 | struct btrfs_key key; |
| 2394 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2395 | struct btrfs_ref generic_ref = { 0 }; |
| 2396 | bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2397 | int i; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2398 | int action; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2399 | int level; |
| 2400 | int ret = 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2401 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2402 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 2403 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2404 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2405 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2406 | nritems = btrfs_header_nritems(buf); |
| 2407 | level = btrfs_header_level(buf); |
| 2408 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 2409 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2410 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2411 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2412 | if (full_backref) |
| 2413 | parent = buf->start; |
| 2414 | else |
| 2415 | parent = 0; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2416 | if (inc) |
| 2417 | action = BTRFS_ADD_DELAYED_REF; |
| 2418 | else |
| 2419 | action = BTRFS_DROP_DELAYED_REF; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2420 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2421 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2422 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2423 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 2424 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2425 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2426 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2427 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2428 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2429 | BTRFS_FILE_EXTENT_INLINE) |
| 2430 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2431 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2432 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2433 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2434 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2435 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2436 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2437 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2438 | num_bytes, parent); |
| 2439 | generic_ref.real_root = root->root_key.objectid; |
| 2440 | btrfs_init_data_ref(&generic_ref, ref_root, key.objectid, |
| 2441 | key.offset); |
| 2442 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2443 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2444 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2445 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2446 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2447 | if (ret) |
| 2448 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2449 | } else { |
| 2450 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2451 | num_bytes = fs_info->nodesize; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2452 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2453 | num_bytes, parent); |
| 2454 | generic_ref.real_root = root->root_key.objectid; |
| 2455 | btrfs_init_tree_ref(&generic_ref, level - 1, ref_root); |
| 2456 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2457 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2458 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2459 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2460 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2461 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2462 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2463 | } |
| 2464 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2465 | return 0; |
| 2466 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2467 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2468 | } |
| 2469 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2470 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2471 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2472 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2473 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2474 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2475 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2476 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2477 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2478 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2479 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2480 | } |
| 2481 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2482 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2483 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2484 | struct btrfs_block_group *block_group; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2485 | int readonly = 0; |
| 2486 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2487 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2488 | if (!block_group || block_group->ro) |
| 2489 | readonly = 1; |
| 2490 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2491 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2492 | return readonly; |
| 2493 | } |
| 2494 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 2495 | static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2496 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2497 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2498 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2499 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2500 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2501 | if (data) |
| 2502 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2503 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2504 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2505 | else |
| 2506 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 2507 | |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 2508 | ret = btrfs_get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2509 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2510 | } |
| 2511 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2512 | static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2513 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2514 | struct btrfs_block_group *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2515 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2516 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2517 | spin_lock(&fs_info->block_group_cache_lock); |
| 2518 | bytenr = fs_info->first_logical_byte; |
| 2519 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 2520 | |
| 2521 | if (bytenr < (u64)-1) |
| 2522 | return bytenr; |
| 2523 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2524 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2525 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2526 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2527 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2528 | bytenr = cache->start; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2529 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2530 | |
| 2531 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2532 | } |
| 2533 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 2534 | static int pin_down_extent(struct btrfs_trans_handle *trans, |
| 2535 | struct btrfs_block_group *cache, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2536 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2537 | { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2538 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 2539 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2540 | spin_lock(&cache->space_info->lock); |
| 2541 | spin_lock(&cache->lock); |
| 2542 | cache->pinned += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2543 | btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, |
| 2544 | num_bytes); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2545 | if (reserved) { |
| 2546 | cache->reserved -= num_bytes; |
| 2547 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2548 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2549 | spin_unlock(&cache->lock); |
| 2550 | spin_unlock(&cache->space_info->lock); |
| 2551 | |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 2552 | __btrfs_mod_total_bytes_pinned(cache->space_info, num_bytes); |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 2553 | set_extent_dirty(&trans->transaction->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2554 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2555 | return 0; |
| 2556 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2557 | |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 2558 | int btrfs_pin_extent(struct btrfs_trans_handle *trans, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2559 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2560 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2561 | struct btrfs_block_group *cache; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2562 | |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 2563 | cache = btrfs_lookup_block_group(trans->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2564 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2565 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 2566 | pin_down_extent(trans, cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2567 | |
| 2568 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2569 | return 0; |
| 2570 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2571 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2572 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2573 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2574 | */ |
Nikolay Borisov | 9fce570 | 2020-01-20 16:09:13 +0200 | [diff] [blame] | 2575 | int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2576 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2577 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2578 | struct btrfs_block_group *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2579 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2580 | |
Nikolay Borisov | f2fb729 | 2020-01-20 16:09:16 +0200 | [diff] [blame] | 2581 | btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes); |
| 2582 | |
Nikolay Borisov | 9fce570 | 2020-01-20 16:09:13 +0200 | [diff] [blame] | 2583 | cache = btrfs_lookup_block_group(trans->fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2584 | if (!cache) |
| 2585 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2586 | |
| 2587 | /* |
| 2588 | * pull in the free space cache (if any) so that our pin |
| 2589 | * removes the free space from the cache. We have load_only set |
| 2590 | * to one because the slow code to read in the free extents does check |
| 2591 | * the pinned extents. |
| 2592 | */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2593 | btrfs_cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2594 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 2595 | pin_down_extent(trans, cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2596 | |
| 2597 | /* remove us from the free space cache (if we're there at all) */ |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2598 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2599 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2600 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2601 | } |
| 2602 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2603 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 2604 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2605 | { |
| 2606 | int ret; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2607 | struct btrfs_block_group *block_group; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2608 | struct btrfs_caching_control *caching_ctl; |
| 2609 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2610 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2611 | if (!block_group) |
| 2612 | return -EINVAL; |
| 2613 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2614 | btrfs_cache_block_group(block_group, 0); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2615 | caching_ctl = btrfs_get_caching_control(block_group); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2616 | |
| 2617 | if (!caching_ctl) { |
| 2618 | /* Logic error */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2619 | BUG_ON(!btrfs_block_group_done(block_group)); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2620 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 2621 | } else { |
| 2622 | mutex_lock(&caching_ctl->mutex); |
| 2623 | |
| 2624 | if (start >= caching_ctl->progress) { |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2625 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2626 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2627 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 2628 | ret = btrfs_remove_free_space(block_group, |
| 2629 | start, num_bytes); |
| 2630 | } else { |
| 2631 | num_bytes = caching_ctl->progress - start; |
| 2632 | ret = btrfs_remove_free_space(block_group, |
| 2633 | start, num_bytes); |
| 2634 | if (ret) |
| 2635 | goto out_lock; |
| 2636 | |
| 2637 | num_bytes = (start + num_bytes) - |
| 2638 | caching_ctl->progress; |
| 2639 | start = caching_ctl->progress; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2640 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2641 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2642 | } |
| 2643 | out_lock: |
| 2644 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2645 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2646 | } |
| 2647 | btrfs_put_block_group(block_group); |
| 2648 | return ret; |
| 2649 | } |
| 2650 | |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2651 | int btrfs_exclude_logged_extents(struct extent_buffer *eb) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2652 | { |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2653 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2654 | struct btrfs_file_extent_item *item; |
| 2655 | struct btrfs_key key; |
| 2656 | int found_type; |
| 2657 | int i; |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2658 | int ret = 0; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2659 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2660 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2661 | return 0; |
| 2662 | |
| 2663 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 2664 | btrfs_item_key_to_cpu(eb, &key, i); |
| 2665 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2666 | continue; |
| 2667 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 2668 | found_type = btrfs_file_extent_type(eb, item); |
| 2669 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 2670 | continue; |
| 2671 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 2672 | continue; |
| 2673 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 2674 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2675 | ret = __exclude_logged_extent(fs_info, key.objectid, key.offset); |
| 2676 | if (ret) |
| 2677 | break; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2678 | } |
| 2679 | |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2680 | return ret; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2681 | } |
| 2682 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 2683 | static void |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2684 | btrfs_inc_block_group_reservations(struct btrfs_block_group *bg) |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 2685 | { |
| 2686 | atomic_inc(&bg->reservations); |
| 2687 | } |
| 2688 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2689 | /* |
| 2690 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 2691 | * what it should be based on the mount options. |
| 2692 | */ |
| 2693 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2694 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 2695 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2696 | { |
| 2697 | struct btrfs_free_cluster *ret = NULL; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2698 | |
| 2699 | *empty_cluster = 0; |
| 2700 | if (btrfs_mixed_space_info(space_info)) |
| 2701 | return ret; |
| 2702 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2703 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2704 | ret = &fs_info->meta_alloc_cluster; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2705 | if (btrfs_test_opt(fs_info, SSD)) |
| 2706 | *empty_cluster = SZ_2M; |
| 2707 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2708 | *empty_cluster = SZ_64K; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2709 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && |
| 2710 | btrfs_test_opt(fs_info, SSD_SPREAD)) { |
| 2711 | *empty_cluster = SZ_2M; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2712 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | return ret; |
| 2716 | } |
| 2717 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2718 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 2719 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 2720 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2721 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2722 | struct btrfs_block_group *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2723 | struct btrfs_space_info *space_info; |
| 2724 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2725 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2726 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2727 | u64 total_unpinned = 0; |
| 2728 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2729 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2730 | |
| 2731 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2732 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2733 | if (!cache || |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2734 | start >= cache->start + cache->length) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2735 | if (cache) |
| 2736 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2737 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2738 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2739 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2740 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2741 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2742 | cache->space_info, |
| 2743 | &empty_cluster); |
| 2744 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2745 | } |
| 2746 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2747 | len = cache->start + cache->length - start; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2748 | len = min(len, end + 1 - start); |
| 2749 | |
Josef Bacik | 56d1654 | 2020-10-23 09:58:04 -0400 | [diff] [blame] | 2750 | if (start < cache->last_byte_to_unpin && return_free_space) { |
| 2751 | u64 add_len = min(len, cache->last_byte_to_unpin - start); |
| 2752 | |
| 2753 | btrfs_add_free_space(cache, start, add_len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2754 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2755 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2756 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2757 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2758 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2759 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2760 | /* |
| 2761 | * If this space cluster has been marked as fragmented and we've |
| 2762 | * unpinned enough in this block group to potentially allow a |
| 2763 | * cluster to be created inside of it go ahead and clear the |
| 2764 | * fragmented check. |
| 2765 | */ |
| 2766 | if (cluster && cluster->fragmented && |
| 2767 | total_unpinned > empty_cluster) { |
| 2768 | spin_lock(&cluster->lock); |
| 2769 | cluster->fragmented = 0; |
| 2770 | spin_unlock(&cluster->lock); |
| 2771 | } |
| 2772 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2773 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2774 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2775 | cache->pinned -= len; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2776 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 2777 | space_info->max_extent_size = 0; |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 2778 | __btrfs_mod_total_bytes_pinned(space_info, -len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2779 | if (cache->ro) { |
| 2780 | space_info->bytes_readonly += len; |
| 2781 | readonly = true; |
| 2782 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2783 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2784 | if (!readonly && return_free_space && |
| 2785 | global_rsv->space_info == space_info) { |
| 2786 | u64 to_add = len; |
Nikolay Borisov | 92ac58e | 2017-08-17 10:52:28 +0300 | [diff] [blame] | 2787 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2788 | spin_lock(&global_rsv->lock); |
| 2789 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2790 | to_add = min(len, global_rsv->size - |
| 2791 | global_rsv->reserved); |
| 2792 | global_rsv->reserved += to_add; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2793 | btrfs_space_info_update_bytes_may_use(fs_info, |
| 2794 | space_info, to_add); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2795 | if (global_rsv->reserved >= global_rsv->size) |
| 2796 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2797 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2798 | } |
| 2799 | spin_unlock(&global_rsv->lock); |
| 2800 | } |
Josef Bacik | 2732798 | 2020-07-21 10:22:18 -0400 | [diff] [blame] | 2801 | /* Add to any tickets we may have */ |
| 2802 | if (!readonly && return_free_space && len) |
| 2803 | btrfs_try_granting_tickets(fs_info, space_info); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2804 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2808 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2809 | return 0; |
| 2810 | } |
| 2811 | |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2812 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2813 | { |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2814 | struct btrfs_fs_info *fs_info = trans->fs_info; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2815 | struct btrfs_block_group *block_group, *tmp; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2816 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2817 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2818 | u64 start; |
| 2819 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2820 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2821 | |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 2822 | unpin = &trans->transaction->pinned_extents; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2823 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 2824 | while (!TRANS_ABORTED(trans)) { |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2825 | struct extent_state *cached_state = NULL; |
| 2826 | |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2827 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2828 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2829 | EXTENT_DIRTY, &cached_state); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2830 | if (ret) { |
| 2831 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2832 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2833 | } |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 2834 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
| 2835 | clear_extent_bits(&fs_info->excluded_extents, start, |
Nikolay Borisov | f2fb729 | 2020-01-20 16:09:16 +0200 | [diff] [blame] | 2836 | end, EXTENT_UPTODATE); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2837 | |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 2838 | if (btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2839 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2840 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2841 | |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2842 | clear_extent_dirty(unpin, start, end, &cached_state); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2843 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2844 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2845 | free_extent_state(cached_state); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2846 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2847 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2848 | |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 2849 | if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) { |
| 2850 | btrfs_discard_calc_delay(&fs_info->discard_ctl); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2851 | btrfs_discard_schedule_work(&fs_info->discard_ctl, true); |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 2852 | } |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2853 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2854 | /* |
| 2855 | * Transaction is finished. We don't need the lock anymore. We |
| 2856 | * do need to clean up the block groups in case of a transaction |
| 2857 | * abort. |
| 2858 | */ |
| 2859 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 2860 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 2861 | u64 trimmed = 0; |
| 2862 | |
| 2863 | ret = -EROFS; |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 2864 | if (!TRANS_ABORTED(trans)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2865 | ret = btrfs_discard_extent(fs_info, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2866 | block_group->start, |
| 2867 | block_group->length, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2868 | &trimmed); |
| 2869 | |
| 2870 | list_del_init(&block_group->bg_list); |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 2871 | btrfs_unfreeze_block_group(block_group); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2872 | btrfs_put_block_group(block_group); |
| 2873 | |
| 2874 | if (ret) { |
| 2875 | const char *errstr = btrfs_decode_error(ret); |
| 2876 | btrfs_warn(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 2877 | "discard failed while removing blockgroup: errno=%d %s", |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2878 | ret, errstr); |
| 2879 | } |
| 2880 | } |
| 2881 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2882 | return 0; |
| 2883 | } |
| 2884 | |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 2885 | /* |
| 2886 | * Drop one or more refs of @node. |
| 2887 | * |
| 2888 | * 1. Locate the extent refs. |
| 2889 | * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item. |
| 2890 | * Locate it, then reduce the refs number or remove the ref line completely. |
| 2891 | * |
| 2892 | * 2. Update the refs count in EXTENT/METADATA_ITEM |
| 2893 | * |
| 2894 | * Inline backref case: |
| 2895 | * |
| 2896 | * in extent tree we have: |
| 2897 | * |
| 2898 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82 |
| 2899 | * refs 2 gen 6 flags DATA |
| 2900 | * extent data backref root FS_TREE objectid 258 offset 0 count 1 |
| 2901 | * extent data backref root FS_TREE objectid 257 offset 0 count 1 |
| 2902 | * |
| 2903 | * This function gets called with: |
| 2904 | * |
| 2905 | * node->bytenr = 13631488 |
| 2906 | * node->num_bytes = 1048576 |
| 2907 | * root_objectid = FS_TREE |
| 2908 | * owner_objectid = 257 |
| 2909 | * owner_offset = 0 |
| 2910 | * refs_to_drop = 1 |
| 2911 | * |
| 2912 | * Then we should get some like: |
| 2913 | * |
| 2914 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82 |
| 2915 | * refs 1 gen 6 flags DATA |
| 2916 | * extent data backref root FS_TREE objectid 258 offset 0 count 1 |
| 2917 | * |
| 2918 | * Keyed backref case: |
| 2919 | * |
| 2920 | * in extent tree we have: |
| 2921 | * |
| 2922 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24 |
| 2923 | * refs 754 gen 6 flags DATA |
| 2924 | * [...] |
| 2925 | * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28 |
| 2926 | * extent data backref root FS_TREE objectid 866 offset 0 count 1 |
| 2927 | * |
| 2928 | * This function get called with: |
| 2929 | * |
| 2930 | * node->bytenr = 13631488 |
| 2931 | * node->num_bytes = 1048576 |
| 2932 | * root_objectid = FS_TREE |
| 2933 | * owner_objectid = 866 |
| 2934 | * owner_offset = 0 |
| 2935 | * refs_to_drop = 1 |
| 2936 | * |
| 2937 | * Then we should get some like: |
| 2938 | * |
| 2939 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24 |
| 2940 | * refs 753 gen 6 flags DATA |
| 2941 | * |
| 2942 | * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed. |
| 2943 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2944 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2945 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 2946 | u64 root_objectid, u64 owner_objectid, |
| 2947 | u64 owner_offset, int refs_to_drop, |
| 2948 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2949 | { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2950 | struct btrfs_fs_info *info = trans->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2951 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2952 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2953 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2954 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2955 | struct btrfs_extent_item *ei; |
| 2956 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2957 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2958 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2959 | int extent_slot = 0; |
| 2960 | int found_extent = 0; |
| 2961 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2962 | u32 item_size; |
| 2963 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2964 | u64 bytenr = node->bytenr; |
| 2965 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2966 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2967 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2968 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2969 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2970 | if (!path) |
| 2971 | return -ENOMEM; |
| 2972 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2973 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2974 | |
| 2975 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 2976 | |
| 2977 | if (!is_data && refs_to_drop != 1) { |
| 2978 | btrfs_crit(info, |
| 2979 | "invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u", |
| 2980 | node->bytenr, refs_to_drop); |
| 2981 | ret = -EINVAL; |
| 2982 | btrfs_abort_transaction(trans, ret); |
| 2983 | goto out; |
| 2984 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2985 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2986 | if (is_data) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 2987 | skinny_metadata = false; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2988 | |
Nikolay Borisov | fbe4801 | 2018-06-20 15:48:52 +0300 | [diff] [blame] | 2989 | ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes, |
| 2990 | parent, root_objectid, owner_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2991 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2992 | if (ret == 0) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 2993 | /* |
| 2994 | * Either the inline backref or the SHARED_DATA_REF/ |
| 2995 | * SHARED_BLOCK_REF is found |
| 2996 | * |
| 2997 | * Here is a quick path to locate EXTENT/METADATA_ITEM. |
| 2998 | * It's possible the EXTENT/METADATA_ITEM is near current slot. |
| 2999 | */ |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3000 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3001 | while (extent_slot >= 0) { |
| 3002 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3003 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3004 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3005 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3006 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3007 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3008 | found_extent = 1; |
| 3009 | break; |
| 3010 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3011 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 3012 | key.offset == owner_objectid) { |
| 3013 | found_extent = 1; |
| 3014 | break; |
| 3015 | } |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3016 | |
| 3017 | /* Quick path didn't find the EXTEMT/METADATA_ITEM */ |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3018 | if (path->slots[0] - extent_slot > 5) |
| 3019 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3020 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3021 | } |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 3022 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3023 | if (!found_extent) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3024 | if (iref) { |
| 3025 | btrfs_crit(info, |
| 3026 | "invalid iref, no EXTENT/METADATA_ITEM found but has inline extent ref"); |
| 3027 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3028 | goto err_dump; |
| 3029 | } |
| 3030 | /* Must be SHARED_* item, remove the backref first */ |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3031 | ret = remove_extent_backref(trans, path, NULL, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 3032 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3033 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3034 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3035 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3036 | goto out; |
| 3037 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3038 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3039 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3040 | |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3041 | /* Slow path to locate EXTENT/METADATA_ITEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3042 | key.objectid = bytenr; |
| 3043 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3044 | key.offset = num_bytes; |
| 3045 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3046 | if (!is_data && skinny_metadata) { |
| 3047 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3048 | key.offset = owner_objectid; |
| 3049 | } |
| 3050 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3051 | ret = btrfs_search_slot(trans, extent_root, |
| 3052 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3053 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 3054 | /* |
| 3055 | * Couldn't find our skinny metadata item, |
| 3056 | * see if we have ye olde extent item. |
| 3057 | */ |
| 3058 | path->slots[0]--; |
| 3059 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3060 | path->slots[0]); |
| 3061 | if (key.objectid == bytenr && |
| 3062 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3063 | key.offset == num_bytes) |
| 3064 | ret = 0; |
| 3065 | } |
| 3066 | |
| 3067 | if (ret > 0 && skinny_metadata) { |
| 3068 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 3069 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3070 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3071 | key.offset = num_bytes; |
| 3072 | btrfs_release_path(path); |
| 3073 | ret = btrfs_search_slot(trans, extent_root, |
| 3074 | &key, path, -1, 1); |
| 3075 | } |
| 3076 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3077 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3078 | btrfs_err(info, |
| 3079 | "umm, got %d back from search, was looking for %llu", |
| 3080 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 3081 | if (ret > 0) |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3082 | btrfs_print_leaf(path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3083 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3084 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3085 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3086 | goto out; |
| 3087 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3088 | extent_slot = path->slots[0]; |
| 3089 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3090 | } else if (WARN_ON(ret == -ENOENT)) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3091 | btrfs_print_leaf(path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3092 | btrfs_err(info, |
| 3093 | "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3094 | bytenr, parent, root_objectid, owner_objectid, |
| 3095 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3096 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 3097 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3098 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3099 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3100 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3101 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3102 | |
| 3103 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3104 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 3105 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 3106 | ret = -EINVAL; |
| 3107 | btrfs_print_v0_err(info); |
| 3108 | btrfs_abort_transaction(trans, ret); |
| 3109 | goto out; |
| 3110 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3111 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3112 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3113 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3114 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3115 | struct btrfs_tree_block_info *bi; |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3116 | if (item_size < sizeof(*ei) + sizeof(*bi)) { |
| 3117 | btrfs_crit(info, |
Pujin Shi | cad69d1 | 2020-10-14 11:24:19 +0800 | [diff] [blame] | 3118 | "invalid extent item size for key (%llu, %u, %llu) owner %llu, has %u expect >= %zu", |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3119 | key.objectid, key.type, key.offset, |
| 3120 | owner_objectid, item_size, |
| 3121 | sizeof(*ei) + sizeof(*bi)); |
| 3122 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3123 | goto err_dump; |
| 3124 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3125 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3126 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3127 | } |
| 3128 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3129 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3130 | if (refs < refs_to_drop) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3131 | btrfs_crit(info, |
| 3132 | "trying to drop %d refs but we only have %llu for bytenr %llu", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3133 | refs_to_drop, refs, bytenr); |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3134 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3135 | goto err_dump; |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3136 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3137 | refs -= refs_to_drop; |
| 3138 | |
| 3139 | if (refs > 0) { |
| 3140 | if (extent_op) |
| 3141 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3142 | /* |
| 3143 | * In the case of inline back ref, reference count will |
| 3144 | * be updated by remove_extent_backref |
| 3145 | */ |
| 3146 | if (iref) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3147 | if (!found_extent) { |
| 3148 | btrfs_crit(info, |
| 3149 | "invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found"); |
| 3150 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3151 | goto err_dump; |
| 3152 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3153 | } else { |
| 3154 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3155 | btrfs_mark_buffer_dirty(leaf); |
| 3156 | } |
| 3157 | if (found_extent) { |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3158 | ret = remove_extent_backref(trans, path, iref, |
| 3159 | refs_to_drop, is_data, |
| 3160 | &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3161 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3162 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3163 | goto out; |
| 3164 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3165 | } |
| 3166 | } else { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3167 | /* In this branch refs == 1 */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3168 | if (found_extent) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3169 | if (is_data && refs_to_drop != |
| 3170 | extent_data_ref_count(path, iref)) { |
| 3171 | btrfs_crit(info, |
| 3172 | "invalid refs_to_drop, current refs %u refs_to_drop %u", |
| 3173 | extent_data_ref_count(path, iref), |
| 3174 | refs_to_drop); |
| 3175 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3176 | goto err_dump; |
| 3177 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3178 | if (iref) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3179 | if (path->slots[0] != extent_slot) { |
| 3180 | btrfs_crit(info, |
| 3181 | "invalid iref, extent item key (%llu %u %llu) doesn't have wanted iref", |
| 3182 | key.objectid, key.type, |
| 3183 | key.offset); |
| 3184 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3185 | goto err_dump; |
| 3186 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3187 | } else { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3188 | /* |
| 3189 | * No inline ref, we must be at SHARED_* item, |
| 3190 | * And it's single ref, it must be: |
| 3191 | * | extent_slot ||extent_slot + 1| |
| 3192 | * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ] |
| 3193 | */ |
| 3194 | if (path->slots[0] != extent_slot + 1) { |
| 3195 | btrfs_crit(info, |
| 3196 | "invalid SHARED_* item, previous item is not EXTENT/METADATA_ITEM"); |
| 3197 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3198 | goto err_dump; |
| 3199 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3200 | path->slots[0] = extent_slot; |
| 3201 | num_to_del = 2; |
| 3202 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3203 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3204 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3205 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3206 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3207 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3208 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3209 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3210 | goto out; |
| 3211 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3212 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3213 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3214 | if (is_data) { |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 3215 | ret = btrfs_del_csums(trans, info->csum_root, bytenr, |
| 3216 | num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3217 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3218 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3219 | goto out; |
| 3220 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3221 | } |
| 3222 | |
Nikolay Borisov | e7355e5 | 2018-05-10 15:44:55 +0300 | [diff] [blame] | 3223 | ret = add_to_free_space_tree(trans, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3224 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3225 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3226 | goto out; |
| 3227 | } |
| 3228 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 3229 | ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3230 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3231 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3232 | goto out; |
| 3233 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3234 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3235 | btrfs_release_path(path); |
| 3236 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3237 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3238 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3239 | return ret; |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3240 | err_dump: |
| 3241 | /* |
| 3242 | * Leaf dump can take up a lot of log buffer, so we only do full leaf |
| 3243 | * dump for debug build. |
| 3244 | */ |
| 3245 | if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) { |
| 3246 | btrfs_crit(info, "path->slots[0]=%d extent_slot=%d", |
| 3247 | path->slots[0], extent_slot); |
| 3248 | btrfs_print_leaf(path->nodes[0]); |
| 3249 | } |
| 3250 | |
| 3251 | btrfs_free_path(path); |
| 3252 | return -EUCLEAN; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3256 | * when we free an block, it is possible (and likely) that we free the last |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3257 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3258 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3259 | * removes it from the tree. |
| 3260 | */ |
| 3261 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3262 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3263 | { |
| 3264 | struct btrfs_delayed_ref_head *head; |
| 3265 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3266 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3267 | |
| 3268 | delayed_refs = &trans->transaction->delayed_refs; |
| 3269 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 3270 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3271 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3272 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3273 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3274 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 3275 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3276 | goto out; |
| 3277 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 3278 | if (cleanup_extent_op(head) != NULL) |
| 3279 | goto out; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3280 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3281 | /* |
| 3282 | * waiting for the lock here would deadlock. If someone else has it |
| 3283 | * locked they are already in the process of dropping it anyway |
| 3284 | */ |
| 3285 | if (!mutex_trylock(&head->mutex)) |
| 3286 | goto out; |
| 3287 | |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3288 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3289 | head->processing = 0; |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3290 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3291 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3292 | spin_unlock(&delayed_refs->lock); |
| 3293 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3294 | BUG_ON(head->extent_op); |
| 3295 | if (head->must_insert_reserved) |
| 3296 | ret = 1; |
| 3297 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 3298 | btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3299 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 3300 | btrfs_put_delayed_ref_head(head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3301 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3302 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3303 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3304 | |
| 3305 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3306 | spin_unlock(&delayed_refs->lock); |
| 3307 | return 0; |
| 3308 | } |
| 3309 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3310 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 3311 | struct btrfs_root *root, |
| 3312 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3313 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3314 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3315 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3316 | struct btrfs_ref generic_ref = { 0 }; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3317 | int ret; |
| 3318 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3319 | btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, |
| 3320 | buf->start, buf->len, parent); |
| 3321 | btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), |
| 3322 | root->root_key.objectid); |
| 3323 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3324 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3325 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 3326 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3327 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3328 | } |
| 3329 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3330 | if (last_ref && btrfs_header_generation(buf) == trans->transid) { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3331 | struct btrfs_block_group *cache; |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3332 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3333 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3334 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3335 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3336 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3337 | } |
| 3338 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3339 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3340 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3341 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 3342 | pin_down_extent(trans, cache, buf->start, buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3343 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3344 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 3348 | |
| 3349 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 3350 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3351 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 3352 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3353 | } |
| 3354 | out: |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3355 | if (last_ref) { |
| 3356 | /* |
| 3357 | * Deleting the buffer, clear the corrupt flag since it doesn't |
| 3358 | * matter anymore. |
| 3359 | */ |
| 3360 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
| 3361 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3362 | } |
| 3363 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3364 | /* Can return -ENOMEM */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3365 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3366 | { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3367 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3368 | int ret; |
| 3369 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 3370 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3371 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3372 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3373 | /* |
| 3374 | * tree log blocks never actually go into the extent allocation |
| 3375 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3376 | */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3377 | if ((ref->type == BTRFS_REF_METADATA && |
| 3378 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3379 | (ref->type == BTRFS_REF_DATA && |
| 3380 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3381 | /* unlocks the pinned mutex */ |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 3382 | btrfs_pin_extent(trans, ref->bytenr, ref->len, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3383 | ret = 0; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3384 | } else if (ref->type == BTRFS_REF_METADATA) { |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 3385 | ret = btrfs_add_delayed_tree_ref(trans, ref, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3386 | } else { |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 3387 | ret = btrfs_add_delayed_data_ref(trans, ref, 0); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3388 | } |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3389 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3390 | if (!((ref->type == BTRFS_REF_METADATA && |
| 3391 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3392 | (ref->type == BTRFS_REF_DATA && |
| 3393 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID))) |
| 3394 | btrfs_ref_tree_mod(fs_info, ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3395 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3396 | return ret; |
| 3397 | } |
| 3398 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3399 | enum btrfs_loop_type { |
David Sterba | f262fa8 | 2019-06-18 20:00:08 +0200 | [diff] [blame] | 3400 | LOOP_CACHING_NOWAIT, |
| 3401 | LOOP_CACHING_WAIT, |
| 3402 | LOOP_ALLOC_CHUNK, |
| 3403 | LOOP_NO_EMPTY_SIZE, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3404 | }; |
| 3405 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3406 | static inline void |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3407 | btrfs_lock_block_group(struct btrfs_block_group *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3408 | int delalloc) |
| 3409 | { |
| 3410 | if (delalloc) |
| 3411 | down_read(&cache->data_rwsem); |
| 3412 | } |
| 3413 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3414 | static inline void btrfs_grab_block_group(struct btrfs_block_group *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3415 | int delalloc) |
| 3416 | { |
| 3417 | btrfs_get_block_group(cache); |
| 3418 | if (delalloc) |
| 3419 | down_read(&cache->data_rwsem); |
| 3420 | } |
| 3421 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3422 | static struct btrfs_block_group *btrfs_lock_cluster( |
| 3423 | struct btrfs_block_group *block_group, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3424 | struct btrfs_free_cluster *cluster, |
| 3425 | int delalloc) |
Jules Irenge | c142c6a | 2020-03-31 21:46:41 +0100 | [diff] [blame] | 3426 | __acquires(&cluster->refill_lock) |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3427 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3428 | struct btrfs_block_group *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3429 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3430 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3431 | while (1) { |
| 3432 | used_bg = cluster->block_group; |
| 3433 | if (!used_bg) |
| 3434 | return NULL; |
| 3435 | |
| 3436 | if (used_bg == block_group) |
| 3437 | return used_bg; |
| 3438 | |
| 3439 | btrfs_get_block_group(used_bg); |
| 3440 | |
| 3441 | if (!delalloc) |
| 3442 | return used_bg; |
| 3443 | |
| 3444 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 3445 | return used_bg; |
| 3446 | |
| 3447 | spin_unlock(&cluster->refill_lock); |
| 3448 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 3449 | /* We should only have one-level nested. */ |
| 3450 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3451 | |
| 3452 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3453 | if (used_bg == cluster->block_group) |
| 3454 | return used_bg; |
| 3455 | |
| 3456 | up_read(&used_bg->data_rwsem); |
| 3457 | btrfs_put_block_group(used_bg); |
| 3458 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | static inline void |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3462 | btrfs_release_block_group(struct btrfs_block_group *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3463 | int delalloc) |
| 3464 | { |
| 3465 | if (delalloc) |
| 3466 | up_read(&cache->data_rwsem); |
| 3467 | btrfs_put_block_group(cache); |
| 3468 | } |
| 3469 | |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 3470 | enum btrfs_extent_allocation_policy { |
| 3471 | BTRFS_EXTENT_ALLOC_CLUSTERED, |
| 3472 | }; |
| 3473 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3474 | /* |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3475 | * Structure used internally for find_free_extent() function. Wraps needed |
| 3476 | * parameters. |
| 3477 | */ |
| 3478 | struct find_free_extent_ctl { |
| 3479 | /* Basic allocation info */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3480 | u64 num_bytes; |
| 3481 | u64 empty_size; |
| 3482 | u64 flags; |
| 3483 | int delalloc; |
| 3484 | |
| 3485 | /* Where to start the search inside the bg */ |
| 3486 | u64 search_start; |
| 3487 | |
| 3488 | /* For clustered allocation */ |
| 3489 | u64 empty_cluster; |
Naohiro Aota | c10859b | 2020-02-25 12:56:18 +0900 | [diff] [blame] | 3490 | struct btrfs_free_cluster *last_ptr; |
| 3491 | bool use_cluster; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3492 | |
| 3493 | bool have_caching_bg; |
| 3494 | bool orig_have_caching_bg; |
| 3495 | |
| 3496 | /* RAID index, converted from flags */ |
| 3497 | int index; |
| 3498 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3499 | /* |
| 3500 | * Current loop number, check find_free_extent_update_loop() for details |
| 3501 | */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3502 | int loop; |
| 3503 | |
| 3504 | /* |
| 3505 | * Whether we're refilling a cluster, if true we need to re-search |
| 3506 | * current block group but don't try to refill the cluster again. |
| 3507 | */ |
| 3508 | bool retry_clustered; |
| 3509 | |
| 3510 | /* |
| 3511 | * Whether we're updating free space cache, if true we need to re-search |
| 3512 | * current block group but don't try updating free space cache again. |
| 3513 | */ |
| 3514 | bool retry_unclustered; |
| 3515 | |
| 3516 | /* If current block group is cached */ |
| 3517 | int cached; |
| 3518 | |
| 3519 | /* Max contiguous hole found */ |
| 3520 | u64 max_extent_size; |
| 3521 | |
| 3522 | /* Total free space from free space cache, not always contiguous */ |
| 3523 | u64 total_free_space; |
| 3524 | |
| 3525 | /* Found result */ |
| 3526 | u64 found_offset; |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 3527 | |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 3528 | /* Hint where to start looking for an empty space */ |
| 3529 | u64 hint_byte; |
| 3530 | |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 3531 | /* Allocation policy */ |
| 3532 | enum btrfs_extent_allocation_policy policy; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3533 | }; |
| 3534 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3535 | |
| 3536 | /* |
| 3537 | * Helper function for find_free_extent(). |
| 3538 | * |
| 3539 | * Return -ENOENT to inform caller that we need fallback to unclustered mode. |
| 3540 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3541 | * Return >0 to inform caller that we find nothing |
| 3542 | * Return 0 means we have found a location and set ffe_ctl->found_offset. |
| 3543 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3544 | static int find_free_extent_clustered(struct btrfs_block_group *bg, |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3545 | struct find_free_extent_ctl *ffe_ctl, |
| 3546 | struct btrfs_block_group **cluster_bg_ret) |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3547 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3548 | struct btrfs_block_group *cluster_bg; |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3549 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3550 | u64 aligned_cluster; |
| 3551 | u64 offset; |
| 3552 | int ret; |
| 3553 | |
| 3554 | cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc); |
| 3555 | if (!cluster_bg) |
| 3556 | goto refill_cluster; |
| 3557 | if (cluster_bg != bg && (cluster_bg->ro || |
| 3558 | !block_group_bits(cluster_bg, ffe_ctl->flags))) |
| 3559 | goto release_cluster; |
| 3560 | |
| 3561 | offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3562 | ffe_ctl->num_bytes, cluster_bg->start, |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3563 | &ffe_ctl->max_extent_size); |
| 3564 | if (offset) { |
| 3565 | /* We have a block, we're done */ |
| 3566 | spin_unlock(&last_ptr->refill_lock); |
| 3567 | trace_btrfs_reserve_extent_cluster(cluster_bg, |
| 3568 | ffe_ctl->search_start, ffe_ctl->num_bytes); |
| 3569 | *cluster_bg_ret = cluster_bg; |
| 3570 | ffe_ctl->found_offset = offset; |
| 3571 | return 0; |
| 3572 | } |
| 3573 | WARN_ON(last_ptr->block_group != cluster_bg); |
| 3574 | |
| 3575 | release_cluster: |
| 3576 | /* |
| 3577 | * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so |
| 3578 | * lets just skip it and let the allocator find whatever block it can |
| 3579 | * find. If we reach this point, we will have tried the cluster |
| 3580 | * allocator plenty of times and not have found anything, so we are |
| 3581 | * likely way too fragmented for the clustering stuff to find anything. |
| 3582 | * |
| 3583 | * However, if the cluster is taken from the current block group, |
| 3584 | * release the cluster first, so that we stand a better chance of |
| 3585 | * succeeding in the unclustered allocation. |
| 3586 | */ |
| 3587 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) { |
| 3588 | spin_unlock(&last_ptr->refill_lock); |
| 3589 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3590 | return -ENOENT; |
| 3591 | } |
| 3592 | |
| 3593 | /* This cluster didn't work out, free it and start over */ |
| 3594 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3595 | |
| 3596 | if (cluster_bg != bg) |
| 3597 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3598 | |
| 3599 | refill_cluster: |
| 3600 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) { |
| 3601 | spin_unlock(&last_ptr->refill_lock); |
| 3602 | return -ENOENT; |
| 3603 | } |
| 3604 | |
| 3605 | aligned_cluster = max_t(u64, |
| 3606 | ffe_ctl->empty_cluster + ffe_ctl->empty_size, |
| 3607 | bg->full_stripe_len); |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3608 | ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start, |
| 3609 | ffe_ctl->num_bytes, aligned_cluster); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3610 | if (ret == 0) { |
| 3611 | /* Now pull our allocation out of this cluster */ |
| 3612 | offset = btrfs_alloc_from_cluster(bg, last_ptr, |
| 3613 | ffe_ctl->num_bytes, ffe_ctl->search_start, |
| 3614 | &ffe_ctl->max_extent_size); |
| 3615 | if (offset) { |
| 3616 | /* We found one, proceed */ |
| 3617 | spin_unlock(&last_ptr->refill_lock); |
| 3618 | trace_btrfs_reserve_extent_cluster(bg, |
| 3619 | ffe_ctl->search_start, |
| 3620 | ffe_ctl->num_bytes); |
| 3621 | ffe_ctl->found_offset = offset; |
| 3622 | return 0; |
| 3623 | } |
| 3624 | } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT && |
| 3625 | !ffe_ctl->retry_clustered) { |
| 3626 | spin_unlock(&last_ptr->refill_lock); |
| 3627 | |
| 3628 | ffe_ctl->retry_clustered = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3629 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3630 | ffe_ctl->empty_cluster + ffe_ctl->empty_size); |
| 3631 | return -EAGAIN; |
| 3632 | } |
| 3633 | /* |
| 3634 | * At this point we either didn't find a cluster or we weren't able to |
| 3635 | * allocate a block from our cluster. Free the cluster we've been |
| 3636 | * trying to use, and go to the next block group. |
| 3637 | */ |
| 3638 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3639 | spin_unlock(&last_ptr->refill_lock); |
| 3640 | return 1; |
| 3641 | } |
| 3642 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3643 | /* |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3644 | * Return >0 to inform caller that we find nothing |
| 3645 | * Return 0 when we found an free extent and set ffe_ctrl->found_offset |
| 3646 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3647 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3648 | static int find_free_extent_unclustered(struct btrfs_block_group *bg, |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3649 | struct find_free_extent_ctl *ffe_ctl) |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3650 | { |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3651 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3652 | u64 offset; |
| 3653 | |
| 3654 | /* |
| 3655 | * We are doing an unclustered allocation, set the fragmented flag so |
| 3656 | * we don't bother trying to setup a cluster again until we get more |
| 3657 | * space. |
| 3658 | */ |
| 3659 | if (unlikely(last_ptr)) { |
| 3660 | spin_lock(&last_ptr->lock); |
| 3661 | last_ptr->fragmented = 1; |
| 3662 | spin_unlock(&last_ptr->lock); |
| 3663 | } |
| 3664 | if (ffe_ctl->cached) { |
| 3665 | struct btrfs_free_space_ctl *free_space_ctl; |
| 3666 | |
| 3667 | free_space_ctl = bg->free_space_ctl; |
| 3668 | spin_lock(&free_space_ctl->tree_lock); |
| 3669 | if (free_space_ctl->free_space < |
| 3670 | ffe_ctl->num_bytes + ffe_ctl->empty_cluster + |
| 3671 | ffe_ctl->empty_size) { |
| 3672 | ffe_ctl->total_free_space = max_t(u64, |
| 3673 | ffe_ctl->total_free_space, |
| 3674 | free_space_ctl->free_space); |
| 3675 | spin_unlock(&free_space_ctl->tree_lock); |
| 3676 | return 1; |
| 3677 | } |
| 3678 | spin_unlock(&free_space_ctl->tree_lock); |
| 3679 | } |
| 3680 | |
| 3681 | offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start, |
| 3682 | ffe_ctl->num_bytes, ffe_ctl->empty_size, |
| 3683 | &ffe_ctl->max_extent_size); |
| 3684 | |
| 3685 | /* |
| 3686 | * If we didn't find a chunk, and we haven't failed on this block group |
| 3687 | * before, and this block group is in the middle of caching and we are |
| 3688 | * ok with waiting, then go ahead and wait for progress to be made, and |
| 3689 | * set @retry_unclustered to true. |
| 3690 | * |
| 3691 | * If @retry_unclustered is true then we've already waited on this |
| 3692 | * block group once and should move on to the next block group. |
| 3693 | */ |
| 3694 | if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached && |
| 3695 | ffe_ctl->loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3696 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
| 3697 | ffe_ctl->empty_size); |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3698 | ffe_ctl->retry_unclustered = true; |
| 3699 | return -EAGAIN; |
| 3700 | } else if (!offset) { |
| 3701 | return 1; |
| 3702 | } |
| 3703 | ffe_ctl->found_offset = offset; |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 3707 | static int do_allocation_clustered(struct btrfs_block_group *block_group, |
| 3708 | struct find_free_extent_ctl *ffe_ctl, |
| 3709 | struct btrfs_block_group **bg_ret) |
| 3710 | { |
| 3711 | int ret; |
| 3712 | |
| 3713 | /* We want to try and use the cluster allocator, so lets look there */ |
| 3714 | if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) { |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3715 | ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret); |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 3716 | if (ret >= 0 || ret == -EAGAIN) |
| 3717 | return ret; |
| 3718 | /* ret == -ENOENT case falls through */ |
| 3719 | } |
| 3720 | |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3721 | return find_free_extent_unclustered(block_group, ffe_ctl); |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | static int do_allocation(struct btrfs_block_group *block_group, |
| 3725 | struct find_free_extent_ctl *ffe_ctl, |
| 3726 | struct btrfs_block_group **bg_ret) |
| 3727 | { |
| 3728 | switch (ffe_ctl->policy) { |
| 3729 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3730 | return do_allocation_clustered(block_group, ffe_ctl, bg_ret); |
| 3731 | default: |
| 3732 | BUG(); |
| 3733 | } |
| 3734 | } |
| 3735 | |
Naohiro Aota | baba506 | 2020-02-25 12:56:21 +0900 | [diff] [blame] | 3736 | static void release_block_group(struct btrfs_block_group *block_group, |
| 3737 | struct find_free_extent_ctl *ffe_ctl, |
| 3738 | int delalloc) |
| 3739 | { |
| 3740 | switch (ffe_ctl->policy) { |
| 3741 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3742 | ffe_ctl->retry_clustered = false; |
| 3743 | ffe_ctl->retry_unclustered = false; |
| 3744 | break; |
| 3745 | default: |
| 3746 | BUG(); |
| 3747 | } |
| 3748 | |
| 3749 | BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) != |
| 3750 | ffe_ctl->index); |
| 3751 | btrfs_release_block_group(block_group, delalloc); |
| 3752 | } |
| 3753 | |
Naohiro Aota | 0ab9724 | 2020-02-25 12:56:22 +0900 | [diff] [blame] | 3754 | static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl, |
| 3755 | struct btrfs_key *ins) |
| 3756 | { |
| 3757 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
| 3758 | |
| 3759 | if (!ffe_ctl->use_cluster && last_ptr) { |
| 3760 | spin_lock(&last_ptr->lock); |
| 3761 | last_ptr->window_start = ins->objectid; |
| 3762 | spin_unlock(&last_ptr->lock); |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | static void found_extent(struct find_free_extent_ctl *ffe_ctl, |
| 3767 | struct btrfs_key *ins) |
| 3768 | { |
| 3769 | switch (ffe_ctl->policy) { |
| 3770 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3771 | found_extent_clustered(ffe_ctl, ins); |
| 3772 | break; |
| 3773 | default: |
| 3774 | BUG(); |
| 3775 | } |
| 3776 | } |
| 3777 | |
Naohiro Aota | c70e213 | 2020-02-25 12:56:24 +0900 | [diff] [blame] | 3778 | static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl) |
| 3779 | { |
| 3780 | switch (ffe_ctl->policy) { |
| 3781 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3782 | /* |
| 3783 | * If we can't allocate a new chunk we've already looped through |
| 3784 | * at least once, move on to the NO_EMPTY_SIZE case. |
| 3785 | */ |
| 3786 | ffe_ctl->loop = LOOP_NO_EMPTY_SIZE; |
| 3787 | return 0; |
| 3788 | default: |
| 3789 | BUG(); |
| 3790 | } |
| 3791 | } |
| 3792 | |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3793 | /* |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3794 | * Return >0 means caller needs to re-search for free extent |
| 3795 | * Return 0 means we have the needed free extent. |
| 3796 | * Return <0 means we failed to locate any free extent. |
| 3797 | */ |
| 3798 | static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info, |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3799 | struct btrfs_key *ins, |
| 3800 | struct find_free_extent_ctl *ffe_ctl, |
Naohiro Aota | 15b7ee6 | 2020-02-25 12:56:23 +0900 | [diff] [blame] | 3801 | bool full_search) |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3802 | { |
| 3803 | struct btrfs_root *root = fs_info->extent_root; |
| 3804 | int ret; |
| 3805 | |
| 3806 | if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) && |
| 3807 | ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg) |
| 3808 | ffe_ctl->orig_have_caching_bg = true; |
| 3809 | |
| 3810 | if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT && |
| 3811 | ffe_ctl->have_caching_bg) |
| 3812 | return 1; |
| 3813 | |
| 3814 | if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES) |
| 3815 | return 1; |
| 3816 | |
| 3817 | if (ins->objectid) { |
Naohiro Aota | 0ab9724 | 2020-02-25 12:56:22 +0900 | [diff] [blame] | 3818 | found_extent(ffe_ctl, ins); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3819 | return 0; |
| 3820 | } |
| 3821 | |
| 3822 | /* |
| 3823 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 3824 | * caching kthreads as we move along |
| 3825 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 3826 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 3827 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 3828 | * again |
| 3829 | */ |
| 3830 | if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) { |
| 3831 | ffe_ctl->index = 0; |
| 3832 | if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) { |
| 3833 | /* |
| 3834 | * We want to skip the LOOP_CACHING_WAIT step if we |
| 3835 | * don't have any uncached bgs and we've already done a |
| 3836 | * full search through. |
| 3837 | */ |
| 3838 | if (ffe_ctl->orig_have_caching_bg || !full_search) |
| 3839 | ffe_ctl->loop = LOOP_CACHING_WAIT; |
| 3840 | else |
| 3841 | ffe_ctl->loop = LOOP_ALLOC_CHUNK; |
| 3842 | } else { |
| 3843 | ffe_ctl->loop++; |
| 3844 | } |
| 3845 | |
| 3846 | if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) { |
| 3847 | struct btrfs_trans_handle *trans; |
| 3848 | int exist = 0; |
| 3849 | |
| 3850 | trans = current->journal_info; |
| 3851 | if (trans) |
| 3852 | exist = 1; |
| 3853 | else |
| 3854 | trans = btrfs_join_transaction(root); |
| 3855 | |
| 3856 | if (IS_ERR(trans)) { |
| 3857 | ret = PTR_ERR(trans); |
| 3858 | return ret; |
| 3859 | } |
| 3860 | |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 3861 | ret = btrfs_chunk_alloc(trans, ffe_ctl->flags, |
| 3862 | CHUNK_ALLOC_FORCE); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3863 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3864 | /* Do not bail out on ENOSPC since we can do more. */ |
Naohiro Aota | c70e213 | 2020-02-25 12:56:24 +0900 | [diff] [blame] | 3865 | if (ret == -ENOSPC) |
| 3866 | ret = chunk_allocation_failed(ffe_ctl); |
| 3867 | else if (ret < 0) |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3868 | btrfs_abort_transaction(trans, ret); |
| 3869 | else |
| 3870 | ret = 0; |
| 3871 | if (!exist) |
| 3872 | btrfs_end_transaction(trans); |
| 3873 | if (ret) |
| 3874 | return ret; |
| 3875 | } |
| 3876 | |
| 3877 | if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) { |
Naohiro Aota | 45d8e03 | 2020-02-25 12:56:25 +0900 | [diff] [blame] | 3878 | if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED) |
| 3879 | return -ENOSPC; |
| 3880 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3881 | /* |
| 3882 | * Don't loop again if we already have no empty_size and |
| 3883 | * no empty_cluster. |
| 3884 | */ |
| 3885 | if (ffe_ctl->empty_size == 0 && |
| 3886 | ffe_ctl->empty_cluster == 0) |
| 3887 | return -ENOSPC; |
| 3888 | ffe_ctl->empty_size = 0; |
| 3889 | ffe_ctl->empty_cluster = 0; |
| 3890 | } |
| 3891 | return 1; |
| 3892 | } |
| 3893 | return -ENOSPC; |
| 3894 | } |
| 3895 | |
Naohiro Aota | 7e89540 | 2020-02-25 12:56:26 +0900 | [diff] [blame] | 3896 | static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info, |
| 3897 | struct find_free_extent_ctl *ffe_ctl, |
| 3898 | struct btrfs_space_info *space_info, |
| 3899 | struct btrfs_key *ins) |
| 3900 | { |
| 3901 | /* |
| 3902 | * If our free space is heavily fragmented we may not be able to make |
| 3903 | * big contiguous allocations, so instead of doing the expensive search |
| 3904 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 3905 | * can go ahead and search for a more manageable chunk. |
| 3906 | * |
| 3907 | * If our max_extent_size is large enough for our allocation simply |
| 3908 | * disable clustering since we will likely not be able to find enough |
| 3909 | * space to create a cluster and induce latency trying. |
| 3910 | */ |
| 3911 | if (space_info->max_extent_size) { |
| 3912 | spin_lock(&space_info->lock); |
| 3913 | if (space_info->max_extent_size && |
| 3914 | ffe_ctl->num_bytes > space_info->max_extent_size) { |
| 3915 | ins->offset = space_info->max_extent_size; |
| 3916 | spin_unlock(&space_info->lock); |
| 3917 | return -ENOSPC; |
| 3918 | } else if (space_info->max_extent_size) { |
| 3919 | ffe_ctl->use_cluster = false; |
| 3920 | } |
| 3921 | spin_unlock(&space_info->lock); |
| 3922 | } |
| 3923 | |
| 3924 | ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info, |
| 3925 | &ffe_ctl->empty_cluster); |
| 3926 | if (ffe_ctl->last_ptr) { |
| 3927 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
| 3928 | |
| 3929 | spin_lock(&last_ptr->lock); |
| 3930 | if (last_ptr->block_group) |
| 3931 | ffe_ctl->hint_byte = last_ptr->window_start; |
| 3932 | if (last_ptr->fragmented) { |
| 3933 | /* |
| 3934 | * We still set window_start so we can keep track of the |
| 3935 | * last place we found an allocation to try and save |
| 3936 | * some time. |
| 3937 | */ |
| 3938 | ffe_ctl->hint_byte = last_ptr->window_start; |
| 3939 | ffe_ctl->use_cluster = false; |
| 3940 | } |
| 3941 | spin_unlock(&last_ptr->lock); |
| 3942 | } |
| 3943 | |
| 3944 | return 0; |
| 3945 | } |
| 3946 | |
| 3947 | static int prepare_allocation(struct btrfs_fs_info *fs_info, |
| 3948 | struct find_free_extent_ctl *ffe_ctl, |
| 3949 | struct btrfs_space_info *space_info, |
| 3950 | struct btrfs_key *ins) |
| 3951 | { |
| 3952 | switch (ffe_ctl->policy) { |
| 3953 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3954 | return prepare_allocation_clustered(fs_info, ffe_ctl, |
| 3955 | space_info, ins); |
| 3956 | default: |
| 3957 | BUG(); |
| 3958 | } |
| 3959 | } |
| 3960 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3961 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3962 | * walks the btree of allocated extents and find a hole of a given size. |
| 3963 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3964 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3965 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3966 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3967 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3968 | * |
| 3969 | * If there is no suitable free space, we will record the max size of |
| 3970 | * the free space extent currently. |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3971 | * |
| 3972 | * The overall logic and call chain: |
| 3973 | * |
| 3974 | * find_free_extent() |
| 3975 | * |- Iterate through all block groups |
| 3976 | * | |- Get a valid block group |
| 3977 | * | |- Try to do clustered allocation in that block group |
| 3978 | * | |- Try to do unclustered allocation in that block group |
| 3979 | * | |- Check if the result is valid |
| 3980 | * | | |- If valid, then exit |
| 3981 | * | |- Jump to next block group |
| 3982 | * | |
| 3983 | * |- Push harder to find free extents |
| 3984 | * |- If not found, re-iterate all block groups |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3985 | */ |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 3986 | static noinline int find_free_extent(struct btrfs_root *root, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 3987 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 3988 | u64 hint_byte_orig, struct btrfs_key *ins, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 3989 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3990 | { |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 3991 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3992 | int ret = 0; |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 3993 | int cache_block_group_error = 0; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3994 | struct btrfs_block_group *block_group = NULL; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3995 | struct find_free_extent_ctl ffe_ctl = {0}; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3996 | struct btrfs_space_info *space_info; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 3997 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3998 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3999 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4000 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4001 | ffe_ctl.num_bytes = num_bytes; |
| 4002 | ffe_ctl.empty_size = empty_size; |
| 4003 | ffe_ctl.flags = flags; |
| 4004 | ffe_ctl.search_start = 0; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4005 | ffe_ctl.delalloc = delalloc; |
| 4006 | ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags); |
| 4007 | ffe_ctl.have_caching_bg = false; |
| 4008 | ffe_ctl.orig_have_caching_bg = false; |
| 4009 | ffe_ctl.found_offset = 0; |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 4010 | ffe_ctl.hint_byte = hint_byte_orig; |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 4011 | ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4012 | |
Naohiro Aota | c10859b | 2020-02-25 12:56:18 +0900 | [diff] [blame] | 4013 | /* For clustered allocation */ |
| 4014 | ffe_ctl.retry_clustered = false; |
| 4015 | ffe_ctl.retry_unclustered = false; |
| 4016 | ffe_ctl.last_ptr = NULL; |
| 4017 | ffe_ctl.use_cluster = true; |
| 4018 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4019 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4020 | ins->objectid = 0; |
| 4021 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4022 | |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 4023 | trace_find_free_extent(root, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 4024 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 4025 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4026 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4027 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4028 | return -ENOSPC; |
| 4029 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4030 | |
Naohiro Aota | 7e89540 | 2020-02-25 12:56:26 +0900 | [diff] [blame] | 4031 | ret = prepare_allocation(fs_info, &ffe_ctl, space_info, ins); |
| 4032 | if (ret < 0) |
| 4033 | return ret; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4034 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4035 | ffe_ctl.search_start = max(ffe_ctl.search_start, |
| 4036 | first_logical_byte(fs_info, 0)); |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 4037 | ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte); |
| 4038 | if (ffe_ctl.search_start == ffe_ctl.hint_byte) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4039 | block_group = btrfs_lookup_block_group(fs_info, |
| 4040 | ffe_ctl.search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4041 | /* |
| 4042 | * we don't want to use the block group if it doesn't match our |
| 4043 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4044 | * |
| 4045 | * However if we are re-searching with an ideal block group |
| 4046 | * picked out then we don't care that the block group is cached. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4047 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4048 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 4049 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4050 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4051 | if (list_empty(&block_group->list) || |
| 4052 | block_group->ro) { |
| 4053 | /* |
| 4054 | * someone is removing this block group, |
| 4055 | * we can't jump into the have_block_group |
| 4056 | * target because our list pointers are not |
| 4057 | * valid |
| 4058 | */ |
| 4059 | btrfs_put_block_group(block_group); |
| 4060 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4061 | } else { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4062 | ffe_ctl.index = btrfs_bg_flags_to_raid_index( |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4063 | block_group->flags); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4064 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4065 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4066 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4067 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4068 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4069 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4070 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4071 | search: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4072 | ffe_ctl.have_caching_bg = false; |
| 4073 | if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) || |
| 4074 | ffe_ctl.index == 0) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 4075 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4076 | down_read(&space_info->groups_sem); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4077 | list_for_each_entry(block_group, |
| 4078 | &space_info->block_groups[ffe_ctl.index], list) { |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4079 | struct btrfs_block_group *bg_ret; |
| 4080 | |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 4081 | /* If the block group is read-only, we can skip it entirely. */ |
| 4082 | if (unlikely(block_group->ro)) |
| 4083 | continue; |
| 4084 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4085 | btrfs_grab_block_group(block_group, delalloc); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 4086 | ffe_ctl.search_start = block_group->start; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4087 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4088 | /* |
| 4089 | * this can happen if we end up cycling through all the |
| 4090 | * raid types, but we want to make sure we only allocate |
| 4091 | * for the proper type. |
| 4092 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4093 | if (!block_group_bits(block_group, flags)) { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 4094 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 4095 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 4096 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4097 | BTRFS_BLOCK_GROUP_RAID10; |
| 4098 | |
| 4099 | /* |
| 4100 | * if they asked for extra copies and this block group |
| 4101 | * doesn't provide them, bail. This does allow us to |
| 4102 | * fill raid0 from raid1. |
| 4103 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4104 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4105 | goto loop; |
Qu Wenruo | 2a28468 | 2019-07-16 17:00:33 +0800 | [diff] [blame] | 4106 | |
| 4107 | /* |
| 4108 | * This block group has different flags than we want. |
| 4109 | * It's possible that we have MIXED_GROUP flag but no |
| 4110 | * block group is mixed. Just skip such block group. |
| 4111 | */ |
| 4112 | btrfs_release_block_group(block_group, delalloc); |
| 4113 | continue; |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4114 | } |
| 4115 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4116 | have_block_group: |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4117 | ffe_ctl.cached = btrfs_block_group_done(block_group); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4118 | if (unlikely(!ffe_ctl.cached)) { |
| 4119 | ffe_ctl.have_caching_bg = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4120 | ret = btrfs_cache_block_group(block_group, 0); |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4121 | |
| 4122 | /* |
| 4123 | * If we get ENOMEM here or something else we want to |
| 4124 | * try other block groups, because it may not be fatal. |
| 4125 | * However if we can't find anything else we need to |
| 4126 | * save our return here so that we return the actual |
| 4127 | * error that caused problems, not ENOSPC. |
| 4128 | */ |
| 4129 | if (ret < 0) { |
| 4130 | if (!cache_block_group_error) |
| 4131 | cache_block_group_error = ret; |
| 4132 | ret = 0; |
| 4133 | goto loop; |
| 4134 | } |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 4135 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4136 | } |
| 4137 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4138 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 4139 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4140 | |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4141 | bg_ret = NULL; |
| 4142 | ret = do_allocation(block_group, &ffe_ctl, &bg_ret); |
| 4143 | if (ret == 0) { |
| 4144 | if (bg_ret && bg_ret != block_group) { |
| 4145 | btrfs_release_block_group(block_group, delalloc); |
| 4146 | block_group = bg_ret; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4147 | } |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4148 | } else if (ret == -EAGAIN) { |
| 4149 | goto have_block_group; |
| 4150 | } else if (ret > 0) { |
| 4151 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4152 | } |
| 4153 | |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4154 | /* Checks */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4155 | ffe_ctl.search_start = round_up(ffe_ctl.found_offset, |
| 4156 | fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4157 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4158 | /* move on to the next group */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4159 | if (ffe_ctl.search_start + num_bytes > |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 4160 | block_group->start + block_group->length) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4161 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4162 | num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4163 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4164 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4165 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4166 | if (ffe_ctl.found_offset < ffe_ctl.search_start) |
| 4167 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4168 | ffe_ctl.search_start - ffe_ctl.found_offset); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4169 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4170 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 4171 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4172 | if (ret == -EAGAIN) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4173 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4174 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4175 | goto loop; |
| 4176 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4177 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4178 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4179 | /* we are all good, lets return */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4180 | ins->objectid = ffe_ctl.search_start; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4181 | ins->offset = num_bytes; |
| 4182 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4183 | trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start, |
| 4184 | num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4185 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4186 | break; |
| 4187 | loop: |
Naohiro Aota | baba506 | 2020-02-25 12:56:21 +0900 | [diff] [blame] | 4188 | release_block_group(block_group, &ffe_ctl, delalloc); |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 4189 | cond_resched(); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4190 | } |
| 4191 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4192 | |
Naohiro Aota | 15b7ee6 | 2020-02-25 12:56:23 +0900 | [diff] [blame] | 4193 | ret = find_free_extent_update_loop(fs_info, ins, &ffe_ctl, full_search); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 4194 | if (ret > 0) |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 4195 | goto search; |
| 4196 | |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4197 | if (ret == -ENOSPC && !cache_block_group_error) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4198 | /* |
| 4199 | * Use ffe_ctl->total_free_space as fallback if we can't find |
| 4200 | * any contiguous hole. |
| 4201 | */ |
| 4202 | if (!ffe_ctl.max_extent_size) |
| 4203 | ffe_ctl.max_extent_size = ffe_ctl.total_free_space; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4204 | spin_lock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4205 | space_info->max_extent_size = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4206 | spin_unlock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4207 | ins->offset = ffe_ctl.max_extent_size; |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4208 | } else if (ret == -ENOSPC) { |
| 4209 | ret = cache_block_group_error; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4210 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4211 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4212 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4213 | |
Nikolay Borisov | 6f47c70 | 2018-03-13 12:22:32 +0200 | [diff] [blame] | 4214 | /* |
| 4215 | * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a |
| 4216 | * hole that is at least as big as @num_bytes. |
| 4217 | * |
| 4218 | * @root - The root that will contain this extent |
| 4219 | * |
| 4220 | * @ram_bytes - The amount of space in ram that @num_bytes take. This |
| 4221 | * is used for accounting purposes. This value differs |
| 4222 | * from @num_bytes only in the case of compressed extents. |
| 4223 | * |
| 4224 | * @num_bytes - Number of bytes to allocate on-disk. |
| 4225 | * |
| 4226 | * @min_alloc_size - Indicates the minimum amount of space that the |
| 4227 | * allocator should try to satisfy. In some cases |
| 4228 | * @num_bytes may be larger than what is required and if |
| 4229 | * the filesystem is fragmented then allocation fails. |
| 4230 | * However, the presence of @min_alloc_size gives a |
| 4231 | * chance to try and satisfy the smaller allocation. |
| 4232 | * |
| 4233 | * @empty_size - A hint that you plan on doing more COW. This is the |
| 4234 | * size in bytes the allocator should try to find free |
| 4235 | * next to the block it returns. This is just a hint and |
| 4236 | * may be ignored by the allocator. |
| 4237 | * |
| 4238 | * @hint_byte - Hint to the allocator to start searching above the byte |
| 4239 | * address passed. It might be ignored. |
| 4240 | * |
| 4241 | * @ins - This key is modified to record the found hole. It will |
| 4242 | * have the following values: |
| 4243 | * ins->objectid == start position |
| 4244 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
| 4245 | * ins->offset == the size of the hole. |
| 4246 | * |
| 4247 | * @is_data - Boolean flag indicating whether an extent is |
| 4248 | * allocated for data (true) or metadata (false) |
| 4249 | * |
| 4250 | * @delalloc - Boolean flag indicating whether this allocation is for |
| 4251 | * delalloc or not. If 'true' data_rwsem of block groups |
| 4252 | * is going to be acquired. |
| 4253 | * |
| 4254 | * |
| 4255 | * Returns 0 when an allocation succeeded or < 0 when an error occurred. In |
| 4256 | * case -ENOSPC is returned then @ins->offset will contain the size of the |
| 4257 | * largest available hole the allocator managed to find. |
| 4258 | */ |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4259 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4260 | u64 num_bytes, u64 min_alloc_size, |
| 4261 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4262 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4263 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4264 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 4265 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4266 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4267 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4268 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 4269 | flags = get_alloc_profile_by_root(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4270 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4271 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 4272 | ret = find_free_extent(root, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4273 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4274 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4275 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4276 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4277 | if (!final_tried && ins->offset) { |
| 4278 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4279 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4280 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4281 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4282 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4283 | if (num_bytes == min_alloc_size) |
| 4284 | final_tried = true; |
| 4285 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4286 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4287 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4288 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 4289 | sinfo = btrfs_find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4290 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4291 | "allocation failed flags %llu, wanted %llu", |
| 4292 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 4293 | if (sinfo) |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 4294 | btrfs_dump_space_info(fs_info, sinfo, |
| 4295 | num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4296 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4297 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4298 | |
| 4299 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4300 | } |
| 4301 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4302 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4303 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4304 | { |
Nikolay Borisov | 7ef54d5 | 2019-11-21 14:03:30 +0200 | [diff] [blame] | 4305 | struct btrfs_block_group *cache; |
| 4306 | |
| 4307 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4308 | if (!cache) { |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4309 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 4310 | start); |
Nikolay Borisov | 7ef54d5 | 2019-11-21 14:03:30 +0200 | [diff] [blame] | 4311 | return -ENOSPC; |
| 4312 | } |
| 4313 | |
| 4314 | btrfs_add_free_space(cache, start, len); |
| 4315 | btrfs_free_reserved_bytes(cache, len, delalloc); |
| 4316 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
| 4317 | |
| 4318 | btrfs_put_block_group(cache); |
| 4319 | return 0; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4320 | } |
| 4321 | |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 4322 | int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start, |
| 4323 | u64 len) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4324 | { |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4325 | struct btrfs_block_group *cache; |
| 4326 | int ret = 0; |
| 4327 | |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 4328 | cache = btrfs_lookup_block_group(trans->fs_info, start); |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4329 | if (!cache) { |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 4330 | btrfs_err(trans->fs_info, "unable to find block group for %llu", |
| 4331 | start); |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4332 | return -ENOSPC; |
| 4333 | } |
| 4334 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 4335 | ret = pin_down_extent(trans, cache, start, len, 1); |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4336 | btrfs_put_block_group(cache); |
| 4337 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4338 | } |
| 4339 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4340 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4341 | u64 parent, u64 root_objectid, |
| 4342 | u64 flags, u64 owner, u64 offset, |
| 4343 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4344 | { |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4345 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4346 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4347 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4348 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4349 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4350 | struct extent_buffer *leaf; |
| 4351 | int type; |
| 4352 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4353 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4354 | if (parent > 0) |
| 4355 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4356 | else |
| 4357 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4358 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4359 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4360 | |
| 4361 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4362 | if (!path) |
| 4363 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4364 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4365 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4366 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4367 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4368 | if (ret) { |
| 4369 | btrfs_free_path(path); |
| 4370 | return ret; |
| 4371 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4372 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4373 | leaf = path->nodes[0]; |
| 4374 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4375 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4376 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4377 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4378 | btrfs_set_extent_flags(leaf, extent_item, |
| 4379 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4380 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4381 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4382 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4383 | if (parent > 0) { |
| 4384 | struct btrfs_shared_data_ref *ref; |
| 4385 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4386 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4387 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4388 | } else { |
| 4389 | struct btrfs_extent_data_ref *ref; |
| 4390 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4391 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4392 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4393 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4394 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4395 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4396 | |
| 4397 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4398 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4399 | |
Nikolay Borisov | 25a356d | 2018-05-10 15:44:54 +0300 | [diff] [blame] | 4400 | ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4401 | if (ret) |
| 4402 | return ret; |
| 4403 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4404 | ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4405 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4406 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4407 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4408 | BUG(); |
| 4409 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4410 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4411 | return ret; |
| 4412 | } |
| 4413 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4414 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4415 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4416 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4417 | { |
Nikolay Borisov | 9dcdbe0 | 2018-05-21 12:27:20 +0300 | [diff] [blame] | 4418 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4419 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4420 | struct btrfs_extent_item *extent_item; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4421 | struct btrfs_key extent_key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4422 | struct btrfs_tree_block_info *block_info; |
| 4423 | struct btrfs_extent_inline_ref *iref; |
| 4424 | struct btrfs_path *path; |
| 4425 | struct extent_buffer *leaf; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4426 | struct btrfs_delayed_tree_ref *ref; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4427 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4428 | u64 num_bytes; |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4429 | u64 flags = extent_op->flags_to_set; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4430 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4431 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4432 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 4433 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4434 | extent_key.objectid = node->bytenr; |
| 4435 | if (skinny_metadata) { |
| 4436 | extent_key.offset = ref->level; |
| 4437 | extent_key.type = BTRFS_METADATA_ITEM_KEY; |
| 4438 | num_bytes = fs_info->nodesize; |
| 4439 | } else { |
| 4440 | extent_key.offset = node->num_bytes; |
| 4441 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4442 | size += sizeof(*block_info); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4443 | num_bytes = node->num_bytes; |
| 4444 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4445 | |
| 4446 | path = btrfs_alloc_path(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 4447 | if (!path) |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4448 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4449 | |
| 4450 | path->leave_spinning = 1; |
| 4451 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4452 | &extent_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4453 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 4454 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4455 | return ret; |
| 4456 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4457 | |
| 4458 | leaf = path->nodes[0]; |
| 4459 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4460 | struct btrfs_extent_item); |
| 4461 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4462 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4463 | btrfs_set_extent_flags(leaf, extent_item, |
| 4464 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4465 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4466 | if (skinny_metadata) { |
| 4467 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4468 | } else { |
| 4469 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4470 | btrfs_set_tree_block_key(leaf, block_info, &extent_op->key); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4471 | btrfs_set_tree_block_level(leaf, block_info, ref->level); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4472 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4473 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4474 | |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4475 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4476 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4477 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4478 | BTRFS_SHARED_BLOCK_REF_KEY); |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4479 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4480 | } else { |
| 4481 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4482 | BTRFS_TREE_BLOCK_REF_KEY); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4483 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | btrfs_mark_buffer_dirty(leaf); |
| 4487 | btrfs_free_path(path); |
| 4488 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4489 | ret = remove_from_free_space_tree(trans, extent_key.objectid, |
| 4490 | num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4491 | if (ret) |
| 4492 | return ret; |
| 4493 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4494 | ret = btrfs_update_block_group(trans, extent_key.objectid, |
| 4495 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4496 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4497 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4498 | extent_key.objectid, extent_key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4499 | BUG(); |
| 4500 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 4501 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4502 | trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4503 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4504 | return ret; |
| 4505 | } |
| 4506 | |
| 4507 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4508 | struct btrfs_root *root, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 4509 | u64 offset, u64 ram_bytes, |
| 4510 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4511 | { |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4512 | struct btrfs_ref generic_ref = { 0 }; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4513 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4514 | BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4515 | |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4516 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4517 | ins->objectid, ins->offset, 0); |
| 4518 | btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4519 | btrfs_ref_tree_mod(root->fs_info, &generic_ref); |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 4520 | |
| 4521 | return btrfs_add_delayed_data_ref(trans, &generic_ref, ram_bytes); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4522 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4523 | |
| 4524 | /* |
| 4525 | * this is used by the tree logging recovery code. It records that |
| 4526 | * an extent has been allocated and makes sure to clear the free |
| 4527 | * space cache bits as well |
| 4528 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4529 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4530 | u64 root_objectid, u64 owner, u64 offset, |
| 4531 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4532 | { |
Nikolay Borisov | 61da2ab | 2018-06-20 15:49:13 +0300 | [diff] [blame] | 4533 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4534 | int ret; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4535 | struct btrfs_block_group *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4536 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4537 | |
| 4538 | /* |
| 4539 | * Mixed block groups will exclude before processing the log so we only |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4540 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4541 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4542 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4543 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 4544 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4545 | if (ret) |
| 4546 | return ret; |
| 4547 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4548 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4549 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4550 | if (!block_group) |
| 4551 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4552 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4553 | space_info = block_group->space_info; |
| 4554 | spin_lock(&space_info->lock); |
| 4555 | spin_lock(&block_group->lock); |
| 4556 | space_info->bytes_reserved += ins->offset; |
| 4557 | block_group->reserved += ins->offset; |
| 4558 | spin_unlock(&block_group->lock); |
| 4559 | spin_unlock(&space_info->lock); |
| 4560 | |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4561 | ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, |
| 4562 | offset, ins, 1); |
Josef Bacik | bd72717 | 2020-02-13 10:47:30 -0500 | [diff] [blame] | 4563 | if (ret) |
Josef Bacik | ab9b2c7 | 2020-02-13 10:47:30 -0500 | [diff] [blame] | 4564 | btrfs_pin_extent(trans, ins->objectid, ins->offset, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4565 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4566 | return ret; |
| 4567 | } |
| 4568 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4569 | static struct extent_buffer * |
| 4570 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4571 | u64 bytenr, int level, u64 owner, |
| 4572 | enum btrfs_lock_nesting nest) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4573 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4574 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4575 | struct extent_buffer *buf; |
| 4576 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4577 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4578 | if (IS_ERR(buf)) |
| 4579 | return buf; |
| 4580 | |
Qu Wenruo | b72c3ab | 2018-08-21 09:53:47 +0800 | [diff] [blame] | 4581 | /* |
| 4582 | * Extra safety check in case the extent tree is corrupted and extent |
| 4583 | * allocator chooses to use a tree block which is already used and |
| 4584 | * locked. |
| 4585 | */ |
| 4586 | if (buf->lock_owner == current->pid) { |
| 4587 | btrfs_err_rl(fs_info, |
| 4588 | "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected", |
| 4589 | buf->start, btrfs_header_owner(buf), current->pid); |
| 4590 | free_extent_buffer(buf); |
| 4591 | return ERR_PTR(-EUCLEAN); |
| 4592 | } |
| 4593 | |
Josef Bacik | ad24466 | 2020-08-10 11:42:30 -0400 | [diff] [blame] | 4594 | btrfs_set_buffer_lockdep_class(owner, buf, level); |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4595 | __btrfs_tree_lock(buf, nest); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 4596 | btrfs_clean_tree_block(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4597 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4598 | |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4599 | btrfs_set_lock_blocking_write(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 4600 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4601 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4602 | memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header)); |
| 4603 | btrfs_set_header_level(buf, level); |
| 4604 | btrfs_set_header_bytenr(buf, buf->start); |
| 4605 | btrfs_set_header_generation(buf, trans->transid); |
| 4606 | btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV); |
| 4607 | btrfs_set_header_owner(buf, owner); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 4608 | write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4609 | write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4610 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4611 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4612 | /* |
| 4613 | * we allow two log transactions at a time, use different |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4614 | * EXTENT bit to differentiate dirty pages. |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4615 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4616 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4617 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 4618 | buf->start + buf->len - 1, GFP_NOFS); |
| 4619 | else |
| 4620 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 4621 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4622 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4623 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4624 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4625 | buf->start + buf->len - 1, GFP_NOFS); |
| 4626 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 4627 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4628 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4629 | return buf; |
| 4630 | } |
| 4631 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4632 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4633 | * finds a free extent and does all the dirty work required for allocation |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4634 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4635 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 4636 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4637 | struct btrfs_root *root, |
| 4638 | u64 parent, u64 root_objectid, |
| 4639 | const struct btrfs_disk_key *key, |
| 4640 | int level, u64 hint, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4641 | u64 empty_size, |
| 4642 | enum btrfs_lock_nesting nest) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4643 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4644 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4645 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4646 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4647 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4648 | struct btrfs_delayed_extent_op *extent_op; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4649 | struct btrfs_ref generic_ref = { 0 }; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4650 | u64 flags = 0; |
| 4651 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4652 | u32 blocksize = fs_info->nodesize; |
| 4653 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4654 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4655 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4656 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4657 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4658 | level, root_objectid, nest); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4659 | if (!IS_ERR(buf)) |
| 4660 | root->alloc_bytenr += blocksize; |
| 4661 | return buf; |
| 4662 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4663 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 4664 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4665 | block_rsv = btrfs_use_block_rsv(trans, root, blocksize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4666 | if (IS_ERR(block_rsv)) |
| 4667 | return ERR_CAST(block_rsv); |
| 4668 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4669 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4670 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4671 | if (ret) |
| 4672 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4673 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4674 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4675 | root_objectid, nest); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4676 | if (IS_ERR(buf)) { |
| 4677 | ret = PTR_ERR(buf); |
| 4678 | goto out_free_reserved; |
| 4679 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4680 | |
| 4681 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4682 | if (parent == 0) |
| 4683 | parent = ins.objectid; |
| 4684 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4685 | } else |
| 4686 | BUG_ON(parent > 0); |
| 4687 | |
| 4688 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 4689 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4690 | if (!extent_op) { |
| 4691 | ret = -ENOMEM; |
| 4692 | goto out_free_buf; |
| 4693 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4694 | if (key) |
| 4695 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4696 | else |
| 4697 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4698 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 4699 | extent_op->update_key = skinny_metadata ? false : true; |
| 4700 | extent_op->update_flags = true; |
| 4701 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4702 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4703 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4704 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4705 | ins.objectid, ins.offset, parent); |
| 4706 | generic_ref.real_root = root->root_key.objectid; |
| 4707 | btrfs_init_tree_ref(&generic_ref, level, root_objectid); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4708 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Josef Bacik | 7ec1536 | 2021-01-15 16:48:55 -0500 | [diff] [blame] | 4709 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, extent_op); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4710 | if (ret) |
| 4711 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4712 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4713 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4714 | |
| 4715 | out_free_delayed: |
| 4716 | btrfs_free_delayed_extent_op(extent_op); |
| 4717 | out_free_buf: |
| 4718 | free_extent_buffer(buf); |
| 4719 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4720 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4721 | out_unuse: |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4722 | btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4723 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4724 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4725 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4726 | struct walk_control { |
| 4727 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4728 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4729 | struct btrfs_key update_progress; |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 4730 | struct btrfs_key drop_progress; |
| 4731 | int drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4732 | int stage; |
| 4733 | int level; |
| 4734 | int shared_level; |
| 4735 | int update_ref; |
| 4736 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4737 | int reada_slot; |
| 4738 | int reada_count; |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4739 | int restarted; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4740 | }; |
| 4741 | |
| 4742 | #define DROP_REFERENCE 1 |
| 4743 | #define UPDATE_BACKREF 2 |
| 4744 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4745 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4746 | struct btrfs_root *root, |
| 4747 | struct walk_control *wc, |
| 4748 | struct btrfs_path *path) |
| 4749 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4750 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4751 | u64 bytenr; |
| 4752 | u64 generation; |
| 4753 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4754 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4755 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4756 | struct btrfs_key key; |
| 4757 | struct extent_buffer *eb; |
| 4758 | int ret; |
| 4759 | int slot; |
| 4760 | int nread = 0; |
| 4761 | |
| 4762 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4763 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4764 | wc->reada_count = max(wc->reada_count, 2); |
| 4765 | } else { |
| 4766 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4767 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4768 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | eb = path->nodes[wc->level]; |
| 4772 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4773 | |
| 4774 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 4775 | if (nread >= wc->reada_count) |
| 4776 | break; |
| 4777 | |
| 4778 | cond_resched(); |
| 4779 | bytenr = btrfs_node_blockptr(eb, slot); |
| 4780 | generation = btrfs_node_ptr_generation(eb, slot); |
| 4781 | |
| 4782 | if (slot == path->slots[wc->level]) |
| 4783 | goto reada; |
| 4784 | |
| 4785 | if (wc->stage == UPDATE_BACKREF && |
| 4786 | generation <= root->root_key.offset) |
| 4787 | continue; |
| 4788 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4789 | /* We don't lock the tree block, it's OK to be racy here */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4790 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4791 | wc->level - 1, 1, &refs, |
| 4792 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4793 | /* We don't care about errors in readahead. */ |
| 4794 | if (ret < 0) |
| 4795 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4796 | BUG_ON(refs == 0); |
| 4797 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4798 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4799 | if (refs == 1) |
| 4800 | goto reada; |
| 4801 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4802 | if (wc->level == 1 && |
| 4803 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4804 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4805 | if (!wc->update_ref || |
| 4806 | generation <= root->root_key.offset) |
| 4807 | continue; |
| 4808 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 4809 | ret = btrfs_comp_cpu_keys(&key, |
| 4810 | &wc->update_progress); |
| 4811 | if (ret < 0) |
| 4812 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4813 | } else { |
| 4814 | if (wc->level == 1 && |
| 4815 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4816 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4817 | } |
| 4818 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4819 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4820 | nread++; |
| 4821 | } |
| 4822 | wc->reada_slot = slot; |
| 4823 | } |
| 4824 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4825 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4826 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4827 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4828 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 4829 | * back refs for pointers in the block. |
| 4830 | * |
| 4831 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4832 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4833 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 4834 | struct btrfs_root *root, |
| 4835 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4836 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4837 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4838 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4839 | int level = wc->level; |
| 4840 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4841 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4842 | int ret; |
| 4843 | |
| 4844 | if (wc->stage == UPDATE_BACKREF && |
| 4845 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 4846 | return 1; |
| 4847 | |
| 4848 | /* |
| 4849 | * when reference count of tree block is 1, it won't increase |
| 4850 | * again. once full backref flag is set, we never clear it. |
| 4851 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4852 | if (lookup_info && |
| 4853 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 4854 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4855 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4856 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4857 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4858 | &wc->refs[level], |
| 4859 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4860 | BUG_ON(ret == -ENOMEM); |
| 4861 | if (ret) |
| 4862 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4863 | BUG_ON(wc->refs[level] == 0); |
| 4864 | } |
| 4865 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4866 | if (wc->stage == DROP_REFERENCE) { |
| 4867 | if (wc->refs[level] > 1) |
| 4868 | return 1; |
| 4869 | |
| 4870 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4871 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4872 | path->locks[level] = 0; |
| 4873 | } |
| 4874 | return 0; |
| 4875 | } |
| 4876 | |
| 4877 | /* wc->stage == UPDATE_BACKREF */ |
| 4878 | if (!(wc->flags[level] & flag)) { |
| 4879 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4880 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4881 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4882 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4883 | BUG_ON(ret); /* -ENOMEM */ |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 4884 | ret = btrfs_set_disk_extent_flags(trans, eb, flag, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4885 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4886 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4887 | wc->flags[level] |= flag; |
| 4888 | } |
| 4889 | |
| 4890 | /* |
| 4891 | * the block is shared by multiple trees, so it's not good to |
| 4892 | * keep the tree lock |
| 4893 | */ |
| 4894 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4895 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4896 | path->locks[level] = 0; |
| 4897 | } |
| 4898 | return 0; |
| 4899 | } |
| 4900 | |
| 4901 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4902 | * This is used to verify a ref exists for this root to deal with a bug where we |
| 4903 | * would have a drop_progress key that hadn't been updated properly. |
| 4904 | */ |
| 4905 | static int check_ref_exists(struct btrfs_trans_handle *trans, |
| 4906 | struct btrfs_root *root, u64 bytenr, u64 parent, |
| 4907 | int level) |
| 4908 | { |
| 4909 | struct btrfs_path *path; |
| 4910 | struct btrfs_extent_inline_ref *iref; |
| 4911 | int ret; |
| 4912 | |
| 4913 | path = btrfs_alloc_path(); |
| 4914 | if (!path) |
| 4915 | return -ENOMEM; |
| 4916 | |
| 4917 | ret = lookup_extent_backref(trans, path, &iref, bytenr, |
| 4918 | root->fs_info->nodesize, parent, |
| 4919 | root->root_key.objectid, level, 0); |
| 4920 | btrfs_free_path(path); |
| 4921 | if (ret == -ENOENT) |
| 4922 | return 0; |
| 4923 | if (ret < 0) |
| 4924 | return ret; |
| 4925 | return 1; |
| 4926 | } |
| 4927 | |
| 4928 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4929 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4930 | * |
| 4931 | * when wc->stage == DROP_REFERENCE, this function checks |
| 4932 | * reference count of the block pointed to. if the block |
| 4933 | * is shared and we need update back refs for the subtree |
| 4934 | * rooted at the block, this function changes wc->stage to |
| 4935 | * UPDATE_BACKREF. if the block is shared and there is no |
| 4936 | * need to update back, this function drops the reference |
| 4937 | * to the block. |
| 4938 | * |
| 4939 | * NOTE: return value 1 means we should stop walking down. |
| 4940 | */ |
| 4941 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 4942 | struct btrfs_root *root, |
| 4943 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4944 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4945 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4946 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4947 | u64 bytenr; |
| 4948 | u64 generation; |
| 4949 | u64 parent; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4950 | struct btrfs_key key; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4951 | struct btrfs_key first_key; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4952 | struct btrfs_ref ref = { 0 }; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4953 | struct extent_buffer *next; |
| 4954 | int level = wc->level; |
| 4955 | int reada = 0; |
| 4956 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4957 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4958 | |
| 4959 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 4960 | path->slots[level]); |
| 4961 | /* |
| 4962 | * if the lower level block was created before the snapshot |
| 4963 | * was created, we know there is no need to update back refs |
| 4964 | * for the subtree |
| 4965 | */ |
| 4966 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4967 | generation <= root->root_key.offset) { |
| 4968 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4969 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4970 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4971 | |
| 4972 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4973 | btrfs_node_key_to_cpu(path->nodes[level], &first_key, |
| 4974 | path->slots[level]); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4975 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4976 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4977 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4978 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4979 | if (IS_ERR(next)) |
| 4980 | return PTR_ERR(next); |
| 4981 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 4982 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 4983 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4984 | reada = 1; |
| 4985 | } |
| 4986 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4987 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4988 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4989 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4990 | &wc->refs[level - 1], |
| 4991 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4992 | if (ret < 0) |
| 4993 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4994 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4995 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4996 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4997 | ret = -EIO; |
| 4998 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4999 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5000 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5001 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5002 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5003 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5004 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5005 | if (level == 1 && |
| 5006 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5007 | goto skip; |
| 5008 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5009 | if (!wc->update_ref || |
| 5010 | generation <= root->root_key.offset) |
| 5011 | goto skip; |
| 5012 | |
| 5013 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5014 | path->slots[level]); |
| 5015 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5016 | if (ret < 0) |
| 5017 | goto skip; |
| 5018 | |
| 5019 | wc->stage = UPDATE_BACKREF; |
| 5020 | wc->shared_level = level - 1; |
| 5021 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5022 | } else { |
| 5023 | if (level == 1 && |
| 5024 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5025 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5026 | } |
| 5027 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 5028 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5029 | btrfs_tree_unlock(next); |
| 5030 | free_extent_buffer(next); |
| 5031 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5032 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | if (!next) { |
| 5036 | if (reada && level == 1) |
| 5037 | reada_walk_down(trans, root, wc, path); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 5038 | next = read_tree_block(fs_info, bytenr, generation, level - 1, |
| 5039 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 5040 | if (IS_ERR(next)) { |
| 5041 | return PTR_ERR(next); |
| 5042 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 5043 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 5044 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 5045 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5046 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5047 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5051 | ASSERT(level == btrfs_header_level(next)); |
| 5052 | if (level != btrfs_header_level(next)) { |
| 5053 | btrfs_err(root->fs_info, "mismatched level"); |
| 5054 | ret = -EIO; |
| 5055 | goto out_unlock; |
| 5056 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5057 | path->nodes[level] = next; |
| 5058 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5059 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5060 | wc->level = level; |
| 5061 | if (wc->level == 1) |
| 5062 | wc->reada_slot = 0; |
| 5063 | return 0; |
| 5064 | skip: |
| 5065 | wc->refs[level - 1] = 0; |
| 5066 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5067 | if (wc->stage == DROP_REFERENCE) { |
| 5068 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5069 | parent = path->nodes[level]->start; |
| 5070 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5071 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5072 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5073 | if (root->root_key.objectid != |
| 5074 | btrfs_header_owner(path->nodes[level])) { |
| 5075 | btrfs_err(root->fs_info, |
| 5076 | "mismatched block owner"); |
| 5077 | ret = -EIO; |
| 5078 | goto out_unlock; |
| 5079 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5080 | parent = 0; |
| 5081 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5082 | |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 5083 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5084 | * If we had a drop_progress we need to verify the refs are set |
| 5085 | * as expected. If we find our ref then we know that from here |
| 5086 | * on out everything should be correct, and we can clear the |
| 5087 | * ->restarted flag. |
| 5088 | */ |
| 5089 | if (wc->restarted) { |
| 5090 | ret = check_ref_exists(trans, root, bytenr, parent, |
| 5091 | level - 1); |
| 5092 | if (ret < 0) |
| 5093 | goto out_unlock; |
| 5094 | if (ret == 0) |
| 5095 | goto no_delete; |
| 5096 | ret = 0; |
| 5097 | wc->restarted = 0; |
| 5098 | } |
| 5099 | |
| 5100 | /* |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 5101 | * Reloc tree doesn't contribute to qgroup numbers, and we have |
| 5102 | * already accounted them at merge time (replace_path), |
| 5103 | * thus we could skip expensive subtree trace here. |
| 5104 | */ |
| 5105 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
| 5106 | need_account) { |
Lu Fengqi | deb4062 | 2018-07-18 14:45:38 +0800 | [diff] [blame] | 5107 | ret = btrfs_qgroup_trace_subtree(trans, next, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 5108 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5109 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5110 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5111 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 5112 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5113 | } |
| 5114 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5115 | |
| 5116 | /* |
| 5117 | * We need to update the next key in our walk control so we can |
| 5118 | * update the drop_progress key accordingly. We don't care if |
| 5119 | * find_next_key doesn't find a key because that means we're at |
| 5120 | * the end and are going to clean up now. |
| 5121 | */ |
| 5122 | wc->drop_level = level; |
| 5123 | find_next_key(path, level, &wc->drop_progress); |
| 5124 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 5125 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
| 5126 | fs_info->nodesize, parent); |
| 5127 | btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid); |
| 5128 | ret = btrfs_free_extent(trans, &ref); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5129 | if (ret) |
| 5130 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5131 | } |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5132 | no_delete: |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5133 | *lookup_info = 1; |
| 5134 | ret = 1; |
| 5135 | |
| 5136 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5137 | btrfs_tree_unlock(next); |
| 5138 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5139 | |
| 5140 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 5144 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5145 | * |
| 5146 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5147 | * reference count on the block. |
| 5148 | * |
| 5149 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5150 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5151 | * to UPDATE_BACKREF previously while processing the block. |
| 5152 | * |
| 5153 | * NOTE: return value 1 means we should stop walking up. |
| 5154 | */ |
| 5155 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5156 | struct btrfs_root *root, |
| 5157 | struct btrfs_path *path, |
| 5158 | struct walk_control *wc) |
| 5159 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5160 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5161 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5162 | int level = wc->level; |
| 5163 | struct extent_buffer *eb = path->nodes[level]; |
| 5164 | u64 parent = 0; |
| 5165 | |
| 5166 | if (wc->stage == UPDATE_BACKREF) { |
| 5167 | BUG_ON(wc->shared_level < level); |
| 5168 | if (level < wc->shared_level) |
| 5169 | goto out; |
| 5170 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5171 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5172 | if (ret > 0) |
| 5173 | wc->update_ref = 0; |
| 5174 | |
| 5175 | wc->stage = DROP_REFERENCE; |
| 5176 | wc->shared_level = -1; |
| 5177 | path->slots[level] = 0; |
| 5178 | |
| 5179 | /* |
| 5180 | * check reference count again if the block isn't locked. |
| 5181 | * we should start walking down the tree again if reference |
| 5182 | * count is one. |
| 5183 | */ |
| 5184 | if (!path->locks[level]) { |
| 5185 | BUG_ON(level == 0); |
| 5186 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5187 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5188 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5189 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5190 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5191 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5192 | &wc->refs[level], |
| 5193 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5194 | if (ret < 0) { |
| 5195 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5196 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5197 | return ret; |
| 5198 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5199 | BUG_ON(wc->refs[level] == 0); |
| 5200 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5201 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5202 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5203 | return 1; |
| 5204 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /* wc->stage == DROP_REFERENCE */ |
| 5209 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5210 | |
| 5211 | if (wc->refs[level] == 1) { |
| 5212 | if (level == 0) { |
| 5213 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5214 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5215 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5216 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5217 | BUG_ON(ret); /* -ENOMEM */ |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5218 | if (is_fstree(root->root_key.objectid)) { |
| 5219 | ret = btrfs_qgroup_trace_leaf_items(trans, eb); |
| 5220 | if (ret) { |
| 5221 | btrfs_err_rl(fs_info, |
| 5222 | "error %d accounting leaf items, quota is out of sync, rescan required", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5223 | ret); |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5224 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5225 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5226 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5227 | /* make block locked assertion in btrfs_clean_tree_block happy */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5228 | if (!path->locks[level] && |
| 5229 | btrfs_header_generation(eb) == trans->transid) { |
| 5230 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5231 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5232 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5233 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5234 | btrfs_clean_tree_block(eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5235 | } |
| 5236 | |
| 5237 | if (eb == root->node) { |
| 5238 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5239 | parent = eb->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5240 | else if (root->root_key.objectid != btrfs_header_owner(eb)) |
| 5241 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5242 | } else { |
| 5243 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5244 | parent = path->nodes[level + 1]->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5245 | else if (root->root_key.objectid != |
| 5246 | btrfs_header_owner(path->nodes[level + 1])) |
| 5247 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5248 | } |
| 5249 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5250 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5251 | out: |
| 5252 | wc->refs[level] = 0; |
| 5253 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5254 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5255 | |
| 5256 | owner_mismatch: |
| 5257 | btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", |
| 5258 | btrfs_header_owner(eb), root->root_key.objectid); |
| 5259 | return -EUCLEAN; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5260 | } |
| 5261 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5262 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5263 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5264 | struct btrfs_path *path, |
| 5265 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5266 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5267 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5268 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5269 | int ret; |
| 5270 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5271 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5272 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5273 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5274 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5275 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5276 | if (level == 0) |
| 5277 | break; |
| 5278 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5279 | if (path->slots[level] >= |
| 5280 | btrfs_header_nritems(path->nodes[level])) |
| 5281 | break; |
| 5282 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5283 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5284 | if (ret > 0) { |
| 5285 | path->slots[level]++; |
| 5286 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5287 | } else if (ret < 0) |
| 5288 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5289 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5290 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5291 | return 0; |
| 5292 | } |
| 5293 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5294 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5295 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5296 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5297 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5298 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5299 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5300 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5301 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5302 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5303 | while (level < max_level && path->nodes[level]) { |
| 5304 | wc->level = level; |
| 5305 | if (path->slots[level] + 1 < |
| 5306 | btrfs_header_nritems(path->nodes[level])) { |
| 5307 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5308 | return 0; |
| 5309 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5310 | ret = walk_up_proc(trans, root, path, wc); |
| 5311 | if (ret > 0) |
| 5312 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5313 | if (ret < 0) |
| 5314 | return ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5315 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5316 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5317 | btrfs_tree_unlock_rw(path->nodes[level], |
| 5318 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5319 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5320 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5321 | free_extent_buffer(path->nodes[level]); |
| 5322 | path->nodes[level] = NULL; |
| 5323 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5324 | } |
| 5325 | } |
| 5326 | return 1; |
| 5327 | } |
| 5328 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5329 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5330 | * drop a subvolume tree. |
| 5331 | * |
| 5332 | * this function traverses the tree freeing any blocks that only |
| 5333 | * referenced by the tree. |
| 5334 | * |
| 5335 | * when a shared tree block is found. this function decreases its |
| 5336 | * reference count by one. if update_ref is true, this function |
| 5337 | * also make sure backrefs for the shared block and all lower level |
| 5338 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5339 | * |
| 5340 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5341 | */ |
Nikolay Borisov | 0078a9f | 2020-03-10 11:43:51 +0200 | [diff] [blame] | 5342 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5343 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5344 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5345 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5346 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5347 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5348 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5349 | struct walk_control *wc; |
| 5350 | struct btrfs_key key; |
| 5351 | int err = 0; |
| 5352 | int ret; |
| 5353 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5354 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5355 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 5356 | btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5357 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5358 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5359 | if (!path) { |
| 5360 | err = -ENOMEM; |
| 5361 | goto out; |
| 5362 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5363 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5364 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5365 | if (!wc) { |
| 5366 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5367 | err = -ENOMEM; |
| 5368 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5369 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5370 | |
Qu Wenruo | f3e3d9c | 2020-07-13 09:03:20 +0800 | [diff] [blame] | 5371 | /* |
| 5372 | * Use join to avoid potential EINTR from transaction start. See |
| 5373 | * wait_reserve_ticket and the whole reservation callchain. |
| 5374 | */ |
| 5375 | if (for_reloc) |
| 5376 | trans = btrfs_join_transaction(tree_root); |
| 5377 | else |
| 5378 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5379 | if (IS_ERR(trans)) { |
| 5380 | err = PTR_ERR(trans); |
| 5381 | goto out_free; |
| 5382 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 5383 | |
Josef Bacik | 0568e82 | 2018-11-30 11:52:14 -0500 | [diff] [blame] | 5384 | err = btrfs_run_delayed_items(trans); |
| 5385 | if (err) |
| 5386 | goto out_end_trans; |
| 5387 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 5388 | /* |
| 5389 | * This will help us catch people modifying the fs tree while we're |
| 5390 | * dropping it. It is unsafe to mess with the fs tree while it's being |
| 5391 | * dropped as we unlock the root node and parent nodes as we walk down |
| 5392 | * the tree, assuming nothing will change. If something does change |
| 5393 | * then we'll have stale information and drop references to blocks we've |
| 5394 | * already dropped. |
| 5395 | */ |
| 5396 | set_bit(BTRFS_ROOT_DELETING, &root->state); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5397 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5398 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5399 | path->nodes[level] = btrfs_lock_root_node(root); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5400 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5401 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5402 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5403 | memset(&wc->update_progress, 0, |
| 5404 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5405 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5406 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5407 | memcpy(&wc->update_progress, &key, |
| 5408 | sizeof(wc->update_progress)); |
| 5409 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5410 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5411 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5412 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5413 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5414 | path->lowest_level = 0; |
| 5415 | if (ret < 0) { |
| 5416 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5417 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5418 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5419 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5420 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5421 | /* |
| 5422 | * unlock our path, this is safe because only this |
| 5423 | * function is allowed to delete this snapshot |
| 5424 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5425 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5426 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5427 | level = btrfs_header_level(root->node); |
| 5428 | while (1) { |
| 5429 | btrfs_tree_lock(path->nodes[level]); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5430 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5431 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5432 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5433 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5434 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5435 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5436 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5437 | if (ret < 0) { |
| 5438 | err = ret; |
| 5439 | goto out_end_trans; |
| 5440 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5441 | BUG_ON(wc->refs[level] == 0); |
| 5442 | |
| 5443 | if (level == root_item->drop_level) |
| 5444 | break; |
| 5445 | |
| 5446 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5447 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5448 | WARN_ON(wc->refs[level] != 1); |
| 5449 | level--; |
| 5450 | } |
| 5451 | } |
| 5452 | |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5453 | wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5454 | wc->level = level; |
| 5455 | wc->shared_level = -1; |
| 5456 | wc->stage = DROP_REFERENCE; |
| 5457 | wc->update_ref = update_ref; |
| 5458 | wc->keep_locks = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5459 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5460 | |
| 5461 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5462 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5463 | ret = walk_down_tree(trans, root, path, wc); |
| 5464 | if (ret < 0) { |
| 5465 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5466 | break; |
| 5467 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5468 | |
| 5469 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5470 | if (ret < 0) { |
| 5471 | err = ret; |
| 5472 | break; |
| 5473 | } |
| 5474 | |
| 5475 | if (ret > 0) { |
| 5476 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5477 | break; |
| 5478 | } |
| 5479 | |
| 5480 | if (wc->stage == DROP_REFERENCE) { |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5481 | wc->drop_level = wc->level; |
| 5482 | btrfs_node_key_to_cpu(path->nodes[wc->drop_level], |
| 5483 | &wc->drop_progress, |
| 5484 | path->slots[wc->drop_level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5485 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5486 | btrfs_cpu_key_to_disk(&root_item->drop_progress, |
| 5487 | &wc->drop_progress); |
| 5488 | root_item->drop_level = wc->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5489 | |
| 5490 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5491 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5492 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5493 | ret = btrfs_update_root(trans, tree_root, |
| 5494 | &root->root_key, |
| 5495 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5496 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5497 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5498 | err = ret; |
| 5499 | goto out_end_trans; |
| 5500 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5501 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5502 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5503 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5504 | btrfs_debug(fs_info, |
| 5505 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 5506 | err = -EAGAIN; |
| 5507 | goto out_free; |
| 5508 | } |
| 5509 | |
Josef Bacik | 9e2fc8f | 2020-12-16 11:22:05 -0500 | [diff] [blame] | 5510 | /* |
| 5511 | * Use join to avoid potential EINTR from transaction |
| 5512 | * start. See wait_reserve_ticket and the whole |
| 5513 | * reservation callchain. |
| 5514 | */ |
| 5515 | if (for_reloc) |
| 5516 | trans = btrfs_join_transaction(tree_root); |
| 5517 | else |
| 5518 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5519 | if (IS_ERR(trans)) { |
| 5520 | err = PTR_ERR(trans); |
| 5521 | goto out_free; |
| 5522 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5523 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5524 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5525 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5526 | if (err) |
| 5527 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5528 | |
Lu Fengqi | ab9ce7d | 2018-08-01 11:32:27 +0800 | [diff] [blame] | 5529 | ret = btrfs_del_root(trans, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5530 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5531 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | e19182c | 2017-12-04 13:11:45 -0500 | [diff] [blame] | 5532 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5533 | goto out_end_trans; |
| 5534 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5535 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5536 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5537 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 5538 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5539 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5540 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5541 | err = ret; |
| 5542 | goto out_end_trans; |
| 5543 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 5544 | /* if we fail to delete the orphan item this time |
| 5545 | * around, it'll get picked up the next time. |
| 5546 | * |
| 5547 | * The most common failure here is just -ENOENT. |
| 5548 | */ |
| 5549 | btrfs_del_orphan_item(trans, tree_root, |
| 5550 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5551 | } |
| 5552 | } |
| 5553 | |
Qu Wenruo | a3cf0e4 | 2020-07-14 09:12:20 +0800 | [diff] [blame] | 5554 | /* |
| 5555 | * This subvolume is going to be completely dropped, and won't be |
| 5556 | * recorded as dirty roots, thus pertrans meta rsv will not be freed at |
| 5557 | * commit transaction time. So free it here manually. |
| 5558 | */ |
| 5559 | btrfs_qgroup_convert_reserved_meta(root, INT_MAX); |
| 5560 | btrfs_qgroup_free_meta_all_pertrans(root); |
| 5561 | |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 5562 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 5563 | btrfs_add_dropped_root(trans, root); |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 5564 | else |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5565 | btrfs_put_root(root); |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5566 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5567 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5568 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5569 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5570 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5571 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5572 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5573 | /* |
| 5574 | * So if we need to stop dropping the snapshot for whatever reason we |
| 5575 | * need to make sure to add it back to the dead root list so that we |
| 5576 | * keep trying to do the work later. This also cleans up roots if we |
| 5577 | * don't have it in the radix (like when we recover after a power fail |
| 5578 | * or unmount) so we don't leak memory. |
| 5579 | */ |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 5580 | if (!for_reloc && !root_dropped) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5581 | btrfs_add_dead_root(root); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 5582 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5583 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5584 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5585 | /* |
| 5586 | * drop subtree rooted at tree block 'node'. |
| 5587 | * |
| 5588 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5589 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5590 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5591 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5592 | struct btrfs_root *root, |
| 5593 | struct extent_buffer *node, |
| 5594 | struct extent_buffer *parent) |
| 5595 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5596 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5597 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5598 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5599 | int level; |
| 5600 | int parent_level; |
| 5601 | int ret = 0; |
| 5602 | int wret; |
| 5603 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5604 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5605 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5606 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5607 | if (!path) |
| 5608 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5609 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5610 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5611 | if (!wc) { |
| 5612 | btrfs_free_path(path); |
| 5613 | return -ENOMEM; |
| 5614 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5615 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5616 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5617 | parent_level = btrfs_header_level(parent); |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 5618 | atomic_inc(&parent->refs); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5619 | path->nodes[parent_level] = parent; |
| 5620 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5621 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5622 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5623 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5624 | path->nodes[level] = node; |
| 5625 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5626 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5627 | |
| 5628 | wc->refs[parent_level] = 1; |
| 5629 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5630 | wc->level = level; |
| 5631 | wc->shared_level = -1; |
| 5632 | wc->stage = DROP_REFERENCE; |
| 5633 | wc->update_ref = 0; |
| 5634 | wc->keep_locks = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5635 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5636 | |
| 5637 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5638 | wret = walk_down_tree(trans, root, path, wc); |
| 5639 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5640 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5641 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5642 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5643 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5644 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5645 | if (wret < 0) |
| 5646 | ret = wret; |
| 5647 | if (wret != 0) |
| 5648 | break; |
| 5649 | } |
| 5650 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5651 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5652 | btrfs_free_path(path); |
| 5653 | return ret; |
| 5654 | } |
| 5655 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5656 | /* |
| 5657 | * helper to account the unused space of all the readonly block group in the |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5658 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5659 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5660 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5661 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 5662 | struct btrfs_block_group *block_group; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5663 | u64 free_bytes = 0; |
| 5664 | int factor; |
| 5665 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5666 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5667 | if (list_empty(&sinfo->ro_bgs)) |
| 5668 | return 0; |
| 5669 | |
| 5670 | spin_lock(&sinfo->lock); |
| 5671 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5672 | spin_lock(&block_group->lock); |
| 5673 | |
| 5674 | if (!block_group->ro) { |
| 5675 | spin_unlock(&block_group->lock); |
| 5676 | continue; |
| 5677 | } |
| 5678 | |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 5679 | factor = btrfs_bg_type_to_factor(block_group->flags); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 5680 | free_bytes += (block_group->length - |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 5681 | block_group->used) * factor; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5682 | |
| 5683 | spin_unlock(&block_group->lock); |
| 5684 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5685 | spin_unlock(&sinfo->lock); |
| 5686 | |
| 5687 | return free_bytes; |
| 5688 | } |
| 5689 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5690 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 5691 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5692 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5693 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5694 | } |
| 5695 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5696 | /* |
| 5697 | * It used to be that old block groups would be left around forever. |
| 5698 | * Iterating over them would be enough to trim unused space. Since we |
| 5699 | * now automatically remove them, we also need to iterate over unallocated |
| 5700 | * space. |
| 5701 | * |
| 5702 | * We don't want a transaction for this since the discard may take a |
| 5703 | * substantial amount of time. We don't require that a transaction be |
| 5704 | * running, but we do need to take a running transaction into account |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5705 | * to ensure that we're not discarding chunks that were released or |
| 5706 | * allocated in the current transaction. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5707 | * |
| 5708 | * Holding the chunks lock will prevent other threads from allocating |
| 5709 | * or releasing chunks, but it won't prevent a running transaction |
| 5710 | * from committing and releasing the memory that the pending chunks |
| 5711 | * list head uses. For that, we need to take a reference to the |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5712 | * transaction and hold the commit root sem. We only need to hold |
| 5713 | * it while performing the free space search since we have already |
| 5714 | * held back allocations. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5715 | */ |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5716 | static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5717 | { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5718 | u64 start = SZ_1M, len = 0, end = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5719 | int ret; |
| 5720 | |
| 5721 | *trimmed = 0; |
| 5722 | |
Jeff Mahoney | 0be88e3 | 2018-09-06 17:18:15 -0400 | [diff] [blame] | 5723 | /* Discard not supported = nothing to do. */ |
| 5724 | if (!blk_queue_discard(bdev_get_queue(device->bdev))) |
| 5725 | return 0; |
| 5726 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5727 | /* Not writable = nothing to do. */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5728 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5729 | return 0; |
| 5730 | |
| 5731 | /* No free space = nothing to do. */ |
| 5732 | if (device->total_bytes <= device->bytes_used) |
| 5733 | return 0; |
| 5734 | |
| 5735 | ret = 0; |
| 5736 | |
| 5737 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 5738 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5739 | u64 bytes; |
| 5740 | |
| 5741 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 5742 | if (ret) |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5743 | break; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5744 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5745 | find_first_clear_extent_bit(&device->alloc_state, start, |
| 5746 | &start, &end, |
| 5747 | CHUNK_TRIMMED | CHUNK_ALLOCATED); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5748 | |
Qu Wenruo | c57dd1f | 2020-07-31 19:29:11 +0800 | [diff] [blame] | 5749 | /* Check if there are any CHUNK_* bits left */ |
| 5750 | if (start > device->total_bytes) { |
| 5751 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); |
| 5752 | btrfs_warn_in_rcu(fs_info, |
| 5753 | "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu", |
| 5754 | start, end - start + 1, |
| 5755 | rcu_str_deref(device->name), |
| 5756 | device->total_bytes); |
| 5757 | mutex_unlock(&fs_info->chunk_mutex); |
| 5758 | ret = 0; |
| 5759 | break; |
| 5760 | } |
| 5761 | |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5762 | /* Ensure we skip the reserved area in the first 1M */ |
| 5763 | start = max_t(u64, start, SZ_1M); |
| 5764 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5765 | /* |
| 5766 | * If find_first_clear_extent_bit find a range that spans the |
| 5767 | * end of the device it will set end to -1, in this case it's up |
| 5768 | * to the caller to trim the value to the size of the device. |
| 5769 | */ |
| 5770 | end = min(end, device->total_bytes - 1); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5771 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5772 | len = end - start + 1; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5773 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5774 | /* We didn't find any extents */ |
| 5775 | if (!len) { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5776 | mutex_unlock(&fs_info->chunk_mutex); |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5777 | ret = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5778 | break; |
| 5779 | } |
| 5780 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5781 | ret = btrfs_issue_discard(device->bdev, start, len, |
| 5782 | &bytes); |
| 5783 | if (!ret) |
| 5784 | set_extent_bits(&device->alloc_state, start, |
| 5785 | start + bytes - 1, |
| 5786 | CHUNK_TRIMMED); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5787 | mutex_unlock(&fs_info->chunk_mutex); |
| 5788 | |
| 5789 | if (ret) |
| 5790 | break; |
| 5791 | |
| 5792 | start += len; |
| 5793 | *trimmed += bytes; |
| 5794 | |
| 5795 | if (fatal_signal_pending(current)) { |
| 5796 | ret = -ERESTARTSYS; |
| 5797 | break; |
| 5798 | } |
| 5799 | |
| 5800 | cond_resched(); |
| 5801 | } |
| 5802 | |
| 5803 | return ret; |
| 5804 | } |
| 5805 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5806 | /* |
| 5807 | * Trim the whole filesystem by: |
| 5808 | * 1) trimming the free space in each block group |
| 5809 | * 2) trimming the unallocated space on each device |
| 5810 | * |
| 5811 | * This will also continue trimming even if a block group or device encounters |
| 5812 | * an error. The return value will be the last error, or 0 if nothing bad |
| 5813 | * happens. |
| 5814 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5815 | int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5816 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 5817 | struct btrfs_block_group *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5818 | struct btrfs_device *device; |
| 5819 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5820 | u64 group_trimmed; |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5821 | u64 range_end = U64_MAX; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5822 | u64 start; |
| 5823 | u64 end; |
| 5824 | u64 trimmed = 0; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5825 | u64 bg_failed = 0; |
| 5826 | u64 dev_failed = 0; |
| 5827 | int bg_ret = 0; |
| 5828 | int dev_ret = 0; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5829 | int ret = 0; |
| 5830 | |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5831 | /* |
| 5832 | * Check range overflow if range->len is set. |
| 5833 | * The default range->len is U64_MAX. |
| 5834 | */ |
| 5835 | if (range->len != U64_MAX && |
| 5836 | check_add_overflow(range->start, range->len, &range_end)) |
| 5837 | return -EINVAL; |
| 5838 | |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 5839 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 5840 | for (; cache; cache = btrfs_next_block_group(cache)) { |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 5841 | if (cache->start >= range_end) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5842 | btrfs_put_block_group(cache); |
| 5843 | break; |
| 5844 | } |
| 5845 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 5846 | start = max(range->start, cache->start); |
| 5847 | end = min(range_end, cache->start + cache->length); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5848 | |
| 5849 | if (end - start >= range->minlen) { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 5850 | if (!btrfs_block_group_done(cache)) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5851 | ret = btrfs_cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5852 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5853 | bg_failed++; |
| 5854 | bg_ret = ret; |
| 5855 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5856 | } |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5857 | ret = btrfs_wait_block_group_cache_done(cache); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5858 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5859 | bg_failed++; |
| 5860 | bg_ret = ret; |
| 5861 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5862 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5863 | } |
| 5864 | ret = btrfs_trim_block_group(cache, |
| 5865 | &group_trimmed, |
| 5866 | start, |
| 5867 | end, |
| 5868 | range->minlen); |
| 5869 | |
| 5870 | trimmed += group_trimmed; |
| 5871 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5872 | bg_failed++; |
| 5873 | bg_ret = ret; |
| 5874 | continue; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5875 | } |
| 5876 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5879 | if (bg_failed) |
| 5880 | btrfs_warn(fs_info, |
| 5881 | "failed to trim %llu block group(s), last error %d", |
| 5882 | bg_failed, bg_ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5883 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | d4e329d | 2018-09-06 17:18:14 -0400 | [diff] [blame] | 5884 | devices = &fs_info->fs_devices->devices; |
| 5885 | list_for_each_entry(device, devices, dev_list) { |
Anand Jain | 755971d | 2021-07-04 19:14:39 +0800 | [diff] [blame] | 5886 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
| 5887 | continue; |
| 5888 | |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5889 | ret = btrfs_trim_free_extents(device, &group_trimmed); |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5890 | if (ret) { |
| 5891 | dev_failed++; |
| 5892 | dev_ret = ret; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5893 | break; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5894 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5895 | |
| 5896 | trimmed += group_trimmed; |
| 5897 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5898 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5899 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5900 | if (dev_failed) |
| 5901 | btrfs_warn(fs_info, |
| 5902 | "failed to trim %llu device(s), last error %d", |
| 5903 | dev_failed, dev_ret); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5904 | range->len = trimmed; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5905 | if (bg_ret) |
| 5906 | return bg_ret; |
| 5907 | return dev_ret; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5908 | } |