David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 7 | #include <linux/sched/signal.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 11 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 12 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 13 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 15 | #include <linux/ratelimit.h> |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 16 | #include <linux/percpu_counter.h> |
Josef Bacik | 69fe2d7 | 2017-10-19 14:15:57 -0400 | [diff] [blame] | 17 | #include <linux/lockdep.h> |
Nikolay Borisov | 9678c54 | 2018-01-08 11:45:05 +0200 | [diff] [blame] | 18 | #include <linux/crc32c.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 19 | #include "misc.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 20 | #include "tree-log.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 21 | #include "disk-io.h" |
| 22 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 25 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 26 | #include "free-space-cache.h" |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 27 | #include "free-space-tree.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 28 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 29 | #include "qgroup.h" |
Josef Bacik | fd708b8 | 2017-09-29 15:43:50 -0400 | [diff] [blame] | 30 | #include "ref-verify.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | d12ffdd | 2019-06-19 13:47:17 -0400 | [diff] [blame] | 32 | #include "block-rsv.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 33 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 34 | #include "block-group.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 36 | #undef SCRAMBLE_DELAYED_REFS |
| 37 | |
Qu Wenruo | 9f9b8e8 | 2018-10-24 20:24:01 +0800 | [diff] [blame] | 38 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 39 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 40 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 41 | u64 root_objectid, u64 owner_objectid, |
| 42 | u64 owner_offset, int refs_to_drop, |
| 43 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 44 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 45 | struct extent_buffer *leaf, |
| 46 | struct btrfs_extent_item *ei); |
| 47 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 48 | u64 parent, u64 root_objectid, |
| 49 | u64 flags, u64 owner, u64 offset, |
| 50 | struct btrfs_key *ins, int ref_mod); |
| 51 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 52 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 53 | struct btrfs_delayed_extent_op *extent_op); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 54 | static int find_next_key(struct btrfs_path *path, int level, |
| 55 | struct btrfs_key *key); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 56 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 57 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 58 | { |
| 59 | return (cache->flags & bits) == bits; |
| 60 | } |
| 61 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 62 | int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info, |
| 63 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 64 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 65 | u64 end = start + num_bytes - 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 66 | set_extent_bits(&fs_info->freed_extents[0], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 67 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 68 | set_extent_bits(&fs_info->freed_extents[1], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 69 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 70 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 73 | void btrfs_free_excluded_extents(struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 74 | { |
Nikolay Borisov | 9e715da | 2018-06-20 15:49:08 +0300 | [diff] [blame] | 75 | struct btrfs_fs_info *fs_info = cache->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 76 | u64 start, end; |
| 77 | |
| 78 | start = cache->key.objectid; |
| 79 | end = start + cache->key.offset - 1; |
| 80 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 81 | clear_extent_bits(&fs_info->freed_extents[0], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 82 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 83 | clear_extent_bits(&fs_info->freed_extents[1], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 84 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 87 | static u64 generic_ref_to_space_flags(struct btrfs_ref *ref) |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 88 | { |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 89 | if (ref->type == BTRFS_REF_METADATA) { |
| 90 | if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 91 | return BTRFS_BLOCK_GROUP_SYSTEM; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 92 | else |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 93 | return BTRFS_BLOCK_GROUP_METADATA; |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 94 | } |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 95 | return BTRFS_BLOCK_GROUP_DATA; |
| 96 | } |
| 97 | |
| 98 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 99 | struct btrfs_ref *ref) |
| 100 | { |
| 101 | struct btrfs_space_info *space_info; |
| 102 | u64 flags = generic_ref_to_space_flags(ref); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 103 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 104 | space_info = btrfs_find_space_info(fs_info, flags); |
Omar Sandoval | 55e8196 | 2017-06-06 16:45:27 -0700 | [diff] [blame] | 105 | ASSERT(space_info); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 106 | percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len, |
| 107 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 108 | } |
| 109 | |
| 110 | static void sub_pinned_bytes(struct btrfs_fs_info *fs_info, |
| 111 | struct btrfs_ref *ref) |
| 112 | { |
| 113 | struct btrfs_space_info *space_info; |
| 114 | u64 flags = generic_ref_to_space_flags(ref); |
| 115 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 116 | space_info = btrfs_find_space_info(fs_info, flags); |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 117 | ASSERT(space_info); |
| 118 | percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len, |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 119 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Omar Sandoval | 0d9f824 | 2017-06-06 16:45:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 122 | /* 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] | 123 | 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] | 124 | { |
| 125 | int ret; |
| 126 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 127 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 128 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 129 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 130 | if (!path) |
| 131 | return -ENOMEM; |
| 132 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 133 | key.objectid = start; |
| 134 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 135 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 136 | 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] | 137 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 141 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 142 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 143 | * |
| 144 | * the head node for delayed ref is used to store the sum of all the |
| 145 | * reference count modifications queued up in the rbtree. the head |
| 146 | * node may also store the extent flags to set. This way you can check |
| 147 | * to see what the reference count and extent flags would be if all of |
| 148 | * the delayed refs are not processed. |
| 149 | */ |
| 150 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 151 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 152 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 153 | { |
| 154 | struct btrfs_delayed_ref_head *head; |
| 155 | struct btrfs_delayed_ref_root *delayed_refs; |
| 156 | struct btrfs_path *path; |
| 157 | struct btrfs_extent_item *ei; |
| 158 | struct extent_buffer *leaf; |
| 159 | struct btrfs_key key; |
| 160 | u32 item_size; |
| 161 | u64 num_refs; |
| 162 | u64 extent_flags; |
| 163 | int ret; |
| 164 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 165 | /* |
| 166 | * If we don't have skinny metadata, don't bother doing anything |
| 167 | * different |
| 168 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 169 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 170 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 171 | metadata = 0; |
| 172 | } |
| 173 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 174 | path = btrfs_alloc_path(); |
| 175 | if (!path) |
| 176 | return -ENOMEM; |
| 177 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 178 | if (!trans) { |
| 179 | path->skip_locking = 1; |
| 180 | path->search_commit_root = 1; |
| 181 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 182 | |
| 183 | search_again: |
| 184 | key.objectid = bytenr; |
| 185 | key.offset = offset; |
| 186 | if (metadata) |
| 187 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 188 | else |
| 189 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 190 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 191 | 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] | 192 | if (ret < 0) |
| 193 | goto out_free; |
| 194 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 195 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 196 | if (path->slots[0]) { |
| 197 | path->slots[0]--; |
| 198 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 199 | path->slots[0]); |
| 200 | if (key.objectid == bytenr && |
| 201 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 202 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 203 | ret = 0; |
| 204 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 207 | if (ret == 0) { |
| 208 | leaf = path->nodes[0]; |
| 209 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 210 | if (item_size >= sizeof(*ei)) { |
| 211 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 212 | struct btrfs_extent_item); |
| 213 | num_refs = btrfs_extent_refs(leaf, ei); |
| 214 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 215 | } else { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 216 | ret = -EINVAL; |
| 217 | btrfs_print_v0_err(fs_info); |
| 218 | if (trans) |
| 219 | btrfs_abort_transaction(trans, ret); |
| 220 | else |
| 221 | btrfs_handle_fs_error(fs_info, ret, NULL); |
| 222 | |
| 223 | goto out_free; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 224 | } |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 225 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 226 | BUG_ON(num_refs == 0); |
| 227 | } else { |
| 228 | num_refs = 0; |
| 229 | extent_flags = 0; |
| 230 | ret = 0; |
| 231 | } |
| 232 | |
| 233 | if (!trans) |
| 234 | goto out; |
| 235 | |
| 236 | delayed_refs = &trans->transaction->delayed_refs; |
| 237 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 238 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 239 | if (head) { |
| 240 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 241 | refcount_inc(&head->refs); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 242 | spin_unlock(&delayed_refs->lock); |
| 243 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 244 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 245 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 246 | /* |
| 247 | * Mutex was contended, block until it's released and try |
| 248 | * again |
| 249 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 250 | mutex_lock(&head->mutex); |
| 251 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 252 | btrfs_put_delayed_ref_head(head); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 253 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 254 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 255 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 256 | if (head->extent_op && head->extent_op->update_flags) |
| 257 | extent_flags |= head->extent_op->flags_to_set; |
| 258 | else |
| 259 | BUG_ON(num_refs == 0); |
| 260 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 261 | num_refs += head->ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 262 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 263 | mutex_unlock(&head->mutex); |
| 264 | } |
| 265 | spin_unlock(&delayed_refs->lock); |
| 266 | out: |
| 267 | WARN_ON(num_refs == 0); |
| 268 | if (refs) |
| 269 | *refs = num_refs; |
| 270 | if (flags) |
| 271 | *flags = extent_flags; |
| 272 | out_free: |
| 273 | btrfs_free_path(path); |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 278 | * Back reference rules. Back refs have three main goals: |
| 279 | * |
| 280 | * 1) differentiate between all holders of references to an extent so that |
| 281 | * when a reference is dropped we can make sure it was a valid reference |
| 282 | * before freeing the extent. |
| 283 | * |
| 284 | * 2) Provide enough information to quickly find the holders of an extent |
| 285 | * if we notice a given block is corrupted or bad. |
| 286 | * |
| 287 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 288 | * maintenance. This is actually the same as #2, but with a slightly |
| 289 | * different use case. |
| 290 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 291 | * There are two kinds of back refs. The implicit back refs is optimized |
| 292 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 293 | * back refs of this kind provide information about the block's owner tree |
| 294 | * and the pointer's key. These information allow us to find the block by |
| 295 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 296 | * referenced by their owner trees. The location of tree block is recorded |
| 297 | * in the back refs. Actually the full back refs is generic, and can be |
| 298 | * used in all cases the implicit back refs is used. The major shortcoming |
| 299 | * of the full back refs is its overhead. Every time a tree block gets |
| 300 | * COWed, we have to update back refs entry for all pointers in it. |
| 301 | * |
| 302 | * For a newly allocated tree block, we use implicit back refs for |
| 303 | * pointers in it. This means most tree related operations only involve |
| 304 | * implicit back refs. For a tree block created in old transaction, the |
| 305 | * only way to drop a reference to it is COW it. So we can detect the |
| 306 | * event that tree block loses its owner tree's reference and do the |
| 307 | * back refs conversion. |
| 308 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 309 | * 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] | 310 | * |
| 311 | * The reference count of the block is one and the tree is the block's |
| 312 | * owner tree. Nothing to do in this case. |
| 313 | * |
| 314 | * The reference count of the block is one and the tree is not the |
| 315 | * block's owner tree. In this case, full back refs is used for pointers |
| 316 | * in the block. Remove these full back refs, add implicit back refs for |
| 317 | * every pointers in the new block. |
| 318 | * |
| 319 | * The reference count of the block is greater than one and the tree is |
| 320 | * the block's owner tree. In this case, implicit back refs is used for |
| 321 | * pointers in the block. Add full back refs for every pointers in the |
| 322 | * block, increase lower level extents' reference counts. The original |
| 323 | * implicit back refs are entailed to the new block. |
| 324 | * |
| 325 | * The reference count of the block is greater than one and the tree is |
| 326 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 327 | * the new block, increase lower level extents' reference count. |
| 328 | * |
| 329 | * Back Reference Key composing: |
| 330 | * |
| 331 | * The key objectid corresponds to the first byte in the extent, |
| 332 | * The key type is used to differentiate between types of back refs. |
| 333 | * There are different meanings of the key offset for different types |
| 334 | * of back refs. |
| 335 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 336 | * File extents can be referenced by: |
| 337 | * |
| 338 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 339 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 340 | * - different offsets inside a file (bookend extents in file.c) |
| 341 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 342 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 343 | * |
| 344 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 345 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 346 | * - original offset in the file |
| 347 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 348 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 349 | * The key offset for the implicit back refs is hash of the first |
| 350 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 351 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 352 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 353 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 354 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 355 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 356 | * The key offset for the implicit back refs is the first byte of |
| 357 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 358 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 359 | * When a file extent is allocated, The implicit back refs is used. |
| 360 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 361 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 362 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 363 | * |
| 364 | * When a file extent is removed file truncation, we find the |
| 365 | * corresponding implicit back refs and check the following fields: |
| 366 | * |
| 367 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 368 | * |
| 369 | * Btree extents can be referenced by: |
| 370 | * |
| 371 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 372 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 373 | * Both the implicit back refs and the full back refs for tree blocks |
| 374 | * only consist of key. The key offset for the implicit back refs is |
| 375 | * objectid of block's owner tree. The key offset for the full back refs |
| 376 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 377 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 378 | * When implicit back refs is used, information about the lowest key and |
| 379 | * level of the tree block are required. These information are stored in |
| 380 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 381 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 382 | |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 383 | /* |
| 384 | * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 385 | * is_data == BTRFS_REF_TYPE_DATA, data type is requiried, |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 386 | * is_data == BTRFS_REF_TYPE_ANY, either type is OK. |
| 387 | */ |
| 388 | int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb, |
| 389 | struct btrfs_extent_inline_ref *iref, |
| 390 | enum btrfs_inline_ref_type is_data) |
| 391 | { |
| 392 | int type = btrfs_extent_inline_ref_type(eb, iref); |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 393 | u64 offset = btrfs_extent_inline_ref_offset(eb, iref); |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 394 | |
| 395 | if (type == BTRFS_TREE_BLOCK_REF_KEY || |
| 396 | type == BTRFS_SHARED_BLOCK_REF_KEY || |
| 397 | type == BTRFS_SHARED_DATA_REF_KEY || |
| 398 | type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 399 | if (is_data == BTRFS_REF_TYPE_BLOCK) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 400 | if (type == BTRFS_TREE_BLOCK_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 401 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 402 | if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 403 | ASSERT(eb->fs_info); |
| 404 | /* |
| 405 | * Every shared one has parent tree |
| 406 | * block, which must be aligned to |
| 407 | * nodesize. |
| 408 | */ |
| 409 | if (offset && |
| 410 | IS_ALIGNED(offset, eb->fs_info->nodesize)) |
| 411 | return type; |
| 412 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 413 | } else if (is_data == BTRFS_REF_TYPE_DATA) { |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 414 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 415 | return type; |
Liu Bo | 64ecdb6 | 2017-08-18 15:15:24 -0600 | [diff] [blame] | 416 | if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 417 | ASSERT(eb->fs_info); |
| 418 | /* |
| 419 | * Every shared one has parent tree |
| 420 | * block, which must be aligned to |
| 421 | * nodesize. |
| 422 | */ |
| 423 | if (offset && |
| 424 | IS_ALIGNED(offset, eb->fs_info->nodesize)) |
| 425 | return type; |
| 426 | } |
Liu Bo | 167ce95 | 2017-08-18 15:15:18 -0600 | [diff] [blame] | 427 | } else { |
| 428 | ASSERT(is_data == BTRFS_REF_TYPE_ANY); |
| 429 | return type; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | btrfs_print_leaf((struct extent_buffer *)eb); |
| 434 | btrfs_err(eb->fs_info, "eb %llu invalid extent inline ref type %d", |
| 435 | eb->start, type); |
| 436 | WARN_ON(1); |
| 437 | |
| 438 | return BTRFS_REF_TYPE_INVALID; |
| 439 | } |
| 440 | |
Qu Wenruo | 0785a9a | 2019-08-09 09:24:24 +0800 | [diff] [blame] | 441 | u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 442 | { |
| 443 | u32 high_crc = ~(u32)0; |
| 444 | u32 low_crc = ~(u32)0; |
| 445 | __le64 lenum; |
| 446 | |
| 447 | lenum = cpu_to_le64(root_objectid); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 448 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 449 | lenum = cpu_to_le64(owner); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 450 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 451 | lenum = cpu_to_le64(offset); |
Johannes Thumshirn | 65019df | 2019-05-22 10:18:59 +0200 | [diff] [blame] | 452 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 453 | |
| 454 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 455 | } |
| 456 | |
| 457 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 458 | struct btrfs_extent_data_ref *ref) |
| 459 | { |
| 460 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 461 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 462 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 463 | } |
| 464 | |
| 465 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 466 | struct btrfs_extent_data_ref *ref, |
| 467 | u64 root_objectid, u64 owner, u64 offset) |
| 468 | { |
| 469 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 470 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 471 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 472 | return 0; |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 477 | struct btrfs_path *path, |
| 478 | u64 bytenr, u64 parent, |
| 479 | u64 root_objectid, |
| 480 | u64 owner, u64 offset) |
| 481 | { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 482 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 483 | struct btrfs_key key; |
| 484 | struct btrfs_extent_data_ref *ref; |
| 485 | struct extent_buffer *leaf; |
| 486 | u32 nritems; |
| 487 | int ret; |
| 488 | int recow; |
| 489 | int err = -ENOENT; |
| 490 | |
| 491 | key.objectid = bytenr; |
| 492 | if (parent) { |
| 493 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 494 | key.offset = parent; |
| 495 | } else { |
| 496 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 497 | key.offset = hash_extent_data_ref(root_objectid, |
| 498 | owner, offset); |
| 499 | } |
| 500 | again: |
| 501 | recow = 0; |
| 502 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 503 | if (ret < 0) { |
| 504 | err = ret; |
| 505 | goto fail; |
| 506 | } |
| 507 | |
| 508 | if (parent) { |
| 509 | if (!ret) |
| 510 | return 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 511 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 515 | nritems = btrfs_header_nritems(leaf); |
| 516 | while (1) { |
| 517 | if (path->slots[0] >= nritems) { |
| 518 | ret = btrfs_next_leaf(root, path); |
| 519 | if (ret < 0) |
| 520 | err = ret; |
| 521 | if (ret) |
| 522 | goto fail; |
| 523 | |
| 524 | leaf = path->nodes[0]; |
| 525 | nritems = btrfs_header_nritems(leaf); |
| 526 | recow = 1; |
| 527 | } |
| 528 | |
| 529 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 530 | if (key.objectid != bytenr || |
| 531 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 532 | goto fail; |
| 533 | |
| 534 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 535 | struct btrfs_extent_data_ref); |
| 536 | |
| 537 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 538 | owner, offset)) { |
| 539 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 540 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 541 | goto again; |
| 542 | } |
| 543 | err = 0; |
| 544 | break; |
| 545 | } |
| 546 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 547 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 548 | fail: |
| 549 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 550 | } |
| 551 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 552 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 553 | struct btrfs_path *path, |
| 554 | u64 bytenr, u64 parent, |
| 555 | u64 root_objectid, u64 owner, |
| 556 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 557 | { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 558 | struct btrfs_root *root = trans->fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 559 | struct btrfs_key key; |
| 560 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 561 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 562 | u32 num_refs; |
| 563 | int ret; |
| 564 | |
| 565 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 566 | if (parent) { |
| 567 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 568 | key.offset = parent; |
| 569 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 570 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 571 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 572 | key.offset = hash_extent_data_ref(root_objectid, |
| 573 | owner, offset); |
| 574 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 575 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 576 | |
| 577 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 578 | if (ret && ret != -EEXIST) |
| 579 | goto fail; |
| 580 | |
| 581 | leaf = path->nodes[0]; |
| 582 | if (parent) { |
| 583 | struct btrfs_shared_data_ref *ref; |
| 584 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 585 | struct btrfs_shared_data_ref); |
| 586 | if (ret == 0) { |
| 587 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 588 | } else { |
| 589 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 590 | num_refs += refs_to_add; |
| 591 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 592 | } |
| 593 | } else { |
| 594 | struct btrfs_extent_data_ref *ref; |
| 595 | while (ret == -EEXIST) { |
| 596 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 597 | struct btrfs_extent_data_ref); |
| 598 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 599 | owner, offset)) |
| 600 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 601 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 602 | key.offset++; |
| 603 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 604 | size); |
| 605 | if (ret && ret != -EEXIST) |
| 606 | goto fail; |
| 607 | |
| 608 | leaf = path->nodes[0]; |
| 609 | } |
| 610 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 611 | struct btrfs_extent_data_ref); |
| 612 | if (ret == 0) { |
| 613 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 614 | root_objectid); |
| 615 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 616 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 617 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 618 | } else { |
| 619 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 620 | num_refs += refs_to_add; |
| 621 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 622 | } |
| 623 | } |
| 624 | btrfs_mark_buffer_dirty(leaf); |
| 625 | ret = 0; |
| 626 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 627 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 628 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 629 | } |
| 630 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 631 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 632 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 633 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 634 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 635 | struct btrfs_key key; |
| 636 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 637 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 638 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 639 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 640 | int ret = 0; |
| 641 | |
| 642 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 643 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 644 | |
| 645 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 646 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 647 | struct btrfs_extent_data_ref); |
| 648 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 649 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 650 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 651 | struct btrfs_shared_data_ref); |
| 652 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 653 | } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 654 | btrfs_print_v0_err(trans->fs_info); |
| 655 | btrfs_abort_transaction(trans, -EINVAL); |
| 656 | return -EINVAL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 657 | } else { |
| 658 | BUG(); |
| 659 | } |
| 660 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 661 | BUG_ON(num_refs < refs_to_drop); |
| 662 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 663 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 664 | if (num_refs == 0) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 665 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 666 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 667 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 668 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 669 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 670 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 671 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 672 | btrfs_mark_buffer_dirty(leaf); |
| 673 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 674 | return ret; |
| 675 | } |
| 676 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 677 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 678 | struct btrfs_extent_inline_ref *iref) |
| 679 | { |
| 680 | struct btrfs_key key; |
| 681 | struct extent_buffer *leaf; |
| 682 | struct btrfs_extent_data_ref *ref1; |
| 683 | struct btrfs_shared_data_ref *ref2; |
| 684 | u32 num_refs = 0; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 685 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 686 | |
| 687 | leaf = path->nodes[0]; |
| 688 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 689 | |
| 690 | BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 691 | if (iref) { |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 692 | /* |
| 693 | * If type is invalid, we should have bailed out earlier than |
| 694 | * this call. |
| 695 | */ |
| 696 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 697 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
| 698 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 699 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 700 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 701 | } else { |
| 702 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 703 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 704 | } |
| 705 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 706 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 707 | struct btrfs_extent_data_ref); |
| 708 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 709 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 710 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 711 | struct btrfs_shared_data_ref); |
| 712 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 713 | } else { |
| 714 | WARN_ON(1); |
| 715 | } |
| 716 | return num_refs; |
| 717 | } |
| 718 | |
| 719 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 720 | struct btrfs_path *path, |
| 721 | u64 bytenr, u64 parent, |
| 722 | u64 root_objectid) |
| 723 | { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 724 | struct btrfs_root *root = trans->fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 725 | struct btrfs_key key; |
| 726 | int ret; |
| 727 | |
| 728 | key.objectid = bytenr; |
| 729 | if (parent) { |
| 730 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 731 | key.offset = parent; |
| 732 | } else { |
| 733 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 734 | key.offset = root_objectid; |
| 735 | } |
| 736 | |
| 737 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 738 | if (ret > 0) |
| 739 | ret = -ENOENT; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 744 | struct btrfs_path *path, |
| 745 | u64 bytenr, u64 parent, |
| 746 | u64 root_objectid) |
| 747 | { |
| 748 | struct btrfs_key key; |
| 749 | int ret; |
| 750 | |
| 751 | key.objectid = bytenr; |
| 752 | if (parent) { |
| 753 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 754 | key.offset = parent; |
| 755 | } else { |
| 756 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 757 | key.offset = root_objectid; |
| 758 | } |
| 759 | |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 760 | ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 761 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 762 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 763 | return ret; |
| 764 | } |
| 765 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 766 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 767 | { |
| 768 | int type; |
| 769 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 770 | if (parent > 0) |
| 771 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 772 | else |
| 773 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 774 | } else { |
| 775 | if (parent > 0) |
| 776 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 777 | else |
| 778 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 779 | } |
| 780 | return type; |
| 781 | } |
| 782 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 783 | static int find_next_key(struct btrfs_path *path, int level, |
| 784 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 785 | |
| 786 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 787 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 788 | if (!path->nodes[level]) |
| 789 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 790 | if (path->slots[level] + 1 >= |
| 791 | btrfs_header_nritems(path->nodes[level])) |
| 792 | continue; |
| 793 | if (level == 0) |
| 794 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 795 | path->slots[level] + 1); |
| 796 | else |
| 797 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 798 | path->slots[level] + 1); |
| 799 | return 0; |
| 800 | } |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 806 | * to the address of inline back ref, and 0 is returned. |
| 807 | * |
| 808 | * if back ref isn't found, *ref_ret is set to the address where it |
| 809 | * should be inserted, and -ENOENT is returned. |
| 810 | * |
| 811 | * if insert is true and there are too many inline back refs, the path |
| 812 | * points to the extent item, and -EAGAIN is returned. |
| 813 | * |
| 814 | * NOTE: inline back refs are ordered in the same way that back ref |
| 815 | * items in the tree are ordered. |
| 816 | */ |
| 817 | static noinline_for_stack |
| 818 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 819 | struct btrfs_path *path, |
| 820 | struct btrfs_extent_inline_ref **ref_ret, |
| 821 | u64 bytenr, u64 num_bytes, |
| 822 | u64 parent, u64 root_objectid, |
| 823 | u64 owner, u64 offset, int insert) |
| 824 | { |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 825 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 826 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 827 | struct btrfs_key key; |
| 828 | struct extent_buffer *leaf; |
| 829 | struct btrfs_extent_item *ei; |
| 830 | struct btrfs_extent_inline_ref *iref; |
| 831 | u64 flags; |
| 832 | u64 item_size; |
| 833 | unsigned long ptr; |
| 834 | unsigned long end; |
| 835 | int extra_size; |
| 836 | int type; |
| 837 | int want; |
| 838 | int ret; |
| 839 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 840 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 841 | int needed; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 842 | |
| 843 | key.objectid = bytenr; |
| 844 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 845 | key.offset = num_bytes; |
| 846 | |
| 847 | want = extent_ref_type(parent, owner); |
| 848 | if (insert) { |
| 849 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 850 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 851 | } else |
| 852 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 853 | |
| 854 | /* |
Nikolay Borisov | 16d1c06 | 2018-06-18 14:59:26 +0300 | [diff] [blame] | 855 | * Owner is our level, so we can just add one to get the level for the |
| 856 | * block we are interested in. |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 857 | */ |
| 858 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 859 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 860 | key.offset = owner; |
| 861 | } |
| 862 | |
| 863 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 864 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 865 | if (ret < 0) { |
| 866 | err = ret; |
| 867 | goto out; |
| 868 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 869 | |
| 870 | /* |
| 871 | * We may be a newly converted file system which still has the old fat |
| 872 | * extent entries for metadata, so try and see if we have one of those. |
| 873 | */ |
| 874 | if (ret > 0 && skinny_metadata) { |
| 875 | skinny_metadata = false; |
| 876 | if (path->slots[0]) { |
| 877 | path->slots[0]--; |
| 878 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 879 | path->slots[0]); |
| 880 | if (key.objectid == bytenr && |
| 881 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 882 | key.offset == num_bytes) |
| 883 | ret = 0; |
| 884 | } |
| 885 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 886 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 887 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 888 | key.offset = num_bytes; |
| 889 | btrfs_release_path(path); |
| 890 | goto again; |
| 891 | } |
| 892 | } |
| 893 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 894 | if (ret && !insert) { |
| 895 | err = -ENOENT; |
| 896 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 897 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 898 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 899 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 900 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 901 | |
| 902 | leaf = path->nodes[0]; |
| 903 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 904 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 905 | err = -EINVAL; |
| 906 | btrfs_print_v0_err(fs_info); |
| 907 | btrfs_abort_transaction(trans, err); |
| 908 | goto out; |
| 909 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 910 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 911 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 912 | flags = btrfs_extent_flags(leaf, ei); |
| 913 | |
| 914 | ptr = (unsigned long)(ei + 1); |
| 915 | end = (unsigned long)ei + item_size; |
| 916 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 917 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | ptr += sizeof(struct btrfs_tree_block_info); |
| 919 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 920 | } |
| 921 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 922 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) |
| 923 | needed = BTRFS_REF_TYPE_DATA; |
| 924 | else |
| 925 | needed = BTRFS_REF_TYPE_BLOCK; |
| 926 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 927 | err = -ENOENT; |
| 928 | while (1) { |
| 929 | if (ptr >= end) { |
| 930 | WARN_ON(ptr > end); |
| 931 | break; |
| 932 | } |
| 933 | iref = (struct btrfs_extent_inline_ref *)ptr; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 934 | type = btrfs_get_extent_inline_ref_type(leaf, iref, needed); |
| 935 | if (type == BTRFS_REF_TYPE_INVALID) { |
Su Yue | af431dc | 2018-06-22 16:18:01 +0800 | [diff] [blame] | 936 | err = -EUCLEAN; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 937 | goto out; |
| 938 | } |
| 939 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 940 | if (want < type) |
| 941 | break; |
| 942 | if (want > type) { |
| 943 | ptr += btrfs_extent_inline_ref_size(type); |
| 944 | continue; |
| 945 | } |
| 946 | |
| 947 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 948 | struct btrfs_extent_data_ref *dref; |
| 949 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 950 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 951 | owner, offset)) { |
| 952 | err = 0; |
| 953 | break; |
| 954 | } |
| 955 | if (hash_extent_data_ref_item(leaf, dref) < |
| 956 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 957 | break; |
| 958 | } else { |
| 959 | u64 ref_offset; |
| 960 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 961 | if (parent > 0) { |
| 962 | if (parent == ref_offset) { |
| 963 | err = 0; |
| 964 | break; |
| 965 | } |
| 966 | if (ref_offset < parent) |
| 967 | break; |
| 968 | } else { |
| 969 | if (root_objectid == ref_offset) { |
| 970 | err = 0; |
| 971 | break; |
| 972 | } |
| 973 | if (ref_offset < root_objectid) |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | ptr += btrfs_extent_inline_ref_size(type); |
| 978 | } |
| 979 | if (err == -ENOENT && insert) { |
| 980 | if (item_size + extra_size >= |
| 981 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 982 | err = -EAGAIN; |
| 983 | goto out; |
| 984 | } |
| 985 | /* |
| 986 | * To add new inline back ref, we have to make sure |
| 987 | * there is no corresponding back ref item. |
| 988 | * For simplicity, we just do not add new inline back |
| 989 | * ref if there is any kind of item for this block |
| 990 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 991 | if (find_next_key(path, 0, &key) == 0 && |
| 992 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 993 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 994 | err = -EAGAIN; |
| 995 | goto out; |
| 996 | } |
| 997 | } |
| 998 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 999 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1000 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1001 | path->keep_locks = 0; |
| 1002 | btrfs_unlock_up_safe(path, 1); |
| 1003 | } |
| 1004 | return err; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * helper to add new inline back ref |
| 1009 | */ |
| 1010 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1011 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1012 | struct btrfs_path *path, |
| 1013 | struct btrfs_extent_inline_ref *iref, |
| 1014 | u64 parent, u64 root_objectid, |
| 1015 | u64 owner, u64 offset, int refs_to_add, |
| 1016 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1017 | { |
| 1018 | struct extent_buffer *leaf; |
| 1019 | struct btrfs_extent_item *ei; |
| 1020 | unsigned long ptr; |
| 1021 | unsigned long end; |
| 1022 | unsigned long item_offset; |
| 1023 | u64 refs; |
| 1024 | int size; |
| 1025 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1026 | |
| 1027 | leaf = path->nodes[0]; |
| 1028 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1029 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1030 | |
| 1031 | type = extent_ref_type(parent, owner); |
| 1032 | size = btrfs_extent_inline_ref_size(type); |
| 1033 | |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 1034 | btrfs_extend_item(path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1035 | |
| 1036 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1037 | refs = btrfs_extent_refs(leaf, ei); |
| 1038 | refs += refs_to_add; |
| 1039 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1040 | if (extent_op) |
| 1041 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1042 | |
| 1043 | ptr = (unsigned long)ei + item_offset; |
| 1044 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1045 | if (ptr < end - size) |
| 1046 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1047 | end - size - ptr); |
| 1048 | |
| 1049 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1050 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1051 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1052 | struct btrfs_extent_data_ref *dref; |
| 1053 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1054 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1055 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1056 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1057 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1058 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1059 | struct btrfs_shared_data_ref *sref; |
| 1060 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1061 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1062 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1063 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1064 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1065 | } else { |
| 1066 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1067 | } |
| 1068 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1072 | struct btrfs_path *path, |
| 1073 | struct btrfs_extent_inline_ref **ref_ret, |
| 1074 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1075 | u64 root_objectid, u64 owner, u64 offset) |
| 1076 | { |
| 1077 | int ret; |
| 1078 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1079 | ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr, |
| 1080 | num_bytes, parent, root_objectid, |
| 1081 | owner, offset, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1082 | if (ret != -ENOENT) |
| 1083 | return ret; |
| 1084 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1085 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1086 | *ref_ret = NULL; |
| 1087 | |
| 1088 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Nikolay Borisov | b8582ee | 2018-06-20 15:48:50 +0300 | [diff] [blame] | 1089 | ret = lookup_tree_block_ref(trans, path, bytenr, parent, |
| 1090 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1091 | } else { |
Nikolay Borisov | bd1d53e | 2018-06-20 15:48:51 +0300 | [diff] [blame] | 1092 | ret = lookup_extent_data_ref(trans, path, bytenr, parent, |
| 1093 | root_objectid, owner, offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1094 | } |
| 1095 | return ret; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | * helper to update/remove inline back ref |
| 1100 | */ |
| 1101 | static noinline_for_stack |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1102 | void update_inline_extent_backref(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1103 | struct btrfs_extent_inline_ref *iref, |
| 1104 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1105 | struct btrfs_delayed_extent_op *extent_op, |
| 1106 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1107 | { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1108 | struct extent_buffer *leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1109 | struct btrfs_extent_item *ei; |
| 1110 | struct btrfs_extent_data_ref *dref = NULL; |
| 1111 | struct btrfs_shared_data_ref *sref = NULL; |
| 1112 | unsigned long ptr; |
| 1113 | unsigned long end; |
| 1114 | u32 item_size; |
| 1115 | int size; |
| 1116 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1117 | u64 refs; |
| 1118 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1119 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1120 | refs = btrfs_extent_refs(leaf, ei); |
| 1121 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1122 | refs += refs_to_mod; |
| 1123 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1124 | if (extent_op) |
| 1125 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1126 | |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 1127 | /* |
| 1128 | * If type is invalid, we should have bailed out after |
| 1129 | * lookup_inline_extent_backref(). |
| 1130 | */ |
| 1131 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY); |
| 1132 | ASSERT(type != BTRFS_REF_TYPE_INVALID); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1133 | |
| 1134 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1135 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1136 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1137 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1138 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1139 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1140 | } else { |
| 1141 | refs = 1; |
| 1142 | BUG_ON(refs_to_mod != -1); |
| 1143 | } |
| 1144 | |
| 1145 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1146 | refs += refs_to_mod; |
| 1147 | |
| 1148 | if (refs > 0) { |
| 1149 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1150 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1151 | else |
| 1152 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1153 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1154 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1155 | size = btrfs_extent_inline_ref_size(type); |
| 1156 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1157 | ptr = (unsigned long)iref; |
| 1158 | end = (unsigned long)ei + item_size; |
| 1159 | if (ptr + size < end) |
| 1160 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1161 | end - ptr - size); |
| 1162 | item_size -= size; |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 1163 | btrfs_truncate_item(path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1164 | } |
| 1165 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static noinline_for_stack |
| 1169 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1170 | struct btrfs_path *path, |
| 1171 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1172 | u64 root_objectid, u64 owner, |
| 1173 | u64 offset, int refs_to_add, |
| 1174 | struct btrfs_delayed_extent_op *extent_op) |
| 1175 | { |
| 1176 | struct btrfs_extent_inline_ref *iref; |
| 1177 | int ret; |
| 1178 | |
Nikolay Borisov | 867cc1f | 2018-06-20 15:48:48 +0300 | [diff] [blame] | 1179 | ret = lookup_inline_extent_backref(trans, path, &iref, bytenr, |
| 1180 | num_bytes, parent, root_objectid, |
| 1181 | owner, offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1182 | if (ret == 0) { |
| 1183 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1184 | update_inline_extent_backref(path, iref, refs_to_add, |
| 1185 | extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1186 | } else if (ret == -ENOENT) { |
Nikolay Borisov | a639cde | 2018-06-20 15:49:10 +0300 | [diff] [blame] | 1187 | setup_inline_extent_backref(trans->fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1188 | root_objectid, owner, offset, |
| 1189 | refs_to_add, extent_op); |
| 1190 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1191 | } |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
| 1195 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1196 | struct btrfs_path *path, |
| 1197 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1198 | u64 owner, u64 offset, int refs_to_add) |
| 1199 | { |
| 1200 | int ret; |
| 1201 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1202 | BUG_ON(refs_to_add != 1); |
Nikolay Borisov | 1072840 | 2018-06-20 15:48:43 +0300 | [diff] [blame] | 1203 | ret = insert_tree_block_ref(trans, path, bytenr, parent, |
| 1204 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1205 | } else { |
Nikolay Borisov | 62b895a | 2018-06-20 15:48:44 +0300 | [diff] [blame] | 1206 | ret = insert_extent_data_ref(trans, path, bytenr, parent, |
| 1207 | root_objectid, owner, offset, |
| 1208 | refs_to_add); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1209 | } |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1214 | struct btrfs_path *path, |
| 1215 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1216 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1217 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1218 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1219 | |
| 1220 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1221 | if (iref) { |
Nikolay Borisov | 61a18f1 | 2018-06-20 15:48:49 +0300 | [diff] [blame] | 1222 | update_inline_extent_backref(path, iref, -refs_to_drop, NULL, |
| 1223 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1224 | } else if (is_data) { |
Nikolay Borisov | e9f6290 | 2018-06-20 15:48:46 +0300 | [diff] [blame] | 1225 | ret = remove_extent_data_ref(trans, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1226 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1227 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1228 | *last_ref = 1; |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 1229 | ret = btrfs_del_item(trans, trans->fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1230 | } |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1234 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1235 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1236 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1237 | int j, ret = 0; |
| 1238 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1239 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1240 | |
| 1241 | if (WARN_ON(start != aligned_start)) { |
| 1242 | len -= aligned_start - start; |
| 1243 | len = round_down(len, 1 << 9); |
| 1244 | start = aligned_start; |
| 1245 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1246 | |
| 1247 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1248 | |
| 1249 | if (!len) |
| 1250 | return 0; |
| 1251 | |
| 1252 | end = start + len; |
| 1253 | bytes_left = len; |
| 1254 | |
| 1255 | /* Skip any superblocks on this device. */ |
| 1256 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1257 | u64 sb_start = btrfs_sb_offset(j); |
| 1258 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1259 | u64 size = sb_start - start; |
| 1260 | |
| 1261 | if (!in_range(sb_start, start, bytes_left) && |
| 1262 | !in_range(sb_end, start, bytes_left) && |
| 1263 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1264 | continue; |
| 1265 | |
| 1266 | /* |
| 1267 | * Superblock spans beginning of range. Adjust start and |
| 1268 | * try again. |
| 1269 | */ |
| 1270 | if (sb_start <= start) { |
| 1271 | start += sb_end - start; |
| 1272 | if (start > end) { |
| 1273 | bytes_left = 0; |
| 1274 | break; |
| 1275 | } |
| 1276 | bytes_left = end - start; |
| 1277 | continue; |
| 1278 | } |
| 1279 | |
| 1280 | if (size) { |
| 1281 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1282 | GFP_NOFS, 0); |
| 1283 | if (!ret) |
| 1284 | *discarded_bytes += size; |
| 1285 | else if (ret != -EOPNOTSUPP) |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | start = sb_end; |
| 1290 | if (start > end) { |
| 1291 | bytes_left = 0; |
| 1292 | break; |
| 1293 | } |
| 1294 | bytes_left = end - start; |
| 1295 | } |
| 1296 | |
| 1297 | if (bytes_left) { |
| 1298 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1299 | GFP_NOFS, 0); |
| 1300 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1301 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1302 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1303 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1304 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1305 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1306 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 1307 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1308 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1309 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1310 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1311 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1312 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1313 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 1314 | /* |
| 1315 | * Avoid races with device replace and make sure our bbio has devices |
| 1316 | * associated to its stripes that don't go away while we are discarding. |
| 1317 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1318 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1319 | /* Tell the block device(s) that the sectors can be discarded */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1320 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
| 1321 | &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1322 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1323 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1324 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1325 | int i; |
| 1326 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1327 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1328 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1329 | u64 bytes; |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1330 | struct request_queue *req_q; |
| 1331 | |
Filipe Manana | 627e087 | 2018-01-30 18:40:22 +0000 | [diff] [blame] | 1332 | if (!stripe->dev->bdev) { |
| 1333 | ASSERT(btrfs_test_opt(fs_info, DEGRADED)); |
| 1334 | continue; |
| 1335 | } |
Anand Jain | 38b5f68 | 2017-11-29 18:53:43 +0800 | [diff] [blame] | 1336 | req_q = bdev_get_queue(stripe->dev->bdev); |
| 1337 | if (!blk_queue_discard(req_q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1338 | continue; |
| 1339 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1340 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1341 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1342 | stripe->length, |
| 1343 | &bytes); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1344 | if (!ret) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1345 | discarded_bytes += bytes; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1346 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1347 | 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] | 1348 | |
| 1349 | /* |
| 1350 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1351 | * just ignore the return value so we don't screw up |
| 1352 | * people calling discard_extent. |
| 1353 | */ |
| 1354 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1355 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1356 | btrfs_put_bbio(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1357 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1358 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1359 | |
| 1360 | if (actual_bytes) |
| 1361 | *actual_bytes = discarded_bytes; |
| 1362 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1363 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1364 | if (ret == -EOPNOTSUPP) |
| 1365 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1366 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1367 | } |
| 1368 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1369 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1370 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1371 | struct btrfs_ref *generic_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1372 | { |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1373 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1374 | int old_ref_mod, new_ref_mod; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1375 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1376 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1377 | ASSERT(generic_ref->type != BTRFS_REF_NOT_SET && |
| 1378 | generic_ref->action); |
| 1379 | BUG_ON(generic_ref->type == BTRFS_REF_METADATA && |
| 1380 | generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1381 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1382 | if (generic_ref->type == BTRFS_REF_METADATA) |
| 1383 | ret = btrfs_add_delayed_tree_ref(trans, generic_ref, |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 1384 | NULL, &old_ref_mod, &new_ref_mod); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1385 | else |
| 1386 | ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1387 | &old_ref_mod, &new_ref_mod); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1388 | |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 1389 | btrfs_ref_tree_mod(fs_info, generic_ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 1390 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 1391 | if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 1392 | sub_pinned_bytes(fs_info, generic_ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 1393 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1394 | return ret; |
| 1395 | } |
| 1396 | |
Nikolay Borisov | bd3c685 | 2018-06-18 14:59:25 +0300 | [diff] [blame] | 1397 | /* |
| 1398 | * __btrfs_inc_extent_ref - insert backreference for a given extent |
| 1399 | * |
| 1400 | * @trans: Handle of transaction |
| 1401 | * |
| 1402 | * @node: The delayed ref node used to get the bytenr/length for |
| 1403 | * extent whose references are incremented. |
| 1404 | * |
| 1405 | * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/ |
| 1406 | * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical |
| 1407 | * bytenr of the parent block. Since new extents are always |
| 1408 | * created with indirect references, this will only be the case |
| 1409 | * when relocating a shared extent. In that case, root_objectid |
| 1410 | * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must |
| 1411 | * be 0 |
| 1412 | * |
| 1413 | * @root_objectid: The id of the root where this modification has originated, |
| 1414 | * this can be either one of the well-known metadata trees or |
| 1415 | * the subvolume id which references this extent. |
| 1416 | * |
| 1417 | * @owner: For data extents it is the inode number of the owning file. |
| 1418 | * For metadata extents this parameter holds the level in the |
| 1419 | * tree of the extent. |
| 1420 | * |
| 1421 | * @offset: For metadata extents the offset is ignored and is currently |
| 1422 | * always passed as 0. For data extents it is the fileoffset |
| 1423 | * this extent belongs to. |
| 1424 | * |
| 1425 | * @refs_to_add Number of references to add |
| 1426 | * |
| 1427 | * @extent_op Pointer to a structure, holding information necessary when |
| 1428 | * updating a tree block's flags |
| 1429 | * |
| 1430 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1431 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1432 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1433 | u64 parent, u64 root_objectid, |
| 1434 | u64 owner, u64 offset, int refs_to_add, |
| 1435 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1436 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1437 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1438 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1439 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1440 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1441 | u64 bytenr = node->bytenr; |
| 1442 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1443 | u64 refs; |
| 1444 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1445 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1446 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1447 | if (!path) |
| 1448 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1449 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1450 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1451 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1452 | /* 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] | 1453 | ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes, |
| 1454 | parent, root_objectid, owner, |
| 1455 | offset, refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 1456 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1457 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1458 | |
| 1459 | /* |
| 1460 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 1461 | * inline extent ref, so just update the reference count and add a |
| 1462 | * normal backref. |
| 1463 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1464 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1465 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1466 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1467 | refs = btrfs_extent_refs(leaf, item); |
| 1468 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1469 | if (extent_op) |
| 1470 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1471 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1472 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1473 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1474 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1475 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1476 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1477 | /* now insert the actual backref */ |
Nikolay Borisov | 3759341 | 2018-06-20 15:48:45 +0300 | [diff] [blame] | 1478 | ret = insert_extent_backref(trans, path, bytenr, parent, root_objectid, |
| 1479 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1480 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1481 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1482 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1483 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 1484 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1485 | } |
| 1486 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1487 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1488 | struct btrfs_delayed_ref_node *node, |
| 1489 | struct btrfs_delayed_extent_op *extent_op, |
| 1490 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1491 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1492 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1493 | struct btrfs_delayed_data_ref *ref; |
| 1494 | struct btrfs_key ins; |
| 1495 | u64 parent = 0; |
| 1496 | u64 ref_root = 0; |
| 1497 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1498 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1499 | ins.objectid = node->bytenr; |
| 1500 | ins.offset = node->num_bytes; |
| 1501 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1502 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1503 | ref = btrfs_delayed_node_to_data_ref(node); |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1504 | trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1505 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1506 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1507 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1508 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1509 | |
| 1510 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1511 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1512 | flags |= extent_op->flags_to_set; |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 1513 | ret = alloc_reserved_file_extent(trans, parent, ref_root, |
| 1514 | flags, ref->objectid, |
| 1515 | ref->offset, &ins, |
| 1516 | node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1517 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1518 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1519 | ref->objectid, ref->offset, |
| 1520 | node->ref_mod, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1521 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1522 | ret = __btrfs_free_extent(trans, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1523 | ref_root, ref->objectid, |
| 1524 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1525 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1526 | } else { |
| 1527 | BUG(); |
| 1528 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1529 | return ret; |
| 1530 | } |
| 1531 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1532 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1533 | struct extent_buffer *leaf, |
| 1534 | struct btrfs_extent_item *ei) |
| 1535 | { |
| 1536 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1537 | if (extent_op->update_flags) { |
| 1538 | flags |= extent_op->flags_to_set; |
| 1539 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1540 | } |
| 1541 | |
| 1542 | if (extent_op->update_key) { |
| 1543 | struct btrfs_tree_block_info *bi; |
| 1544 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1545 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1546 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1551 | struct btrfs_delayed_ref_head *head, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1552 | struct btrfs_delayed_extent_op *extent_op) |
| 1553 | { |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1554 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1555 | struct btrfs_key key; |
| 1556 | struct btrfs_path *path; |
| 1557 | struct btrfs_extent_item *ei; |
| 1558 | struct extent_buffer *leaf; |
| 1559 | u32 item_size; |
| 1560 | int ret; |
| 1561 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1562 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1563 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1564 | if (trans->aborted) |
| 1565 | return 0; |
| 1566 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1567 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1568 | metadata = 0; |
| 1569 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1570 | path = btrfs_alloc_path(); |
| 1571 | if (!path) |
| 1572 | return -ENOMEM; |
| 1573 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1574 | key.objectid = head->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1575 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1576 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1577 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1578 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1579 | } else { |
| 1580 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1581 | key.offset = head->num_bytes; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | again: |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1585 | path->reada = READA_FORWARD; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1586 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1587 | 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] | 1588 | if (ret < 0) { |
| 1589 | err = ret; |
| 1590 | goto out; |
| 1591 | } |
| 1592 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1593 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1594 | if (path->slots[0] > 0) { |
| 1595 | path->slots[0]--; |
| 1596 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1597 | path->slots[0]); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1598 | if (key.objectid == head->bytenr && |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1599 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1600 | key.offset == head->num_bytes) |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1601 | ret = 0; |
| 1602 | } |
| 1603 | if (ret > 0) { |
| 1604 | btrfs_release_path(path); |
| 1605 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1606 | |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1607 | key.objectid = head->bytenr; |
| 1608 | key.offset = head->num_bytes; |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 1609 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1610 | goto again; |
| 1611 | } |
| 1612 | } else { |
| 1613 | err = -EIO; |
| 1614 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1615 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | leaf = path->nodes[0]; |
| 1619 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1620 | |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 1621 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 1622 | err = -EINVAL; |
| 1623 | btrfs_print_v0_err(fs_info); |
| 1624 | btrfs_abort_transaction(trans, err); |
| 1625 | goto out; |
| 1626 | } |
| 1627 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1628 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1629 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1630 | |
| 1631 | btrfs_mark_buffer_dirty(leaf); |
| 1632 | out: |
| 1633 | btrfs_free_path(path); |
| 1634 | return err; |
| 1635 | } |
| 1636 | |
| 1637 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1638 | struct btrfs_delayed_ref_node *node, |
| 1639 | struct btrfs_delayed_extent_op *extent_op, |
| 1640 | int insert_reserved) |
| 1641 | { |
| 1642 | int ret = 0; |
| 1643 | struct btrfs_delayed_tree_ref *ref; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1644 | u64 parent = 0; |
| 1645 | u64 ref_root = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1646 | |
| 1647 | ref = btrfs_delayed_node_to_tree_ref(node); |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1648 | trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 1649 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1650 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1651 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1652 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1653 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1654 | if (node->ref_mod != 1) { |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1655 | btrfs_err(trans->fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 1656 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 1657 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 1658 | parent); |
| 1659 | return -EIO; |
| 1660 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1661 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1662 | BUG_ON(!extent_op || !extent_op->update_flags); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 1663 | ret = alloc_reserved_tree_block(trans, node, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1664 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Nikolay Borisov | 2590d0f | 2018-06-20 15:48:59 +0300 | [diff] [blame] | 1665 | ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root, |
| 1666 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1667 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 1668 | ret = __btrfs_free_extent(trans, node, parent, ref_root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 1669 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1670 | } else { |
| 1671 | BUG(); |
| 1672 | } |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1676 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1677 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1678 | struct btrfs_delayed_ref_node *node, |
| 1679 | struct btrfs_delayed_extent_op *extent_op, |
| 1680 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1681 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1682 | int ret = 0; |
| 1683 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1684 | if (trans->aborted) { |
| 1685 | if (insert_reserved) |
Nikolay Borisov | 5fac7f9 | 2018-06-20 15:49:11 +0300 | [diff] [blame] | 1686 | btrfs_pin_extent(trans->fs_info, node->bytenr, |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1687 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1688 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 1689 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1690 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1691 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1692 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Nikolay Borisov | f97806f | 2018-06-20 15:49:04 +0300 | [diff] [blame] | 1693 | ret = run_delayed_tree_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1694 | insert_reserved); |
| 1695 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1696 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Nikolay Borisov | 2bf98ef | 2018-06-20 15:49:00 +0300 | [diff] [blame] | 1697 | ret = run_delayed_data_ref(trans, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1698 | insert_reserved); |
| 1699 | else |
| 1700 | BUG(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 1701 | if (ret && insert_reserved) |
| 1702 | btrfs_pin_extent(trans->fs_info, node->bytenr, |
| 1703 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1704 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1705 | } |
| 1706 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1707 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1708 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1709 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1710 | struct btrfs_delayed_ref_node *ref; |
| 1711 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1712 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 1713 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1714 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1715 | /* |
| 1716 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1717 | * This is to prevent a ref count from going down to zero, which deletes |
| 1718 | * the extent item from the extent tree, when there still are references |
| 1719 | * to add, which would fail because they would not find the extent item. |
| 1720 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1721 | if (!list_empty(&head->ref_add_list)) |
| 1722 | return list_first_entry(&head->ref_add_list, |
| 1723 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 1724 | |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1725 | ref = rb_entry(rb_first_cached(&head->ref_tree), |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 1726 | struct btrfs_delayed_ref_node, ref_node); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 1727 | ASSERT(list_empty(&ref->add_list)); |
| 1728 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1729 | } |
| 1730 | |
Josef Bacik | 2eadaa2 | 2017-09-29 15:43:52 -0400 | [diff] [blame] | 1731 | static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
| 1732 | struct btrfs_delayed_ref_head *head) |
| 1733 | { |
| 1734 | spin_lock(&delayed_refs->lock); |
| 1735 | head->processing = 0; |
| 1736 | delayed_refs->num_heads_ready++; |
| 1737 | spin_unlock(&delayed_refs->lock); |
| 1738 | btrfs_delayed_ref_unlock(head); |
| 1739 | } |
| 1740 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1741 | static struct btrfs_delayed_extent_op *cleanup_extent_op( |
| 1742 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1743 | { |
| 1744 | struct btrfs_delayed_extent_op *extent_op = head->extent_op; |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1745 | |
| 1746 | if (!extent_op) |
| 1747 | return NULL; |
| 1748 | |
| 1749 | if (head->must_insert_reserved) { |
| 1750 | head->extent_op = NULL; |
| 1751 | btrfs_free_delayed_extent_op(extent_op); |
| 1752 | return NULL; |
| 1753 | } |
| 1754 | return extent_op; |
| 1755 | } |
| 1756 | |
| 1757 | static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans, |
| 1758 | struct btrfs_delayed_ref_head *head) |
| 1759 | { |
| 1760 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1761 | int ret; |
| 1762 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1763 | extent_op = cleanup_extent_op(head); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1764 | if (!extent_op) |
| 1765 | return 0; |
| 1766 | head->extent_op = NULL; |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1767 | spin_unlock(&head->lock); |
Nikolay Borisov | 20b9a2d | 2018-06-20 15:49:01 +0300 | [diff] [blame] | 1768 | ret = run_delayed_extent_op(trans, head, extent_op); |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 1769 | btrfs_free_delayed_extent_op(extent_op); |
| 1770 | return ret ? ret : 1; |
| 1771 | } |
| 1772 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1773 | void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, |
| 1774 | struct btrfs_delayed_ref_root *delayed_refs, |
| 1775 | struct btrfs_delayed_ref_head *head) |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1776 | { |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1777 | int nr_items = 1; /* Dropping this ref head update. */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1778 | |
| 1779 | if (head->total_ref_mod < 0) { |
| 1780 | struct btrfs_space_info *space_info; |
| 1781 | u64 flags; |
| 1782 | |
| 1783 | if (head->is_data) |
| 1784 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 1785 | else if (head->is_system) |
| 1786 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 1787 | else |
| 1788 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 1789 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1790 | ASSERT(space_info); |
| 1791 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 1792 | -head->num_bytes, |
| 1793 | BTRFS_TOTAL_BYTES_PINNED_BATCH); |
| 1794 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1795 | /* |
| 1796 | * We had csum deletions accounted for in our delayed refs rsv, |
| 1797 | * we need to drop the csum leaves for this update from our |
| 1798 | * delayed_refs_rsv. |
| 1799 | */ |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1800 | if (head->is_data) { |
| 1801 | spin_lock(&delayed_refs->lock); |
| 1802 | delayed_refs->pending_csums -= head->num_bytes; |
| 1803 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1804 | nr_items += btrfs_csum_bytes_to_leaves(fs_info, |
| 1805 | head->num_bytes); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
Josef Bacik | ba2c4d4 | 2018-12-03 10:20:33 -0500 | [diff] [blame] | 1809 | btrfs_delayed_refs_rsv_release(fs_info, nr_items); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1810 | } |
| 1811 | |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1812 | static int cleanup_ref_head(struct btrfs_trans_handle *trans, |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1813 | struct btrfs_delayed_ref_head *head) |
| 1814 | { |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 1815 | |
| 1816 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1817 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1818 | int ret; |
| 1819 | |
| 1820 | delayed_refs = &trans->transaction->delayed_refs; |
| 1821 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 1822 | ret = run_and_cleanup_extent_op(trans, head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1823 | if (ret < 0) { |
| 1824 | unselect_delayed_ref_head(delayed_refs, head); |
| 1825 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
| 1826 | return ret; |
| 1827 | } else if (ret) { |
| 1828 | return ret; |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | * Need to drop our head ref lock and re-acquire the delayed ref lock |
| 1833 | * and then re-check to make sure nobody got added. |
| 1834 | */ |
| 1835 | spin_unlock(&head->lock); |
| 1836 | spin_lock(&delayed_refs->lock); |
| 1837 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 1838 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) { |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1839 | spin_unlock(&head->lock); |
| 1840 | spin_unlock(&delayed_refs->lock); |
| 1841 | return 1; |
| 1842 | } |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 1843 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1844 | spin_unlock(&head->lock); |
Nikolay Borisov | 1e7a142 | 2018-04-11 11:21:18 +0300 | [diff] [blame] | 1845 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1846 | |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1847 | if (head->must_insert_reserved) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1848 | btrfs_pin_extent(fs_info, head->bytenr, |
| 1849 | head->num_bytes, 1); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1850 | if (head->is_data) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1851 | ret = btrfs_del_csums(trans, fs_info, head->bytenr, |
| 1852 | head->num_bytes); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 1856 | btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head); |
Josef Bacik | 07c4777 | 2018-12-03 10:20:30 -0500 | [diff] [blame] | 1857 | |
| 1858 | trace_run_delayed_ref_head(fs_info, head, 0); |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 1859 | btrfs_delayed_ref_unlock(head); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 1860 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
| 1863 | |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1864 | static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head( |
| 1865 | struct btrfs_trans_handle *trans) |
| 1866 | { |
| 1867 | struct btrfs_delayed_ref_root *delayed_refs = |
| 1868 | &trans->transaction->delayed_refs; |
| 1869 | struct btrfs_delayed_ref_head *head = NULL; |
| 1870 | int ret; |
| 1871 | |
| 1872 | spin_lock(&delayed_refs->lock); |
Lu Fengqi | 5637c74 | 2018-10-11 13:40:33 +0800 | [diff] [blame] | 1873 | head = btrfs_select_ref_head(delayed_refs); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1874 | if (!head) { |
| 1875 | spin_unlock(&delayed_refs->lock); |
| 1876 | return head; |
| 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * Grab the lock that says we are going to process all the refs for |
| 1881 | * this head |
| 1882 | */ |
Lu Fengqi | 9e920a6 | 2018-10-11 13:40:34 +0800 | [diff] [blame] | 1883 | ret = btrfs_delayed_ref_lock(delayed_refs, head); |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1884 | spin_unlock(&delayed_refs->lock); |
| 1885 | |
| 1886 | /* |
| 1887 | * We may have dropped the spin lock to get the head mutex lock, and |
| 1888 | * that might have given someone else time to free the head. If that's |
| 1889 | * true, it has been removed from our list and we can move on. |
| 1890 | */ |
| 1891 | if (ret == -EAGAIN) |
| 1892 | head = ERR_PTR(-EAGAIN); |
| 1893 | |
| 1894 | return head; |
| 1895 | } |
| 1896 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1897 | static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans, |
| 1898 | struct btrfs_delayed_ref_head *locked_ref, |
| 1899 | unsigned long *run_refs) |
| 1900 | { |
| 1901 | struct btrfs_fs_info *fs_info = trans->fs_info; |
| 1902 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1903 | struct btrfs_delayed_extent_op *extent_op; |
| 1904 | struct btrfs_delayed_ref_node *ref; |
| 1905 | int must_insert_reserved = 0; |
| 1906 | int ret; |
| 1907 | |
| 1908 | delayed_refs = &trans->transaction->delayed_refs; |
| 1909 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1910 | lockdep_assert_held(&locked_ref->mutex); |
| 1911 | lockdep_assert_held(&locked_ref->lock); |
| 1912 | |
Nikolay Borisov | e726138 | 2018-08-15 10:39:55 +0300 | [diff] [blame] | 1913 | while ((ref = select_delayed_ref(locked_ref))) { |
| 1914 | if (ref->seq && |
| 1915 | btrfs_check_delayed_seq(fs_info, ref->seq)) { |
| 1916 | spin_unlock(&locked_ref->lock); |
| 1917 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1918 | return -EAGAIN; |
| 1919 | } |
| 1920 | |
| 1921 | (*run_refs)++; |
| 1922 | ref->in_tree = 0; |
| 1923 | rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree); |
| 1924 | RB_CLEAR_NODE(&ref->ref_node); |
| 1925 | if (!list_empty(&ref->add_list)) |
| 1926 | list_del(&ref->add_list); |
| 1927 | /* |
| 1928 | * When we play the delayed ref, also correct the ref_mod on |
| 1929 | * head |
| 1930 | */ |
| 1931 | switch (ref->action) { |
| 1932 | case BTRFS_ADD_DELAYED_REF: |
| 1933 | case BTRFS_ADD_DELAYED_EXTENT: |
| 1934 | locked_ref->ref_mod -= ref->ref_mod; |
| 1935 | break; |
| 1936 | case BTRFS_DROP_DELAYED_REF: |
| 1937 | locked_ref->ref_mod += ref->ref_mod; |
| 1938 | break; |
| 1939 | default: |
| 1940 | WARN_ON(1); |
| 1941 | } |
| 1942 | atomic_dec(&delayed_refs->num_entries); |
| 1943 | |
| 1944 | /* |
| 1945 | * Record the must_insert_reserved flag before we drop the |
| 1946 | * spin lock. |
| 1947 | */ |
| 1948 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1949 | locked_ref->must_insert_reserved = 0; |
| 1950 | |
| 1951 | extent_op = locked_ref->extent_op; |
| 1952 | locked_ref->extent_op = NULL; |
| 1953 | spin_unlock(&locked_ref->lock); |
| 1954 | |
| 1955 | ret = run_one_delayed_ref(trans, ref, extent_op, |
| 1956 | must_insert_reserved); |
| 1957 | |
| 1958 | btrfs_free_delayed_extent_op(extent_op); |
| 1959 | if (ret) { |
| 1960 | unselect_delayed_ref_head(delayed_refs, locked_ref); |
| 1961 | btrfs_put_delayed_ref(ref); |
| 1962 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 1963 | ret); |
| 1964 | return ret; |
| 1965 | } |
| 1966 | |
| 1967 | btrfs_put_delayed_ref(ref); |
| 1968 | cond_resched(); |
| 1969 | |
| 1970 | spin_lock(&locked_ref->lock); |
| 1971 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
| 1972 | } |
| 1973 | |
| 1974 | return 0; |
| 1975 | } |
| 1976 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1977 | /* |
| 1978 | * Returns 0 on success or if called with an already aborted transaction. |
| 1979 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 1980 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1981 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1982 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1983 | { |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 1984 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1985 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1986 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 1987 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1988 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 1989 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 1990 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1991 | |
| 1992 | delayed_refs = &trans->transaction->delayed_refs; |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1993 | do { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1994 | if (!locked_ref) { |
Nikolay Borisov | b1cdbcb | 2018-08-15 10:39:54 +0300 | [diff] [blame] | 1995 | locked_ref = btrfs_obtain_ref_head(trans); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 1996 | if (IS_ERR_OR_NULL(locked_ref)) { |
| 1997 | if (PTR_ERR(locked_ref) == -EAGAIN) { |
| 1998 | continue; |
| 1999 | } else { |
| 2000 | break; |
| 2001 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2002 | } |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2003 | count++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2004 | } |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2005 | /* |
| 2006 | * We need to try and merge add/drops of the same ref since we |
| 2007 | * can run into issues with relocate dropping the implicit ref |
| 2008 | * and then it being added back again before the drop can |
| 2009 | * finish. If we merged anything we need to re-loop so we can |
| 2010 | * get a good ref. |
| 2011 | * Or we can get node references of the same type that weren't |
| 2012 | * merged when created due to bumps in the tree mod seq, and |
| 2013 | * we need to merge them to prevent adding an inline extent |
| 2014 | * backref before dropping it (triggering a BUG_ON at |
| 2015 | * insert_inline_extent_backref()). |
| 2016 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2017 | spin_lock(&locked_ref->lock); |
Nikolay Borisov | be97f13 | 2018-04-19 11:06:39 +0300 | [diff] [blame] | 2018 | btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2019 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2020 | ret = btrfs_run_delayed_refs_for_head(trans, locked_ref, |
| 2021 | &actual_count); |
| 2022 | if (ret < 0 && ret != -EAGAIN) { |
| 2023 | /* |
| 2024 | * Error, btrfs_run_delayed_refs_for_head already |
| 2025 | * unlocked everything so just bail out |
| 2026 | */ |
| 2027 | return ret; |
| 2028 | } else if (!ret) { |
| 2029 | /* |
| 2030 | * Success, perform the usual cleanup of a processed |
| 2031 | * head |
| 2032 | */ |
Nikolay Borisov | f9871ed | 2018-06-20 15:49:03 +0300 | [diff] [blame] | 2033 | ret = cleanup_ref_head(trans, locked_ref); |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2034 | if (ret > 0 ) { |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2035 | /* We dropped our lock, we need to loop. */ |
| 2036 | ret = 0; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2037 | continue; |
Josef Bacik | 194ab0b | 2017-09-29 15:43:54 -0400 | [diff] [blame] | 2038 | } else if (ret) { |
| 2039 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2040 | } |
Josef Bacik | c1103f7 | 2017-09-29 15:43:56 -0400 | [diff] [blame] | 2041 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2042 | |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2043 | /* |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2044 | * Either success case or btrfs_run_delayed_refs_for_head |
| 2045 | * returned -EAGAIN, meaning we need to select another head |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2046 | */ |
Josef Bacik | b00e625 | 2017-09-29 15:43:53 -0400 | [diff] [blame] | 2047 | |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2048 | locked_ref = NULL; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2049 | cond_resched(); |
Nikolay Borisov | 0110a4c | 2018-08-15 10:39:56 +0300 | [diff] [blame] | 2050 | } while ((nr != -1 && count < nr) || locked_ref); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2051 | |
| 2052 | /* |
| 2053 | * We don't want to include ref heads since we can have empty ref heads |
| 2054 | * and those will drastically skew our runtime down since we just do |
| 2055 | * accounting, no actual extent tree updates. |
| 2056 | */ |
| 2057 | if (actual_count > 0) { |
| 2058 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2059 | u64 avg; |
| 2060 | |
| 2061 | /* |
| 2062 | * We weigh the current average higher than our current runtime |
| 2063 | * to avoid large swings in the average. |
| 2064 | */ |
| 2065 | spin_lock(&delayed_refs->lock); |
| 2066 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2067 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2068 | spin_unlock(&delayed_refs->lock); |
| 2069 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2070 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2071 | } |
| 2072 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2073 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2074 | /* |
| 2075 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2076 | * correlates in most cases to the order added. To expose dependencies on this |
| 2077 | * order, we start to process the tree in the middle instead of the beginning |
| 2078 | */ |
| 2079 | static u64 find_middle(struct rb_root *root) |
| 2080 | { |
| 2081 | struct rb_node *n = root->rb_node; |
| 2082 | struct btrfs_delayed_ref_node *entry; |
| 2083 | int alt = 1; |
| 2084 | u64 middle; |
| 2085 | u64 first = 0, last = 0; |
| 2086 | |
| 2087 | n = rb_first(root); |
| 2088 | if (n) { |
| 2089 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2090 | first = entry->bytenr; |
| 2091 | } |
| 2092 | n = rb_last(root); |
| 2093 | if (n) { |
| 2094 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2095 | last = entry->bytenr; |
| 2096 | } |
| 2097 | n = root->rb_node; |
| 2098 | |
| 2099 | while (n) { |
| 2100 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2101 | WARN_ON(!entry->in_tree); |
| 2102 | |
| 2103 | middle = entry->bytenr; |
| 2104 | |
| 2105 | if (alt) |
| 2106 | n = n->rb_left; |
| 2107 | else |
| 2108 | n = n->rb_right; |
| 2109 | |
| 2110 | alt = 1 - alt; |
| 2111 | } |
| 2112 | return middle; |
| 2113 | } |
| 2114 | #endif |
| 2115 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2116 | 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] | 2117 | { |
| 2118 | u64 num_bytes; |
| 2119 | |
| 2120 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2121 | sizeof(struct btrfs_extent_inline_ref)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2122 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2123 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2124 | |
| 2125 | /* |
| 2126 | * 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] | 2127 | * closer to what we're really going to want to use. |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2128 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2129 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2130 | } |
| 2131 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2132 | /* |
| 2133 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2134 | * would require to store the csums for that many bytes. |
| 2135 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2136 | 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] | 2137 | { |
| 2138 | u64 csum_size; |
| 2139 | u64 num_csums_per_leaf; |
| 2140 | u64 num_csums; |
| 2141 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2142 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2143 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2144 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2145 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2146 | num_csums += num_csums_per_leaf - 1; |
| 2147 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2148 | return num_csums; |
| 2149 | } |
| 2150 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2151 | /* |
| 2152 | * this starts processing the delayed reference count updates and |
| 2153 | * extent insertions we have queued up so far. count can be |
| 2154 | * 0, which means to process everything in the tree at the start |
| 2155 | * of the run (but not newly added entries), or it can be some target |
| 2156 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2157 | * |
| 2158 | * Returns 0 on success or if called with an aborted transaction |
| 2159 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2160 | */ |
| 2161 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2162 | unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2163 | { |
Nikolay Borisov | c79a70b | 2018-03-15 17:27:37 +0200 | [diff] [blame] | 2164 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2165 | struct rb_node *node; |
| 2166 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2167 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2168 | int ret; |
| 2169 | int run_all = count == (unsigned long)-1; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2170 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2171 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2172 | if (trans->aborted) |
| 2173 | return 0; |
| 2174 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2175 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2176 | return 0; |
| 2177 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2178 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2179 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2180 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2181 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2182 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2183 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2184 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2185 | #endif |
Nikolay Borisov | 0a1e458 | 2018-03-15 16:00:27 +0200 | [diff] [blame] | 2186 | ret = __btrfs_run_delayed_refs(trans, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2187 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2188 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2189 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2190 | } |
| 2191 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2192 | if (run_all) { |
Josef Bacik | 119e80d | 2018-11-21 14:05:42 -0500 | [diff] [blame] | 2193 | btrfs_create_pending_block_groups(trans); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2194 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2195 | spin_lock(&delayed_refs->lock); |
Liu Bo | 5c9d028 | 2018-08-23 03:51:49 +0800 | [diff] [blame] | 2196 | node = rb_first_cached(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2197 | if (!node) { |
| 2198 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2199 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2200 | } |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2201 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2202 | href_node); |
| 2203 | refcount_inc(&head->refs); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2204 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2205 | |
| 2206 | /* Mutex was contended, block until it's released and retry. */ |
| 2207 | mutex_lock(&head->mutex); |
| 2208 | mutex_unlock(&head->mutex); |
| 2209 | |
| 2210 | btrfs_put_delayed_ref_head(head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2211 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2212 | goto again; |
| 2213 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2214 | out: |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2215 | return 0; |
| 2216 | } |
| 2217 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2218 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2219 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2220 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2221 | { |
| 2222 | struct btrfs_delayed_extent_op *extent_op; |
| 2223 | int ret; |
| 2224 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2225 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2226 | if (!extent_op) |
| 2227 | return -ENOMEM; |
| 2228 | |
| 2229 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 2230 | extent_op->update_flags = true; |
| 2231 | extent_op->update_key = false; |
| 2232 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2233 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2234 | |
David Sterba | c6e340b | 2019-03-20 11:42:34 +0100 | [diff] [blame] | 2235 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2236 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2237 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2238 | return ret; |
| 2239 | } |
| 2240 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2241 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2242 | struct btrfs_path *path, |
| 2243 | u64 objectid, u64 offset, u64 bytenr) |
| 2244 | { |
| 2245 | struct btrfs_delayed_ref_head *head; |
| 2246 | struct btrfs_delayed_ref_node *ref; |
| 2247 | struct btrfs_delayed_data_ref *data_ref; |
| 2248 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2249 | struct btrfs_transaction *cur_trans; |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2250 | struct rb_node *node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2251 | int ret = 0; |
| 2252 | |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2253 | spin_lock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2254 | cur_trans = root->fs_info->running_transaction; |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2255 | if (cur_trans) |
| 2256 | refcount_inc(&cur_trans->use_count); |
| 2257 | spin_unlock(&root->fs_info->trans_lock); |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2258 | if (!cur_trans) |
| 2259 | return 0; |
| 2260 | |
| 2261 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2262 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 2263 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2264 | if (!head) { |
| 2265 | spin_unlock(&delayed_refs->lock); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2266 | btrfs_put_transaction(cur_trans); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2267 | return 0; |
| 2268 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2269 | |
| 2270 | if (!mutex_trylock(&head->mutex)) { |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2271 | refcount_inc(&head->refs); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2272 | spin_unlock(&delayed_refs->lock); |
| 2273 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2274 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2275 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2276 | /* |
| 2277 | * Mutex was contended, block until it's released and let |
| 2278 | * caller try again |
| 2279 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2280 | mutex_lock(&head->mutex); |
| 2281 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 2282 | btrfs_put_delayed_ref_head(head); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2283 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2284 | return -EAGAIN; |
| 2285 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2286 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2287 | |
| 2288 | spin_lock(&head->lock); |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2289 | /* |
| 2290 | * XXX: We should replace this with a proper search function in the |
| 2291 | * future. |
| 2292 | */ |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 2293 | for (node = rb_first_cached(&head->ref_tree); node; |
| 2294 | node = rb_next(node)) { |
Josef Bacik | 0e0adbc | 2017-10-19 14:16:00 -0400 | [diff] [blame] | 2295 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2296 | /* If it's a shared ref we know a cross reference exists */ |
| 2297 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 2298 | ret = 1; |
| 2299 | break; |
| 2300 | } |
| 2301 | |
| 2302 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2303 | |
| 2304 | /* |
| 2305 | * If our ref doesn't match the one we're currently looking at |
| 2306 | * then we have a cross reference. |
| 2307 | */ |
| 2308 | if (data_ref->root != root->root_key.objectid || |
| 2309 | data_ref->objectid != objectid || |
| 2310 | data_ref->offset != offset) { |
| 2311 | ret = 1; |
| 2312 | break; |
| 2313 | } |
| 2314 | } |
| 2315 | spin_unlock(&head->lock); |
| 2316 | mutex_unlock(&head->mutex); |
ethanwu | 998ac6d | 2018-04-29 15:59:42 +0800 | [diff] [blame] | 2317 | btrfs_put_transaction(cur_trans); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2318 | return ret; |
| 2319 | } |
| 2320 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2321 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2322 | struct btrfs_path *path, |
| 2323 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2324 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2325 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2326 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2327 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2328 | struct btrfs_extent_data_ref *ref; |
| 2329 | struct btrfs_extent_inline_ref *iref; |
| 2330 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2331 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2332 | u32 item_size; |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2333 | int type; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2334 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2335 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2336 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2337 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2338 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2339 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2340 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2341 | if (ret < 0) |
| 2342 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2343 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2344 | |
| 2345 | ret = -ENOENT; |
| 2346 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2347 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2348 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2349 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2350 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2351 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2352 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2353 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2354 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2355 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2356 | ret = 1; |
| 2357 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2358 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2359 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2360 | /* 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] | 2361 | if (item_size != sizeof(*ei) + |
| 2362 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2363 | goto out; |
| 2364 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2365 | /* If extent created before last snapshot => it's definitely shared */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2366 | if (btrfs_extent_generation(leaf, ei) <= |
| 2367 | btrfs_root_last_snapshot(&root->root_item)) |
| 2368 | goto out; |
| 2369 | |
| 2370 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2371 | |
Nikolay Borisov | a6bd9cd | 2019-08-21 10:42:57 +0300 | [diff] [blame] | 2372 | /* If this extent has SHARED_DATA_REF then it's shared */ |
Liu Bo | 3de28d5 | 2017-08-18 15:15:19 -0600 | [diff] [blame] | 2373 | type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA); |
| 2374 | if (type != BTRFS_EXTENT_DATA_REF_KEY) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2375 | goto out; |
| 2376 | |
| 2377 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2378 | if (btrfs_extent_refs(leaf, ei) != |
| 2379 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2380 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2381 | root->root_key.objectid || |
| 2382 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2383 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2384 | goto out; |
| 2385 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2386 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2387 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2388 | return ret; |
| 2389 | } |
| 2390 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2391 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
| 2392 | u64 bytenr) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2393 | { |
| 2394 | struct btrfs_path *path; |
| 2395 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2396 | |
| 2397 | path = btrfs_alloc_path(); |
| 2398 | if (!path) |
Su Yue | 9132c4f | 2018-05-30 14:49:10 +0800 | [diff] [blame] | 2399 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2400 | |
| 2401 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 2402 | ret = check_committed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2403 | offset, bytenr); |
| 2404 | if (ret && ret != -ENOENT) |
| 2405 | goto out; |
| 2406 | |
Misono Tomohiro | 380fd06 | 2018-08-30 10:59:16 +0900 | [diff] [blame] | 2407 | ret = check_delayed_ref(root, path, objectid, offset, bytenr); |
| 2408 | } while (ret == -EAGAIN); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2409 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2410 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2411 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2412 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2413 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2414 | return ret; |
| 2415 | } |
| 2416 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2417 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2418 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2419 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2420 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2421 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2422 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2423 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2424 | u64 num_bytes; |
| 2425 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2426 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2427 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2428 | struct btrfs_key key; |
| 2429 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2430 | struct btrfs_ref generic_ref = { 0 }; |
| 2431 | bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2432 | int i; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2433 | int action; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2434 | int level; |
| 2435 | int ret = 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2436 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2437 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 2438 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 2439 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2440 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2441 | nritems = btrfs_header_nritems(buf); |
| 2442 | level = btrfs_header_level(buf); |
| 2443 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 2444 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2445 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2446 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2447 | if (full_backref) |
| 2448 | parent = buf->start; |
| 2449 | else |
| 2450 | parent = 0; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2451 | if (inc) |
| 2452 | action = BTRFS_ADD_DELAYED_REF; |
| 2453 | else |
| 2454 | action = BTRFS_DROP_DELAYED_REF; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2455 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2456 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2457 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2458 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 2459 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2460 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2461 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2462 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2463 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2464 | BTRFS_FILE_EXTENT_INLINE) |
| 2465 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2466 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2467 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2468 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2469 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2470 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2471 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2472 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2473 | num_bytes, parent); |
| 2474 | generic_ref.real_root = root->root_key.objectid; |
| 2475 | btrfs_init_data_ref(&generic_ref, ref_root, key.objectid, |
| 2476 | key.offset); |
| 2477 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2478 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2479 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2480 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2481 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2482 | if (ret) |
| 2483 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2484 | } else { |
| 2485 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2486 | num_bytes = fs_info->nodesize; |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2487 | btrfs_init_generic_ref(&generic_ref, action, bytenr, |
| 2488 | num_bytes, parent); |
| 2489 | generic_ref.real_root = root->root_key.objectid; |
| 2490 | btrfs_init_tree_ref(&generic_ref, level - 1, ref_root); |
| 2491 | generic_ref.skip_qgroup = for_reloc; |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2492 | if (inc) |
Qu Wenruo | 82fa113 | 2019-04-04 14:45:35 +0800 | [diff] [blame] | 2493 | ret = btrfs_inc_extent_ref(trans, &generic_ref); |
Qu Wenruo | dd28b6a | 2019-04-04 14:45:30 +0800 | [diff] [blame] | 2494 | else |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 2495 | ret = btrfs_free_extent(trans, &generic_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2496 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2497 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2498 | } |
| 2499 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2500 | return 0; |
| 2501 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2502 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2503 | } |
| 2504 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2505 | 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] | 2506 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2507 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2508 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2509 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2510 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2511 | 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] | 2512 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2513 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 2514 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2515 | } |
| 2516 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2517 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2518 | { |
| 2519 | struct btrfs_block_group_cache *block_group; |
| 2520 | int readonly = 0; |
| 2521 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2522 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2523 | if (!block_group || block_group->ro) |
| 2524 | readonly = 1; |
| 2525 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2526 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2527 | return readonly; |
| 2528 | } |
| 2529 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 2530 | 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] | 2531 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2532 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2533 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2534 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2535 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2536 | if (data) |
| 2537 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2538 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2539 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2540 | else |
| 2541 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 2542 | |
Josef Bacik | 878d7b6 | 2019-06-20 15:38:05 -0400 | [diff] [blame] | 2543 | ret = btrfs_get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2544 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2545 | } |
| 2546 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2547 | 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] | 2548 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2549 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2550 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2551 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2552 | spin_lock(&fs_info->block_group_cache_lock); |
| 2553 | bytenr = fs_info->first_logical_byte; |
| 2554 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 2555 | |
| 2556 | if (bytenr < (u64)-1) |
| 2557 | return bytenr; |
| 2558 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2559 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2560 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2561 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2562 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2563 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2564 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2565 | |
| 2566 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2567 | } |
| 2568 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2569 | static int pin_down_extent(struct btrfs_block_group_cache *cache, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2570 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2571 | { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2572 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 2573 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2574 | spin_lock(&cache->space_info->lock); |
| 2575 | spin_lock(&cache->lock); |
| 2576 | cache->pinned += num_bytes; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2577 | btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info, |
| 2578 | num_bytes); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2579 | if (reserved) { |
| 2580 | cache->reserved -= num_bytes; |
| 2581 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2582 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2583 | spin_unlock(&cache->lock); |
| 2584 | spin_unlock(&cache->space_info->lock); |
| 2585 | |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 2586 | percpu_counter_add_batch(&cache->space_info->total_bytes_pinned, |
| 2587 | num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2588 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2589 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2590 | return 0; |
| 2591 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2592 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2593 | int btrfs_pin_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2594 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2595 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2596 | struct btrfs_block_group_cache *cache; |
| 2597 | |
Nikolay Borisov | ce6d3eb | 2019-10-08 16:26:16 +0300 | [diff] [blame^] | 2598 | ASSERT(fs_info->running_transaction); |
| 2599 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2600 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2601 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2602 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2603 | pin_down_extent(cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2604 | |
| 2605 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2606 | return 0; |
| 2607 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2608 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2609 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2610 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2611 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2612 | 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] | 2613 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2614 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2615 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2616 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2617 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2618 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2619 | if (!cache) |
| 2620 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2621 | |
| 2622 | /* |
| 2623 | * pull in the free space cache (if any) so that our pin |
| 2624 | * removes the free space from the cache. We have load_only set |
| 2625 | * to one because the slow code to read in the free extents does check |
| 2626 | * the pinned extents. |
| 2627 | */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2628 | btrfs_cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2629 | |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 2630 | pin_down_extent(cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2631 | |
| 2632 | /* 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] | 2633 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2634 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 2635 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 2636 | } |
| 2637 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2638 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 2639 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2640 | { |
| 2641 | int ret; |
| 2642 | struct btrfs_block_group_cache *block_group; |
| 2643 | struct btrfs_caching_control *caching_ctl; |
| 2644 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2645 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2646 | if (!block_group) |
| 2647 | return -EINVAL; |
| 2648 | |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2649 | btrfs_cache_block_group(block_group, 0); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2650 | caching_ctl = btrfs_get_caching_control(block_group); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2651 | |
| 2652 | if (!caching_ctl) { |
| 2653 | /* Logic error */ |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2654 | BUG_ON(!btrfs_block_group_cache_done(block_group)); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2655 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 2656 | } else { |
| 2657 | mutex_lock(&caching_ctl->mutex); |
| 2658 | |
| 2659 | if (start >= caching_ctl->progress) { |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2660 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2661 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2662 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 2663 | ret = btrfs_remove_free_space(block_group, |
| 2664 | start, num_bytes); |
| 2665 | } else { |
| 2666 | num_bytes = caching_ctl->progress - start; |
| 2667 | ret = btrfs_remove_free_space(block_group, |
| 2668 | start, num_bytes); |
| 2669 | if (ret) |
| 2670 | goto out_lock; |
| 2671 | |
| 2672 | num_bytes = (start + num_bytes) - |
| 2673 | caching_ctl->progress; |
| 2674 | start = caching_ctl->progress; |
Josef Bacik | 6f410d1 | 2019-06-20 15:37:49 -0400 | [diff] [blame] | 2675 | ret = btrfs_add_excluded_extent(fs_info, start, |
| 2676 | num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2677 | } |
| 2678 | out_lock: |
| 2679 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2680 | btrfs_put_caching_control(caching_ctl); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2681 | } |
| 2682 | btrfs_put_block_group(block_group); |
| 2683 | return ret; |
| 2684 | } |
| 2685 | |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2686 | int btrfs_exclude_logged_extents(struct extent_buffer *eb) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2687 | { |
David Sterba | bcdc428 | 2019-03-20 12:14:33 +0100 | [diff] [blame] | 2688 | struct btrfs_fs_info *fs_info = eb->fs_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2689 | struct btrfs_file_extent_item *item; |
| 2690 | struct btrfs_key key; |
| 2691 | int found_type; |
| 2692 | int i; |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2693 | int ret = 0; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2694 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2695 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2696 | return 0; |
| 2697 | |
| 2698 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 2699 | btrfs_item_key_to_cpu(eb, &key, i); |
| 2700 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 2701 | continue; |
| 2702 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 2703 | found_type = btrfs_file_extent_type(eb, item); |
| 2704 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 2705 | continue; |
| 2706 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 2707 | continue; |
| 2708 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 2709 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2710 | ret = __exclude_logged_extent(fs_info, key.objectid, key.offset); |
| 2711 | if (ret) |
| 2712 | break; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2713 | } |
| 2714 | |
Gu Jinxiang | b89311e | 2018-05-22 17:46:51 +0800 | [diff] [blame] | 2715 | return ret; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 2716 | } |
| 2717 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 2718 | static void |
| 2719 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 2720 | { |
| 2721 | atomic_inc(&bg->reservations); |
| 2722 | } |
| 2723 | |
David Sterba | 8b74c03 | 2017-02-10 19:20:56 +0100 | [diff] [blame] | 2724 | void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2725 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2726 | struct btrfs_caching_control *next; |
| 2727 | struct btrfs_caching_control *caching_ctl; |
| 2728 | struct btrfs_block_group_cache *cache; |
| 2729 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 2730 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2731 | |
| 2732 | list_for_each_entry_safe(caching_ctl, next, |
| 2733 | &fs_info->caching_block_groups, list) { |
| 2734 | cache = caching_ctl->block_group; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 2735 | if (btrfs_block_group_cache_done(cache)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2736 | cache->last_byte_to_unpin = (u64)-1; |
| 2737 | list_del_init(&caching_ctl->list); |
Josef Bacik | e3cb339 | 2019-06-20 15:37:50 -0400 | [diff] [blame] | 2738 | btrfs_put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2739 | } else { |
| 2740 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 2745 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 2746 | else |
| 2747 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 2748 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 2749 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2750 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 2751 | btrfs_update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2752 | } |
| 2753 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2754 | /* |
| 2755 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 2756 | * what it should be based on the mount options. |
| 2757 | */ |
| 2758 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2759 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 2760 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2761 | { |
| 2762 | struct btrfs_free_cluster *ret = NULL; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2763 | |
| 2764 | *empty_cluster = 0; |
| 2765 | if (btrfs_mixed_space_info(space_info)) |
| 2766 | return ret; |
| 2767 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2768 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2769 | ret = &fs_info->meta_alloc_cluster; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2770 | if (btrfs_test_opt(fs_info, SSD)) |
| 2771 | *empty_cluster = SZ_2M; |
| 2772 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2773 | *empty_cluster = SZ_64K; |
Hans van Kranenburg | 583b723 | 2017-07-28 08:31:28 +0200 | [diff] [blame] | 2774 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && |
| 2775 | btrfs_test_opt(fs_info, SSD_SPREAD)) { |
| 2776 | *empty_cluster = SZ_2M; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2777 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | return ret; |
| 2781 | } |
| 2782 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2783 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 2784 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 2785 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2786 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2787 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2788 | struct btrfs_space_info *space_info; |
| 2789 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2790 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2791 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2792 | u64 total_unpinned = 0; |
| 2793 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2794 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2795 | |
| 2796 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2797 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2798 | if (!cache || |
| 2799 | start >= cache->key.objectid + cache->key.offset) { |
| 2800 | if (cache) |
| 2801 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2802 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2803 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2804 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2805 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2806 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2807 | cache->space_info, |
| 2808 | &empty_cluster); |
| 2809 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | len = cache->key.objectid + cache->key.offset - start; |
| 2813 | len = min(len, end + 1 - start); |
| 2814 | |
| 2815 | if (start < cache->last_byte_to_unpin) { |
| 2816 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 2817 | if (return_free_space) |
| 2818 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2819 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2820 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2821 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2822 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2823 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2824 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 2825 | /* |
| 2826 | * If this space cluster has been marked as fragmented and we've |
| 2827 | * unpinned enough in this block group to potentially allow a |
| 2828 | * cluster to be created inside of it go ahead and clear the |
| 2829 | * fragmented check. |
| 2830 | */ |
| 2831 | if (cluster && cluster->fragmented && |
| 2832 | total_unpinned > empty_cluster) { |
| 2833 | spin_lock(&cluster->lock); |
| 2834 | cluster->fragmented = 0; |
| 2835 | spin_unlock(&cluster->lock); |
| 2836 | } |
| 2837 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2838 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2839 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2840 | cache->pinned -= len; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2841 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 2842 | space_info->max_extent_size = 0; |
Ethan Lien | dec59fa | 2018-07-13 16:50:42 +0800 | [diff] [blame] | 2843 | percpu_counter_add_batch(&space_info->total_bytes_pinned, |
| 2844 | -len, BTRFS_TOTAL_BYTES_PINNED_BATCH); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2845 | if (cache->ro) { |
| 2846 | space_info->bytes_readonly += len; |
| 2847 | readonly = true; |
| 2848 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2849 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2850 | if (!readonly && return_free_space && |
| 2851 | global_rsv->space_info == space_info) { |
| 2852 | u64 to_add = len; |
Nikolay Borisov | 92ac58e | 2017-08-17 10:52:28 +0300 | [diff] [blame] | 2853 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2854 | spin_lock(&global_rsv->lock); |
| 2855 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2856 | to_add = min(len, global_rsv->size - |
| 2857 | global_rsv->reserved); |
| 2858 | global_rsv->reserved += to_add; |
Josef Bacik | bb96c4e | 2019-06-18 16:09:21 -0400 | [diff] [blame] | 2859 | btrfs_space_info_update_bytes_may_use(fs_info, |
| 2860 | space_info, to_add); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2861 | if (global_rsv->reserved >= global_rsv->size) |
| 2862 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2863 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2864 | } |
| 2865 | spin_unlock(&global_rsv->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 2866 | /* Add to any tickets we may have */ |
| 2867 | if (len) |
Josef Bacik | 18fa228 | 2019-08-22 15:10:58 -0400 | [diff] [blame] | 2868 | btrfs_try_granting_tickets(fs_info, |
| 2869 | space_info); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 2870 | } |
| 2871 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2875 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2876 | return 0; |
| 2877 | } |
| 2878 | |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2879 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2880 | { |
Nikolay Borisov | 5ead2dd | 2018-03-15 16:00:26 +0200 | [diff] [blame] | 2881 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2882 | struct btrfs_block_group_cache *block_group, *tmp; |
| 2883 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2884 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2885 | u64 start; |
| 2886 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2887 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2888 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2889 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 2890 | unpin = &fs_info->freed_extents[1]; |
| 2891 | else |
| 2892 | unpin = &fs_info->freed_extents[0]; |
| 2893 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2894 | while (!trans->aborted) { |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2895 | struct extent_state *cached_state = NULL; |
| 2896 | |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2897 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2898 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2899 | EXTENT_DIRTY, &cached_state); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2900 | if (ret) { |
| 2901 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2902 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2903 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2904 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2905 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2906 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2907 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2908 | |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2909 | clear_extent_dirty(unpin, start, end, &cached_state); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2910 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 2911 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 0e6ec38 | 2018-11-16 13:04:44 +0000 | [diff] [blame] | 2912 | free_extent_state(cached_state); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2913 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2914 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2915 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2916 | /* |
| 2917 | * Transaction is finished. We don't need the lock anymore. We |
| 2918 | * do need to clean up the block groups in case of a transaction |
| 2919 | * abort. |
| 2920 | */ |
| 2921 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 2922 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 2923 | u64 trimmed = 0; |
| 2924 | |
| 2925 | ret = -EROFS; |
| 2926 | if (!trans->aborted) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2927 | ret = btrfs_discard_extent(fs_info, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2928 | block_group->key.objectid, |
| 2929 | block_group->key.offset, |
| 2930 | &trimmed); |
| 2931 | |
| 2932 | list_del_init(&block_group->bg_list); |
| 2933 | btrfs_put_block_group_trimming(block_group); |
| 2934 | btrfs_put_block_group(block_group); |
| 2935 | |
| 2936 | if (ret) { |
| 2937 | const char *errstr = btrfs_decode_error(ret); |
| 2938 | btrfs_warn(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 2939 | "discard failed while removing blockgroup: errno=%d %s", |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 2940 | ret, errstr); |
| 2941 | } |
| 2942 | } |
| 2943 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2944 | return 0; |
| 2945 | } |
| 2946 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2947 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2948 | struct btrfs_delayed_ref_node *node, u64 parent, |
| 2949 | u64 root_objectid, u64 owner_objectid, |
| 2950 | u64 owner_offset, int refs_to_drop, |
| 2951 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2952 | { |
Nikolay Borisov | e72cb92 | 2018-06-20 15:48:57 +0300 | [diff] [blame] | 2953 | struct btrfs_fs_info *info = trans->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2954 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2955 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2956 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2957 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2958 | struct btrfs_extent_item *ei; |
| 2959 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2960 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2961 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2962 | int extent_slot = 0; |
| 2963 | int found_extent = 0; |
| 2964 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2965 | u32 item_size; |
| 2966 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2967 | u64 bytenr = node->bytenr; |
| 2968 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2969 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2970 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2971 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2972 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2973 | if (!path) |
| 2974 | return -ENOMEM; |
| 2975 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2976 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2977 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2978 | |
| 2979 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 2980 | BUG_ON(!is_data && refs_to_drop != 1); |
| 2981 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2982 | if (is_data) |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 2983 | skinny_metadata = false; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2984 | |
Nikolay Borisov | fbe4801 | 2018-06-20 15:48:52 +0300 | [diff] [blame] | 2985 | ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes, |
| 2986 | parent, root_objectid, owner_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2987 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2988 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2989 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2990 | while (extent_slot >= 0) { |
| 2991 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2992 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2993 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2994 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2995 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2996 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2997 | found_extent = 1; |
| 2998 | break; |
| 2999 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3000 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 3001 | key.offset == owner_objectid) { |
| 3002 | found_extent = 1; |
| 3003 | break; |
| 3004 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3005 | if (path->slots[0] - extent_slot > 5) |
| 3006 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3007 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3008 | } |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 3009 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3010 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3011 | BUG_ON(iref); |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3012 | ret = remove_extent_backref(trans, path, NULL, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 3013 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3014 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3015 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3016 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3017 | goto out; |
| 3018 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3019 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3020 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3021 | |
| 3022 | key.objectid = bytenr; |
| 3023 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3024 | key.offset = num_bytes; |
| 3025 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3026 | if (!is_data && skinny_metadata) { |
| 3027 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3028 | key.offset = owner_objectid; |
| 3029 | } |
| 3030 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3031 | ret = btrfs_search_slot(trans, extent_root, |
| 3032 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3033 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 3034 | /* |
| 3035 | * Couldn't find our skinny metadata item, |
| 3036 | * see if we have ye olde extent item. |
| 3037 | */ |
| 3038 | path->slots[0]--; |
| 3039 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 3040 | path->slots[0]); |
| 3041 | if (key.objectid == bytenr && |
| 3042 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3043 | key.offset == num_bytes) |
| 3044 | ret = 0; |
| 3045 | } |
| 3046 | |
| 3047 | if (ret > 0 && skinny_metadata) { |
| 3048 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 3049 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3050 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3051 | key.offset = num_bytes; |
| 3052 | btrfs_release_path(path); |
| 3053 | ret = btrfs_search_slot(trans, extent_root, |
| 3054 | &key, path, -1, 1); |
| 3055 | } |
| 3056 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3057 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3058 | btrfs_err(info, |
| 3059 | "umm, got %d back from search, was looking for %llu", |
| 3060 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 3061 | if (ret > 0) |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3062 | btrfs_print_leaf(path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3063 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3064 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3065 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3066 | goto out; |
| 3067 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3068 | extent_slot = path->slots[0]; |
| 3069 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3070 | } else if (WARN_ON(ret == -ENOENT)) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 3071 | btrfs_print_leaf(path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3072 | btrfs_err(info, |
| 3073 | "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] | 3074 | bytenr, parent, root_objectid, owner_objectid, |
| 3075 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3076 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 3077 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3078 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3079 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3080 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3081 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3082 | |
| 3083 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3084 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
David Sterba | 6d8ff4e | 2018-06-26 16:20:59 +0200 | [diff] [blame] | 3085 | if (unlikely(item_size < sizeof(*ei))) { |
Nikolay Borisov | ba3c2b1 | 2018-06-26 16:57:36 +0300 | [diff] [blame] | 3086 | ret = -EINVAL; |
| 3087 | btrfs_print_v0_err(info); |
| 3088 | btrfs_abort_transaction(trans, ret); |
| 3089 | goto out; |
| 3090 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3091 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3092 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3093 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3094 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3095 | struct btrfs_tree_block_info *bi; |
| 3096 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3097 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3098 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3099 | } |
| 3100 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3101 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3102 | if (refs < refs_to_drop) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3103 | btrfs_err(info, |
| 3104 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", |
| 3105 | refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3106 | ret = -EINVAL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3107 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 3108 | goto out; |
| 3109 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3110 | refs -= refs_to_drop; |
| 3111 | |
| 3112 | if (refs > 0) { |
| 3113 | if (extent_op) |
| 3114 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3115 | /* |
| 3116 | * In the case of inline back ref, reference count will |
| 3117 | * be updated by remove_extent_backref |
| 3118 | */ |
| 3119 | if (iref) { |
| 3120 | BUG_ON(!found_extent); |
| 3121 | } else { |
| 3122 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3123 | btrfs_mark_buffer_dirty(leaf); |
| 3124 | } |
| 3125 | if (found_extent) { |
Nikolay Borisov | 87cc7a8 | 2018-06-20 15:49:12 +0300 | [diff] [blame] | 3126 | ret = remove_extent_backref(trans, path, iref, |
| 3127 | refs_to_drop, is_data, |
| 3128 | &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3129 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3130 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3131 | goto out; |
| 3132 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3133 | } |
| 3134 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3135 | if (found_extent) { |
| 3136 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 3137 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3138 | if (iref) { |
| 3139 | BUG_ON(path->slots[0] != extent_slot); |
| 3140 | } else { |
| 3141 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3142 | path->slots[0] = extent_slot; |
| 3143 | num_to_del = 2; |
| 3144 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3145 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3146 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3147 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3148 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3149 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3150 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3151 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3152 | goto out; |
| 3153 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3154 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3155 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3156 | if (is_data) { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3157 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3158 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3159 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3160 | goto out; |
| 3161 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3162 | } |
| 3163 | |
Nikolay Borisov | e7355e5 | 2018-05-10 15:44:55 +0300 | [diff] [blame] | 3164 | ret = add_to_free_space_tree(trans, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3165 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3166 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 3167 | goto out; |
| 3168 | } |
| 3169 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 3170 | ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3171 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3172 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 3173 | goto out; |
| 3174 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3175 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3176 | btrfs_release_path(path); |
| 3177 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3178 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3179 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3180 | return ret; |
| 3181 | } |
| 3182 | |
| 3183 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3184 | * 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] | 3185 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3186 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3187 | * removes it from the tree. |
| 3188 | */ |
| 3189 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3190 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3191 | { |
| 3192 | struct btrfs_delayed_ref_head *head; |
| 3193 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3194 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3195 | |
| 3196 | delayed_refs = &trans->transaction->delayed_refs; |
| 3197 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 3198 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3199 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3200 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3201 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3202 | spin_lock(&head->lock); |
Liu Bo | e3d0396 | 2018-08-23 03:51:50 +0800 | [diff] [blame] | 3203 | if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3204 | goto out; |
| 3205 | |
Josef Bacik | bedc6617 | 2018-12-03 10:20:31 -0500 | [diff] [blame] | 3206 | if (cleanup_extent_op(head) != NULL) |
| 3207 | goto out; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3208 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3209 | /* |
| 3210 | * waiting for the lock here would deadlock. If someone else has it |
| 3211 | * locked they are already in the process of dropping it anyway |
| 3212 | */ |
| 3213 | if (!mutex_trylock(&head->mutex)) |
| 3214 | goto out; |
| 3215 | |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3216 | btrfs_delete_ref_head(delayed_refs, head); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3217 | head->processing = 0; |
Josef Bacik | d7baffd | 2018-12-03 10:20:29 -0500 | [diff] [blame] | 3218 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3219 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3220 | spin_unlock(&delayed_refs->lock); |
| 3221 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3222 | BUG_ON(head->extent_op); |
| 3223 | if (head->must_insert_reserved) |
| 3224 | ret = 1; |
| 3225 | |
Josef Bacik | 31890da | 2018-11-21 14:05:41 -0500 | [diff] [blame] | 3226 | btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3227 | mutex_unlock(&head->mutex); |
Josef Bacik | d278850 | 2017-09-29 15:43:57 -0400 | [diff] [blame] | 3228 | btrfs_put_delayed_ref_head(head); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3229 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3230 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3231 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 3232 | |
| 3233 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3234 | spin_unlock(&delayed_refs->lock); |
| 3235 | return 0; |
| 3236 | } |
| 3237 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3238 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 3239 | struct btrfs_root *root, |
| 3240 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 3241 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3242 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3243 | struct btrfs_fs_info *fs_info = root->fs_info; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3244 | struct btrfs_ref generic_ref = { 0 }; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3245 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3246 | int ret; |
| 3247 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3248 | btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, |
| 3249 | buf->start, buf->len, parent); |
| 3250 | btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf), |
| 3251 | root->root_key.objectid); |
| 3252 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3253 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3254 | int old_ref_mod, new_ref_mod; |
| 3255 | |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3256 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 3257 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3258 | &old_ref_mod, &new_ref_mod); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3259 | BUG_ON(ret); /* -ENOMEM */ |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3260 | pin = old_ref_mod >= 0 && new_ref_mod < 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3261 | } |
| 3262 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3263 | if (last_ref && btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3264 | struct btrfs_block_group_cache *cache; |
| 3265 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3266 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3267 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3268 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3269 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3270 | } |
| 3271 | |
Omar Sandoval | 4da8b76 | 2017-06-06 16:45:28 -0700 | [diff] [blame] | 3272 | pin = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3273 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3274 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3275 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 3276 | pin_down_extent(cache, buf->start, buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3277 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 3278 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 3282 | |
| 3283 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 3284 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 3285 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 3286 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3287 | } |
| 3288 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3289 | if (pin) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 3290 | add_pinned_bytes(fs_info, &generic_ref); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3291 | |
Omar Sandoval | 0a16c7d | 2017-06-06 16:45:29 -0700 | [diff] [blame] | 3292 | if (last_ref) { |
| 3293 | /* |
| 3294 | * Deleting the buffer, clear the corrupt flag since it doesn't |
| 3295 | * matter anymore. |
| 3296 | */ |
| 3297 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
| 3298 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3299 | } |
| 3300 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3301 | /* Can return -ENOMEM */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3302 | 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] | 3303 | { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3304 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3305 | int old_ref_mod, new_ref_mod; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3306 | int ret; |
| 3307 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 3308 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3309 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3310 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3311 | /* |
| 3312 | * tree log blocks never actually go into the extent allocation |
| 3313 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3314 | */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3315 | if ((ref->type == BTRFS_REF_METADATA && |
| 3316 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3317 | (ref->type == BTRFS_REF_DATA && |
| 3318 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3319 | /* unlocks the pinned mutex */ |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3320 | btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3321 | old_ref_mod = new_ref_mod = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3322 | ret = 0; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3323 | } else if (ref->type == BTRFS_REF_METADATA) { |
| 3324 | ret = btrfs_add_delayed_tree_ref(trans, ref, NULL, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3325 | &old_ref_mod, &new_ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3326 | } else { |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3327 | ret = btrfs_add_delayed_data_ref(trans, ref, 0, |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3328 | &old_ref_mod, &new_ref_mod); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3329 | } |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3330 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 3331 | if (!((ref->type == BTRFS_REF_METADATA && |
| 3332 | ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) || |
| 3333 | (ref->type == BTRFS_REF_DATA && |
| 3334 | ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID))) |
| 3335 | btrfs_ref_tree_mod(fs_info, ref); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 3336 | |
Qu Wenruo | ddf30cf | 2019-04-04 14:45:34 +0800 | [diff] [blame] | 3337 | if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) |
Qu Wenruo | 7819244 | 2019-05-15 07:33:48 +0800 | [diff] [blame] | 3338 | add_pinned_bytes(fs_info, ref); |
Omar Sandoval | d7eae34 | 2017-06-06 16:45:31 -0700 | [diff] [blame] | 3339 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3340 | return ret; |
| 3341 | } |
| 3342 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3343 | enum btrfs_loop_type { |
David Sterba | f262fa8 | 2019-06-18 20:00:08 +0200 | [diff] [blame] | 3344 | LOOP_CACHING_NOWAIT, |
| 3345 | LOOP_CACHING_WAIT, |
| 3346 | LOOP_ALLOC_CHUNK, |
| 3347 | LOOP_NO_EMPTY_SIZE, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3348 | }; |
| 3349 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3350 | static inline void |
| 3351 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 3352 | int delalloc) |
| 3353 | { |
| 3354 | if (delalloc) |
| 3355 | down_read(&cache->data_rwsem); |
| 3356 | } |
| 3357 | |
| 3358 | static inline void |
| 3359 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 3360 | int delalloc) |
| 3361 | { |
| 3362 | btrfs_get_block_group(cache); |
| 3363 | if (delalloc) |
| 3364 | down_read(&cache->data_rwsem); |
| 3365 | } |
| 3366 | |
| 3367 | static struct btrfs_block_group_cache * |
| 3368 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 3369 | struct btrfs_free_cluster *cluster, |
| 3370 | int delalloc) |
| 3371 | { |
Sudip Mukherjee | 89771cc | 2016-02-16 13:32:47 +0530 | [diff] [blame] | 3372 | struct btrfs_block_group_cache *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3373 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3374 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3375 | while (1) { |
| 3376 | used_bg = cluster->block_group; |
| 3377 | if (!used_bg) |
| 3378 | return NULL; |
| 3379 | |
| 3380 | if (used_bg == block_group) |
| 3381 | return used_bg; |
| 3382 | |
| 3383 | btrfs_get_block_group(used_bg); |
| 3384 | |
| 3385 | if (!delalloc) |
| 3386 | return used_bg; |
| 3387 | |
| 3388 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 3389 | return used_bg; |
| 3390 | |
| 3391 | spin_unlock(&cluster->refill_lock); |
| 3392 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 3393 | /* We should only have one-level nested. */ |
| 3394 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 3395 | |
| 3396 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3397 | if (used_bg == cluster->block_group) |
| 3398 | return used_bg; |
| 3399 | |
| 3400 | up_read(&used_bg->data_rwsem); |
| 3401 | btrfs_put_block_group(used_bg); |
| 3402 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | static inline void |
| 3406 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 3407 | int delalloc) |
| 3408 | { |
| 3409 | if (delalloc) |
| 3410 | up_read(&cache->data_rwsem); |
| 3411 | btrfs_put_block_group(cache); |
| 3412 | } |
| 3413 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3414 | /* |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3415 | * Structure used internally for find_free_extent() function. Wraps needed |
| 3416 | * parameters. |
| 3417 | */ |
| 3418 | struct find_free_extent_ctl { |
| 3419 | /* Basic allocation info */ |
| 3420 | u64 ram_bytes; |
| 3421 | u64 num_bytes; |
| 3422 | u64 empty_size; |
| 3423 | u64 flags; |
| 3424 | int delalloc; |
| 3425 | |
| 3426 | /* Where to start the search inside the bg */ |
| 3427 | u64 search_start; |
| 3428 | |
| 3429 | /* For clustered allocation */ |
| 3430 | u64 empty_cluster; |
| 3431 | |
| 3432 | bool have_caching_bg; |
| 3433 | bool orig_have_caching_bg; |
| 3434 | |
| 3435 | /* RAID index, converted from flags */ |
| 3436 | int index; |
| 3437 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3438 | /* |
| 3439 | * Current loop number, check find_free_extent_update_loop() for details |
| 3440 | */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3441 | int loop; |
| 3442 | |
| 3443 | /* |
| 3444 | * Whether we're refilling a cluster, if true we need to re-search |
| 3445 | * current block group but don't try to refill the cluster again. |
| 3446 | */ |
| 3447 | bool retry_clustered; |
| 3448 | |
| 3449 | /* |
| 3450 | * Whether we're updating free space cache, if true we need to re-search |
| 3451 | * current block group but don't try updating free space cache again. |
| 3452 | */ |
| 3453 | bool retry_unclustered; |
| 3454 | |
| 3455 | /* If current block group is cached */ |
| 3456 | int cached; |
| 3457 | |
| 3458 | /* Max contiguous hole found */ |
| 3459 | u64 max_extent_size; |
| 3460 | |
| 3461 | /* Total free space from free space cache, not always contiguous */ |
| 3462 | u64 total_free_space; |
| 3463 | |
| 3464 | /* Found result */ |
| 3465 | u64 found_offset; |
| 3466 | }; |
| 3467 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3468 | |
| 3469 | /* |
| 3470 | * Helper function for find_free_extent(). |
| 3471 | * |
| 3472 | * Return -ENOENT to inform caller that we need fallback to unclustered mode. |
| 3473 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3474 | * Return >0 to inform caller that we find nothing |
| 3475 | * Return 0 means we have found a location and set ffe_ctl->found_offset. |
| 3476 | */ |
| 3477 | static int find_free_extent_clustered(struct btrfs_block_group_cache *bg, |
| 3478 | struct btrfs_free_cluster *last_ptr, |
| 3479 | struct find_free_extent_ctl *ffe_ctl, |
| 3480 | struct btrfs_block_group_cache **cluster_bg_ret) |
| 3481 | { |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3482 | struct btrfs_block_group_cache *cluster_bg; |
| 3483 | u64 aligned_cluster; |
| 3484 | u64 offset; |
| 3485 | int ret; |
| 3486 | |
| 3487 | cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc); |
| 3488 | if (!cluster_bg) |
| 3489 | goto refill_cluster; |
| 3490 | if (cluster_bg != bg && (cluster_bg->ro || |
| 3491 | !block_group_bits(cluster_bg, ffe_ctl->flags))) |
| 3492 | goto release_cluster; |
| 3493 | |
| 3494 | offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr, |
| 3495 | ffe_ctl->num_bytes, cluster_bg->key.objectid, |
| 3496 | &ffe_ctl->max_extent_size); |
| 3497 | if (offset) { |
| 3498 | /* We have a block, we're done */ |
| 3499 | spin_unlock(&last_ptr->refill_lock); |
| 3500 | trace_btrfs_reserve_extent_cluster(cluster_bg, |
| 3501 | ffe_ctl->search_start, ffe_ctl->num_bytes); |
| 3502 | *cluster_bg_ret = cluster_bg; |
| 3503 | ffe_ctl->found_offset = offset; |
| 3504 | return 0; |
| 3505 | } |
| 3506 | WARN_ON(last_ptr->block_group != cluster_bg); |
| 3507 | |
| 3508 | release_cluster: |
| 3509 | /* |
| 3510 | * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so |
| 3511 | * lets just skip it and let the allocator find whatever block it can |
| 3512 | * find. If we reach this point, we will have tried the cluster |
| 3513 | * allocator plenty of times and not have found anything, so we are |
| 3514 | * likely way too fragmented for the clustering stuff to find anything. |
| 3515 | * |
| 3516 | * However, if the cluster is taken from the current block group, |
| 3517 | * release the cluster first, so that we stand a better chance of |
| 3518 | * succeeding in the unclustered allocation. |
| 3519 | */ |
| 3520 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) { |
| 3521 | spin_unlock(&last_ptr->refill_lock); |
| 3522 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3523 | return -ENOENT; |
| 3524 | } |
| 3525 | |
| 3526 | /* This cluster didn't work out, free it and start over */ |
| 3527 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3528 | |
| 3529 | if (cluster_bg != bg) |
| 3530 | btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc); |
| 3531 | |
| 3532 | refill_cluster: |
| 3533 | if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) { |
| 3534 | spin_unlock(&last_ptr->refill_lock); |
| 3535 | return -ENOENT; |
| 3536 | } |
| 3537 | |
| 3538 | aligned_cluster = max_t(u64, |
| 3539 | ffe_ctl->empty_cluster + ffe_ctl->empty_size, |
| 3540 | bg->full_stripe_len); |
David Sterba | 2ceeae2 | 2019-03-20 13:53:49 +0100 | [diff] [blame] | 3541 | ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start, |
| 3542 | ffe_ctl->num_bytes, aligned_cluster); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3543 | if (ret == 0) { |
| 3544 | /* Now pull our allocation out of this cluster */ |
| 3545 | offset = btrfs_alloc_from_cluster(bg, last_ptr, |
| 3546 | ffe_ctl->num_bytes, ffe_ctl->search_start, |
| 3547 | &ffe_ctl->max_extent_size); |
| 3548 | if (offset) { |
| 3549 | /* We found one, proceed */ |
| 3550 | spin_unlock(&last_ptr->refill_lock); |
| 3551 | trace_btrfs_reserve_extent_cluster(bg, |
| 3552 | ffe_ctl->search_start, |
| 3553 | ffe_ctl->num_bytes); |
| 3554 | ffe_ctl->found_offset = offset; |
| 3555 | return 0; |
| 3556 | } |
| 3557 | } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT && |
| 3558 | !ffe_ctl->retry_clustered) { |
| 3559 | spin_unlock(&last_ptr->refill_lock); |
| 3560 | |
| 3561 | ffe_ctl->retry_clustered = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3562 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3563 | ffe_ctl->empty_cluster + ffe_ctl->empty_size); |
| 3564 | return -EAGAIN; |
| 3565 | } |
| 3566 | /* |
| 3567 | * At this point we either didn't find a cluster or we weren't able to |
| 3568 | * allocate a block from our cluster. Free the cluster we've been |
| 3569 | * trying to use, and go to the next block group. |
| 3570 | */ |
| 3571 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3572 | spin_unlock(&last_ptr->refill_lock); |
| 3573 | return 1; |
| 3574 | } |
| 3575 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3576 | /* |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3577 | * Return >0 to inform caller that we find nothing |
| 3578 | * Return 0 when we found an free extent and set ffe_ctrl->found_offset |
| 3579 | * Return -EAGAIN to inform caller that we need to re-search this block group |
| 3580 | */ |
| 3581 | static int find_free_extent_unclustered(struct btrfs_block_group_cache *bg, |
| 3582 | struct btrfs_free_cluster *last_ptr, |
| 3583 | struct find_free_extent_ctl *ffe_ctl) |
| 3584 | { |
| 3585 | u64 offset; |
| 3586 | |
| 3587 | /* |
| 3588 | * We are doing an unclustered allocation, set the fragmented flag so |
| 3589 | * we don't bother trying to setup a cluster again until we get more |
| 3590 | * space. |
| 3591 | */ |
| 3592 | if (unlikely(last_ptr)) { |
| 3593 | spin_lock(&last_ptr->lock); |
| 3594 | last_ptr->fragmented = 1; |
| 3595 | spin_unlock(&last_ptr->lock); |
| 3596 | } |
| 3597 | if (ffe_ctl->cached) { |
| 3598 | struct btrfs_free_space_ctl *free_space_ctl; |
| 3599 | |
| 3600 | free_space_ctl = bg->free_space_ctl; |
| 3601 | spin_lock(&free_space_ctl->tree_lock); |
| 3602 | if (free_space_ctl->free_space < |
| 3603 | ffe_ctl->num_bytes + ffe_ctl->empty_cluster + |
| 3604 | ffe_ctl->empty_size) { |
| 3605 | ffe_ctl->total_free_space = max_t(u64, |
| 3606 | ffe_ctl->total_free_space, |
| 3607 | free_space_ctl->free_space); |
| 3608 | spin_unlock(&free_space_ctl->tree_lock); |
| 3609 | return 1; |
| 3610 | } |
| 3611 | spin_unlock(&free_space_ctl->tree_lock); |
| 3612 | } |
| 3613 | |
| 3614 | offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start, |
| 3615 | ffe_ctl->num_bytes, ffe_ctl->empty_size, |
| 3616 | &ffe_ctl->max_extent_size); |
| 3617 | |
| 3618 | /* |
| 3619 | * If we didn't find a chunk, and we haven't failed on this block group |
| 3620 | * before, and this block group is in the middle of caching and we are |
| 3621 | * ok with waiting, then go ahead and wait for progress to be made, and |
| 3622 | * set @retry_unclustered to true. |
| 3623 | * |
| 3624 | * If @retry_unclustered is true then we've already waited on this |
| 3625 | * block group once and should move on to the next block group. |
| 3626 | */ |
| 3627 | if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached && |
| 3628 | ffe_ctl->loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3629 | btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes + |
| 3630 | ffe_ctl->empty_size); |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3631 | ffe_ctl->retry_unclustered = true; |
| 3632 | return -EAGAIN; |
| 3633 | } else if (!offset) { |
| 3634 | return 1; |
| 3635 | } |
| 3636 | ffe_ctl->found_offset = offset; |
| 3637 | return 0; |
| 3638 | } |
| 3639 | |
| 3640 | /* |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3641 | * Return >0 means caller needs to re-search for free extent |
| 3642 | * Return 0 means we have the needed free extent. |
| 3643 | * Return <0 means we failed to locate any free extent. |
| 3644 | */ |
| 3645 | static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info, |
| 3646 | struct btrfs_free_cluster *last_ptr, |
| 3647 | struct btrfs_key *ins, |
| 3648 | struct find_free_extent_ctl *ffe_ctl, |
| 3649 | int full_search, bool use_cluster) |
| 3650 | { |
| 3651 | struct btrfs_root *root = fs_info->extent_root; |
| 3652 | int ret; |
| 3653 | |
| 3654 | if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) && |
| 3655 | ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg) |
| 3656 | ffe_ctl->orig_have_caching_bg = true; |
| 3657 | |
| 3658 | if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT && |
| 3659 | ffe_ctl->have_caching_bg) |
| 3660 | return 1; |
| 3661 | |
| 3662 | if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES) |
| 3663 | return 1; |
| 3664 | |
| 3665 | if (ins->objectid) { |
| 3666 | if (!use_cluster && last_ptr) { |
| 3667 | spin_lock(&last_ptr->lock); |
| 3668 | last_ptr->window_start = ins->objectid; |
| 3669 | spin_unlock(&last_ptr->lock); |
| 3670 | } |
| 3671 | return 0; |
| 3672 | } |
| 3673 | |
| 3674 | /* |
| 3675 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 3676 | * caching kthreads as we move along |
| 3677 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 3678 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 3679 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 3680 | * again |
| 3681 | */ |
| 3682 | if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) { |
| 3683 | ffe_ctl->index = 0; |
| 3684 | if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) { |
| 3685 | /* |
| 3686 | * We want to skip the LOOP_CACHING_WAIT step if we |
| 3687 | * don't have any uncached bgs and we've already done a |
| 3688 | * full search through. |
| 3689 | */ |
| 3690 | if (ffe_ctl->orig_have_caching_bg || !full_search) |
| 3691 | ffe_ctl->loop = LOOP_CACHING_WAIT; |
| 3692 | else |
| 3693 | ffe_ctl->loop = LOOP_ALLOC_CHUNK; |
| 3694 | } else { |
| 3695 | ffe_ctl->loop++; |
| 3696 | } |
| 3697 | |
| 3698 | if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) { |
| 3699 | struct btrfs_trans_handle *trans; |
| 3700 | int exist = 0; |
| 3701 | |
| 3702 | trans = current->journal_info; |
| 3703 | if (trans) |
| 3704 | exist = 1; |
| 3705 | else |
| 3706 | trans = btrfs_join_transaction(root); |
| 3707 | |
| 3708 | if (IS_ERR(trans)) { |
| 3709 | ret = PTR_ERR(trans); |
| 3710 | return ret; |
| 3711 | } |
| 3712 | |
Josef Bacik | fc471cb | 2019-06-18 16:09:17 -0400 | [diff] [blame] | 3713 | ret = btrfs_chunk_alloc(trans, ffe_ctl->flags, |
| 3714 | CHUNK_ALLOC_FORCE); |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3715 | |
| 3716 | /* |
| 3717 | * If we can't allocate a new chunk we've already looped |
| 3718 | * through at least once, move on to the NO_EMPTY_SIZE |
| 3719 | * case. |
| 3720 | */ |
| 3721 | if (ret == -ENOSPC) |
| 3722 | ffe_ctl->loop = LOOP_NO_EMPTY_SIZE; |
| 3723 | |
| 3724 | /* Do not bail out on ENOSPC since we can do more. */ |
| 3725 | if (ret < 0 && ret != -ENOSPC) |
| 3726 | btrfs_abort_transaction(trans, ret); |
| 3727 | else |
| 3728 | ret = 0; |
| 3729 | if (!exist) |
| 3730 | btrfs_end_transaction(trans); |
| 3731 | if (ret) |
| 3732 | return ret; |
| 3733 | } |
| 3734 | |
| 3735 | if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) { |
| 3736 | /* |
| 3737 | * Don't loop again if we already have no empty_size and |
| 3738 | * no empty_cluster. |
| 3739 | */ |
| 3740 | if (ffe_ctl->empty_size == 0 && |
| 3741 | ffe_ctl->empty_cluster == 0) |
| 3742 | return -ENOSPC; |
| 3743 | ffe_ctl->empty_size = 0; |
| 3744 | ffe_ctl->empty_cluster = 0; |
| 3745 | } |
| 3746 | return 1; |
| 3747 | } |
| 3748 | return -ENOSPC; |
| 3749 | } |
| 3750 | |
| 3751 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3752 | * walks the btree of allocated extents and find a hole of a given size. |
| 3753 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3754 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3755 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3756 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3757 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 3758 | * |
| 3759 | * If there is no suitable free space, we will record the max size of |
| 3760 | * the free space extent currently. |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 3761 | * |
| 3762 | * The overall logic and call chain: |
| 3763 | * |
| 3764 | * find_free_extent() |
| 3765 | * |- Iterate through all block groups |
| 3766 | * | |- Get a valid block group |
| 3767 | * | |- Try to do clustered allocation in that block group |
| 3768 | * | |- Try to do unclustered allocation in that block group |
| 3769 | * | |- Check if the result is valid |
| 3770 | * | | |- If valid, then exit |
| 3771 | * | |- Jump to next block group |
| 3772 | * | |
| 3773 | * |- Push harder to find free extents |
| 3774 | * |- If not found, re-iterate all block groups |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3775 | */ |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 3776 | static noinline int find_free_extent(struct btrfs_fs_info *fs_info, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 3777 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
| 3778 | u64 hint_byte, struct btrfs_key *ins, |
| 3779 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3780 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3781 | int ret = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3782 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3783 | struct btrfs_block_group_cache *block_group = NULL; |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3784 | struct find_free_extent_ctl ffe_ctl = {0}; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3785 | struct btrfs_space_info *space_info; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3786 | bool use_cluster = true; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 3787 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3788 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3789 | WARN_ON(num_bytes < fs_info->sectorsize); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3790 | |
| 3791 | ffe_ctl.ram_bytes = ram_bytes; |
| 3792 | ffe_ctl.num_bytes = num_bytes; |
| 3793 | ffe_ctl.empty_size = empty_size; |
| 3794 | ffe_ctl.flags = flags; |
| 3795 | ffe_ctl.search_start = 0; |
| 3796 | ffe_ctl.retry_clustered = false; |
| 3797 | ffe_ctl.retry_unclustered = false; |
| 3798 | ffe_ctl.delalloc = delalloc; |
| 3799 | ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags); |
| 3800 | ffe_ctl.have_caching_bg = false; |
| 3801 | ffe_ctl.orig_have_caching_bg = false; |
| 3802 | ffe_ctl.found_offset = 0; |
| 3803 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3804 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3805 | ins->objectid = 0; |
| 3806 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3807 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 3808 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 3809 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 3810 | space_info = btrfs_find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 3811 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3812 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 3813 | return -ENOSPC; |
| 3814 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3815 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3816 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3817 | * If our free space is heavily fragmented we may not be able to make |
| 3818 | * big contiguous allocations, so instead of doing the expensive search |
| 3819 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 3820 | * can go ahead and search for a more manageable chunk. |
| 3821 | * |
| 3822 | * If our max_extent_size is large enough for our allocation simply |
| 3823 | * disable clustering since we will likely not be able to find enough |
| 3824 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3825 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3826 | if (unlikely(space_info->max_extent_size)) { |
| 3827 | spin_lock(&space_info->lock); |
| 3828 | if (space_info->max_extent_size && |
| 3829 | num_bytes > space_info->max_extent_size) { |
| 3830 | ins->offset = space_info->max_extent_size; |
| 3831 | spin_unlock(&space_info->lock); |
| 3832 | return -ENOSPC; |
| 3833 | } else if (space_info->max_extent_size) { |
| 3834 | use_cluster = false; |
| 3835 | } |
| 3836 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3837 | } |
| 3838 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3839 | last_ptr = fetch_cluster_info(fs_info, space_info, |
| 3840 | &ffe_ctl.empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3841 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3842 | spin_lock(&last_ptr->lock); |
| 3843 | if (last_ptr->block_group) |
| 3844 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3845 | if (last_ptr->fragmented) { |
| 3846 | /* |
| 3847 | * We still set window_start so we can keep track of the |
| 3848 | * last place we found an allocation to try and save |
| 3849 | * some time. |
| 3850 | */ |
| 3851 | hint_byte = last_ptr->window_start; |
| 3852 | use_cluster = false; |
| 3853 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3854 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3855 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3856 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3857 | ffe_ctl.search_start = max(ffe_ctl.search_start, |
| 3858 | first_logical_byte(fs_info, 0)); |
| 3859 | ffe_ctl.search_start = max(ffe_ctl.search_start, hint_byte); |
| 3860 | if (ffe_ctl.search_start == hint_byte) { |
| 3861 | block_group = btrfs_lookup_block_group(fs_info, |
| 3862 | ffe_ctl.search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3863 | /* |
| 3864 | * we don't want to use the block group if it doesn't match our |
| 3865 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3866 | * |
| 3867 | * However if we are re-searching with an ideal block group |
| 3868 | * 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] | 3869 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 3870 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 3871 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3872 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3873 | if (list_empty(&block_group->list) || |
| 3874 | block_group->ro) { |
| 3875 | /* |
| 3876 | * someone is removing this block group, |
| 3877 | * we can't jump into the have_block_group |
| 3878 | * target because our list pointers are not |
| 3879 | * valid |
| 3880 | */ |
| 3881 | btrfs_put_block_group(block_group); |
| 3882 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3883 | } else { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3884 | ffe_ctl.index = btrfs_bg_flags_to_raid_index( |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 3885 | block_group->flags); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3886 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3887 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 3888 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3889 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3890 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3891 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3892 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3893 | search: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3894 | ffe_ctl.have_caching_bg = false; |
| 3895 | if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) || |
| 3896 | ffe_ctl.index == 0) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 3897 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3898 | down_read(&space_info->groups_sem); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3899 | list_for_each_entry(block_group, |
| 3900 | &space_info->block_groups[ffe_ctl.index], list) { |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 3901 | /* If the block group is read-only, we can skip it entirely. */ |
| 3902 | if (unlikely(block_group->ro)) |
| 3903 | continue; |
| 3904 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3905 | btrfs_grab_block_group(block_group, delalloc); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3906 | ffe_ctl.search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3907 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3908 | /* |
| 3909 | * this can happen if we end up cycling through all the |
| 3910 | * raid types, but we want to make sure we only allocate |
| 3911 | * for the proper type. |
| 3912 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 3913 | if (!block_group_bits(block_group, flags)) { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 3914 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 3915 | BTRFS_BLOCK_GROUP_RAID1_MASK | |
David Sterba | a07e8a4 | 2019-05-31 16:54:26 +0200 | [diff] [blame] | 3916 | BTRFS_BLOCK_GROUP_RAID56_MASK | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3917 | BTRFS_BLOCK_GROUP_RAID10; |
| 3918 | |
| 3919 | /* |
| 3920 | * if they asked for extra copies and this block group |
| 3921 | * doesn't provide them, bail. This does allow us to |
| 3922 | * fill raid0 from raid1. |
| 3923 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 3924 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3925 | goto loop; |
Qu Wenruo | 2a28468 | 2019-07-16 17:00:33 +0800 | [diff] [blame] | 3926 | |
| 3927 | /* |
| 3928 | * This block group has different flags than we want. |
| 3929 | * It's possible that we have MIXED_GROUP flag but no |
| 3930 | * block group is mixed. Just skip such block group. |
| 3931 | */ |
| 3932 | btrfs_release_block_group(block_group, delalloc); |
| 3933 | continue; |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 3934 | } |
| 3935 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3936 | have_block_group: |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3937 | ffe_ctl.cached = btrfs_block_group_cache_done(block_group); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3938 | if (unlikely(!ffe_ctl.cached)) { |
| 3939 | ffe_ctl.have_caching_bg = true; |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 3940 | ret = btrfs_cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 3941 | BUG_ON(ret < 0); |
| 3942 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3943 | } |
| 3944 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 3945 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 3946 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3947 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3948 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 3949 | * Ok we want to try and use the cluster allocator, so |
| 3950 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3951 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 3952 | if (last_ptr && use_cluster) { |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3953 | struct btrfs_block_group_cache *cluster_bg = NULL; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3954 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3955 | ret = find_free_extent_clustered(block_group, last_ptr, |
| 3956 | &ffe_ctl, &cluster_bg); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 3957 | |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3958 | if (ret == 0) { |
| 3959 | if (cluster_bg && cluster_bg != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 3960 | btrfs_release_block_group(block_group, |
| 3961 | delalloc); |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3962 | block_group = cluster_bg; |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 3963 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3964 | goto checks; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3965 | } else if (ret == -EAGAIN) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3966 | goto have_block_group; |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3967 | } else if (ret > 0) { |
| 3968 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3969 | } |
Qu Wenruo | d06e3bb | 2018-11-02 09:39:48 +0800 | [diff] [blame] | 3970 | /* ret == -ENOENT case falls through */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3971 | } |
| 3972 | |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3973 | ret = find_free_extent_unclustered(block_group, last_ptr, |
| 3974 | &ffe_ctl); |
| 3975 | if (ret == -EAGAIN) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3976 | goto have_block_group; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3977 | else if (ret > 0) |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 3978 | goto loop; |
Qu Wenruo | e1a4184 | 2018-11-02 09:39:49 +0800 | [diff] [blame] | 3979 | /* ret == 0 case falls through */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3980 | checks: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3981 | ffe_ctl.search_start = round_up(ffe_ctl.found_offset, |
| 3982 | fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3983 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3984 | /* move on to the next group */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3985 | if (ffe_ctl.search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 3986 | block_group->key.objectid + block_group->key.offset) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3987 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 3988 | num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3989 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3990 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3991 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3992 | if (ffe_ctl.found_offset < ffe_ctl.search_start) |
| 3993 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 3994 | ffe_ctl.search_start - ffe_ctl.found_offset); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3995 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 3996 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 3997 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3998 | if (ret == -EAGAIN) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 3999 | btrfs_add_free_space(block_group, ffe_ctl.found_offset, |
| 4000 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4001 | goto loop; |
| 4002 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4003 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4004 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4005 | /* we are all good, lets return */ |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4006 | ins->objectid = ffe_ctl.search_start; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4007 | ins->offset = num_bytes; |
| 4008 | |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4009 | trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start, |
| 4010 | num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4011 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4012 | break; |
| 4013 | loop: |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4014 | ffe_ctl.retry_clustered = false; |
| 4015 | ffe_ctl.retry_unclustered = false; |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4016 | BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) != |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4017 | ffe_ctl.index); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4018 | btrfs_release_block_group(block_group, delalloc); |
Jeff Mahoney | 1444393 | 2017-07-19 23:25:51 -0400 | [diff] [blame] | 4019 | cond_resched(); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4020 | } |
| 4021 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4022 | |
Qu Wenruo | e72d79d | 2018-11-02 09:39:50 +0800 | [diff] [blame] | 4023 | ret = find_free_extent_update_loop(fs_info, last_ptr, ins, &ffe_ctl, |
| 4024 | full_search, use_cluster); |
| 4025 | if (ret > 0) |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 4026 | goto search; |
| 4027 | |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4028 | if (ret == -ENOSPC) { |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4029 | /* |
| 4030 | * Use ffe_ctl->total_free_space as fallback if we can't find |
| 4031 | * any contiguous hole. |
| 4032 | */ |
| 4033 | if (!ffe_ctl.max_extent_size) |
| 4034 | ffe_ctl.max_extent_size = ffe_ctl.total_free_space; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4035 | spin_lock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4036 | space_info->max_extent_size = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4037 | spin_unlock(&space_info->lock); |
Qu Wenruo | b4bd745 | 2018-11-02 09:39:47 +0800 | [diff] [blame] | 4038 | ins->offset = ffe_ctl.max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 4039 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4040 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4041 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4042 | |
Nikolay Borisov | 6f47c70 | 2018-03-13 12:22:32 +0200 | [diff] [blame] | 4043 | /* |
| 4044 | * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a |
| 4045 | * hole that is at least as big as @num_bytes. |
| 4046 | * |
| 4047 | * @root - The root that will contain this extent |
| 4048 | * |
| 4049 | * @ram_bytes - The amount of space in ram that @num_bytes take. This |
| 4050 | * is used for accounting purposes. This value differs |
| 4051 | * from @num_bytes only in the case of compressed extents. |
| 4052 | * |
| 4053 | * @num_bytes - Number of bytes to allocate on-disk. |
| 4054 | * |
| 4055 | * @min_alloc_size - Indicates the minimum amount of space that the |
| 4056 | * allocator should try to satisfy. In some cases |
| 4057 | * @num_bytes may be larger than what is required and if |
| 4058 | * the filesystem is fragmented then allocation fails. |
| 4059 | * However, the presence of @min_alloc_size gives a |
| 4060 | * chance to try and satisfy the smaller allocation. |
| 4061 | * |
| 4062 | * @empty_size - A hint that you plan on doing more COW. This is the |
| 4063 | * size in bytes the allocator should try to find free |
| 4064 | * next to the block it returns. This is just a hint and |
| 4065 | * may be ignored by the allocator. |
| 4066 | * |
| 4067 | * @hint_byte - Hint to the allocator to start searching above the byte |
| 4068 | * address passed. It might be ignored. |
| 4069 | * |
| 4070 | * @ins - This key is modified to record the found hole. It will |
| 4071 | * have the following values: |
| 4072 | * ins->objectid == start position |
| 4073 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
| 4074 | * ins->offset == the size of the hole. |
| 4075 | * |
| 4076 | * @is_data - Boolean flag indicating whether an extent is |
| 4077 | * allocated for data (true) or metadata (false) |
| 4078 | * |
| 4079 | * @delalloc - Boolean flag indicating whether this allocation is for |
| 4080 | * delalloc or not. If 'true' data_rwsem of block groups |
| 4081 | * is going to be acquired. |
| 4082 | * |
| 4083 | * |
| 4084 | * Returns 0 when an allocation succeeded or < 0 when an error occurred. In |
| 4085 | * case -ENOSPC is returned then @ins->offset will contain the size of the |
| 4086 | * largest available hole the allocator managed to find. |
| 4087 | */ |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4088 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4089 | u64 num_bytes, u64 min_alloc_size, |
| 4090 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4091 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4092 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4093 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 4094 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 4095 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4096 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4097 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 4098 | flags = get_alloc_profile_by_root(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4099 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4100 | WARN_ON(num_bytes < fs_info->sectorsize); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 4101 | ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4102 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4103 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4104 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 4105 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 4106 | if (!final_tried && ins->offset) { |
| 4107 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4108 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4109 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4110 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4111 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4112 | if (num_bytes == min_alloc_size) |
| 4113 | final_tried = true; |
| 4114 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 4115 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4116 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4117 | |
Josef Bacik | 280c2908 | 2019-06-18 16:09:19 -0400 | [diff] [blame] | 4118 | sinfo = btrfs_find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4119 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4120 | "allocation failed flags %llu, wanted %llu", |
| 4121 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 4122 | if (sinfo) |
Josef Bacik | 5da6afe | 2019-06-18 16:09:24 -0400 | [diff] [blame] | 4123 | btrfs_dump_space_info(fs_info, sinfo, |
| 4124 | num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4125 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4126 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4127 | |
| 4128 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4129 | } |
| 4130 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4131 | static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4132 | u64 start, u64 len, |
| 4133 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4134 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4135 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4136 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4137 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4138 | cache = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4139 | if (!cache) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4140 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 4141 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4142 | return -ENOSPC; |
| 4143 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4144 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4145 | if (pin) |
David Sterba | fdf0860 | 2019-03-20 12:12:32 +0100 | [diff] [blame] | 4146 | pin_down_extent(cache, start, len, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4147 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4148 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4149 | ret = btrfs_discard_extent(fs_info, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4150 | btrfs_add_free_space(cache, start, len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 4151 | btrfs_free_reserved_bytes(cache, len, delalloc); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4152 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4153 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 4154 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4155 | btrfs_put_block_group(cache); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4156 | return ret; |
| 4157 | } |
| 4158 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4159 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4160 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4161 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4162 | return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4163 | } |
| 4164 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4165 | 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] | 4166 | u64 start, u64 len) |
| 4167 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4168 | return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4169 | } |
| 4170 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4171 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4172 | u64 parent, u64 root_objectid, |
| 4173 | u64 flags, u64 owner, u64 offset, |
| 4174 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4175 | { |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4176 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4177 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4178 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4179 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4180 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4181 | struct extent_buffer *leaf; |
| 4182 | int type; |
| 4183 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4184 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4185 | if (parent > 0) |
| 4186 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4187 | else |
| 4188 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4189 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4190 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4191 | |
| 4192 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4193 | if (!path) |
| 4194 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4195 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4196 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4197 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4198 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4199 | if (ret) { |
| 4200 | btrfs_free_path(path); |
| 4201 | return ret; |
| 4202 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4203 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4204 | leaf = path->nodes[0]; |
| 4205 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4206 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4207 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4208 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4209 | btrfs_set_extent_flags(leaf, extent_item, |
| 4210 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4211 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4212 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4213 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4214 | if (parent > 0) { |
| 4215 | struct btrfs_shared_data_ref *ref; |
| 4216 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4217 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4218 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4219 | } else { |
| 4220 | struct btrfs_extent_data_ref *ref; |
| 4221 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4222 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4223 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4224 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4225 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4226 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4227 | |
| 4228 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4229 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4230 | |
Nikolay Borisov | 25a356d | 2018-05-10 15:44:54 +0300 | [diff] [blame] | 4231 | ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4232 | if (ret) |
| 4233 | return ret; |
| 4234 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4235 | ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4236 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4237 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4238 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4239 | BUG(); |
| 4240 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 4241 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4242 | return ret; |
| 4243 | } |
| 4244 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4245 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4246 | struct btrfs_delayed_ref_node *node, |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4247 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4248 | { |
Nikolay Borisov | 9dcdbe0 | 2018-05-21 12:27:20 +0300 | [diff] [blame] | 4249 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4250 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4251 | struct btrfs_extent_item *extent_item; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4252 | struct btrfs_key extent_key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4253 | struct btrfs_tree_block_info *block_info; |
| 4254 | struct btrfs_extent_inline_ref *iref; |
| 4255 | struct btrfs_path *path; |
| 4256 | struct extent_buffer *leaf; |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4257 | struct btrfs_delayed_tree_ref *ref; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4258 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4259 | u64 num_bytes; |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4260 | u64 flags = extent_op->flags_to_set; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4261 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4262 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4263 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 4264 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4265 | extent_key.objectid = node->bytenr; |
| 4266 | if (skinny_metadata) { |
| 4267 | extent_key.offset = ref->level; |
| 4268 | extent_key.type = BTRFS_METADATA_ITEM_KEY; |
| 4269 | num_bytes = fs_info->nodesize; |
| 4270 | } else { |
| 4271 | extent_key.offset = node->num_bytes; |
| 4272 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4273 | size += sizeof(*block_info); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4274 | num_bytes = node->num_bytes; |
| 4275 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4276 | |
| 4277 | path = btrfs_alloc_path(); |
Josef Bacik | 80ee54b | 2018-10-11 15:54:22 -0400 | [diff] [blame] | 4278 | if (!path) |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 4279 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4280 | |
| 4281 | path->leave_spinning = 1; |
| 4282 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4283 | &extent_key, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4284 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 4285 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4286 | return ret; |
| 4287 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4288 | |
| 4289 | leaf = path->nodes[0]; |
| 4290 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4291 | struct btrfs_extent_item); |
| 4292 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4293 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4294 | btrfs_set_extent_flags(leaf, extent_item, |
| 4295 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4296 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4297 | if (skinny_metadata) { |
| 4298 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4299 | } else { |
| 4300 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
Nikolay Borisov | 21ebfbe | 2018-05-21 12:27:22 +0300 | [diff] [blame] | 4301 | btrfs_set_tree_block_key(leaf, block_info, &extent_op->key); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4302 | btrfs_set_tree_block_level(leaf, block_info, ref->level); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4303 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4304 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4305 | |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4306 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4307 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4308 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4309 | BTRFS_SHARED_BLOCK_REF_KEY); |
Nikolay Borisov | d4b2073 | 2018-05-21 12:27:23 +0300 | [diff] [blame] | 4310 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4311 | } else { |
| 4312 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4313 | BTRFS_TREE_BLOCK_REF_KEY); |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4314 | btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | btrfs_mark_buffer_dirty(leaf); |
| 4318 | btrfs_free_path(path); |
| 4319 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4320 | ret = remove_from_free_space_tree(trans, extent_key.objectid, |
| 4321 | num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 4322 | if (ret) |
| 4323 | return ret; |
| 4324 | |
Josef Bacik | ade4b51 | 2019-06-20 15:38:01 -0400 | [diff] [blame] | 4325 | ret = btrfs_update_block_group(trans, extent_key.objectid, |
| 4326 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4327 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4328 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4329 | extent_key.objectid, extent_key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4330 | BUG(); |
| 4331 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 4332 | |
Nikolay Borisov | 4e6bd4e | 2018-05-21 12:27:21 +0300 | [diff] [blame] | 4333 | trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4334 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4335 | return ret; |
| 4336 | } |
| 4337 | |
| 4338 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4339 | struct btrfs_root *root, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 4340 | u64 offset, u64 ram_bytes, |
| 4341 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4342 | { |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4343 | struct btrfs_ref generic_ref = { 0 }; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4344 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4345 | |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 4346 | BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4347 | |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4348 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4349 | ins->objectid, ins->offset, 0); |
| 4350 | btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4351 | btrfs_ref_tree_mod(root->fs_info, &generic_ref); |
Qu Wenruo | 7667559 | 2019-04-04 14:45:32 +0800 | [diff] [blame] | 4352 | ret = btrfs_add_delayed_data_ref(trans, &generic_ref, |
| 4353 | ram_bytes, NULL, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4354 | return ret; |
| 4355 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4356 | |
| 4357 | /* |
| 4358 | * this is used by the tree logging recovery code. It records that |
| 4359 | * an extent has been allocated and makes sure to clear the free |
| 4360 | * space cache bits as well |
| 4361 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4362 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4363 | u64 root_objectid, u64 owner, u64 offset, |
| 4364 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4365 | { |
Nikolay Borisov | 61da2ab | 2018-06-20 15:49:13 +0300 | [diff] [blame] | 4366 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4367 | int ret; |
| 4368 | struct btrfs_block_group_cache *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4369 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4370 | |
| 4371 | /* |
| 4372 | * 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] | 4373 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4374 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4375 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4376 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 4377 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4378 | if (ret) |
| 4379 | return ret; |
| 4380 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4381 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4382 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 4383 | if (!block_group) |
| 4384 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4385 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 4386 | space_info = block_group->space_info; |
| 4387 | spin_lock(&space_info->lock); |
| 4388 | spin_lock(&block_group->lock); |
| 4389 | space_info->bytes_reserved += ins->offset; |
| 4390 | block_group->reserved += ins->offset; |
| 4391 | spin_unlock(&block_group->lock); |
| 4392 | spin_unlock(&space_info->lock); |
| 4393 | |
Nikolay Borisov | ef89b82 | 2018-06-20 15:48:58 +0300 | [diff] [blame] | 4394 | ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, |
| 4395 | offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 4396 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4397 | return ret; |
| 4398 | } |
| 4399 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4400 | static struct extent_buffer * |
| 4401 | 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] | 4402 | u64 bytenr, int level, u64 owner) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4403 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4404 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4405 | struct extent_buffer *buf; |
| 4406 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4407 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4408 | if (IS_ERR(buf)) |
| 4409 | return buf; |
| 4410 | |
Qu Wenruo | b72c3ab | 2018-08-21 09:53:47 +0800 | [diff] [blame] | 4411 | /* |
| 4412 | * Extra safety check in case the extent tree is corrupted and extent |
| 4413 | * allocator chooses to use a tree block which is already used and |
| 4414 | * locked. |
| 4415 | */ |
| 4416 | if (buf->lock_owner == current->pid) { |
| 4417 | btrfs_err_rl(fs_info, |
| 4418 | "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected", |
| 4419 | buf->start, btrfs_header_owner(buf), current->pid); |
| 4420 | free_extent_buffer(buf); |
| 4421 | return ERR_PTR(-EUCLEAN); |
| 4422 | } |
| 4423 | |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 4424 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4425 | btrfs_tree_lock(buf); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 4426 | btrfs_clean_tree_block(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4427 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4428 | |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4429 | btrfs_set_lock_blocking_write(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 4430 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4431 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4432 | memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header)); |
| 4433 | btrfs_set_header_level(buf, level); |
| 4434 | btrfs_set_header_bytenr(buf, buf->start); |
| 4435 | btrfs_set_header_generation(buf, trans->transid); |
| 4436 | btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV); |
| 4437 | btrfs_set_header_owner(buf, owner); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 4438 | write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4439 | write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4440 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4441 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4442 | /* |
| 4443 | * we allow two log transactions at a time, use different |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4444 | * EXTENT bit to differentiate dirty pages. |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4445 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4446 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4447 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 4448 | buf->start + buf->len - 1, GFP_NOFS); |
| 4449 | else |
| 4450 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 4451 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4452 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 4453 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4454 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4455 | buf->start + buf->len - 1, GFP_NOFS); |
| 4456 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 4457 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4458 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4459 | return buf; |
| 4460 | } |
| 4461 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4462 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4463 | * 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] | 4464 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4465 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 4466 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4467 | struct btrfs_root *root, |
| 4468 | u64 parent, u64 root_objectid, |
| 4469 | const struct btrfs_disk_key *key, |
| 4470 | int level, u64 hint, |
| 4471 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4472 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4473 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4474 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4475 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4476 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4477 | struct btrfs_delayed_extent_op *extent_op; |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4478 | struct btrfs_ref generic_ref = { 0 }; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4479 | u64 flags = 0; |
| 4480 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4481 | u32 blocksize = fs_info->nodesize; |
| 4482 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4483 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4484 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4485 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4486 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4487 | level, root_objectid); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 4488 | if (!IS_ERR(buf)) |
| 4489 | root->alloc_bytenr += blocksize; |
| 4490 | return buf; |
| 4491 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 4492 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 4493 | |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4494 | block_rsv = btrfs_use_block_rsv(trans, root, blocksize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4495 | if (IS_ERR(block_rsv)) |
| 4496 | return ERR_CAST(block_rsv); |
| 4497 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 4498 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 4499 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4500 | if (ret) |
| 4501 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4502 | |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 4503 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level, |
| 4504 | root_objectid); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4505 | if (IS_ERR(buf)) { |
| 4506 | ret = PTR_ERR(buf); |
| 4507 | goto out_free_reserved; |
| 4508 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4509 | |
| 4510 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4511 | if (parent == 0) |
| 4512 | parent = ins.objectid; |
| 4513 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4514 | } else |
| 4515 | BUG_ON(parent > 0); |
| 4516 | |
| 4517 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 4518 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4519 | if (!extent_op) { |
| 4520 | ret = -ENOMEM; |
| 4521 | goto out_free_buf; |
| 4522 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4523 | if (key) |
| 4524 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4525 | else |
| 4526 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4527 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 4528 | extent_op->update_key = skinny_metadata ? false : true; |
| 4529 | extent_op->update_flags = true; |
| 4530 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4531 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4532 | |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4533 | btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT, |
| 4534 | ins.objectid, ins.offset, parent); |
| 4535 | generic_ref.real_root = root->root_key.objectid; |
| 4536 | btrfs_init_tree_ref(&generic_ref, level, root_objectid); |
Qu Wenruo | 8a5040f | 2019-04-04 14:45:33 +0800 | [diff] [blame] | 4537 | btrfs_ref_tree_mod(fs_info, &generic_ref); |
Qu Wenruo | ed4f255 | 2019-04-04 14:45:31 +0800 | [diff] [blame] | 4538 | ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, |
Omar Sandoval | 7be0791 | 2017-06-06 16:45:30 -0700 | [diff] [blame] | 4539 | extent_op, NULL, NULL); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4540 | if (ret) |
| 4541 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4542 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4543 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4544 | |
| 4545 | out_free_delayed: |
| 4546 | btrfs_free_delayed_extent_op(extent_op); |
| 4547 | out_free_buf: |
| 4548 | free_extent_buffer(buf); |
| 4549 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4550 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4551 | out_unuse: |
Josef Bacik | 67f9c22 | 2019-06-19 13:47:23 -0400 | [diff] [blame] | 4552 | btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 4553 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4554 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4555 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4556 | struct walk_control { |
| 4557 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4558 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4559 | struct btrfs_key update_progress; |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 4560 | struct btrfs_key drop_progress; |
| 4561 | int drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4562 | int stage; |
| 4563 | int level; |
| 4564 | int shared_level; |
| 4565 | int update_ref; |
| 4566 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4567 | int reada_slot; |
| 4568 | int reada_count; |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4569 | int restarted; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4570 | }; |
| 4571 | |
| 4572 | #define DROP_REFERENCE 1 |
| 4573 | #define UPDATE_BACKREF 2 |
| 4574 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4575 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4576 | struct btrfs_root *root, |
| 4577 | struct walk_control *wc, |
| 4578 | struct btrfs_path *path) |
| 4579 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4580 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4581 | u64 bytenr; |
| 4582 | u64 generation; |
| 4583 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4584 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4585 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4586 | struct btrfs_key key; |
| 4587 | struct extent_buffer *eb; |
| 4588 | int ret; |
| 4589 | int slot; |
| 4590 | int nread = 0; |
| 4591 | |
| 4592 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4593 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4594 | wc->reada_count = max(wc->reada_count, 2); |
| 4595 | } else { |
| 4596 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4597 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4598 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | eb = path->nodes[wc->level]; |
| 4602 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4603 | |
| 4604 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 4605 | if (nread >= wc->reada_count) |
| 4606 | break; |
| 4607 | |
| 4608 | cond_resched(); |
| 4609 | bytenr = btrfs_node_blockptr(eb, slot); |
| 4610 | generation = btrfs_node_ptr_generation(eb, slot); |
| 4611 | |
| 4612 | if (slot == path->slots[wc->level]) |
| 4613 | goto reada; |
| 4614 | |
| 4615 | if (wc->stage == UPDATE_BACKREF && |
| 4616 | generation <= root->root_key.offset) |
| 4617 | continue; |
| 4618 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4619 | /* 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] | 4620 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4621 | wc->level - 1, 1, &refs, |
| 4622 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4623 | /* We don't care about errors in readahead. */ |
| 4624 | if (ret < 0) |
| 4625 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4626 | BUG_ON(refs == 0); |
| 4627 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4628 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4629 | if (refs == 1) |
| 4630 | goto reada; |
| 4631 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4632 | if (wc->level == 1 && |
| 4633 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4634 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4635 | if (!wc->update_ref || |
| 4636 | generation <= root->root_key.offset) |
| 4637 | continue; |
| 4638 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 4639 | ret = btrfs_comp_cpu_keys(&key, |
| 4640 | &wc->update_progress); |
| 4641 | if (ret < 0) |
| 4642 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4643 | } else { |
| 4644 | if (wc->level == 1 && |
| 4645 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4646 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4647 | } |
| 4648 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4649 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4650 | nread++; |
| 4651 | } |
| 4652 | wc->reada_slot = slot; |
| 4653 | } |
| 4654 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4655 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4656 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4657 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4658 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 4659 | * back refs for pointers in the block. |
| 4660 | * |
| 4661 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4662 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4663 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 4664 | struct btrfs_root *root, |
| 4665 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4666 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4667 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4668 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4669 | int level = wc->level; |
| 4670 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4671 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4672 | int ret; |
| 4673 | |
| 4674 | if (wc->stage == UPDATE_BACKREF && |
| 4675 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 4676 | return 1; |
| 4677 | |
| 4678 | /* |
| 4679 | * when reference count of tree block is 1, it won't increase |
| 4680 | * again. once full backref flag is set, we never clear it. |
| 4681 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4682 | if (lookup_info && |
| 4683 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 4684 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4685 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4686 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 4687 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4688 | &wc->refs[level], |
| 4689 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4690 | BUG_ON(ret == -ENOMEM); |
| 4691 | if (ret) |
| 4692 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4693 | BUG_ON(wc->refs[level] == 0); |
| 4694 | } |
| 4695 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4696 | if (wc->stage == DROP_REFERENCE) { |
| 4697 | if (wc->refs[level] > 1) |
| 4698 | return 1; |
| 4699 | |
| 4700 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4701 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4702 | path->locks[level] = 0; |
| 4703 | } |
| 4704 | return 0; |
| 4705 | } |
| 4706 | |
| 4707 | /* wc->stage == UPDATE_BACKREF */ |
| 4708 | if (!(wc->flags[level] & flag)) { |
| 4709 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4710 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4711 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 4712 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4713 | BUG_ON(ret); /* -ENOMEM */ |
David Sterba | f5c8daa | 2019-03-20 11:43:36 +0100 | [diff] [blame] | 4714 | ret = btrfs_set_disk_extent_flags(trans, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 4715 | eb->len, flag, |
| 4716 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4717 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4718 | wc->flags[level] |= flag; |
| 4719 | } |
| 4720 | |
| 4721 | /* |
| 4722 | * the block is shared by multiple trees, so it's not good to |
| 4723 | * keep the tree lock |
| 4724 | */ |
| 4725 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4726 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4727 | path->locks[level] = 0; |
| 4728 | } |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
| 4732 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4733 | * This is used to verify a ref exists for this root to deal with a bug where we |
| 4734 | * would have a drop_progress key that hadn't been updated properly. |
| 4735 | */ |
| 4736 | static int check_ref_exists(struct btrfs_trans_handle *trans, |
| 4737 | struct btrfs_root *root, u64 bytenr, u64 parent, |
| 4738 | int level) |
| 4739 | { |
| 4740 | struct btrfs_path *path; |
| 4741 | struct btrfs_extent_inline_ref *iref; |
| 4742 | int ret; |
| 4743 | |
| 4744 | path = btrfs_alloc_path(); |
| 4745 | if (!path) |
| 4746 | return -ENOMEM; |
| 4747 | |
| 4748 | ret = lookup_extent_backref(trans, path, &iref, bytenr, |
| 4749 | root->fs_info->nodesize, parent, |
| 4750 | root->root_key.objectid, level, 0); |
| 4751 | btrfs_free_path(path); |
| 4752 | if (ret == -ENOENT) |
| 4753 | return 0; |
| 4754 | if (ret < 0) |
| 4755 | return ret; |
| 4756 | return 1; |
| 4757 | } |
| 4758 | |
| 4759 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4760 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4761 | * |
| 4762 | * when wc->stage == DROP_REFERENCE, this function checks |
| 4763 | * reference count of the block pointed to. if the block |
| 4764 | * is shared and we need update back refs for the subtree |
| 4765 | * rooted at the block, this function changes wc->stage to |
| 4766 | * UPDATE_BACKREF. if the block is shared and there is no |
| 4767 | * need to update back, this function drops the reference |
| 4768 | * to the block. |
| 4769 | * |
| 4770 | * NOTE: return value 1 means we should stop walking down. |
| 4771 | */ |
| 4772 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 4773 | struct btrfs_root *root, |
| 4774 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4775 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4776 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4777 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4778 | u64 bytenr; |
| 4779 | u64 generation; |
| 4780 | u64 parent; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4781 | struct btrfs_key key; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4782 | struct btrfs_key first_key; |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4783 | struct btrfs_ref ref = { 0 }; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4784 | struct extent_buffer *next; |
| 4785 | int level = wc->level; |
| 4786 | int reada = 0; |
| 4787 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4788 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4789 | |
| 4790 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 4791 | path->slots[level]); |
| 4792 | /* |
| 4793 | * if the lower level block was created before the snapshot |
| 4794 | * was created, we know there is no need to update back refs |
| 4795 | * for the subtree |
| 4796 | */ |
| 4797 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4798 | generation <= root->root_key.offset) { |
| 4799 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4800 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4801 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4802 | |
| 4803 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4804 | btrfs_node_key_to_cpu(path->nodes[level], &first_key, |
| 4805 | path->slots[level]); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4806 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4807 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4808 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4809 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 4810 | if (IS_ERR(next)) |
| 4811 | return PTR_ERR(next); |
| 4812 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 4813 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 4814 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4815 | reada = 1; |
| 4816 | } |
| 4817 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4818 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4819 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4820 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4821 | &wc->refs[level - 1], |
| 4822 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4823 | if (ret < 0) |
| 4824 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4825 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4826 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4827 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4828 | ret = -EIO; |
| 4829 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4830 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4831 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4832 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4833 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4834 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4835 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4836 | if (level == 1 && |
| 4837 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4838 | goto skip; |
| 4839 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4840 | if (!wc->update_ref || |
| 4841 | generation <= root->root_key.offset) |
| 4842 | goto skip; |
| 4843 | |
| 4844 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 4845 | path->slots[level]); |
| 4846 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 4847 | if (ret < 0) |
| 4848 | goto skip; |
| 4849 | |
| 4850 | wc->stage = UPDATE_BACKREF; |
| 4851 | wc->shared_level = level - 1; |
| 4852 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4853 | } else { |
| 4854 | if (level == 1 && |
| 4855 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 4856 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4857 | } |
| 4858 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 4859 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4860 | btrfs_tree_unlock(next); |
| 4861 | free_extent_buffer(next); |
| 4862 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4863 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | if (!next) { |
| 4867 | if (reada && level == 1) |
| 4868 | reada_walk_down(trans, root, wc, path); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 4869 | next = read_tree_block(fs_info, bytenr, generation, level - 1, |
| 4870 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 4871 | if (IS_ERR(next)) { |
| 4872 | return PTR_ERR(next); |
| 4873 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 4874 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 4875 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 4876 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4877 | btrfs_tree_lock(next); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4878 | btrfs_set_lock_blocking_write(next); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4882 | ASSERT(level == btrfs_header_level(next)); |
| 4883 | if (level != btrfs_header_level(next)) { |
| 4884 | btrfs_err(root->fs_info, "mismatched level"); |
| 4885 | ret = -EIO; |
| 4886 | goto out_unlock; |
| 4887 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4888 | path->nodes[level] = next; |
| 4889 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 4890 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4891 | wc->level = level; |
| 4892 | if (wc->level == 1) |
| 4893 | wc->reada_slot = 0; |
| 4894 | return 0; |
| 4895 | skip: |
| 4896 | wc->refs[level - 1] = 0; |
| 4897 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4898 | if (wc->stage == DROP_REFERENCE) { |
| 4899 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 4900 | parent = path->nodes[level]->start; |
| 4901 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4902 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4903 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4904 | if (root->root_key.objectid != |
| 4905 | btrfs_header_owner(path->nodes[level])) { |
| 4906 | btrfs_err(root->fs_info, |
| 4907 | "mismatched block owner"); |
| 4908 | ret = -EIO; |
| 4909 | goto out_unlock; |
| 4910 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4911 | parent = 0; |
| 4912 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4913 | |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 4914 | /* |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4915 | * If we had a drop_progress we need to verify the refs are set |
| 4916 | * as expected. If we find our ref then we know that from here |
| 4917 | * on out everything should be correct, and we can clear the |
| 4918 | * ->restarted flag. |
| 4919 | */ |
| 4920 | if (wc->restarted) { |
| 4921 | ret = check_ref_exists(trans, root, bytenr, parent, |
| 4922 | level - 1); |
| 4923 | if (ret < 0) |
| 4924 | goto out_unlock; |
| 4925 | if (ret == 0) |
| 4926 | goto no_delete; |
| 4927 | ret = 0; |
| 4928 | wc->restarted = 0; |
| 4929 | } |
| 4930 | |
| 4931 | /* |
Qu Wenruo | 2cd86d3 | 2018-09-27 14:42:33 +0800 | [diff] [blame] | 4932 | * Reloc tree doesn't contribute to qgroup numbers, and we have |
| 4933 | * already accounted them at merge time (replace_path), |
| 4934 | * thus we could skip expensive subtree trace here. |
| 4935 | */ |
| 4936 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
| 4937 | need_account) { |
Lu Fengqi | deb4062 | 2018-07-18 14:45:38 +0800 | [diff] [blame] | 4938 | ret = btrfs_qgroup_trace_subtree(trans, next, |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 4939 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4940 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4941 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4942 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 4943 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 4944 | } |
| 4945 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 4946 | |
| 4947 | /* |
| 4948 | * We need to update the next key in our walk control so we can |
| 4949 | * update the drop_progress key accordingly. We don't care if |
| 4950 | * find_next_key doesn't find a key because that means we're at |
| 4951 | * the end and are going to clean up now. |
| 4952 | */ |
| 4953 | wc->drop_level = level; |
| 4954 | find_next_key(path, level, &wc->drop_progress); |
| 4955 | |
Qu Wenruo | ffd4bb2 | 2019-04-04 14:45:36 +0800 | [diff] [blame] | 4956 | btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, |
| 4957 | fs_info->nodesize, parent); |
| 4958 | btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid); |
| 4959 | ret = btrfs_free_extent(trans, &ref); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4960 | if (ret) |
| 4961 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4962 | } |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 4963 | no_delete: |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4964 | *lookup_info = 1; |
| 4965 | ret = 1; |
| 4966 | |
| 4967 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4968 | btrfs_tree_unlock(next); |
| 4969 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 4970 | |
| 4971 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 4975 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4976 | * |
| 4977 | * when wc->stage == DROP_REFERENCE, this function drops |
| 4978 | * reference count on the block. |
| 4979 | * |
| 4980 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 4981 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 4982 | * to UPDATE_BACKREF previously while processing the block. |
| 4983 | * |
| 4984 | * NOTE: return value 1 means we should stop walking up. |
| 4985 | */ |
| 4986 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 4987 | struct btrfs_root *root, |
| 4988 | struct btrfs_path *path, |
| 4989 | struct walk_control *wc) |
| 4990 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4991 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4992 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4993 | int level = wc->level; |
| 4994 | struct extent_buffer *eb = path->nodes[level]; |
| 4995 | u64 parent = 0; |
| 4996 | |
| 4997 | if (wc->stage == UPDATE_BACKREF) { |
| 4998 | BUG_ON(wc->shared_level < level); |
| 4999 | if (level < wc->shared_level) |
| 5000 | goto out; |
| 5001 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5002 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5003 | if (ret > 0) |
| 5004 | wc->update_ref = 0; |
| 5005 | |
| 5006 | wc->stage = DROP_REFERENCE; |
| 5007 | wc->shared_level = -1; |
| 5008 | path->slots[level] = 0; |
| 5009 | |
| 5010 | /* |
| 5011 | * check reference count again if the block isn't locked. |
| 5012 | * we should start walking down the tree again if reference |
| 5013 | * count is one. |
| 5014 | */ |
| 5015 | if (!path->locks[level]) { |
| 5016 | BUG_ON(level == 0); |
| 5017 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5018 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5019 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5020 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5021 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5022 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5023 | &wc->refs[level], |
| 5024 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5025 | if (ret < 0) { |
| 5026 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5027 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5028 | return ret; |
| 5029 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5030 | BUG_ON(wc->refs[level] == 0); |
| 5031 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5032 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 5033 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5034 | return 1; |
| 5035 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | /* wc->stage == DROP_REFERENCE */ |
| 5040 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5041 | |
| 5042 | if (wc->refs[level] == 1) { |
| 5043 | if (level == 0) { |
| 5044 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5045 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5046 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 5047 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5048 | BUG_ON(ret); /* -ENOMEM */ |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5049 | if (is_fstree(root->root_key.objectid)) { |
| 5050 | ret = btrfs_qgroup_trace_leaf_items(trans, eb); |
| 5051 | if (ret) { |
| 5052 | btrfs_err_rl(fs_info, |
| 5053 | "error %d accounting leaf items, quota is out of sync, rescan required", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5054 | ret); |
Qu Wenruo | c4140cb | 2019-04-04 14:45:37 +0800 | [diff] [blame] | 5055 | } |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5056 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5057 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5058 | /* make block locked assertion in btrfs_clean_tree_block happy */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5059 | if (!path->locks[level] && |
| 5060 | btrfs_header_generation(eb) == trans->transid) { |
| 5061 | btrfs_tree_lock(eb); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5062 | btrfs_set_lock_blocking_write(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5063 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5064 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5065 | btrfs_clean_tree_block(eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | if (eb == root->node) { |
| 5069 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5070 | parent = eb->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5071 | else if (root->root_key.objectid != btrfs_header_owner(eb)) |
| 5072 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5073 | } else { |
| 5074 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5075 | parent = path->nodes[level + 1]->start; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5076 | else if (root->root_key.objectid != |
| 5077 | btrfs_header_owner(path->nodes[level + 1])) |
| 5078 | goto owner_mismatch; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5079 | } |
| 5080 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5081 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5082 | out: |
| 5083 | wc->refs[level] = 0; |
| 5084 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5085 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5086 | |
| 5087 | owner_mismatch: |
| 5088 | btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", |
| 5089 | btrfs_header_owner(eb), root->root_key.objectid); |
| 5090 | return -EUCLEAN; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5091 | } |
| 5092 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5093 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5094 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5095 | struct btrfs_path *path, |
| 5096 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5097 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5098 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5099 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5100 | int ret; |
| 5101 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5102 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5103 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5104 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5105 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5106 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5107 | if (level == 0) |
| 5108 | break; |
| 5109 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5110 | if (path->slots[level] >= |
| 5111 | btrfs_header_nritems(path->nodes[level])) |
| 5112 | break; |
| 5113 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5114 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5115 | if (ret > 0) { |
| 5116 | path->slots[level]++; |
| 5117 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5118 | } else if (ret < 0) |
| 5119 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5120 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5121 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5122 | return 0; |
| 5123 | } |
| 5124 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5125 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5126 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5127 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5128 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5129 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5130 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5131 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5132 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5133 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5134 | while (level < max_level && path->nodes[level]) { |
| 5135 | wc->level = level; |
| 5136 | if (path->slots[level] + 1 < |
| 5137 | btrfs_header_nritems(path->nodes[level])) { |
| 5138 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5139 | return 0; |
| 5140 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5141 | ret = walk_up_proc(trans, root, path, wc); |
| 5142 | if (ret > 0) |
| 5143 | return 0; |
Qu Wenruo | 65c6e82 | 2018-08-21 09:42:03 +0800 | [diff] [blame] | 5144 | if (ret < 0) |
| 5145 | return ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5146 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5147 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5148 | btrfs_tree_unlock_rw(path->nodes[level], |
| 5149 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5150 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5151 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5152 | free_extent_buffer(path->nodes[level]); |
| 5153 | path->nodes[level] = NULL; |
| 5154 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5155 | } |
| 5156 | } |
| 5157 | return 1; |
| 5158 | } |
| 5159 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5160 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5161 | * drop a subvolume tree. |
| 5162 | * |
| 5163 | * this function traverses the tree freeing any blocks that only |
| 5164 | * referenced by the tree. |
| 5165 | * |
| 5166 | * when a shared tree block is found. this function decreases its |
| 5167 | * reference count by one. if update_ref is true, this function |
| 5168 | * also make sure backrefs for the shared block and all lower level |
| 5169 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5170 | * |
| 5171 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5172 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 5173 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5174 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 5175 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5176 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5177 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5178 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5179 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5180 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5181 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5182 | struct walk_control *wc; |
| 5183 | struct btrfs_key key; |
| 5184 | int err = 0; |
| 5185 | int ret; |
| 5186 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5187 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5188 | |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 5189 | btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 5190 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5191 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5192 | if (!path) { |
| 5193 | err = -ENOMEM; |
| 5194 | goto out; |
| 5195 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5196 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5197 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5198 | if (!wc) { |
| 5199 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5200 | err = -ENOMEM; |
| 5201 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 5202 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5203 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5204 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5205 | if (IS_ERR(trans)) { |
| 5206 | err = PTR_ERR(trans); |
| 5207 | goto out_free; |
| 5208 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 5209 | |
Josef Bacik | 0568e82 | 2018-11-30 11:52:14 -0500 | [diff] [blame] | 5210 | err = btrfs_run_delayed_items(trans); |
| 5211 | if (err) |
| 5212 | goto out_end_trans; |
| 5213 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5214 | if (block_rsv) |
| 5215 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5216 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 5217 | /* |
| 5218 | * This will help us catch people modifying the fs tree while we're |
| 5219 | * dropping it. It is unsafe to mess with the fs tree while it's being |
| 5220 | * dropped as we unlock the root node and parent nodes as we walk down |
| 5221 | * the tree, assuming nothing will change. If something does change |
| 5222 | * then we'll have stale information and drop references to blocks we've |
| 5223 | * already dropped. |
| 5224 | */ |
| 5225 | set_bit(BTRFS_ROOT_DELETING, &root->state); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5226 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5227 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5228 | path->nodes[level] = btrfs_lock_root_node(root); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5229 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5230 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5231 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5232 | memset(&wc->update_progress, 0, |
| 5233 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5234 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5235 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5236 | memcpy(&wc->update_progress, &key, |
| 5237 | sizeof(wc->update_progress)); |
| 5238 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5239 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5240 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5241 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5242 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5243 | path->lowest_level = 0; |
| 5244 | if (ret < 0) { |
| 5245 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5246 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5247 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5248 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5249 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5250 | /* |
| 5251 | * unlock our path, this is safe because only this |
| 5252 | * function is allowed to delete this snapshot |
| 5253 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5254 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5255 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5256 | level = btrfs_header_level(root->node); |
| 5257 | while (1) { |
| 5258 | btrfs_tree_lock(path->nodes[level]); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 5259 | btrfs_set_lock_blocking_write(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5260 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5261 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5262 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5263 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5264 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5265 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5266 | if (ret < 0) { |
| 5267 | err = ret; |
| 5268 | goto out_end_trans; |
| 5269 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5270 | BUG_ON(wc->refs[level] == 0); |
| 5271 | |
| 5272 | if (level == root_item->drop_level) |
| 5273 | break; |
| 5274 | |
| 5275 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 5276 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5277 | WARN_ON(wc->refs[level] != 1); |
| 5278 | level--; |
| 5279 | } |
| 5280 | } |
| 5281 | |
Josef Bacik | 78c52d9 | 2019-02-06 15:46:14 -0500 | [diff] [blame] | 5282 | wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5283 | wc->level = level; |
| 5284 | wc->shared_level = -1; |
| 5285 | wc->stage = DROP_REFERENCE; |
| 5286 | wc->update_ref = update_ref; |
| 5287 | wc->keep_locks = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5288 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5289 | |
| 5290 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 5291 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5292 | ret = walk_down_tree(trans, root, path, wc); |
| 5293 | if (ret < 0) { |
| 5294 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5295 | break; |
| 5296 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5297 | |
| 5298 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5299 | if (ret < 0) { |
| 5300 | err = ret; |
| 5301 | break; |
| 5302 | } |
| 5303 | |
| 5304 | if (ret > 0) { |
| 5305 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5306 | break; |
| 5307 | } |
| 5308 | |
| 5309 | if (wc->stage == DROP_REFERENCE) { |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5310 | wc->drop_level = wc->level; |
| 5311 | btrfs_node_key_to_cpu(path->nodes[wc->drop_level], |
| 5312 | &wc->drop_progress, |
| 5313 | path->slots[wc->drop_level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5314 | } |
Josef Bacik | aea6f02 | 2019-02-06 15:46:15 -0500 | [diff] [blame] | 5315 | btrfs_cpu_key_to_disk(&root_item->drop_progress, |
| 5316 | &wc->drop_progress); |
| 5317 | root_item->drop_level = wc->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5318 | |
| 5319 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5320 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5321 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5322 | ret = btrfs_update_root(trans, tree_root, |
| 5323 | &root->root_key, |
| 5324 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5325 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5326 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5327 | err = ret; |
| 5328 | goto out_end_trans; |
| 5329 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5330 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5331 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5332 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5333 | btrfs_debug(fs_info, |
| 5334 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 5335 | err = -EAGAIN; |
| 5336 | goto out_free; |
| 5337 | } |
| 5338 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5339 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5340 | if (IS_ERR(trans)) { |
| 5341 | err = PTR_ERR(trans); |
| 5342 | goto out_free; |
| 5343 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5344 | if (block_rsv) |
| 5345 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5346 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5347 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5348 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5349 | if (err) |
| 5350 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5351 | |
Lu Fengqi | ab9ce7d | 2018-08-01 11:32:27 +0800 | [diff] [blame] | 5352 | ret = btrfs_del_root(trans, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5353 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5354 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | e19182c | 2017-12-04 13:11:45 -0500 | [diff] [blame] | 5355 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5356 | goto out_end_trans; |
| 5357 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5358 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5359 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 5360 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 5361 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5362 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 5363 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5364 | err = ret; |
| 5365 | goto out_end_trans; |
| 5366 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 5367 | /* if we fail to delete the orphan item this time |
| 5368 | * around, it'll get picked up the next time. |
| 5369 | * |
| 5370 | * The most common failure here is just -ENOENT. |
| 5371 | */ |
| 5372 | btrfs_del_orphan_item(trans, tree_root, |
| 5373 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5374 | } |
| 5375 | } |
| 5376 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 5377 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 5378 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5379 | } else { |
| 5380 | free_extent_buffer(root->node); |
| 5381 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 5382 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5383 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5384 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5385 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 5386 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5387 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5388 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5389 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 5390 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5391 | /* |
| 5392 | * So if we need to stop dropping the snapshot for whatever reason we |
| 5393 | * need to make sure to add it back to the dead root list so that we |
| 5394 | * keep trying to do the work later. This also cleans up roots if we |
| 5395 | * don't have it in the radix (like when we recover after a power fail |
| 5396 | * or unmount) so we don't leak memory. |
| 5397 | */ |
Thomas Meyer | 897ca81 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 5398 | if (!for_reloc && !root_dropped) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 5399 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 5400 | if (err && err != -EAGAIN) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 5401 | btrfs_handle_fs_error(fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 5402 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5403 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5404 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5405 | /* |
| 5406 | * drop subtree rooted at tree block 'node'. |
| 5407 | * |
| 5408 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5409 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5410 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5411 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5412 | struct btrfs_root *root, |
| 5413 | struct extent_buffer *node, |
| 5414 | struct extent_buffer *parent) |
| 5415 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5416 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5417 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5418 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5419 | int level; |
| 5420 | int parent_level; |
| 5421 | int ret = 0; |
| 5422 | int wret; |
| 5423 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5424 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5425 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5426 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5427 | if (!path) |
| 5428 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5429 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5430 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5431 | if (!wc) { |
| 5432 | btrfs_free_path(path); |
| 5433 | return -ENOMEM; |
| 5434 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5435 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5436 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5437 | parent_level = btrfs_header_level(parent); |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 5438 | atomic_inc(&parent->refs); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5439 | path->nodes[parent_level] = parent; |
| 5440 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5441 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5442 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5443 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5444 | path->nodes[level] = node; |
| 5445 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5446 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5447 | |
| 5448 | wc->refs[parent_level] = 1; |
| 5449 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5450 | wc->level = level; |
| 5451 | wc->shared_level = -1; |
| 5452 | wc->stage = DROP_REFERENCE; |
| 5453 | wc->update_ref = 0; |
| 5454 | wc->keep_locks = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5455 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5456 | |
| 5457 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5458 | wret = walk_down_tree(trans, root, path, wc); |
| 5459 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5460 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5461 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5462 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5463 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5464 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5465 | if (wret < 0) |
| 5466 | ret = wret; |
| 5467 | if (wret != 0) |
| 5468 | break; |
| 5469 | } |
| 5470 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5471 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5472 | btrfs_free_path(path); |
| 5473 | return ret; |
| 5474 | } |
| 5475 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5476 | /* |
| 5477 | * 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] | 5478 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5479 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5480 | 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] | 5481 | { |
| 5482 | struct btrfs_block_group_cache *block_group; |
| 5483 | u64 free_bytes = 0; |
| 5484 | int factor; |
| 5485 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5486 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 5487 | if (list_empty(&sinfo->ro_bgs)) |
| 5488 | return 0; |
| 5489 | |
| 5490 | spin_lock(&sinfo->lock); |
| 5491 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5492 | spin_lock(&block_group->lock); |
| 5493 | |
| 5494 | if (!block_group->ro) { |
| 5495 | spin_unlock(&block_group->lock); |
| 5496 | continue; |
| 5497 | } |
| 5498 | |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 5499 | factor = btrfs_bg_type_to_factor(block_group->flags); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5500 | free_bytes += (block_group->key.offset - |
| 5501 | btrfs_block_group_used(&block_group->item)) * |
| 5502 | factor; |
| 5503 | |
| 5504 | spin_unlock(&block_group->lock); |
| 5505 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 5506 | spin_unlock(&sinfo->lock); |
| 5507 | |
| 5508 | return free_bytes; |
| 5509 | } |
| 5510 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5511 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 5512 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5513 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5514 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 5515 | } |
| 5516 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5517 | /* |
| 5518 | * It used to be that old block groups would be left around forever. |
| 5519 | * Iterating over them would be enough to trim unused space. Since we |
| 5520 | * now automatically remove them, we also need to iterate over unallocated |
| 5521 | * space. |
| 5522 | * |
| 5523 | * We don't want a transaction for this since the discard may take a |
| 5524 | * substantial amount of time. We don't require that a transaction be |
| 5525 | * running, but we do need to take a running transaction into account |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5526 | * to ensure that we're not discarding chunks that were released or |
| 5527 | * allocated in the current transaction. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5528 | * |
| 5529 | * Holding the chunks lock will prevent other threads from allocating |
| 5530 | * or releasing chunks, but it won't prevent a running transaction |
| 5531 | * from committing and releasing the memory that the pending chunks |
| 5532 | * 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] | 5533 | * transaction and hold the commit root sem. We only need to hold |
| 5534 | * it while performing the free space search since we have already |
| 5535 | * held back allocations. |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5536 | */ |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5537 | static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5538 | { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5539 | u64 start = SZ_1M, len = 0, end = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5540 | int ret; |
| 5541 | |
| 5542 | *trimmed = 0; |
| 5543 | |
Jeff Mahoney | 0be88e3 | 2018-09-06 17:18:15 -0400 | [diff] [blame] | 5544 | /* Discard not supported = nothing to do. */ |
| 5545 | if (!blk_queue_discard(bdev_get_queue(device->bdev))) |
| 5546 | return 0; |
| 5547 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5548 | /* Not writable = nothing to do. */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5549 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5550 | return 0; |
| 5551 | |
| 5552 | /* No free space = nothing to do. */ |
| 5553 | if (device->total_bytes <= device->bytes_used) |
| 5554 | return 0; |
| 5555 | |
| 5556 | ret = 0; |
| 5557 | |
| 5558 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 5559 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5560 | u64 bytes; |
| 5561 | |
| 5562 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 5563 | if (ret) |
Jeff Mahoney | fee7acc | 2018-09-06 17:18:16 -0400 | [diff] [blame] | 5564 | break; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5565 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5566 | find_first_clear_extent_bit(&device->alloc_state, start, |
| 5567 | &start, &end, |
| 5568 | CHUNK_TRIMMED | CHUNK_ALLOCATED); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5569 | |
| 5570 | /* Ensure we skip the reserved area in the first 1M */ |
| 5571 | start = max_t(u64, start, SZ_1M); |
| 5572 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5573 | /* |
| 5574 | * If find_first_clear_extent_bit find a range that spans the |
| 5575 | * end of the device it will set end to -1, in this case it's up |
| 5576 | * to the caller to trim the value to the size of the device. |
| 5577 | */ |
| 5578 | end = min(end, device->total_bytes - 1); |
Nikolay Borisov | 53460a4 | 2019-06-03 13:06:01 +0300 | [diff] [blame] | 5579 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5580 | len = end - start + 1; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5581 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5582 | /* We didn't find any extents */ |
| 5583 | if (!len) { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5584 | mutex_unlock(&fs_info->chunk_mutex); |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5585 | ret = 0; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5586 | break; |
| 5587 | } |
| 5588 | |
Nikolay Borisov | 929be17 | 2019-03-27 14:24:18 +0200 | [diff] [blame] | 5589 | ret = btrfs_issue_discard(device->bdev, start, len, |
| 5590 | &bytes); |
| 5591 | if (!ret) |
| 5592 | set_extent_bits(&device->alloc_state, start, |
| 5593 | start + bytes - 1, |
| 5594 | CHUNK_TRIMMED); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5595 | mutex_unlock(&fs_info->chunk_mutex); |
| 5596 | |
| 5597 | if (ret) |
| 5598 | break; |
| 5599 | |
| 5600 | start += len; |
| 5601 | *trimmed += bytes; |
| 5602 | |
| 5603 | if (fatal_signal_pending(current)) { |
| 5604 | ret = -ERESTARTSYS; |
| 5605 | break; |
| 5606 | } |
| 5607 | |
| 5608 | cond_resched(); |
| 5609 | } |
| 5610 | |
| 5611 | return ret; |
| 5612 | } |
| 5613 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5614 | /* |
| 5615 | * Trim the whole filesystem by: |
| 5616 | * 1) trimming the free space in each block group |
| 5617 | * 2) trimming the unallocated space on each device |
| 5618 | * |
| 5619 | * This will also continue trimming even if a block group or device encounters |
| 5620 | * an error. The return value will be the last error, or 0 if nothing bad |
| 5621 | * happens. |
| 5622 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5623 | 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] | 5624 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5625 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5626 | struct btrfs_device *device; |
| 5627 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5628 | u64 group_trimmed; |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5629 | u64 range_end = U64_MAX; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5630 | u64 start; |
| 5631 | u64 end; |
| 5632 | u64 trimmed = 0; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5633 | u64 bg_failed = 0; |
| 5634 | u64 dev_failed = 0; |
| 5635 | int bg_ret = 0; |
| 5636 | int dev_ret = 0; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5637 | int ret = 0; |
| 5638 | |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5639 | /* |
| 5640 | * Check range overflow if range->len is set. |
| 5641 | * The default range->len is U64_MAX. |
| 5642 | */ |
| 5643 | if (range->len != U64_MAX && |
| 5644 | check_add_overflow(range->start, range->len, &range_end)) |
| 5645 | return -EINVAL; |
| 5646 | |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 5647 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
Josef Bacik | 2e405ad | 2019-06-20 15:37:45 -0400 | [diff] [blame] | 5648 | for (; cache; cache = btrfs_next_block_group(cache)) { |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5649 | if (cache->key.objectid >= range_end) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5650 | btrfs_put_block_group(cache); |
| 5651 | break; |
| 5652 | } |
| 5653 | |
| 5654 | start = max(range->start, cache->key.objectid); |
Qu Wenruo | 07301df | 2019-05-28 16:21:54 +0800 | [diff] [blame] | 5655 | end = min(range_end, cache->key.objectid + cache->key.offset); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5656 | |
| 5657 | if (end - start >= range->minlen) { |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5658 | if (!btrfs_block_group_cache_done(cache)) { |
| 5659 | ret = btrfs_cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5660 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5661 | bg_failed++; |
| 5662 | bg_ret = ret; |
| 5663 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5664 | } |
Josef Bacik | 676f1f7 | 2019-06-20 15:37:48 -0400 | [diff] [blame] | 5665 | ret = btrfs_wait_block_group_cache_done(cache); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5666 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5667 | bg_failed++; |
| 5668 | bg_ret = ret; |
| 5669 | continue; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 5670 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5671 | } |
| 5672 | ret = btrfs_trim_block_group(cache, |
| 5673 | &group_trimmed, |
| 5674 | start, |
| 5675 | end, |
| 5676 | range->minlen); |
| 5677 | |
| 5678 | trimmed += group_trimmed; |
| 5679 | if (ret) { |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5680 | bg_failed++; |
| 5681 | bg_ret = ret; |
| 5682 | continue; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5683 | } |
| 5684 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5687 | if (bg_failed) |
| 5688 | btrfs_warn(fs_info, |
| 5689 | "failed to trim %llu block group(s), last error %d", |
| 5690 | bg_failed, bg_ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5691 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | d4e329d | 2018-09-06 17:18:14 -0400 | [diff] [blame] | 5692 | devices = &fs_info->fs_devices->devices; |
| 5693 | list_for_each_entry(device, devices, dev_list) { |
Nikolay Borisov | 8103d10 | 2019-06-03 13:06:00 +0300 | [diff] [blame] | 5694 | ret = btrfs_trim_free_extents(device, &group_trimmed); |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5695 | if (ret) { |
| 5696 | dev_failed++; |
| 5697 | dev_ret = ret; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5698 | break; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5699 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5700 | |
| 5701 | trimmed += group_trimmed; |
| 5702 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5703 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 5704 | |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5705 | if (dev_failed) |
| 5706 | btrfs_warn(fs_info, |
| 5707 | "failed to trim %llu device(s), last error %d", |
| 5708 | dev_failed, dev_ret); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5709 | range->len = trimmed; |
Qu Wenruo | 93bba24 | 2018-09-07 14:16:23 +0800 | [diff] [blame] | 5710 | if (bg_ret) |
| 5711 | return bg_ret; |
| 5712 | return dev_ret; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 5713 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5714 | |
| 5715 | /* |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5716 | * btrfs_{start,end}_write_no_snapshotting() are similar to |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5717 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 5718 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 5719 | * 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] | 5720 | * operations while snapshotting is ongoing and that cause the snapshot to be |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 5721 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5722 | */ |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5723 | void btrfs_end_write_no_snapshotting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5724 | { |
| 5725 | percpu_counter_dec(&root->subv_writers->counter); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 5726 | cond_wake_up(&root->subv_writers->wait); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5727 | } |
| 5728 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5729 | int btrfs_start_write_no_snapshotting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5730 | { |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5731 | if (atomic_read(&root->will_be_snapshotted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5732 | return 0; |
| 5733 | |
| 5734 | percpu_counter_inc(&root->subv_writers->counter); |
| 5735 | /* |
| 5736 | * Make sure counter is updated before we check for snapshot creation. |
| 5737 | */ |
| 5738 | smp_mb(); |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5739 | if (atomic_read(&root->will_be_snapshotted)) { |
| 5740 | btrfs_end_write_no_snapshotting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 5741 | return 0; |
| 5742 | } |
| 5743 | return 1; |
| 5744 | } |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5745 | |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5746 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) |
| 5747 | { |
| 5748 | while (true) { |
| 5749 | int ret; |
| 5750 | |
David Sterba | ea14b57f | 2017-06-22 02:19:11 +0200 | [diff] [blame] | 5751 | ret = btrfs_start_write_no_snapshotting(root); |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5752 | if (ret) |
| 5753 | break; |
Peter Zijlstra | 4625956 | 2018-03-15 11:43:08 +0100 | [diff] [blame] | 5754 | wait_var_event(&root->will_be_snapshotted, |
| 5755 | !atomic_read(&root->will_be_snapshotted)); |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 5756 | } |
| 5757 | } |