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" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 28 | #include "math.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 29 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 30 | #include "qgroup.h" |
Josef Bacik | fd708b8 | 2017-09-29 15:43:50 -0400 | [diff] [blame] | 31 | #include "ref-verify.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 32 | #include "space-info.h" |
Josef Bacik | d12ffdd | 2019-06-19 13:47:17 -0400 | [diff] [blame] | 33 | #include "block-rsv.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 34 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 35 | #include "block-group.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 36 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 37 | #undef SCRAMBLE_DELAYED_REFS |
| 38 | |
Qu Wenruo | 9f9b8e8 | 2018-10-24 20:24:01 +0800 | [diff] [blame] | 39 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 40 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 41 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 42 | u64 root_objectid, u64 owner_objectid, |
| 43 | u64 owner_offset, int refs_to_drop, |
| 44 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 45 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 46 | struct extent_buffer *leaf, |
| 47 | struct btrfs_extent_item *ei); |
| 48 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 49 | u64 parent, u64 root_objectid, |
| 50 | u64 flags, u64 owner, u64 offset, |
| 51 | struct btrfs_key *ins, int ref_mod); |
| 52 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 53 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 54 | struct btrfs_delayed_extent_op *extent_op); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 55 | static int find_next_key(struct btrfs_path *path, int level, |
| 56 | struct btrfs_key *key); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 57 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 58 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 59 | { |
| 60 | return (cache->flags & bits) == bits; |
| 61 | } |
| 62 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 63 | int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info, |
| 64 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 65 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 66 | u64 end = start + num_bytes - 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 67 | set_extent_bits(&fs_info->freed_extents[0], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 68 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 69 | set_extent_bits(&fs_info->freed_extents[1], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 70 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 71 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 74 | void btrfs_free_excluded_extents(struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 75 | { |
Nikolay Borisov | 9e715da | 2018-06-20 15:49:08 +0300 | [diff] [blame] | 76 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 77 | u64 start, end; |
| 78 | |
| 79 | start = cache->key.objectid; |
| 80 | end = start + cache->key.offset - 1; |
| 81 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 82 | clear_extent_bits(&fs_info->freed_extents[0], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 83 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 84 | clear_extent_bits(&fs_info->freed_extents[1], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 85 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 88 | static u64 generic_ref_to_space_flags(struct btrfs_ref *ref) |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 89 | { |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 90 | if (ref->type == BTRFS_REF_METADATA) { |
| 91 | if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 92 | return BTRFS_BLOCK_GROUP_SYSTEM; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 93 | else |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 94 | return BTRFS_BLOCK_GROUP_METADATA; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 95 | } |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 96 | return BTRFS_BLOCK_GROUP_DATA; |
| 97 | } |
| 98 | |
| 99 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 100 | struct btrfs_ref *ref) |
| 101 | { |
| 102 | struct btrfs_space_info *space_info; |
| 103 | u64 flags = generic_ref_to_space_flags(ref); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 104 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 105 | space_info = btrfs_find_space_info(fs_info, flags); |
Omar Sandoval | 55e8196 | 2017-06-06 16:45:27 -0700 | [diff] [blame] | 106 | ASSERT(space_info); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 107 | percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len, |
| 108 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 109 | } |
| 110 | |
| 111 | static void sub_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 112 | struct btrfs_ref *ref) |
| 113 | { |
| 114 | struct btrfs_space_info *space_info; |
| 115 | u64 flags = generic_ref_to_space_flags(ref); |
| 116 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 117 | space_info = btrfs_find_space_info(fs_info, flags); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 118 | ASSERT(space_info); |
| 119 | percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len, |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 120 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 123 | /* 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] | 124 | 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] | 125 | { |
| 126 | int ret; |
| 127 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 128 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 129 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 130 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 131 | if (!path) |
| 132 | return -ENOMEM; |
| 133 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 134 | key.objectid = start; |
| 135 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 136 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 137 | 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] | 138 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 142 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 143 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 144 | * |
| 145 | * the head node for delayed ref is used to store the sum of all the |
| 146 | * reference count modifications queued up in the rbtree. the head |
| 147 | * node may also store the extent flags to set. This way you can check |
| 148 | * to see what the reference count and extent flags would be if all of |
| 149 | * the delayed refs are not processed. |
| 150 | */ |
| 151 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 152 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 153 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 154 | { |
| 155 | struct btrfs_delayed_ref_head *head; |
| 156 | struct btrfs_delayed_ref_root *delayed_refs; |
| 157 | struct btrfs_path *path; |
| 158 | struct btrfs_extent_item *ei; |
| 159 | struct extent_buffer *leaf; |
| 160 | struct btrfs_key key; |
| 161 | u32 item_size; |
| 162 | u64 num_refs; |
| 163 | u64 extent_flags; |
| 164 | int ret; |
| 165 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 166 | /* |
| 167 | * If we don't have skinny metadata, don't bother doing anything |
| 168 | * different |
| 169 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 170 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 171 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 172 | metadata = 0; |
| 173 | } |
| 174 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 175 | path = btrfs_alloc_path(); |
| 176 | if (!path) |
| 177 | return -ENOMEM; |
| 178 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 179 | if (!trans) { |
| 180 | path->skip_locking = 1; |
| 181 | path->search_commit_root = 1; |
| 182 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 183 | |
| 184 | search_again: |
| 185 | key.objectid = bytenr; |
| 186 | key.offset = offset; |
| 187 | if (metadata) |
| 188 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 189 | else |
| 190 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 191 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 192 | 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] | 193 | if (ret < 0) |
| 194 | goto out_free; |
| 195 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 196 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 197 | if (path->slots[0]) { |
| 198 | path->slots[0]--; |
| 199 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 200 | path->slots[0]); |
| 201 | if (key.objectid == bytenr && |
| 202 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 203 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 204 | ret = 0; |
| 205 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 208 | if (ret == 0) { |
| 209 | leaf = path->nodes[0]; |
| 210 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 211 | if (item_size >= sizeof(*ei)) { |
| 212 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 213 | struct btrfs_extent_item); |
| 214 | num_refs = btrfs_extent_refs(leaf, ei); |
| 215 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 216 | } else { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 217 | ret = -EINVAL; |
| 218 | btrfs_print_v0_err(fs_info); |
| 219 | if (trans) |
| 220 | btrfs_abort_transaction(trans, ret); |
| 221 | else |
| 222 | btrfs_handle_fs_error(fs_info, ret, NULL); |
| 223 | |
| 224 | goto out_free; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 225 | } |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 226 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 227 | BUG_ON(num_refs == 0); |
| 228 | } else { |
| 229 | num_refs = 0; |
| 230 | extent_flags = 0; |
| 231 | ret = 0; |
| 232 | } |
| 233 | |
| 234 | if (!trans) |
| 235 | goto out; |
| 236 | |
| 237 | delayed_refs = &trans->transaction->delayed_refs; |
| 238 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 239 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 240 | if (head) { |
| 241 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 242 | refcount_inc(&head->refs); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 243 | spin_unlock(&delayed_refs->lock); |
| 244 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 245 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 246 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Mutex was contended, block until it's released and try |
| 249 | * again |
| 250 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 251 | mutex_lock(&head->mutex); |
| 252 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 253 | btrfs_put_delayed_ref_head(head); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 254 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 255 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 256 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 257 | if (head->extent_op && head->extent_op->update_flags) |
| 258 | extent_flags |= head->extent_op->flags_to_set; |
| 259 | else |
| 260 | BUG_ON(num_refs == 0); |
| 261 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 262 | num_refs += head->ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 263 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 264 | mutex_unlock(&head->mutex); |
| 265 | } |
| 266 | spin_unlock(&delayed_refs->lock); |
| 267 | out: |
| 268 | WARN_ON(num_refs == 0); |
| 269 | if (refs) |
| 270 | *refs = num_refs; |
| 271 | if (flags) |
| 272 | *flags = extent_flags; |
| 273 | out_free: |
| 274 | btrfs_free_path(path); |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 279 | * Back reference rules. Back refs have three main goals: |
| 280 | * |
| 281 | * 1) differentiate between all holders of references to an extent so that |
| 282 | * when a reference is dropped we can make sure it was a valid reference |
| 283 | * before freeing the extent. |
| 284 | * |
| 285 | * 2) Provide enough information to quickly find the holders of an extent |
| 286 | * if we notice a given block is corrupted or bad. |
| 287 | * |
| 288 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 289 | * maintenance. This is actually the same as #2, but with a slightly |
| 290 | * different use case. |
| 291 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 292 | * There are two kinds of back refs. The implicit back refs is optimized |
| 293 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 294 | * back refs of this kind provide information about the block's owner tree |
| 295 | * and the pointer's key. These information allow us to find the block by |
| 296 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 297 | * referenced by their owner trees. The location of tree block is recorded |
| 298 | * in the back refs. Actually the full back refs is generic, and can be |
| 299 | * used in all cases the implicit back refs is used. The major shortcoming |
| 300 | * of the full back refs is its overhead. Every time a tree block gets |
| 301 | * COWed, we have to update back refs entry for all pointers in it. |
| 302 | * |
| 303 | * For a newly allocated tree block, we use implicit back refs for |
| 304 | * pointers in it. This means most tree related operations only involve |
| 305 | * implicit back refs. For a tree block created in old transaction, the |
| 306 | * only way to drop a reference to it is COW it. So we can detect the |
| 307 | * event that tree block loses its owner tree's reference and do the |
| 308 | * back refs conversion. |
| 309 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 310 | * 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] | 311 | * |
| 312 | * The reference count of the block is one and the tree is the block's |
| 313 | * owner tree. Nothing to do in this case. |
| 314 | * |
| 315 | * The reference count of the block is one and the tree is not the |
| 316 | * block's owner tree. In this case, full back refs is used for pointers |
| 317 | * in the block. Remove these full back refs, add implicit back refs for |
| 318 | * every pointers in the new block. |
| 319 | * |
| 320 | * The reference count of the block is greater than one and the tree is |
| 321 | * the block's owner tree. In this case, implicit back refs is used for |
| 322 | * pointers in the block. Add full back refs for every pointers in the |
| 323 | * block, increase lower level extents' reference counts. The original |
| 324 | * implicit back refs are entailed to the new block. |
| 325 | * |
| 326 | * The reference count of the block is greater than one and the tree is |
| 327 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 328 | * the new block, increase lower level extents' reference count. |
| 329 | * |
| 330 | * Back Reference Key composing: |
| 331 | * |
| 332 | * The key objectid corresponds to the first byte in the extent, |
| 333 | * The key type is used to differentiate between types of back refs. |
| 334 | * There are different meanings of the key offset for different types |
| 335 | * of back refs. |
| 336 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 337 | * File extents can be referenced by: |
| 338 | * |
| 339 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 340 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 341 | * - different offsets inside a file (bookend extents in file.c) |
| 342 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 343 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 344 | * |
| 345 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 346 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 347 | * - original offset in the file |
| 348 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 349 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 350 | * The key offset for the implicit back refs is hash of the first |
| 351 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 352 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 353 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 354 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 355 | * - number of pointers in 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 | * The key offset for the implicit back refs is the first byte of |
| 358 | * the tree leaf |
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 | * When a file extent is allocated, The implicit back refs is used. |
| 361 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 362 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 363 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 364 | * |
| 365 | * When a file extent is removed file truncation, we find the |
| 366 | * corresponding implicit back refs and check the following fields: |
| 367 | * |
| 368 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 369 | * |
| 370 | * Btree extents can be referenced by: |
| 371 | * |
| 372 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 373 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 374 | * Both the implicit back refs and the full back refs for tree blocks |
| 375 | * only consist of key. The key offset for the implicit back refs is |
| 376 | * objectid of block's owner tree. The key offset for the full back refs |
| 377 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 378 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 379 | * When implicit back refs is used, information about the lowest key and |
| 380 | * level of the tree block are required. These information are stored in |
| 381 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 382 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 383 | |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 384 | /* |
| 385 | * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 386 | * is_data == BTRFS_REF_TYPE_DATA, data type is requiried, |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 387 | * is_data == BTRFS_REF_TYPE_ANY, either type is OK. |
| 388 | */ |
| 389 | int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, |
| 390 | struct btrfs_extent_inline_ref *iref, |
| 391 | enum btrfs_inline_ref_type is_data) |
| 392 | { |
| 393 | int type = btrfs_extent_inline_ref_type(eb, iref); |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 394 | u64 offset = btrfs_extent_inline_ref_offset(eb, iref); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 395 | |
| 396 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 397 | type == BTRFS_SHARED_BLOCK_REF_KEY || |
| 398 | type == BTRFS_SHARED_DATA_REF_KEY || |
| 399 | type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 400 | if (is_data == BTRFS_REF_TYPE_BLOCK) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 401 | if (type == BTRFS_TREE_BLOCK_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 402 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 403 | if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 404 | ASSERT(eb->fs_info); |
| 405 | /* |
| 406 | * Every shared one has parent tree |
| 407 | * block, which must be aligned to |
| 408 | * nodesize. |
| 409 | */ |
| 410 | if (offset && |
| 411 | IS_ALIGNED(offset, eb->fs_info->nodesize)) |
| 412 | return type; |
| 413 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 414 | } else if (is_data == BTRFS_REF_TYPE_DATA) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 415 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 416 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 417 | if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 418 | ASSERT(eb->fs_info); |
| 419 | /* |
| 420 | * Every shared one has parent tree |
| 421 | * block, which must be aligned to |
| 422 | * nodesize. |
| 423 | */ |
| 424 | if (offset && |
| 425 | IS_ALIGNED(offset, eb->fs_info->nodesize)) |
| 426 | return type; |
| 427 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 428 | } else { |
| 429 | ASSERT(is_data == BTRFS_REF_TYPE_ANY); |
| 430 | return type; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | btrfs_print_leaf((struct extent_buffer *)eb); |
| 435 | btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d", |
| 436 | eb->start, type); |
| 437 | WARN_ON(1); |
| 438 | |
| 439 | return BTRFS_REF_TYPE_INVALID; |
| 440 | } |
| 441 | |
Qu Wenruo | 0785a9a | 2019-08-09 09:24:24 +0800 | [diff] [blame] | 442 | u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 443 | { |
| 444 | u32 high_crc = ~(u32)0; |
| 445 | u32 low_crc = ~(u32)0; |
| 446 | __le64 lenum; |
| 447 | |
| 448 | lenum = cpu_to_le64(root_objectid); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 449 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 450 | lenum = cpu_to_le64(owner); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 451 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 452 | lenum = cpu_to_le64(offset); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 453 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 454 | |
| 455 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 456 | } |
| 457 | |
| 458 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 459 | struct btrfs_extent_data_ref *ref) |
| 460 | { |
| 461 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 462 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 463 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 464 | } |
| 465 | |
| 466 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 467 | struct btrfs_extent_data_ref *ref, |
| 468 | u64 root_objectid, u64 owner, u64 offset) |
| 469 | { |
| 470 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 471 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 472 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 473 | return 0; |
| 474 | return 1; |
| 475 | } |
| 476 | |
| 477 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 478 | struct btrfs_path *path, |
| 479 | u64 bytenr, u64 parent, |
| 480 | u64 root_objectid, |
| 481 | u64 owner, u64 offset) |
| 482 | { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 483 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 484 | struct btrfs_key key; |
| 485 | struct btrfs_extent_data_ref *ref; |
| 486 | struct extent_buffer *leaf; |
| 487 | u32 nritems; |
| 488 | int ret; |
| 489 | int recow; |
| 490 | int err = -ENOENT; |
| 491 | |
| 492 | key.objectid = bytenr; |
| 493 | if (parent) { |
| 494 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 495 | key.offset = parent; |
| 496 | } else { |
| 497 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 498 | key.offset = hash_extent_data_ref(root_objectid, |
| 499 | owner, offset); |
| 500 | } |
| 501 | again: |
| 502 | recow = 0; |
| 503 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 504 | if (ret < 0) { |
| 505 | err = ret; |
| 506 | goto fail; |
| 507 | } |
| 508 | |
| 509 | if (parent) { |
| 510 | if (!ret) |
| 511 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 512 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 516 | nritems = btrfs_header_nritems(leaf); |
| 517 | while (1) { |
| 518 | if (path->slots[0] >= nritems) { |
| 519 | ret = btrfs_next_leaf(root, path); |
| 520 | if (ret < 0) |
| 521 | err = ret; |
| 522 | if (ret) |
| 523 | goto fail; |
| 524 | |
| 525 | leaf = path->nodes[0]; |
| 526 | nritems = btrfs_header_nritems(leaf); |
| 527 | recow = 1; |
| 528 | } |
| 529 | |
| 530 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 531 | if (key.objectid != bytenr || |
| 532 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 533 | goto fail; |
| 534 | |
| 535 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 536 | struct btrfs_extent_data_ref); |
| 537 | |
| 538 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 539 | owner, offset)) { |
| 540 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 541 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 542 | goto again; |
| 543 | } |
| 544 | err = 0; |
| 545 | break; |
| 546 | } |
| 547 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 548 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 549 | fail: |
| 550 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 551 | } |
| 552 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 553 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 554 | struct btrfs_path *path, |
| 555 | u64 bytenr, u64 parent, |
| 556 | u64 root_objectid, u64 owner, |
| 557 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 558 | { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 559 | struct btrfs_root *root = trans->fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 560 | struct btrfs_key key; |
| 561 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 562 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 563 | u32 num_refs; |
| 564 | int ret; |
| 565 | |
| 566 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 567 | if (parent) { |
| 568 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 569 | key.offset = parent; |
| 570 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 571 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 572 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 573 | key.offset = hash_extent_data_ref(root_objectid, |
| 574 | owner, offset); |
| 575 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 576 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 577 | |
| 578 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 579 | if (ret && ret != -EEXIST) |
| 580 | goto fail; |
| 581 | |
| 582 | leaf = path->nodes[0]; |
| 583 | if (parent) { |
| 584 | struct btrfs_shared_data_ref *ref; |
| 585 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 586 | struct btrfs_shared_data_ref); |
| 587 | if (ret == 0) { |
| 588 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 589 | } else { |
| 590 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 591 | num_refs += refs_to_add; |
| 592 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 593 | } |
| 594 | } else { |
| 595 | struct btrfs_extent_data_ref *ref; |
| 596 | while (ret == -EEXIST) { |
| 597 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 598 | struct btrfs_extent_data_ref); |
| 599 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 600 | owner, offset)) |
| 601 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 602 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 603 | key.offset++; |
| 604 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 605 | size); |
| 606 | if (ret && ret != -EEXIST) |
| 607 | goto fail; |
| 608 | |
| 609 | leaf = path->nodes[0]; |
| 610 | } |
| 611 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 612 | struct btrfs_extent_data_ref); |
| 613 | if (ret == 0) { |
| 614 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 615 | root_objectid); |
| 616 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 617 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 618 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 619 | } else { |
| 620 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 621 | num_refs += refs_to_add; |
| 622 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 623 | } |
| 624 | } |
| 625 | btrfs_mark_buffer_dirty(leaf); |
| 626 | ret = 0; |
| 627 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 628 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 629 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 630 | } |
| 631 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 632 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 633 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 634 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 635 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 636 | struct btrfs_key key; |
| 637 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 638 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 639 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 640 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 641 | int ret = 0; |
| 642 | |
| 643 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 644 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 645 | |
| 646 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 647 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 648 | struct btrfs_extent_data_ref); |
| 649 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 650 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 651 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 652 | struct btrfs_shared_data_ref); |
| 653 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 654 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 655 | btrfs_print_v0_err(trans->fs_info); |
| 656 | btrfs_abort_transaction(trans, -EINVAL); |
| 657 | return -EINVAL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 658 | } else { |
| 659 | BUG(); |
| 660 | } |
| 661 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 662 | BUG_ON(num_refs < refs_to_drop); |
| 663 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 664 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 665 | if (num_refs == 0) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 666 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 667 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 668 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 669 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 670 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 671 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 672 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 673 | btrfs_mark_buffer_dirty(leaf); |
| 674 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 675 | return ret; |
| 676 | } |
| 677 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 678 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 679 | struct btrfs_extent_inline_ref *iref) |
| 680 | { |
| 681 | struct btrfs_key key; |
| 682 | struct extent_buffer *leaf; |
| 683 | struct btrfs_extent_data_ref *ref1; |
| 684 | struct btrfs_shared_data_ref *ref2; |
| 685 | u32 num_refs = 0; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 686 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 687 | |
| 688 | leaf = path->nodes[0]; |
| 689 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 690 | |
| 691 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 692 | if (iref) { |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 693 | /* |
| 694 | * If type is invalid, we should have bailed out earlier than |
| 695 | * this call. |
| 696 | */ |
| 697 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 698 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
| 699 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 700 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 701 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 702 | } else { |
| 703 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 704 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 705 | } |
| 706 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 707 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 708 | struct btrfs_extent_data_ref); |
| 709 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 710 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 711 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 712 | struct btrfs_shared_data_ref); |
| 713 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 714 | } else { |
| 715 | WARN_ON(1); |
| 716 | } |
| 717 | return num_refs; |
| 718 | } |
| 719 | |
| 720 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 721 | struct btrfs_path *path, |
| 722 | u64 bytenr, u64 parent, |
| 723 | u64 root_objectid) |
| 724 | { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 725 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 726 | struct btrfs_key key; |
| 727 | int ret; |
| 728 | |
| 729 | key.objectid = bytenr; |
| 730 | if (parent) { |
| 731 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 732 | key.offset = parent; |
| 733 | } else { |
| 734 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 735 | key.offset = root_objectid; |
| 736 | } |
| 737 | |
| 738 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 739 | if (ret > 0) |
| 740 | ret = -ENOENT; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 745 | struct btrfs_path *path, |
| 746 | u64 bytenr, u64 parent, |
| 747 | u64 root_objectid) |
| 748 | { |
| 749 | struct btrfs_key key; |
| 750 | int ret; |
| 751 | |
| 752 | key.objectid = bytenr; |
| 753 | if (parent) { |
| 754 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 755 | key.offset = parent; |
| 756 | } else { |
| 757 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 758 | key.offset = root_objectid; |
| 759 | } |
| 760 | |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 761 | ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 762 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 763 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 764 | return ret; |
| 765 | } |
| 766 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 767 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 768 | { |
| 769 | int type; |
| 770 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 771 | if (parent > 0) |
| 772 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 773 | else |
| 774 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 775 | } else { |
| 776 | if (parent > 0) |
| 777 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 778 | else |
| 779 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 780 | } |
| 781 | return type; |
| 782 | } |
| 783 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 784 | static int find_next_key(struct btrfs_path *path, int level, |
| 785 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 786 | |
| 787 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 788 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 789 | if (!path->nodes[level]) |
| 790 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 791 | if (path->slots[level] + 1 >= |
| 792 | btrfs_header_nritems(path->nodes[level])) |
| 793 | continue; |
| 794 | if (level == 0) |
| 795 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 796 | path->slots[level] + 1); |
| 797 | else |
| 798 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 799 | path->slots[level] + 1); |
| 800 | return 0; |
| 801 | } |
| 802 | return 1; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 807 | * to the address of inline back ref, and 0 is returned. |
| 808 | * |
| 809 | * if back ref isn't found, *ref_ret is set to the address where it |
| 810 | * should be inserted, and -ENOENT is returned. |
| 811 | * |
| 812 | * if insert is true and there are too many inline back refs, the path |
| 813 | * points to the extent item, and -EAGAIN is returned. |
| 814 | * |
| 815 | * NOTE: inline back refs are ordered in the same way that back ref |
| 816 | * items in the tree are ordered. |
| 817 | */ |
| 818 | static noinline_for_stack |
| 819 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 820 | struct btrfs_path *path, |
| 821 | struct btrfs_extent_inline_ref **ref_ret, |
| 822 | u64 bytenr, u64 num_bytes, |
| 823 | u64 parent, u64 root_objectid, |
| 824 | u64 owner, u64 offset, int insert) |
| 825 | { |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 826 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 827 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 828 | struct btrfs_key key; |
| 829 | struct extent_buffer *leaf; |
| 830 | struct btrfs_extent_item *ei; |
| 831 | struct btrfs_extent_inline_ref *iref; |
| 832 | u64 flags; |
| 833 | u64 item_size; |
| 834 | unsigned long ptr; |
| 835 | unsigned long end; |
| 836 | int extra_size; |
| 837 | int type; |
| 838 | int want; |
| 839 | int ret; |
| 840 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 841 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 842 | int needed; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 843 | |
| 844 | key.objectid = bytenr; |
| 845 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 846 | key.offset = num_bytes; |
| 847 | |
| 848 | want = extent_ref_type(parent, owner); |
| 849 | if (insert) { |
| 850 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 851 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 852 | } else |
| 853 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 854 | |
| 855 | /* |
Nikolay Borisov | 16d1c06 | 2018-06-18 14:59:26 +0300 | [diff] [blame] | 856 | * Owner is our level, so we can just add one to get the level for the |
| 857 | * block we are interested in. |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 858 | */ |
| 859 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 860 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 861 | key.offset = owner; |
| 862 | } |
| 863 | |
| 864 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 865 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 866 | if (ret < 0) { |
| 867 | err = ret; |
| 868 | goto out; |
| 869 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 870 | |
| 871 | /* |
| 872 | * We may be a newly converted file system which still has the old fat |
| 873 | * extent entries for metadata, so try and see if we have one of those. |
| 874 | */ |
| 875 | if (ret > 0 && skinny_metadata) { |
| 876 | skinny_metadata = false; |
| 877 | if (path->slots[0]) { |
| 878 | path->slots[0]--; |
| 879 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 880 | path->slots[0]); |
| 881 | if (key.objectid == bytenr && |
| 882 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 883 | key.offset == num_bytes) |
| 884 | ret = 0; |
| 885 | } |
| 886 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 887 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 888 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 889 | key.offset = num_bytes; |
| 890 | btrfs_release_path(path); |
| 891 | goto again; |
| 892 | } |
| 893 | } |
| 894 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 895 | if (ret && !insert) { |
| 896 | err = -ENOENT; |
| 897 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 898 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 899 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 900 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 901 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 902 | |
| 903 | leaf = path->nodes[0]; |
| 904 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 905 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 906 | err = -EINVAL; |
| 907 | btrfs_print_v0_err(fs_info); |
| 908 | btrfs_abort_transaction(trans, err); |
| 909 | goto out; |
| 910 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 911 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 912 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 913 | flags = btrfs_extent_flags(leaf, ei); |
| 914 | |
| 915 | ptr = (unsigned long)(ei + 1); |
| 916 | end = (unsigned long)ei + item_size; |
| 917 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 918 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 919 | ptr += sizeof(struct btrfs_tree_block_info); |
| 920 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 921 | } |
| 922 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 923 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) |
| 924 | needed = BTRFS_REF_TYPE_DATA; |
| 925 | else |
| 926 | needed = BTRFS_REF_TYPE_BLOCK; |
| 927 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 928 | err = -ENOENT; |
| 929 | while (1) { |
| 930 | if (ptr >= end) { |
| 931 | WARN_ON(ptr > end); |
| 932 | break; |
| 933 | } |
| 934 | iref = (struct btrfs_extent_inline_ref *)ptr; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 935 | type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); |
| 936 | if (type == BTRFS_REF_TYPE_INVALID) { |
Su Yue | af431dc | 2018-06-22 16:18:01 +0800 | [diff] [blame] | 937 | err = -EUCLEAN; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 938 | goto out; |
| 939 | } |
| 940 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 941 | if (want < type) |
| 942 | break; |
| 943 | if (want > type) { |
| 944 | ptr += btrfs_extent_inline_ref_size(type); |
| 945 | continue; |
| 946 | } |
| 947 | |
| 948 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 949 | struct btrfs_extent_data_ref *dref; |
| 950 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 951 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 952 | owner, offset)) { |
| 953 | err = 0; |
| 954 | break; |
| 955 | } |
| 956 | if (hash_extent_data_ref_item(leaf, dref) < |
| 957 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 958 | break; |
| 959 | } else { |
| 960 | u64 ref_offset; |
| 961 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 962 | if (parent > 0) { |
| 963 | if (parent == ref_offset) { |
| 964 | err = 0; |
| 965 | break; |
| 966 | } |
| 967 | if (ref_offset < parent) |
| 968 | break; |
| 969 | } else { |
| 970 | if (root_objectid == ref_offset) { |
| 971 | err = 0; |
| 972 | break; |
| 973 | } |
| 974 | if (ref_offset < root_objectid) |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | ptr += btrfs_extent_inline_ref_size(type); |
| 979 | } |
| 980 | if (err == -ENOENT && insert) { |
| 981 | if (item_size + extra_size >= |
| 982 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 983 | err = -EAGAIN; |
| 984 | goto out; |
| 985 | } |
| 986 | /* |
| 987 | * To add new inline back ref, we have to make sure |
| 988 | * there is no corresponding back ref item. |
| 989 | * For simplicity, we just do not add new inline back |
| 990 | * ref if there is any kind of item for this block |
| 991 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 992 | if (find_next_key(path, 0, &key) == 0 && |
| 993 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 994 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 995 | err = -EAGAIN; |
| 996 | goto out; |
| 997 | } |
| 998 | } |
| 999 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1000 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1001 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1002 | path->keep_locks = 0; |
| 1003 | btrfs_unlock_up_safe(path, 1); |
| 1004 | } |
| 1005 | return err; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * helper to add new inline back ref |
| 1010 | */ |
| 1011 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1012 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1013 | struct btrfs_path *path, |
| 1014 | struct btrfs_extent_inline_ref *iref, |
| 1015 | u64 parent, u64 root_objectid, |
| 1016 | u64 owner, u64 offset, int refs_to_add, |
| 1017 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1018 | { |
| 1019 | struct extent_buffer *leaf; |
| 1020 | struct btrfs_extent_item *ei; |
| 1021 | unsigned long ptr; |
| 1022 | unsigned long end; |
| 1023 | unsigned long item_offset; |
| 1024 | u64 refs; |
| 1025 | int size; |
| 1026 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1027 | |
| 1028 | leaf = path->nodes[0]; |
| 1029 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1030 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1031 | |
| 1032 | type = extent_ref_type(parent, owner); |
| 1033 | size = btrfs_extent_inline_ref_size(type); |
| 1034 | |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 1035 | btrfs_extend_item(path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1036 | |
| 1037 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1038 | refs = btrfs_extent_refs(leaf, ei); |
| 1039 | refs += refs_to_add; |
| 1040 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1041 | if (extent_op) |
| 1042 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1043 | |
| 1044 | ptr = (unsigned long)ei + item_offset; |
| 1045 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1046 | if (ptr < end - size) |
| 1047 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1048 | end - size - ptr); |
| 1049 | |
| 1050 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1051 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1052 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1053 | struct btrfs_extent_data_ref *dref; |
| 1054 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1055 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1056 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1057 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1058 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1059 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1060 | struct btrfs_shared_data_ref *sref; |
| 1061 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1062 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1063 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1064 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1065 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1066 | } else { |
| 1067 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1068 | } |
| 1069 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1073 | struct btrfs_path *path, |
| 1074 | struct btrfs_extent_inline_ref **ref_ret, |
| 1075 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1076 | u64 root_objectid, u64 owner, u64 offset) |
| 1077 | { |
| 1078 | int ret; |
| 1079 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1080 | ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr, |
| 1081 | num_bytes, parent, root_objectid, |
| 1082 | owner, offset, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1083 | if (ret != -ENOENT) |
| 1084 | return ret; |
| 1085 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1086 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1087 | *ref_ret = NULL; |
| 1088 | |
| 1089 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 1090 | ret = lookup_tree_block_ref(trans, path, bytenr, parent, |
| 1091 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1092 | } else { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 1093 | ret = lookup_extent_data_ref(trans, path, bytenr, parent, |
| 1094 | root_objectid, owner, offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1095 | } |
| 1096 | return ret; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * helper to update/remove inline back ref |
| 1101 | */ |
| 1102 | static noinline_for_stack |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1103 | void update_inline_extent_backref(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1104 | struct btrfs_extent_inline_ref *iref, |
| 1105 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1106 | struct btrfs_delayed_extent_op *extent_op, |
| 1107 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1108 | { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1109 | struct extent_buffer *leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1110 | struct btrfs_extent_item *ei; |
| 1111 | struct btrfs_extent_data_ref *dref = NULL; |
| 1112 | struct btrfs_shared_data_ref *sref = NULL; |
| 1113 | unsigned long ptr; |
| 1114 | unsigned long end; |
| 1115 | u32 item_size; |
| 1116 | int size; |
| 1117 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1118 | u64 refs; |
| 1119 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1120 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1121 | refs = btrfs_extent_refs(leaf, ei); |
| 1122 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1123 | refs += refs_to_mod; |
| 1124 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1125 | if (extent_op) |
| 1126 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1127 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1128 | /* |
| 1129 | * If type is invalid, we should have bailed out after |
| 1130 | * lookup_inline_extent_backref(). |
| 1131 | */ |
| 1132 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY); |
| 1133 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1134 | |
| 1135 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1136 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1137 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1138 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1139 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1140 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1141 | } else { |
| 1142 | refs = 1; |
| 1143 | BUG_ON(refs_to_mod != -1); |
| 1144 | } |
| 1145 | |
| 1146 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1147 | refs += refs_to_mod; |
| 1148 | |
| 1149 | if (refs > 0) { |
| 1150 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1151 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1152 | else |
| 1153 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1154 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1155 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1156 | size = btrfs_extent_inline_ref_size(type); |
| 1157 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1158 | ptr = (unsigned long)iref; |
| 1159 | end = (unsigned long)ei + item_size; |
| 1160 | if (ptr + size < end) |
| 1161 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1162 | end - ptr - size); |
| 1163 | item_size -= size; |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 1164 | btrfs_truncate_item(path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1165 | } |
| 1166 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static noinline_for_stack |
| 1170 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1171 | struct btrfs_path *path, |
| 1172 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1173 | u64 root_objectid, u64 owner, |
| 1174 | u64 offset, int refs_to_add, |
| 1175 | struct btrfs_delayed_extent_op *extent_op) |
| 1176 | { |
| 1177 | struct btrfs_extent_inline_ref *iref; |
| 1178 | int ret; |
| 1179 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1180 | ret = lookup_inline_extent_backref(trans, path, &iref, bytenr, |
| 1181 | num_bytes, parent, root_objectid, |
| 1182 | owner, offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1183 | if (ret == 0) { |
| 1184 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1185 | update_inline_extent_backref(path, iref, refs_to_add, |
| 1186 | extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1187 | } else if (ret == -ENOENT) { |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1188 | setup_inline_extent_backref(trans->fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1189 | root_objectid, owner, offset, |
| 1190 | refs_to_add, extent_op); |
| 1191 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1192 | } |
| 1193 | return ret; |
| 1194 | } |
| 1195 | |
| 1196 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1197 | struct btrfs_path *path, |
| 1198 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1199 | u64 owner, u64 offset, int refs_to_add) |
| 1200 | { |
| 1201 | int ret; |
| 1202 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1203 | BUG_ON(refs_to_add != 1); |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 1204 | ret = insert_tree_block_ref(trans, path, bytenr, parent, |
| 1205 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1206 | } else { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 1207 | ret = insert_extent_data_ref(trans, path, bytenr, parent, |
| 1208 | root_objectid, owner, offset, |
| 1209 | refs_to_add); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1210 | } |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1215 | struct btrfs_path *path, |
| 1216 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1217 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1218 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1219 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1220 | |
| 1221 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1222 | if (iref) { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1223 | update_inline_extent_backref(path, iref, -refs_to_drop, NULL, |
| 1224 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1225 | } else if (is_data) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 1226 | ret = remove_extent_data_ref(trans, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1227 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1228 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1229 | *last_ref = 1; |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 1230 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1231 | } |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1235 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1236 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1237 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1238 | int j, ret = 0; |
| 1239 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1240 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1241 | |
| 1242 | if (WARN_ON(start != aligned_start)) { |
| 1243 | len -= aligned_start - start; |
| 1244 | len = round_down(len, 1 << 9); |
| 1245 | start = aligned_start; |
| 1246 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1247 | |
| 1248 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1249 | |
| 1250 | if (!len) |
| 1251 | return 0; |
| 1252 | |
| 1253 | end = start + len; |
| 1254 | bytes_left = len; |
| 1255 | |
| 1256 | /* Skip any superblocks on this device. */ |
| 1257 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1258 | u64 sb_start = btrfs_sb_offset(j); |
| 1259 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1260 | u64 size = sb_start - start; |
| 1261 | |
| 1262 | if (!in_range(sb_start, start, bytes_left) && |
| 1263 | !in_range(sb_end, start, bytes_left) && |
| 1264 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1265 | continue; |
| 1266 | |
| 1267 | /* |
| 1268 | * Superblock spans beginning of range. Adjust start and |
| 1269 | * try again. |
| 1270 | */ |
| 1271 | if (sb_start <= start) { |
| 1272 | start += sb_end - start; |
| 1273 | if (start > end) { |
| 1274 | bytes_left = 0; |
| 1275 | break; |
| 1276 | } |
| 1277 | bytes_left = end - start; |
| 1278 | continue; |
| 1279 | } |
| 1280 | |
| 1281 | if (size) { |
| 1282 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1283 | GFP_NOFS, 0); |
| 1284 | if (!ret) |
| 1285 | *discarded_bytes += size; |
| 1286 | else if (ret != -EOPNOTSUPP) |
| 1287 | return ret; |
| 1288 | } |
| 1289 | |
| 1290 | start = sb_end; |
| 1291 | if (start > end) { |
| 1292 | bytes_left = 0; |
| 1293 | break; |
| 1294 | } |
| 1295 | bytes_left = end - start; |
| 1296 | } |
| 1297 | |
| 1298 | if (bytes_left) { |
| 1299 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1300 | GFP_NOFS, 0); |
| 1301 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1302 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1303 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1304 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1305 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1306 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1307 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 1308 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1309 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1310 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1311 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1312 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1313 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1314 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 1315 | /* |
| 1316 | * Avoid races with device replace and make sure our bbio has devices |
| 1317 | * associated to its stripes that don't go away while we are discarding. |
| 1318 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1319 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1320 | /* Tell the block device(s) that the sectors can be discarded */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1321 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
| 1322 | &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1323 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1324 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1325 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1326 | int i; |
| 1327 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1328 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1329 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1330 | u64 bytes; |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1331 | struct request_queue *req_q; |
| 1332 | |
Filipe Manana | 627e087 | 2018-01-30 18:40:22 +0000 | [diff] [blame] | 1333 | if (!stripe->dev->bdev) { |
| 1334 | ASSERT(btrfs_test_opt(fs_info, DEGRADED)); |
| 1335 | continue; |
| 1336 | } |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1337 | req_q = bdev_get_queue(stripe->dev->bdev); |
| 1338 | if (!blk_queue_discard(req_q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1339 | continue; |
| 1340 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1341 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1342 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1343 | stripe->length, |
| 1344 | &bytes); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1345 | if (!ret) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1346 | discarded_bytes += bytes; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1347 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1348 | break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */ |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1349 | |
| 1350 | /* |
| 1351 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1352 | * just ignore the return value so we don't screw up |
| 1353 | * people calling discard_extent. |
| 1354 | */ |
| 1355 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1356 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1357 | btrfs_put_bbio(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1358 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1359 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (actual_bytes) |
| 1362 | *actual_bytes = discarded_bytes; |
| 1363 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1364 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1365 | if (ret == -EOPNOTSUPP) |
| 1366 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1367 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1368 | } |
| 1369 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1370 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1371 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1372 | struct btrfs_ref *generic_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1373 | { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1374 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1375 | int old_ref_mod, new_ref_mod; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1376 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1377 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1378 | ASSERT(generic_ref->type != BTRFS_REF_NOT_SET && |
| 1379 | generic_ref->action); |
| 1380 | BUG_ON(generic_ref->type == BTRFS_REF_METADATA && |
| 1381 | generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1382 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1383 | if (generic_ref->type == BTRFS_REF_METADATA) |
| 1384 | ret = btrfs_add_delayed_tree_ref(trans, generic_ref, |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 1385 | NULL, &old_ref_mod, &new_ref_mod); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1386 | else |
| 1387 | ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1388 | &old_ref_mod, &new_ref_mod); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1389 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1390 | btrfs_ref_tree_mod(fs_info, generic_ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 1391 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 1392 | if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 1393 | sub_pinned_bytes(fs_info, generic_ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1394 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1395 | return ret; |
| 1396 | } |
| 1397 | |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1398 | /* |
| 1399 | * __btrfs_inc_extent_ref - insert backreference for a given extent |
| 1400 | * |
| 1401 | * @trans: Handle of transaction |
| 1402 | * |
| 1403 | * @node: The delayed ref node used to get the bytenr/length for |
| 1404 | * extent whose references are incremented. |
| 1405 | * |
| 1406 | * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/ |
| 1407 | * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical |
| 1408 | * bytenr of the parent block. Since new extents are always |
| 1409 | * created with indirect references, this will only be the case |
| 1410 | * when relocating a shared extent. In that case, root_objectid |
| 1411 | * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must |
| 1412 | * be 0 |
| 1413 | * |
| 1414 | * @root_objectid: The id of the root where this modification has originated, |
| 1415 | * this can be either one of the well-known metadata trees or |
| 1416 | * the subvolume id which references this extent. |
| 1417 | * |
| 1418 | * @owner: For data extents it is the inode number of the owning file. |
| 1419 | * For metadata extents this parameter holds the level in the |
| 1420 | * tree of the extent. |
| 1421 | * |
| 1422 | * @offset: For metadata extents the offset is ignored and is currently |
| 1423 | * always passed as 0. For data extents it is the fileoffset |
| 1424 | * this extent belongs to. |
| 1425 | * |
| 1426 | * @refs_to_add Number of references to add |
| 1427 | * |
| 1428 | * @extent_op Pointer to a structure, holding information necessary when |
| 1429 | * updating a tree block's flags |
| 1430 | * |
| 1431 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1432 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1433 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1434 | u64 parent, u64 root_objectid, |
| 1435 | u64 owner, u64 offset, int refs_to_add, |
| 1436 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1437 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1438 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1439 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1440 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1441 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1442 | u64 bytenr = node->bytenr; |
| 1443 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1444 | u64 refs; |
| 1445 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1446 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1447 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1448 | if (!path) |
| 1449 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1450 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1451 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1452 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1453 | /* 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] | 1454 | ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, |
| 1455 | parent, root_objectid, owner, |
| 1456 | offset, refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 1457 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1458 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1459 | |
| 1460 | /* |
| 1461 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 1462 | * inline extent ref, so just update the reference count and add a |
| 1463 | * normal backref. |
| 1464 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1465 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1466 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1467 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1468 | refs = btrfs_extent_refs(leaf, item); |
| 1469 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1470 | if (extent_op) |
| 1471 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1472 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1473 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1474 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1475 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1476 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1477 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1478 | /* now insert the actual backref */ |
Nikolay Borisov | 3759341 | 2018-06-20 15:48:45 +0300 | [diff] [blame] | 1479 | ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid, |
| 1480 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1481 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1482 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1483 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1484 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 1485 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1488 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1489 | struct btrfs_delayed_ref_node *node, |
| 1490 | struct btrfs_delayed_extent_op *extent_op, |
| 1491 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1492 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1493 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1494 | struct btrfs_delayed_data_ref *ref; |
| 1495 | struct btrfs_key ins; |
| 1496 | u64 parent = 0; |
| 1497 | u64 ref_root = 0; |
| 1498 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1499 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1500 | ins.objectid = node->bytenr; |
| 1501 | ins.offset = node->num_bytes; |
| 1502 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1503 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1504 | ref = btrfs_delayed_node_to_data_ref(node); |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1505 | trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1506 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1507 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1508 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1509 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1510 | |
| 1511 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1512 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1513 | flags |= extent_op->flags_to_set; |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 1514 | ret = alloc_reserved_file_extent(trans, parent, ref_root, |
| 1515 | flags, ref->objectid, |
| 1516 | ref->offset, &ins, |
| 1517 | node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1518 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1519 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1520 | ref->objectid, ref->offset, |
| 1521 | node->ref_mod, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1522 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1523 | ret = __btrfs_free_extent(trans, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1524 | ref_root, ref->objectid, |
| 1525 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1526 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1527 | } else { |
| 1528 | BUG(); |
| 1529 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1530 | return ret; |
| 1531 | } |
| 1532 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1533 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1534 | struct extent_buffer *leaf, |
| 1535 | struct btrfs_extent_item *ei) |
| 1536 | { |
| 1537 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1538 | if (extent_op->update_flags) { |
| 1539 | flags |= extent_op->flags_to_set; |
| 1540 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1541 | } |
| 1542 | |
| 1543 | if (extent_op->update_key) { |
| 1544 | struct btrfs_tree_block_info *bi; |
| 1545 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1546 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1547 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1552 | struct btrfs_delayed_ref_head *head, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1553 | struct btrfs_delayed_extent_op *extent_op) |
| 1554 | { |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1555 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1556 | struct btrfs_key key; |
| 1557 | struct btrfs_path *path; |
| 1558 | struct btrfs_extent_item *ei; |
| 1559 | struct extent_buffer *leaf; |
| 1560 | u32 item_size; |
| 1561 | int ret; |
| 1562 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1563 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1564 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1565 | if (trans->aborted) |
| 1566 | return 0; |
| 1567 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1568 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1569 | metadata = 0; |
| 1570 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1571 | path = btrfs_alloc_path(); |
| 1572 | if (!path) |
| 1573 | return -ENOMEM; |
| 1574 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1575 | key.objectid = head->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1576 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1577 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1578 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1579 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1580 | } else { |
| 1581 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1582 | key.offset = head->num_bytes; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | again: |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1586 | path->reada = READA_FORWARD; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1587 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1588 | 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] | 1589 | if (ret < 0) { |
| 1590 | err = ret; |
| 1591 | goto out; |
| 1592 | } |
| 1593 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1594 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1595 | if (path->slots[0] > 0) { |
| 1596 | path->slots[0]--; |
| 1597 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1598 | path->slots[0]); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1599 | if (key.objectid == head->bytenr && |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1600 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1601 | key.offset == head->num_bytes) |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1602 | ret = 0; |
| 1603 | } |
| 1604 | if (ret > 0) { |
| 1605 | btrfs_release_path(path); |
| 1606 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1607 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1608 | key.objectid = head->bytenr; |
| 1609 | key.offset = head->num_bytes; |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1610 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1611 | goto again; |
| 1612 | } |
| 1613 | } else { |
| 1614 | err = -EIO; |
| 1615 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1616 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | leaf = path->nodes[0]; |
| 1620 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1621 | |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 1622 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1623 | err = -EINVAL; |
| 1624 | btrfs_print_v0_err(fs_info); |
| 1625 | btrfs_abort_transaction(trans, err); |
| 1626 | goto out; |
| 1627 | } |
| 1628 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1629 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1630 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1631 | |
| 1632 | btrfs_mark_buffer_dirty(leaf); |
| 1633 | out: |
| 1634 | btrfs_free_path(path); |
| 1635 | return err; |
| 1636 | } |
| 1637 | |
| 1638 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1639 | struct btrfs_delayed_ref_node *node, |
| 1640 | struct btrfs_delayed_extent_op *extent_op, |
| 1641 | int insert_reserved) |
| 1642 | { |
| 1643 | int ret = 0; |
| 1644 | struct btrfs_delayed_tree_ref *ref; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1645 | u64 parent = 0; |
| 1646 | u64 ref_root = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1647 | |
| 1648 | ref = btrfs_delayed_node_to_tree_ref(node); |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1649 | trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1650 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1651 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1652 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1653 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1654 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1655 | if (node->ref_mod != 1) { |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1656 | btrfs_err(trans->fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1657 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 1658 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 1659 | parent); |
| 1660 | return -EIO; |
| 1661 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1662 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1663 | BUG_ON(!extent_op || !extent_op->update_flags); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 1664 | ret = alloc_reserved_tree_block(trans, node, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1665 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1666 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1667 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1668 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1669 | ret = __btrfs_free_extent(trans, node, parent, ref_root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1670 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1671 | } else { |
| 1672 | BUG(); |
| 1673 | } |
| 1674 | return ret; |
| 1675 | } |
| 1676 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1677 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1678 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1679 | struct btrfs_delayed_ref_node *node, |
| 1680 | struct btrfs_delayed_extent_op *extent_op, |
| 1681 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1682 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1683 | int ret = 0; |
| 1684 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1685 | if (trans->aborted) { |
| 1686 | if (insert_reserved) |
Nikolay Borisov | 5fac7f9 | 2018-06-20 15:49:11 +0300 | [diff] [blame] | 1687 | btrfs_pin_extent(trans->fs_info, node->bytenr, |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1688 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1689 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1690 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1691 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1692 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1693 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1694 | ret = run_delayed_tree_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1695 | insert_reserved); |
| 1696 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1697 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1698 | ret = run_delayed_data_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1699 | insert_reserved); |
| 1700 | else |
| 1701 | BUG(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 1702 | if (ret && insert_reserved) |
| 1703 | btrfs_pin_extent(trans->fs_info, node->bytenr, |
| 1704 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1705 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1706 | } |
| 1707 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1708 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1709 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1710 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1711 | struct btrfs_delayed_ref_node *ref; |
| 1712 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1713 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1714 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1715 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1716 | /* |
| 1717 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1718 | * This is to prevent a ref count from going down to zero, which deletes |
| 1719 | * the extent item from the extent tree, when there still are references |
| 1720 | * to add, which would fail because they would not find the extent item. |
| 1721 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1722 | if (!list_empty(&head->ref_add_list)) |
| 1723 | return list_first_entry(&head->ref_add_list, |
| 1724 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1725 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1726 | ref = rb_entry(rb_first_cached(&head->ref_tree), |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 1727 | struct btrfs_delayed_ref_node, ref_node); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1728 | ASSERT(list_empty(&ref->add_list)); |
| 1729 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1730 | } |
| 1731 | |
Josef Bacik | 2eadaa2 | 2017-09-29 15:43:52 -0400 | [diff] [blame] | 1732 | static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
| 1733 | struct btrfs_delayed_ref_head *head) |
| 1734 | { |
| 1735 | spin_lock(&delayed_refs->lock); |
| 1736 | head->processing = 0; |
| 1737 | delayed_refs->num_heads_ready++; |
| 1738 | spin_unlock(&delayed_refs->lock); |
| 1739 | btrfs_delayed_ref_unlock(head); |
| 1740 | } |
| 1741 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1742 | static struct btrfs_delayed_extent_op *cleanup_extent_op( |
| 1743 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1744 | { |
| 1745 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1746 | |
| 1747 | if (!extent_op) |
| 1748 | return NULL; |
| 1749 | |
| 1750 | if (head->must_insert_reserved) { |
| 1751 | head->extent_op = NULL; |
| 1752 | btrfs_free_delayed_extent_op(extent_op); |
| 1753 | return NULL; |
| 1754 | } |
| 1755 | return extent_op; |
| 1756 | } |
| 1757 | |
| 1758 | static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, |
| 1759 | struct btrfs_delayed_ref_head *head) |
| 1760 | { |
| 1761 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1762 | int ret; |
| 1763 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1764 | extent_op = cleanup_extent_op(head); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1765 | if (!extent_op) |
| 1766 | return 0; |
| 1767 | head->extent_op = NULL; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1768 | spin_unlock(&head->lock); |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1769 | ret = run_delayed_extent_op(trans, head, extent_op); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1770 | btrfs_free_delayed_extent_op(extent_op); |
| 1771 | return ret ? ret : 1; |
| 1772 | } |
| 1773 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1774 | void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, |
| 1775 | struct btrfs_delayed_ref_root *delayed_refs, |
| 1776 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1777 | { |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1778 | int nr_items = 1; /* Dropping this ref head update. */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1779 | |
| 1780 | if (head->total_ref_mod < 0) { |
| 1781 | struct btrfs_space_info *space_info; |
| 1782 | u64 flags; |
| 1783 | |
| 1784 | if (head->is_data) |
| 1785 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 1786 | else if (head->is_system) |
| 1787 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 1788 | else |
| 1789 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 1790 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1791 | ASSERT(space_info); |
| 1792 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 1793 | -head->num_bytes, |
| 1794 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 1795 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1796 | /* |
| 1797 | * We had csum deletions accounted for in our delayed refs rsv, |
| 1798 | * we need to drop the csum leaves for this update from our |
| 1799 | * delayed_refs_rsv. |
| 1800 | */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1801 | if (head->is_data) { |
| 1802 | spin_lock(&delayed_refs->lock); |
| 1803 | delayed_refs->pending_csums -= head->num_bytes; |
| 1804 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1805 | nr_items += btrfs_csum_bytes_to_leaves(fs_info, |
| 1806 | head->num_bytes); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1810 | btrfs_delayed_refs_rsv_release(fs_info, nr_items); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1811 | } |
| 1812 | |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1813 | static int cleanup_ref_head(struct btrfs_trans_handle *trans, |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1814 | struct btrfs_delayed_ref_head *head) |
| 1815 | { |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 1816 | |
| 1817 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1818 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1819 | int ret; |
| 1820 | |
| 1821 | delayed_refs = &trans->transaction->delayed_refs; |
| 1822 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1823 | ret = run_and_cleanup_extent_op(trans, head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1824 | if (ret < 0) { |
| 1825 | unselect_delayed_ref_head(delayed_refs, head); |
| 1826 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
| 1827 | return ret; |
| 1828 | } else if (ret) { |
| 1829 | return ret; |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Need to drop our head ref lock and re-acquire the delayed ref lock |
| 1834 | * and then re-check to make sure nobody got added. |
| 1835 | */ |
| 1836 | spin_unlock(&head->lock); |
| 1837 | spin_lock(&delayed_refs->lock); |
| 1838 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1839 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) { |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1840 | spin_unlock(&head->lock); |
| 1841 | spin_unlock(&delayed_refs->lock); |
| 1842 | return 1; |
| 1843 | } |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 1844 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1845 | spin_unlock(&head->lock); |
Nikolay Borisov | 1e7a142 | 2018-04-11 11:21:18 +0300 | [diff] [blame] | 1846 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1847 | |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1848 | if (head->must_insert_reserved) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1849 | btrfs_pin_extent(fs_info, head->bytenr, |
| 1850 | head->num_bytes, 1); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1851 | if (head->is_data) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1852 | ret = btrfs_del_csums(trans, fs_info, head->bytenr, |
| 1853 | head->num_bytes); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1857 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1858 | |
| 1859 | trace_run_delayed_ref_head(fs_info, head, 0); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1860 | btrfs_delayed_ref_unlock(head); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1861 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1862 | return 0; |
| 1863 | } |
| 1864 | |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1865 | static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( |
| 1866 | struct btrfs_trans_handle *trans) |
| 1867 | { |
| 1868 | struct btrfs_delayed_ref_root *delayed_refs = |
| 1869 | &trans->transaction->delayed_refs; |
| 1870 | struct btrfs_delayed_ref_head *head = NULL; |
| 1871 | int ret; |
| 1872 | |
| 1873 | spin_lock(&delayed_refs->lock); |
Lu Fengqi | 5637c74 | 2018-10-11 13:40:33 +0800 | [diff] [blame] | 1874 | head = btrfs_select_ref_head(delayed_refs); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1875 | if (!head) { |
| 1876 | spin_unlock(&delayed_refs->lock); |
| 1877 | return head; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * Grab the lock that says we are going to process all the refs for |
| 1882 | * this head |
| 1883 | */ |
Lu Fengqi | 9e920a6 | 2018-10-11 13:40:34 +0800 | [diff] [blame] | 1884 | ret = btrfs_delayed_ref_lock(delayed_refs, head); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1885 | spin_unlock(&delayed_refs->lock); |
| 1886 | |
| 1887 | /* |
| 1888 | * We may have dropped the spin lock to get the head mutex lock, and |
| 1889 | * that might have given someone else time to free the head. If that's |
| 1890 | * true, it has been removed from our list and we can move on. |
| 1891 | */ |
| 1892 | if (ret == -EAGAIN) |
| 1893 | head = ERR_PTR(-EAGAIN); |
| 1894 | |
| 1895 | return head; |
| 1896 | } |
| 1897 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1898 | static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, |
| 1899 | struct btrfs_delayed_ref_head *locked_ref, |
| 1900 | unsigned long *run_refs) |
| 1901 | { |
| 1902 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1903 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1904 | struct btrfs_delayed_extent_op *extent_op; |
| 1905 | struct btrfs_delayed_ref_node *ref; |
| 1906 | int must_insert_reserved = 0; |
| 1907 | int ret; |
| 1908 | |
| 1909 | delayed_refs = &trans->transaction->delayed_refs; |
| 1910 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1911 | lockdep_assert_held(&locked_ref->mutex); |
| 1912 | lockdep_assert_held(&locked_ref->lock); |
| 1913 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1914 | while ((ref = select_delayed_ref(locked_ref))) { |
| 1915 | if (ref->seq && |
| 1916 | btrfs_check_delayed_seq(fs_info, ref->seq)) { |
| 1917 | spin_unlock(&locked_ref->lock); |
| 1918 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1919 | return -EAGAIN; |
| 1920 | } |
| 1921 | |
| 1922 | (*run_refs)++; |
| 1923 | ref->in_tree = 0; |
| 1924 | rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree); |
| 1925 | RB_CLEAR_NODE(&ref->ref_node); |
| 1926 | if (!list_empty(&ref->add_list)) |
| 1927 | list_del(&ref->add_list); |
| 1928 | /* |
| 1929 | * When we play the delayed ref, also correct the ref_mod on |
| 1930 | * head |
| 1931 | */ |
| 1932 | switch (ref->action) { |
| 1933 | case BTRFS_ADD_DELAYED_REF: |
| 1934 | case BTRFS_ADD_DELAYED_EXTENT: |
| 1935 | locked_ref->ref_mod -= ref->ref_mod; |
| 1936 | break; |
| 1937 | case BTRFS_DROP_DELAYED_REF: |
| 1938 | locked_ref->ref_mod += ref->ref_mod; |
| 1939 | break; |
| 1940 | default: |
| 1941 | WARN_ON(1); |
| 1942 | } |
| 1943 | atomic_dec(&delayed_refs->num_entries); |
| 1944 | |
| 1945 | /* |
| 1946 | * Record the must_insert_reserved flag before we drop the |
| 1947 | * spin lock. |
| 1948 | */ |
| 1949 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1950 | locked_ref->must_insert_reserved = 0; |
| 1951 | |
| 1952 | extent_op = locked_ref->extent_op; |
| 1953 | locked_ref->extent_op = NULL; |
| 1954 | spin_unlock(&locked_ref->lock); |
| 1955 | |
| 1956 | ret = run_one_delayed_ref(trans, ref, extent_op, |
| 1957 | must_insert_reserved); |
| 1958 | |
| 1959 | btrfs_free_delayed_extent_op(extent_op); |
| 1960 | if (ret) { |
| 1961 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1962 | btrfs_put_delayed_ref(ref); |
| 1963 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 1964 | ret); |
| 1965 | return ret; |
| 1966 | } |
| 1967 | |
| 1968 | btrfs_put_delayed_ref(ref); |
| 1969 | cond_resched(); |
| 1970 | |
| 1971 | spin_lock(&locked_ref->lock); |
| 1972 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
| 1973 | } |
| 1974 | |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1978 | /* |
| 1979 | * Returns 0 on success or if called with an already aborted transaction. |
| 1980 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 1981 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1982 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1983 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1984 | { |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 1985 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1986 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1987 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 1988 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1989 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1990 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 1991 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1992 | |
| 1993 | delayed_refs = &trans->transaction->delayed_refs; |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1994 | do { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1995 | if (!locked_ref) { |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1996 | locked_ref = btrfs_obtain_ref_head(trans); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1997 | if (IS_ERR_OR_NULL(locked_ref)) { |
| 1998 | if (PTR_ERR(locked_ref) == -EAGAIN) { |
| 1999 | continue; |
| 2000 | } else { |
| 2001 | break; |
| 2002 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2003 | } |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2004 | count++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2005 | } |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2006 | /* |
| 2007 | * We need to try and merge add/drops of the same ref since we |
| 2008 | * can run into issues with relocate dropping the implicit ref |
| 2009 | * and then it being added back again before the drop can |
| 2010 | * finish. If we merged anything we need to re-loop so we can |
| 2011 | * get a good ref. |
| 2012 | * Or we can get node references of the same type that weren't |
| 2013 | * merged when created due to bumps in the tree mod seq, and |
| 2014 | * we need to merge them to prevent adding an inline extent |
| 2015 | * backref before dropping it (triggering a BUG_ON at |
| 2016 | * insert_inline_extent_backref()). |
| 2017 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2018 | spin_lock(&locked_ref->lock); |
Nikolay Borisov | be97f13 | 2018-04-19 11:06:39 +0300 | [diff] [blame] | 2019 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2020 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2021 | ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, |
| 2022 | &actual_count); |
| 2023 | if (ret < 0 && ret != -EAGAIN) { |
| 2024 | /* |
| 2025 | * Error, btrfs_run_delayed_refs_for_head already |
| 2026 | * unlocked everything so just bail out |
| 2027 | */ |
| 2028 | return ret; |
| 2029 | } else if (!ret) { |
| 2030 | /* |
| 2031 | * Success, perform the usual cleanup of a processed |
| 2032 | * head |
| 2033 | */ |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 2034 | ret = cleanup_ref_head(trans, locked_ref); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2035 | if (ret > 0 ) { |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2036 | /* We dropped our lock, we need to loop. */ |
| 2037 | ret = 0; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2038 | continue; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2039 | } else if (ret) { |
| 2040 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2041 | } |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2042 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2043 | |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2044 | /* |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2045 | * Either success case or btrfs_run_delayed_refs_for_head |
| 2046 | * returned -EAGAIN, meaning we need to select another head |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2047 | */ |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2048 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2049 | locked_ref = NULL; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2050 | cond_resched(); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2051 | } while ((nr != -1 && count < nr) || locked_ref); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2052 | |
| 2053 | /* |
| 2054 | * We don't want to include ref heads since we can have empty ref heads |
| 2055 | * and those will drastically skew our runtime down since we just do |
| 2056 | * accounting, no actual extent tree updates. |
| 2057 | */ |
| 2058 | if (actual_count > 0) { |
| 2059 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2060 | u64 avg; |
| 2061 | |
| 2062 | /* |
| 2063 | * We weigh the current average higher than our current runtime |
| 2064 | * to avoid large swings in the average. |
| 2065 | */ |
| 2066 | spin_lock(&delayed_refs->lock); |
| 2067 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2068 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2069 | spin_unlock(&delayed_refs->lock); |
| 2070 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2071 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2072 | } |
| 2073 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2074 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2075 | /* |
| 2076 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2077 | * correlates in most cases to the order added. To expose dependencies on this |
| 2078 | * order, we start to process the tree in the middle instead of the beginning |
| 2079 | */ |
| 2080 | static u64 find_middle(struct rb_root *root) |
| 2081 | { |
| 2082 | struct rb_node *n = root->rb_node; |
| 2083 | struct btrfs_delayed_ref_node *entry; |
| 2084 | int alt = 1; |
| 2085 | u64 middle; |
| 2086 | u64 first = 0, last = 0; |
| 2087 | |
| 2088 | n = rb_first(root); |
| 2089 | if (n) { |
| 2090 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2091 | first = entry->bytenr; |
| 2092 | } |
| 2093 | n = rb_last(root); |
| 2094 | if (n) { |
| 2095 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2096 | last = entry->bytenr; |
| 2097 | } |
| 2098 | n = root->rb_node; |
| 2099 | |
| 2100 | while (n) { |
| 2101 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2102 | WARN_ON(!entry->in_tree); |
| 2103 | |
| 2104 | middle = entry->bytenr; |
| 2105 | |
| 2106 | if (alt) |
| 2107 | n = n->rb_left; |
| 2108 | else |
| 2109 | n = n->rb_right; |
| 2110 | |
| 2111 | alt = 1 - alt; |
| 2112 | } |
| 2113 | return middle; |
| 2114 | } |
| 2115 | #endif |
| 2116 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2117 | static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2118 | { |
| 2119 | u64 num_bytes; |
| 2120 | |
| 2121 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2122 | sizeof(struct btrfs_extent_inline_ref)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2123 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2124 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2125 | |
| 2126 | /* |
| 2127 | * We don't ever fill up leaves all the way so multiply by 2 just to be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2128 | * closer to what we're really going to want to use. |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2129 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2130 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2131 | } |
| 2132 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2133 | /* |
| 2134 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2135 | * would require to store the csums for that many bytes. |
| 2136 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2137 | u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes) |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2138 | { |
| 2139 | u64 csum_size; |
| 2140 | u64 num_csums_per_leaf; |
| 2141 | u64 num_csums; |
| 2142 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2143 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2144 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2145 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2146 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2147 | num_csums += num_csums_per_leaf - 1; |
| 2148 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2149 | return num_csums; |
| 2150 | } |
| 2151 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2152 | /* |
| 2153 | * this starts processing the delayed reference count updates and |
| 2154 | * extent insertions we have queued up so far. count can be |
| 2155 | * 0, which means to process everything in the tree at the start |
| 2156 | * of the run (but not newly added entries), or it can be some target |
| 2157 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2158 | * |
| 2159 | * Returns 0 on success or if called with an aborted transaction |
| 2160 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2161 | */ |
| 2162 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2163 | unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2164 | { |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2165 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2166 | struct rb_node *node; |
| 2167 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2168 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2169 | int ret; |
| 2170 | int run_all = count == (unsigned long)-1; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2171 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2172 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2173 | if (trans->aborted) |
| 2174 | return 0; |
| 2175 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2176 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2177 | return 0; |
| 2178 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2179 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2180 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2181 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2182 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2183 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2184 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2185 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2186 | #endif |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2187 | ret = __btrfs_run_delayed_refs(trans, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2188 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2189 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2190 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2191 | } |
| 2192 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2193 | if (run_all) { |
Josef Bacik | 119e80d | 2018-11-21 14:05:42 -0500 | [diff] [blame] | 2194 | btrfs_create_pending_block_groups(trans); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2195 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2196 | spin_lock(&delayed_refs->lock); |
Liu Bo | 5c9d028 | 2018-08-23 03:51:49 +0800 | [diff] [blame] | 2197 | node = rb_first_cached(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2198 | if (!node) { |
| 2199 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2200 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2201 | } |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2202 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2203 | href_node); |
| 2204 | refcount_inc(&head->refs); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2205 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2206 | |
| 2207 | /* Mutex was contended, block until it's released and retry. */ |
| 2208 | mutex_lock(&head->mutex); |
| 2209 | mutex_unlock(&head->mutex); |
| 2210 | |
| 2211 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2212 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2213 | goto again; |
| 2214 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2215 | out: |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2216 | return 0; |
| 2217 | } |
| 2218 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2219 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2220 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2221 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2222 | { |
| 2223 | struct btrfs_delayed_extent_op *extent_op; |
| 2224 | int ret; |
| 2225 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2226 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2227 | if (!extent_op) |
| 2228 | return -ENOMEM; |
| 2229 | |
| 2230 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 2231 | extent_op->update_flags = true; |
| 2232 | extent_op->update_key = false; |
| 2233 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2234 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2235 | |
David Sterba | c6e340b | 2019-03-20 11:42:34 +0100 | [diff] [blame] | 2236 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2237 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2238 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2239 | return ret; |
| 2240 | } |
| 2241 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2242 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2243 | struct btrfs_path *path, |
| 2244 | u64 objectid, u64 offset, u64 bytenr) |
| 2245 | { |
| 2246 | struct btrfs_delayed_ref_head *head; |
| 2247 | struct btrfs_delayed_ref_node *ref; |
| 2248 | struct btrfs_delayed_data_ref *data_ref; |
| 2249 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2250 | struct btrfs_transaction *cur_trans; |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2251 | struct rb_node *node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2252 | int ret = 0; |
| 2253 | |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2254 | spin_lock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2255 | cur_trans = root->fs_info->running_transaction; |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2256 | if (cur_trans) |
| 2257 | refcount_inc(&cur_trans->use_count); |
| 2258 | spin_unlock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2259 | if (!cur_trans) |
| 2260 | return 0; |
| 2261 | |
| 2262 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2263 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 2264 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2265 | if (!head) { |
| 2266 | spin_unlock(&delayed_refs->lock); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2267 | btrfs_put_transaction(cur_trans); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2268 | return 0; |
| 2269 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2270 | |
| 2271 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2272 | refcount_inc(&head->refs); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2273 | spin_unlock(&delayed_refs->lock); |
| 2274 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2275 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2276 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2277 | /* |
| 2278 | * Mutex was contended, block until it's released and let |
| 2279 | * caller try again |
| 2280 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2281 | mutex_lock(&head->mutex); |
| 2282 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2283 | btrfs_put_delayed_ref_head(head); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2284 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2285 | return -EAGAIN; |
| 2286 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2287 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2288 | |
| 2289 | spin_lock(&head->lock); |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2290 | /* |
| 2291 | * XXX: We should replace this with a proper search function in the |
| 2292 | * future. |
| 2293 | */ |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2294 | for (node = rb_first_cached(&head->ref_tree); node; |
| 2295 | node = rb_next(node)) { |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2296 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2297 | /* If it's a shared ref we know a cross reference exists */ |
| 2298 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 2299 | ret = 1; |
| 2300 | break; |
| 2301 | } |
| 2302 | |
| 2303 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2304 | |
| 2305 | /* |
| 2306 | * If our ref doesn't match the one we're currently looking at |
| 2307 | * then we have a cross reference. |
| 2308 | */ |
| 2309 | if (data_ref->root != root->root_key.objectid || |
| 2310 | data_ref->objectid != objectid || |
| 2311 | data_ref->offset != offset) { |
| 2312 | ret = 1; |
| 2313 | break; |
| 2314 | } |
| 2315 | } |
| 2316 | spin_unlock(&head->lock); |
| 2317 | mutex_unlock(&head->mutex); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2318 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2319 | return ret; |
| 2320 | } |
| 2321 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2322 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2323 | struct btrfs_path *path, |
| 2324 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2325 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2326 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2327 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2328 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2329 | struct btrfs_extent_data_ref *ref; |
| 2330 | struct btrfs_extent_inline_ref *iref; |
| 2331 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2332 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2333 | u32 item_size; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2334 | int type; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2335 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2336 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2337 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2338 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2339 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2340 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2341 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2342 | if (ret < 0) |
| 2343 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2344 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2345 | |
| 2346 | ret = -ENOENT; |
| 2347 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2348 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2349 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2350 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2351 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2352 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2353 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2354 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2355 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2356 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2357 | ret = 1; |
| 2358 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2359 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2360 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2361 | /* 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] | 2362 | if (item_size != sizeof(*ei) + |
| 2363 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2364 | goto out; |
| 2365 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2366 | /* If extent created before last snapshot => it's definitely shared */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2367 | if (btrfs_extent_generation(leaf, ei) <= |
| 2368 | btrfs_root_last_snapshot(&root->root_item)) |
| 2369 | goto out; |
| 2370 | |
| 2371 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2372 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2373 | /* If this extent has SHARED_DATA_REF then it's shared */ |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2374 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 2375 | if (type != BTRFS_EXTENT_DATA_REF_KEY) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2376 | goto out; |
| 2377 | |
| 2378 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2379 | if (btrfs_extent_refs(leaf, ei) != |
| 2380 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2381 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2382 | root->root_key.objectid || |
| 2383 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2384 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2385 | goto out; |
| 2386 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2387 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2388 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2389 | return ret; |
| 2390 | } |
| 2391 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2392 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
| 2393 | u64 bytenr) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2394 | { |
| 2395 | struct btrfs_path *path; |
| 2396 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2397 | |
| 2398 | path = btrfs_alloc_path(); |
| 2399 | if (!path) |
Su Yue | 9132c4f | 2018-05-30 14:49:10 +0800 | [diff] [blame] | 2400 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2401 | |
| 2402 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2403 | ret = check_committed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2404 | offset, bytenr); |
| 2405 | if (ret && ret != -ENOENT) |
| 2406 | goto out; |
| 2407 | |
Misono Tomohiro | 380fd06 | 2018-08-30 10:59:16 +0900 | [diff] [blame] | 2408 | ret = check_delayed_ref(root, path, objectid, offset, bytenr); |
| 2409 | } while (ret == -EAGAIN); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2410 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2411 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2412 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2413 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2414 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2415 | return ret; |
| 2416 | } |
| 2417 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2418 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2419 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2420 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2421 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2422 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2423 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2424 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2425 | u64 num_bytes; |
| 2426 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2427 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2428 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2429 | struct btrfs_key key; |
| 2430 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2431 | struct btrfs_ref generic_ref = { 0 }; |
| 2432 | bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2433 | int i; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2434 | int action; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2435 | int level; |
| 2436 | int ret = 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2437 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2438 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 2439 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2440 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2441 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2442 | nritems = btrfs_header_nritems(buf); |
| 2443 | level = btrfs_header_level(buf); |
| 2444 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2445 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2446 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2447 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2448 | if (full_backref) |
| 2449 | parent = buf->start; |
| 2450 | else |
| 2451 | parent = 0; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2452 | if (inc) |
| 2453 | action = BTRFS_ADD_DELAYED_REF; |
| 2454 | else |
| 2455 | action = BTRFS_DROP_DELAYED_REF; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2456 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2457 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2458 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2459 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 2460 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2461 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2462 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2463 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2464 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2465 | BTRFS_FILE_EXTENT_INLINE) |
| 2466 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2467 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2468 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2469 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2470 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2471 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2472 | key.offset -= btrfs_file_extent_offset(buf, fi); |
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_data_ref(&generic_ref, ref_root, key.objectid, |
| 2477 | key.offset); |
| 2478 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2479 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2480 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2481 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2482 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2483 | if (ret) |
| 2484 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2485 | } else { |
| 2486 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2487 | num_bytes = fs_info->nodesize; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2488 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2489 | num_bytes, parent); |
| 2490 | generic_ref.real_root = root->root_key.objectid; |
| 2491 | btrfs_init_tree_ref(&generic_ref, level - 1, ref_root); |
| 2492 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2493 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2494 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2495 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2496 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2497 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2498 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2499 | } |
| 2500 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2501 | return 0; |
| 2502 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2503 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2504 | } |
| 2505 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2506 | 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] | 2507 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2508 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2509 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2510 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2511 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2512 | 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] | 2513 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2514 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2515 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2516 | } |
| 2517 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2518 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2519 | { |
| 2520 | struct btrfs_block_group_cache *block_group; |
| 2521 | int readonly = 0; |
| 2522 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2523 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2524 | if (!block_group || block_group->ro) |
| 2525 | readonly = 1; |
| 2526 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2527 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2528 | return readonly; |
| 2529 | } |
| 2530 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 2531 | 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] | 2532 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2533 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2534 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2535 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2536 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2537 | if (data) |
| 2538 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2539 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2540 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2541 | else |
| 2542 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 2543 | |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 2544 | ret = btrfs_get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2545 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2546 | } |
| 2547 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2548 | 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] | 2549 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2550 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2551 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2552 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2553 | spin_lock(&fs_info->block_group_cache_lock); |
| 2554 | bytenr = fs_info->first_logical_byte; |
| 2555 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 2556 | |
| 2557 | if (bytenr < (u64)-1) |
| 2558 | return bytenr; |
| 2559 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2560 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2561 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2562 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2563 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2564 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2565 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2566 | |
| 2567 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2570 | static int pin_down_extent(struct btrfs_block_group_cache *cache, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2571 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2572 | { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2573 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 2574 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2575 | spin_lock(&cache->space_info->lock); |
| 2576 | spin_lock(&cache->lock); |
| 2577 | cache->pinned += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2578 | btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, |
| 2579 | num_bytes); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2580 | if (reserved) { |
| 2581 | cache->reserved -= num_bytes; |
| 2582 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2583 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2584 | spin_unlock(&cache->lock); |
| 2585 | spin_unlock(&cache->space_info->lock); |
| 2586 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2587 | trace_btrfs_space_reservation(fs_info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 2588 | cache->space_info->flags, num_bytes, 1); |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 2589 | percpu_counter_add_batch(&cache->space_info->total_bytes_pinned, |
| 2590 | num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2591 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2592 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2593 | return 0; |
| 2594 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2595 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2596 | /* |
| 2597 | * this function must be called within transaction |
| 2598 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2599 | int btrfs_pin_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2600 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2601 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2602 | struct btrfs_block_group_cache *cache; |
| 2603 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2604 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2605 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2606 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2607 | pin_down_extent(cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2608 | |
| 2609 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2610 | return 0; |
| 2611 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2612 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2613 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2614 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2615 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2616 | int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2617 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2618 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2619 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2620 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2621 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2622 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2623 | if (!cache) |
| 2624 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2625 | |
| 2626 | /* |
| 2627 | * pull in the free space cache (if any) so that our pin |
| 2628 | * removes the free space from the cache. We have load_only set |
| 2629 | * to one because the slow code to read in the free extents does check |
| 2630 | * the pinned extents. |
| 2631 | */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2632 | btrfs_cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2633 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2634 | pin_down_extent(cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2635 | |
| 2636 | /* 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] | 2637 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2638 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2639 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2640 | } |
| 2641 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2642 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 2643 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2644 | { |
| 2645 | int ret; |
| 2646 | struct btrfs_block_group_cache *block_group; |
| 2647 | struct btrfs_caching_control *caching_ctl; |
| 2648 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2649 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2650 | if (!block_group) |
| 2651 | return -EINVAL; |
| 2652 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2653 | btrfs_cache_block_group(block_group, 0); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2654 | caching_ctl = btrfs_get_caching_control(block_group); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2655 | |
| 2656 | if (!caching_ctl) { |
| 2657 | /* Logic error */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2658 | BUG_ON(!btrfs_block_group_cache_done(block_group)); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2659 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 2660 | } else { |
| 2661 | mutex_lock(&caching_ctl->mutex); |
| 2662 | |
| 2663 | if (start >= caching_ctl->progress) { |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2664 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2665 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2666 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 2667 | ret = btrfs_remove_free_space(block_group, |
| 2668 | start, num_bytes); |
| 2669 | } else { |
| 2670 | num_bytes = caching_ctl->progress - start; |
| 2671 | ret = btrfs_remove_free_space(block_group, |
| 2672 | start, num_bytes); |
| 2673 | if (ret) |
| 2674 | goto out_lock; |
| 2675 | |
| 2676 | num_bytes = (start + num_bytes) - |
| 2677 | caching_ctl->progress; |
| 2678 | start = caching_ctl->progress; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2679 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2680 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2681 | } |
| 2682 | out_lock: |
| 2683 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2684 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2685 | } |
| 2686 | btrfs_put_block_group(block_group); |
| 2687 | return ret; |
| 2688 | } |
| 2689 | |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2690 | int btrfs_exclude_logged_extents(struct extent_buffer *eb) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2691 | { |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2692 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2693 | struct btrfs_file_extent_item *item; |
| 2694 | struct btrfs_key key; |
| 2695 | int found_type; |
| 2696 | int i; |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2697 | int ret = 0; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2698 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2699 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2700 | return 0; |
| 2701 | |
| 2702 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 2703 | btrfs_item_key_to_cpu(eb, &key, i); |
| 2704 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2705 | continue; |
| 2706 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 2707 | found_type = btrfs_file_extent_type(eb, item); |
| 2708 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 2709 | continue; |
| 2710 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 2711 | continue; |
| 2712 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 2713 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2714 | ret = __exclude_logged_extent(fs_info, key.objectid, key.offset); |
| 2715 | if (ret) |
| 2716 | break; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2717 | } |
| 2718 | |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2719 | return ret; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2720 | } |
| 2721 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 2722 | static void |
| 2723 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 2724 | { |
| 2725 | atomic_inc(&bg->reservations); |
| 2726 | } |
| 2727 | |
David Sterba | 8b74c03 | 2017-02-10 19:20:56 +0100 | [diff] [blame] | 2728 | void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2729 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2730 | struct btrfs_caching_control *next; |
| 2731 | struct btrfs_caching_control *caching_ctl; |
| 2732 | struct btrfs_block_group_cache *cache; |
| 2733 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 2734 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2735 | |
| 2736 | list_for_each_entry_safe(caching_ctl, next, |
| 2737 | &fs_info->caching_block_groups, list) { |
| 2738 | cache = caching_ctl->block_group; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2739 | if (btrfs_block_group_cache_done(cache)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2740 | cache->last_byte_to_unpin = (u64)-1; |
| 2741 | list_del_init(&caching_ctl->list); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2742 | btrfs_put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2743 | } else { |
| 2744 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 2749 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 2750 | else |
| 2751 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 2752 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 2753 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2754 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 2755 | btrfs_update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2756 | } |
| 2757 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2758 | /* |
| 2759 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 2760 | * what it should be based on the mount options. |
| 2761 | */ |
| 2762 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2763 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 2764 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2765 | { |
| 2766 | struct btrfs_free_cluster *ret = NULL; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2767 | |
| 2768 | *empty_cluster = 0; |
| 2769 | if (btrfs_mixed_space_info(space_info)) |
| 2770 | return ret; |
| 2771 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2772 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2773 | ret = &fs_info->meta_alloc_cluster; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2774 | if (btrfs_test_opt(fs_info, SSD)) |
| 2775 | *empty_cluster = SZ_2M; |
| 2776 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2777 | *empty_cluster = SZ_64K; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2778 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && |
| 2779 | btrfs_test_opt(fs_info, SSD_SPREAD)) { |
| 2780 | *empty_cluster = SZ_2M; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2781 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | return ret; |
| 2785 | } |
| 2786 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2787 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 2788 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 2789 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2790 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2791 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2792 | struct btrfs_space_info *space_info; |
| 2793 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2794 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2795 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2796 | u64 total_unpinned = 0; |
| 2797 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2798 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2799 | |
| 2800 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2801 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2802 | if (!cache || |
| 2803 | start >= cache->key.objectid + cache->key.offset) { |
| 2804 | if (cache) |
| 2805 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2806 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2807 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2808 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2809 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2810 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2811 | cache->space_info, |
| 2812 | &empty_cluster); |
| 2813 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | len = cache->key.objectid + cache->key.offset - start; |
| 2817 | len = min(len, end + 1 - start); |
| 2818 | |
| 2819 | if (start < cache->last_byte_to_unpin) { |
| 2820 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 2821 | if (return_free_space) |
| 2822 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2823 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2824 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2825 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2826 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2827 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2828 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2829 | /* |
| 2830 | * If this space cluster has been marked as fragmented and we've |
| 2831 | * unpinned enough in this block group to potentially allow a |
| 2832 | * cluster to be created inside of it go ahead and clear the |
| 2833 | * fragmented check. |
| 2834 | */ |
| 2835 | if (cluster && cluster->fragmented && |
| 2836 | total_unpinned > empty_cluster) { |
| 2837 | spin_lock(&cluster->lock); |
| 2838 | cluster->fragmented = 0; |
| 2839 | spin_unlock(&cluster->lock); |
| 2840 | } |
| 2841 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2842 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2843 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2844 | cache->pinned -= len; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2845 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 2846 | |
| 2847 | trace_btrfs_space_reservation(fs_info, "pinned", |
| 2848 | space_info->flags, len, 0); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 2849 | space_info->max_extent_size = 0; |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 2850 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 2851 | -len, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2852 | if (cache->ro) { |
| 2853 | space_info->bytes_readonly += len; |
| 2854 | readonly = true; |
| 2855 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2856 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2857 | if (!readonly && return_free_space && |
| 2858 | global_rsv->space_info == space_info) { |
| 2859 | u64 to_add = len; |
Nikolay Borisov | 92ac58e | 2017-08-17 10:52:28 +0300 | [diff] [blame] | 2860 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2861 | spin_lock(&global_rsv->lock); |
| 2862 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2863 | to_add = min(len, global_rsv->size - |
| 2864 | global_rsv->reserved); |
| 2865 | global_rsv->reserved += to_add; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2866 | btrfs_space_info_update_bytes_may_use(fs_info, |
| 2867 | space_info, to_add); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2868 | if (global_rsv->reserved >= global_rsv->size) |
| 2869 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2870 | trace_btrfs_space_reservation(fs_info, |
| 2871 | "space_info", |
| 2872 | space_info->flags, |
| 2873 | to_add, 1); |
| 2874 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2875 | } |
| 2876 | spin_unlock(&global_rsv->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2877 | /* Add to any tickets we may have */ |
| 2878 | if (len) |
Josef Bacik | d44b72a | 2019-06-18 16:09:18 -0400 | [diff] [blame] | 2879 | btrfs_space_info_add_new_bytes(fs_info, |
| 2880 | space_info, len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2881 | } |
| 2882 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2886 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2887 | return 0; |
| 2888 | } |
| 2889 | |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2890 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2891 | { |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2892 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2893 | struct btrfs_block_group_cache *block_group, *tmp; |
| 2894 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2895 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2896 | u64 start; |
| 2897 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2898 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2899 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2900 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 2901 | unpin = &fs_info->freed_extents[1]; |
| 2902 | else |
| 2903 | unpin = &fs_info->freed_extents[0]; |
| 2904 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2905 | while (!trans->aborted) { |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2906 | struct extent_state *cached_state = NULL; |
| 2907 | |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2908 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2909 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2910 | EXTENT_DIRTY, &cached_state); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2911 | if (ret) { |
| 2912 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2913 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2914 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2915 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2916 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2917 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2918 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2919 | |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2920 | clear_extent_dirty(unpin, start, end, &cached_state); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2921 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2922 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2923 | free_extent_state(cached_state); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2924 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2925 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2926 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2927 | /* |
| 2928 | * Transaction is finished. We don't need the lock anymore. We |
| 2929 | * do need to clean up the block groups in case of a transaction |
| 2930 | * abort. |
| 2931 | */ |
| 2932 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 2933 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 2934 | u64 trimmed = 0; |
| 2935 | |
| 2936 | ret = -EROFS; |
| 2937 | if (!trans->aborted) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2938 | ret = btrfs_discard_extent(fs_info, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2939 | block_group->key.objectid, |
| 2940 | block_group->key.offset, |
| 2941 | &trimmed); |
| 2942 | |
| 2943 | list_del_init(&block_group->bg_list); |
| 2944 | btrfs_put_block_group_trimming(block_group); |
| 2945 | btrfs_put_block_group(block_group); |
| 2946 | |
| 2947 | if (ret) { |
| 2948 | const char *errstr = btrfs_decode_error(ret); |
| 2949 | btrfs_warn(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 2950 | "discard failed while removing blockgroup: errno=%d %s", |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2951 | ret, errstr); |
| 2952 | } |
| 2953 | } |
| 2954 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2955 | return 0; |
| 2956 | } |
| 2957 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2958 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2959 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 2960 | u64 root_objectid, u64 owner_objectid, |
| 2961 | u64 owner_offset, int refs_to_drop, |
| 2962 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2963 | { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2964 | struct btrfs_fs_info *info = trans->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2965 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2966 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2967 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2968 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2969 | struct btrfs_extent_item *ei; |
| 2970 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2971 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2972 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2973 | int extent_slot = 0; |
| 2974 | int found_extent = 0; |
| 2975 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2976 | u32 item_size; |
| 2977 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2978 | u64 bytenr = node->bytenr; |
| 2979 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2980 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2981 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2982 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2983 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2984 | if (!path) |
| 2985 | return -ENOMEM; |
| 2986 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2987 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2988 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2989 | |
| 2990 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 2991 | BUG_ON(!is_data && refs_to_drop != 1); |
| 2992 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2993 | if (is_data) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 2994 | skinny_metadata = false; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2995 | |
Nikolay Borisov | fbe4801 | 2018-06-20 15:48:52 +0300 | [diff] [blame] | 2996 | ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes, |
| 2997 | parent, root_objectid, owner_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2998 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2999 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3000 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3001 | while (extent_slot >= 0) { |
| 3002 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3003 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3004 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3005 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3006 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3007 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3008 | found_extent = 1; |
| 3009 | break; |
| 3010 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3011 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 3012 | key.offset == owner_objectid) { |
| 3013 | found_extent = 1; |
| 3014 | break; |
| 3015 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3016 | if (path->slots[0] - extent_slot > 5) |
| 3017 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3018 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3019 | } |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 3020 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3021 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3022 | BUG_ON(iref); |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3023 | ret = remove_extent_backref(trans, path, NULL, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 3024 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3025 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3026 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3027 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3028 | goto out; |
| 3029 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3030 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3031 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3032 | |
| 3033 | key.objectid = bytenr; |
| 3034 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3035 | key.offset = num_bytes; |
| 3036 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3037 | if (!is_data && skinny_metadata) { |
| 3038 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3039 | key.offset = owner_objectid; |
| 3040 | } |
| 3041 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3042 | ret = btrfs_search_slot(trans, extent_root, |
| 3043 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3044 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 3045 | /* |
| 3046 | * Couldn't find our skinny metadata item, |
| 3047 | * see if we have ye olde extent item. |
| 3048 | */ |
| 3049 | path->slots[0]--; |
| 3050 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3051 | path->slots[0]); |
| 3052 | if (key.objectid == bytenr && |
| 3053 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3054 | key.offset == num_bytes) |
| 3055 | ret = 0; |
| 3056 | } |
| 3057 | |
| 3058 | if (ret > 0 && skinny_metadata) { |
| 3059 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 3060 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3061 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3062 | key.offset = num_bytes; |
| 3063 | btrfs_release_path(path); |
| 3064 | ret = btrfs_search_slot(trans, extent_root, |
| 3065 | &key, path, -1, 1); |
| 3066 | } |
| 3067 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3068 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3069 | btrfs_err(info, |
| 3070 | "umm, got %d back from search, was looking for %llu", |
| 3071 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 3072 | if (ret > 0) |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3073 | btrfs_print_leaf(path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3074 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3075 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3076 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3077 | goto out; |
| 3078 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3079 | extent_slot = path->slots[0]; |
| 3080 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3081 | } else if (WARN_ON(ret == -ENOENT)) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3082 | btrfs_print_leaf(path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3083 | btrfs_err(info, |
| 3084 | "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] | 3085 | bytenr, parent, root_objectid, owner_objectid, |
| 3086 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3087 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 3088 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3089 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3090 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3091 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3092 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3093 | |
| 3094 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3095 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 3096 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 3097 | ret = -EINVAL; |
| 3098 | btrfs_print_v0_err(info); |
| 3099 | btrfs_abort_transaction(trans, ret); |
| 3100 | goto out; |
| 3101 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3102 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3103 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3104 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3105 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3106 | struct btrfs_tree_block_info *bi; |
| 3107 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3108 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3109 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3110 | } |
| 3111 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3112 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3113 | if (refs < refs_to_drop) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3114 | btrfs_err(info, |
| 3115 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", |
| 3116 | refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3117 | ret = -EINVAL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3118 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3119 | goto out; |
| 3120 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3121 | refs -= refs_to_drop; |
| 3122 | |
| 3123 | if (refs > 0) { |
| 3124 | if (extent_op) |
| 3125 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3126 | /* |
| 3127 | * In the case of inline back ref, reference count will |
| 3128 | * be updated by remove_extent_backref |
| 3129 | */ |
| 3130 | if (iref) { |
| 3131 | BUG_ON(!found_extent); |
| 3132 | } else { |
| 3133 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3134 | btrfs_mark_buffer_dirty(leaf); |
| 3135 | } |
| 3136 | if (found_extent) { |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3137 | ret = remove_extent_backref(trans, path, iref, |
| 3138 | refs_to_drop, is_data, |
| 3139 | &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3140 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3141 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3142 | goto out; |
| 3143 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3144 | } |
| 3145 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3146 | if (found_extent) { |
| 3147 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 3148 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3149 | if (iref) { |
| 3150 | BUG_ON(path->slots[0] != extent_slot); |
| 3151 | } else { |
| 3152 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3153 | path->slots[0] = extent_slot; |
| 3154 | num_to_del = 2; |
| 3155 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3156 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3157 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3158 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3159 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3160 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3161 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3162 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3163 | goto out; |
| 3164 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3165 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3166 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3167 | if (is_data) { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3168 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3169 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3170 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3171 | goto out; |
| 3172 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3173 | } |
| 3174 | |
Nikolay Borisov | e7355e5 | 2018-05-10 15:44:55 +0300 | [diff] [blame] | 3175 | ret = add_to_free_space_tree(trans, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3176 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3177 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3178 | goto out; |
| 3179 | } |
| 3180 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 3181 | ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3182 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3183 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3184 | goto out; |
| 3185 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3186 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3187 | btrfs_release_path(path); |
| 3188 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3189 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3190 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3191 | return ret; |
| 3192 | } |
| 3193 | |
| 3194 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3195 | * 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] | 3196 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3197 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3198 | * removes it from the tree. |
| 3199 | */ |
| 3200 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3201 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3202 | { |
| 3203 | struct btrfs_delayed_ref_head *head; |
| 3204 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3205 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3206 | |
| 3207 | delayed_refs = &trans->transaction->delayed_refs; |
| 3208 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 3209 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3210 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3211 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3212 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3213 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 3214 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3215 | goto out; |
| 3216 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 3217 | if (cleanup_extent_op(head) != NULL) |
| 3218 | goto out; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3219 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3220 | /* |
| 3221 | * waiting for the lock here would deadlock. If someone else has it |
| 3222 | * locked they are already in the process of dropping it anyway |
| 3223 | */ |
| 3224 | if (!mutex_trylock(&head->mutex)) |
| 3225 | goto out; |
| 3226 | |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3227 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3228 | head->processing = 0; |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3229 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3230 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3231 | spin_unlock(&delayed_refs->lock); |
| 3232 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3233 | BUG_ON(head->extent_op); |
| 3234 | if (head->must_insert_reserved) |
| 3235 | ret = 1; |
| 3236 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 3237 | btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3238 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 3239 | btrfs_put_delayed_ref_head(head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3240 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3241 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3242 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3243 | |
| 3244 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3245 | spin_unlock(&delayed_refs->lock); |
| 3246 | return 0; |
| 3247 | } |
| 3248 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3249 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 3250 | struct btrfs_root *root, |
| 3251 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3252 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3253 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3254 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3255 | struct btrfs_ref generic_ref = { 0 }; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3256 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3257 | int ret; |
| 3258 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3259 | btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, |
| 3260 | buf->start, buf->len, parent); |
| 3261 | btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), |
| 3262 | root->root_key.objectid); |
| 3263 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3264 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3265 | int old_ref_mod, new_ref_mod; |
| 3266 | |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3267 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3268 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3269 | &old_ref_mod, &new_ref_mod); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3270 | BUG_ON(ret); /* -ENOMEM */ |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3271 | pin = old_ref_mod >= 0 && new_ref_mod < 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3272 | } |
| 3273 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3274 | if (last_ref && btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3275 | struct btrfs_block_group_cache *cache; |
| 3276 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3277 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3278 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3279 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3280 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3281 | } |
| 3282 | |
Omar Sandoval | 4da8b76 | 2017-06-06 16:45:28 -0700 | [diff] [blame] | 3283 | pin = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3284 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3285 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3286 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 3287 | pin_down_extent(cache, buf->start, buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3288 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3289 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 3293 | |
| 3294 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 3295 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3296 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 3297 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3298 | } |
| 3299 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3300 | if (pin) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 3301 | add_pinned_bytes(fs_info, &generic_ref); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3302 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3303 | if (last_ref) { |
| 3304 | /* |
| 3305 | * Deleting the buffer, clear the corrupt flag since it doesn't |
| 3306 | * matter anymore. |
| 3307 | */ |
| 3308 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
| 3309 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3310 | } |
| 3311 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3312 | /* Can return -ENOMEM */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3313 | 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] | 3314 | { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3315 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3316 | int old_ref_mod, new_ref_mod; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3317 | int ret; |
| 3318 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 3319 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3320 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3321 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3322 | /* |
| 3323 | * tree log blocks never actually go into the extent allocation |
| 3324 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3325 | */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3326 | if ((ref->type == BTRFS_REF_METADATA && |
| 3327 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3328 | (ref->type == BTRFS_REF_DATA && |
| 3329 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3330 | /* unlocks the pinned mutex */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3331 | btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3332 | old_ref_mod = new_ref_mod = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3333 | ret = 0; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3334 | } else if (ref->type == BTRFS_REF_METADATA) { |
| 3335 | ret = btrfs_add_delayed_tree_ref(trans, ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3336 | &old_ref_mod, &new_ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3337 | } else { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3338 | ret = btrfs_add_delayed_data_ref(trans, ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3339 | &old_ref_mod, &new_ref_mod); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3340 | } |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3341 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3342 | if (!((ref->type == BTRFS_REF_METADATA && |
| 3343 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3344 | (ref->type == BTRFS_REF_DATA && |
| 3345 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID))) |
| 3346 | btrfs_ref_tree_mod(fs_info, ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3347 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 3348 | if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 3349 | add_pinned_bytes(fs_info, ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3350 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3351 | return ret; |
| 3352 | } |
| 3353 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3354 | enum btrfs_loop_type { |
David Sterba | f262fa8 | 2019-06-18 20:00:08 +0200 | [diff] [blame] | 3355 | LOOP_CACHING_NOWAIT, |
| 3356 | LOOP_CACHING_WAIT, |
| 3357 | LOOP_ALLOC_CHUNK, |
| 3358 | LOOP_NO_EMPTY_SIZE, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3359 | }; |
| 3360 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3361 | static inline void |
| 3362 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 3363 | int delalloc) |
| 3364 | { |
| 3365 | if (delalloc) |
| 3366 | down_read(&cache->data_rwsem); |
| 3367 | } |
| 3368 | |
| 3369 | static inline void |
| 3370 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 3371 | int delalloc) |
| 3372 | { |
| 3373 | btrfs_get_block_group(cache); |
| 3374 | if (delalloc) |
| 3375 | down_read(&cache->data_rwsem); |
| 3376 | } |
| 3377 | |
| 3378 | static struct btrfs_block_group_cache * |
| 3379 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 3380 | struct btrfs_free_cluster *cluster, |
| 3381 | int delalloc) |
| 3382 | { |
Sudip Mukherjee | 89771cc | 2016-02-16 13:32:47 +0530 | [diff] [blame] | 3383 | struct btrfs_block_group_cache *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3384 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3385 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3386 | while (1) { |
| 3387 | used_bg = cluster->block_group; |
| 3388 | if (!used_bg) |
| 3389 | return NULL; |
| 3390 | |
| 3391 | if (used_bg == block_group) |
| 3392 | return used_bg; |
| 3393 | |
| 3394 | btrfs_get_block_group(used_bg); |
| 3395 | |
| 3396 | if (!delalloc) |
| 3397 | return used_bg; |
| 3398 | |
| 3399 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 3400 | return used_bg; |
| 3401 | |
| 3402 | spin_unlock(&cluster->refill_lock); |
| 3403 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 3404 | /* We should only have one-level nested. */ |
| 3405 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3406 | |
| 3407 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3408 | if (used_bg == cluster->block_group) |
| 3409 | return used_bg; |
| 3410 | |
| 3411 | up_read(&used_bg->data_rwsem); |
| 3412 | btrfs_put_block_group(used_bg); |
| 3413 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | static inline void |
| 3417 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 3418 | int delalloc) |
| 3419 | { |
| 3420 | if (delalloc) |
| 3421 | up_read(&cache->data_rwsem); |
| 3422 | btrfs_put_block_group(cache); |
| 3423 | } |
| 3424 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3425 | /* |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3426 | * Structure used internally for find_free_extent() function. Wraps needed |
| 3427 | * parameters. |
| 3428 | */ |
| 3429 | struct find_free_extent_ctl { |
| 3430 | /* Basic allocation info */ |
| 3431 | u64 ram_bytes; |
| 3432 | u64 num_bytes; |
| 3433 | u64 empty_size; |
| 3434 | u64 flags; |
| 3435 | int delalloc; |
| 3436 | |
| 3437 | /* Where to start the search inside the bg */ |
| 3438 | u64 search_start; |
| 3439 | |
| 3440 | /* For clustered allocation */ |
| 3441 | u64 empty_cluster; |
| 3442 | |
| 3443 | bool have_caching_bg; |
| 3444 | bool orig_have_caching_bg; |
| 3445 | |
| 3446 | /* RAID index, converted from flags */ |
| 3447 | int index; |
| 3448 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3449 | /* |
| 3450 | * Current loop number, check find_free_extent_update_loop() for details |
| 3451 | */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3452 | int loop; |
| 3453 | |
| 3454 | /* |
| 3455 | * Whether we're refilling a cluster, if true we need to re-search |
| 3456 | * current block group but don't try to refill the cluster again. |
| 3457 | */ |
| 3458 | bool retry_clustered; |
| 3459 | |
| 3460 | /* |
| 3461 | * Whether we're updating free space cache, if true we need to re-search |
| 3462 | * current block group but don't try updating free space cache again. |
| 3463 | */ |
| 3464 | bool retry_unclustered; |
| 3465 | |
| 3466 | /* If current block group is cached */ |
| 3467 | int cached; |
| 3468 | |
| 3469 | /* Max contiguous hole found */ |
| 3470 | u64 max_extent_size; |
| 3471 | |
| 3472 | /* Total free space from free space cache, not always contiguous */ |
| 3473 | u64 total_free_space; |
| 3474 | |
| 3475 | /* Found result */ |
| 3476 | u64 found_offset; |
| 3477 | }; |
| 3478 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3479 | |
| 3480 | /* |
| 3481 | * Helper function for find_free_extent(). |
| 3482 | * |
| 3483 | * Return -ENOENT to inform caller that we need fallback to unclustered mode. |
| 3484 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3485 | * Return >0 to inform caller that we find nothing |
| 3486 | * Return 0 means we have found a location and set ffe_ctl->found_offset. |
| 3487 | */ |
| 3488 | static int find_free_extent_clustered(struct btrfs_block_group_cache *bg, |
| 3489 | struct btrfs_free_cluster *last_ptr, |
| 3490 | struct find_free_extent_ctl *ffe_ctl, |
| 3491 | struct btrfs_block_group_cache **cluster_bg_ret) |
| 3492 | { |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3493 | struct btrfs_block_group_cache *cluster_bg; |
| 3494 | u64 aligned_cluster; |
| 3495 | u64 offset; |
| 3496 | int ret; |
| 3497 | |
| 3498 | cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc); |
| 3499 | if (!cluster_bg) |
| 3500 | goto refill_cluster; |
| 3501 | if (cluster_bg != bg && (cluster_bg->ro || |
| 3502 | !block_group_bits(cluster_bg, ffe_ctl->flags))) |
| 3503 | goto release_cluster; |
| 3504 | |
| 3505 | offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr, |
| 3506 | ffe_ctl->num_bytes, cluster_bg->key.objectid, |
| 3507 | &ffe_ctl->max_extent_size); |
| 3508 | if (offset) { |
| 3509 | /* We have a block, we're done */ |
| 3510 | spin_unlock(&last_ptr->refill_lock); |
| 3511 | trace_btrfs_reserve_extent_cluster(cluster_bg, |
| 3512 | ffe_ctl->search_start, ffe_ctl->num_bytes); |
| 3513 | *cluster_bg_ret = cluster_bg; |
| 3514 | ffe_ctl->found_offset = offset; |
| 3515 | return 0; |
| 3516 | } |
| 3517 | WARN_ON(last_ptr->block_group != cluster_bg); |
| 3518 | |
| 3519 | release_cluster: |
| 3520 | /* |
| 3521 | * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so |
| 3522 | * lets just skip it and let the allocator find whatever block it can |
| 3523 | * find. If we reach this point, we will have tried the cluster |
| 3524 | * allocator plenty of times and not have found anything, so we are |
| 3525 | * likely way too fragmented for the clustering stuff to find anything. |
| 3526 | * |
| 3527 | * However, if the cluster is taken from the current block group, |
| 3528 | * release the cluster first, so that we stand a better chance of |
| 3529 | * succeeding in the unclustered allocation. |
| 3530 | */ |
| 3531 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) { |
| 3532 | spin_unlock(&last_ptr->refill_lock); |
| 3533 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3534 | return -ENOENT; |
| 3535 | } |
| 3536 | |
| 3537 | /* This cluster didn't work out, free it and start over */ |
| 3538 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3539 | |
| 3540 | if (cluster_bg != bg) |
| 3541 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3542 | |
| 3543 | refill_cluster: |
| 3544 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) { |
| 3545 | spin_unlock(&last_ptr->refill_lock); |
| 3546 | return -ENOENT; |
| 3547 | } |
| 3548 | |
| 3549 | aligned_cluster = max_t(u64, |
| 3550 | ffe_ctl->empty_cluster + ffe_ctl->empty_size, |
| 3551 | bg->full_stripe_len); |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3552 | ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start, |
| 3553 | ffe_ctl->num_bytes, aligned_cluster); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3554 | if (ret == 0) { |
| 3555 | /* Now pull our allocation out of this cluster */ |
| 3556 | offset = btrfs_alloc_from_cluster(bg, last_ptr, |
| 3557 | ffe_ctl->num_bytes, ffe_ctl->search_start, |
| 3558 | &ffe_ctl->max_extent_size); |
| 3559 | if (offset) { |
| 3560 | /* We found one, proceed */ |
| 3561 | spin_unlock(&last_ptr->refill_lock); |
| 3562 | trace_btrfs_reserve_extent_cluster(bg, |
| 3563 | ffe_ctl->search_start, |
| 3564 | ffe_ctl->num_bytes); |
| 3565 | ffe_ctl->found_offset = offset; |
| 3566 | return 0; |
| 3567 | } |
| 3568 | } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT && |
| 3569 | !ffe_ctl->retry_clustered) { |
| 3570 | spin_unlock(&last_ptr->refill_lock); |
| 3571 | |
| 3572 | ffe_ctl->retry_clustered = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3573 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3574 | ffe_ctl->empty_cluster + ffe_ctl->empty_size); |
| 3575 | return -EAGAIN; |
| 3576 | } |
| 3577 | /* |
| 3578 | * At this point we either didn't find a cluster or we weren't able to |
| 3579 | * allocate a block from our cluster. Free the cluster we've been |
| 3580 | * trying to use, and go to the next block group. |
| 3581 | */ |
| 3582 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3583 | spin_unlock(&last_ptr->refill_lock); |
| 3584 | return 1; |
| 3585 | } |
| 3586 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3587 | /* |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3588 | * Return >0 to inform caller that we find nothing |
| 3589 | * Return 0 when we found an free extent and set ffe_ctrl->found_offset |
| 3590 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3591 | */ |
| 3592 | static int find_free_extent_unclustered(struct btrfs_block_group_cache *bg, |
| 3593 | struct btrfs_free_cluster *last_ptr, |
| 3594 | struct find_free_extent_ctl *ffe_ctl) |
| 3595 | { |
| 3596 | u64 offset; |
| 3597 | |
| 3598 | /* |
| 3599 | * We are doing an unclustered allocation, set the fragmented flag so |
| 3600 | * we don't bother trying to setup a cluster again until we get more |
| 3601 | * space. |
| 3602 | */ |
| 3603 | if (unlikely(last_ptr)) { |
| 3604 | spin_lock(&last_ptr->lock); |
| 3605 | last_ptr->fragmented = 1; |
| 3606 | spin_unlock(&last_ptr->lock); |
| 3607 | } |
| 3608 | if (ffe_ctl->cached) { |
| 3609 | struct btrfs_free_space_ctl *free_space_ctl; |
| 3610 | |
| 3611 | free_space_ctl = bg->free_space_ctl; |
| 3612 | spin_lock(&free_space_ctl->tree_lock); |
| 3613 | if (free_space_ctl->free_space < |
| 3614 | ffe_ctl->num_bytes + ffe_ctl->empty_cluster + |
| 3615 | ffe_ctl->empty_size) { |
| 3616 | ffe_ctl->total_free_space = max_t(u64, |
| 3617 | ffe_ctl->total_free_space, |
| 3618 | free_space_ctl->free_space); |
| 3619 | spin_unlock(&free_space_ctl->tree_lock); |
| 3620 | return 1; |
| 3621 | } |
| 3622 | spin_unlock(&free_space_ctl->tree_lock); |
| 3623 | } |
| 3624 | |
| 3625 | offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start, |
| 3626 | ffe_ctl->num_bytes, ffe_ctl->empty_size, |
| 3627 | &ffe_ctl->max_extent_size); |
| 3628 | |
| 3629 | /* |
| 3630 | * If we didn't find a chunk, and we haven't failed on this block group |
| 3631 | * before, and this block group is in the middle of caching and we are |
| 3632 | * ok with waiting, then go ahead and wait for progress to be made, and |
| 3633 | * set @retry_unclustered to true. |
| 3634 | * |
| 3635 | * If @retry_unclustered is true then we've already waited on this |
| 3636 | * block group once and should move on to the next block group. |
| 3637 | */ |
| 3638 | if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached && |
| 3639 | ffe_ctl->loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3640 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
| 3641 | ffe_ctl->empty_size); |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3642 | ffe_ctl->retry_unclustered = true; |
| 3643 | return -EAGAIN; |
| 3644 | } else if (!offset) { |
| 3645 | return 1; |
| 3646 | } |
| 3647 | ffe_ctl->found_offset = offset; |
| 3648 | return 0; |
| 3649 | } |
| 3650 | |
| 3651 | /* |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3652 | * Return >0 means caller needs to re-search for free extent |
| 3653 | * Return 0 means we have the needed free extent. |
| 3654 | * Return <0 means we failed to locate any free extent. |
| 3655 | */ |
| 3656 | static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info, |
| 3657 | struct btrfs_free_cluster *last_ptr, |
| 3658 | struct btrfs_key *ins, |
| 3659 | struct find_free_extent_ctl *ffe_ctl, |
| 3660 | int full_search, bool use_cluster) |
| 3661 | { |
| 3662 | struct btrfs_root *root = fs_info->extent_root; |
| 3663 | int ret; |
| 3664 | |
| 3665 | if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) && |
| 3666 | ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg) |
| 3667 | ffe_ctl->orig_have_caching_bg = true; |
| 3668 | |
| 3669 | if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT && |
| 3670 | ffe_ctl->have_caching_bg) |
| 3671 | return 1; |
| 3672 | |
| 3673 | if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES) |
| 3674 | return 1; |
| 3675 | |
| 3676 | if (ins->objectid) { |
| 3677 | if (!use_cluster && last_ptr) { |
| 3678 | spin_lock(&last_ptr->lock); |
| 3679 | last_ptr->window_start = ins->objectid; |
| 3680 | spin_unlock(&last_ptr->lock); |
| 3681 | } |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
| 3685 | /* |
| 3686 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 3687 | * caching kthreads as we move along |
| 3688 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 3689 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 3690 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 3691 | * again |
| 3692 | */ |
| 3693 | if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) { |
| 3694 | ffe_ctl->index = 0; |
| 3695 | if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) { |
| 3696 | /* |
| 3697 | * We want to skip the LOOP_CACHING_WAIT step if we |
| 3698 | * don't have any uncached bgs and we've already done a |
| 3699 | * full search through. |
| 3700 | */ |
| 3701 | if (ffe_ctl->orig_have_caching_bg || !full_search) |
| 3702 | ffe_ctl->loop = LOOP_CACHING_WAIT; |
| 3703 | else |
| 3704 | ffe_ctl->loop = LOOP_ALLOC_CHUNK; |
| 3705 | } else { |
| 3706 | ffe_ctl->loop++; |
| 3707 | } |
| 3708 | |
| 3709 | if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) { |
| 3710 | struct btrfs_trans_handle *trans; |
| 3711 | int exist = 0; |
| 3712 | |
| 3713 | trans = current->journal_info; |
| 3714 | if (trans) |
| 3715 | exist = 1; |
| 3716 | else |
| 3717 | trans = btrfs_join_transaction(root); |
| 3718 | |
| 3719 | if (IS_ERR(trans)) { |
| 3720 | ret = PTR_ERR(trans); |
| 3721 | return ret; |
| 3722 | } |
| 3723 | |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 3724 | ret = btrfs_chunk_alloc(trans, ffe_ctl->flags, |
| 3725 | CHUNK_ALLOC_FORCE); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3726 | |
| 3727 | /* |
| 3728 | * If we can't allocate a new chunk we've already looped |
| 3729 | * through at least once, move on to the NO_EMPTY_SIZE |
| 3730 | * case. |
| 3731 | */ |
| 3732 | if (ret == -ENOSPC) |
| 3733 | ffe_ctl->loop = LOOP_NO_EMPTY_SIZE; |
| 3734 | |
| 3735 | /* Do not bail out on ENOSPC since we can do more. */ |
| 3736 | if (ret < 0 && ret != -ENOSPC) |
| 3737 | btrfs_abort_transaction(trans, ret); |
| 3738 | else |
| 3739 | ret = 0; |
| 3740 | if (!exist) |
| 3741 | btrfs_end_transaction(trans); |
| 3742 | if (ret) |
| 3743 | return ret; |
| 3744 | } |
| 3745 | |
| 3746 | if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) { |
| 3747 | /* |
| 3748 | * Don't loop again if we already have no empty_size and |
| 3749 | * no empty_cluster. |
| 3750 | */ |
| 3751 | if (ffe_ctl->empty_size == 0 && |
| 3752 | ffe_ctl->empty_cluster == 0) |
| 3753 | return -ENOSPC; |
| 3754 | ffe_ctl->empty_size = 0; |
| 3755 | ffe_ctl->empty_cluster = 0; |
| 3756 | } |
| 3757 | return 1; |
| 3758 | } |
| 3759 | return -ENOSPC; |
| 3760 | } |
| 3761 | |
| 3762 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3763 | * walks the btree of allocated extents and find a hole of a given size. |
| 3764 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3765 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3766 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3767 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3768 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3769 | * |
| 3770 | * If there is no suitable free space, we will record the max size of |
| 3771 | * the free space extent currently. |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3772 | * |
| 3773 | * The overall logic and call chain: |
| 3774 | * |
| 3775 | * find_free_extent() |
| 3776 | * |- Iterate through all block groups |
| 3777 | * | |- Get a valid block group |
| 3778 | * | |- Try to do clustered allocation in that block group |
| 3779 | * | |- Try to do unclustered allocation in that block group |
| 3780 | * | |- Check if the result is valid |
| 3781 | * | | |- If valid, then exit |
| 3782 | * | |- Jump to next block group |
| 3783 | * | |
| 3784 | * |- Push harder to find free extents |
| 3785 | * |- If not found, re-iterate all block groups |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3786 | */ |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 3787 | static noinline int find_free_extent(struct btrfs_fs_info *fs_info, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 3788 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
| 3789 | u64 hint_byte, struct btrfs_key *ins, |
| 3790 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3791 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3792 | int ret = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3793 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3794 | struct btrfs_block_group_cache *block_group = NULL; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3795 | struct find_free_extent_ctl ffe_ctl = {0}; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3796 | struct btrfs_space_info *space_info; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3797 | bool use_cluster = true; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 3798 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3799 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3800 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3801 | |
| 3802 | ffe_ctl.ram_bytes = ram_bytes; |
| 3803 | ffe_ctl.num_bytes = num_bytes; |
| 3804 | ffe_ctl.empty_size = empty_size; |
| 3805 | ffe_ctl.flags = flags; |
| 3806 | ffe_ctl.search_start = 0; |
| 3807 | ffe_ctl.retry_clustered = false; |
| 3808 | ffe_ctl.retry_unclustered = false; |
| 3809 | ffe_ctl.delalloc = delalloc; |
| 3810 | ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags); |
| 3811 | ffe_ctl.have_caching_bg = false; |
| 3812 | ffe_ctl.orig_have_caching_bg = false; |
| 3813 | ffe_ctl.found_offset = 0; |
| 3814 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3815 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3816 | ins->objectid = 0; |
| 3817 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3818 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 3819 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3820 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 3821 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 3822 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3823 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 3824 | return -ENOSPC; |
| 3825 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3826 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3827 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3828 | * If our free space is heavily fragmented we may not be able to make |
| 3829 | * big contiguous allocations, so instead of doing the expensive search |
| 3830 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 3831 | * can go ahead and search for a more manageable chunk. |
| 3832 | * |
| 3833 | * If our max_extent_size is large enough for our allocation simply |
| 3834 | * disable clustering since we will likely not be able to find enough |
| 3835 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3836 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3837 | if (unlikely(space_info->max_extent_size)) { |
| 3838 | spin_lock(&space_info->lock); |
| 3839 | if (space_info->max_extent_size && |
| 3840 | num_bytes > space_info->max_extent_size) { |
| 3841 | ins->offset = space_info->max_extent_size; |
| 3842 | spin_unlock(&space_info->lock); |
| 3843 | return -ENOSPC; |
| 3844 | } else if (space_info->max_extent_size) { |
| 3845 | use_cluster = false; |
| 3846 | } |
| 3847 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3848 | } |
| 3849 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3850 | last_ptr = fetch_cluster_info(fs_info, space_info, |
| 3851 | &ffe_ctl.empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3852 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3853 | spin_lock(&last_ptr->lock); |
| 3854 | if (last_ptr->block_group) |
| 3855 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3856 | if (last_ptr->fragmented) { |
| 3857 | /* |
| 3858 | * We still set window_start so we can keep track of the |
| 3859 | * last place we found an allocation to try and save |
| 3860 | * some time. |
| 3861 | */ |
| 3862 | hint_byte = last_ptr->window_start; |
| 3863 | use_cluster = false; |
| 3864 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3865 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3866 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3867 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3868 | ffe_ctl.search_start = max(ffe_ctl.search_start, |
| 3869 | first_logical_byte(fs_info, 0)); |
| 3870 | ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte); |
| 3871 | if (ffe_ctl.search_start == hint_byte) { |
| 3872 | block_group = btrfs_lookup_block_group(fs_info, |
| 3873 | ffe_ctl.search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3874 | /* |
| 3875 | * we don't want to use the block group if it doesn't match our |
| 3876 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3877 | * |
| 3878 | * However if we are re-searching with an ideal block group |
| 3879 | * 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] | 3880 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 3881 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 3882 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3883 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3884 | if (list_empty(&block_group->list) || |
| 3885 | block_group->ro) { |
| 3886 | /* |
| 3887 | * someone is removing this block group, |
| 3888 | * we can't jump into the have_block_group |
| 3889 | * target because our list pointers are not |
| 3890 | * valid |
| 3891 | */ |
| 3892 | btrfs_put_block_group(block_group); |
| 3893 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3894 | } else { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3895 | ffe_ctl.index = btrfs_bg_flags_to_raid_index( |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 3896 | block_group->flags); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3897 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3898 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3899 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3900 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3901 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3902 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3903 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3904 | search: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3905 | ffe_ctl.have_caching_bg = false; |
| 3906 | if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) || |
| 3907 | ffe_ctl.index == 0) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 3908 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3909 | down_read(&space_info->groups_sem); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3910 | list_for_each_entry(block_group, |
| 3911 | &space_info->block_groups[ffe_ctl.index], list) { |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 3912 | /* If the block group is read-only, we can skip it entirely. */ |
| 3913 | if (unlikely(block_group->ro)) |
| 3914 | continue; |
| 3915 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3916 | btrfs_grab_block_group(block_group, delalloc); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3917 | ffe_ctl.search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3918 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3919 | /* |
| 3920 | * this can happen if we end up cycling through all the |
| 3921 | * raid types, but we want to make sure we only allocate |
| 3922 | * for the proper type. |
| 3923 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 3924 | if (!block_group_bits(block_group, flags)) { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 3925 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 3926 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 3927 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3928 | BTRFS_BLOCK_GROUP_RAID10; |
| 3929 | |
| 3930 | /* |
| 3931 | * if they asked for extra copies and this block group |
| 3932 | * doesn't provide them, bail. This does allow us to |
| 3933 | * fill raid0 from raid1. |
| 3934 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 3935 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3936 | goto loop; |
Qu Wenruo | 2a28468 | 2019-07-16 17:00:33 +0800 | [diff] [blame] | 3937 | |
| 3938 | /* |
| 3939 | * This block group has different flags than we want. |
| 3940 | * It's possible that we have MIXED_GROUP flag but no |
| 3941 | * block group is mixed. Just skip such block group. |
| 3942 | */ |
| 3943 | btrfs_release_block_group(block_group, delalloc); |
| 3944 | continue; |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3945 | } |
| 3946 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3947 | have_block_group: |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3948 | ffe_ctl.cached = btrfs_block_group_cache_done(block_group); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3949 | if (unlikely(!ffe_ctl.cached)) { |
| 3950 | ffe_ctl.have_caching_bg = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3951 | ret = btrfs_cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 3952 | BUG_ON(ret < 0); |
| 3953 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3954 | } |
| 3955 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 3956 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 3957 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3958 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3959 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 3960 | * Ok we want to try and use the cluster allocator, so |
| 3961 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3962 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3963 | if (last_ptr && use_cluster) { |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3964 | struct btrfs_block_group_cache *cluster_bg = NULL; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3965 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3966 | ret = find_free_extent_clustered(block_group, last_ptr, |
| 3967 | &ffe_ctl, &cluster_bg); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 3968 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3969 | if (ret == 0) { |
| 3970 | if (cluster_bg && cluster_bg != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3971 | btrfs_release_block_group(block_group, |
| 3972 | delalloc); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3973 | block_group = cluster_bg; |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 3974 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3975 | goto checks; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3976 | } else if (ret == -EAGAIN) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3977 | goto have_block_group; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3978 | } else if (ret > 0) { |
| 3979 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3980 | } |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3981 | /* ret == -ENOENT case falls through */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3982 | } |
| 3983 | |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3984 | ret = find_free_extent_unclustered(block_group, last_ptr, |
| 3985 | &ffe_ctl); |
| 3986 | if (ret == -EAGAIN) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3987 | goto have_block_group; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3988 | else if (ret > 0) |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 3989 | goto loop; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3990 | /* ret == 0 case falls through */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3991 | checks: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3992 | ffe_ctl.search_start = round_up(ffe_ctl.found_offset, |
| 3993 | fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3994 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3995 | /* move on to the next group */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3996 | if (ffe_ctl.search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 3997 | block_group->key.objectid + block_group->key.offset) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3998 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 3999 | num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4000 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4001 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4002 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4003 | if (ffe_ctl.found_offset < ffe_ctl.search_start) |
| 4004 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4005 | ffe_ctl.search_start - ffe_ctl.found_offset); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4006 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4007 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 4008 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4009 | if (ret == -EAGAIN) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4010 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4011 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4012 | goto loop; |
| 4013 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4014 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4015 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4016 | /* we are all good, lets return */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4017 | ins->objectid = ffe_ctl.search_start; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4018 | ins->offset = num_bytes; |
| 4019 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4020 | trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start, |
| 4021 | num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4022 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4023 | break; |
| 4024 | loop: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4025 | ffe_ctl.retry_clustered = false; |
| 4026 | ffe_ctl.retry_unclustered = false; |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4027 | BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) != |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4028 | ffe_ctl.index); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4029 | btrfs_release_block_group(block_group, delalloc); |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 4030 | cond_resched(); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4031 | } |
| 4032 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4033 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 4034 | ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl, |
| 4035 | full_search, use_cluster); |
| 4036 | if (ret > 0) |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 4037 | goto search; |
| 4038 | |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4039 | if (ret == -ENOSPC) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4040 | /* |
| 4041 | * Use ffe_ctl->total_free_space as fallback if we can't find |
| 4042 | * any contiguous hole. |
| 4043 | */ |
| 4044 | if (!ffe_ctl.max_extent_size) |
| 4045 | ffe_ctl.max_extent_size = ffe_ctl.total_free_space; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4046 | spin_lock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4047 | space_info->max_extent_size = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4048 | spin_unlock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4049 | ins->offset = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4050 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4051 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4052 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4053 | |
Nikolay Borisov | 6f47c70 | 2018-03-13 12:22:32 +0200 | [diff] [blame] | 4054 | /* |
| 4055 | * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a |
| 4056 | * hole that is at least as big as @num_bytes. |
| 4057 | * |
| 4058 | * @root - The root that will contain this extent |
| 4059 | * |
| 4060 | * @ram_bytes - The amount of space in ram that @num_bytes take. This |
| 4061 | * is used for accounting purposes. This value differs |
| 4062 | * from @num_bytes only in the case of compressed extents. |
| 4063 | * |
| 4064 | * @num_bytes - Number of bytes to allocate on-disk. |
| 4065 | * |
| 4066 | * @min_alloc_size - Indicates the minimum amount of space that the |
| 4067 | * allocator should try to satisfy. In some cases |
| 4068 | * @num_bytes may be larger than what is required and if |
| 4069 | * the filesystem is fragmented then allocation fails. |
| 4070 | * However, the presence of @min_alloc_size gives a |
| 4071 | * chance to try and satisfy the smaller allocation. |
| 4072 | * |
| 4073 | * @empty_size - A hint that you plan on doing more COW. This is the |
| 4074 | * size in bytes the allocator should try to find free |
| 4075 | * next to the block it returns. This is just a hint and |
| 4076 | * may be ignored by the allocator. |
| 4077 | * |
| 4078 | * @hint_byte - Hint to the allocator to start searching above the byte |
| 4079 | * address passed. It might be ignored. |
| 4080 | * |
| 4081 | * @ins - This key is modified to record the found hole. It will |
| 4082 | * have the following values: |
| 4083 | * ins->objectid == start position |
| 4084 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
| 4085 | * ins->offset == the size of the hole. |
| 4086 | * |
| 4087 | * @is_data - Boolean flag indicating whether an extent is |
| 4088 | * allocated for data (true) or metadata (false) |
| 4089 | * |
| 4090 | * @delalloc - Boolean flag indicating whether this allocation is for |
| 4091 | * delalloc or not. If 'true' data_rwsem of block groups |
| 4092 | * is going to be acquired. |
| 4093 | * |
| 4094 | * |
| 4095 | * Returns 0 when an allocation succeeded or < 0 when an error occurred. In |
| 4096 | * case -ENOSPC is returned then @ins->offset will contain the size of the |
| 4097 | * largest available hole the allocator managed to find. |
| 4098 | */ |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4099 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4100 | u64 num_bytes, u64 min_alloc_size, |
| 4101 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4102 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4103 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4104 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 4105 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4106 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4107 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4108 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 4109 | flags = get_alloc_profile_by_root(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4110 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4111 | WARN_ON(num_bytes < fs_info->sectorsize); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 4112 | ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4113 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4114 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4115 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4116 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4117 | if (!final_tried && ins->offset) { |
| 4118 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4119 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4120 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4121 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4122 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4123 | if (num_bytes == min_alloc_size) |
| 4124 | final_tried = true; |
| 4125 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4126 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4127 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4128 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 4129 | sinfo = btrfs_find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4130 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4131 | "allocation failed flags %llu, wanted %llu", |
| 4132 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 4133 | if (sinfo) |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 4134 | btrfs_dump_space_info(fs_info, sinfo, |
| 4135 | num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4136 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4137 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4138 | |
| 4139 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4140 | } |
| 4141 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4142 | static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4143 | u64 start, u64 len, |
| 4144 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4145 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4146 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4147 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4148 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4149 | cache = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4150 | if (!cache) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4151 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 4152 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4153 | return -ENOSPC; |
| 4154 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4155 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4156 | if (pin) |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4157 | pin_down_extent(cache, start, len, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4158 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4159 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4160 | ret = btrfs_discard_extent(fs_info, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4161 | btrfs_add_free_space(cache, start, len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4162 | btrfs_free_reserved_bytes(cache, len, delalloc); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4163 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4164 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 4165 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4166 | btrfs_put_block_group(cache); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4167 | return ret; |
| 4168 | } |
| 4169 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4170 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4171 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4172 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4173 | return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4174 | } |
| 4175 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4176 | int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4177 | u64 start, u64 len) |
| 4178 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4179 | return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4180 | } |
| 4181 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4182 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4183 | u64 parent, u64 root_objectid, |
| 4184 | u64 flags, u64 owner, u64 offset, |
| 4185 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4186 | { |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4187 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4188 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4189 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4190 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4191 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4192 | struct extent_buffer *leaf; |
| 4193 | int type; |
| 4194 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4195 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4196 | if (parent > 0) |
| 4197 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4198 | else |
| 4199 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4200 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4201 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4202 | |
| 4203 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4204 | if (!path) |
| 4205 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4206 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4207 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4208 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4209 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4210 | if (ret) { |
| 4211 | btrfs_free_path(path); |
| 4212 | return ret; |
| 4213 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4214 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4215 | leaf = path->nodes[0]; |
| 4216 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4217 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4218 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4219 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4220 | btrfs_set_extent_flags(leaf, extent_item, |
| 4221 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4222 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4223 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4224 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4225 | if (parent > 0) { |
| 4226 | struct btrfs_shared_data_ref *ref; |
| 4227 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4228 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4229 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4230 | } else { |
| 4231 | struct btrfs_extent_data_ref *ref; |
| 4232 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4233 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4234 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4235 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4236 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4237 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4238 | |
| 4239 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4240 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4241 | |
Nikolay Borisov | 25a356d | 2018-05-10 15:44:54 +0300 | [diff] [blame] | 4242 | ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4243 | if (ret) |
| 4244 | return ret; |
| 4245 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4246 | ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4247 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4248 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4249 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4250 | BUG(); |
| 4251 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4252 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4253 | return ret; |
| 4254 | } |
| 4255 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4256 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4257 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4258 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4259 | { |
Nikolay Borisov | 9dcdbe0 | 2018-05-21 12:27:20 +0300 | [diff] [blame] | 4260 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4261 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4262 | struct btrfs_extent_item *extent_item; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4263 | struct btrfs_key extent_key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4264 | struct btrfs_tree_block_info *block_info; |
| 4265 | struct btrfs_extent_inline_ref *iref; |
| 4266 | struct btrfs_path *path; |
| 4267 | struct extent_buffer *leaf; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4268 | struct btrfs_delayed_tree_ref *ref; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4269 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4270 | u64 num_bytes; |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4271 | u64 flags = extent_op->flags_to_set; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4272 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4273 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4274 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 4275 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4276 | extent_key.objectid = node->bytenr; |
| 4277 | if (skinny_metadata) { |
| 4278 | extent_key.offset = ref->level; |
| 4279 | extent_key.type = BTRFS_METADATA_ITEM_KEY; |
| 4280 | num_bytes = fs_info->nodesize; |
| 4281 | } else { |
| 4282 | extent_key.offset = node->num_bytes; |
| 4283 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4284 | size += sizeof(*block_info); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4285 | num_bytes = node->num_bytes; |
| 4286 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4287 | |
| 4288 | path = btrfs_alloc_path(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 4289 | if (!path) |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4290 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4291 | |
| 4292 | path->leave_spinning = 1; |
| 4293 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4294 | &extent_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4295 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 4296 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4297 | return ret; |
| 4298 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4299 | |
| 4300 | leaf = path->nodes[0]; |
| 4301 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4302 | struct btrfs_extent_item); |
| 4303 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4304 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4305 | btrfs_set_extent_flags(leaf, extent_item, |
| 4306 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4307 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4308 | if (skinny_metadata) { |
| 4309 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4310 | } else { |
| 4311 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4312 | btrfs_set_tree_block_key(leaf, block_info, &extent_op->key); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4313 | btrfs_set_tree_block_level(leaf, block_info, ref->level); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4314 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4315 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4316 | |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4317 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4318 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4319 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4320 | BTRFS_SHARED_BLOCK_REF_KEY); |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4321 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4322 | } else { |
| 4323 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4324 | BTRFS_TREE_BLOCK_REF_KEY); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4325 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | btrfs_mark_buffer_dirty(leaf); |
| 4329 | btrfs_free_path(path); |
| 4330 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4331 | ret = remove_from_free_space_tree(trans, extent_key.objectid, |
| 4332 | num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4333 | if (ret) |
| 4334 | return ret; |
| 4335 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4336 | ret = btrfs_update_block_group(trans, extent_key.objectid, |
| 4337 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4338 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4339 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4340 | extent_key.objectid, extent_key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4341 | BUG(); |
| 4342 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 4343 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4344 | trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4345 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4346 | return ret; |
| 4347 | } |
| 4348 | |
| 4349 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4350 | struct btrfs_root *root, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 4351 | u64 offset, u64 ram_bytes, |
| 4352 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4353 | { |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4354 | struct btrfs_ref generic_ref = { 0 }; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4355 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4356 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4357 | BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4358 | |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4359 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4360 | ins->objectid, ins->offset, 0); |
| 4361 | btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4362 | btrfs_ref_tree_mod(root->fs_info, &generic_ref); |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4363 | ret = btrfs_add_delayed_data_ref(trans, &generic_ref, |
| 4364 | ram_bytes, NULL, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4365 | return ret; |
| 4366 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4367 | |
| 4368 | /* |
| 4369 | * this is used by the tree logging recovery code. It records that |
| 4370 | * an extent has been allocated and makes sure to clear the free |
| 4371 | * space cache bits as well |
| 4372 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4373 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4374 | u64 root_objectid, u64 owner, u64 offset, |
| 4375 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4376 | { |
Nikolay Borisov | 61da2ab | 2018-06-20 15:49:13 +0300 | [diff] [blame] | 4377 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4378 | int ret; |
| 4379 | struct btrfs_block_group_cache *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4380 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4381 | |
| 4382 | /* |
| 4383 | * 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] | 4384 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4385 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4386 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4387 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 4388 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4389 | if (ret) |
| 4390 | return ret; |
| 4391 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4392 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4393 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4394 | if (!block_group) |
| 4395 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4396 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4397 | space_info = block_group->space_info; |
| 4398 | spin_lock(&space_info->lock); |
| 4399 | spin_lock(&block_group->lock); |
| 4400 | space_info->bytes_reserved += ins->offset; |
| 4401 | block_group->reserved += ins->offset; |
| 4402 | spin_unlock(&block_group->lock); |
| 4403 | spin_unlock(&space_info->lock); |
| 4404 | |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4405 | ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, |
| 4406 | offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4407 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4408 | return ret; |
| 4409 | } |
| 4410 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4411 | static struct extent_buffer * |
| 4412 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4413 | u64 bytenr, int level, u64 owner) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4414 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4415 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4416 | struct extent_buffer *buf; |
| 4417 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4418 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4419 | if (IS_ERR(buf)) |
| 4420 | return buf; |
| 4421 | |
Qu Wenruo | b72c3ab | 2018-08-21 09:53:47 +0800 | [diff] [blame] | 4422 | /* |
| 4423 | * Extra safety check in case the extent tree is corrupted and extent |
| 4424 | * allocator chooses to use a tree block which is already used and |
| 4425 | * locked. |
| 4426 | */ |
| 4427 | if (buf->lock_owner == current->pid) { |
| 4428 | btrfs_err_rl(fs_info, |
| 4429 | "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected", |
| 4430 | buf->start, btrfs_header_owner(buf), current->pid); |
| 4431 | free_extent_buffer(buf); |
| 4432 | return ERR_PTR(-EUCLEAN); |
| 4433 | } |
| 4434 | |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 4435 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4436 | btrfs_tree_lock(buf); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 4437 | btrfs_clean_tree_block(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4438 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4439 | |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4440 | btrfs_set_lock_blocking_write(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 4441 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4442 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4443 | memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header)); |
| 4444 | btrfs_set_header_level(buf, level); |
| 4445 | btrfs_set_header_bytenr(buf, buf->start); |
| 4446 | btrfs_set_header_generation(buf, trans->transid); |
| 4447 | btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV); |
| 4448 | btrfs_set_header_owner(buf, owner); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 4449 | write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4450 | write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4451 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4452 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4453 | /* |
| 4454 | * we allow two log transactions at a time, use different |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4455 | * EXTENT bit to differentiate dirty pages. |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4456 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4457 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4458 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 4459 | buf->start + buf->len - 1, GFP_NOFS); |
| 4460 | else |
| 4461 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 4462 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4463 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4464 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4465 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4466 | buf->start + buf->len - 1, GFP_NOFS); |
| 4467 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 4468 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4469 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4470 | return buf; |
| 4471 | } |
| 4472 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4473 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4474 | * 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] | 4475 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4476 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 4477 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4478 | struct btrfs_root *root, |
| 4479 | u64 parent, u64 root_objectid, |
| 4480 | const struct btrfs_disk_key *key, |
| 4481 | int level, u64 hint, |
| 4482 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4483 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4484 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4485 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4486 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4487 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4488 | struct btrfs_delayed_extent_op *extent_op; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4489 | struct btrfs_ref generic_ref = { 0 }; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4490 | u64 flags = 0; |
| 4491 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4492 | u32 blocksize = fs_info->nodesize; |
| 4493 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4494 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4495 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4496 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4497 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4498 | level, root_objectid); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4499 | if (!IS_ERR(buf)) |
| 4500 | root->alloc_bytenr += blocksize; |
| 4501 | return buf; |
| 4502 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4503 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 4504 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4505 | block_rsv = btrfs_use_block_rsv(trans, root, blocksize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4506 | if (IS_ERR(block_rsv)) |
| 4507 | return ERR_CAST(block_rsv); |
| 4508 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4509 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4510 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4511 | if (ret) |
| 4512 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4513 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4514 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level, |
| 4515 | root_objectid); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4516 | if (IS_ERR(buf)) { |
| 4517 | ret = PTR_ERR(buf); |
| 4518 | goto out_free_reserved; |
| 4519 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4520 | |
| 4521 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4522 | if (parent == 0) |
| 4523 | parent = ins.objectid; |
| 4524 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4525 | } else |
| 4526 | BUG_ON(parent > 0); |
| 4527 | |
| 4528 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 4529 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4530 | if (!extent_op) { |
| 4531 | ret = -ENOMEM; |
| 4532 | goto out_free_buf; |
| 4533 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4534 | if (key) |
| 4535 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4536 | else |
| 4537 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4538 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 4539 | extent_op->update_key = skinny_metadata ? false : true; |
| 4540 | extent_op->update_flags = true; |
| 4541 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4542 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4543 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4544 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4545 | ins.objectid, ins.offset, parent); |
| 4546 | generic_ref.real_root = root->root_key.objectid; |
| 4547 | btrfs_init_tree_ref(&generic_ref, level, root_objectid); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4548 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4549 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, |
Omar Sandoval | 7be0791 | 2017-06-06 16:45:30 -0700 | [diff] [blame] | 4550 | extent_op, NULL, NULL); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4551 | if (ret) |
| 4552 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4553 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4554 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4555 | |
| 4556 | out_free_delayed: |
| 4557 | btrfs_free_delayed_extent_op(extent_op); |
| 4558 | out_free_buf: |
| 4559 | free_extent_buffer(buf); |
| 4560 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4561 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4562 | out_unuse: |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4563 | btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4564 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4565 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4566 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4567 | struct walk_control { |
| 4568 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4569 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4570 | struct btrfs_key update_progress; |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 4571 | struct btrfs_key drop_progress; |
| 4572 | int drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4573 | int stage; |
| 4574 | int level; |
| 4575 | int shared_level; |
| 4576 | int update_ref; |
| 4577 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4578 | int reada_slot; |
| 4579 | int reada_count; |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4580 | int restarted; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4581 | }; |
| 4582 | |
| 4583 | #define DROP_REFERENCE 1 |
| 4584 | #define UPDATE_BACKREF 2 |
| 4585 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4586 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4587 | struct btrfs_root *root, |
| 4588 | struct walk_control *wc, |
| 4589 | struct btrfs_path *path) |
| 4590 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4591 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4592 | u64 bytenr; |
| 4593 | u64 generation; |
| 4594 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4595 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4596 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4597 | struct btrfs_key key; |
| 4598 | struct extent_buffer *eb; |
| 4599 | int ret; |
| 4600 | int slot; |
| 4601 | int nread = 0; |
| 4602 | |
| 4603 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4604 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4605 | wc->reada_count = max(wc->reada_count, 2); |
| 4606 | } else { |
| 4607 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4608 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4609 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | eb = path->nodes[wc->level]; |
| 4613 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4614 | |
| 4615 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 4616 | if (nread >= wc->reada_count) |
| 4617 | break; |
| 4618 | |
| 4619 | cond_resched(); |
| 4620 | bytenr = btrfs_node_blockptr(eb, slot); |
| 4621 | generation = btrfs_node_ptr_generation(eb, slot); |
| 4622 | |
| 4623 | if (slot == path->slots[wc->level]) |
| 4624 | goto reada; |
| 4625 | |
| 4626 | if (wc->stage == UPDATE_BACKREF && |
| 4627 | generation <= root->root_key.offset) |
| 4628 | continue; |
| 4629 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4630 | /* 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] | 4631 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4632 | wc->level - 1, 1, &refs, |
| 4633 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4634 | /* We don't care about errors in readahead. */ |
| 4635 | if (ret < 0) |
| 4636 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4637 | BUG_ON(refs == 0); |
| 4638 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4639 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4640 | if (refs == 1) |
| 4641 | goto reada; |
| 4642 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4643 | if (wc->level == 1 && |
| 4644 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4645 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4646 | if (!wc->update_ref || |
| 4647 | generation <= root->root_key.offset) |
| 4648 | continue; |
| 4649 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 4650 | ret = btrfs_comp_cpu_keys(&key, |
| 4651 | &wc->update_progress); |
| 4652 | if (ret < 0) |
| 4653 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4654 | } else { |
| 4655 | if (wc->level == 1 && |
| 4656 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4657 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4658 | } |
| 4659 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4660 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4661 | nread++; |
| 4662 | } |
| 4663 | wc->reada_slot = slot; |
| 4664 | } |
| 4665 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4666 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4667 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4668 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4669 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 4670 | * back refs for pointers in the block. |
| 4671 | * |
| 4672 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4673 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4674 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 4675 | struct btrfs_root *root, |
| 4676 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4677 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4678 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4679 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4680 | int level = wc->level; |
| 4681 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4682 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4683 | int ret; |
| 4684 | |
| 4685 | if (wc->stage == UPDATE_BACKREF && |
| 4686 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 4687 | return 1; |
| 4688 | |
| 4689 | /* |
| 4690 | * when reference count of tree block is 1, it won't increase |
| 4691 | * again. once full backref flag is set, we never clear it. |
| 4692 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4693 | if (lookup_info && |
| 4694 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 4695 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4696 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4697 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4698 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4699 | &wc->refs[level], |
| 4700 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4701 | BUG_ON(ret == -ENOMEM); |
| 4702 | if (ret) |
| 4703 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4704 | BUG_ON(wc->refs[level] == 0); |
| 4705 | } |
| 4706 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4707 | if (wc->stage == DROP_REFERENCE) { |
| 4708 | if (wc->refs[level] > 1) |
| 4709 | return 1; |
| 4710 | |
| 4711 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4712 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4713 | path->locks[level] = 0; |
| 4714 | } |
| 4715 | return 0; |
| 4716 | } |
| 4717 | |
| 4718 | /* wc->stage == UPDATE_BACKREF */ |
| 4719 | if (!(wc->flags[level] & flag)) { |
| 4720 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4721 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4722 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4723 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4724 | BUG_ON(ret); /* -ENOMEM */ |
David Sterba | f5c8daa | 2019-03-20 11:43:36 +0100 | [diff] [blame] | 4725 | ret = btrfs_set_disk_extent_flags(trans, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4726 | eb->len, flag, |
| 4727 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4728 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4729 | wc->flags[level] |= flag; |
| 4730 | } |
| 4731 | |
| 4732 | /* |
| 4733 | * the block is shared by multiple trees, so it's not good to |
| 4734 | * keep the tree lock |
| 4735 | */ |
| 4736 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4737 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4738 | path->locks[level] = 0; |
| 4739 | } |
| 4740 | return 0; |
| 4741 | } |
| 4742 | |
| 4743 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4744 | * This is used to verify a ref exists for this root to deal with a bug where we |
| 4745 | * would have a drop_progress key that hadn't been updated properly. |
| 4746 | */ |
| 4747 | static int check_ref_exists(struct btrfs_trans_handle *trans, |
| 4748 | struct btrfs_root *root, u64 bytenr, u64 parent, |
| 4749 | int level) |
| 4750 | { |
| 4751 | struct btrfs_path *path; |
| 4752 | struct btrfs_extent_inline_ref *iref; |
| 4753 | int ret; |
| 4754 | |
| 4755 | path = btrfs_alloc_path(); |
| 4756 | if (!path) |
| 4757 | return -ENOMEM; |
| 4758 | |
| 4759 | ret = lookup_extent_backref(trans, path, &iref, bytenr, |
| 4760 | root->fs_info->nodesize, parent, |
| 4761 | root->root_key.objectid, level, 0); |
| 4762 | btrfs_free_path(path); |
| 4763 | if (ret == -ENOENT) |
| 4764 | return 0; |
| 4765 | if (ret < 0) |
| 4766 | return ret; |
| 4767 | return 1; |
| 4768 | } |
| 4769 | |
| 4770 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4771 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4772 | * |
| 4773 | * when wc->stage == DROP_REFERENCE, this function checks |
| 4774 | * reference count of the block pointed to. if the block |
| 4775 | * is shared and we need update back refs for the subtree |
| 4776 | * rooted at the block, this function changes wc->stage to |
| 4777 | * UPDATE_BACKREF. if the block is shared and there is no |
| 4778 | * need to update back, this function drops the reference |
| 4779 | * to the block. |
| 4780 | * |
| 4781 | * NOTE: return value 1 means we should stop walking down. |
| 4782 | */ |
| 4783 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 4784 | struct btrfs_root *root, |
| 4785 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4786 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4787 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4788 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4789 | u64 bytenr; |
| 4790 | u64 generation; |
| 4791 | u64 parent; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4792 | struct btrfs_key key; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4793 | struct btrfs_key first_key; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4794 | struct btrfs_ref ref = { 0 }; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4795 | struct extent_buffer *next; |
| 4796 | int level = wc->level; |
| 4797 | int reada = 0; |
| 4798 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4799 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4800 | |
| 4801 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 4802 | path->slots[level]); |
| 4803 | /* |
| 4804 | * if the lower level block was created before the snapshot |
| 4805 | * was created, we know there is no need to update back refs |
| 4806 | * for the subtree |
| 4807 | */ |
| 4808 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4809 | generation <= root->root_key.offset) { |
| 4810 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4811 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4812 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4813 | |
| 4814 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4815 | btrfs_node_key_to_cpu(path->nodes[level], &first_key, |
| 4816 | path->slots[level]); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4817 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4818 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4819 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4820 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4821 | if (IS_ERR(next)) |
| 4822 | return PTR_ERR(next); |
| 4823 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 4824 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 4825 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4826 | reada = 1; |
| 4827 | } |
| 4828 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4829 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4830 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4831 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4832 | &wc->refs[level - 1], |
| 4833 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4834 | if (ret < 0) |
| 4835 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4836 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4837 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4838 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4839 | ret = -EIO; |
| 4840 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4841 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4842 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4843 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4844 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4845 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4846 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4847 | if (level == 1 && |
| 4848 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4849 | goto skip; |
| 4850 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4851 | if (!wc->update_ref || |
| 4852 | generation <= root->root_key.offset) |
| 4853 | goto skip; |
| 4854 | |
| 4855 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 4856 | path->slots[level]); |
| 4857 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 4858 | if (ret < 0) |
| 4859 | goto skip; |
| 4860 | |
| 4861 | wc->stage = UPDATE_BACKREF; |
| 4862 | wc->shared_level = level - 1; |
| 4863 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4864 | } else { |
| 4865 | if (level == 1 && |
| 4866 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4867 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4868 | } |
| 4869 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 4870 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4871 | btrfs_tree_unlock(next); |
| 4872 | free_extent_buffer(next); |
| 4873 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4874 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | if (!next) { |
| 4878 | if (reada && level == 1) |
| 4879 | reada_walk_down(trans, root, wc, path); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4880 | next = read_tree_block(fs_info, bytenr, generation, level - 1, |
| 4881 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 4882 | if (IS_ERR(next)) { |
| 4883 | return PTR_ERR(next); |
| 4884 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 4885 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 4886 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 4887 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4888 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4889 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4893 | ASSERT(level == btrfs_header_level(next)); |
| 4894 | if (level != btrfs_header_level(next)) { |
| 4895 | btrfs_err(root->fs_info, "mismatched level"); |
| 4896 | ret = -EIO; |
| 4897 | goto out_unlock; |
| 4898 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4899 | path->nodes[level] = next; |
| 4900 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4901 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4902 | wc->level = level; |
| 4903 | if (wc->level == 1) |
| 4904 | wc->reada_slot = 0; |
| 4905 | return 0; |
| 4906 | skip: |
| 4907 | wc->refs[level - 1] = 0; |
| 4908 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4909 | if (wc->stage == DROP_REFERENCE) { |
| 4910 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 4911 | parent = path->nodes[level]->start; |
| 4912 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4913 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4914 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4915 | if (root->root_key.objectid != |
| 4916 | btrfs_header_owner(path->nodes[level])) { |
| 4917 | btrfs_err(root->fs_info, |
| 4918 | "mismatched block owner"); |
| 4919 | ret = -EIO; |
| 4920 | goto out_unlock; |
| 4921 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4922 | parent = 0; |
| 4923 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4924 | |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 4925 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4926 | * If we had a drop_progress we need to verify the refs are set |
| 4927 | * as expected. If we find our ref then we know that from here |
| 4928 | * on out everything should be correct, and we can clear the |
| 4929 | * ->restarted flag. |
| 4930 | */ |
| 4931 | if (wc->restarted) { |
| 4932 | ret = check_ref_exists(trans, root, bytenr, parent, |
| 4933 | level - 1); |
| 4934 | if (ret < 0) |
| 4935 | goto out_unlock; |
| 4936 | if (ret == 0) |
| 4937 | goto no_delete; |
| 4938 | ret = 0; |
| 4939 | wc->restarted = 0; |
| 4940 | } |
| 4941 | |
| 4942 | /* |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 4943 | * Reloc tree doesn't contribute to qgroup numbers, and we have |
| 4944 | * already accounted them at merge time (replace_path), |
| 4945 | * thus we could skip expensive subtree trace here. |
| 4946 | */ |
| 4947 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
| 4948 | need_account) { |
Lu Fengqi | deb4062 | 2018-07-18 14:45:38 +0800 | [diff] [blame] | 4949 | ret = btrfs_qgroup_trace_subtree(trans, next, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 4950 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4951 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4952 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4953 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 4954 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4955 | } |
| 4956 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 4957 | |
| 4958 | /* |
| 4959 | * We need to update the next key in our walk control so we can |
| 4960 | * update the drop_progress key accordingly. We don't care if |
| 4961 | * find_next_key doesn't find a key because that means we're at |
| 4962 | * the end and are going to clean up now. |
| 4963 | */ |
| 4964 | wc->drop_level = level; |
| 4965 | find_next_key(path, level, &wc->drop_progress); |
| 4966 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4967 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
| 4968 | fs_info->nodesize, parent); |
| 4969 | btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid); |
| 4970 | ret = btrfs_free_extent(trans, &ref); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4971 | if (ret) |
| 4972 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4973 | } |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4974 | no_delete: |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4975 | *lookup_info = 1; |
| 4976 | ret = 1; |
| 4977 | |
| 4978 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4979 | btrfs_tree_unlock(next); |
| 4980 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4981 | |
| 4982 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4986 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4987 | * |
| 4988 | * when wc->stage == DROP_REFERENCE, this function drops |
| 4989 | * reference count on the block. |
| 4990 | * |
| 4991 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 4992 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 4993 | * to UPDATE_BACKREF previously while processing the block. |
| 4994 | * |
| 4995 | * NOTE: return value 1 means we should stop walking up. |
| 4996 | */ |
| 4997 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 4998 | struct btrfs_root *root, |
| 4999 | struct btrfs_path *path, |
| 5000 | struct walk_control *wc) |
| 5001 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5002 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5003 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5004 | int level = wc->level; |
| 5005 | struct extent_buffer *eb = path->nodes[level]; |
| 5006 | u64 parent = 0; |
| 5007 | |
| 5008 | if (wc->stage == UPDATE_BACKREF) { |
| 5009 | BUG_ON(wc->shared_level < level); |
| 5010 | if (level < wc->shared_level) |
| 5011 | goto out; |
| 5012 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5013 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5014 | if (ret > 0) |
| 5015 | wc->update_ref = 0; |
| 5016 | |
| 5017 | wc->stage = DROP_REFERENCE; |
| 5018 | wc->shared_level = -1; |
| 5019 | path->slots[level] = 0; |
| 5020 | |
| 5021 | /* |
| 5022 | * check reference count again if the block isn't locked. |
| 5023 | * we should start walking down the tree again if reference |
| 5024 | * count is one. |
| 5025 | */ |
| 5026 | if (!path->locks[level]) { |
| 5027 | BUG_ON(level == 0); |
| 5028 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5029 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5030 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5031 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5032 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5033 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5034 | &wc->refs[level], |
| 5035 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5036 | if (ret < 0) { |
| 5037 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5038 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5039 | return ret; |
| 5040 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5041 | BUG_ON(wc->refs[level] == 0); |
| 5042 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5043 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5044 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5045 | return 1; |
| 5046 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5047 | } |
| 5048 | } |
| 5049 | |
| 5050 | /* wc->stage == DROP_REFERENCE */ |
| 5051 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5052 | |
| 5053 | if (wc->refs[level] == 1) { |
| 5054 | if (level == 0) { |
| 5055 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5056 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5057 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5058 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5059 | BUG_ON(ret); /* -ENOMEM */ |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5060 | if (is_fstree(root->root_key.objectid)) { |
| 5061 | ret = btrfs_qgroup_trace_leaf_items(trans, eb); |
| 5062 | if (ret) { |
| 5063 | btrfs_err_rl(fs_info, |
| 5064 | "error %d accounting leaf items, quota is out of sync, rescan required", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5065 | ret); |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5066 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5067 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5068 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5069 | /* make block locked assertion in btrfs_clean_tree_block happy */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5070 | if (!path->locks[level] && |
| 5071 | btrfs_header_generation(eb) == trans->transid) { |
| 5072 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5073 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5074 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5075 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5076 | btrfs_clean_tree_block(eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | if (eb == root->node) { |
| 5080 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5081 | parent = eb->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5082 | else if (root->root_key.objectid != btrfs_header_owner(eb)) |
| 5083 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5084 | } else { |
| 5085 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5086 | parent = path->nodes[level + 1]->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5087 | else if (root->root_key.objectid != |
| 5088 | btrfs_header_owner(path->nodes[level + 1])) |
| 5089 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5090 | } |
| 5091 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5092 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5093 | out: |
| 5094 | wc->refs[level] = 0; |
| 5095 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5096 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5097 | |
| 5098 | owner_mismatch: |
| 5099 | btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", |
| 5100 | btrfs_header_owner(eb), root->root_key.objectid); |
| 5101 | return -EUCLEAN; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5102 | } |
| 5103 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5104 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5105 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5106 | struct btrfs_path *path, |
| 5107 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5108 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5109 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5110 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5111 | int ret; |
| 5112 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5113 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5114 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5115 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5116 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5117 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5118 | if (level == 0) |
| 5119 | break; |
| 5120 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5121 | if (path->slots[level] >= |
| 5122 | btrfs_header_nritems(path->nodes[level])) |
| 5123 | break; |
| 5124 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5125 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5126 | if (ret > 0) { |
| 5127 | path->slots[level]++; |
| 5128 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5129 | } else if (ret < 0) |
| 5130 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5131 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5132 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5133 | return 0; |
| 5134 | } |
| 5135 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5136 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5137 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5138 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5139 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5140 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5141 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5142 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5143 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5144 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5145 | while (level < max_level && path->nodes[level]) { |
| 5146 | wc->level = level; |
| 5147 | if (path->slots[level] + 1 < |
| 5148 | btrfs_header_nritems(path->nodes[level])) { |
| 5149 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5150 | return 0; |
| 5151 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5152 | ret = walk_up_proc(trans, root, path, wc); |
| 5153 | if (ret > 0) |
| 5154 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5155 | if (ret < 0) |
| 5156 | return ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5157 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5158 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5159 | btrfs_tree_unlock_rw(path->nodes[level], |
| 5160 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5161 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5162 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5163 | free_extent_buffer(path->nodes[level]); |
| 5164 | path->nodes[level] = NULL; |
| 5165 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | return 1; |
| 5169 | } |
| 5170 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5171 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5172 | * drop a subvolume tree. |
| 5173 | * |
| 5174 | * this function traverses the tree freeing any blocks that only |
| 5175 | * referenced by the tree. |
| 5176 | * |
| 5177 | * when a shared tree block is found. this function decreases its |
| 5178 | * reference count by one. if update_ref is true, this function |
| 5179 | * also make sure backrefs for the shared block and all lower level |
| 5180 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5181 | * |
| 5182 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5183 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 5184 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5185 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 5186 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5187 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5188 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5189 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5190 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5191 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5192 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5193 | struct walk_control *wc; |
| 5194 | struct btrfs_key key; |
| 5195 | int err = 0; |
| 5196 | int ret; |
| 5197 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5198 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5199 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 5200 | btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5201 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5202 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5203 | if (!path) { |
| 5204 | err = -ENOMEM; |
| 5205 | goto out; |
| 5206 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5207 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5208 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5209 | if (!wc) { |
| 5210 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5211 | err = -ENOMEM; |
| 5212 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5213 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5214 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5215 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5216 | if (IS_ERR(trans)) { |
| 5217 | err = PTR_ERR(trans); |
| 5218 | goto out_free; |
| 5219 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 5220 | |
Josef Bacik | 0568e82 | 2018-11-30 11:52:14 -0500 | [diff] [blame] | 5221 | err = btrfs_run_delayed_items(trans); |
| 5222 | if (err) |
| 5223 | goto out_end_trans; |
| 5224 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5225 | if (block_rsv) |
| 5226 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5227 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 5228 | /* |
| 5229 | * This will help us catch people modifying the fs tree while we're |
| 5230 | * dropping it. It is unsafe to mess with the fs tree while it's being |
| 5231 | * dropped as we unlock the root node and parent nodes as we walk down |
| 5232 | * the tree, assuming nothing will change. If something does change |
| 5233 | * then we'll have stale information and drop references to blocks we've |
| 5234 | * already dropped. |
| 5235 | */ |
| 5236 | set_bit(BTRFS_ROOT_DELETING, &root->state); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5237 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5238 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5239 | path->nodes[level] = btrfs_lock_root_node(root); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5240 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5241 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5242 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5243 | memset(&wc->update_progress, 0, |
| 5244 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5245 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5246 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5247 | memcpy(&wc->update_progress, &key, |
| 5248 | sizeof(wc->update_progress)); |
| 5249 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5250 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5251 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5252 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5253 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5254 | path->lowest_level = 0; |
| 5255 | if (ret < 0) { |
| 5256 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5257 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5258 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5259 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5260 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5261 | /* |
| 5262 | * unlock our path, this is safe because only this |
| 5263 | * function is allowed to delete this snapshot |
| 5264 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5265 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5266 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5267 | level = btrfs_header_level(root->node); |
| 5268 | while (1) { |
| 5269 | btrfs_tree_lock(path->nodes[level]); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5270 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5271 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5272 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5273 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5274 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5275 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5276 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5277 | if (ret < 0) { |
| 5278 | err = ret; |
| 5279 | goto out_end_trans; |
| 5280 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5281 | BUG_ON(wc->refs[level] == 0); |
| 5282 | |
| 5283 | if (level == root_item->drop_level) |
| 5284 | break; |
| 5285 | |
| 5286 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5287 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5288 | WARN_ON(wc->refs[level] != 1); |
| 5289 | level--; |
| 5290 | } |
| 5291 | } |
| 5292 | |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5293 | wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5294 | wc->level = level; |
| 5295 | wc->shared_level = -1; |
| 5296 | wc->stage = DROP_REFERENCE; |
| 5297 | wc->update_ref = update_ref; |
| 5298 | wc->keep_locks = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5299 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5300 | |
| 5301 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5302 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5303 | ret = walk_down_tree(trans, root, path, wc); |
| 5304 | if (ret < 0) { |
| 5305 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5306 | break; |
| 5307 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5308 | |
| 5309 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5310 | if (ret < 0) { |
| 5311 | err = ret; |
| 5312 | break; |
| 5313 | } |
| 5314 | |
| 5315 | if (ret > 0) { |
| 5316 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5317 | break; |
| 5318 | } |
| 5319 | |
| 5320 | if (wc->stage == DROP_REFERENCE) { |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5321 | wc->drop_level = wc->level; |
| 5322 | btrfs_node_key_to_cpu(path->nodes[wc->drop_level], |
| 5323 | &wc->drop_progress, |
| 5324 | path->slots[wc->drop_level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5325 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5326 | btrfs_cpu_key_to_disk(&root_item->drop_progress, |
| 5327 | &wc->drop_progress); |
| 5328 | root_item->drop_level = wc->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5329 | |
| 5330 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5331 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5332 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5333 | ret = btrfs_update_root(trans, tree_root, |
| 5334 | &root->root_key, |
| 5335 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5336 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5337 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5338 | err = ret; |
| 5339 | goto out_end_trans; |
| 5340 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5341 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5342 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5343 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5344 | btrfs_debug(fs_info, |
| 5345 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 5346 | err = -EAGAIN; |
| 5347 | goto out_free; |
| 5348 | } |
| 5349 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5350 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5351 | if (IS_ERR(trans)) { |
| 5352 | err = PTR_ERR(trans); |
| 5353 | goto out_free; |
| 5354 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5355 | if (block_rsv) |
| 5356 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5357 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5358 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5359 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5360 | if (err) |
| 5361 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5362 | |
Lu Fengqi | ab9ce7d | 2018-08-01 11:32:27 +0800 | [diff] [blame] | 5363 | ret = btrfs_del_root(trans, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5364 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5365 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | e19182c | 2017-12-04 13:11:45 -0500 | [diff] [blame] | 5366 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5367 | goto out_end_trans; |
| 5368 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5369 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5370 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5371 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 5372 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5373 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5374 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5375 | err = ret; |
| 5376 | goto out_end_trans; |
| 5377 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 5378 | /* if we fail to delete the orphan item this time |
| 5379 | * around, it'll get picked up the next time. |
| 5380 | * |
| 5381 | * The most common failure here is just -ENOENT. |
| 5382 | */ |
| 5383 | btrfs_del_orphan_item(trans, tree_root, |
| 5384 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 5388 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 5389 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5390 | } else { |
| 5391 | free_extent_buffer(root->node); |
| 5392 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 5393 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5394 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5395 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5396 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5397 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5398 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5399 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5400 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5401 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5402 | /* |
| 5403 | * So if we need to stop dropping the snapshot for whatever reason we |
| 5404 | * need to make sure to add it back to the dead root list so that we |
| 5405 | * keep trying to do the work later. This also cleans up roots if we |
| 5406 | * don't have it in the radix (like when we recover after a power fail |
| 5407 | * or unmount) so we don't leak memory. |
| 5408 | */ |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 5409 | if (!for_reloc && !root_dropped) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5410 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 5411 | if (err && err != -EAGAIN) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5412 | btrfs_handle_fs_error(fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 5413 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5414 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5415 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5416 | /* |
| 5417 | * drop subtree rooted at tree block 'node'. |
| 5418 | * |
| 5419 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5420 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5421 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5422 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5423 | struct btrfs_root *root, |
| 5424 | struct extent_buffer *node, |
| 5425 | struct extent_buffer *parent) |
| 5426 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5427 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5428 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5429 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5430 | int level; |
| 5431 | int parent_level; |
| 5432 | int ret = 0; |
| 5433 | int wret; |
| 5434 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5435 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5436 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5437 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5438 | if (!path) |
| 5439 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5440 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5441 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5442 | if (!wc) { |
| 5443 | btrfs_free_path(path); |
| 5444 | return -ENOMEM; |
| 5445 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5446 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5447 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5448 | parent_level = btrfs_header_level(parent); |
| 5449 | extent_buffer_get(parent); |
| 5450 | path->nodes[parent_level] = parent; |
| 5451 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5452 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5453 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5454 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5455 | path->nodes[level] = node; |
| 5456 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5457 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5458 | |
| 5459 | wc->refs[parent_level] = 1; |
| 5460 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5461 | wc->level = level; |
| 5462 | wc->shared_level = -1; |
| 5463 | wc->stage = DROP_REFERENCE; |
| 5464 | wc->update_ref = 0; |
| 5465 | wc->keep_locks = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5466 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5467 | |
| 5468 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5469 | wret = walk_down_tree(trans, root, path, wc); |
| 5470 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5471 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5472 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5473 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5474 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5475 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5476 | if (wret < 0) |
| 5477 | ret = wret; |
| 5478 | if (wret != 0) |
| 5479 | break; |
| 5480 | } |
| 5481 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5482 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5483 | btrfs_free_path(path); |
| 5484 | return ret; |
| 5485 | } |
| 5486 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5487 | /* |
| 5488 | * 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] | 5489 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5490 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5491 | 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] | 5492 | { |
| 5493 | struct btrfs_block_group_cache *block_group; |
| 5494 | u64 free_bytes = 0; |
| 5495 | int factor; |
| 5496 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5497 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5498 | if (list_empty(&sinfo->ro_bgs)) |
| 5499 | return 0; |
| 5500 | |
| 5501 | spin_lock(&sinfo->lock); |
| 5502 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5503 | spin_lock(&block_group->lock); |
| 5504 | |
| 5505 | if (!block_group->ro) { |
| 5506 | spin_unlock(&block_group->lock); |
| 5507 | continue; |
| 5508 | } |
| 5509 | |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 5510 | factor = btrfs_bg_type_to_factor(block_group->flags); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5511 | free_bytes += (block_group->key.offset - |
| 5512 | btrfs_block_group_used(&block_group->item)) * |
| 5513 | factor; |
| 5514 | |
| 5515 | spin_unlock(&block_group->lock); |
| 5516 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5517 | spin_unlock(&sinfo->lock); |
| 5518 | |
| 5519 | return free_bytes; |
| 5520 | } |
| 5521 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5522 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 5523 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5524 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5525 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5526 | } |
| 5527 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5528 | /* |
| 5529 | * It used to be that old block groups would be left around forever. |
| 5530 | * Iterating over them would be enough to trim unused space. Since we |
| 5531 | * now automatically remove them, we also need to iterate over unallocated |
| 5532 | * space. |
| 5533 | * |
| 5534 | * We don't want a transaction for this since the discard may take a |
| 5535 | * substantial amount of time. We don't require that a transaction be |
| 5536 | * running, but we do need to take a running transaction into account |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5537 | * to ensure that we're not discarding chunks that were released or |
| 5538 | * allocated in the current transaction. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5539 | * |
| 5540 | * Holding the chunks lock will prevent other threads from allocating |
| 5541 | * or releasing chunks, but it won't prevent a running transaction |
| 5542 | * from committing and releasing the memory that the pending chunks |
| 5543 | * 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] | 5544 | * transaction and hold the commit root sem. We only need to hold |
| 5545 | * it while performing the free space search since we have already |
| 5546 | * held back allocations. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5547 | */ |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5548 | static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5549 | { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5550 | u64 start = SZ_1M, len = 0, end = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5551 | int ret; |
| 5552 | |
| 5553 | *trimmed = 0; |
| 5554 | |
Jeff Mahoney | 0be88e3 | 2018-09-06 17:18:15 -0400 | [diff] [blame] | 5555 | /* Discard not supported = nothing to do. */ |
| 5556 | if (!blk_queue_discard(bdev_get_queue(device->bdev))) |
| 5557 | return 0; |
| 5558 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5559 | /* Not writable = nothing to do. */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5560 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5561 | return 0; |
| 5562 | |
| 5563 | /* No free space = nothing to do. */ |
| 5564 | if (device->total_bytes <= device->bytes_used) |
| 5565 | return 0; |
| 5566 | |
| 5567 | ret = 0; |
| 5568 | |
| 5569 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 5570 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5571 | u64 bytes; |
| 5572 | |
| 5573 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 5574 | if (ret) |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5575 | break; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5576 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5577 | find_first_clear_extent_bit(&device->alloc_state, start, |
| 5578 | &start, &end, |
| 5579 | CHUNK_TRIMMED | CHUNK_ALLOCATED); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5580 | |
| 5581 | /* Ensure we skip the reserved area in the first 1M */ |
| 5582 | start = max_t(u64, start, SZ_1M); |
| 5583 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5584 | /* |
| 5585 | * If find_first_clear_extent_bit find a range that spans the |
| 5586 | * end of the device it will set end to -1, in this case it's up |
| 5587 | * to the caller to trim the value to the size of the device. |
| 5588 | */ |
| 5589 | end = min(end, device->total_bytes - 1); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5590 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5591 | len = end - start + 1; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5592 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5593 | /* We didn't find any extents */ |
| 5594 | if (!len) { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5595 | mutex_unlock(&fs_info->chunk_mutex); |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5596 | ret = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5597 | break; |
| 5598 | } |
| 5599 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5600 | ret = btrfs_issue_discard(device->bdev, start, len, |
| 5601 | &bytes); |
| 5602 | if (!ret) |
| 5603 | set_extent_bits(&device->alloc_state, start, |
| 5604 | start + bytes - 1, |
| 5605 | CHUNK_TRIMMED); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5606 | mutex_unlock(&fs_info->chunk_mutex); |
| 5607 | |
| 5608 | if (ret) |
| 5609 | break; |
| 5610 | |
| 5611 | start += len; |
| 5612 | *trimmed += bytes; |
| 5613 | |
| 5614 | if (fatal_signal_pending(current)) { |
| 5615 | ret = -ERESTARTSYS; |
| 5616 | break; |
| 5617 | } |
| 5618 | |
| 5619 | cond_resched(); |
| 5620 | } |
| 5621 | |
| 5622 | return ret; |
| 5623 | } |
| 5624 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5625 | /* |
| 5626 | * Trim the whole filesystem by: |
| 5627 | * 1) trimming the free space in each block group |
| 5628 | * 2) trimming the unallocated space on each device |
| 5629 | * |
| 5630 | * This will also continue trimming even if a block group or device encounters |
| 5631 | * an error. The return value will be the last error, or 0 if nothing bad |
| 5632 | * happens. |
| 5633 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5634 | 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] | 5635 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5636 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5637 | struct btrfs_device *device; |
| 5638 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5639 | u64 group_trimmed; |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5640 | u64 range_end = U64_MAX; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5641 | u64 start; |
| 5642 | u64 end; |
| 5643 | u64 trimmed = 0; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5644 | u64 bg_failed = 0; |
| 5645 | u64 dev_failed = 0; |
| 5646 | int bg_ret = 0; |
| 5647 | int dev_ret = 0; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5648 | int ret = 0; |
| 5649 | |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5650 | /* |
| 5651 | * Check range overflow if range->len is set. |
| 5652 | * The default range->len is U64_MAX. |
| 5653 | */ |
| 5654 | if (range->len != U64_MAX && |
| 5655 | check_add_overflow(range->start, range->len, &range_end)) |
| 5656 | return -EINVAL; |
| 5657 | |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 5658 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 5659 | for (; cache; cache = btrfs_next_block_group(cache)) { |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5660 | if (cache->key.objectid >= range_end) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5661 | btrfs_put_block_group(cache); |
| 5662 | break; |
| 5663 | } |
| 5664 | |
| 5665 | start = max(range->start, cache->key.objectid); |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5666 | end = min(range_end, cache->key.objectid + cache->key.offset); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5667 | |
| 5668 | if (end - start >= range->minlen) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5669 | if (!btrfs_block_group_cache_done(cache)) { |
| 5670 | ret = btrfs_cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5671 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5672 | bg_failed++; |
| 5673 | bg_ret = ret; |
| 5674 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5675 | } |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5676 | ret = btrfs_wait_block_group_cache_done(cache); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5677 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5678 | bg_failed++; |
| 5679 | bg_ret = ret; |
| 5680 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5681 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5682 | } |
| 5683 | ret = btrfs_trim_block_group(cache, |
| 5684 | &group_trimmed, |
| 5685 | start, |
| 5686 | end, |
| 5687 | range->minlen); |
| 5688 | |
| 5689 | trimmed += group_trimmed; |
| 5690 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5691 | bg_failed++; |
| 5692 | bg_ret = ret; |
| 5693 | continue; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5694 | } |
| 5695 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5698 | if (bg_failed) |
| 5699 | btrfs_warn(fs_info, |
| 5700 | "failed to trim %llu block group(s), last error %d", |
| 5701 | bg_failed, bg_ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5702 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | d4e329d | 2018-09-06 17:18:14 -0400 | [diff] [blame] | 5703 | devices = &fs_info->fs_devices->devices; |
| 5704 | list_for_each_entry(device, devices, dev_list) { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5705 | ret = btrfs_trim_free_extents(device, &group_trimmed); |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5706 | if (ret) { |
| 5707 | dev_failed++; |
| 5708 | dev_ret = ret; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5709 | break; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5710 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5711 | |
| 5712 | trimmed += group_trimmed; |
| 5713 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5714 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5715 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5716 | if (dev_failed) |
| 5717 | btrfs_warn(fs_info, |
| 5718 | "failed to trim %llu device(s), last error %d", |
| 5719 | dev_failed, dev_ret); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5720 | range->len = trimmed; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5721 | if (bg_ret) |
| 5722 | return bg_ret; |
| 5723 | return dev_ret; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5724 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5725 | |
| 5726 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5727 | * btrfs_{start,end}_write_no_snapshotting() are similar to |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5728 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 5729 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 5730 | * but flush the data into disk after the snapshot creation, or to prevent |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5731 | * operations while snapshotting is ongoing and that cause the snapshot to be |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5732 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5733 | */ |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5734 | void btrfs_end_write_no_snapshotting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5735 | { |
| 5736 | percpu_counter_dec(&root->subv_writers->counter); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 5737 | cond_wake_up(&root->subv_writers->wait); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5738 | } |
| 5739 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5740 | int btrfs_start_write_no_snapshotting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5741 | { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5742 | if (atomic_read(&root->will_be_snapshotted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5743 | return 0; |
| 5744 | |
| 5745 | percpu_counter_inc(&root->subv_writers->counter); |
| 5746 | /* |
| 5747 | * Make sure counter is updated before we check for snapshot creation. |
| 5748 | */ |
| 5749 | smp_mb(); |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5750 | if (atomic_read(&root->will_be_snapshotted)) { |
| 5751 | btrfs_end_write_no_snapshotting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5752 | return 0; |
| 5753 | } |
| 5754 | return 1; |
| 5755 | } |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5756 | |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5757 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) |
| 5758 | { |
| 5759 | while (true) { |
| 5760 | int ret; |
| 5761 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5762 | ret = btrfs_start_write_no_snapshotting(root); |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5763 | if (ret) |
| 5764 | break; |
Peter Zijlstra | 4625956 | 2018-03-15 11:43:08 +0100 | [diff] [blame] | 5765 | wait_var_event(&root->will_be_snapshotted, |
| 5766 | !atomic_read(&root->will_be_snapshotted)); |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5767 | } |
| 5768 | } |