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 | |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 85 | static u64 generic_ref_to_space_flags(struct btrfs_ref *ref) |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 86 | { |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 87 | if (ref->type == BTRFS_REF_METADATA) { |
| 88 | if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 89 | return BTRFS_BLOCK_GROUP_SYSTEM; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 90 | else |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 91 | return BTRFS_BLOCK_GROUP_METADATA; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 92 | } |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 93 | return BTRFS_BLOCK_GROUP_DATA; |
| 94 | } |
| 95 | |
| 96 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 97 | struct btrfs_ref *ref) |
| 98 | { |
| 99 | struct btrfs_space_info *space_info; |
| 100 | u64 flags = generic_ref_to_space_flags(ref); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 101 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 102 | space_info = btrfs_find_space_info(fs_info, flags); |
Omar Sandoval | 55e8196 | 2017-06-06 16:45:27 -0700 | [diff] [blame] | 103 | ASSERT(space_info); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 104 | percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len, |
| 105 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 106 | } |
| 107 | |
| 108 | static void sub_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 109 | struct btrfs_ref *ref) |
| 110 | { |
| 111 | struct btrfs_space_info *space_info; |
| 112 | u64 flags = generic_ref_to_space_flags(ref); |
| 113 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 114 | space_info = btrfs_find_space_info(fs_info, flags); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 115 | ASSERT(space_info); |
| 116 | percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len, |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 117 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 120 | /* 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] | 121 | 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] | 122 | { |
| 123 | int ret; |
| 124 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 125 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 126 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 127 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 128 | if (!path) |
| 129 | return -ENOMEM; |
| 130 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 131 | key.objectid = start; |
| 132 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 133 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 134 | 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] | 135 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 136 | return ret; |
| 137 | } |
| 138 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 139 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 140 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 141 | * |
| 142 | * the head node for delayed ref is used to store the sum of all the |
| 143 | * reference count modifications queued up in the rbtree. the head |
| 144 | * node may also store the extent flags to set. This way you can check |
| 145 | * to see what the reference count and extent flags would be if all of |
| 146 | * the delayed refs are not processed. |
| 147 | */ |
| 148 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 149 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 150 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 151 | { |
| 152 | struct btrfs_delayed_ref_head *head; |
| 153 | struct btrfs_delayed_ref_root *delayed_refs; |
| 154 | struct btrfs_path *path; |
| 155 | struct btrfs_extent_item *ei; |
| 156 | struct extent_buffer *leaf; |
| 157 | struct btrfs_key key; |
| 158 | u32 item_size; |
| 159 | u64 num_refs; |
| 160 | u64 extent_flags; |
| 161 | int ret; |
| 162 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 163 | /* |
| 164 | * If we don't have skinny metadata, don't bother doing anything |
| 165 | * different |
| 166 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 167 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 168 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 169 | metadata = 0; |
| 170 | } |
| 171 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 172 | path = btrfs_alloc_path(); |
| 173 | if (!path) |
| 174 | return -ENOMEM; |
| 175 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 176 | if (!trans) { |
| 177 | path->skip_locking = 1; |
| 178 | path->search_commit_root = 1; |
| 179 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 180 | |
| 181 | search_again: |
| 182 | key.objectid = bytenr; |
| 183 | key.offset = offset; |
| 184 | if (metadata) |
| 185 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 186 | else |
| 187 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 188 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 189 | 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] | 190 | if (ret < 0) |
| 191 | goto out_free; |
| 192 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 193 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 194 | if (path->slots[0]) { |
| 195 | path->slots[0]--; |
| 196 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 197 | path->slots[0]); |
| 198 | if (key.objectid == bytenr && |
| 199 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 200 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 201 | ret = 0; |
| 202 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 205 | if (ret == 0) { |
| 206 | leaf = path->nodes[0]; |
| 207 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 208 | if (item_size >= sizeof(*ei)) { |
| 209 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 210 | struct btrfs_extent_item); |
| 211 | num_refs = btrfs_extent_refs(leaf, ei); |
| 212 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 213 | } else { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 214 | ret = -EINVAL; |
| 215 | btrfs_print_v0_err(fs_info); |
| 216 | if (trans) |
| 217 | btrfs_abort_transaction(trans, ret); |
| 218 | else |
| 219 | btrfs_handle_fs_error(fs_info, ret, NULL); |
| 220 | |
| 221 | goto out_free; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 222 | } |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 223 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 224 | BUG_ON(num_refs == 0); |
| 225 | } else { |
| 226 | num_refs = 0; |
| 227 | extent_flags = 0; |
| 228 | ret = 0; |
| 229 | } |
| 230 | |
| 231 | if (!trans) |
| 232 | goto out; |
| 233 | |
| 234 | delayed_refs = &trans->transaction->delayed_refs; |
| 235 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 236 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 237 | if (head) { |
| 238 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 239 | refcount_inc(&head->refs); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 240 | spin_unlock(&delayed_refs->lock); |
| 241 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 242 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 243 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 244 | /* |
| 245 | * Mutex was contended, block until it's released and try |
| 246 | * again |
| 247 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 248 | mutex_lock(&head->mutex); |
| 249 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 250 | btrfs_put_delayed_ref_head(head); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 251 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 252 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 253 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 254 | if (head->extent_op && head->extent_op->update_flags) |
| 255 | extent_flags |= head->extent_op->flags_to_set; |
| 256 | else |
| 257 | BUG_ON(num_refs == 0); |
| 258 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 259 | num_refs += head->ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 260 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 261 | mutex_unlock(&head->mutex); |
| 262 | } |
| 263 | spin_unlock(&delayed_refs->lock); |
| 264 | out: |
| 265 | WARN_ON(num_refs == 0); |
| 266 | if (refs) |
| 267 | *refs = num_refs; |
| 268 | if (flags) |
| 269 | *flags = extent_flags; |
| 270 | out_free: |
| 271 | btrfs_free_path(path); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 276 | * Back reference rules. Back refs have three main goals: |
| 277 | * |
| 278 | * 1) differentiate between all holders of references to an extent so that |
| 279 | * when a reference is dropped we can make sure it was a valid reference |
| 280 | * before freeing the extent. |
| 281 | * |
| 282 | * 2) Provide enough information to quickly find the holders of an extent |
| 283 | * if we notice a given block is corrupted or bad. |
| 284 | * |
| 285 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 286 | * maintenance. This is actually the same as #2, but with a slightly |
| 287 | * different use case. |
| 288 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 289 | * There are two kinds of back refs. The implicit back refs is optimized |
| 290 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 291 | * back refs of this kind provide information about the block's owner tree |
| 292 | * and the pointer's key. These information allow us to find the block by |
| 293 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 294 | * referenced by their owner trees. The location of tree block is recorded |
| 295 | * in the back refs. Actually the full back refs is generic, and can be |
| 296 | * used in all cases the implicit back refs is used. The major shortcoming |
| 297 | * of the full back refs is its overhead. Every time a tree block gets |
| 298 | * COWed, we have to update back refs entry for all pointers in it. |
| 299 | * |
| 300 | * For a newly allocated tree block, we use implicit back refs for |
| 301 | * pointers in it. This means most tree related operations only involve |
| 302 | * implicit back refs. For a tree block created in old transaction, the |
| 303 | * only way to drop a reference to it is COW it. So we can detect the |
| 304 | * event that tree block loses its owner tree's reference and do the |
| 305 | * back refs conversion. |
| 306 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 307 | * 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] | 308 | * |
| 309 | * The reference count of the block is one and the tree is the block's |
| 310 | * owner tree. Nothing to do in this case. |
| 311 | * |
| 312 | * The reference count of the block is one and the tree is not the |
| 313 | * block's owner tree. In this case, full back refs is used for pointers |
| 314 | * in the block. Remove these full back refs, add implicit back refs for |
| 315 | * every pointers in the new block. |
| 316 | * |
| 317 | * The reference count of the block is greater than one and the tree is |
| 318 | * the block's owner tree. In this case, implicit back refs is used for |
| 319 | * pointers in the block. Add full back refs for every pointers in the |
| 320 | * block, increase lower level extents' reference counts. The original |
| 321 | * implicit back refs are entailed to the new block. |
| 322 | * |
| 323 | * The reference count of the block is greater than one and the tree is |
| 324 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 325 | * the new block, increase lower level extents' reference count. |
| 326 | * |
| 327 | * Back Reference Key composing: |
| 328 | * |
| 329 | * The key objectid corresponds to the first byte in the extent, |
| 330 | * The key type is used to differentiate between types of back refs. |
| 331 | * There are different meanings of the key offset for different types |
| 332 | * of back refs. |
| 333 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 334 | * File extents can be referenced by: |
| 335 | * |
| 336 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 337 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 338 | * - different offsets inside a file (bookend extents in file.c) |
| 339 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 340 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 341 | * |
| 342 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 343 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 344 | * - original offset in the file |
| 345 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 346 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 347 | * The key offset for the implicit back refs is hash of the first |
| 348 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 349 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 350 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 351 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 352 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 353 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 354 | * The key offset for the implicit back refs is the first byte of |
| 355 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 356 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 357 | * When a file extent is allocated, The implicit back refs is used. |
| 358 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 359 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 360 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 361 | * |
| 362 | * When a file extent is removed file truncation, we find the |
| 363 | * corresponding implicit back refs and check the following fields: |
| 364 | * |
| 365 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 366 | * |
| 367 | * Btree extents can be referenced by: |
| 368 | * |
| 369 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 370 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 371 | * Both the implicit back refs and the full back refs for tree blocks |
| 372 | * only consist of key. The key offset for the implicit back refs is |
| 373 | * objectid of block's owner tree. The key offset for the full back refs |
| 374 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 375 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 376 | * When implicit back refs is used, information about the lowest key and |
| 377 | * level of the tree block are required. These information are stored in |
| 378 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 379 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 380 | |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 381 | /* |
| 382 | * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 383 | * is_data == BTRFS_REF_TYPE_DATA, data type is requiried, |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 384 | * is_data == BTRFS_REF_TYPE_ANY, either type is OK. |
| 385 | */ |
| 386 | int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, |
| 387 | struct btrfs_extent_inline_ref *iref, |
| 388 | enum btrfs_inline_ref_type is_data) |
| 389 | { |
| 390 | int type = btrfs_extent_inline_ref_type(eb, iref); |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 391 | u64 offset = btrfs_extent_inline_ref_offset(eb, iref); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 392 | |
| 393 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 394 | type == BTRFS_SHARED_BLOCK_REF_KEY || |
| 395 | type == BTRFS_SHARED_DATA_REF_KEY || |
| 396 | type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 397 | if (is_data == BTRFS_REF_TYPE_BLOCK) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 398 | if (type == BTRFS_TREE_BLOCK_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 399 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 400 | if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 401 | ASSERT(eb->fs_info); |
| 402 | /* |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 403 | * Every shared one has parent tree block, |
| 404 | * which must be aligned to sector size. |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 405 | */ |
| 406 | if (offset && |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 407 | IS_ALIGNED(offset, eb->fs_info->sectorsize)) |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 408 | return type; |
| 409 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 410 | } else if (is_data == BTRFS_REF_TYPE_DATA) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 411 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 412 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 413 | if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 414 | ASSERT(eb->fs_info); |
| 415 | /* |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 416 | * Every shared one has parent tree block, |
| 417 | * which must be aligned to sector size. |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 418 | */ |
| 419 | if (offset && |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 420 | IS_ALIGNED(offset, eb->fs_info->sectorsize)) |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 421 | return type; |
| 422 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 423 | } else { |
| 424 | ASSERT(is_data == BTRFS_REF_TYPE_ANY); |
| 425 | return type; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | btrfs_print_leaf((struct extent_buffer *)eb); |
Qu Wenruo | ea57788 | 2020-08-26 17:26:43 +0800 | [diff] [blame] | 430 | btrfs_err(eb->fs_info, |
| 431 | "eb %llu iref 0x%lx invalid extent inline ref type %d", |
| 432 | eb->start, (unsigned long)iref, type); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 433 | WARN_ON(1); |
| 434 | |
| 435 | return BTRFS_REF_TYPE_INVALID; |
| 436 | } |
| 437 | |
Qu Wenruo | 0785a9a | 2019-08-09 09:24:24 +0800 | [diff] [blame] | 438 | u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 439 | { |
| 440 | u32 high_crc = ~(u32)0; |
| 441 | u32 low_crc = ~(u32)0; |
| 442 | __le64 lenum; |
| 443 | |
| 444 | lenum = cpu_to_le64(root_objectid); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 445 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 446 | lenum = cpu_to_le64(owner); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 447 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 448 | lenum = cpu_to_le64(offset); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 449 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 450 | |
| 451 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 452 | } |
| 453 | |
| 454 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 455 | struct btrfs_extent_data_ref *ref) |
| 456 | { |
| 457 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 458 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 459 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 460 | } |
| 461 | |
| 462 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 463 | struct btrfs_extent_data_ref *ref, |
| 464 | u64 root_objectid, u64 owner, u64 offset) |
| 465 | { |
| 466 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 467 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 468 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 469 | return 0; |
| 470 | return 1; |
| 471 | } |
| 472 | |
| 473 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 474 | struct btrfs_path *path, |
| 475 | u64 bytenr, u64 parent, |
| 476 | u64 root_objectid, |
| 477 | u64 owner, u64 offset) |
| 478 | { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 479 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 480 | struct btrfs_key key; |
| 481 | struct btrfs_extent_data_ref *ref; |
| 482 | struct extent_buffer *leaf; |
| 483 | u32 nritems; |
| 484 | int ret; |
| 485 | int recow; |
| 486 | int err = -ENOENT; |
| 487 | |
| 488 | key.objectid = bytenr; |
| 489 | if (parent) { |
| 490 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 491 | key.offset = parent; |
| 492 | } else { |
| 493 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 494 | key.offset = hash_extent_data_ref(root_objectid, |
| 495 | owner, offset); |
| 496 | } |
| 497 | again: |
| 498 | recow = 0; |
| 499 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 500 | if (ret < 0) { |
| 501 | err = ret; |
| 502 | goto fail; |
| 503 | } |
| 504 | |
| 505 | if (parent) { |
| 506 | if (!ret) |
| 507 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 508 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 512 | nritems = btrfs_header_nritems(leaf); |
| 513 | while (1) { |
| 514 | if (path->slots[0] >= nritems) { |
| 515 | ret = btrfs_next_leaf(root, path); |
| 516 | if (ret < 0) |
| 517 | err = ret; |
| 518 | if (ret) |
| 519 | goto fail; |
| 520 | |
| 521 | leaf = path->nodes[0]; |
| 522 | nritems = btrfs_header_nritems(leaf); |
| 523 | recow = 1; |
| 524 | } |
| 525 | |
| 526 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 527 | if (key.objectid != bytenr || |
| 528 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 529 | goto fail; |
| 530 | |
| 531 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 532 | struct btrfs_extent_data_ref); |
| 533 | |
| 534 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 535 | owner, offset)) { |
| 536 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 537 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 538 | goto again; |
| 539 | } |
| 540 | err = 0; |
| 541 | break; |
| 542 | } |
| 543 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 544 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 545 | fail: |
| 546 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 549 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 550 | struct btrfs_path *path, |
| 551 | u64 bytenr, u64 parent, |
| 552 | u64 root_objectid, u64 owner, |
| 553 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 554 | { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 555 | struct btrfs_root *root = trans->fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 556 | struct btrfs_key key; |
| 557 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 558 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 559 | u32 num_refs; |
| 560 | int ret; |
| 561 | |
| 562 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 563 | if (parent) { |
| 564 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 565 | key.offset = parent; |
| 566 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 567 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 568 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 569 | key.offset = hash_extent_data_ref(root_objectid, |
| 570 | owner, offset); |
| 571 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 572 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 573 | |
| 574 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 575 | if (ret && ret != -EEXIST) |
| 576 | goto fail; |
| 577 | |
| 578 | leaf = path->nodes[0]; |
| 579 | if (parent) { |
| 580 | struct btrfs_shared_data_ref *ref; |
| 581 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 582 | struct btrfs_shared_data_ref); |
| 583 | if (ret == 0) { |
| 584 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 585 | } else { |
| 586 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 587 | num_refs += refs_to_add; |
| 588 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 589 | } |
| 590 | } else { |
| 591 | struct btrfs_extent_data_ref *ref; |
| 592 | while (ret == -EEXIST) { |
| 593 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 594 | struct btrfs_extent_data_ref); |
| 595 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 596 | owner, offset)) |
| 597 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 598 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 599 | key.offset++; |
| 600 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 601 | size); |
| 602 | if (ret && ret != -EEXIST) |
| 603 | goto fail; |
| 604 | |
| 605 | leaf = path->nodes[0]; |
| 606 | } |
| 607 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 608 | struct btrfs_extent_data_ref); |
| 609 | if (ret == 0) { |
| 610 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 611 | root_objectid); |
| 612 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 613 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 614 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 615 | } else { |
| 616 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 617 | num_refs += refs_to_add; |
| 618 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 619 | } |
| 620 | } |
| 621 | btrfs_mark_buffer_dirty(leaf); |
| 622 | ret = 0; |
| 623 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 624 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 625 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 628 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 629 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 630 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 631 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 632 | struct btrfs_key key; |
| 633 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 634 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 635 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 636 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 637 | int ret = 0; |
| 638 | |
| 639 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 640 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 641 | |
| 642 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 643 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 644 | struct btrfs_extent_data_ref); |
| 645 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 646 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 647 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 648 | struct btrfs_shared_data_ref); |
| 649 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 650 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 651 | btrfs_print_v0_err(trans->fs_info); |
| 652 | btrfs_abort_transaction(trans, -EINVAL); |
| 653 | return -EINVAL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 654 | } else { |
| 655 | BUG(); |
| 656 | } |
| 657 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 658 | BUG_ON(num_refs < refs_to_drop); |
| 659 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 660 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 661 | if (num_refs == 0) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 662 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 663 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 664 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 665 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 666 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 667 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 668 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 669 | btrfs_mark_buffer_dirty(leaf); |
| 670 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 671 | return ret; |
| 672 | } |
| 673 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 674 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 675 | struct btrfs_extent_inline_ref *iref) |
| 676 | { |
| 677 | struct btrfs_key key; |
| 678 | struct extent_buffer *leaf; |
| 679 | struct btrfs_extent_data_ref *ref1; |
| 680 | struct btrfs_shared_data_ref *ref2; |
| 681 | u32 num_refs = 0; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 682 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 683 | |
| 684 | leaf = path->nodes[0]; |
| 685 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 686 | |
| 687 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 688 | if (iref) { |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 689 | /* |
| 690 | * If type is invalid, we should have bailed out earlier than |
| 691 | * this call. |
| 692 | */ |
| 693 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 694 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
| 695 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 696 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 697 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 698 | } else { |
| 699 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 700 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 701 | } |
| 702 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 703 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 704 | struct btrfs_extent_data_ref); |
| 705 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 706 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 707 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 708 | struct btrfs_shared_data_ref); |
| 709 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 710 | } else { |
| 711 | WARN_ON(1); |
| 712 | } |
| 713 | return num_refs; |
| 714 | } |
| 715 | |
| 716 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 717 | struct btrfs_path *path, |
| 718 | u64 bytenr, u64 parent, |
| 719 | u64 root_objectid) |
| 720 | { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 721 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 722 | struct btrfs_key key; |
| 723 | int ret; |
| 724 | |
| 725 | key.objectid = bytenr; |
| 726 | if (parent) { |
| 727 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 728 | key.offset = parent; |
| 729 | } else { |
| 730 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 731 | key.offset = root_objectid; |
| 732 | } |
| 733 | |
| 734 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 735 | if (ret > 0) |
| 736 | ret = -ENOENT; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 741 | struct btrfs_path *path, |
| 742 | u64 bytenr, u64 parent, |
| 743 | u64 root_objectid) |
| 744 | { |
| 745 | struct btrfs_key key; |
| 746 | int ret; |
| 747 | |
| 748 | key.objectid = bytenr; |
| 749 | if (parent) { |
| 750 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 751 | key.offset = parent; |
| 752 | } else { |
| 753 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 754 | key.offset = root_objectid; |
| 755 | } |
| 756 | |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 757 | ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 758 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 759 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 763 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 764 | { |
| 765 | int type; |
| 766 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 767 | if (parent > 0) |
| 768 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 769 | else |
| 770 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 771 | } else { |
| 772 | if (parent > 0) |
| 773 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 774 | else |
| 775 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 776 | } |
| 777 | return type; |
| 778 | } |
| 779 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 780 | static int find_next_key(struct btrfs_path *path, int level, |
| 781 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 782 | |
| 783 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 784 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 785 | if (!path->nodes[level]) |
| 786 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 787 | if (path->slots[level] + 1 >= |
| 788 | btrfs_header_nritems(path->nodes[level])) |
| 789 | continue; |
| 790 | if (level == 0) |
| 791 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 792 | path->slots[level] + 1); |
| 793 | else |
| 794 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 795 | path->slots[level] + 1); |
| 796 | return 0; |
| 797 | } |
| 798 | return 1; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 803 | * to the address of inline back ref, and 0 is returned. |
| 804 | * |
| 805 | * if back ref isn't found, *ref_ret is set to the address where it |
| 806 | * should be inserted, and -ENOENT is returned. |
| 807 | * |
| 808 | * if insert is true and there are too many inline back refs, the path |
| 809 | * points to the extent item, and -EAGAIN is returned. |
| 810 | * |
| 811 | * NOTE: inline back refs are ordered in the same way that back ref |
| 812 | * items in the tree are ordered. |
| 813 | */ |
| 814 | static noinline_for_stack |
| 815 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 816 | struct btrfs_path *path, |
| 817 | struct btrfs_extent_inline_ref **ref_ret, |
| 818 | u64 bytenr, u64 num_bytes, |
| 819 | u64 parent, u64 root_objectid, |
| 820 | u64 owner, u64 offset, int insert) |
| 821 | { |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 822 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 823 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 824 | struct btrfs_key key; |
| 825 | struct extent_buffer *leaf; |
| 826 | struct btrfs_extent_item *ei; |
| 827 | struct btrfs_extent_inline_ref *iref; |
| 828 | u64 flags; |
| 829 | u64 item_size; |
| 830 | unsigned long ptr; |
| 831 | unsigned long end; |
| 832 | int extra_size; |
| 833 | int type; |
| 834 | int want; |
| 835 | int ret; |
| 836 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 837 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 838 | int needed; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 839 | |
| 840 | key.objectid = bytenr; |
| 841 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 842 | key.offset = num_bytes; |
| 843 | |
| 844 | want = extent_ref_type(parent, owner); |
| 845 | if (insert) { |
| 846 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 847 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 848 | } else |
| 849 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 850 | |
| 851 | /* |
Nikolay Borisov | 16d1c06 | 2018-06-18 14:59:26 +0300 | [diff] [blame] | 852 | * Owner is our level, so we can just add one to get the level for the |
| 853 | * block we are interested in. |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 854 | */ |
| 855 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 856 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 857 | key.offset = owner; |
| 858 | } |
| 859 | |
| 860 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 861 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 862 | if (ret < 0) { |
| 863 | err = ret; |
| 864 | goto out; |
| 865 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 866 | |
| 867 | /* |
| 868 | * We may be a newly converted file system which still has the old fat |
| 869 | * extent entries for metadata, so try and see if we have one of those. |
| 870 | */ |
| 871 | if (ret > 0 && skinny_metadata) { |
| 872 | skinny_metadata = false; |
| 873 | if (path->slots[0]) { |
| 874 | path->slots[0]--; |
| 875 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 876 | path->slots[0]); |
| 877 | if (key.objectid == bytenr && |
| 878 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 879 | key.offset == num_bytes) |
| 880 | ret = 0; |
| 881 | } |
| 882 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 883 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 884 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 885 | key.offset = num_bytes; |
| 886 | btrfs_release_path(path); |
| 887 | goto again; |
| 888 | } |
| 889 | } |
| 890 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 891 | if (ret && !insert) { |
| 892 | err = -ENOENT; |
| 893 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 894 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 895 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 896 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 897 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 898 | |
| 899 | leaf = path->nodes[0]; |
| 900 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 901 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 902 | err = -EINVAL; |
| 903 | btrfs_print_v0_err(fs_info); |
| 904 | btrfs_abort_transaction(trans, err); |
| 905 | goto out; |
| 906 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 907 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 908 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 909 | flags = btrfs_extent_flags(leaf, ei); |
| 910 | |
| 911 | ptr = (unsigned long)(ei + 1); |
| 912 | end = (unsigned long)ei + item_size; |
| 913 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 914 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 915 | ptr += sizeof(struct btrfs_tree_block_info); |
| 916 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 917 | } |
| 918 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 919 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) |
| 920 | needed = BTRFS_REF_TYPE_DATA; |
| 921 | else |
| 922 | needed = BTRFS_REF_TYPE_BLOCK; |
| 923 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 924 | err = -ENOENT; |
| 925 | while (1) { |
| 926 | if (ptr >= end) { |
| 927 | WARN_ON(ptr > end); |
| 928 | break; |
| 929 | } |
| 930 | iref = (struct btrfs_extent_inline_ref *)ptr; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 931 | type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); |
| 932 | if (type == BTRFS_REF_TYPE_INVALID) { |
Su Yue | af431dc | 2018-06-22 16:18:01 +0800 | [diff] [blame] | 933 | err = -EUCLEAN; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 934 | goto out; |
| 935 | } |
| 936 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 937 | if (want < type) |
| 938 | break; |
| 939 | if (want > type) { |
| 940 | ptr += btrfs_extent_inline_ref_size(type); |
| 941 | continue; |
| 942 | } |
| 943 | |
| 944 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 945 | struct btrfs_extent_data_ref *dref; |
| 946 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 947 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 948 | owner, offset)) { |
| 949 | err = 0; |
| 950 | break; |
| 951 | } |
| 952 | if (hash_extent_data_ref_item(leaf, dref) < |
| 953 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 954 | break; |
| 955 | } else { |
| 956 | u64 ref_offset; |
| 957 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 958 | if (parent > 0) { |
| 959 | if (parent == ref_offset) { |
| 960 | err = 0; |
| 961 | break; |
| 962 | } |
| 963 | if (ref_offset < parent) |
| 964 | break; |
| 965 | } else { |
| 966 | if (root_objectid == ref_offset) { |
| 967 | err = 0; |
| 968 | break; |
| 969 | } |
| 970 | if (ref_offset < root_objectid) |
| 971 | break; |
| 972 | } |
| 973 | } |
| 974 | ptr += btrfs_extent_inline_ref_size(type); |
| 975 | } |
| 976 | if (err == -ENOENT && insert) { |
| 977 | if (item_size + extra_size >= |
| 978 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 979 | err = -EAGAIN; |
| 980 | goto out; |
| 981 | } |
| 982 | /* |
| 983 | * To add new inline back ref, we have to make sure |
| 984 | * there is no corresponding back ref item. |
| 985 | * For simplicity, we just do not add new inline back |
| 986 | * ref if there is any kind of item for this block |
| 987 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 988 | if (find_next_key(path, 0, &key) == 0 && |
| 989 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 990 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 991 | err = -EAGAIN; |
| 992 | goto out; |
| 993 | } |
| 994 | } |
| 995 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 996 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 997 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 998 | path->keep_locks = 0; |
| 999 | btrfs_unlock_up_safe(path, 1); |
| 1000 | } |
| 1001 | return err; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * helper to add new inline back ref |
| 1006 | */ |
| 1007 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1008 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1009 | struct btrfs_path *path, |
| 1010 | struct btrfs_extent_inline_ref *iref, |
| 1011 | u64 parent, u64 root_objectid, |
| 1012 | u64 owner, u64 offset, int refs_to_add, |
| 1013 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1014 | { |
| 1015 | struct extent_buffer *leaf; |
| 1016 | struct btrfs_extent_item *ei; |
| 1017 | unsigned long ptr; |
| 1018 | unsigned long end; |
| 1019 | unsigned long item_offset; |
| 1020 | u64 refs; |
| 1021 | int size; |
| 1022 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1023 | |
| 1024 | leaf = path->nodes[0]; |
| 1025 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1026 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1027 | |
| 1028 | type = extent_ref_type(parent, owner); |
| 1029 | size = btrfs_extent_inline_ref_size(type); |
| 1030 | |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 1031 | btrfs_extend_item(path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1032 | |
| 1033 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1034 | refs = btrfs_extent_refs(leaf, ei); |
| 1035 | refs += refs_to_add; |
| 1036 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1037 | if (extent_op) |
| 1038 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1039 | |
| 1040 | ptr = (unsigned long)ei + item_offset; |
| 1041 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1042 | if (ptr < end - size) |
| 1043 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1044 | end - size - ptr); |
| 1045 | |
| 1046 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1047 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1048 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1049 | struct btrfs_extent_data_ref *dref; |
| 1050 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1051 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1052 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1053 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1054 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1055 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1056 | struct btrfs_shared_data_ref *sref; |
| 1057 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1058 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1059 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1060 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1061 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1062 | } else { |
| 1063 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1064 | } |
| 1065 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1069 | struct btrfs_path *path, |
| 1070 | struct btrfs_extent_inline_ref **ref_ret, |
| 1071 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1072 | u64 root_objectid, u64 owner, u64 offset) |
| 1073 | { |
| 1074 | int ret; |
| 1075 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1076 | ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr, |
| 1077 | num_bytes, parent, root_objectid, |
| 1078 | owner, offset, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1079 | if (ret != -ENOENT) |
| 1080 | return ret; |
| 1081 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1082 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1083 | *ref_ret = NULL; |
| 1084 | |
| 1085 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 1086 | ret = lookup_tree_block_ref(trans, path, bytenr, parent, |
| 1087 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1088 | } else { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 1089 | ret = lookup_extent_data_ref(trans, path, bytenr, parent, |
| 1090 | root_objectid, owner, offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1091 | } |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * helper to update/remove inline back ref |
| 1097 | */ |
| 1098 | static noinline_for_stack |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1099 | void update_inline_extent_backref(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1100 | struct btrfs_extent_inline_ref *iref, |
| 1101 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1102 | struct btrfs_delayed_extent_op *extent_op, |
| 1103 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1104 | { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1105 | struct extent_buffer *leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1106 | struct btrfs_extent_item *ei; |
| 1107 | struct btrfs_extent_data_ref *dref = NULL; |
| 1108 | struct btrfs_shared_data_ref *sref = NULL; |
| 1109 | unsigned long ptr; |
| 1110 | unsigned long end; |
| 1111 | u32 item_size; |
| 1112 | int size; |
| 1113 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1114 | u64 refs; |
| 1115 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1116 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1117 | refs = btrfs_extent_refs(leaf, ei); |
| 1118 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1119 | refs += refs_to_mod; |
| 1120 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1121 | if (extent_op) |
| 1122 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1123 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1124 | /* |
| 1125 | * If type is invalid, we should have bailed out after |
| 1126 | * lookup_inline_extent_backref(). |
| 1127 | */ |
| 1128 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY); |
| 1129 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | |
| 1131 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1132 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1133 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1134 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1135 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1136 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1137 | } else { |
| 1138 | refs = 1; |
| 1139 | BUG_ON(refs_to_mod != -1); |
| 1140 | } |
| 1141 | |
| 1142 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1143 | refs += refs_to_mod; |
| 1144 | |
| 1145 | if (refs > 0) { |
| 1146 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1147 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1148 | else |
| 1149 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1150 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1151 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1152 | size = btrfs_extent_inline_ref_size(type); |
| 1153 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1154 | ptr = (unsigned long)iref; |
| 1155 | end = (unsigned long)ei + item_size; |
| 1156 | if (ptr + size < end) |
| 1157 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1158 | end - ptr - size); |
| 1159 | item_size -= size; |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 1160 | btrfs_truncate_item(path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1161 | } |
| 1162 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static noinline_for_stack |
| 1166 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1167 | struct btrfs_path *path, |
| 1168 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1169 | u64 root_objectid, u64 owner, |
| 1170 | u64 offset, int refs_to_add, |
| 1171 | struct btrfs_delayed_extent_op *extent_op) |
| 1172 | { |
| 1173 | struct btrfs_extent_inline_ref *iref; |
| 1174 | int ret; |
| 1175 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1176 | ret = lookup_inline_extent_backref(trans, path, &iref, bytenr, |
| 1177 | num_bytes, parent, root_objectid, |
| 1178 | owner, offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1179 | if (ret == 0) { |
Qu Wenruo | 07cce5c | 2020-08-19 14:35:49 +0800 | [diff] [blame] | 1180 | /* |
| 1181 | * We're adding refs to a tree block we already own, this |
| 1182 | * should not happen at all. |
| 1183 | */ |
| 1184 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1185 | btrfs_crit(trans->fs_info, |
| 1186 | "adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu", |
| 1187 | bytenr, num_bytes, root_objectid); |
| 1188 | if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) { |
| 1189 | WARN_ON(1); |
| 1190 | btrfs_crit(trans->fs_info, |
| 1191 | "path->slots[0]=%d path->nodes[0]:", path->slots[0]); |
| 1192 | btrfs_print_leaf(path->nodes[0]); |
| 1193 | } |
| 1194 | return -EUCLEAN; |
| 1195 | } |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1196 | update_inline_extent_backref(path, iref, refs_to_add, |
| 1197 | extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1198 | } else if (ret == -ENOENT) { |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1199 | setup_inline_extent_backref(trans->fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1200 | root_objectid, owner, offset, |
| 1201 | refs_to_add, extent_op); |
| 1202 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1203 | } |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1207 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1208 | struct btrfs_path *path, |
| 1209 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1210 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1211 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1212 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1213 | |
| 1214 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1215 | if (iref) { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1216 | update_inline_extent_backref(path, iref, -refs_to_drop, NULL, |
| 1217 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1218 | } else if (is_data) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 1219 | ret = remove_extent_data_ref(trans, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1220 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1221 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1222 | *last_ref = 1; |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 1223 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1224 | } |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1228 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1229 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1230 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1231 | int j, ret = 0; |
| 1232 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1233 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1234 | |
| 1235 | if (WARN_ON(start != aligned_start)) { |
| 1236 | len -= aligned_start - start; |
| 1237 | len = round_down(len, 1 << 9); |
| 1238 | start = aligned_start; |
| 1239 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1240 | |
| 1241 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1242 | |
| 1243 | if (!len) |
| 1244 | return 0; |
| 1245 | |
| 1246 | end = start + len; |
| 1247 | bytes_left = len; |
| 1248 | |
| 1249 | /* Skip any superblocks on this device. */ |
| 1250 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1251 | u64 sb_start = btrfs_sb_offset(j); |
| 1252 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1253 | u64 size = sb_start - start; |
| 1254 | |
| 1255 | if (!in_range(sb_start, start, bytes_left) && |
| 1256 | !in_range(sb_end, start, bytes_left) && |
| 1257 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1258 | continue; |
| 1259 | |
| 1260 | /* |
| 1261 | * Superblock spans beginning of range. Adjust start and |
| 1262 | * try again. |
| 1263 | */ |
| 1264 | if (sb_start <= start) { |
| 1265 | start += sb_end - start; |
| 1266 | if (start > end) { |
| 1267 | bytes_left = 0; |
| 1268 | break; |
| 1269 | } |
| 1270 | bytes_left = end - start; |
| 1271 | continue; |
| 1272 | } |
| 1273 | |
| 1274 | if (size) { |
| 1275 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1276 | GFP_NOFS, 0); |
| 1277 | if (!ret) |
| 1278 | *discarded_bytes += size; |
| 1279 | else if (ret != -EOPNOTSUPP) |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | start = sb_end; |
| 1284 | if (start > end) { |
| 1285 | bytes_left = 0; |
| 1286 | break; |
| 1287 | } |
| 1288 | bytes_left = end - start; |
| 1289 | } |
| 1290 | |
| 1291 | if (bytes_left) { |
| 1292 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1293 | GFP_NOFS, 0); |
| 1294 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1295 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1296 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1297 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1298 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1299 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1300 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 1301 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1302 | { |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1303 | int ret = 0; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1304 | u64 discarded_bytes = 0; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1305 | u64 end = bytenr + num_bytes; |
| 1306 | u64 cur = bytenr; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1307 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1308 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1309 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 1310 | /* |
| 1311 | * Avoid races with device replace and make sure our bbio has devices |
| 1312 | * associated to its stripes that don't go away while we are discarding. |
| 1313 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1314 | btrfs_bio_counter_inc_blocked(fs_info); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1315 | while (cur < end) { |
| 1316 | struct btrfs_bio_stripe *stripe; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1317 | int i; |
| 1318 | |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1319 | num_bytes = end - cur; |
| 1320 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1321 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur, |
| 1322 | &num_bytes, &bbio, 0); |
| 1323 | /* |
| 1324 | * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or |
| 1325 | * -EOPNOTSUPP. For any such error, @num_bytes is not updated, |
| 1326 | * thus we can't continue anyway. |
| 1327 | */ |
| 1328 | if (ret < 0) |
| 1329 | goto out; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1330 | |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1331 | stripe = bbio->stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1332 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1333 | u64 bytes; |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1334 | struct request_queue *req_q; |
| 1335 | |
Filipe Manana | 627e087 | 2018-01-30 18:40:22 +0000 | [diff] [blame] | 1336 | if (!stripe->dev->bdev) { |
| 1337 | ASSERT(btrfs_test_opt(fs_info, DEGRADED)); |
| 1338 | continue; |
| 1339 | } |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1340 | req_q = bdev_get_queue(stripe->dev->bdev); |
| 1341 | if (!blk_queue_discard(req_q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1342 | continue; |
| 1343 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1344 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1345 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1346 | stripe->length, |
| 1347 | &bytes); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1348 | if (!ret) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1349 | discarded_bytes += bytes; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1350 | } else if (ret != -EOPNOTSUPP) { |
| 1351 | /* |
| 1352 | * Logic errors or -ENOMEM, or -EIO, but |
| 1353 | * unlikely to happen. |
| 1354 | * |
| 1355 | * And since there are two loops, explicitly |
| 1356 | * go to out to avoid confusion. |
| 1357 | */ |
| 1358 | btrfs_put_bbio(bbio); |
| 1359 | goto out; |
| 1360 | } |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1361 | |
| 1362 | /* |
| 1363 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1364 | * just ignore the return value so we don't screw up |
| 1365 | * people calling discard_extent. |
| 1366 | */ |
| 1367 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1368 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1369 | btrfs_put_bbio(bbio); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1370 | cur += num_bytes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1371 | } |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 1372 | out: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1373 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1374 | |
| 1375 | if (actual_bytes) |
| 1376 | *actual_bytes = discarded_bytes; |
| 1377 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1378 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1379 | if (ret == -EOPNOTSUPP) |
| 1380 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1381 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1382 | } |
| 1383 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1384 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1385 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1386 | struct btrfs_ref *generic_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1387 | { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1388 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1389 | int old_ref_mod, new_ref_mod; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1390 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1391 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1392 | ASSERT(generic_ref->type != BTRFS_REF_NOT_SET && |
| 1393 | generic_ref->action); |
| 1394 | BUG_ON(generic_ref->type == BTRFS_REF_METADATA && |
| 1395 | generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1396 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1397 | if (generic_ref->type == BTRFS_REF_METADATA) |
| 1398 | ret = btrfs_add_delayed_tree_ref(trans, generic_ref, |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 1399 | NULL, &old_ref_mod, &new_ref_mod); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1400 | else |
| 1401 | ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1402 | &old_ref_mod, &new_ref_mod); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1403 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1404 | btrfs_ref_tree_mod(fs_info, generic_ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 1405 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 1406 | if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 1407 | sub_pinned_bytes(fs_info, generic_ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1408 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1409 | return ret; |
| 1410 | } |
| 1411 | |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1412 | /* |
| 1413 | * __btrfs_inc_extent_ref - insert backreference for a given extent |
| 1414 | * |
Qu Wenruo | 07cce5c | 2020-08-19 14:35:49 +0800 | [diff] [blame] | 1415 | * The counterpart is in __btrfs_free_extent(), with examples and more details |
| 1416 | * how it works. |
| 1417 | * |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1418 | * @trans: Handle of transaction |
| 1419 | * |
| 1420 | * @node: The delayed ref node used to get the bytenr/length for |
| 1421 | * extent whose references are incremented. |
| 1422 | * |
| 1423 | * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/ |
| 1424 | * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical |
| 1425 | * bytenr of the parent block. Since new extents are always |
| 1426 | * created with indirect references, this will only be the case |
| 1427 | * when relocating a shared extent. In that case, root_objectid |
| 1428 | * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must |
| 1429 | * be 0 |
| 1430 | * |
| 1431 | * @root_objectid: The id of the root where this modification has originated, |
| 1432 | * this can be either one of the well-known metadata trees or |
| 1433 | * the subvolume id which references this extent. |
| 1434 | * |
| 1435 | * @owner: For data extents it is the inode number of the owning file. |
| 1436 | * For metadata extents this parameter holds the level in the |
| 1437 | * tree of the extent. |
| 1438 | * |
| 1439 | * @offset: For metadata extents the offset is ignored and is currently |
| 1440 | * always passed as 0. For data extents it is the fileoffset |
| 1441 | * this extent belongs to. |
| 1442 | * |
| 1443 | * @refs_to_add Number of references to add |
| 1444 | * |
| 1445 | * @extent_op Pointer to a structure, holding information necessary when |
| 1446 | * updating a tree block's flags |
| 1447 | * |
| 1448 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1449 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1450 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1451 | u64 parent, u64 root_objectid, |
| 1452 | u64 owner, u64 offset, int refs_to_add, |
| 1453 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1454 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1455 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1456 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1457 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1458 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1459 | u64 bytenr = node->bytenr; |
| 1460 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1461 | u64 refs; |
| 1462 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1463 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1464 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1465 | if (!path) |
| 1466 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1467 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1468 | /* 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] | 1469 | ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, |
| 1470 | parent, root_objectid, owner, |
| 1471 | offset, refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 1472 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1473 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1474 | |
| 1475 | /* |
| 1476 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 1477 | * inline extent ref, so just update the reference count and add a |
| 1478 | * normal backref. |
| 1479 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1480 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1481 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1482 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1483 | refs = btrfs_extent_refs(leaf, item); |
| 1484 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1485 | if (extent_op) |
| 1486 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1487 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1488 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1489 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1490 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1491 | /* now insert the actual backref */ |
Nikolay Borisov | 65cd6d9 | 2020-03-04 11:33:53 +0200 | [diff] [blame] | 1492 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1493 | BUG_ON(refs_to_add != 1); |
| 1494 | ret = insert_tree_block_ref(trans, path, bytenr, parent, |
| 1495 | root_objectid); |
| 1496 | } else { |
| 1497 | ret = insert_extent_data_ref(trans, path, bytenr, parent, |
| 1498 | root_objectid, owner, offset, |
| 1499 | refs_to_add); |
| 1500 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1501 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1502 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1503 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1504 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 1505 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1506 | } |
| 1507 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1508 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1509 | struct btrfs_delayed_ref_node *node, |
| 1510 | struct btrfs_delayed_extent_op *extent_op, |
| 1511 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1512 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1513 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1514 | struct btrfs_delayed_data_ref *ref; |
| 1515 | struct btrfs_key ins; |
| 1516 | u64 parent = 0; |
| 1517 | u64 ref_root = 0; |
| 1518 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1519 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1520 | ins.objectid = node->bytenr; |
| 1521 | ins.offset = node->num_bytes; |
| 1522 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1523 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1524 | ref = btrfs_delayed_node_to_data_ref(node); |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1525 | trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1526 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1527 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1528 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1529 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1530 | |
| 1531 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1532 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1533 | flags |= extent_op->flags_to_set; |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 1534 | ret = alloc_reserved_file_extent(trans, parent, ref_root, |
| 1535 | flags, ref->objectid, |
| 1536 | ref->offset, &ins, |
| 1537 | node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1538 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1539 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1540 | ref->objectid, ref->offset, |
| 1541 | node->ref_mod, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1542 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1543 | ret = __btrfs_free_extent(trans, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1544 | ref_root, ref->objectid, |
| 1545 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1546 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1547 | } else { |
| 1548 | BUG(); |
| 1549 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1550 | return ret; |
| 1551 | } |
| 1552 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1553 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1554 | struct extent_buffer *leaf, |
| 1555 | struct btrfs_extent_item *ei) |
| 1556 | { |
| 1557 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1558 | if (extent_op->update_flags) { |
| 1559 | flags |= extent_op->flags_to_set; |
| 1560 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1561 | } |
| 1562 | |
| 1563 | if (extent_op->update_key) { |
| 1564 | struct btrfs_tree_block_info *bi; |
| 1565 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1566 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1567 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1572 | struct btrfs_delayed_ref_head *head, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1573 | struct btrfs_delayed_extent_op *extent_op) |
| 1574 | { |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1575 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1576 | struct btrfs_key key; |
| 1577 | struct btrfs_path *path; |
| 1578 | struct btrfs_extent_item *ei; |
| 1579 | struct extent_buffer *leaf; |
| 1580 | u32 item_size; |
| 1581 | int ret; |
| 1582 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1583 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1584 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 1585 | if (TRANS_ABORTED(trans)) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1586 | return 0; |
| 1587 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1588 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1589 | metadata = 0; |
| 1590 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1591 | path = btrfs_alloc_path(); |
| 1592 | if (!path) |
| 1593 | return -ENOMEM; |
| 1594 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1595 | key.objectid = head->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1596 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1597 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1598 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1599 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1600 | } else { |
| 1601 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1602 | key.offset = head->num_bytes; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1606 | 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] | 1607 | if (ret < 0) { |
| 1608 | err = ret; |
| 1609 | goto out; |
| 1610 | } |
| 1611 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1612 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1613 | if (path->slots[0] > 0) { |
| 1614 | path->slots[0]--; |
| 1615 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1616 | path->slots[0]); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1617 | if (key.objectid == head->bytenr && |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1618 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1619 | key.offset == head->num_bytes) |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1620 | ret = 0; |
| 1621 | } |
| 1622 | if (ret > 0) { |
| 1623 | btrfs_release_path(path); |
| 1624 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1625 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1626 | key.objectid = head->bytenr; |
| 1627 | key.offset = head->num_bytes; |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1628 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1629 | goto again; |
| 1630 | } |
| 1631 | } else { |
| 1632 | err = -EIO; |
| 1633 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1634 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | leaf = path->nodes[0]; |
| 1638 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1639 | |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 1640 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1641 | err = -EINVAL; |
| 1642 | btrfs_print_v0_err(fs_info); |
| 1643 | btrfs_abort_transaction(trans, err); |
| 1644 | goto out; |
| 1645 | } |
| 1646 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1647 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1648 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1649 | |
| 1650 | btrfs_mark_buffer_dirty(leaf); |
| 1651 | out: |
| 1652 | btrfs_free_path(path); |
| 1653 | return err; |
| 1654 | } |
| 1655 | |
| 1656 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1657 | struct btrfs_delayed_ref_node *node, |
| 1658 | struct btrfs_delayed_extent_op *extent_op, |
| 1659 | int insert_reserved) |
| 1660 | { |
| 1661 | int ret = 0; |
| 1662 | struct btrfs_delayed_tree_ref *ref; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1663 | u64 parent = 0; |
| 1664 | u64 ref_root = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1665 | |
| 1666 | ref = btrfs_delayed_node_to_tree_ref(node); |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1667 | trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1668 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1669 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1670 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1671 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1672 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1673 | if (node->ref_mod != 1) { |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1674 | btrfs_err(trans->fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1675 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 1676 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 1677 | parent); |
| 1678 | return -EIO; |
| 1679 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1680 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1681 | BUG_ON(!extent_op || !extent_op->update_flags); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 1682 | ret = alloc_reserved_tree_block(trans, node, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1683 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1684 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1685 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1686 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1687 | ret = __btrfs_free_extent(trans, node, parent, ref_root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1688 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1689 | } else { |
| 1690 | BUG(); |
| 1691 | } |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1695 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1696 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1697 | struct btrfs_delayed_ref_node *node, |
| 1698 | struct btrfs_delayed_extent_op *extent_op, |
| 1699 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1700 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1701 | int ret = 0; |
| 1702 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 1703 | if (TRANS_ABORTED(trans)) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1704 | if (insert_reserved) |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 1705 | btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1706 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1707 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1708 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1709 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1710 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1711 | ret = run_delayed_tree_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1712 | insert_reserved); |
| 1713 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1714 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1715 | ret = run_delayed_data_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1716 | insert_reserved); |
| 1717 | else |
| 1718 | BUG(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 1719 | if (ret && insert_reserved) |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 1720 | btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1721 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1722 | } |
| 1723 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1724 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1725 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1726 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1727 | struct btrfs_delayed_ref_node *ref; |
| 1728 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1729 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1730 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1731 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1732 | /* |
| 1733 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1734 | * This is to prevent a ref count from going down to zero, which deletes |
| 1735 | * the extent item from the extent tree, when there still are references |
| 1736 | * to add, which would fail because they would not find the extent item. |
| 1737 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1738 | if (!list_empty(&head->ref_add_list)) |
| 1739 | return list_first_entry(&head->ref_add_list, |
| 1740 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1741 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1742 | ref = rb_entry(rb_first_cached(&head->ref_tree), |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 1743 | struct btrfs_delayed_ref_node, ref_node); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1744 | ASSERT(list_empty(&ref->add_list)); |
| 1745 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1746 | } |
| 1747 | |
Josef Bacik | 2eadaa2 | 2017-09-29 15:43:52 -0400 | [diff] [blame] | 1748 | static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
| 1749 | struct btrfs_delayed_ref_head *head) |
| 1750 | { |
| 1751 | spin_lock(&delayed_refs->lock); |
| 1752 | head->processing = 0; |
| 1753 | delayed_refs->num_heads_ready++; |
| 1754 | spin_unlock(&delayed_refs->lock); |
| 1755 | btrfs_delayed_ref_unlock(head); |
| 1756 | } |
| 1757 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1758 | static struct btrfs_delayed_extent_op *cleanup_extent_op( |
| 1759 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1760 | { |
| 1761 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1762 | |
| 1763 | if (!extent_op) |
| 1764 | return NULL; |
| 1765 | |
| 1766 | if (head->must_insert_reserved) { |
| 1767 | head->extent_op = NULL; |
| 1768 | btrfs_free_delayed_extent_op(extent_op); |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | return extent_op; |
| 1772 | } |
| 1773 | |
| 1774 | static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, |
| 1775 | struct btrfs_delayed_ref_head *head) |
| 1776 | { |
| 1777 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1778 | int ret; |
| 1779 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1780 | extent_op = cleanup_extent_op(head); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1781 | if (!extent_op) |
| 1782 | return 0; |
| 1783 | head->extent_op = NULL; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1784 | spin_unlock(&head->lock); |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1785 | ret = run_delayed_extent_op(trans, head, extent_op); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1786 | btrfs_free_delayed_extent_op(extent_op); |
| 1787 | return ret ? ret : 1; |
| 1788 | } |
| 1789 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1790 | void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, |
| 1791 | struct btrfs_delayed_ref_root *delayed_refs, |
| 1792 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1793 | { |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1794 | int nr_items = 1; /* Dropping this ref head update. */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1795 | |
| 1796 | if (head->total_ref_mod < 0) { |
| 1797 | struct btrfs_space_info *space_info; |
| 1798 | u64 flags; |
| 1799 | |
| 1800 | if (head->is_data) |
| 1801 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 1802 | else if (head->is_system) |
| 1803 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 1804 | else |
| 1805 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 1806 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1807 | ASSERT(space_info); |
| 1808 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 1809 | -head->num_bytes, |
| 1810 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 1811 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1812 | /* |
| 1813 | * We had csum deletions accounted for in our delayed refs rsv, |
| 1814 | * we need to drop the csum leaves for this update from our |
| 1815 | * delayed_refs_rsv. |
| 1816 | */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1817 | if (head->is_data) { |
| 1818 | spin_lock(&delayed_refs->lock); |
| 1819 | delayed_refs->pending_csums -= head->num_bytes; |
| 1820 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1821 | nr_items += btrfs_csum_bytes_to_leaves(fs_info, |
| 1822 | head->num_bytes); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1826 | btrfs_delayed_refs_rsv_release(fs_info, nr_items); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1827 | } |
| 1828 | |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1829 | static int cleanup_ref_head(struct btrfs_trans_handle *trans, |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1830 | struct btrfs_delayed_ref_head *head) |
| 1831 | { |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 1832 | |
| 1833 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1834 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1835 | int ret; |
| 1836 | |
| 1837 | delayed_refs = &trans->transaction->delayed_refs; |
| 1838 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1839 | ret = run_and_cleanup_extent_op(trans, head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1840 | if (ret < 0) { |
| 1841 | unselect_delayed_ref_head(delayed_refs, head); |
| 1842 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
| 1843 | return ret; |
| 1844 | } else if (ret) { |
| 1845 | return ret; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Need to drop our head ref lock and re-acquire the delayed ref lock |
| 1850 | * and then re-check to make sure nobody got added. |
| 1851 | */ |
| 1852 | spin_unlock(&head->lock); |
| 1853 | spin_lock(&delayed_refs->lock); |
| 1854 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1855 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) { |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1856 | spin_unlock(&head->lock); |
| 1857 | spin_unlock(&delayed_refs->lock); |
| 1858 | return 1; |
| 1859 | } |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 1860 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1861 | spin_unlock(&head->lock); |
Nikolay Borisov | 1e7a142 | 2018-04-11 11:21:18 +0300 | [diff] [blame] | 1862 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1863 | |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1864 | if (head->must_insert_reserved) { |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 1865 | btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1866 | if (head->is_data) { |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 1867 | ret = btrfs_del_csums(trans, fs_info->csum_root, |
| 1868 | head->bytenr, head->num_bytes); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1872 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1873 | |
| 1874 | trace_run_delayed_ref_head(fs_info, head, 0); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1875 | btrfs_delayed_ref_unlock(head); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1876 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1877 | return 0; |
| 1878 | } |
| 1879 | |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1880 | static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( |
| 1881 | struct btrfs_trans_handle *trans) |
| 1882 | { |
| 1883 | struct btrfs_delayed_ref_root *delayed_refs = |
| 1884 | &trans->transaction->delayed_refs; |
| 1885 | struct btrfs_delayed_ref_head *head = NULL; |
| 1886 | int ret; |
| 1887 | |
| 1888 | spin_lock(&delayed_refs->lock); |
Lu Fengqi | 5637c74 | 2018-10-11 13:40:33 +0800 | [diff] [blame] | 1889 | head = btrfs_select_ref_head(delayed_refs); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1890 | if (!head) { |
| 1891 | spin_unlock(&delayed_refs->lock); |
| 1892 | return head; |
| 1893 | } |
| 1894 | |
| 1895 | /* |
| 1896 | * Grab the lock that says we are going to process all the refs for |
| 1897 | * this head |
| 1898 | */ |
Lu Fengqi | 9e920a6 | 2018-10-11 13:40:34 +0800 | [diff] [blame] | 1899 | ret = btrfs_delayed_ref_lock(delayed_refs, head); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1900 | spin_unlock(&delayed_refs->lock); |
| 1901 | |
| 1902 | /* |
| 1903 | * We may have dropped the spin lock to get the head mutex lock, and |
| 1904 | * that might have given someone else time to free the head. If that's |
| 1905 | * true, it has been removed from our list and we can move on. |
| 1906 | */ |
| 1907 | if (ret == -EAGAIN) |
| 1908 | head = ERR_PTR(-EAGAIN); |
| 1909 | |
| 1910 | return head; |
| 1911 | } |
| 1912 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1913 | static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, |
| 1914 | struct btrfs_delayed_ref_head *locked_ref, |
| 1915 | unsigned long *run_refs) |
| 1916 | { |
| 1917 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1918 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1919 | struct btrfs_delayed_extent_op *extent_op; |
| 1920 | struct btrfs_delayed_ref_node *ref; |
| 1921 | int must_insert_reserved = 0; |
| 1922 | int ret; |
| 1923 | |
| 1924 | delayed_refs = &trans->transaction->delayed_refs; |
| 1925 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1926 | lockdep_assert_held(&locked_ref->mutex); |
| 1927 | lockdep_assert_held(&locked_ref->lock); |
| 1928 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1929 | while ((ref = select_delayed_ref(locked_ref))) { |
| 1930 | if (ref->seq && |
| 1931 | btrfs_check_delayed_seq(fs_info, ref->seq)) { |
| 1932 | spin_unlock(&locked_ref->lock); |
| 1933 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1934 | return -EAGAIN; |
| 1935 | } |
| 1936 | |
| 1937 | (*run_refs)++; |
| 1938 | ref->in_tree = 0; |
| 1939 | rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree); |
| 1940 | RB_CLEAR_NODE(&ref->ref_node); |
| 1941 | if (!list_empty(&ref->add_list)) |
| 1942 | list_del(&ref->add_list); |
| 1943 | /* |
| 1944 | * When we play the delayed ref, also correct the ref_mod on |
| 1945 | * head |
| 1946 | */ |
| 1947 | switch (ref->action) { |
| 1948 | case BTRFS_ADD_DELAYED_REF: |
| 1949 | case BTRFS_ADD_DELAYED_EXTENT: |
| 1950 | locked_ref->ref_mod -= ref->ref_mod; |
| 1951 | break; |
| 1952 | case BTRFS_DROP_DELAYED_REF: |
| 1953 | locked_ref->ref_mod += ref->ref_mod; |
| 1954 | break; |
| 1955 | default: |
| 1956 | WARN_ON(1); |
| 1957 | } |
| 1958 | atomic_dec(&delayed_refs->num_entries); |
| 1959 | |
| 1960 | /* |
| 1961 | * Record the must_insert_reserved flag before we drop the |
| 1962 | * spin lock. |
| 1963 | */ |
| 1964 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1965 | locked_ref->must_insert_reserved = 0; |
| 1966 | |
| 1967 | extent_op = locked_ref->extent_op; |
| 1968 | locked_ref->extent_op = NULL; |
| 1969 | spin_unlock(&locked_ref->lock); |
| 1970 | |
| 1971 | ret = run_one_delayed_ref(trans, ref, extent_op, |
| 1972 | must_insert_reserved); |
| 1973 | |
| 1974 | btrfs_free_delayed_extent_op(extent_op); |
| 1975 | if (ret) { |
| 1976 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1977 | btrfs_put_delayed_ref(ref); |
| 1978 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 1979 | ret); |
| 1980 | return ret; |
| 1981 | } |
| 1982 | |
| 1983 | btrfs_put_delayed_ref(ref); |
| 1984 | cond_resched(); |
| 1985 | |
| 1986 | spin_lock(&locked_ref->lock); |
| 1987 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
| 1988 | } |
| 1989 | |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1993 | /* |
| 1994 | * Returns 0 on success or if called with an already aborted transaction. |
| 1995 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 1996 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1997 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1998 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1999 | { |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2000 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2001 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2002 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2003 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2004 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2005 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2006 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2007 | |
| 2008 | delayed_refs = &trans->transaction->delayed_refs; |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2009 | do { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2010 | if (!locked_ref) { |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 2011 | locked_ref = btrfs_obtain_ref_head(trans); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2012 | if (IS_ERR_OR_NULL(locked_ref)) { |
| 2013 | if (PTR_ERR(locked_ref) == -EAGAIN) { |
| 2014 | continue; |
| 2015 | } else { |
| 2016 | break; |
| 2017 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2018 | } |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2019 | count++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2020 | } |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2021 | /* |
| 2022 | * We need to try and merge add/drops of the same ref since we |
| 2023 | * can run into issues with relocate dropping the implicit ref |
| 2024 | * and then it being added back again before the drop can |
| 2025 | * finish. If we merged anything we need to re-loop so we can |
| 2026 | * get a good ref. |
| 2027 | * Or we can get node references of the same type that weren't |
| 2028 | * merged when created due to bumps in the tree mod seq, and |
| 2029 | * we need to merge them to prevent adding an inline extent |
| 2030 | * backref before dropping it (triggering a BUG_ON at |
| 2031 | * insert_inline_extent_backref()). |
| 2032 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2033 | spin_lock(&locked_ref->lock); |
Nikolay Borisov | be97f13 | 2018-04-19 11:06:39 +0300 | [diff] [blame] | 2034 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2035 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2036 | ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, |
| 2037 | &actual_count); |
| 2038 | if (ret < 0 && ret != -EAGAIN) { |
| 2039 | /* |
| 2040 | * Error, btrfs_run_delayed_refs_for_head already |
| 2041 | * unlocked everything so just bail out |
| 2042 | */ |
| 2043 | return ret; |
| 2044 | } else if (!ret) { |
| 2045 | /* |
| 2046 | * Success, perform the usual cleanup of a processed |
| 2047 | * head |
| 2048 | */ |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 2049 | ret = cleanup_ref_head(trans, locked_ref); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2050 | if (ret > 0 ) { |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2051 | /* We dropped our lock, we need to loop. */ |
| 2052 | ret = 0; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2053 | continue; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2054 | } else if (ret) { |
| 2055 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2056 | } |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2057 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2058 | |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2059 | /* |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2060 | * Either success case or btrfs_run_delayed_refs_for_head |
| 2061 | * returned -EAGAIN, meaning we need to select another head |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2062 | */ |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2063 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2064 | locked_ref = NULL; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2065 | cond_resched(); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2066 | } while ((nr != -1 && count < nr) || locked_ref); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2067 | |
| 2068 | /* |
| 2069 | * We don't want to include ref heads since we can have empty ref heads |
| 2070 | * and those will drastically skew our runtime down since we just do |
| 2071 | * accounting, no actual extent tree updates. |
| 2072 | */ |
| 2073 | if (actual_count > 0) { |
| 2074 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2075 | u64 avg; |
| 2076 | |
| 2077 | /* |
| 2078 | * We weigh the current average higher than our current runtime |
| 2079 | * to avoid large swings in the average. |
| 2080 | */ |
| 2081 | spin_lock(&delayed_refs->lock); |
| 2082 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2083 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2084 | spin_unlock(&delayed_refs->lock); |
| 2085 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2086 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2087 | } |
| 2088 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2089 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2090 | /* |
| 2091 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2092 | * correlates in most cases to the order added. To expose dependencies on this |
| 2093 | * order, we start to process the tree in the middle instead of the beginning |
| 2094 | */ |
| 2095 | static u64 find_middle(struct rb_root *root) |
| 2096 | { |
| 2097 | struct rb_node *n = root->rb_node; |
| 2098 | struct btrfs_delayed_ref_node *entry; |
| 2099 | int alt = 1; |
| 2100 | u64 middle; |
| 2101 | u64 first = 0, last = 0; |
| 2102 | |
| 2103 | n = rb_first(root); |
| 2104 | if (n) { |
| 2105 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2106 | first = entry->bytenr; |
| 2107 | } |
| 2108 | n = rb_last(root); |
| 2109 | if (n) { |
| 2110 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2111 | last = entry->bytenr; |
| 2112 | } |
| 2113 | n = root->rb_node; |
| 2114 | |
| 2115 | while (n) { |
| 2116 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2117 | WARN_ON(!entry->in_tree); |
| 2118 | |
| 2119 | middle = entry->bytenr; |
| 2120 | |
| 2121 | if (alt) |
| 2122 | n = n->rb_left; |
| 2123 | else |
| 2124 | n = n->rb_right; |
| 2125 | |
| 2126 | alt = 1 - alt; |
| 2127 | } |
| 2128 | return middle; |
| 2129 | } |
| 2130 | #endif |
| 2131 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2132 | /* |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2133 | * this starts processing the delayed reference count updates and |
| 2134 | * extent insertions we have queued up so far. count can be |
| 2135 | * 0, which means to process everything in the tree at the start |
| 2136 | * of the run (but not newly added entries), or it can be some target |
| 2137 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2138 | * |
| 2139 | * Returns 0 on success or if called with an aborted transaction |
| 2140 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2141 | */ |
| 2142 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2143 | unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2144 | { |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2145 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2146 | struct rb_node *node; |
| 2147 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2148 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2149 | int ret; |
| 2150 | int run_all = count == (unsigned long)-1; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2151 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2152 | /* We'll clean this up in btrfs_cleanup_transaction */ |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 2153 | if (TRANS_ABORTED(trans)) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2154 | return 0; |
| 2155 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2156 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2157 | return 0; |
| 2158 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2159 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2160 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2161 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2162 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2163 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2164 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2165 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2166 | #endif |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2167 | ret = __btrfs_run_delayed_refs(trans, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2168 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2169 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2170 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2171 | } |
| 2172 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2173 | if (run_all) { |
Josef Bacik | 119e80d | 2018-11-21 14:05:42 -0500 | [diff] [blame] | 2174 | btrfs_create_pending_block_groups(trans); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2175 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2176 | spin_lock(&delayed_refs->lock); |
Liu Bo | 5c9d028 | 2018-08-23 03:51:49 +0800 | [diff] [blame] | 2177 | node = rb_first_cached(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2178 | if (!node) { |
| 2179 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2180 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2181 | } |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2182 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2183 | href_node); |
| 2184 | refcount_inc(&head->refs); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2185 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2186 | |
| 2187 | /* Mutex was contended, block until it's released and retry. */ |
| 2188 | mutex_lock(&head->mutex); |
| 2189 | mutex_unlock(&head->mutex); |
| 2190 | |
| 2191 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2192 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2193 | goto again; |
| 2194 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2195 | out: |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2196 | return 0; |
| 2197 | } |
| 2198 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2199 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 2200 | struct extent_buffer *eb, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2201 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2202 | { |
| 2203 | struct btrfs_delayed_extent_op *extent_op; |
| 2204 | int ret; |
| 2205 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2206 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2207 | if (!extent_op) |
| 2208 | return -ENOMEM; |
| 2209 | |
| 2210 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 2211 | extent_op->update_flags = true; |
| 2212 | extent_op->update_key = false; |
| 2213 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2214 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2215 | |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 2216 | 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] | 2217 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2218 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2219 | return ret; |
| 2220 | } |
| 2221 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2222 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2223 | struct btrfs_path *path, |
| 2224 | u64 objectid, u64 offset, u64 bytenr) |
| 2225 | { |
| 2226 | struct btrfs_delayed_ref_head *head; |
| 2227 | struct btrfs_delayed_ref_node *ref; |
| 2228 | struct btrfs_delayed_data_ref *data_ref; |
| 2229 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2230 | struct btrfs_transaction *cur_trans; |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2231 | struct rb_node *node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2232 | int ret = 0; |
| 2233 | |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2234 | spin_lock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2235 | cur_trans = root->fs_info->running_transaction; |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2236 | if (cur_trans) |
| 2237 | refcount_inc(&cur_trans->use_count); |
| 2238 | spin_unlock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2239 | if (!cur_trans) |
| 2240 | return 0; |
| 2241 | |
| 2242 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2243 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 2244 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2245 | if (!head) { |
| 2246 | spin_unlock(&delayed_refs->lock); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2247 | btrfs_put_transaction(cur_trans); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2248 | return 0; |
| 2249 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2250 | |
| 2251 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2252 | refcount_inc(&head->refs); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2253 | spin_unlock(&delayed_refs->lock); |
| 2254 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2255 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2256 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2257 | /* |
| 2258 | * Mutex was contended, block until it's released and let |
| 2259 | * caller try again |
| 2260 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2261 | mutex_lock(&head->mutex); |
| 2262 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2263 | btrfs_put_delayed_ref_head(head); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2264 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2265 | return -EAGAIN; |
| 2266 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2267 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2268 | |
| 2269 | spin_lock(&head->lock); |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2270 | /* |
| 2271 | * XXX: We should replace this with a proper search function in the |
| 2272 | * future. |
| 2273 | */ |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2274 | for (node = rb_first_cached(&head->ref_tree); node; |
| 2275 | node = rb_next(node)) { |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2276 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2277 | /* If it's a shared ref we know a cross reference exists */ |
| 2278 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 2279 | ret = 1; |
| 2280 | break; |
| 2281 | } |
| 2282 | |
| 2283 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2284 | |
| 2285 | /* |
| 2286 | * If our ref doesn't match the one we're currently looking at |
| 2287 | * then we have a cross reference. |
| 2288 | */ |
| 2289 | if (data_ref->root != root->root_key.objectid || |
| 2290 | data_ref->objectid != objectid || |
| 2291 | data_ref->offset != offset) { |
| 2292 | ret = 1; |
| 2293 | break; |
| 2294 | } |
| 2295 | } |
| 2296 | spin_unlock(&head->lock); |
| 2297 | mutex_unlock(&head->mutex); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2298 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2299 | return ret; |
| 2300 | } |
| 2301 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2302 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2303 | struct btrfs_path *path, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2304 | u64 objectid, u64 offset, u64 bytenr, |
| 2305 | bool strict) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2306 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2307 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2308 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2309 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2310 | struct btrfs_extent_data_ref *ref; |
| 2311 | struct btrfs_extent_inline_ref *iref; |
| 2312 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2313 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2314 | u32 item_size; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2315 | int type; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2316 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2317 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2318 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2319 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2320 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2321 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2322 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2323 | if (ret < 0) |
| 2324 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2325 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2326 | |
| 2327 | ret = -ENOENT; |
| 2328 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2329 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2330 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2331 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2332 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2333 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2334 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2335 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2336 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2337 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2338 | ret = 1; |
| 2339 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2340 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2341 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2342 | /* 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] | 2343 | if (item_size != sizeof(*ei) + |
| 2344 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2345 | goto out; |
| 2346 | |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2347 | /* |
| 2348 | * If extent created before last snapshot => it's shared unless the |
| 2349 | * snapshot has been deleted. Use the heuristic if strict is false. |
| 2350 | */ |
| 2351 | if (!strict && |
| 2352 | (btrfs_extent_generation(leaf, ei) <= |
| 2353 | btrfs_root_last_snapshot(&root->root_item))) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2354 | goto out; |
| 2355 | |
| 2356 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2357 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2358 | /* If this extent has SHARED_DATA_REF then it's shared */ |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2359 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 2360 | if (type != BTRFS_EXTENT_DATA_REF_KEY) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2361 | goto out; |
| 2362 | |
| 2363 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2364 | if (btrfs_extent_refs(leaf, ei) != |
| 2365 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2366 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2367 | root->root_key.objectid || |
| 2368 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2369 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2370 | goto out; |
| 2371 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2372 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2373 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2374 | return ret; |
| 2375 | } |
| 2376 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2377 | 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] | 2378 | u64 bytenr, bool strict) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2379 | { |
| 2380 | struct btrfs_path *path; |
| 2381 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2382 | |
| 2383 | path = btrfs_alloc_path(); |
| 2384 | if (!path) |
Su Yue | 9132c4f | 2018-05-30 14:49:10 +0800 | [diff] [blame] | 2385 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2386 | |
| 2387 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2388 | ret = check_committed_ref(root, path, objectid, |
Boris Burkov | a84d5d4 | 2020-08-18 11:00:05 -0700 | [diff] [blame] | 2389 | offset, bytenr, strict); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2390 | if (ret && ret != -ENOENT) |
| 2391 | goto out; |
| 2392 | |
Misono Tomohiro | 380fd06 | 2018-08-30 10:59:16 +0900 | [diff] [blame] | 2393 | ret = check_delayed_ref(root, path, objectid, offset, bytenr); |
| 2394 | } while (ret == -EAGAIN); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2395 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2396 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2397 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2398 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2399 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2400 | return ret; |
| 2401 | } |
| 2402 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2403 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2404 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2405 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2406 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2407 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2408 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2409 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2410 | u64 num_bytes; |
| 2411 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2412 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2413 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2414 | struct btrfs_key key; |
| 2415 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2416 | struct btrfs_ref generic_ref = { 0 }; |
| 2417 | bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2418 | int i; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2419 | int action; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2420 | int level; |
| 2421 | int ret = 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2422 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2423 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 2424 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2425 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2426 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2427 | nritems = btrfs_header_nritems(buf); |
| 2428 | level = btrfs_header_level(buf); |
| 2429 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 2430 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2431 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2432 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2433 | if (full_backref) |
| 2434 | parent = buf->start; |
| 2435 | else |
| 2436 | parent = 0; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2437 | if (inc) |
| 2438 | action = BTRFS_ADD_DELAYED_REF; |
| 2439 | else |
| 2440 | action = BTRFS_DROP_DELAYED_REF; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2441 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2442 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2443 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2444 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 2445 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2446 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2447 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2448 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2449 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2450 | BTRFS_FILE_EXTENT_INLINE) |
| 2451 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2452 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2453 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2454 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2455 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2456 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2457 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2458 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2459 | num_bytes, parent); |
| 2460 | generic_ref.real_root = root->root_key.objectid; |
| 2461 | btrfs_init_data_ref(&generic_ref, ref_root, key.objectid, |
| 2462 | key.offset); |
| 2463 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2464 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2465 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2466 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2467 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2468 | if (ret) |
| 2469 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2470 | } else { |
| 2471 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2472 | num_bytes = fs_info->nodesize; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2473 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2474 | num_bytes, parent); |
| 2475 | generic_ref.real_root = root->root_key.objectid; |
| 2476 | btrfs_init_tree_ref(&generic_ref, level - 1, ref_root); |
| 2477 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2478 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2479 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2480 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2481 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2482 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2483 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2484 | } |
| 2485 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2486 | return 0; |
| 2487 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2488 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2489 | } |
| 2490 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2491 | 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] | 2492 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2493 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2494 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2495 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2496 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2497 | 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] | 2498 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2499 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2500 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2501 | } |
| 2502 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2503 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2504 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2505 | struct btrfs_block_group *block_group; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2506 | int readonly = 0; |
| 2507 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2508 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2509 | if (!block_group || block_group->ro) |
| 2510 | readonly = 1; |
| 2511 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2512 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2513 | return readonly; |
| 2514 | } |
| 2515 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 2516 | 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] | 2517 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2518 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2519 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2520 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2521 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2522 | if (data) |
| 2523 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2524 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2525 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2526 | else |
| 2527 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 2528 | |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 2529 | ret = btrfs_get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2530 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2531 | } |
| 2532 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2533 | 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] | 2534 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2535 | struct btrfs_block_group *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2536 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2537 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2538 | spin_lock(&fs_info->block_group_cache_lock); |
| 2539 | bytenr = fs_info->first_logical_byte; |
| 2540 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 2541 | |
| 2542 | if (bytenr < (u64)-1) |
| 2543 | return bytenr; |
| 2544 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2545 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2546 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2547 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2548 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2549 | bytenr = cache->start; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2550 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2551 | |
| 2552 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2553 | } |
| 2554 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 2555 | static int pin_down_extent(struct btrfs_trans_handle *trans, |
| 2556 | struct btrfs_block_group *cache, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2557 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2558 | { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2559 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 2560 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2561 | spin_lock(&cache->space_info->lock); |
| 2562 | spin_lock(&cache->lock); |
| 2563 | cache->pinned += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2564 | btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, |
| 2565 | num_bytes); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2566 | if (reserved) { |
| 2567 | cache->reserved -= num_bytes; |
| 2568 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2569 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2570 | spin_unlock(&cache->lock); |
| 2571 | spin_unlock(&cache->space_info->lock); |
| 2572 | |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 2573 | percpu_counter_add_batch(&cache->space_info->total_bytes_pinned, |
| 2574 | num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 2575 | set_extent_dirty(&trans->transaction->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2576 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2577 | return 0; |
| 2578 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2579 | |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 2580 | int btrfs_pin_extent(struct btrfs_trans_handle *trans, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2581 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2582 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2583 | struct btrfs_block_group *cache; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2584 | |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 2585 | cache = btrfs_lookup_block_group(trans->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2586 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2587 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 2588 | pin_down_extent(trans, cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2589 | |
| 2590 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2591 | return 0; |
| 2592 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2593 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2594 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2595 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2596 | */ |
Nikolay Borisov | 9fce570 | 2020-01-20 16:09:13 +0200 | [diff] [blame] | 2597 | int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2598 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2599 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2600 | struct btrfs_block_group *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2601 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2602 | |
Nikolay Borisov | f2fb729 | 2020-01-20 16:09:16 +0200 | [diff] [blame] | 2603 | btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes); |
| 2604 | |
Nikolay Borisov | 9fce570 | 2020-01-20 16:09:13 +0200 | [diff] [blame] | 2605 | cache = btrfs_lookup_block_group(trans->fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2606 | if (!cache) |
| 2607 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2608 | |
| 2609 | /* |
| 2610 | * pull in the free space cache (if any) so that our pin |
| 2611 | * removes the free space from the cache. We have load_only set |
| 2612 | * to one because the slow code to read in the free extents does check |
| 2613 | * the pinned extents. |
| 2614 | */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2615 | btrfs_cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2616 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 2617 | pin_down_extent(trans, cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2618 | |
| 2619 | /* 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] | 2620 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2621 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2622 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2623 | } |
| 2624 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2625 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 2626 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2627 | { |
| 2628 | int ret; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2629 | struct btrfs_block_group *block_group; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2630 | struct btrfs_caching_control *caching_ctl; |
| 2631 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2632 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2633 | if (!block_group) |
| 2634 | return -EINVAL; |
| 2635 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2636 | btrfs_cache_block_group(block_group, 0); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2637 | caching_ctl = btrfs_get_caching_control(block_group); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2638 | |
| 2639 | if (!caching_ctl) { |
| 2640 | /* Logic error */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2641 | BUG_ON(!btrfs_block_group_done(block_group)); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2642 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 2643 | } else { |
Josef Bacik | e747853 | 2020-10-23 09:58:10 -0400 | [diff] [blame^] | 2644 | /* |
| 2645 | * We must wait for v1 caching to finish, otherwise we may not |
| 2646 | * remove our space. |
| 2647 | */ |
| 2648 | btrfs_wait_space_cache_v1_finished(block_group, caching_ctl); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2649 | mutex_lock(&caching_ctl->mutex); |
| 2650 | |
| 2651 | if (start >= caching_ctl->progress) { |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2652 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2653 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2654 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 2655 | ret = btrfs_remove_free_space(block_group, |
| 2656 | start, num_bytes); |
| 2657 | } else { |
| 2658 | num_bytes = caching_ctl->progress - start; |
| 2659 | ret = btrfs_remove_free_space(block_group, |
| 2660 | start, num_bytes); |
| 2661 | if (ret) |
| 2662 | goto out_lock; |
| 2663 | |
| 2664 | num_bytes = (start + num_bytes) - |
| 2665 | caching_ctl->progress; |
| 2666 | start = caching_ctl->progress; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2667 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2668 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2669 | } |
| 2670 | out_lock: |
| 2671 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2672 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2673 | } |
| 2674 | btrfs_put_block_group(block_group); |
| 2675 | return ret; |
| 2676 | } |
| 2677 | |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2678 | int btrfs_exclude_logged_extents(struct extent_buffer *eb) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2679 | { |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2680 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2681 | struct btrfs_file_extent_item *item; |
| 2682 | struct btrfs_key key; |
| 2683 | int found_type; |
| 2684 | int i; |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2685 | int ret = 0; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2686 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2687 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2688 | return 0; |
| 2689 | |
| 2690 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 2691 | btrfs_item_key_to_cpu(eb, &key, i); |
| 2692 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2693 | continue; |
| 2694 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 2695 | found_type = btrfs_file_extent_type(eb, item); |
| 2696 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 2697 | continue; |
| 2698 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 2699 | continue; |
| 2700 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 2701 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2702 | ret = __exclude_logged_extent(fs_info, key.objectid, key.offset); |
| 2703 | if (ret) |
| 2704 | break; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2705 | } |
| 2706 | |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2707 | return ret; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2708 | } |
| 2709 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 2710 | static void |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2711 | btrfs_inc_block_group_reservations(struct btrfs_block_group *bg) |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 2712 | { |
| 2713 | atomic_inc(&bg->reservations); |
| 2714 | } |
| 2715 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2716 | /* |
| 2717 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 2718 | * what it should be based on the mount options. |
| 2719 | */ |
| 2720 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2721 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 2722 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2723 | { |
| 2724 | struct btrfs_free_cluster *ret = NULL; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2725 | |
| 2726 | *empty_cluster = 0; |
| 2727 | if (btrfs_mixed_space_info(space_info)) |
| 2728 | return ret; |
| 2729 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2730 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2731 | ret = &fs_info->meta_alloc_cluster; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2732 | if (btrfs_test_opt(fs_info, SSD)) |
| 2733 | *empty_cluster = SZ_2M; |
| 2734 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2735 | *empty_cluster = SZ_64K; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2736 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && |
| 2737 | btrfs_test_opt(fs_info, SSD_SPREAD)) { |
| 2738 | *empty_cluster = SZ_2M; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2739 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | return ret; |
| 2743 | } |
| 2744 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2745 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 2746 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 2747 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2748 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2749 | struct btrfs_block_group *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2750 | struct btrfs_space_info *space_info; |
| 2751 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2752 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2753 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2754 | u64 total_unpinned = 0; |
| 2755 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2756 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2757 | |
| 2758 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2759 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2760 | if (!cache || |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2761 | start >= cache->start + cache->length) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2762 | if (cache) |
| 2763 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2764 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2765 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2766 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2767 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2768 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2769 | cache->space_info, |
| 2770 | &empty_cluster); |
| 2771 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2772 | } |
| 2773 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2774 | len = cache->start + cache->length - start; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2775 | len = min(len, end + 1 - start); |
| 2776 | |
Josef Bacik | 2ca08c5 | 2020-10-23 09:58:06 -0400 | [diff] [blame] | 2777 | down_read(&fs_info->commit_root_sem); |
Josef Bacik | 9076dbd | 2020-10-23 09:58:04 -0400 | [diff] [blame] | 2778 | if (start < cache->last_byte_to_unpin && return_free_space) { |
| 2779 | u64 add_len = min(len, cache->last_byte_to_unpin - start); |
| 2780 | |
| 2781 | btrfs_add_free_space(cache, start, add_len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2782 | } |
Josef Bacik | 2ca08c5 | 2020-10-23 09:58:06 -0400 | [diff] [blame] | 2783 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2784 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2785 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2786 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2787 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2788 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2789 | /* |
| 2790 | * If this space cluster has been marked as fragmented and we've |
| 2791 | * unpinned enough in this block group to potentially allow a |
| 2792 | * cluster to be created inside of it go ahead and clear the |
| 2793 | * fragmented check. |
| 2794 | */ |
| 2795 | if (cluster && cluster->fragmented && |
| 2796 | total_unpinned > empty_cluster) { |
| 2797 | spin_lock(&cluster->lock); |
| 2798 | cluster->fragmented = 0; |
| 2799 | spin_unlock(&cluster->lock); |
| 2800 | } |
| 2801 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2802 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2803 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2804 | cache->pinned -= len; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2805 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 2806 | space_info->max_extent_size = 0; |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 2807 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 2808 | -len, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2809 | if (cache->ro) { |
| 2810 | space_info->bytes_readonly += len; |
| 2811 | readonly = true; |
| 2812 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2813 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2814 | if (!readonly && return_free_space && |
| 2815 | global_rsv->space_info == space_info) { |
| 2816 | u64 to_add = len; |
Nikolay Borisov | 92ac58e | 2017-08-17 10:52:28 +0300 | [diff] [blame] | 2817 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2818 | spin_lock(&global_rsv->lock); |
| 2819 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2820 | to_add = min(len, global_rsv->size - |
| 2821 | global_rsv->reserved); |
| 2822 | global_rsv->reserved += to_add; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2823 | btrfs_space_info_update_bytes_may_use(fs_info, |
| 2824 | space_info, to_add); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2825 | if (global_rsv->reserved >= global_rsv->size) |
| 2826 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2827 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2828 | } |
| 2829 | spin_unlock(&global_rsv->lock); |
| 2830 | } |
Josef Bacik | 2732798 | 2020-07-21 10:22:18 -0400 | [diff] [blame] | 2831 | /* Add to any tickets we may have */ |
| 2832 | if (!readonly && return_free_space && len) |
| 2833 | btrfs_try_granting_tickets(fs_info, space_info); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2834 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2838 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2839 | return 0; |
| 2840 | } |
| 2841 | |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2842 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2843 | { |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2844 | struct btrfs_fs_info *fs_info = trans->fs_info; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 2845 | struct btrfs_block_group *block_group, *tmp; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2846 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2847 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2848 | u64 start; |
| 2849 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2850 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2851 | |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 2852 | unpin = &trans->transaction->pinned_extents; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2853 | |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 2854 | while (!TRANS_ABORTED(trans)) { |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2855 | struct extent_state *cached_state = NULL; |
| 2856 | |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2857 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2858 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2859 | EXTENT_DIRTY, &cached_state); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2860 | if (ret) { |
| 2861 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2862 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2863 | } |
Nikolay Borisov | fe119a6 | 2020-01-20 16:09:18 +0200 | [diff] [blame] | 2864 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
| 2865 | clear_extent_bits(&fs_info->excluded_extents, start, |
Nikolay Borisov | f2fb729 | 2020-01-20 16:09:16 +0200 | [diff] [blame] | 2866 | end, EXTENT_UPTODATE); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2867 | |
Dennis Zhou | 46b27f5 | 2019-12-13 16:22:11 -0800 | [diff] [blame] | 2868 | if (btrfs_test_opt(fs_info, DISCARD_SYNC)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2869 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2870 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2871 | |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2872 | clear_extent_dirty(unpin, start, end, &cached_state); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2873 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2874 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2875 | free_extent_state(cached_state); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2876 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2877 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2878 | |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 2879 | if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) { |
| 2880 | btrfs_discard_calc_delay(&fs_info->discard_ctl); |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2881 | btrfs_discard_schedule_work(&fs_info->discard_ctl, true); |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 2882 | } |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2883 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2884 | /* |
| 2885 | * Transaction is finished. We don't need the lock anymore. We |
| 2886 | * do need to clean up the block groups in case of a transaction |
| 2887 | * abort. |
| 2888 | */ |
| 2889 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 2890 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 2891 | u64 trimmed = 0; |
| 2892 | |
| 2893 | ret = -EROFS; |
David Sterba | bf31f87 | 2020-02-05 17:34:34 +0100 | [diff] [blame] | 2894 | if (!TRANS_ABORTED(trans)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2895 | ret = btrfs_discard_extent(fs_info, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 2896 | block_group->start, |
| 2897 | block_group->length, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2898 | &trimmed); |
| 2899 | |
| 2900 | list_del_init(&block_group->bg_list); |
Filipe Manana | 6b7304a | 2020-05-08 11:01:47 +0100 | [diff] [blame] | 2901 | btrfs_unfreeze_block_group(block_group); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2902 | btrfs_put_block_group(block_group); |
| 2903 | |
| 2904 | if (ret) { |
| 2905 | const char *errstr = btrfs_decode_error(ret); |
| 2906 | btrfs_warn(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 2907 | "discard failed while removing blockgroup: errno=%d %s", |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2908 | ret, errstr); |
| 2909 | } |
| 2910 | } |
| 2911 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2912 | return 0; |
| 2913 | } |
| 2914 | |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 2915 | /* |
| 2916 | * Drop one or more refs of @node. |
| 2917 | * |
| 2918 | * 1. Locate the extent refs. |
| 2919 | * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item. |
| 2920 | * Locate it, then reduce the refs number or remove the ref line completely. |
| 2921 | * |
| 2922 | * 2. Update the refs count in EXTENT/METADATA_ITEM |
| 2923 | * |
| 2924 | * Inline backref case: |
| 2925 | * |
| 2926 | * in extent tree we have: |
| 2927 | * |
| 2928 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82 |
| 2929 | * refs 2 gen 6 flags DATA |
| 2930 | * extent data backref root FS_TREE objectid 258 offset 0 count 1 |
| 2931 | * extent data backref root FS_TREE objectid 257 offset 0 count 1 |
| 2932 | * |
| 2933 | * This function gets called with: |
| 2934 | * |
| 2935 | * node->bytenr = 13631488 |
| 2936 | * node->num_bytes = 1048576 |
| 2937 | * root_objectid = FS_TREE |
| 2938 | * owner_objectid = 257 |
| 2939 | * owner_offset = 0 |
| 2940 | * refs_to_drop = 1 |
| 2941 | * |
| 2942 | * Then we should get some like: |
| 2943 | * |
| 2944 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82 |
| 2945 | * refs 1 gen 6 flags DATA |
| 2946 | * extent data backref root FS_TREE objectid 258 offset 0 count 1 |
| 2947 | * |
| 2948 | * Keyed backref case: |
| 2949 | * |
| 2950 | * in extent tree we have: |
| 2951 | * |
| 2952 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24 |
| 2953 | * refs 754 gen 6 flags DATA |
| 2954 | * [...] |
| 2955 | * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28 |
| 2956 | * extent data backref root FS_TREE objectid 866 offset 0 count 1 |
| 2957 | * |
| 2958 | * This function get called with: |
| 2959 | * |
| 2960 | * node->bytenr = 13631488 |
| 2961 | * node->num_bytes = 1048576 |
| 2962 | * root_objectid = FS_TREE |
| 2963 | * owner_objectid = 866 |
| 2964 | * owner_offset = 0 |
| 2965 | * refs_to_drop = 1 |
| 2966 | * |
| 2967 | * Then we should get some like: |
| 2968 | * |
| 2969 | * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24 |
| 2970 | * refs 753 gen 6 flags DATA |
| 2971 | * |
| 2972 | * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed. |
| 2973 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2974 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2975 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 2976 | u64 root_objectid, u64 owner_objectid, |
| 2977 | u64 owner_offset, int refs_to_drop, |
| 2978 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2979 | { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2980 | struct btrfs_fs_info *info = trans->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2981 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2982 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2983 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2984 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2985 | struct btrfs_extent_item *ei; |
| 2986 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2987 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2988 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2989 | int extent_slot = 0; |
| 2990 | int found_extent = 0; |
| 2991 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2992 | u32 item_size; |
| 2993 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2994 | u64 bytenr = node->bytenr; |
| 2995 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2996 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2997 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2998 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2999 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3000 | if (!path) |
| 3001 | return -ENOMEM; |
| 3002 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3003 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3004 | |
| 3005 | if (!is_data && refs_to_drop != 1) { |
| 3006 | btrfs_crit(info, |
| 3007 | "invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u", |
| 3008 | node->bytenr, refs_to_drop); |
| 3009 | ret = -EINVAL; |
| 3010 | btrfs_abort_transaction(trans, ret); |
| 3011 | goto out; |
| 3012 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3013 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3014 | if (is_data) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 3015 | skinny_metadata = false; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3016 | |
Nikolay Borisov | fbe4801 | 2018-06-20 15:48:52 +0300 | [diff] [blame] | 3017 | ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes, |
| 3018 | parent, root_objectid, owner_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3019 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3020 | if (ret == 0) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3021 | /* |
| 3022 | * Either the inline backref or the SHARED_DATA_REF/ |
| 3023 | * SHARED_BLOCK_REF is found |
| 3024 | * |
| 3025 | * Here is a quick path to locate EXTENT/METADATA_ITEM. |
| 3026 | * It's possible the EXTENT/METADATA_ITEM is near current slot. |
| 3027 | */ |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3028 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3029 | while (extent_slot >= 0) { |
| 3030 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3031 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3032 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3033 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3034 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3035 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3036 | found_extent = 1; |
| 3037 | break; |
| 3038 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3039 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 3040 | key.offset == owner_objectid) { |
| 3041 | found_extent = 1; |
| 3042 | break; |
| 3043 | } |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3044 | |
| 3045 | /* Quick path didn't find the EXTEMT/METADATA_ITEM */ |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3046 | if (path->slots[0] - extent_slot > 5) |
| 3047 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3048 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3049 | } |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 3050 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3051 | if (!found_extent) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3052 | if (iref) { |
| 3053 | btrfs_crit(info, |
| 3054 | "invalid iref, no EXTENT/METADATA_ITEM found but has inline extent ref"); |
| 3055 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3056 | goto err_dump; |
| 3057 | } |
| 3058 | /* Must be SHARED_* item, remove the backref first */ |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3059 | ret = remove_extent_backref(trans, path, NULL, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 3060 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3061 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3062 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3063 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3064 | goto out; |
| 3065 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3066 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3067 | |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3068 | /* Slow path to locate EXTENT/METADATA_ITEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3069 | key.objectid = bytenr; |
| 3070 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3071 | key.offset = num_bytes; |
| 3072 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3073 | if (!is_data && skinny_metadata) { |
| 3074 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3075 | key.offset = owner_objectid; |
| 3076 | } |
| 3077 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3078 | ret = btrfs_search_slot(trans, extent_root, |
| 3079 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3080 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 3081 | /* |
| 3082 | * Couldn't find our skinny metadata item, |
| 3083 | * see if we have ye olde extent item. |
| 3084 | */ |
| 3085 | path->slots[0]--; |
| 3086 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3087 | path->slots[0]); |
| 3088 | if (key.objectid == bytenr && |
| 3089 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3090 | key.offset == num_bytes) |
| 3091 | ret = 0; |
| 3092 | } |
| 3093 | |
| 3094 | if (ret > 0 && skinny_metadata) { |
| 3095 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 3096 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3097 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3098 | key.offset = num_bytes; |
| 3099 | btrfs_release_path(path); |
| 3100 | ret = btrfs_search_slot(trans, extent_root, |
| 3101 | &key, path, -1, 1); |
| 3102 | } |
| 3103 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3104 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3105 | btrfs_err(info, |
| 3106 | "umm, got %d back from search, was looking for %llu", |
| 3107 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 3108 | if (ret > 0) |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3109 | btrfs_print_leaf(path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3110 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3111 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3112 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3113 | goto out; |
| 3114 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3115 | extent_slot = path->slots[0]; |
| 3116 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3117 | } else if (WARN_ON(ret == -ENOENT)) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3118 | btrfs_print_leaf(path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3119 | btrfs_err(info, |
| 3120 | "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] | 3121 | bytenr, parent, root_objectid, owner_objectid, |
| 3122 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3123 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 3124 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3125 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3126 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3127 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3128 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3129 | |
| 3130 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3131 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 3132 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 3133 | ret = -EINVAL; |
| 3134 | btrfs_print_v0_err(info); |
| 3135 | btrfs_abort_transaction(trans, ret); |
| 3136 | goto out; |
| 3137 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3138 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3139 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3140 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3141 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3142 | struct btrfs_tree_block_info *bi; |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3143 | if (item_size < sizeof(*ei) + sizeof(*bi)) { |
| 3144 | btrfs_crit(info, |
Pujin Shi | cad69d1 | 2020-10-14 11:24:19 +0800 | [diff] [blame] | 3145 | "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] | 3146 | key.objectid, key.type, key.offset, |
| 3147 | owner_objectid, item_size, |
| 3148 | sizeof(*ei) + sizeof(*bi)); |
| 3149 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3150 | goto err_dump; |
| 3151 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3152 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3153 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3154 | } |
| 3155 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3156 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3157 | if (refs < refs_to_drop) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3158 | btrfs_crit(info, |
| 3159 | "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] | 3160 | refs_to_drop, refs, bytenr); |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3161 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3162 | goto err_dump; |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3163 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3164 | refs -= refs_to_drop; |
| 3165 | |
| 3166 | if (refs > 0) { |
| 3167 | if (extent_op) |
| 3168 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3169 | /* |
| 3170 | * In the case of inline back ref, reference count will |
| 3171 | * be updated by remove_extent_backref |
| 3172 | */ |
| 3173 | if (iref) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3174 | if (!found_extent) { |
| 3175 | btrfs_crit(info, |
| 3176 | "invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found"); |
| 3177 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3178 | goto err_dump; |
| 3179 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3180 | } else { |
| 3181 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3182 | btrfs_mark_buffer_dirty(leaf); |
| 3183 | } |
| 3184 | if (found_extent) { |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3185 | ret = remove_extent_backref(trans, path, iref, |
| 3186 | refs_to_drop, is_data, |
| 3187 | &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3188 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3189 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3190 | goto out; |
| 3191 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3192 | } |
| 3193 | } else { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3194 | /* In this branch refs == 1 */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3195 | if (found_extent) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3196 | if (is_data && refs_to_drop != |
| 3197 | extent_data_ref_count(path, iref)) { |
| 3198 | btrfs_crit(info, |
| 3199 | "invalid refs_to_drop, current refs %u refs_to_drop %u", |
| 3200 | extent_data_ref_count(path, iref), |
| 3201 | refs_to_drop); |
| 3202 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3203 | goto err_dump; |
| 3204 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3205 | if (iref) { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3206 | if (path->slots[0] != extent_slot) { |
| 3207 | btrfs_crit(info, |
| 3208 | "invalid iref, extent item key (%llu %u %llu) doesn't have wanted iref", |
| 3209 | key.objectid, key.type, |
| 3210 | key.offset); |
| 3211 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3212 | goto err_dump; |
| 3213 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3214 | } else { |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3215 | /* |
| 3216 | * No inline ref, we must be at SHARED_* item, |
| 3217 | * And it's single ref, it must be: |
| 3218 | * | extent_slot ||extent_slot + 1| |
| 3219 | * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ] |
| 3220 | */ |
| 3221 | if (path->slots[0] != extent_slot + 1) { |
| 3222 | btrfs_crit(info, |
| 3223 | "invalid SHARED_* item, previous item is not EXTENT/METADATA_ITEM"); |
| 3224 | btrfs_abort_transaction(trans, -EUCLEAN); |
| 3225 | goto err_dump; |
| 3226 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3227 | path->slots[0] = extent_slot; |
| 3228 | num_to_del = 2; |
| 3229 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3230 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3231 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3232 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3233 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3234 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3235 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3236 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3237 | goto out; |
| 3238 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3239 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3240 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3241 | if (is_data) { |
Filipe Manana | 40e046a | 2019-12-05 16:58:30 +0000 | [diff] [blame] | 3242 | ret = btrfs_del_csums(trans, info->csum_root, bytenr, |
| 3243 | num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3244 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3245 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3246 | goto out; |
| 3247 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3248 | } |
| 3249 | |
Nikolay Borisov | e7355e5 | 2018-05-10 15:44:55 +0300 | [diff] [blame] | 3250 | ret = add_to_free_space_tree(trans, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3251 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3252 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3253 | goto out; |
| 3254 | } |
| 3255 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 3256 | ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3257 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3258 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3259 | goto out; |
| 3260 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3261 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3262 | btrfs_release_path(path); |
| 3263 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3264 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3265 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3266 | return ret; |
Qu Wenruo | 1c2a07f5 | 2020-08-19 14:35:48 +0800 | [diff] [blame] | 3267 | err_dump: |
| 3268 | /* |
| 3269 | * Leaf dump can take up a lot of log buffer, so we only do full leaf |
| 3270 | * dump for debug build. |
| 3271 | */ |
| 3272 | if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) { |
| 3273 | btrfs_crit(info, "path->slots[0]=%d extent_slot=%d", |
| 3274 | path->slots[0], extent_slot); |
| 3275 | btrfs_print_leaf(path->nodes[0]); |
| 3276 | } |
| 3277 | |
| 3278 | btrfs_free_path(path); |
| 3279 | return -EUCLEAN; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3283 | * 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] | 3284 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3285 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3286 | * removes it from the tree. |
| 3287 | */ |
| 3288 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3289 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3290 | { |
| 3291 | struct btrfs_delayed_ref_head *head; |
| 3292 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3293 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3294 | |
| 3295 | delayed_refs = &trans->transaction->delayed_refs; |
| 3296 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 3297 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3298 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3299 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3300 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3301 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 3302 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3303 | goto out; |
| 3304 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 3305 | if (cleanup_extent_op(head) != NULL) |
| 3306 | goto out; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3307 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3308 | /* |
| 3309 | * waiting for the lock here would deadlock. If someone else has it |
| 3310 | * locked they are already in the process of dropping it anyway |
| 3311 | */ |
| 3312 | if (!mutex_trylock(&head->mutex)) |
| 3313 | goto out; |
| 3314 | |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3315 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3316 | head->processing = 0; |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3317 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3318 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3319 | spin_unlock(&delayed_refs->lock); |
| 3320 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3321 | BUG_ON(head->extent_op); |
| 3322 | if (head->must_insert_reserved) |
| 3323 | ret = 1; |
| 3324 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 3325 | btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3326 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 3327 | btrfs_put_delayed_ref_head(head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3328 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3329 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3330 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3331 | |
| 3332 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3333 | spin_unlock(&delayed_refs->lock); |
| 3334 | return 0; |
| 3335 | } |
| 3336 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3337 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 3338 | struct btrfs_root *root, |
| 3339 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3340 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3341 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3342 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3343 | struct btrfs_ref generic_ref = { 0 }; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3344 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3345 | int ret; |
| 3346 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3347 | btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, |
| 3348 | buf->start, buf->len, parent); |
| 3349 | btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), |
| 3350 | root->root_key.objectid); |
| 3351 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3352 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3353 | int old_ref_mod, new_ref_mod; |
| 3354 | |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3355 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3356 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3357 | &old_ref_mod, &new_ref_mod); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3358 | BUG_ON(ret); /* -ENOMEM */ |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3359 | pin = old_ref_mod >= 0 && new_ref_mod < 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3360 | } |
| 3361 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3362 | if (last_ref && btrfs_header_generation(buf) == trans->transid) { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3363 | struct btrfs_block_group *cache; |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3364 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3365 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3366 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3367 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3368 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3369 | } |
| 3370 | |
Omar Sandoval | 4da8b76 | 2017-06-06 16:45:28 -0700 | [diff] [blame] | 3371 | pin = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3372 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3373 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3374 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 3375 | pin_down_extent(trans, cache, buf->start, buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3376 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3377 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 3381 | |
| 3382 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 3383 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3384 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 3385 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3386 | } |
| 3387 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3388 | if (pin) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 3389 | add_pinned_bytes(fs_info, &generic_ref); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3390 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3391 | if (last_ref) { |
| 3392 | /* |
| 3393 | * Deleting the buffer, clear the corrupt flag since it doesn't |
| 3394 | * matter anymore. |
| 3395 | */ |
| 3396 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
| 3397 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3398 | } |
| 3399 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3400 | /* Can return -ENOMEM */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3401 | 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] | 3402 | { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3403 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3404 | int old_ref_mod, new_ref_mod; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3405 | int ret; |
| 3406 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 3407 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3408 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3409 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3410 | /* |
| 3411 | * tree log blocks never actually go into the extent allocation |
| 3412 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3413 | */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3414 | if ((ref->type == BTRFS_REF_METADATA && |
| 3415 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3416 | (ref->type == BTRFS_REF_DATA && |
| 3417 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3418 | /* unlocks the pinned mutex */ |
Nikolay Borisov | b25c36f | 2020-01-20 16:09:09 +0200 | [diff] [blame] | 3419 | btrfs_pin_extent(trans, ref->bytenr, ref->len, 1); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3420 | old_ref_mod = new_ref_mod = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3421 | ret = 0; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3422 | } else if (ref->type == BTRFS_REF_METADATA) { |
| 3423 | ret = btrfs_add_delayed_tree_ref(trans, ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3424 | &old_ref_mod, &new_ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3425 | } else { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3426 | ret = btrfs_add_delayed_data_ref(trans, ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3427 | &old_ref_mod, &new_ref_mod); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3428 | } |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3429 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3430 | if (!((ref->type == BTRFS_REF_METADATA && |
| 3431 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3432 | (ref->type == BTRFS_REF_DATA && |
| 3433 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID))) |
| 3434 | btrfs_ref_tree_mod(fs_info, ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3435 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 3436 | if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 3437 | add_pinned_bytes(fs_info, ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3438 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3439 | return ret; |
| 3440 | } |
| 3441 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3442 | enum btrfs_loop_type { |
David Sterba | f262fa8 | 2019-06-18 20:00:08 +0200 | [diff] [blame] | 3443 | LOOP_CACHING_NOWAIT, |
| 3444 | LOOP_CACHING_WAIT, |
| 3445 | LOOP_ALLOC_CHUNK, |
| 3446 | LOOP_NO_EMPTY_SIZE, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3447 | }; |
| 3448 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3449 | static inline void |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3450 | btrfs_lock_block_group(struct btrfs_block_group *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3451 | int delalloc) |
| 3452 | { |
| 3453 | if (delalloc) |
| 3454 | down_read(&cache->data_rwsem); |
| 3455 | } |
| 3456 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3457 | static inline void btrfs_grab_block_group(struct btrfs_block_group *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3458 | int delalloc) |
| 3459 | { |
| 3460 | btrfs_get_block_group(cache); |
| 3461 | if (delalloc) |
| 3462 | down_read(&cache->data_rwsem); |
| 3463 | } |
| 3464 | |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3465 | static struct btrfs_block_group *btrfs_lock_cluster( |
| 3466 | struct btrfs_block_group *block_group, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3467 | struct btrfs_free_cluster *cluster, |
| 3468 | int delalloc) |
Jules Irenge | c142c6a | 2020-03-31 21:46:41 +0100 | [diff] [blame] | 3469 | __acquires(&cluster->refill_lock) |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3470 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3471 | struct btrfs_block_group *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3472 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3473 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3474 | while (1) { |
| 3475 | used_bg = cluster->block_group; |
| 3476 | if (!used_bg) |
| 3477 | return NULL; |
| 3478 | |
| 3479 | if (used_bg == block_group) |
| 3480 | return used_bg; |
| 3481 | |
| 3482 | btrfs_get_block_group(used_bg); |
| 3483 | |
| 3484 | if (!delalloc) |
| 3485 | return used_bg; |
| 3486 | |
| 3487 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 3488 | return used_bg; |
| 3489 | |
| 3490 | spin_unlock(&cluster->refill_lock); |
| 3491 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 3492 | /* We should only have one-level nested. */ |
| 3493 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3494 | |
| 3495 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3496 | if (used_bg == cluster->block_group) |
| 3497 | return used_bg; |
| 3498 | |
| 3499 | up_read(&used_bg->data_rwsem); |
| 3500 | btrfs_put_block_group(used_bg); |
| 3501 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static inline void |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3505 | btrfs_release_block_group(struct btrfs_block_group *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3506 | int delalloc) |
| 3507 | { |
| 3508 | if (delalloc) |
| 3509 | up_read(&cache->data_rwsem); |
| 3510 | btrfs_put_block_group(cache); |
| 3511 | } |
| 3512 | |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 3513 | enum btrfs_extent_allocation_policy { |
| 3514 | BTRFS_EXTENT_ALLOC_CLUSTERED, |
| 3515 | }; |
| 3516 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3517 | /* |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3518 | * Structure used internally for find_free_extent() function. Wraps needed |
| 3519 | * parameters. |
| 3520 | */ |
| 3521 | struct find_free_extent_ctl { |
| 3522 | /* Basic allocation info */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3523 | u64 num_bytes; |
| 3524 | u64 empty_size; |
| 3525 | u64 flags; |
| 3526 | int delalloc; |
| 3527 | |
| 3528 | /* Where to start the search inside the bg */ |
| 3529 | u64 search_start; |
| 3530 | |
| 3531 | /* For clustered allocation */ |
| 3532 | u64 empty_cluster; |
Naohiro Aota | c10859b | 2020-02-25 12:56:18 +0900 | [diff] [blame] | 3533 | struct btrfs_free_cluster *last_ptr; |
| 3534 | bool use_cluster; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3535 | |
| 3536 | bool have_caching_bg; |
| 3537 | bool orig_have_caching_bg; |
| 3538 | |
| 3539 | /* RAID index, converted from flags */ |
| 3540 | int index; |
| 3541 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3542 | /* |
| 3543 | * Current loop number, check find_free_extent_update_loop() for details |
| 3544 | */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3545 | int loop; |
| 3546 | |
| 3547 | /* |
| 3548 | * Whether we're refilling a cluster, if true we need to re-search |
| 3549 | * current block group but don't try to refill the cluster again. |
| 3550 | */ |
| 3551 | bool retry_clustered; |
| 3552 | |
| 3553 | /* |
| 3554 | * Whether we're updating free space cache, if true we need to re-search |
| 3555 | * current block group but don't try updating free space cache again. |
| 3556 | */ |
| 3557 | bool retry_unclustered; |
| 3558 | |
| 3559 | /* If current block group is cached */ |
| 3560 | int cached; |
| 3561 | |
| 3562 | /* Max contiguous hole found */ |
| 3563 | u64 max_extent_size; |
| 3564 | |
| 3565 | /* Total free space from free space cache, not always contiguous */ |
| 3566 | u64 total_free_space; |
| 3567 | |
| 3568 | /* Found result */ |
| 3569 | u64 found_offset; |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 3570 | |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 3571 | /* Hint where to start looking for an empty space */ |
| 3572 | u64 hint_byte; |
| 3573 | |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 3574 | /* Allocation policy */ |
| 3575 | enum btrfs_extent_allocation_policy policy; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3576 | }; |
| 3577 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3578 | |
| 3579 | /* |
| 3580 | * Helper function for find_free_extent(). |
| 3581 | * |
| 3582 | * Return -ENOENT to inform caller that we need fallback to unclustered mode. |
| 3583 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3584 | * Return >0 to inform caller that we find nothing |
| 3585 | * Return 0 means we have found a location and set ffe_ctl->found_offset. |
| 3586 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3587 | static int find_free_extent_clustered(struct btrfs_block_group *bg, |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3588 | struct find_free_extent_ctl *ffe_ctl, |
| 3589 | struct btrfs_block_group **cluster_bg_ret) |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3590 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3591 | struct btrfs_block_group *cluster_bg; |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3592 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3593 | u64 aligned_cluster; |
| 3594 | u64 offset; |
| 3595 | int ret; |
| 3596 | |
| 3597 | cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc); |
| 3598 | if (!cluster_bg) |
| 3599 | goto refill_cluster; |
| 3600 | if (cluster_bg != bg && (cluster_bg->ro || |
| 3601 | !block_group_bits(cluster_bg, ffe_ctl->flags))) |
| 3602 | goto release_cluster; |
| 3603 | |
| 3604 | offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr, |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3605 | ffe_ctl->num_bytes, cluster_bg->start, |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3606 | &ffe_ctl->max_extent_size); |
| 3607 | if (offset) { |
| 3608 | /* We have a block, we're done */ |
| 3609 | spin_unlock(&last_ptr->refill_lock); |
| 3610 | trace_btrfs_reserve_extent_cluster(cluster_bg, |
| 3611 | ffe_ctl->search_start, ffe_ctl->num_bytes); |
| 3612 | *cluster_bg_ret = cluster_bg; |
| 3613 | ffe_ctl->found_offset = offset; |
| 3614 | return 0; |
| 3615 | } |
| 3616 | WARN_ON(last_ptr->block_group != cluster_bg); |
| 3617 | |
| 3618 | release_cluster: |
| 3619 | /* |
| 3620 | * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so |
| 3621 | * lets just skip it and let the allocator find whatever block it can |
| 3622 | * find. If we reach this point, we will have tried the cluster |
| 3623 | * allocator plenty of times and not have found anything, so we are |
| 3624 | * likely way too fragmented for the clustering stuff to find anything. |
| 3625 | * |
| 3626 | * However, if the cluster is taken from the current block group, |
| 3627 | * release the cluster first, so that we stand a better chance of |
| 3628 | * succeeding in the unclustered allocation. |
| 3629 | */ |
| 3630 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) { |
| 3631 | spin_unlock(&last_ptr->refill_lock); |
| 3632 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3633 | return -ENOENT; |
| 3634 | } |
| 3635 | |
| 3636 | /* This cluster didn't work out, free it and start over */ |
| 3637 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3638 | |
| 3639 | if (cluster_bg != bg) |
| 3640 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3641 | |
| 3642 | refill_cluster: |
| 3643 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) { |
| 3644 | spin_unlock(&last_ptr->refill_lock); |
| 3645 | return -ENOENT; |
| 3646 | } |
| 3647 | |
| 3648 | aligned_cluster = max_t(u64, |
| 3649 | ffe_ctl->empty_cluster + ffe_ctl->empty_size, |
| 3650 | bg->full_stripe_len); |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3651 | ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start, |
| 3652 | ffe_ctl->num_bytes, aligned_cluster); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3653 | if (ret == 0) { |
| 3654 | /* Now pull our allocation out of this cluster */ |
| 3655 | offset = btrfs_alloc_from_cluster(bg, last_ptr, |
| 3656 | ffe_ctl->num_bytes, ffe_ctl->search_start, |
| 3657 | &ffe_ctl->max_extent_size); |
| 3658 | if (offset) { |
| 3659 | /* We found one, proceed */ |
| 3660 | spin_unlock(&last_ptr->refill_lock); |
| 3661 | trace_btrfs_reserve_extent_cluster(bg, |
| 3662 | ffe_ctl->search_start, |
| 3663 | ffe_ctl->num_bytes); |
| 3664 | ffe_ctl->found_offset = offset; |
| 3665 | return 0; |
| 3666 | } |
| 3667 | } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT && |
| 3668 | !ffe_ctl->retry_clustered) { |
| 3669 | spin_unlock(&last_ptr->refill_lock); |
| 3670 | |
| 3671 | ffe_ctl->retry_clustered = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3672 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3673 | ffe_ctl->empty_cluster + ffe_ctl->empty_size); |
| 3674 | return -EAGAIN; |
| 3675 | } |
| 3676 | /* |
| 3677 | * At this point we either didn't find a cluster or we weren't able to |
| 3678 | * allocate a block from our cluster. Free the cluster we've been |
| 3679 | * trying to use, and go to the next block group. |
| 3680 | */ |
| 3681 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3682 | spin_unlock(&last_ptr->refill_lock); |
| 3683 | return 1; |
| 3684 | } |
| 3685 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3686 | /* |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3687 | * Return >0 to inform caller that we find nothing |
| 3688 | * Return 0 when we found an free extent and set ffe_ctrl->found_offset |
| 3689 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3690 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3691 | static int find_free_extent_unclustered(struct btrfs_block_group *bg, |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3692 | struct find_free_extent_ctl *ffe_ctl) |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3693 | { |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3694 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3695 | u64 offset; |
| 3696 | |
| 3697 | /* |
| 3698 | * We are doing an unclustered allocation, set the fragmented flag so |
| 3699 | * we don't bother trying to setup a cluster again until we get more |
| 3700 | * space. |
| 3701 | */ |
| 3702 | if (unlikely(last_ptr)) { |
| 3703 | spin_lock(&last_ptr->lock); |
| 3704 | last_ptr->fragmented = 1; |
| 3705 | spin_unlock(&last_ptr->lock); |
| 3706 | } |
| 3707 | if (ffe_ctl->cached) { |
| 3708 | struct btrfs_free_space_ctl *free_space_ctl; |
| 3709 | |
| 3710 | free_space_ctl = bg->free_space_ctl; |
| 3711 | spin_lock(&free_space_ctl->tree_lock); |
| 3712 | if (free_space_ctl->free_space < |
| 3713 | ffe_ctl->num_bytes + ffe_ctl->empty_cluster + |
| 3714 | ffe_ctl->empty_size) { |
| 3715 | ffe_ctl->total_free_space = max_t(u64, |
| 3716 | ffe_ctl->total_free_space, |
| 3717 | free_space_ctl->free_space); |
| 3718 | spin_unlock(&free_space_ctl->tree_lock); |
| 3719 | return 1; |
| 3720 | } |
| 3721 | spin_unlock(&free_space_ctl->tree_lock); |
| 3722 | } |
| 3723 | |
| 3724 | offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start, |
| 3725 | ffe_ctl->num_bytes, ffe_ctl->empty_size, |
| 3726 | &ffe_ctl->max_extent_size); |
| 3727 | |
| 3728 | /* |
| 3729 | * If we didn't find a chunk, and we haven't failed on this block group |
| 3730 | * before, and this block group is in the middle of caching and we are |
| 3731 | * ok with waiting, then go ahead and wait for progress to be made, and |
| 3732 | * set @retry_unclustered to true. |
| 3733 | * |
| 3734 | * If @retry_unclustered is true then we've already waited on this |
| 3735 | * block group once and should move on to the next block group. |
| 3736 | */ |
| 3737 | if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached && |
| 3738 | ffe_ctl->loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3739 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
| 3740 | ffe_ctl->empty_size); |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3741 | ffe_ctl->retry_unclustered = true; |
| 3742 | return -EAGAIN; |
| 3743 | } else if (!offset) { |
| 3744 | return 1; |
| 3745 | } |
| 3746 | ffe_ctl->found_offset = offset; |
| 3747 | return 0; |
| 3748 | } |
| 3749 | |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 3750 | static int do_allocation_clustered(struct btrfs_block_group *block_group, |
| 3751 | struct find_free_extent_ctl *ffe_ctl, |
| 3752 | struct btrfs_block_group **bg_ret) |
| 3753 | { |
| 3754 | int ret; |
| 3755 | |
| 3756 | /* We want to try and use the cluster allocator, so lets look there */ |
| 3757 | if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) { |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3758 | ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret); |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 3759 | if (ret >= 0 || ret == -EAGAIN) |
| 3760 | return ret; |
| 3761 | /* ret == -ENOENT case falls through */ |
| 3762 | } |
| 3763 | |
Naohiro Aota | 897cae7 | 2020-02-25 12:56:20 +0900 | [diff] [blame] | 3764 | return find_free_extent_unclustered(block_group, ffe_ctl); |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | static int do_allocation(struct btrfs_block_group *block_group, |
| 3768 | struct find_free_extent_ctl *ffe_ctl, |
| 3769 | struct btrfs_block_group **bg_ret) |
| 3770 | { |
| 3771 | switch (ffe_ctl->policy) { |
| 3772 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3773 | return do_allocation_clustered(block_group, ffe_ctl, bg_ret); |
| 3774 | default: |
| 3775 | BUG(); |
| 3776 | } |
| 3777 | } |
| 3778 | |
Naohiro Aota | baba506 | 2020-02-25 12:56:21 +0900 | [diff] [blame] | 3779 | static void release_block_group(struct btrfs_block_group *block_group, |
| 3780 | struct find_free_extent_ctl *ffe_ctl, |
| 3781 | int delalloc) |
| 3782 | { |
| 3783 | switch (ffe_ctl->policy) { |
| 3784 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3785 | ffe_ctl->retry_clustered = false; |
| 3786 | ffe_ctl->retry_unclustered = false; |
| 3787 | break; |
| 3788 | default: |
| 3789 | BUG(); |
| 3790 | } |
| 3791 | |
| 3792 | BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) != |
| 3793 | ffe_ctl->index); |
| 3794 | btrfs_release_block_group(block_group, delalloc); |
| 3795 | } |
| 3796 | |
Naohiro Aota | 0ab9724 | 2020-02-25 12:56:22 +0900 | [diff] [blame] | 3797 | static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl, |
| 3798 | struct btrfs_key *ins) |
| 3799 | { |
| 3800 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
| 3801 | |
| 3802 | if (!ffe_ctl->use_cluster && last_ptr) { |
| 3803 | spin_lock(&last_ptr->lock); |
| 3804 | last_ptr->window_start = ins->objectid; |
| 3805 | spin_unlock(&last_ptr->lock); |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | static void found_extent(struct find_free_extent_ctl *ffe_ctl, |
| 3810 | struct btrfs_key *ins) |
| 3811 | { |
| 3812 | switch (ffe_ctl->policy) { |
| 3813 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3814 | found_extent_clustered(ffe_ctl, ins); |
| 3815 | break; |
| 3816 | default: |
| 3817 | BUG(); |
| 3818 | } |
| 3819 | } |
| 3820 | |
Naohiro Aota | c70e213 | 2020-02-25 12:56:24 +0900 | [diff] [blame] | 3821 | static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl) |
| 3822 | { |
| 3823 | switch (ffe_ctl->policy) { |
| 3824 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3825 | /* |
| 3826 | * If we can't allocate a new chunk we've already looped through |
| 3827 | * at least once, move on to the NO_EMPTY_SIZE case. |
| 3828 | */ |
| 3829 | ffe_ctl->loop = LOOP_NO_EMPTY_SIZE; |
| 3830 | return 0; |
| 3831 | default: |
| 3832 | BUG(); |
| 3833 | } |
| 3834 | } |
| 3835 | |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3836 | /* |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3837 | * Return >0 means caller needs to re-search for free extent |
| 3838 | * Return 0 means we have the needed free extent. |
| 3839 | * Return <0 means we failed to locate any free extent. |
| 3840 | */ |
| 3841 | 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] | 3842 | struct btrfs_key *ins, |
| 3843 | struct find_free_extent_ctl *ffe_ctl, |
Naohiro Aota | 15b7ee6 | 2020-02-25 12:56:23 +0900 | [diff] [blame] | 3844 | bool full_search) |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3845 | { |
| 3846 | struct btrfs_root *root = fs_info->extent_root; |
| 3847 | int ret; |
| 3848 | |
| 3849 | if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) && |
| 3850 | ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg) |
| 3851 | ffe_ctl->orig_have_caching_bg = true; |
| 3852 | |
| 3853 | if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT && |
| 3854 | ffe_ctl->have_caching_bg) |
| 3855 | return 1; |
| 3856 | |
| 3857 | if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES) |
| 3858 | return 1; |
| 3859 | |
| 3860 | if (ins->objectid) { |
Naohiro Aota | 0ab9724 | 2020-02-25 12:56:22 +0900 | [diff] [blame] | 3861 | found_extent(ffe_ctl, ins); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3862 | return 0; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 3867 | * caching kthreads as we move along |
| 3868 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 3869 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 3870 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 3871 | * again |
| 3872 | */ |
| 3873 | if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) { |
| 3874 | ffe_ctl->index = 0; |
| 3875 | if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) { |
| 3876 | /* |
| 3877 | * We want to skip the LOOP_CACHING_WAIT step if we |
| 3878 | * don't have any uncached bgs and we've already done a |
| 3879 | * full search through. |
| 3880 | */ |
| 3881 | if (ffe_ctl->orig_have_caching_bg || !full_search) |
| 3882 | ffe_ctl->loop = LOOP_CACHING_WAIT; |
| 3883 | else |
| 3884 | ffe_ctl->loop = LOOP_ALLOC_CHUNK; |
| 3885 | } else { |
| 3886 | ffe_ctl->loop++; |
| 3887 | } |
| 3888 | |
| 3889 | if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) { |
| 3890 | struct btrfs_trans_handle *trans; |
| 3891 | int exist = 0; |
| 3892 | |
| 3893 | trans = current->journal_info; |
| 3894 | if (trans) |
| 3895 | exist = 1; |
| 3896 | else |
| 3897 | trans = btrfs_join_transaction(root); |
| 3898 | |
| 3899 | if (IS_ERR(trans)) { |
| 3900 | ret = PTR_ERR(trans); |
| 3901 | return ret; |
| 3902 | } |
| 3903 | |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 3904 | ret = btrfs_chunk_alloc(trans, ffe_ctl->flags, |
| 3905 | CHUNK_ALLOC_FORCE); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3906 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3907 | /* Do not bail out on ENOSPC since we can do more. */ |
Naohiro Aota | c70e213 | 2020-02-25 12:56:24 +0900 | [diff] [blame] | 3908 | if (ret == -ENOSPC) |
| 3909 | ret = chunk_allocation_failed(ffe_ctl); |
| 3910 | else if (ret < 0) |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3911 | btrfs_abort_transaction(trans, ret); |
| 3912 | else |
| 3913 | ret = 0; |
| 3914 | if (!exist) |
| 3915 | btrfs_end_transaction(trans); |
| 3916 | if (ret) |
| 3917 | return ret; |
| 3918 | } |
| 3919 | |
| 3920 | if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) { |
Naohiro Aota | 45d8e03 | 2020-02-25 12:56:25 +0900 | [diff] [blame] | 3921 | if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED) |
| 3922 | return -ENOSPC; |
| 3923 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3924 | /* |
| 3925 | * Don't loop again if we already have no empty_size and |
| 3926 | * no empty_cluster. |
| 3927 | */ |
| 3928 | if (ffe_ctl->empty_size == 0 && |
| 3929 | ffe_ctl->empty_cluster == 0) |
| 3930 | return -ENOSPC; |
| 3931 | ffe_ctl->empty_size = 0; |
| 3932 | ffe_ctl->empty_cluster = 0; |
| 3933 | } |
| 3934 | return 1; |
| 3935 | } |
| 3936 | return -ENOSPC; |
| 3937 | } |
| 3938 | |
Naohiro Aota | 7e89540 | 2020-02-25 12:56:26 +0900 | [diff] [blame] | 3939 | static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info, |
| 3940 | struct find_free_extent_ctl *ffe_ctl, |
| 3941 | struct btrfs_space_info *space_info, |
| 3942 | struct btrfs_key *ins) |
| 3943 | { |
| 3944 | /* |
| 3945 | * If our free space is heavily fragmented we may not be able to make |
| 3946 | * big contiguous allocations, so instead of doing the expensive search |
| 3947 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 3948 | * can go ahead and search for a more manageable chunk. |
| 3949 | * |
| 3950 | * If our max_extent_size is large enough for our allocation simply |
| 3951 | * disable clustering since we will likely not be able to find enough |
| 3952 | * space to create a cluster and induce latency trying. |
| 3953 | */ |
| 3954 | if (space_info->max_extent_size) { |
| 3955 | spin_lock(&space_info->lock); |
| 3956 | if (space_info->max_extent_size && |
| 3957 | ffe_ctl->num_bytes > space_info->max_extent_size) { |
| 3958 | ins->offset = space_info->max_extent_size; |
| 3959 | spin_unlock(&space_info->lock); |
| 3960 | return -ENOSPC; |
| 3961 | } else if (space_info->max_extent_size) { |
| 3962 | ffe_ctl->use_cluster = false; |
| 3963 | } |
| 3964 | spin_unlock(&space_info->lock); |
| 3965 | } |
| 3966 | |
| 3967 | ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info, |
| 3968 | &ffe_ctl->empty_cluster); |
| 3969 | if (ffe_ctl->last_ptr) { |
| 3970 | struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr; |
| 3971 | |
| 3972 | spin_lock(&last_ptr->lock); |
| 3973 | if (last_ptr->block_group) |
| 3974 | ffe_ctl->hint_byte = last_ptr->window_start; |
| 3975 | if (last_ptr->fragmented) { |
| 3976 | /* |
| 3977 | * We still set window_start so we can keep track of the |
| 3978 | * last place we found an allocation to try and save |
| 3979 | * some time. |
| 3980 | */ |
| 3981 | ffe_ctl->hint_byte = last_ptr->window_start; |
| 3982 | ffe_ctl->use_cluster = false; |
| 3983 | } |
| 3984 | spin_unlock(&last_ptr->lock); |
| 3985 | } |
| 3986 | |
| 3987 | return 0; |
| 3988 | } |
| 3989 | |
| 3990 | static int prepare_allocation(struct btrfs_fs_info *fs_info, |
| 3991 | struct find_free_extent_ctl *ffe_ctl, |
| 3992 | struct btrfs_space_info *space_info, |
| 3993 | struct btrfs_key *ins) |
| 3994 | { |
| 3995 | switch (ffe_ctl->policy) { |
| 3996 | case BTRFS_EXTENT_ALLOC_CLUSTERED: |
| 3997 | return prepare_allocation_clustered(fs_info, ffe_ctl, |
| 3998 | space_info, ins); |
| 3999 | default: |
| 4000 | BUG(); |
| 4001 | } |
| 4002 | } |
| 4003 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 4004 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4005 | * walks the btree of allocated extents and find a hole of a given size. |
| 4006 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4007 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4008 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4009 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4010 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4011 | * |
| 4012 | * If there is no suitable free space, we will record the max size of |
| 4013 | * the free space extent currently. |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 4014 | * |
| 4015 | * The overall logic and call chain: |
| 4016 | * |
| 4017 | * find_free_extent() |
| 4018 | * |- Iterate through all block groups |
| 4019 | * | |- Get a valid block group |
| 4020 | * | |- Try to do clustered allocation in that block group |
| 4021 | * | |- Try to do unclustered allocation in that block group |
| 4022 | * | |- Check if the result is valid |
| 4023 | * | | |- If valid, then exit |
| 4024 | * | |- Jump to next block group |
| 4025 | * | |
| 4026 | * |- Push harder to find free extents |
| 4027 | * |- If not found, re-iterate all block groups |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4028 | */ |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 4029 | static noinline int find_free_extent(struct btrfs_root *root, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4030 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 4031 | u64 hint_byte_orig, struct btrfs_key *ins, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4032 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4033 | { |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 4034 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4035 | int ret = 0; |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4036 | int cache_block_group_error = 0; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4037 | struct btrfs_block_group *block_group = NULL; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4038 | struct find_free_extent_ctl ffe_ctl = {0}; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4039 | struct btrfs_space_info *space_info; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 4040 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4041 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4042 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4043 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4044 | ffe_ctl.num_bytes = num_bytes; |
| 4045 | ffe_ctl.empty_size = empty_size; |
| 4046 | ffe_ctl.flags = flags; |
| 4047 | ffe_ctl.search_start = 0; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4048 | ffe_ctl.delalloc = delalloc; |
| 4049 | ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags); |
| 4050 | ffe_ctl.have_caching_bg = false; |
| 4051 | ffe_ctl.orig_have_caching_bg = false; |
| 4052 | ffe_ctl.found_offset = 0; |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 4053 | ffe_ctl.hint_byte = hint_byte_orig; |
Naohiro Aota | cb2f96f | 2020-02-25 12:56:16 +0900 | [diff] [blame] | 4054 | ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4055 | |
Naohiro Aota | c10859b | 2020-02-25 12:56:18 +0900 | [diff] [blame] | 4056 | /* For clustered allocation */ |
| 4057 | ffe_ctl.retry_clustered = false; |
| 4058 | ffe_ctl.retry_unclustered = false; |
| 4059 | ffe_ctl.last_ptr = NULL; |
| 4060 | ffe_ctl.use_cluster = true; |
| 4061 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 4062 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4063 | ins->objectid = 0; |
| 4064 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4065 | |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 4066 | trace_find_free_extent(root, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 4067 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 4068 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4069 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4070 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4071 | return -ENOSPC; |
| 4072 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4073 | |
Naohiro Aota | 7e89540 | 2020-02-25 12:56:26 +0900 | [diff] [blame] | 4074 | ret = prepare_allocation(fs_info, &ffe_ctl, space_info, ins); |
| 4075 | if (ret < 0) |
| 4076 | return ret; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4077 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4078 | ffe_ctl.search_start = max(ffe_ctl.search_start, |
| 4079 | first_logical_byte(fs_info, 0)); |
Naohiro Aota | ea54414 | 2020-02-25 12:56:17 +0900 | [diff] [blame] | 4080 | ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte); |
| 4081 | if (ffe_ctl.search_start == ffe_ctl.hint_byte) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4082 | block_group = btrfs_lookup_block_group(fs_info, |
| 4083 | ffe_ctl.search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4084 | /* |
| 4085 | * we don't want to use the block group if it doesn't match our |
| 4086 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4087 | * |
| 4088 | * However if we are re-searching with an ideal block group |
| 4089 | * 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] | 4090 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4091 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 4092 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4093 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4094 | if (list_empty(&block_group->list) || |
| 4095 | block_group->ro) { |
| 4096 | /* |
| 4097 | * someone is removing this block group, |
| 4098 | * we can't jump into the have_block_group |
| 4099 | * target because our list pointers are not |
| 4100 | * valid |
| 4101 | */ |
| 4102 | btrfs_put_block_group(block_group); |
| 4103 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4104 | } else { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4105 | ffe_ctl.index = btrfs_bg_flags_to_raid_index( |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4106 | block_group->flags); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4107 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4108 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4109 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4110 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4111 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4112 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4113 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4114 | search: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4115 | ffe_ctl.have_caching_bg = false; |
| 4116 | if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) || |
| 4117 | ffe_ctl.index == 0) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 4118 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4119 | down_read(&space_info->groups_sem); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4120 | list_for_each_entry(block_group, |
| 4121 | &space_info->block_groups[ffe_ctl.index], list) { |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4122 | struct btrfs_block_group *bg_ret; |
| 4123 | |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 4124 | /* If the block group is read-only, we can skip it entirely. */ |
| 4125 | if (unlikely(block_group->ro)) |
| 4126 | continue; |
| 4127 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4128 | btrfs_grab_block_group(block_group, delalloc); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 4129 | ffe_ctl.search_start = block_group->start; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4130 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4131 | /* |
| 4132 | * this can happen if we end up cycling through all the |
| 4133 | * raid types, but we want to make sure we only allocate |
| 4134 | * for the proper type. |
| 4135 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4136 | if (!block_group_bits(block_group, flags)) { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 4137 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 4138 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 4139 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4140 | BTRFS_BLOCK_GROUP_RAID10; |
| 4141 | |
| 4142 | /* |
| 4143 | * if they asked for extra copies and this block group |
| 4144 | * doesn't provide them, bail. This does allow us to |
| 4145 | * fill raid0 from raid1. |
| 4146 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4147 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4148 | goto loop; |
Qu Wenruo | 2a28468 | 2019-07-16 17:00:33 +0800 | [diff] [blame] | 4149 | |
| 4150 | /* |
| 4151 | * This block group has different flags than we want. |
| 4152 | * It's possible that we have MIXED_GROUP flag but no |
| 4153 | * block group is mixed. Just skip such block group. |
| 4154 | */ |
| 4155 | btrfs_release_block_group(block_group, delalloc); |
| 4156 | continue; |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4157 | } |
| 4158 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4159 | have_block_group: |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4160 | ffe_ctl.cached = btrfs_block_group_done(block_group); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4161 | if (unlikely(!ffe_ctl.cached)) { |
| 4162 | ffe_ctl.have_caching_bg = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 4163 | ret = btrfs_cache_block_group(block_group, 0); |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4164 | |
| 4165 | /* |
| 4166 | * If we get ENOMEM here or something else we want to |
| 4167 | * try other block groups, because it may not be fatal. |
| 4168 | * However if we can't find anything else we need to |
| 4169 | * save our return here so that we return the actual |
| 4170 | * error that caused problems, not ENOSPC. |
| 4171 | */ |
| 4172 | if (ret < 0) { |
| 4173 | if (!cache_block_group_error) |
| 4174 | cache_block_group_error = ret; |
| 4175 | ret = 0; |
| 4176 | goto loop; |
| 4177 | } |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 4178 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4179 | } |
| 4180 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 4181 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 4182 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4183 | |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4184 | bg_ret = NULL; |
| 4185 | ret = do_allocation(block_group, &ffe_ctl, &bg_ret); |
| 4186 | if (ret == 0) { |
| 4187 | if (bg_ret && bg_ret != block_group) { |
| 4188 | btrfs_release_block_group(block_group, delalloc); |
| 4189 | block_group = bg_ret; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4190 | } |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4191 | } else if (ret == -EAGAIN) { |
| 4192 | goto have_block_group; |
| 4193 | } else if (ret > 0) { |
| 4194 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4195 | } |
| 4196 | |
Naohiro Aota | c668690 | 2020-02-25 12:56:19 +0900 | [diff] [blame] | 4197 | /* Checks */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4198 | ffe_ctl.search_start = round_up(ffe_ctl.found_offset, |
| 4199 | fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4200 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4201 | /* move on to the next group */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4202 | if (ffe_ctl.search_start + num_bytes > |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 4203 | block_group->start + block_group->length) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4204 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4205 | num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4206 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4207 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4208 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4209 | if (ffe_ctl.found_offset < ffe_ctl.search_start) |
| 4210 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4211 | ffe_ctl.search_start - ffe_ctl.found_offset); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4212 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4213 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 4214 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4215 | if (ret == -EAGAIN) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4216 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4217 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4218 | goto loop; |
| 4219 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4220 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4221 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4222 | /* we are all good, lets return */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4223 | ins->objectid = ffe_ctl.search_start; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4224 | ins->offset = num_bytes; |
| 4225 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4226 | trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start, |
| 4227 | num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4228 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4229 | break; |
| 4230 | loop: |
Naohiro Aota | baba506 | 2020-02-25 12:56:21 +0900 | [diff] [blame] | 4231 | release_block_group(block_group, &ffe_ctl, delalloc); |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 4232 | cond_resched(); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4233 | } |
| 4234 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4235 | |
Naohiro Aota | 15b7ee6 | 2020-02-25 12:56:23 +0900 | [diff] [blame] | 4236 | 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] | 4237 | if (ret > 0) |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 4238 | goto search; |
| 4239 | |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4240 | if (ret == -ENOSPC && !cache_block_group_error) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4241 | /* |
| 4242 | * Use ffe_ctl->total_free_space as fallback if we can't find |
| 4243 | * any contiguous hole. |
| 4244 | */ |
| 4245 | if (!ffe_ctl.max_extent_size) |
| 4246 | ffe_ctl.max_extent_size = ffe_ctl.total_free_space; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4247 | spin_lock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4248 | space_info->max_extent_size = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4249 | spin_unlock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4250 | ins->offset = ffe_ctl.max_extent_size; |
Josef Bacik | db8fe64 | 2019-11-19 13:59:00 -0500 | [diff] [blame] | 4251 | } else if (ret == -ENOSPC) { |
| 4252 | ret = cache_block_group_error; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4253 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4254 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4255 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4256 | |
Nikolay Borisov | 6f47c70 | 2018-03-13 12:22:32 +0200 | [diff] [blame] | 4257 | /* |
| 4258 | * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a |
| 4259 | * hole that is at least as big as @num_bytes. |
| 4260 | * |
| 4261 | * @root - The root that will contain this extent |
| 4262 | * |
| 4263 | * @ram_bytes - The amount of space in ram that @num_bytes take. This |
| 4264 | * is used for accounting purposes. This value differs |
| 4265 | * from @num_bytes only in the case of compressed extents. |
| 4266 | * |
| 4267 | * @num_bytes - Number of bytes to allocate on-disk. |
| 4268 | * |
| 4269 | * @min_alloc_size - Indicates the minimum amount of space that the |
| 4270 | * allocator should try to satisfy. In some cases |
| 4271 | * @num_bytes may be larger than what is required and if |
| 4272 | * the filesystem is fragmented then allocation fails. |
| 4273 | * However, the presence of @min_alloc_size gives a |
| 4274 | * chance to try and satisfy the smaller allocation. |
| 4275 | * |
| 4276 | * @empty_size - A hint that you plan on doing more COW. This is the |
| 4277 | * size in bytes the allocator should try to find free |
| 4278 | * next to the block it returns. This is just a hint and |
| 4279 | * may be ignored by the allocator. |
| 4280 | * |
| 4281 | * @hint_byte - Hint to the allocator to start searching above the byte |
| 4282 | * address passed. It might be ignored. |
| 4283 | * |
| 4284 | * @ins - This key is modified to record the found hole. It will |
| 4285 | * have the following values: |
| 4286 | * ins->objectid == start position |
| 4287 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
| 4288 | * ins->offset == the size of the hole. |
| 4289 | * |
| 4290 | * @is_data - Boolean flag indicating whether an extent is |
| 4291 | * allocated for data (true) or metadata (false) |
| 4292 | * |
| 4293 | * @delalloc - Boolean flag indicating whether this allocation is for |
| 4294 | * delalloc or not. If 'true' data_rwsem of block groups |
| 4295 | * is going to be acquired. |
| 4296 | * |
| 4297 | * |
| 4298 | * Returns 0 when an allocation succeeded or < 0 when an error occurred. In |
| 4299 | * case -ENOSPC is returned then @ins->offset will contain the size of the |
| 4300 | * largest available hole the allocator managed to find. |
| 4301 | */ |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4302 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4303 | u64 num_bytes, u64 min_alloc_size, |
| 4304 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4305 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4306 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4307 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 4308 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4309 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4310 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4311 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 4312 | flags = get_alloc_profile_by_root(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4313 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4314 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | 437490f | 2020-07-28 09:42:49 +0800 | [diff] [blame] | 4315 | ret = find_free_extent(root, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4316 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4317 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4318 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4319 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4320 | if (!final_tried && ins->offset) { |
| 4321 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4322 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4323 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4324 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4325 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4326 | if (num_bytes == min_alloc_size) |
| 4327 | final_tried = true; |
| 4328 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4329 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4330 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4331 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 4332 | sinfo = btrfs_find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4333 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4334 | "allocation failed flags %llu, wanted %llu", |
| 4335 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 4336 | if (sinfo) |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 4337 | btrfs_dump_space_info(fs_info, sinfo, |
| 4338 | num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4339 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4340 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4341 | |
| 4342 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4343 | } |
| 4344 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4345 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4346 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4347 | { |
Nikolay Borisov | 7ef54d5 | 2019-11-21 14:03:30 +0200 | [diff] [blame] | 4348 | struct btrfs_block_group *cache; |
| 4349 | |
| 4350 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4351 | if (!cache) { |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4352 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 4353 | start); |
Nikolay Borisov | 7ef54d5 | 2019-11-21 14:03:30 +0200 | [diff] [blame] | 4354 | return -ENOSPC; |
| 4355 | } |
| 4356 | |
| 4357 | btrfs_add_free_space(cache, start, len); |
| 4358 | btrfs_free_reserved_bytes(cache, len, delalloc); |
| 4359 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
| 4360 | |
| 4361 | btrfs_put_block_group(cache); |
| 4362 | return 0; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4363 | } |
| 4364 | |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 4365 | int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start, |
| 4366 | u64 len) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4367 | { |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4368 | struct btrfs_block_group *cache; |
| 4369 | int ret = 0; |
| 4370 | |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 4371 | cache = btrfs_lookup_block_group(trans->fs_info, start); |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4372 | if (!cache) { |
Nikolay Borisov | 7bfc100 | 2020-01-20 16:09:12 +0200 | [diff] [blame] | 4373 | btrfs_err(trans->fs_info, "unable to find block group for %llu", |
| 4374 | start); |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4375 | return -ENOSPC; |
| 4376 | } |
| 4377 | |
Nikolay Borisov | 6690d07 | 2020-01-20 16:09:14 +0200 | [diff] [blame] | 4378 | ret = pin_down_extent(trans, cache, start, len, 1); |
Nikolay Borisov | a0fbf73 | 2019-11-21 14:03:31 +0200 | [diff] [blame] | 4379 | btrfs_put_block_group(cache); |
| 4380 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4381 | } |
| 4382 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4383 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4384 | u64 parent, u64 root_objectid, |
| 4385 | u64 flags, u64 owner, u64 offset, |
| 4386 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4387 | { |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4388 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4389 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4390 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4391 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4392 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4393 | struct extent_buffer *leaf; |
| 4394 | int type; |
| 4395 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4396 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4397 | if (parent > 0) |
| 4398 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4399 | else |
| 4400 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4401 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4402 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4403 | |
| 4404 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4405 | if (!path) |
| 4406 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4407 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4408 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4409 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4410 | if (ret) { |
| 4411 | btrfs_free_path(path); |
| 4412 | return ret; |
| 4413 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4414 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4415 | leaf = path->nodes[0]; |
| 4416 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4417 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4418 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4419 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4420 | btrfs_set_extent_flags(leaf, extent_item, |
| 4421 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4422 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4423 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4424 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4425 | if (parent > 0) { |
| 4426 | struct btrfs_shared_data_ref *ref; |
| 4427 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4428 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4429 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4430 | } else { |
| 4431 | struct btrfs_extent_data_ref *ref; |
| 4432 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4433 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4434 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4435 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4436 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4437 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4438 | |
| 4439 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4440 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4441 | |
Nikolay Borisov | 25a356d | 2018-05-10 15:44:54 +0300 | [diff] [blame] | 4442 | ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4443 | if (ret) |
| 4444 | return ret; |
| 4445 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4446 | ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4447 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4448 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4449 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4450 | BUG(); |
| 4451 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4452 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4453 | return ret; |
| 4454 | } |
| 4455 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4456 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4457 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4458 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4459 | { |
Nikolay Borisov | 9dcdbe0 | 2018-05-21 12:27:20 +0300 | [diff] [blame] | 4460 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4461 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4462 | struct btrfs_extent_item *extent_item; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4463 | struct btrfs_key extent_key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4464 | struct btrfs_tree_block_info *block_info; |
| 4465 | struct btrfs_extent_inline_ref *iref; |
| 4466 | struct btrfs_path *path; |
| 4467 | struct extent_buffer *leaf; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4468 | struct btrfs_delayed_tree_ref *ref; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4469 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4470 | u64 num_bytes; |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4471 | u64 flags = extent_op->flags_to_set; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4472 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4473 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4474 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 4475 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4476 | extent_key.objectid = node->bytenr; |
| 4477 | if (skinny_metadata) { |
| 4478 | extent_key.offset = ref->level; |
| 4479 | extent_key.type = BTRFS_METADATA_ITEM_KEY; |
| 4480 | num_bytes = fs_info->nodesize; |
| 4481 | } else { |
| 4482 | extent_key.offset = node->num_bytes; |
| 4483 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4484 | size += sizeof(*block_info); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4485 | num_bytes = node->num_bytes; |
| 4486 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4487 | |
| 4488 | path = btrfs_alloc_path(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 4489 | if (!path) |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4490 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4491 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4492 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4493 | &extent_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4494 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 4495 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4496 | return ret; |
| 4497 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4498 | |
| 4499 | leaf = path->nodes[0]; |
| 4500 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4501 | struct btrfs_extent_item); |
| 4502 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4503 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4504 | btrfs_set_extent_flags(leaf, extent_item, |
| 4505 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4506 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4507 | if (skinny_metadata) { |
| 4508 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4509 | } else { |
| 4510 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4511 | btrfs_set_tree_block_key(leaf, block_info, &extent_op->key); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4512 | btrfs_set_tree_block_level(leaf, block_info, ref->level); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4513 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4514 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4515 | |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4516 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4517 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4518 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4519 | BTRFS_SHARED_BLOCK_REF_KEY); |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4520 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4521 | } else { |
| 4522 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4523 | BTRFS_TREE_BLOCK_REF_KEY); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4524 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | btrfs_mark_buffer_dirty(leaf); |
| 4528 | btrfs_free_path(path); |
| 4529 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4530 | ret = remove_from_free_space_tree(trans, extent_key.objectid, |
| 4531 | num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4532 | if (ret) |
| 4533 | return ret; |
| 4534 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4535 | ret = btrfs_update_block_group(trans, extent_key.objectid, |
| 4536 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4537 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4538 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4539 | extent_key.objectid, extent_key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4540 | BUG(); |
| 4541 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 4542 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4543 | trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4544 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4545 | return ret; |
| 4546 | } |
| 4547 | |
| 4548 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4549 | struct btrfs_root *root, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 4550 | u64 offset, u64 ram_bytes, |
| 4551 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4552 | { |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4553 | struct btrfs_ref generic_ref = { 0 }; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4554 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4555 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4556 | BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4557 | |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4558 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4559 | ins->objectid, ins->offset, 0); |
| 4560 | btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4561 | btrfs_ref_tree_mod(root->fs_info, &generic_ref); |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4562 | ret = btrfs_add_delayed_data_ref(trans, &generic_ref, |
| 4563 | ram_bytes, NULL, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4564 | return ret; |
| 4565 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4566 | |
| 4567 | /* |
| 4568 | * this is used by the tree logging recovery code. It records that |
| 4569 | * an extent has been allocated and makes sure to clear the free |
| 4570 | * space cache bits as well |
| 4571 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4572 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4573 | u64 root_objectid, u64 owner, u64 offset, |
| 4574 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4575 | { |
Nikolay Borisov | 61da2ab | 2018-06-20 15:49:13 +0300 | [diff] [blame] | 4576 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4577 | int ret; |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 4578 | struct btrfs_block_group *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4579 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4580 | |
| 4581 | /* |
| 4582 | * 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] | 4583 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4584 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4585 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4586 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 4587 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4588 | if (ret) |
| 4589 | return ret; |
| 4590 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4591 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4592 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4593 | if (!block_group) |
| 4594 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4595 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4596 | space_info = block_group->space_info; |
| 4597 | spin_lock(&space_info->lock); |
| 4598 | spin_lock(&block_group->lock); |
| 4599 | space_info->bytes_reserved += ins->offset; |
| 4600 | block_group->reserved += ins->offset; |
| 4601 | spin_unlock(&block_group->lock); |
| 4602 | spin_unlock(&space_info->lock); |
| 4603 | |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4604 | ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, |
| 4605 | offset, ins, 1); |
Josef Bacik | bd72717 | 2020-02-13 10:47:30 -0500 | [diff] [blame] | 4606 | if (ret) |
Josef Bacik | ab9b2c7 | 2020-02-13 10:47:30 -0500 | [diff] [blame] | 4607 | btrfs_pin_extent(trans, ins->objectid, ins->offset, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4608 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4609 | return ret; |
| 4610 | } |
| 4611 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4612 | static struct extent_buffer * |
| 4613 | 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] | 4614 | u64 bytenr, int level, u64 owner, |
| 4615 | enum btrfs_lock_nesting nest) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4616 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4617 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4618 | struct extent_buffer *buf; |
| 4619 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4620 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4621 | if (IS_ERR(buf)) |
| 4622 | return buf; |
| 4623 | |
Qu Wenruo | b72c3ab | 2018-08-21 09:53:47 +0800 | [diff] [blame] | 4624 | /* |
| 4625 | * Extra safety check in case the extent tree is corrupted and extent |
| 4626 | * allocator chooses to use a tree block which is already used and |
| 4627 | * locked. |
| 4628 | */ |
| 4629 | if (buf->lock_owner == current->pid) { |
| 4630 | btrfs_err_rl(fs_info, |
| 4631 | "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected", |
| 4632 | buf->start, btrfs_header_owner(buf), current->pid); |
| 4633 | free_extent_buffer(buf); |
| 4634 | return ERR_PTR(-EUCLEAN); |
| 4635 | } |
| 4636 | |
Josef Bacik | ad24466 | 2020-08-10 11:42:30 -0400 | [diff] [blame] | 4637 | btrfs_set_buffer_lockdep_class(owner, buf, level); |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4638 | __btrfs_tree_lock(buf, nest); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 4639 | btrfs_clean_tree_block(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4640 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4641 | |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 4642 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4643 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4644 | memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header)); |
| 4645 | btrfs_set_header_level(buf, level); |
| 4646 | btrfs_set_header_bytenr(buf, buf->start); |
| 4647 | btrfs_set_header_generation(buf, trans->transid); |
| 4648 | btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV); |
| 4649 | btrfs_set_header_owner(buf, owner); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 4650 | write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4651 | write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4652 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4653 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4654 | /* |
| 4655 | * we allow two log transactions at a time, use different |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4656 | * EXTENT bit to differentiate dirty pages. |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4657 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4658 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4659 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 4660 | buf->start + buf->len - 1, GFP_NOFS); |
| 4661 | else |
| 4662 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 4663 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4664 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4665 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4666 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4667 | buf->start + buf->len - 1, GFP_NOFS); |
| 4668 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 4669 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4670 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4671 | return buf; |
| 4672 | } |
| 4673 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4674 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4675 | * 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] | 4676 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4677 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 4678 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4679 | struct btrfs_root *root, |
| 4680 | u64 parent, u64 root_objectid, |
| 4681 | const struct btrfs_disk_key *key, |
| 4682 | int level, u64 hint, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4683 | u64 empty_size, |
| 4684 | enum btrfs_lock_nesting nest) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4685 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4686 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4687 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4688 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4690 | struct btrfs_delayed_extent_op *extent_op; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4691 | struct btrfs_ref generic_ref = { 0 }; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4692 | u64 flags = 0; |
| 4693 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4694 | u32 blocksize = fs_info->nodesize; |
| 4695 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4696 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4697 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4698 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4699 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4700 | level, root_objectid, nest); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4701 | if (!IS_ERR(buf)) |
| 4702 | root->alloc_bytenr += blocksize; |
| 4703 | return buf; |
| 4704 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4705 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 4706 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4707 | block_rsv = btrfs_use_block_rsv(trans, root, blocksize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4708 | if (IS_ERR(block_rsv)) |
| 4709 | return ERR_CAST(block_rsv); |
| 4710 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4711 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4712 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4713 | if (ret) |
| 4714 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4715 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4716 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4717 | root_objectid, nest); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4718 | if (IS_ERR(buf)) { |
| 4719 | ret = PTR_ERR(buf); |
| 4720 | goto out_free_reserved; |
| 4721 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4722 | |
| 4723 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4724 | if (parent == 0) |
| 4725 | parent = ins.objectid; |
| 4726 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4727 | } else |
| 4728 | BUG_ON(parent > 0); |
| 4729 | |
| 4730 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 4731 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4732 | if (!extent_op) { |
| 4733 | ret = -ENOMEM; |
| 4734 | goto out_free_buf; |
| 4735 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4736 | if (key) |
| 4737 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4738 | else |
| 4739 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4740 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 4741 | extent_op->update_key = skinny_metadata ? false : true; |
| 4742 | extent_op->update_flags = true; |
| 4743 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4744 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4745 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4746 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4747 | ins.objectid, ins.offset, parent); |
| 4748 | generic_ref.real_root = root->root_key.objectid; |
| 4749 | btrfs_init_tree_ref(&generic_ref, level, root_objectid); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4750 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4751 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, |
Omar Sandoval | 7be0791 | 2017-06-06 16:45:30 -0700 | [diff] [blame] | 4752 | extent_op, NULL, NULL); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4753 | if (ret) |
| 4754 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4755 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4756 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4757 | |
| 4758 | out_free_delayed: |
| 4759 | btrfs_free_delayed_extent_op(extent_op); |
| 4760 | out_free_buf: |
| 4761 | free_extent_buffer(buf); |
| 4762 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4763 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4764 | out_unuse: |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4765 | btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4766 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4767 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4768 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4769 | struct walk_control { |
| 4770 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4771 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4772 | struct btrfs_key update_progress; |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 4773 | struct btrfs_key drop_progress; |
| 4774 | int drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4775 | int stage; |
| 4776 | int level; |
| 4777 | int shared_level; |
| 4778 | int update_ref; |
| 4779 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4780 | int reada_slot; |
| 4781 | int reada_count; |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4782 | int restarted; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4783 | }; |
| 4784 | |
| 4785 | #define DROP_REFERENCE 1 |
| 4786 | #define UPDATE_BACKREF 2 |
| 4787 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4788 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4789 | struct btrfs_root *root, |
| 4790 | struct walk_control *wc, |
| 4791 | struct btrfs_path *path) |
| 4792 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4793 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4794 | u64 bytenr; |
| 4795 | u64 generation; |
| 4796 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4797 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4798 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4799 | struct btrfs_key key; |
| 4800 | struct extent_buffer *eb; |
| 4801 | int ret; |
| 4802 | int slot; |
| 4803 | int nread = 0; |
| 4804 | |
| 4805 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4806 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4807 | wc->reada_count = max(wc->reada_count, 2); |
| 4808 | } else { |
| 4809 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4810 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4811 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | eb = path->nodes[wc->level]; |
| 4815 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4816 | |
| 4817 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 4818 | if (nread >= wc->reada_count) |
| 4819 | break; |
| 4820 | |
| 4821 | cond_resched(); |
| 4822 | bytenr = btrfs_node_blockptr(eb, slot); |
| 4823 | generation = btrfs_node_ptr_generation(eb, slot); |
| 4824 | |
| 4825 | if (slot == path->slots[wc->level]) |
| 4826 | goto reada; |
| 4827 | |
| 4828 | if (wc->stage == UPDATE_BACKREF && |
| 4829 | generation <= root->root_key.offset) |
| 4830 | continue; |
| 4831 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4832 | /* 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] | 4833 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4834 | wc->level - 1, 1, &refs, |
| 4835 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4836 | /* We don't care about errors in readahead. */ |
| 4837 | if (ret < 0) |
| 4838 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4839 | BUG_ON(refs == 0); |
| 4840 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4841 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4842 | if (refs == 1) |
| 4843 | goto reada; |
| 4844 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4845 | if (wc->level == 1 && |
| 4846 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4847 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4848 | if (!wc->update_ref || |
| 4849 | generation <= root->root_key.offset) |
| 4850 | continue; |
| 4851 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 4852 | ret = btrfs_comp_cpu_keys(&key, |
| 4853 | &wc->update_progress); |
| 4854 | if (ret < 0) |
| 4855 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4856 | } else { |
| 4857 | if (wc->level == 1 && |
| 4858 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4859 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4860 | } |
| 4861 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4862 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4863 | nread++; |
| 4864 | } |
| 4865 | wc->reada_slot = slot; |
| 4866 | } |
| 4867 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4868 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4869 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4870 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4871 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 4872 | * back refs for pointers in the block. |
| 4873 | * |
| 4874 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4875 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4876 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 4877 | struct btrfs_root *root, |
| 4878 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4879 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4880 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4881 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4882 | int level = wc->level; |
| 4883 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4884 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4885 | int ret; |
| 4886 | |
| 4887 | if (wc->stage == UPDATE_BACKREF && |
| 4888 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 4889 | return 1; |
| 4890 | |
| 4891 | /* |
| 4892 | * when reference count of tree block is 1, it won't increase |
| 4893 | * again. once full backref flag is set, we never clear it. |
| 4894 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4895 | if (lookup_info && |
| 4896 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 4897 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4898 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4899 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4900 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4901 | &wc->refs[level], |
| 4902 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4903 | BUG_ON(ret == -ENOMEM); |
| 4904 | if (ret) |
| 4905 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4906 | BUG_ON(wc->refs[level] == 0); |
| 4907 | } |
| 4908 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4909 | if (wc->stage == DROP_REFERENCE) { |
| 4910 | if (wc->refs[level] > 1) |
| 4911 | return 1; |
| 4912 | |
| 4913 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4914 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4915 | path->locks[level] = 0; |
| 4916 | } |
| 4917 | return 0; |
| 4918 | } |
| 4919 | |
| 4920 | /* wc->stage == UPDATE_BACKREF */ |
| 4921 | if (!(wc->flags[level] & flag)) { |
| 4922 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4923 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4924 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4925 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4926 | BUG_ON(ret); /* -ENOMEM */ |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 4927 | ret = btrfs_set_disk_extent_flags(trans, eb, flag, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4928 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4929 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4930 | wc->flags[level] |= flag; |
| 4931 | } |
| 4932 | |
| 4933 | /* |
| 4934 | * the block is shared by multiple trees, so it's not good to |
| 4935 | * keep the tree lock |
| 4936 | */ |
| 4937 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4938 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4939 | path->locks[level] = 0; |
| 4940 | } |
| 4941 | return 0; |
| 4942 | } |
| 4943 | |
| 4944 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4945 | * This is used to verify a ref exists for this root to deal with a bug where we |
| 4946 | * would have a drop_progress key that hadn't been updated properly. |
| 4947 | */ |
| 4948 | static int check_ref_exists(struct btrfs_trans_handle *trans, |
| 4949 | struct btrfs_root *root, u64 bytenr, u64 parent, |
| 4950 | int level) |
| 4951 | { |
| 4952 | struct btrfs_path *path; |
| 4953 | struct btrfs_extent_inline_ref *iref; |
| 4954 | int ret; |
| 4955 | |
| 4956 | path = btrfs_alloc_path(); |
| 4957 | if (!path) |
| 4958 | return -ENOMEM; |
| 4959 | |
| 4960 | ret = lookup_extent_backref(trans, path, &iref, bytenr, |
| 4961 | root->fs_info->nodesize, parent, |
| 4962 | root->root_key.objectid, level, 0); |
| 4963 | btrfs_free_path(path); |
| 4964 | if (ret == -ENOENT) |
| 4965 | return 0; |
| 4966 | if (ret < 0) |
| 4967 | return ret; |
| 4968 | return 1; |
| 4969 | } |
| 4970 | |
| 4971 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4972 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4973 | * |
| 4974 | * when wc->stage == DROP_REFERENCE, this function checks |
| 4975 | * reference count of the block pointed to. if the block |
| 4976 | * is shared and we need update back refs for the subtree |
| 4977 | * rooted at the block, this function changes wc->stage to |
| 4978 | * UPDATE_BACKREF. if the block is shared and there is no |
| 4979 | * need to update back, this function drops the reference |
| 4980 | * to the block. |
| 4981 | * |
| 4982 | * NOTE: return value 1 means we should stop walking down. |
| 4983 | */ |
| 4984 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 4985 | struct btrfs_root *root, |
| 4986 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4987 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4988 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4989 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4990 | u64 bytenr; |
| 4991 | u64 generation; |
| 4992 | u64 parent; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4993 | struct btrfs_key key; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4994 | struct btrfs_key first_key; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4995 | struct btrfs_ref ref = { 0 }; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4996 | struct extent_buffer *next; |
| 4997 | int level = wc->level; |
| 4998 | int reada = 0; |
| 4999 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5000 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5001 | |
| 5002 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5003 | path->slots[level]); |
| 5004 | /* |
| 5005 | * if the lower level block was created before the snapshot |
| 5006 | * was created, we know there is no need to update back refs |
| 5007 | * for the subtree |
| 5008 | */ |
| 5009 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5010 | generation <= root->root_key.offset) { |
| 5011 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5012 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5013 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5014 | |
| 5015 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 5016 | btrfs_node_key_to_cpu(path->nodes[level], &first_key, |
| 5017 | path->slots[level]); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5018 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5019 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5020 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5021 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 5022 | if (IS_ERR(next)) |
| 5023 | return PTR_ERR(next); |
| 5024 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 5025 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 5026 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5027 | reada = 1; |
| 5028 | } |
| 5029 | btrfs_tree_lock(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5030 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5031 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5032 | &wc->refs[level - 1], |
| 5033 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5034 | if (ret < 0) |
| 5035 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5036 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5037 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5038 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5039 | ret = -EIO; |
| 5040 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5041 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5042 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5043 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5044 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5045 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5046 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5047 | if (level == 1 && |
| 5048 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5049 | goto skip; |
| 5050 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5051 | if (!wc->update_ref || |
| 5052 | generation <= root->root_key.offset) |
| 5053 | goto skip; |
| 5054 | |
| 5055 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5056 | path->slots[level]); |
| 5057 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5058 | if (ret < 0) |
| 5059 | goto skip; |
| 5060 | |
| 5061 | wc->stage = UPDATE_BACKREF; |
| 5062 | wc->shared_level = level - 1; |
| 5063 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5064 | } else { |
| 5065 | if (level == 1 && |
| 5066 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5067 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5068 | } |
| 5069 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 5070 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5071 | btrfs_tree_unlock(next); |
| 5072 | free_extent_buffer(next); |
| 5073 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5074 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | if (!next) { |
| 5078 | if (reada && level == 1) |
| 5079 | reada_walk_down(trans, root, wc, path); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 5080 | next = read_tree_block(fs_info, bytenr, generation, level - 1, |
| 5081 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 5082 | if (IS_ERR(next)) { |
| 5083 | return PTR_ERR(next); |
| 5084 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 5085 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 5086 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 5087 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5088 | btrfs_tree_lock(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5089 | } |
| 5090 | |
| 5091 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5092 | ASSERT(level == btrfs_header_level(next)); |
| 5093 | if (level != btrfs_header_level(next)) { |
| 5094 | btrfs_err(root->fs_info, "mismatched level"); |
| 5095 | ret = -EIO; |
| 5096 | goto out_unlock; |
| 5097 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5098 | path->nodes[level] = next; |
| 5099 | path->slots[level] = 0; |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 5100 | path->locks[level] = BTRFS_WRITE_LOCK; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5101 | wc->level = level; |
| 5102 | if (wc->level == 1) |
| 5103 | wc->reada_slot = 0; |
| 5104 | return 0; |
| 5105 | skip: |
| 5106 | wc->refs[level - 1] = 0; |
| 5107 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5108 | if (wc->stage == DROP_REFERENCE) { |
| 5109 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5110 | parent = path->nodes[level]->start; |
| 5111 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5112 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5113 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5114 | if (root->root_key.objectid != |
| 5115 | btrfs_header_owner(path->nodes[level])) { |
| 5116 | btrfs_err(root->fs_info, |
| 5117 | "mismatched block owner"); |
| 5118 | ret = -EIO; |
| 5119 | goto out_unlock; |
| 5120 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5121 | parent = 0; |
| 5122 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5123 | |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 5124 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5125 | * If we had a drop_progress we need to verify the refs are set |
| 5126 | * as expected. If we find our ref then we know that from here |
| 5127 | * on out everything should be correct, and we can clear the |
| 5128 | * ->restarted flag. |
| 5129 | */ |
| 5130 | if (wc->restarted) { |
| 5131 | ret = check_ref_exists(trans, root, bytenr, parent, |
| 5132 | level - 1); |
| 5133 | if (ret < 0) |
| 5134 | goto out_unlock; |
| 5135 | if (ret == 0) |
| 5136 | goto no_delete; |
| 5137 | ret = 0; |
| 5138 | wc->restarted = 0; |
| 5139 | } |
| 5140 | |
| 5141 | /* |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 5142 | * Reloc tree doesn't contribute to qgroup numbers, and we have |
| 5143 | * already accounted them at merge time (replace_path), |
| 5144 | * thus we could skip expensive subtree trace here. |
| 5145 | */ |
| 5146 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
| 5147 | need_account) { |
Lu Fengqi | deb4062 | 2018-07-18 14:45:38 +0800 | [diff] [blame] | 5148 | ret = btrfs_qgroup_trace_subtree(trans, next, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 5149 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5150 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5151 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5152 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 5153 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5154 | } |
| 5155 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5156 | |
| 5157 | /* |
| 5158 | * We need to update the next key in our walk control so we can |
| 5159 | * update the drop_progress key accordingly. We don't care if |
| 5160 | * find_next_key doesn't find a key because that means we're at |
| 5161 | * the end and are going to clean up now. |
| 5162 | */ |
| 5163 | wc->drop_level = level; |
| 5164 | find_next_key(path, level, &wc->drop_progress); |
| 5165 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 5166 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
| 5167 | fs_info->nodesize, parent); |
| 5168 | btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid); |
| 5169 | ret = btrfs_free_extent(trans, &ref); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5170 | if (ret) |
| 5171 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5172 | } |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5173 | no_delete: |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5174 | *lookup_info = 1; |
| 5175 | ret = 1; |
| 5176 | |
| 5177 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5178 | btrfs_tree_unlock(next); |
| 5179 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 5180 | |
| 5181 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 5185 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5186 | * |
| 5187 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5188 | * reference count on the block. |
| 5189 | * |
| 5190 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5191 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5192 | * to UPDATE_BACKREF previously while processing the block. |
| 5193 | * |
| 5194 | * NOTE: return value 1 means we should stop walking up. |
| 5195 | */ |
| 5196 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5197 | struct btrfs_root *root, |
| 5198 | struct btrfs_path *path, |
| 5199 | struct walk_control *wc) |
| 5200 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5201 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5202 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5203 | int level = wc->level; |
| 5204 | struct extent_buffer *eb = path->nodes[level]; |
| 5205 | u64 parent = 0; |
| 5206 | |
| 5207 | if (wc->stage == UPDATE_BACKREF) { |
| 5208 | BUG_ON(wc->shared_level < level); |
| 5209 | if (level < wc->shared_level) |
| 5210 | goto out; |
| 5211 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5212 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5213 | if (ret > 0) |
| 5214 | wc->update_ref = 0; |
| 5215 | |
| 5216 | wc->stage = DROP_REFERENCE; |
| 5217 | wc->shared_level = -1; |
| 5218 | path->slots[level] = 0; |
| 5219 | |
| 5220 | /* |
| 5221 | * check reference count again if the block isn't locked. |
| 5222 | * we should start walking down the tree again if reference |
| 5223 | * count is one. |
| 5224 | */ |
| 5225 | if (!path->locks[level]) { |
| 5226 | BUG_ON(level == 0); |
| 5227 | btrfs_tree_lock(eb); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 5228 | path->locks[level] = BTRFS_WRITE_LOCK; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5229 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5230 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5231 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5232 | &wc->refs[level], |
| 5233 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5234 | if (ret < 0) { |
| 5235 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5236 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5237 | return ret; |
| 5238 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5239 | BUG_ON(wc->refs[level] == 0); |
| 5240 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5241 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5242 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5243 | return 1; |
| 5244 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | /* wc->stage == DROP_REFERENCE */ |
| 5249 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5250 | |
| 5251 | if (wc->refs[level] == 1) { |
| 5252 | if (level == 0) { |
| 5253 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5254 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5255 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5256 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5257 | BUG_ON(ret); /* -ENOMEM */ |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5258 | if (is_fstree(root->root_key.objectid)) { |
| 5259 | ret = btrfs_qgroup_trace_leaf_items(trans, eb); |
| 5260 | if (ret) { |
| 5261 | btrfs_err_rl(fs_info, |
| 5262 | "error %d accounting leaf items, quota is out of sync, rescan required", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5263 | ret); |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5264 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5265 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5266 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5267 | /* make block locked assertion in btrfs_clean_tree_block happy */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5268 | if (!path->locks[level] && |
| 5269 | btrfs_header_generation(eb) == trans->transid) { |
| 5270 | btrfs_tree_lock(eb); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 5271 | path->locks[level] = BTRFS_WRITE_LOCK; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5272 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5273 | btrfs_clean_tree_block(eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | if (eb == root->node) { |
| 5277 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5278 | parent = eb->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5279 | else if (root->root_key.objectid != btrfs_header_owner(eb)) |
| 5280 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5281 | } else { |
| 5282 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5283 | parent = path->nodes[level + 1]->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5284 | else if (root->root_key.objectid != |
| 5285 | btrfs_header_owner(path->nodes[level + 1])) |
| 5286 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5287 | } |
| 5288 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5289 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5290 | out: |
| 5291 | wc->refs[level] = 0; |
| 5292 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5293 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5294 | |
| 5295 | owner_mismatch: |
| 5296 | btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", |
| 5297 | btrfs_header_owner(eb), root->root_key.objectid); |
| 5298 | return -EUCLEAN; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5299 | } |
| 5300 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5301 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5302 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5303 | struct btrfs_path *path, |
| 5304 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5305 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5306 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5307 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5308 | int ret; |
| 5309 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5310 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5311 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5312 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5313 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5314 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5315 | if (level == 0) |
| 5316 | break; |
| 5317 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5318 | if (path->slots[level] >= |
| 5319 | btrfs_header_nritems(path->nodes[level])) |
| 5320 | break; |
| 5321 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5322 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5323 | if (ret > 0) { |
| 5324 | path->slots[level]++; |
| 5325 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5326 | } else if (ret < 0) |
| 5327 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5328 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5329 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
| 5332 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5333 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5334 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5335 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5336 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5337 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5338 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5339 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5340 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5341 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5342 | while (level < max_level && path->nodes[level]) { |
| 5343 | wc->level = level; |
| 5344 | if (path->slots[level] + 1 < |
| 5345 | btrfs_header_nritems(path->nodes[level])) { |
| 5346 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5347 | return 0; |
| 5348 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5349 | ret = walk_up_proc(trans, root, path, wc); |
| 5350 | if (ret > 0) |
| 5351 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5352 | if (ret < 0) |
| 5353 | return ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5354 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5355 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5356 | btrfs_tree_unlock_rw(path->nodes[level], |
| 5357 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5358 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5359 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5360 | free_extent_buffer(path->nodes[level]); |
| 5361 | path->nodes[level] = NULL; |
| 5362 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5363 | } |
| 5364 | } |
| 5365 | return 1; |
| 5366 | } |
| 5367 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5368 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5369 | * drop a subvolume tree. |
| 5370 | * |
| 5371 | * this function traverses the tree freeing any blocks that only |
| 5372 | * referenced by the tree. |
| 5373 | * |
| 5374 | * when a shared tree block is found. this function decreases its |
| 5375 | * reference count by one. if update_ref is true, this function |
| 5376 | * also make sure backrefs for the shared block and all lower level |
| 5377 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5378 | * |
| 5379 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5380 | */ |
Nikolay Borisov | 0078a9f | 2020-03-10 11:43:51 +0200 | [diff] [blame] | 5381 | 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] | 5382 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5383 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5384 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5385 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5386 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5387 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5388 | struct walk_control *wc; |
| 5389 | struct btrfs_key key; |
| 5390 | int err = 0; |
| 5391 | int ret; |
| 5392 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5393 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5394 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 5395 | btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5396 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5397 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5398 | if (!path) { |
| 5399 | err = -ENOMEM; |
| 5400 | goto out; |
| 5401 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5402 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5403 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5404 | if (!wc) { |
| 5405 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5406 | err = -ENOMEM; |
| 5407 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5408 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5409 | |
Qu Wenruo | f3e3d9c | 2020-07-13 09:03:20 +0800 | [diff] [blame] | 5410 | /* |
| 5411 | * Use join to avoid potential EINTR from transaction start. See |
| 5412 | * wait_reserve_ticket and the whole reservation callchain. |
| 5413 | */ |
| 5414 | if (for_reloc) |
| 5415 | trans = btrfs_join_transaction(tree_root); |
| 5416 | else |
| 5417 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5418 | if (IS_ERR(trans)) { |
| 5419 | err = PTR_ERR(trans); |
| 5420 | goto out_free; |
| 5421 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 5422 | |
Josef Bacik | 0568e82 | 2018-11-30 11:52:14 -0500 | [diff] [blame] | 5423 | err = btrfs_run_delayed_items(trans); |
| 5424 | if (err) |
| 5425 | goto out_end_trans; |
| 5426 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 5427 | /* |
| 5428 | * This will help us catch people modifying the fs tree while we're |
| 5429 | * dropping it. It is unsafe to mess with the fs tree while it's being |
| 5430 | * dropped as we unlock the root node and parent nodes as we walk down |
| 5431 | * the tree, assuming nothing will change. If something does change |
| 5432 | * then we'll have stale information and drop references to blocks we've |
| 5433 | * already dropped. |
| 5434 | */ |
| 5435 | set_bit(BTRFS_ROOT_DELETING, &root->state); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5436 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5437 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5438 | path->nodes[level] = btrfs_lock_root_node(root); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5439 | path->slots[level] = 0; |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 5440 | path->locks[level] = BTRFS_WRITE_LOCK; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5441 | memset(&wc->update_progress, 0, |
| 5442 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5443 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5444 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5445 | memcpy(&wc->update_progress, &key, |
| 5446 | sizeof(wc->update_progress)); |
| 5447 | |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 5448 | level = btrfs_root_drop_level(root_item); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5449 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5450 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5451 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5452 | path->lowest_level = 0; |
| 5453 | if (ret < 0) { |
| 5454 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5455 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5456 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5457 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5458 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5459 | /* |
| 5460 | * unlock our path, this is safe because only this |
| 5461 | * function is allowed to delete this snapshot |
| 5462 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5463 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5464 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5465 | level = btrfs_header_level(root->node); |
| 5466 | while (1) { |
| 5467 | btrfs_tree_lock(path->nodes[level]); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 5468 | path->locks[level] = BTRFS_WRITE_LOCK; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5469 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5470 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5471 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5472 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5473 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5474 | if (ret < 0) { |
| 5475 | err = ret; |
| 5476 | goto out_end_trans; |
| 5477 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5478 | BUG_ON(wc->refs[level] == 0); |
| 5479 | |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 5480 | if (level == btrfs_root_drop_level(root_item)) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5481 | break; |
| 5482 | |
| 5483 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5484 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5485 | WARN_ON(wc->refs[level] != 1); |
| 5486 | level--; |
| 5487 | } |
| 5488 | } |
| 5489 | |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5490 | wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5491 | wc->level = level; |
| 5492 | wc->shared_level = -1; |
| 5493 | wc->stage = DROP_REFERENCE; |
| 5494 | wc->update_ref = update_ref; |
| 5495 | wc->keep_locks = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5496 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5497 | |
| 5498 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5499 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5500 | ret = walk_down_tree(trans, root, path, wc); |
| 5501 | if (ret < 0) { |
| 5502 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5503 | break; |
| 5504 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5505 | |
| 5506 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5507 | if (ret < 0) { |
| 5508 | err = ret; |
| 5509 | break; |
| 5510 | } |
| 5511 | |
| 5512 | if (ret > 0) { |
| 5513 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5514 | break; |
| 5515 | } |
| 5516 | |
| 5517 | if (wc->stage == DROP_REFERENCE) { |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5518 | wc->drop_level = wc->level; |
| 5519 | btrfs_node_key_to_cpu(path->nodes[wc->drop_level], |
| 5520 | &wc->drop_progress, |
| 5521 | path->slots[wc->drop_level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5522 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5523 | btrfs_cpu_key_to_disk(&root_item->drop_progress, |
| 5524 | &wc->drop_progress); |
David Sterba | c842268 | 2020-09-15 21:44:52 +0200 | [diff] [blame] | 5525 | btrfs_set_root_drop_level(root_item, wc->drop_level); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5526 | |
| 5527 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5528 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5529 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5530 | ret = btrfs_update_root(trans, tree_root, |
| 5531 | &root->root_key, |
| 5532 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5533 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5534 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5535 | err = ret; |
| 5536 | goto out_end_trans; |
| 5537 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5538 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5539 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5540 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5541 | btrfs_debug(fs_info, |
| 5542 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 5543 | err = -EAGAIN; |
| 5544 | goto out_free; |
| 5545 | } |
| 5546 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5547 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5548 | if (IS_ERR(trans)) { |
| 5549 | err = PTR_ERR(trans); |
| 5550 | goto out_free; |
| 5551 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5552 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5553 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5554 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5555 | if (err) |
| 5556 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5557 | |
Lu Fengqi | ab9ce7d | 2018-08-01 11:32:27 +0800 | [diff] [blame] | 5558 | ret = btrfs_del_root(trans, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5559 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5560 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | e19182c | 2017-12-04 13:11:45 -0500 | [diff] [blame] | 5561 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5562 | goto out_end_trans; |
| 5563 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5564 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5565 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5566 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 5567 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5568 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5569 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5570 | err = ret; |
| 5571 | goto out_end_trans; |
| 5572 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 5573 | /* if we fail to delete the orphan item this time |
| 5574 | * around, it'll get picked up the next time. |
| 5575 | * |
| 5576 | * The most common failure here is just -ENOENT. |
| 5577 | */ |
| 5578 | btrfs_del_orphan_item(trans, tree_root, |
| 5579 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5580 | } |
| 5581 | } |
| 5582 | |
Qu Wenruo | a3cf0e4 | 2020-07-14 09:12:20 +0800 | [diff] [blame] | 5583 | /* |
| 5584 | * This subvolume is going to be completely dropped, and won't be |
| 5585 | * recorded as dirty roots, thus pertrans meta rsv will not be freed at |
| 5586 | * commit transaction time. So free it here manually. |
| 5587 | */ |
| 5588 | btrfs_qgroup_convert_reserved_meta(root, INT_MAX); |
| 5589 | btrfs_qgroup_free_meta_all_pertrans(root); |
| 5590 | |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 5591 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 5592 | btrfs_add_dropped_root(trans, root); |
Josef Bacik | 8c38938 | 2020-02-14 16:11:42 -0500 | [diff] [blame] | 5593 | else |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 5594 | btrfs_put_root(root); |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5595 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5596 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5597 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5598 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5599 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5600 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5601 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5602 | /* |
| 5603 | * So if we need to stop dropping the snapshot for whatever reason we |
| 5604 | * need to make sure to add it back to the dead root list so that we |
| 5605 | * keep trying to do the work later. This also cleans up roots if we |
| 5606 | * don't have it in the radix (like when we recover after a power fail |
| 5607 | * or unmount) so we don't leak memory. |
| 5608 | */ |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 5609 | if (!for_reloc && !root_dropped) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5610 | btrfs_add_dead_root(root); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 5611 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5612 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5613 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5614 | /* |
| 5615 | * drop subtree rooted at tree block 'node'. |
| 5616 | * |
| 5617 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5618 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5619 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5620 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5621 | struct btrfs_root *root, |
| 5622 | struct extent_buffer *node, |
| 5623 | struct extent_buffer *parent) |
| 5624 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5625 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5626 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5627 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5628 | int level; |
| 5629 | int parent_level; |
| 5630 | int ret = 0; |
| 5631 | int wret; |
| 5632 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5633 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5634 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5635 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5636 | if (!path) |
| 5637 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5638 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5639 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5640 | if (!wc) { |
| 5641 | btrfs_free_path(path); |
| 5642 | return -ENOMEM; |
| 5643 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5644 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5645 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5646 | parent_level = btrfs_header_level(parent); |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 5647 | atomic_inc(&parent->refs); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5648 | path->nodes[parent_level] = parent; |
| 5649 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5650 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5651 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5652 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5653 | path->nodes[level] = node; |
| 5654 | path->slots[level] = 0; |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 5655 | path->locks[level] = BTRFS_WRITE_LOCK; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5656 | |
| 5657 | wc->refs[parent_level] = 1; |
| 5658 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5659 | wc->level = level; |
| 5660 | wc->shared_level = -1; |
| 5661 | wc->stage = DROP_REFERENCE; |
| 5662 | wc->update_ref = 0; |
| 5663 | wc->keep_locks = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5664 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5665 | |
| 5666 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5667 | wret = walk_down_tree(trans, root, path, wc); |
| 5668 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5669 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5670 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5671 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5672 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5673 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5674 | if (wret < 0) |
| 5675 | ret = wret; |
| 5676 | if (wret != 0) |
| 5677 | break; |
| 5678 | } |
| 5679 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5680 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5681 | btrfs_free_path(path); |
| 5682 | return ret; |
| 5683 | } |
| 5684 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5685 | /* |
| 5686 | * 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] | 5687 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5688 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5689 | 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] | 5690 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 5691 | struct btrfs_block_group *block_group; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5692 | u64 free_bytes = 0; |
| 5693 | int factor; |
| 5694 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5695 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5696 | if (list_empty(&sinfo->ro_bgs)) |
| 5697 | return 0; |
| 5698 | |
| 5699 | spin_lock(&sinfo->lock); |
| 5700 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5701 | spin_lock(&block_group->lock); |
| 5702 | |
| 5703 | if (!block_group->ro) { |
| 5704 | spin_unlock(&block_group->lock); |
| 5705 | continue; |
| 5706 | } |
| 5707 | |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 5708 | factor = btrfs_bg_type_to_factor(block_group->flags); |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 5709 | free_bytes += (block_group->length - |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 5710 | block_group->used) * factor; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5711 | |
| 5712 | spin_unlock(&block_group->lock); |
| 5713 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5714 | spin_unlock(&sinfo->lock); |
| 5715 | |
| 5716 | return free_bytes; |
| 5717 | } |
| 5718 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5719 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 5720 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5721 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5722 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5723 | } |
| 5724 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5725 | /* |
| 5726 | * It used to be that old block groups would be left around forever. |
| 5727 | * Iterating over them would be enough to trim unused space. Since we |
| 5728 | * now automatically remove them, we also need to iterate over unallocated |
| 5729 | * space. |
| 5730 | * |
| 5731 | * We don't want a transaction for this since the discard may take a |
| 5732 | * substantial amount of time. We don't require that a transaction be |
| 5733 | * running, but we do need to take a running transaction into account |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5734 | * to ensure that we're not discarding chunks that were released or |
| 5735 | * allocated in the current transaction. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5736 | * |
| 5737 | * Holding the chunks lock will prevent other threads from allocating |
| 5738 | * or releasing chunks, but it won't prevent a running transaction |
| 5739 | * from committing and releasing the memory that the pending chunks |
| 5740 | * 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] | 5741 | * transaction and hold the commit root sem. We only need to hold |
| 5742 | * it while performing the free space search since we have already |
| 5743 | * held back allocations. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5744 | */ |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5745 | static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5746 | { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5747 | u64 start = SZ_1M, len = 0, end = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5748 | int ret; |
| 5749 | |
| 5750 | *trimmed = 0; |
| 5751 | |
Jeff Mahoney | 0be88e3 | 2018-09-06 17:18:15 -0400 | [diff] [blame] | 5752 | /* Discard not supported = nothing to do. */ |
| 5753 | if (!blk_queue_discard(bdev_get_queue(device->bdev))) |
| 5754 | return 0; |
| 5755 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5756 | /* Not writable = nothing to do. */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5757 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5758 | return 0; |
| 5759 | |
| 5760 | /* No free space = nothing to do. */ |
| 5761 | if (device->total_bytes <= device->bytes_used) |
| 5762 | return 0; |
| 5763 | |
| 5764 | ret = 0; |
| 5765 | |
| 5766 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 5767 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5768 | u64 bytes; |
| 5769 | |
| 5770 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 5771 | if (ret) |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5772 | break; |
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 | find_first_clear_extent_bit(&device->alloc_state, start, |
| 5775 | &start, &end, |
| 5776 | CHUNK_TRIMMED | CHUNK_ALLOCATED); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5777 | |
Qu Wenruo | c57dd1f | 2020-07-31 19:29:11 +0800 | [diff] [blame] | 5778 | /* Check if there are any CHUNK_* bits left */ |
| 5779 | if (start > device->total_bytes) { |
| 5780 | WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); |
| 5781 | btrfs_warn_in_rcu(fs_info, |
| 5782 | "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu", |
| 5783 | start, end - start + 1, |
| 5784 | rcu_str_deref(device->name), |
| 5785 | device->total_bytes); |
| 5786 | mutex_unlock(&fs_info->chunk_mutex); |
| 5787 | ret = 0; |
| 5788 | break; |
| 5789 | } |
| 5790 | |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5791 | /* Ensure we skip the reserved area in the first 1M */ |
| 5792 | start = max_t(u64, start, SZ_1M); |
| 5793 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5794 | /* |
| 5795 | * If find_first_clear_extent_bit find a range that spans the |
| 5796 | * end of the device it will set end to -1, in this case it's up |
| 5797 | * to the caller to trim the value to the size of the device. |
| 5798 | */ |
| 5799 | end = min(end, device->total_bytes - 1); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5800 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5801 | len = end - start + 1; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5802 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5803 | /* We didn't find any extents */ |
| 5804 | if (!len) { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5805 | mutex_unlock(&fs_info->chunk_mutex); |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5806 | ret = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5807 | break; |
| 5808 | } |
| 5809 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5810 | ret = btrfs_issue_discard(device->bdev, start, len, |
| 5811 | &bytes); |
| 5812 | if (!ret) |
| 5813 | set_extent_bits(&device->alloc_state, start, |
| 5814 | start + bytes - 1, |
| 5815 | CHUNK_TRIMMED); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5816 | mutex_unlock(&fs_info->chunk_mutex); |
| 5817 | |
| 5818 | if (ret) |
| 5819 | break; |
| 5820 | |
| 5821 | start += len; |
| 5822 | *trimmed += bytes; |
| 5823 | |
| 5824 | if (fatal_signal_pending(current)) { |
| 5825 | ret = -ERESTARTSYS; |
| 5826 | break; |
| 5827 | } |
| 5828 | |
| 5829 | cond_resched(); |
| 5830 | } |
| 5831 | |
| 5832 | return ret; |
| 5833 | } |
| 5834 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5835 | /* |
| 5836 | * Trim the whole filesystem by: |
| 5837 | * 1) trimming the free space in each block group |
| 5838 | * 2) trimming the unallocated space on each device |
| 5839 | * |
| 5840 | * This will also continue trimming even if a block group or device encounters |
| 5841 | * an error. The return value will be the last error, or 0 if nothing bad |
| 5842 | * happens. |
| 5843 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5844 | 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] | 5845 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 5846 | struct btrfs_block_group *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5847 | struct btrfs_device *device; |
| 5848 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5849 | u64 group_trimmed; |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5850 | u64 range_end = U64_MAX; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5851 | u64 start; |
| 5852 | u64 end; |
| 5853 | u64 trimmed = 0; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5854 | u64 bg_failed = 0; |
| 5855 | u64 dev_failed = 0; |
| 5856 | int bg_ret = 0; |
| 5857 | int dev_ret = 0; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5858 | int ret = 0; |
| 5859 | |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5860 | /* |
| 5861 | * Check range overflow if range->len is set. |
| 5862 | * The default range->len is U64_MAX. |
| 5863 | */ |
| 5864 | if (range->len != U64_MAX && |
| 5865 | check_add_overflow(range->start, range->len, &range_end)) |
| 5866 | return -EINVAL; |
| 5867 | |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 5868 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 5869 | for (; cache; cache = btrfs_next_block_group(cache)) { |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 5870 | if (cache->start >= range_end) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5871 | btrfs_put_block_group(cache); |
| 5872 | break; |
| 5873 | } |
| 5874 | |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 5875 | start = max(range->start, cache->start); |
| 5876 | end = min(range_end, cache->start + cache->length); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5877 | |
| 5878 | if (end - start >= range->minlen) { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 5879 | if (!btrfs_block_group_done(cache)) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5880 | ret = btrfs_cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5881 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5882 | bg_failed++; |
| 5883 | bg_ret = ret; |
| 5884 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5885 | } |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5886 | ret = btrfs_wait_block_group_cache_done(cache); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5887 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5888 | bg_failed++; |
| 5889 | bg_ret = ret; |
| 5890 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5891 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5892 | } |
| 5893 | ret = btrfs_trim_block_group(cache, |
| 5894 | &group_trimmed, |
| 5895 | start, |
| 5896 | end, |
| 5897 | range->minlen); |
| 5898 | |
| 5899 | trimmed += group_trimmed; |
| 5900 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5901 | bg_failed++; |
| 5902 | bg_ret = ret; |
| 5903 | continue; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5904 | } |
| 5905 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5906 | } |
| 5907 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5908 | if (bg_failed) |
| 5909 | btrfs_warn(fs_info, |
| 5910 | "failed to trim %llu block group(s), last error %d", |
| 5911 | bg_failed, bg_ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5912 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | d4e329d | 2018-09-06 17:18:14 -0400 | [diff] [blame] | 5913 | devices = &fs_info->fs_devices->devices; |
| 5914 | list_for_each_entry(device, devices, dev_list) { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5915 | ret = btrfs_trim_free_extents(device, &group_trimmed); |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5916 | if (ret) { |
| 5917 | dev_failed++; |
| 5918 | dev_ret = ret; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5919 | break; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5920 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5921 | |
| 5922 | trimmed += group_trimmed; |
| 5923 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5924 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5925 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5926 | if (dev_failed) |
| 5927 | btrfs_warn(fs_info, |
| 5928 | "failed to trim %llu device(s), last error %d", |
| 5929 | dev_failed, dev_ret); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5930 | range->len = trimmed; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5931 | if (bg_ret) |
| 5932 | return bg_ret; |
| 5933 | return dev_ret; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5934 | } |