Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 20 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 21 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 23 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 24 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 27 | #include <linux/ratelimit.h> |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 28 | #include <linux/percpu_counter.h> |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 29 | #include "hash.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 30 | #include "tree-log.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 31 | #include "disk-io.h" |
| 32 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 34 | #include "raid56.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 35 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 36 | #include "free-space-cache.h" |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 37 | #include "free-space-tree.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 38 | #include "math.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 39 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 40 | #include "qgroup.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 41 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 42 | #undef SCRAMBLE_DELAYED_REFS |
| 43 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 44 | /* |
| 45 | * control flags for do_chunk_alloc's force field |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 46 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 47 | * if we really need one. |
| 48 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 49 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 50 | * if we have very few chunks already allocated. This is |
| 51 | * used as part of the clustering code to help make sure |
| 52 | * we have a good pool of storage to cluster in, without |
| 53 | * filling the FS with empty chunks |
| 54 | * |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 55 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 56 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 57 | */ |
| 58 | enum { |
| 59 | CHUNK_ALLOC_NO_FORCE = 0, |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 60 | CHUNK_ALLOC_LIMITED = 1, |
| 61 | CHUNK_ALLOC_FORCE = 2, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 62 | }; |
| 63 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 64 | static int update_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 65 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 66 | u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 67 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 68 | struct btrfs_fs_info *fs_info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 69 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 70 | u64 root_objectid, u64 owner_objectid, |
| 71 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 72 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 73 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 74 | struct extent_buffer *leaf, |
| 75 | struct btrfs_extent_item *ei); |
| 76 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 77 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 78 | u64 parent, u64 root_objectid, |
| 79 | u64 flags, u64 owner, u64 offset, |
| 80 | struct btrfs_key *ins, int ref_mod); |
| 81 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 82 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 83 | u64 parent, u64 root_objectid, |
| 84 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 85 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 86 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 87 | struct btrfs_fs_info *fs_info, u64 flags, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 88 | int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 89 | static int find_next_key(struct btrfs_path *path, int level, |
| 90 | struct btrfs_key *key); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 91 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
| 92 | struct btrfs_space_info *info, u64 bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 93 | int dump_block_groups); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 94 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 95 | u64 ram_bytes, u64 num_bytes, int delalloc); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 96 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 97 | u64 num_bytes, int delalloc); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 98 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 99 | u64 num_bytes); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 100 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
| 101 | struct btrfs_space_info *space_info, |
| 102 | u64 orig_bytes, |
| 103 | enum btrfs_reserve_flush_enum flush); |
| 104 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, |
| 105 | struct btrfs_space_info *space_info, |
| 106 | u64 num_bytes); |
| 107 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, |
| 108 | struct btrfs_space_info *space_info, |
| 109 | u64 num_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 110 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 111 | static noinline int |
| 112 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 113 | { |
| 114 | smp_mb(); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 115 | return cache->cached == BTRFS_CACHE_FINISHED || |
| 116 | cache->cached == BTRFS_CACHE_ERROR; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 119 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 120 | { |
| 121 | return (cache->flags & bits) == bits; |
| 122 | } |
| 123 | |
Filipe Manana | 758f2df | 2015-11-19 11:45:48 +0000 | [diff] [blame] | 124 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 125 | { |
| 126 | atomic_inc(&cache->count); |
| 127 | } |
| 128 | |
| 129 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 130 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 131 | if (atomic_dec_and_test(&cache->count)) { |
| 132 | WARN_ON(cache->pinned > 0); |
| 133 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 134 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 135 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 136 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 139 | /* |
| 140 | * this adds the block group to the fs_info rb tree for the block group |
| 141 | * cache |
| 142 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 143 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 144 | struct btrfs_block_group_cache *block_group) |
| 145 | { |
| 146 | struct rb_node **p; |
| 147 | struct rb_node *parent = NULL; |
| 148 | struct btrfs_block_group_cache *cache; |
| 149 | |
| 150 | spin_lock(&info->block_group_cache_lock); |
| 151 | p = &info->block_group_cache_tree.rb_node; |
| 152 | |
| 153 | while (*p) { |
| 154 | parent = *p; |
| 155 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 156 | cache_node); |
| 157 | if (block_group->key.objectid < cache->key.objectid) { |
| 158 | p = &(*p)->rb_left; |
| 159 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 160 | p = &(*p)->rb_right; |
| 161 | } else { |
| 162 | spin_unlock(&info->block_group_cache_lock); |
| 163 | return -EEXIST; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | rb_link_node(&block_group->cache_node, parent, p); |
| 168 | rb_insert_color(&block_group->cache_node, |
| 169 | &info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 170 | |
| 171 | if (info->first_logical_byte > block_group->key.objectid) |
| 172 | info->first_logical_byte = block_group->key.objectid; |
| 173 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 174 | spin_unlock(&info->block_group_cache_lock); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * This will return the block group at or after bytenr if contains is 0, else |
| 181 | * it will return the block group that contains the bytenr |
| 182 | */ |
| 183 | static struct btrfs_block_group_cache * |
| 184 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 185 | int contains) |
| 186 | { |
| 187 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 188 | struct rb_node *n; |
| 189 | u64 end, start; |
| 190 | |
| 191 | spin_lock(&info->block_group_cache_lock); |
| 192 | n = info->block_group_cache_tree.rb_node; |
| 193 | |
| 194 | while (n) { |
| 195 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 196 | cache_node); |
| 197 | end = cache->key.objectid + cache->key.offset - 1; |
| 198 | start = cache->key.objectid; |
| 199 | |
| 200 | if (bytenr < start) { |
| 201 | if (!contains && (!ret || start < ret->key.objectid)) |
| 202 | ret = cache; |
| 203 | n = n->rb_left; |
| 204 | } else if (bytenr > start) { |
| 205 | if (contains && bytenr <= end) { |
| 206 | ret = cache; |
| 207 | break; |
| 208 | } |
| 209 | n = n->rb_right; |
| 210 | } else { |
| 211 | ret = cache; |
| 212 | break; |
| 213 | } |
| 214 | } |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 215 | if (ret) { |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 216 | btrfs_get_block_group(ret); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 217 | if (bytenr == 0 && info->first_logical_byte > ret->key.objectid) |
| 218 | info->first_logical_byte = ret->key.objectid; |
| 219 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 220 | spin_unlock(&info->block_group_cache_lock); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 225 | static int add_excluded_extent(struct btrfs_fs_info *fs_info, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 226 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 227 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | u64 end = start + num_bytes - 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 229 | set_extent_bits(&fs_info->freed_extents[0], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 230 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 231 | set_extent_bits(&fs_info->freed_extents[1], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 232 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 233 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 236 | static void free_excluded_extents(struct btrfs_fs_info *fs_info, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 237 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 238 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 239 | u64 start, end; |
| 240 | |
| 241 | start = cache->key.objectid; |
| 242 | end = start + cache->key.offset - 1; |
| 243 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 244 | clear_extent_bits(&fs_info->freed_extents[0], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 245 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 246 | clear_extent_bits(&fs_info->freed_extents[1], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 247 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 250 | static int exclude_super_stripes(struct btrfs_fs_info *fs_info, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 251 | struct btrfs_block_group_cache *cache) |
| 252 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 253 | u64 bytenr; |
| 254 | u64 *logical; |
| 255 | int stripe_len; |
| 256 | int i, nr, ret; |
| 257 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 258 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 259 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 260 | cache->bytes_super += stripe_len; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 261 | ret = add_excluded_extent(fs_info, cache->key.objectid, |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 262 | stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 267 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 268 | bytenr = btrfs_sb_offset(i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 269 | ret = btrfs_rmap_block(fs_info, cache->key.objectid, |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 270 | bytenr, 0, &logical, &nr, &stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 271 | if (ret) |
| 272 | return ret; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 273 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 274 | while (nr--) { |
Josef Bacik | 51bf5f0 | 2013-04-23 12:55:21 -0400 | [diff] [blame] | 275 | u64 start, len; |
| 276 | |
| 277 | if (logical[nr] > cache->key.objectid + |
| 278 | cache->key.offset) |
| 279 | continue; |
| 280 | |
| 281 | if (logical[nr] + stripe_len <= cache->key.objectid) |
| 282 | continue; |
| 283 | |
| 284 | start = logical[nr]; |
| 285 | if (start < cache->key.objectid) { |
| 286 | start = cache->key.objectid; |
| 287 | len = (logical[nr] + stripe_len) - start; |
| 288 | } else { |
| 289 | len = min_t(u64, stripe_len, |
| 290 | cache->key.objectid + |
| 291 | cache->key.offset - start); |
| 292 | } |
| 293 | |
| 294 | cache->bytes_super += len; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 295 | ret = add_excluded_extent(fs_info, start, len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 296 | if (ret) { |
| 297 | kfree(logical); |
| 298 | return ret; |
| 299 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 300 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 301 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 302 | kfree(logical); |
| 303 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 307 | static struct btrfs_caching_control * |
| 308 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 309 | { |
| 310 | struct btrfs_caching_control *ctl; |
| 311 | |
| 312 | spin_lock(&cache->lock); |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 313 | if (!cache->caching_ctl) { |
| 314 | spin_unlock(&cache->lock); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 318 | ctl = cache->caching_ctl; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 319 | refcount_inc(&ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 320 | spin_unlock(&cache->lock); |
| 321 | return ctl; |
| 322 | } |
| 323 | |
| 324 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 325 | { |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 326 | if (refcount_dec_and_test(&ctl->count)) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 327 | kfree(ctl); |
| 328 | } |
| 329 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 330 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 331 | static void fragment_free_space(struct btrfs_block_group_cache *block_group) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 332 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 333 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 334 | u64 start = block_group->key.objectid; |
| 335 | u64 len = block_group->key.offset; |
| 336 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 337 | fs_info->nodesize : fs_info->sectorsize; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 338 | u64 step = chunk << 1; |
| 339 | |
| 340 | while (len > chunk) { |
| 341 | btrfs_remove_free_space(block_group, start, chunk); |
| 342 | start += step; |
| 343 | if (len < step) |
| 344 | len = 0; |
| 345 | else |
| 346 | len -= step; |
| 347 | } |
| 348 | } |
| 349 | #endif |
| 350 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 351 | /* |
| 352 | * this is only called by cache_block_group, since we could have freed extents |
| 353 | * we need to check the pinned_extents for any extents that can't be used yet |
| 354 | * since their free space will be released as soon as the transaction commits. |
| 355 | */ |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 356 | u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 357 | struct btrfs_fs_info *info, u64 start, u64 end) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 358 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 359 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 360 | int ret; |
| 361 | |
| 362 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 363 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 364 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 365 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 366 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 367 | if (ret) |
| 368 | break; |
| 369 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 370 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 371 | start = extent_end + 1; |
| 372 | } else if (extent_start > start && extent_start < end) { |
| 373 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 374 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 375 | ret = btrfs_add_free_space(block_group, start, |
| 376 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 377 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 378 | start = extent_end + 1; |
| 379 | } else { |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (start < end) { |
| 385 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 386 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 387 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 388 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 391 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 394 | static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 395 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 396 | struct btrfs_block_group_cache *block_group = caching_ctl->block_group; |
| 397 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 398 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 399 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 400 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 401 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 403 | u64 last = 0; |
| 404 | u32 nritems; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 405 | int ret; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 406 | bool wakeup = true; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 407 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 408 | path = btrfs_alloc_path(); |
| 409 | if (!path) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 410 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 411 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 412 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 413 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 414 | #ifdef CONFIG_BTRFS_DEBUG |
| 415 | /* |
| 416 | * If we're fragmenting we don't want to make anybody think we can |
| 417 | * allocate from this block group until we've had a chance to fragment |
| 418 | * the free space. |
| 419 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 420 | if (btrfs_should_fragment_free_space(block_group)) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 421 | wakeup = false; |
| 422 | #endif |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 423 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 424 | * We don't want to deadlock with somebody trying to allocate a new |
| 425 | * extent for the extent root while also trying to search the extent |
| 426 | * root to add free space. So we skip locking and search the commit |
| 427 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 428 | */ |
| 429 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 430 | path->search_commit_root = 1; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 431 | path->reada = READA_FORWARD; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 432 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 433 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 434 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 435 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 436 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 437 | next: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 438 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 439 | if (ret < 0) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 440 | goto out; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 441 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 442 | leaf = path->nodes[0]; |
| 443 | nritems = btrfs_header_nritems(leaf); |
| 444 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 445 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 446 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 447 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 448 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 449 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 450 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 451 | if (path->slots[0] < nritems) { |
| 452 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 453 | } else { |
| 454 | ret = find_next_key(path, 0, &key); |
| 455 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 456 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 457 | |
Josef Bacik | c9ea7b2 | 2013-09-19 10:02:11 -0400 | [diff] [blame] | 458 | if (need_resched() || |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 459 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 460 | if (wakeup) |
| 461 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 462 | btrfs_release_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 463 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 464 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 465 | cond_resched(); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 466 | mutex_lock(&caching_ctl->mutex); |
| 467 | down_read(&fs_info->commit_root_sem); |
| 468 | goto next; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 469 | } |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 470 | |
| 471 | ret = btrfs_next_leaf(extent_root, path); |
| 472 | if (ret < 0) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 473 | goto out; |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 474 | if (ret) |
| 475 | break; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 476 | leaf = path->nodes[0]; |
| 477 | nritems = btrfs_header_nritems(leaf); |
| 478 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 479 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 480 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 481 | if (key.objectid < last) { |
| 482 | key.objectid = last; |
| 483 | key.offset = 0; |
| 484 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 485 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 486 | if (wakeup) |
| 487 | caching_ctl->progress = last; |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 488 | btrfs_release_path(path); |
| 489 | goto next; |
| 490 | } |
| 491 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 492 | if (key.objectid < block_group->key.objectid) { |
| 493 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 494 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 495 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 496 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 497 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 498 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 499 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 500 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 501 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 502 | key.type == BTRFS_METADATA_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 503 | total_found += add_new_free_space(block_group, |
| 504 | fs_info, last, |
| 505 | key.objectid); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 506 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 507 | last = key.objectid + |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 508 | fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 509 | else |
| 510 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 511 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 512 | if (total_found > CACHING_CTL_WAKE_UP) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 513 | total_found = 0; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 514 | if (wakeup) |
| 515 | wake_up(&caching_ctl->wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 516 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 517 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 518 | path->slots[0]++; |
| 519 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 520 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 521 | |
| 522 | total_found += add_new_free_space(block_group, fs_info, last, |
| 523 | block_group->key.objectid + |
| 524 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 525 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 526 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 527 | out: |
| 528 | btrfs_free_path(path); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | static noinline void caching_thread(struct btrfs_work *work) |
| 533 | { |
| 534 | struct btrfs_block_group_cache *block_group; |
| 535 | struct btrfs_fs_info *fs_info; |
| 536 | struct btrfs_caching_control *caching_ctl; |
Chris Mason | b4570aa | 2015-12-30 07:37:26 -0800 | [diff] [blame] | 537 | struct btrfs_root *extent_root; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 538 | int ret; |
| 539 | |
| 540 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 541 | block_group = caching_ctl->block_group; |
| 542 | fs_info = block_group->fs_info; |
Chris Mason | b4570aa | 2015-12-30 07:37:26 -0800 | [diff] [blame] | 543 | extent_root = fs_info->extent_root; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 544 | |
| 545 | mutex_lock(&caching_ctl->mutex); |
| 546 | down_read(&fs_info->commit_root_sem); |
| 547 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 548 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) |
| 549 | ret = load_free_space_tree(caching_ctl); |
| 550 | else |
| 551 | ret = load_extent_tree_free(caching_ctl); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 552 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 553 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 554 | block_group->caching_ctl = NULL; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 555 | block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 556 | spin_unlock(&block_group->lock); |
| 557 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 558 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 559 | if (btrfs_should_fragment_free_space(block_group)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 560 | u64 bytes_used; |
| 561 | |
| 562 | spin_lock(&block_group->space_info->lock); |
| 563 | spin_lock(&block_group->lock); |
| 564 | bytes_used = block_group->key.offset - |
| 565 | btrfs_block_group_used(&block_group->item); |
| 566 | block_group->space_info->bytes_used += bytes_used >> 1; |
| 567 | spin_unlock(&block_group->lock); |
| 568 | spin_unlock(&block_group->space_info->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 569 | fragment_free_space(block_group); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 570 | } |
| 571 | #endif |
| 572 | |
| 573 | caching_ctl->progress = (u64)-1; |
Chris Mason | f7d3d2f | 2015-12-18 11:11:10 -0800 | [diff] [blame] | 574 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 575 | up_read(&fs_info->commit_root_sem); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 576 | free_excluded_extents(fs_info, block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 577 | mutex_unlock(&caching_ctl->mutex); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 578 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 579 | wake_up(&caching_ctl->wait); |
| 580 | |
| 581 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 582 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 585 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 586 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 587 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 588 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 589 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 590 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 591 | int ret = 0; |
| 592 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 593 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 594 | if (!caching_ctl) |
| 595 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 596 | |
| 597 | INIT_LIST_HEAD(&caching_ctl->list); |
| 598 | mutex_init(&caching_ctl->mutex); |
| 599 | init_waitqueue_head(&caching_ctl->wait); |
| 600 | caching_ctl->block_group = cache; |
| 601 | caching_ctl->progress = cache->key.objectid; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 602 | refcount_set(&caching_ctl->count, 1); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 603 | btrfs_init_work(&caching_ctl->work, btrfs_cache_helper, |
| 604 | caching_thread, NULL, NULL); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 605 | |
| 606 | spin_lock(&cache->lock); |
| 607 | /* |
| 608 | * This should be a rare occasion, but this could happen I think in the |
| 609 | * case where one thread starts to load the space cache info, and then |
| 610 | * some other thread starts a transaction commit which tries to do an |
| 611 | * allocation while the other thread is still loading the space cache |
| 612 | * info. The previous loop should have kept us from choosing this block |
| 613 | * group, but if we've moved to the state where we will wait on caching |
| 614 | * block groups we need to first check if we're doing a fast load here, |
| 615 | * so we can wait for it to finish, otherwise we could end up allocating |
| 616 | * from a block group who's cache gets evicted for one reason or |
| 617 | * another. |
| 618 | */ |
| 619 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 620 | struct btrfs_caching_control *ctl; |
| 621 | |
| 622 | ctl = cache->caching_ctl; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 623 | refcount_inc(&ctl->count); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 624 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 625 | spin_unlock(&cache->lock); |
| 626 | |
| 627 | schedule(); |
| 628 | |
| 629 | finish_wait(&ctl->wait, &wait); |
| 630 | put_caching_control(ctl); |
| 631 | spin_lock(&cache->lock); |
| 632 | } |
| 633 | |
| 634 | if (cache->cached != BTRFS_CACHE_NO) { |
| 635 | spin_unlock(&cache->lock); |
| 636 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 637 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 638 | } |
| 639 | WARN_ON(cache->caching_ctl); |
| 640 | cache->caching_ctl = caching_ctl; |
| 641 | cache->cached = BTRFS_CACHE_FAST; |
| 642 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 643 | |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 644 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 645 | mutex_lock(&caching_ctl->mutex); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 646 | ret = load_free_space_cache(fs_info, cache); |
| 647 | |
| 648 | spin_lock(&cache->lock); |
| 649 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 650 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 651 | cache->cached = BTRFS_CACHE_FINISHED; |
| 652 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 653 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 654 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 655 | if (load_cache_only) { |
| 656 | cache->caching_ctl = NULL; |
| 657 | cache->cached = BTRFS_CACHE_NO; |
| 658 | } else { |
| 659 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 660 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 661 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 662 | } |
| 663 | spin_unlock(&cache->lock); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 664 | #ifdef CONFIG_BTRFS_DEBUG |
| 665 | if (ret == 1 && |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 666 | btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 667 | u64 bytes_used; |
| 668 | |
| 669 | spin_lock(&cache->space_info->lock); |
| 670 | spin_lock(&cache->lock); |
| 671 | bytes_used = cache->key.offset - |
| 672 | btrfs_block_group_used(&cache->item); |
| 673 | cache->space_info->bytes_used += bytes_used >> 1; |
| 674 | spin_unlock(&cache->lock); |
| 675 | spin_unlock(&cache->space_info->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 676 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 677 | } |
| 678 | #endif |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 679 | mutex_unlock(&caching_ctl->mutex); |
| 680 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 681 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 682 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 683 | put_caching_control(caching_ctl); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 684 | free_excluded_extents(fs_info, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 685 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 686 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 687 | } else { |
| 688 | /* |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 689 | * We're either using the free space tree or no caching at all. |
| 690 | * Set cached to the appropriate value and wakeup any waiters. |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 691 | */ |
| 692 | spin_lock(&cache->lock); |
| 693 | if (load_cache_only) { |
| 694 | cache->caching_ctl = NULL; |
| 695 | cache->cached = BTRFS_CACHE_NO; |
| 696 | } else { |
| 697 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 698 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 699 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 700 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 701 | wake_up(&caching_ctl->wait); |
| 702 | } |
| 703 | |
| 704 | if (load_cache_only) { |
| 705 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 706 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 707 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 708 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 709 | down_write(&fs_info->commit_root_sem); |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 710 | refcount_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 711 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 712 | up_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 713 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 714 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 715 | |
Qu Wenruo | e66f0bb | 2014-02-28 10:46:12 +0800 | [diff] [blame] | 716 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 717 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 718 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 719 | } |
| 720 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 721 | /* |
| 722 | * return the block group that starts at or after bytenr |
| 723 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 724 | static struct btrfs_block_group_cache * |
| 725 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 726 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 727 | return block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 728 | } |
| 729 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 730 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 731 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 732 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 733 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 734 | struct btrfs_fs_info *info, |
| 735 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 736 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 737 | return block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 738 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 739 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 740 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 741 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 742 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 743 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 744 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 745 | |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 746 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 747 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 748 | rcu_read_lock(); |
| 749 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 750 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 751 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 752 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 753 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 754 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 755 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 756 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 757 | } |
| 758 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 759 | /* |
| 760 | * after adding space to the filesystem, we need to clear the full flags |
| 761 | * on all the space infos. |
| 762 | */ |
| 763 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 764 | { |
| 765 | struct list_head *head = &info->space_info; |
| 766 | struct btrfs_space_info *found; |
| 767 | |
| 768 | rcu_read_lock(); |
| 769 | list_for_each_entry_rcu(found, head, list) |
| 770 | found->full = 0; |
| 771 | rcu_read_unlock(); |
| 772 | } |
| 773 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 774 | /* 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] | 775 | 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] | 776 | { |
| 777 | int ret; |
| 778 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 779 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 780 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 781 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 782 | if (!path) |
| 783 | return -ENOMEM; |
| 784 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 785 | key.objectid = start; |
| 786 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 787 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 788 | 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] | 789 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 793 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 794 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 795 | * |
| 796 | * the head node for delayed ref is used to store the sum of all the |
| 797 | * reference count modifications queued up in the rbtree. the head |
| 798 | * node may also store the extent flags to set. This way you can check |
| 799 | * to see what the reference count and extent flags would be if all of |
| 800 | * the delayed refs are not processed. |
| 801 | */ |
| 802 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 803 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 804 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 805 | { |
| 806 | struct btrfs_delayed_ref_head *head; |
| 807 | struct btrfs_delayed_ref_root *delayed_refs; |
| 808 | struct btrfs_path *path; |
| 809 | struct btrfs_extent_item *ei; |
| 810 | struct extent_buffer *leaf; |
| 811 | struct btrfs_key key; |
| 812 | u32 item_size; |
| 813 | u64 num_refs; |
| 814 | u64 extent_flags; |
| 815 | int ret; |
| 816 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 817 | /* |
| 818 | * If we don't have skinny metadata, don't bother doing anything |
| 819 | * different |
| 820 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 821 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 822 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 823 | metadata = 0; |
| 824 | } |
| 825 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 826 | path = btrfs_alloc_path(); |
| 827 | if (!path) |
| 828 | return -ENOMEM; |
| 829 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 830 | if (!trans) { |
| 831 | path->skip_locking = 1; |
| 832 | path->search_commit_root = 1; |
| 833 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 834 | |
| 835 | search_again: |
| 836 | key.objectid = bytenr; |
| 837 | key.offset = offset; |
| 838 | if (metadata) |
| 839 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 840 | else |
| 841 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 842 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 843 | 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] | 844 | if (ret < 0) |
| 845 | goto out_free; |
| 846 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 847 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 848 | if (path->slots[0]) { |
| 849 | path->slots[0]--; |
| 850 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 851 | path->slots[0]); |
| 852 | if (key.objectid == bytenr && |
| 853 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 854 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 855 | ret = 0; |
| 856 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 857 | } |
| 858 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 859 | if (ret == 0) { |
| 860 | leaf = path->nodes[0]; |
| 861 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 862 | if (item_size >= sizeof(*ei)) { |
| 863 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 864 | struct btrfs_extent_item); |
| 865 | num_refs = btrfs_extent_refs(leaf, ei); |
| 866 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 867 | } else { |
| 868 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 869 | struct btrfs_extent_item_v0 *ei0; |
| 870 | BUG_ON(item_size != sizeof(*ei0)); |
| 871 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 872 | struct btrfs_extent_item_v0); |
| 873 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 874 | /* FIXME: this isn't correct for data */ |
| 875 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 876 | #else |
| 877 | BUG(); |
| 878 | #endif |
| 879 | } |
| 880 | BUG_ON(num_refs == 0); |
| 881 | } else { |
| 882 | num_refs = 0; |
| 883 | extent_flags = 0; |
| 884 | ret = 0; |
| 885 | } |
| 886 | |
| 887 | if (!trans) |
| 888 | goto out; |
| 889 | |
| 890 | delayed_refs = &trans->transaction->delayed_refs; |
| 891 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 892 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 893 | if (head) { |
| 894 | if (!mutex_trylock(&head->mutex)) { |
Elena Reshetova | 6df8cdf | 2017-03-03 10:55:15 +0200 | [diff] [blame^] | 895 | refcount_inc(&head->node.refs); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 896 | spin_unlock(&delayed_refs->lock); |
| 897 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 898 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 899 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 900 | /* |
| 901 | * Mutex was contended, block until it's released and try |
| 902 | * again |
| 903 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 904 | mutex_lock(&head->mutex); |
| 905 | mutex_unlock(&head->mutex); |
| 906 | btrfs_put_delayed_ref(&head->node); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 907 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 908 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 909 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 910 | if (head->extent_op && head->extent_op->update_flags) |
| 911 | extent_flags |= head->extent_op->flags_to_set; |
| 912 | else |
| 913 | BUG_ON(num_refs == 0); |
| 914 | |
| 915 | num_refs += head->node.ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 916 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 917 | mutex_unlock(&head->mutex); |
| 918 | } |
| 919 | spin_unlock(&delayed_refs->lock); |
| 920 | out: |
| 921 | WARN_ON(num_refs == 0); |
| 922 | if (refs) |
| 923 | *refs = num_refs; |
| 924 | if (flags) |
| 925 | *flags = extent_flags; |
| 926 | out_free: |
| 927 | btrfs_free_path(path); |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 932 | * Back reference rules. Back refs have three main goals: |
| 933 | * |
| 934 | * 1) differentiate between all holders of references to an extent so that |
| 935 | * when a reference is dropped we can make sure it was a valid reference |
| 936 | * before freeing the extent. |
| 937 | * |
| 938 | * 2) Provide enough information to quickly find the holders of an extent |
| 939 | * if we notice a given block is corrupted or bad. |
| 940 | * |
| 941 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 942 | * maintenance. This is actually the same as #2, but with a slightly |
| 943 | * different use case. |
| 944 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 945 | * There are two kinds of back refs. The implicit back refs is optimized |
| 946 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 947 | * back refs of this kind provide information about the block's owner tree |
| 948 | * and the pointer's key. These information allow us to find the block by |
| 949 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 950 | * referenced by their owner trees. The location of tree block is recorded |
| 951 | * in the back refs. Actually the full back refs is generic, and can be |
| 952 | * used in all cases the implicit back refs is used. The major shortcoming |
| 953 | * of the full back refs is its overhead. Every time a tree block gets |
| 954 | * COWed, we have to update back refs entry for all pointers in it. |
| 955 | * |
| 956 | * For a newly allocated tree block, we use implicit back refs for |
| 957 | * pointers in it. This means most tree related operations only involve |
| 958 | * implicit back refs. For a tree block created in old transaction, the |
| 959 | * only way to drop a reference to it is COW it. So we can detect the |
| 960 | * event that tree block loses its owner tree's reference and do the |
| 961 | * back refs conversion. |
| 962 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 963 | * 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] | 964 | * |
| 965 | * The reference count of the block is one and the tree is the block's |
| 966 | * owner tree. Nothing to do in this case. |
| 967 | * |
| 968 | * The reference count of the block is one and the tree is not the |
| 969 | * block's owner tree. In this case, full back refs is used for pointers |
| 970 | * in the block. Remove these full back refs, add implicit back refs for |
| 971 | * every pointers in the new block. |
| 972 | * |
| 973 | * The reference count of the block is greater than one and the tree is |
| 974 | * the block's owner tree. In this case, implicit back refs is used for |
| 975 | * pointers in the block. Add full back refs for every pointers in the |
| 976 | * block, increase lower level extents' reference counts. The original |
| 977 | * implicit back refs are entailed to the new block. |
| 978 | * |
| 979 | * The reference count of the block is greater than one and the tree is |
| 980 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 981 | * the new block, increase lower level extents' reference count. |
| 982 | * |
| 983 | * Back Reference Key composing: |
| 984 | * |
| 985 | * The key objectid corresponds to the first byte in the extent, |
| 986 | * The key type is used to differentiate between types of back refs. |
| 987 | * There are different meanings of the key offset for different types |
| 988 | * of back refs. |
| 989 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 990 | * File extents can be referenced by: |
| 991 | * |
| 992 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 993 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 994 | * - different offsets inside a file (bookend extents in file.c) |
| 995 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 996 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 997 | * |
| 998 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 999 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1000 | * - original offset in the file |
| 1001 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1002 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1003 | * The key offset for the implicit back refs is hash of the first |
| 1004 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1005 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1006 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1007 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1008 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1009 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1010 | * The key offset for the implicit back refs is the first byte of |
| 1011 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1012 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1013 | * When a file extent is allocated, The implicit back refs is used. |
| 1014 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1015 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1016 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 1017 | * |
| 1018 | * When a file extent is removed file truncation, we find the |
| 1019 | * corresponding implicit back refs and check the following fields: |
| 1020 | * |
| 1021 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1022 | * |
| 1023 | * Btree extents can be referenced by: |
| 1024 | * |
| 1025 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1026 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1027 | * Both the implicit back refs and the full back refs for tree blocks |
| 1028 | * only consist of key. The key offset for the implicit back refs is |
| 1029 | * objectid of block's owner tree. The key offset for the full back refs |
| 1030 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1031 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1032 | * When implicit back refs is used, information about the lowest key and |
| 1033 | * level of the tree block are required. These information are stored in |
| 1034 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1035 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1036 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1037 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1038 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1039 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1040 | struct btrfs_path *path, |
| 1041 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1042 | { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1043 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1044 | struct btrfs_extent_item *item; |
| 1045 | struct btrfs_extent_item_v0 *ei0; |
| 1046 | struct btrfs_extent_ref_v0 *ref0; |
| 1047 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1048 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1049 | struct btrfs_key key; |
| 1050 | struct btrfs_key found_key; |
| 1051 | u32 new_size = sizeof(*item); |
| 1052 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1053 | int ret; |
| 1054 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1055 | leaf = path->nodes[0]; |
| 1056 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1057 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1058 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1059 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1060 | struct btrfs_extent_item_v0); |
| 1061 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 1062 | |
| 1063 | if (owner == (u64)-1) { |
| 1064 | while (1) { |
| 1065 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1066 | ret = btrfs_next_leaf(root, path); |
| 1067 | if (ret < 0) |
| 1068 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1069 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1070 | leaf = path->nodes[0]; |
| 1071 | } |
| 1072 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1073 | path->slots[0]); |
| 1074 | BUG_ON(key.objectid != found_key.objectid); |
| 1075 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 1076 | path->slots[0]++; |
| 1077 | continue; |
| 1078 | } |
| 1079 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1080 | struct btrfs_extent_ref_v0); |
| 1081 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 1082 | break; |
| 1083 | } |
| 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 | |
| 1087 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 1088 | new_size += sizeof(*bi); |
| 1089 | |
| 1090 | new_size -= sizeof(*ei0); |
| 1091 | ret = btrfs_search_slot(trans, root, &key, path, |
| 1092 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1093 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1094 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1095 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1096 | |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1097 | btrfs_extend_item(fs_info, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1098 | |
| 1099 | leaf = path->nodes[0]; |
| 1100 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1101 | btrfs_set_extent_refs(leaf, item, refs); |
| 1102 | /* FIXME: get real generation */ |
| 1103 | btrfs_set_extent_generation(leaf, item, 0); |
| 1104 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1105 | btrfs_set_extent_flags(leaf, item, |
| 1106 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1107 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1108 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1109 | /* FIXME: get first key of the block */ |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 1110 | memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1111 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1112 | } else { |
| 1113 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1114 | } |
| 1115 | btrfs_mark_buffer_dirty(leaf); |
| 1116 | return 0; |
| 1117 | } |
| 1118 | #endif |
| 1119 | |
| 1120 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1121 | { |
| 1122 | u32 high_crc = ~(u32)0; |
| 1123 | u32 low_crc = ~(u32)0; |
| 1124 | __le64 lenum; |
| 1125 | |
| 1126 | lenum = cpu_to_le64(root_objectid); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1127 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1128 | lenum = cpu_to_le64(owner); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1129 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | lenum = cpu_to_le64(offset); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1131 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1132 | |
| 1133 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1134 | } |
| 1135 | |
| 1136 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1137 | struct btrfs_extent_data_ref *ref) |
| 1138 | { |
| 1139 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1140 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1141 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1142 | } |
| 1143 | |
| 1144 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1145 | struct btrfs_extent_data_ref *ref, |
| 1146 | u64 root_objectid, u64 owner, u64 offset) |
| 1147 | { |
| 1148 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1149 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1150 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1151 | return 0; |
| 1152 | return 1; |
| 1153 | } |
| 1154 | |
| 1155 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1156 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1157 | struct btrfs_path *path, |
| 1158 | u64 bytenr, u64 parent, |
| 1159 | u64 root_objectid, |
| 1160 | u64 owner, u64 offset) |
| 1161 | { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1162 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1163 | struct btrfs_key key; |
| 1164 | struct btrfs_extent_data_ref *ref; |
| 1165 | struct extent_buffer *leaf; |
| 1166 | u32 nritems; |
| 1167 | int ret; |
| 1168 | int recow; |
| 1169 | int err = -ENOENT; |
| 1170 | |
| 1171 | key.objectid = bytenr; |
| 1172 | if (parent) { |
| 1173 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1174 | key.offset = parent; |
| 1175 | } else { |
| 1176 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1177 | key.offset = hash_extent_data_ref(root_objectid, |
| 1178 | owner, offset); |
| 1179 | } |
| 1180 | again: |
| 1181 | recow = 0; |
| 1182 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1183 | if (ret < 0) { |
| 1184 | err = ret; |
| 1185 | goto fail; |
| 1186 | } |
| 1187 | |
| 1188 | if (parent) { |
| 1189 | if (!ret) |
| 1190 | return 0; |
| 1191 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1192 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1193 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1194 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1195 | if (ret < 0) { |
| 1196 | err = ret; |
| 1197 | goto fail; |
| 1198 | } |
| 1199 | if (!ret) |
| 1200 | return 0; |
| 1201 | #endif |
| 1202 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1206 | nritems = btrfs_header_nritems(leaf); |
| 1207 | while (1) { |
| 1208 | if (path->slots[0] >= nritems) { |
| 1209 | ret = btrfs_next_leaf(root, path); |
| 1210 | if (ret < 0) |
| 1211 | err = ret; |
| 1212 | if (ret) |
| 1213 | goto fail; |
| 1214 | |
| 1215 | leaf = path->nodes[0]; |
| 1216 | nritems = btrfs_header_nritems(leaf); |
| 1217 | recow = 1; |
| 1218 | } |
| 1219 | |
| 1220 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1221 | if (key.objectid != bytenr || |
| 1222 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1223 | goto fail; |
| 1224 | |
| 1225 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1226 | struct btrfs_extent_data_ref); |
| 1227 | |
| 1228 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1229 | owner, offset)) { |
| 1230 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1231 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1232 | goto again; |
| 1233 | } |
| 1234 | err = 0; |
| 1235 | break; |
| 1236 | } |
| 1237 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1238 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1239 | fail: |
| 1240 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1241 | } |
| 1242 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1243 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1244 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1245 | struct btrfs_path *path, |
| 1246 | u64 bytenr, u64 parent, |
| 1247 | u64 root_objectid, u64 owner, |
| 1248 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1249 | { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1250 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1251 | struct btrfs_key key; |
| 1252 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1253 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1254 | u32 num_refs; |
| 1255 | int ret; |
| 1256 | |
| 1257 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1258 | if (parent) { |
| 1259 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1260 | key.offset = parent; |
| 1261 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1262 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1263 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1264 | key.offset = hash_extent_data_ref(root_objectid, |
| 1265 | owner, offset); |
| 1266 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1267 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1268 | |
| 1269 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1270 | if (ret && ret != -EEXIST) |
| 1271 | goto fail; |
| 1272 | |
| 1273 | leaf = path->nodes[0]; |
| 1274 | if (parent) { |
| 1275 | struct btrfs_shared_data_ref *ref; |
| 1276 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1277 | struct btrfs_shared_data_ref); |
| 1278 | if (ret == 0) { |
| 1279 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1280 | } else { |
| 1281 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1282 | num_refs += refs_to_add; |
| 1283 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1284 | } |
| 1285 | } else { |
| 1286 | struct btrfs_extent_data_ref *ref; |
| 1287 | while (ret == -EEXIST) { |
| 1288 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1289 | struct btrfs_extent_data_ref); |
| 1290 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1291 | owner, offset)) |
| 1292 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1293 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1294 | key.offset++; |
| 1295 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1296 | size); |
| 1297 | if (ret && ret != -EEXIST) |
| 1298 | goto fail; |
| 1299 | |
| 1300 | leaf = path->nodes[0]; |
| 1301 | } |
| 1302 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1303 | struct btrfs_extent_data_ref); |
| 1304 | if (ret == 0) { |
| 1305 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1306 | root_objectid); |
| 1307 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1308 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1309 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1310 | } else { |
| 1311 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1312 | num_refs += refs_to_add; |
| 1313 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1314 | } |
| 1315 | } |
| 1316 | btrfs_mark_buffer_dirty(leaf); |
| 1317 | ret = 0; |
| 1318 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1319 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1320 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1321 | } |
| 1322 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1323 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1324 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1325 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1326 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1327 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1328 | struct btrfs_key key; |
| 1329 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1330 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1331 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1332 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1333 | int ret = 0; |
| 1334 | |
| 1335 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1336 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1337 | |
| 1338 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1339 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1340 | struct btrfs_extent_data_ref); |
| 1341 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1342 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1343 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1344 | struct btrfs_shared_data_ref); |
| 1345 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1346 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1347 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1348 | struct btrfs_extent_ref_v0 *ref0; |
| 1349 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1350 | struct btrfs_extent_ref_v0); |
| 1351 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1352 | #endif |
| 1353 | } else { |
| 1354 | BUG(); |
| 1355 | } |
| 1356 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1357 | BUG_ON(num_refs < refs_to_drop); |
| 1358 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1359 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1360 | if (num_refs == 0) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1361 | ret = btrfs_del_item(trans, fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1362 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1363 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1364 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1365 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1366 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1367 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1368 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1369 | else { |
| 1370 | struct btrfs_extent_ref_v0 *ref0; |
| 1371 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1372 | struct btrfs_extent_ref_v0); |
| 1373 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1374 | } |
| 1375 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1376 | btrfs_mark_buffer_dirty(leaf); |
| 1377 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1378 | return ret; |
| 1379 | } |
| 1380 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 1381 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1382 | struct btrfs_extent_inline_ref *iref) |
| 1383 | { |
| 1384 | struct btrfs_key key; |
| 1385 | struct extent_buffer *leaf; |
| 1386 | struct btrfs_extent_data_ref *ref1; |
| 1387 | struct btrfs_shared_data_ref *ref2; |
| 1388 | u32 num_refs = 0; |
| 1389 | |
| 1390 | leaf = path->nodes[0]; |
| 1391 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1392 | if (iref) { |
| 1393 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1394 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1395 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1396 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1397 | } else { |
| 1398 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1399 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1400 | } |
| 1401 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1402 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1403 | struct btrfs_extent_data_ref); |
| 1404 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1405 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1406 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1407 | struct btrfs_shared_data_ref); |
| 1408 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1409 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1410 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1411 | struct btrfs_extent_ref_v0 *ref0; |
| 1412 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1413 | struct btrfs_extent_ref_v0); |
| 1414 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1415 | #endif |
| 1416 | } else { |
| 1417 | WARN_ON(1); |
| 1418 | } |
| 1419 | return num_refs; |
| 1420 | } |
| 1421 | |
| 1422 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1423 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1424 | struct btrfs_path *path, |
| 1425 | u64 bytenr, u64 parent, |
| 1426 | u64 root_objectid) |
| 1427 | { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1428 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1429 | struct btrfs_key key; |
| 1430 | int ret; |
| 1431 | |
| 1432 | key.objectid = bytenr; |
| 1433 | if (parent) { |
| 1434 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1435 | key.offset = parent; |
| 1436 | } else { |
| 1437 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1438 | key.offset = root_objectid; |
| 1439 | } |
| 1440 | |
| 1441 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1442 | if (ret > 0) |
| 1443 | ret = -ENOENT; |
| 1444 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1445 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1446 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1447 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1448 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1449 | if (ret > 0) |
| 1450 | ret = -ENOENT; |
| 1451 | } |
| 1452 | #endif |
| 1453 | return ret; |
| 1454 | } |
| 1455 | |
| 1456 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1457 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1458 | struct btrfs_path *path, |
| 1459 | u64 bytenr, u64 parent, |
| 1460 | u64 root_objectid) |
| 1461 | { |
| 1462 | struct btrfs_key key; |
| 1463 | int ret; |
| 1464 | |
| 1465 | key.objectid = bytenr; |
| 1466 | if (parent) { |
| 1467 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1468 | key.offset = parent; |
| 1469 | } else { |
| 1470 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1471 | key.offset = root_objectid; |
| 1472 | } |
| 1473 | |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1474 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, |
| 1475 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1476 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1477 | return ret; |
| 1478 | } |
| 1479 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1480 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1481 | { |
| 1482 | int type; |
| 1483 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1484 | if (parent > 0) |
| 1485 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1486 | else |
| 1487 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1488 | } else { |
| 1489 | if (parent > 0) |
| 1490 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1491 | else |
| 1492 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1493 | } |
| 1494 | return type; |
| 1495 | } |
| 1496 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1497 | static int find_next_key(struct btrfs_path *path, int level, |
| 1498 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1499 | |
| 1500 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1501 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1502 | if (!path->nodes[level]) |
| 1503 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1504 | if (path->slots[level] + 1 >= |
| 1505 | btrfs_header_nritems(path->nodes[level])) |
| 1506 | continue; |
| 1507 | if (level == 0) |
| 1508 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1509 | path->slots[level] + 1); |
| 1510 | else |
| 1511 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1512 | path->slots[level] + 1); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | return 1; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
| 1519 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1520 | * to the address of inline back ref, and 0 is returned. |
| 1521 | * |
| 1522 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1523 | * should be inserted, and -ENOENT is returned. |
| 1524 | * |
| 1525 | * if insert is true and there are too many inline back refs, the path |
| 1526 | * points to the extent item, and -EAGAIN is returned. |
| 1527 | * |
| 1528 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1529 | * items in the tree are ordered. |
| 1530 | */ |
| 1531 | static noinline_for_stack |
| 1532 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1533 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1534 | struct btrfs_path *path, |
| 1535 | struct btrfs_extent_inline_ref **ref_ret, |
| 1536 | u64 bytenr, u64 num_bytes, |
| 1537 | u64 parent, u64 root_objectid, |
| 1538 | u64 owner, u64 offset, int insert) |
| 1539 | { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1540 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1541 | struct btrfs_key key; |
| 1542 | struct extent_buffer *leaf; |
| 1543 | struct btrfs_extent_item *ei; |
| 1544 | struct btrfs_extent_inline_ref *iref; |
| 1545 | u64 flags; |
| 1546 | u64 item_size; |
| 1547 | unsigned long ptr; |
| 1548 | unsigned long end; |
| 1549 | int extra_size; |
| 1550 | int type; |
| 1551 | int want; |
| 1552 | int ret; |
| 1553 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1554 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1555 | |
| 1556 | key.objectid = bytenr; |
| 1557 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1558 | key.offset = num_bytes; |
| 1559 | |
| 1560 | want = extent_ref_type(parent, owner); |
| 1561 | if (insert) { |
| 1562 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1563 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1564 | } else |
| 1565 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1566 | |
| 1567 | /* |
| 1568 | * Owner is our parent level, so we can just add one to get the level |
| 1569 | * for the block we are interested in. |
| 1570 | */ |
| 1571 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1572 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1573 | key.offset = owner; |
| 1574 | } |
| 1575 | |
| 1576 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1577 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1578 | if (ret < 0) { |
| 1579 | err = ret; |
| 1580 | goto out; |
| 1581 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1582 | |
| 1583 | /* |
| 1584 | * We may be a newly converted file system which still has the old fat |
| 1585 | * extent entries for metadata, so try and see if we have one of those. |
| 1586 | */ |
| 1587 | if (ret > 0 && skinny_metadata) { |
| 1588 | skinny_metadata = false; |
| 1589 | if (path->slots[0]) { |
| 1590 | path->slots[0]--; |
| 1591 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1592 | path->slots[0]); |
| 1593 | if (key.objectid == bytenr && |
| 1594 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1595 | key.offset == num_bytes) |
| 1596 | ret = 0; |
| 1597 | } |
| 1598 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 1599 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1600 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1601 | key.offset = num_bytes; |
| 1602 | btrfs_release_path(path); |
| 1603 | goto again; |
| 1604 | } |
| 1605 | } |
| 1606 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1607 | if (ret && !insert) { |
| 1608 | err = -ENOENT; |
| 1609 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1610 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1611 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1612 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1613 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1614 | |
| 1615 | leaf = path->nodes[0]; |
| 1616 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1617 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1618 | if (item_size < sizeof(*ei)) { |
| 1619 | if (!insert) { |
| 1620 | err = -ENOENT; |
| 1621 | goto out; |
| 1622 | } |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1623 | ret = convert_extent_item_v0(trans, fs_info, path, owner, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1624 | extra_size); |
| 1625 | if (ret < 0) { |
| 1626 | err = ret; |
| 1627 | goto out; |
| 1628 | } |
| 1629 | leaf = path->nodes[0]; |
| 1630 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1631 | } |
| 1632 | #endif |
| 1633 | BUG_ON(item_size < sizeof(*ei)); |
| 1634 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1635 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1636 | flags = btrfs_extent_flags(leaf, ei); |
| 1637 | |
| 1638 | ptr = (unsigned long)(ei + 1); |
| 1639 | end = (unsigned long)ei + item_size; |
| 1640 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1641 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1642 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1643 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | err = -ENOENT; |
| 1647 | while (1) { |
| 1648 | if (ptr >= end) { |
| 1649 | WARN_ON(ptr > end); |
| 1650 | break; |
| 1651 | } |
| 1652 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1653 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1654 | if (want < type) |
| 1655 | break; |
| 1656 | if (want > type) { |
| 1657 | ptr += btrfs_extent_inline_ref_size(type); |
| 1658 | continue; |
| 1659 | } |
| 1660 | |
| 1661 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1662 | struct btrfs_extent_data_ref *dref; |
| 1663 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1664 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1665 | owner, offset)) { |
| 1666 | err = 0; |
| 1667 | break; |
| 1668 | } |
| 1669 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1670 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1671 | break; |
| 1672 | } else { |
| 1673 | u64 ref_offset; |
| 1674 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1675 | if (parent > 0) { |
| 1676 | if (parent == ref_offset) { |
| 1677 | err = 0; |
| 1678 | break; |
| 1679 | } |
| 1680 | if (ref_offset < parent) |
| 1681 | break; |
| 1682 | } else { |
| 1683 | if (root_objectid == ref_offset) { |
| 1684 | err = 0; |
| 1685 | break; |
| 1686 | } |
| 1687 | if (ref_offset < root_objectid) |
| 1688 | break; |
| 1689 | } |
| 1690 | } |
| 1691 | ptr += btrfs_extent_inline_ref_size(type); |
| 1692 | } |
| 1693 | if (err == -ENOENT && insert) { |
| 1694 | if (item_size + extra_size >= |
| 1695 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1696 | err = -EAGAIN; |
| 1697 | goto out; |
| 1698 | } |
| 1699 | /* |
| 1700 | * To add new inline back ref, we have to make sure |
| 1701 | * there is no corresponding back ref item. |
| 1702 | * For simplicity, we just do not add new inline back |
| 1703 | * ref if there is any kind of item for this block |
| 1704 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1705 | if (find_next_key(path, 0, &key) == 0 && |
| 1706 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1707 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1708 | err = -EAGAIN; |
| 1709 | goto out; |
| 1710 | } |
| 1711 | } |
| 1712 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1713 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1714 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1715 | path->keep_locks = 0; |
| 1716 | btrfs_unlock_up_safe(path, 1); |
| 1717 | } |
| 1718 | return err; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * helper to add new inline back ref |
| 1723 | */ |
| 1724 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1725 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1726 | struct btrfs_path *path, |
| 1727 | struct btrfs_extent_inline_ref *iref, |
| 1728 | u64 parent, u64 root_objectid, |
| 1729 | u64 owner, u64 offset, int refs_to_add, |
| 1730 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1731 | { |
| 1732 | struct extent_buffer *leaf; |
| 1733 | struct btrfs_extent_item *ei; |
| 1734 | unsigned long ptr; |
| 1735 | unsigned long end; |
| 1736 | unsigned long item_offset; |
| 1737 | u64 refs; |
| 1738 | int size; |
| 1739 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1740 | |
| 1741 | leaf = path->nodes[0]; |
| 1742 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1743 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1744 | |
| 1745 | type = extent_ref_type(parent, owner); |
| 1746 | size = btrfs_extent_inline_ref_size(type); |
| 1747 | |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1748 | btrfs_extend_item(fs_info, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1749 | |
| 1750 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1751 | refs = btrfs_extent_refs(leaf, ei); |
| 1752 | refs += refs_to_add; |
| 1753 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1754 | if (extent_op) |
| 1755 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1756 | |
| 1757 | ptr = (unsigned long)ei + item_offset; |
| 1758 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1759 | if (ptr < end - size) |
| 1760 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1761 | end - size - ptr); |
| 1762 | |
| 1763 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1764 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1765 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1766 | struct btrfs_extent_data_ref *dref; |
| 1767 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1768 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1769 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1770 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1771 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1772 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1773 | struct btrfs_shared_data_ref *sref; |
| 1774 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1775 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1776 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1777 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1778 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1779 | } else { |
| 1780 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1781 | } |
| 1782 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1786 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1787 | struct btrfs_path *path, |
| 1788 | struct btrfs_extent_inline_ref **ref_ret, |
| 1789 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1790 | u64 root_objectid, u64 owner, u64 offset) |
| 1791 | { |
| 1792 | int ret; |
| 1793 | |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1794 | ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1795 | bytenr, num_bytes, parent, |
| 1796 | root_objectid, owner, offset, 0); |
| 1797 | if (ret != -ENOENT) |
| 1798 | return ret; |
| 1799 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1800 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1801 | *ref_ret = NULL; |
| 1802 | |
| 1803 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1804 | ret = lookup_tree_block_ref(trans, fs_info, path, bytenr, |
| 1805 | parent, root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1806 | } else { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1807 | ret = lookup_extent_data_ref(trans, fs_info, path, bytenr, |
| 1808 | parent, root_objectid, owner, |
| 1809 | offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1810 | } |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * helper to update/remove inline back ref |
| 1816 | */ |
| 1817 | static noinline_for_stack |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1818 | void update_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1819 | struct btrfs_path *path, |
| 1820 | struct btrfs_extent_inline_ref *iref, |
| 1821 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1822 | struct btrfs_delayed_extent_op *extent_op, |
| 1823 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1824 | { |
| 1825 | struct extent_buffer *leaf; |
| 1826 | struct btrfs_extent_item *ei; |
| 1827 | struct btrfs_extent_data_ref *dref = NULL; |
| 1828 | struct btrfs_shared_data_ref *sref = NULL; |
| 1829 | unsigned long ptr; |
| 1830 | unsigned long end; |
| 1831 | u32 item_size; |
| 1832 | int size; |
| 1833 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1834 | u64 refs; |
| 1835 | |
| 1836 | leaf = path->nodes[0]; |
| 1837 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1838 | refs = btrfs_extent_refs(leaf, ei); |
| 1839 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1840 | refs += refs_to_mod; |
| 1841 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1842 | if (extent_op) |
| 1843 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1844 | |
| 1845 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1846 | |
| 1847 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1848 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1849 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1850 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1851 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1852 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1853 | } else { |
| 1854 | refs = 1; |
| 1855 | BUG_ON(refs_to_mod != -1); |
| 1856 | } |
| 1857 | |
| 1858 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1859 | refs += refs_to_mod; |
| 1860 | |
| 1861 | if (refs > 0) { |
| 1862 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1863 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1864 | else |
| 1865 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1866 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1867 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1868 | size = btrfs_extent_inline_ref_size(type); |
| 1869 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1870 | ptr = (unsigned long)iref; |
| 1871 | end = (unsigned long)ei + item_size; |
| 1872 | if (ptr + size < end) |
| 1873 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1874 | end - ptr - size); |
| 1875 | item_size -= size; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1876 | btrfs_truncate_item(fs_info, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1877 | } |
| 1878 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static noinline_for_stack |
| 1882 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1883 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1884 | struct btrfs_path *path, |
| 1885 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1886 | u64 root_objectid, u64 owner, |
| 1887 | u64 offset, int refs_to_add, |
| 1888 | struct btrfs_delayed_extent_op *extent_op) |
| 1889 | { |
| 1890 | struct btrfs_extent_inline_ref *iref; |
| 1891 | int ret; |
| 1892 | |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1893 | ret = lookup_inline_extent_backref(trans, fs_info, path, &iref, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1894 | bytenr, num_bytes, parent, |
| 1895 | root_objectid, owner, offset, 1); |
| 1896 | if (ret == 0) { |
| 1897 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1898 | update_inline_extent_backref(fs_info, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1899 | refs_to_add, extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1900 | } else if (ret == -ENOENT) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1901 | setup_inline_extent_backref(fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1902 | root_objectid, owner, offset, |
| 1903 | refs_to_add, extent_op); |
| 1904 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1905 | } |
| 1906 | return ret; |
| 1907 | } |
| 1908 | |
| 1909 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1910 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1911 | struct btrfs_path *path, |
| 1912 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1913 | u64 owner, u64 offset, int refs_to_add) |
| 1914 | { |
| 1915 | int ret; |
| 1916 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1917 | BUG_ON(refs_to_add != 1); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1918 | ret = insert_tree_block_ref(trans, fs_info, path, bytenr, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1919 | parent, root_objectid); |
| 1920 | } else { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1921 | ret = insert_extent_data_ref(trans, fs_info, path, bytenr, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1922 | parent, root_objectid, |
| 1923 | owner, offset, refs_to_add); |
| 1924 | } |
| 1925 | return ret; |
| 1926 | } |
| 1927 | |
| 1928 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1929 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1930 | struct btrfs_path *path, |
| 1931 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1932 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1933 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1934 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1935 | |
| 1936 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1937 | if (iref) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1938 | update_inline_extent_backref(fs_info, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1939 | -refs_to_drop, NULL, last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1940 | } else if (is_data) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1941 | ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1942 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1943 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1944 | *last_ref = 1; |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1945 | ret = btrfs_del_item(trans, fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1946 | } |
| 1947 | return ret; |
| 1948 | } |
| 1949 | |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1950 | #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len)) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1951 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1952 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1953 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1954 | int j, ret = 0; |
| 1955 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1956 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1957 | |
| 1958 | if (WARN_ON(start != aligned_start)) { |
| 1959 | len -= aligned_start - start; |
| 1960 | len = round_down(len, 1 << 9); |
| 1961 | start = aligned_start; |
| 1962 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1963 | |
| 1964 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1965 | |
| 1966 | if (!len) |
| 1967 | return 0; |
| 1968 | |
| 1969 | end = start + len; |
| 1970 | bytes_left = len; |
| 1971 | |
| 1972 | /* Skip any superblocks on this device. */ |
| 1973 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1974 | u64 sb_start = btrfs_sb_offset(j); |
| 1975 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1976 | u64 size = sb_start - start; |
| 1977 | |
| 1978 | if (!in_range(sb_start, start, bytes_left) && |
| 1979 | !in_range(sb_end, start, bytes_left) && |
| 1980 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1981 | continue; |
| 1982 | |
| 1983 | /* |
| 1984 | * Superblock spans beginning of range. Adjust start and |
| 1985 | * try again. |
| 1986 | */ |
| 1987 | if (sb_start <= start) { |
| 1988 | start += sb_end - start; |
| 1989 | if (start > end) { |
| 1990 | bytes_left = 0; |
| 1991 | break; |
| 1992 | } |
| 1993 | bytes_left = end - start; |
| 1994 | continue; |
| 1995 | } |
| 1996 | |
| 1997 | if (size) { |
| 1998 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1999 | GFP_NOFS, 0); |
| 2000 | if (!ret) |
| 2001 | *discarded_bytes += size; |
| 2002 | else if (ret != -EOPNOTSUPP) |
| 2003 | return ret; |
| 2004 | } |
| 2005 | |
| 2006 | start = sb_end; |
| 2007 | if (start > end) { |
| 2008 | bytes_left = 0; |
| 2009 | break; |
| 2010 | } |
| 2011 | bytes_left = end - start; |
| 2012 | } |
| 2013 | |
| 2014 | if (bytes_left) { |
| 2015 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2016 | GFP_NOFS, 0); |
| 2017 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 2018 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2019 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2020 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2021 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2022 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2023 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 2024 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2025 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2026 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2027 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2028 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2029 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2030 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 2031 | /* |
| 2032 | * Avoid races with device replace and make sure our bbio has devices |
| 2033 | * associated to its stripes that don't go away while we are discarding. |
| 2034 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2035 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2036 | /* Tell the block device(s) that the sectors can be discarded */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2037 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
| 2038 | &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2039 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2040 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2041 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2042 | int i; |
| 2043 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2044 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2045 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2046 | u64 bytes; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2047 | if (!stripe->dev->can_discard) |
| 2048 | continue; |
| 2049 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2050 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 2051 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2052 | stripe->length, |
| 2053 | &bytes); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2054 | if (!ret) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2055 | discarded_bytes += bytes; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2056 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2057 | 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] | 2058 | |
| 2059 | /* |
| 2060 | * Just in case we get back EOPNOTSUPP for some reason, |
| 2061 | * just ignore the return value so we don't screw up |
| 2062 | * people calling discard_extent. |
| 2063 | */ |
| 2064 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2065 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2066 | btrfs_put_bbio(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2067 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2068 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2069 | |
| 2070 | if (actual_bytes) |
| 2071 | *actual_bytes = discarded_bytes; |
| 2072 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2073 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2074 | if (ret == -EOPNOTSUPP) |
| 2075 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2076 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2077 | } |
| 2078 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2079 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2080 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2081 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2082 | u64 bytenr, u64 num_bytes, u64 parent, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2083 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2084 | { |
| 2085 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2086 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2087 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 2088 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2089 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2090 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2091 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 2092 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2093 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2094 | BTRFS_ADD_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2095 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2096 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2097 | num_bytes, parent, root_objectid, |
| 2098 | owner, offset, 0, |
Jeff Mahoney | fef394f | 2016-12-13 14:39:34 -0500 | [diff] [blame] | 2099 | BTRFS_ADD_DELAYED_REF); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2100 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2101 | return ret; |
| 2102 | } |
| 2103 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2104 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2105 | struct btrfs_fs_info *fs_info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2106 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2107 | u64 parent, u64 root_objectid, |
| 2108 | u64 owner, u64 offset, int refs_to_add, |
| 2109 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2110 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2111 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2112 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2113 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2114 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2115 | u64 bytenr = node->bytenr; |
| 2116 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2117 | u64 refs; |
| 2118 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2119 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2120 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2121 | if (!path) |
| 2122 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2123 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2124 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2125 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2126 | /* this will setup the path even if it fails to insert the back ref */ |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 2127 | ret = insert_inline_extent_backref(trans, fs_info, path, bytenr, |
| 2128 | num_bytes, parent, root_objectid, |
| 2129 | owner, offset, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2130 | refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 2131 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2132 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2133 | |
| 2134 | /* |
| 2135 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 2136 | * inline extent ref, so just update the reference count and add a |
| 2137 | * normal backref. |
| 2138 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2139 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2140 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2141 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2142 | refs = btrfs_extent_refs(leaf, item); |
| 2143 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 2144 | if (extent_op) |
| 2145 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2146 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2147 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2148 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2149 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2150 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2151 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2152 | /* now insert the actual backref */ |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 2153 | ret = insert_extent_backref(trans, fs_info, path, bytenr, parent, |
| 2154 | root_objectid, owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2155 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2156 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2157 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2158 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 2159 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2160 | } |
| 2161 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2162 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2163 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2164 | struct btrfs_delayed_ref_node *node, |
| 2165 | struct btrfs_delayed_extent_op *extent_op, |
| 2166 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2167 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2168 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2169 | struct btrfs_delayed_data_ref *ref; |
| 2170 | struct btrfs_key ins; |
| 2171 | u64 parent = 0; |
| 2172 | u64 ref_root = 0; |
| 2173 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2174 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2175 | ins.objectid = node->bytenr; |
| 2176 | ins.offset = node->num_bytes; |
| 2177 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2178 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2179 | ref = btrfs_delayed_node_to_data_ref(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2180 | trace_run_delayed_data_ref(fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2181 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2182 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2183 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2184 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2185 | |
| 2186 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2187 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2188 | flags |= extent_op->flags_to_set; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2189 | ret = alloc_reserved_file_extent(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2190 | parent, ref_root, flags, |
| 2191 | ref->objectid, ref->offset, |
| 2192 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2193 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2194 | ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2195 | ref_root, ref->objectid, |
| 2196 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2197 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2198 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2199 | ret = __btrfs_free_extent(trans, fs_info, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2200 | ref_root, ref->objectid, |
| 2201 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2202 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2203 | } else { |
| 2204 | BUG(); |
| 2205 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2206 | return ret; |
| 2207 | } |
| 2208 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2209 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2210 | struct extent_buffer *leaf, |
| 2211 | struct btrfs_extent_item *ei) |
| 2212 | { |
| 2213 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2214 | if (extent_op->update_flags) { |
| 2215 | flags |= extent_op->flags_to_set; |
| 2216 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2217 | } |
| 2218 | |
| 2219 | if (extent_op->update_key) { |
| 2220 | struct btrfs_tree_block_info *bi; |
| 2221 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2222 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2223 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2228 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2229 | struct btrfs_delayed_ref_node *node, |
| 2230 | struct btrfs_delayed_extent_op *extent_op) |
| 2231 | { |
| 2232 | struct btrfs_key key; |
| 2233 | struct btrfs_path *path; |
| 2234 | struct btrfs_extent_item *ei; |
| 2235 | struct extent_buffer *leaf; |
| 2236 | u32 item_size; |
| 2237 | int ret; |
| 2238 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2239 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2240 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2241 | if (trans->aborted) |
| 2242 | return 0; |
| 2243 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2244 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2245 | metadata = 0; |
| 2246 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2247 | path = btrfs_alloc_path(); |
| 2248 | if (!path) |
| 2249 | return -ENOMEM; |
| 2250 | |
| 2251 | key.objectid = node->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2252 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2253 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2254 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2255 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2256 | } else { |
| 2257 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2258 | key.offset = node->num_bytes; |
| 2259 | } |
| 2260 | |
| 2261 | again: |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2262 | path->reada = READA_FORWARD; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2263 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2264 | 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] | 2265 | if (ret < 0) { |
| 2266 | err = ret; |
| 2267 | goto out; |
| 2268 | } |
| 2269 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2270 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2271 | if (path->slots[0] > 0) { |
| 2272 | path->slots[0]--; |
| 2273 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 2274 | path->slots[0]); |
| 2275 | if (key.objectid == node->bytenr && |
| 2276 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2277 | key.offset == node->num_bytes) |
| 2278 | ret = 0; |
| 2279 | } |
| 2280 | if (ret > 0) { |
| 2281 | btrfs_release_path(path); |
| 2282 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2283 | |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2284 | key.objectid = node->bytenr; |
| 2285 | key.offset = node->num_bytes; |
| 2286 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2287 | goto again; |
| 2288 | } |
| 2289 | } else { |
| 2290 | err = -EIO; |
| 2291 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2292 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | leaf = path->nodes[0]; |
| 2296 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2297 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2298 | if (item_size < sizeof(*ei)) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 2299 | ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2300 | if (ret < 0) { |
| 2301 | err = ret; |
| 2302 | goto out; |
| 2303 | } |
| 2304 | leaf = path->nodes[0]; |
| 2305 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2306 | } |
| 2307 | #endif |
| 2308 | BUG_ON(item_size < sizeof(*ei)); |
| 2309 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2310 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2311 | |
| 2312 | btrfs_mark_buffer_dirty(leaf); |
| 2313 | out: |
| 2314 | btrfs_free_path(path); |
| 2315 | return err; |
| 2316 | } |
| 2317 | |
| 2318 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2319 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2320 | struct btrfs_delayed_ref_node *node, |
| 2321 | struct btrfs_delayed_extent_op *extent_op, |
| 2322 | int insert_reserved) |
| 2323 | { |
| 2324 | int ret = 0; |
| 2325 | struct btrfs_delayed_tree_ref *ref; |
| 2326 | struct btrfs_key ins; |
| 2327 | u64 parent = 0; |
| 2328 | u64 ref_root = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2329 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2330 | |
| 2331 | ref = btrfs_delayed_node_to_tree_ref(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2332 | trace_run_delayed_tree_ref(fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2333 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2334 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2335 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2336 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2337 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2338 | ins.objectid = node->bytenr; |
| 2339 | if (skinny_metadata) { |
| 2340 | ins.offset = ref->level; |
| 2341 | ins.type = BTRFS_METADATA_ITEM_KEY; |
| 2342 | } else { |
| 2343 | ins.offset = node->num_bytes; |
| 2344 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2345 | } |
| 2346 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2347 | if (node->ref_mod != 1) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2348 | btrfs_err(fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2349 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 2350 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 2351 | parent); |
| 2352 | return -EIO; |
| 2353 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2354 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2355 | BUG_ON(!extent_op || !extent_op->update_flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2356 | ret = alloc_reserved_tree_block(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2357 | parent, ref_root, |
| 2358 | extent_op->flags_to_set, |
| 2359 | &extent_op->key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2360 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2361 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2362 | ret = __btrfs_inc_extent_ref(trans, fs_info, node, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2363 | parent, ref_root, |
| 2364 | ref->level, 0, 1, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2365 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2366 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2367 | ret = __btrfs_free_extent(trans, fs_info, node, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2368 | parent, ref_root, |
| 2369 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2370 | } else { |
| 2371 | BUG(); |
| 2372 | } |
| 2373 | return ret; |
| 2374 | } |
| 2375 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2376 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2377 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2378 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2379 | struct btrfs_delayed_ref_node *node, |
| 2380 | struct btrfs_delayed_extent_op *extent_op, |
| 2381 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2382 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2383 | int ret = 0; |
| 2384 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2385 | if (trans->aborted) { |
| 2386 | if (insert_reserved) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2387 | btrfs_pin_extent(fs_info, node->bytenr, |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2388 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2389 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2390 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2391 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2392 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2393 | struct btrfs_delayed_ref_head *head; |
| 2394 | /* |
| 2395 | * we've hit the end of the chain and we were supposed |
| 2396 | * to insert this extent into the tree. But, it got |
| 2397 | * deleted before we ever needed to insert it, so all |
| 2398 | * we have to do is clean up the accounting |
| 2399 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2400 | BUG_ON(extent_op); |
| 2401 | head = btrfs_delayed_node_to_head(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2402 | trace_run_delayed_ref_head(fs_info, node, head, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2403 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2404 | if (insert_reserved) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2405 | btrfs_pin_extent(fs_info, node->bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2406 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2407 | if (head->is_data) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2408 | ret = btrfs_del_csums(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2409 | node->bytenr, |
| 2410 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2411 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2412 | } |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2413 | |
| 2414 | /* Also free its reserved qgroup space */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2415 | btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root, |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2416 | head->qgroup_reserved); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2417 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2418 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2419 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2420 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2421 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2422 | ret = run_delayed_tree_ref(trans, fs_info, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2423 | insert_reserved); |
| 2424 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2425 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2426 | ret = run_delayed_data_ref(trans, fs_info, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2427 | insert_reserved); |
| 2428 | else |
| 2429 | BUG(); |
| 2430 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2431 | } |
| 2432 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2433 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2434 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2435 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2436 | struct btrfs_delayed_ref_node *ref; |
| 2437 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2438 | if (list_empty(&head->ref_list)) |
| 2439 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2440 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2441 | /* |
| 2442 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2443 | * This is to prevent a ref count from going down to zero, which deletes |
| 2444 | * the extent item from the extent tree, when there still are references |
| 2445 | * to add, which would fail because they would not find the extent item. |
| 2446 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2447 | if (!list_empty(&head->ref_add_list)) |
| 2448 | return list_first_entry(&head->ref_add_list, |
| 2449 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2450 | |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2451 | ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node, |
| 2452 | list); |
| 2453 | ASSERT(list_empty(&ref->add_list)); |
| 2454 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2455 | } |
| 2456 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2457 | /* |
| 2458 | * Returns 0 on success or if called with an already aborted transaction. |
| 2459 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2460 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2461 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2462 | struct btrfs_fs_info *fs_info, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2463 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2464 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2465 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2466 | struct btrfs_delayed_ref_node *ref; |
| 2467 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2468 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2469 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2470 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2471 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2472 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2473 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2474 | |
| 2475 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2476 | while (1) { |
| 2477 | if (!locked_ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2478 | if (count >= nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2479 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2480 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2481 | spin_lock(&delayed_refs->lock); |
| 2482 | locked_ref = btrfs_select_ref_head(trans); |
| 2483 | if (!locked_ref) { |
| 2484 | spin_unlock(&delayed_refs->lock); |
| 2485 | break; |
| 2486 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2487 | |
| 2488 | /* grab the lock that says we are going to process |
| 2489 | * all the refs for this head */ |
| 2490 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2491 | spin_unlock(&delayed_refs->lock); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2492 | /* |
| 2493 | * we may have dropped the spin lock to get the head |
| 2494 | * mutex lock, and that might have given someone else |
| 2495 | * time to free the head. If that's true, it has been |
| 2496 | * removed from our list and we can move on. |
| 2497 | */ |
| 2498 | if (ret == -EAGAIN) { |
| 2499 | locked_ref = NULL; |
| 2500 | count++; |
| 2501 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2505 | /* |
| 2506 | * We need to try and merge add/drops of the same ref since we |
| 2507 | * can run into issues with relocate dropping the implicit ref |
| 2508 | * and then it being added back again before the drop can |
| 2509 | * finish. If we merged anything we need to re-loop so we can |
| 2510 | * get a good ref. |
| 2511 | * Or we can get node references of the same type that weren't |
| 2512 | * merged when created due to bumps in the tree mod seq, and |
| 2513 | * we need to merge them to prevent adding an inline extent |
| 2514 | * backref before dropping it (triggering a BUG_ON at |
| 2515 | * insert_inline_extent_backref()). |
| 2516 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2517 | spin_lock(&locked_ref->lock); |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2518 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2519 | locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2520 | |
| 2521 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2522 | * locked_ref is the head node, so we have to go one |
| 2523 | * node back for any delayed ref updates |
| 2524 | */ |
| 2525 | ref = select_delayed_ref(locked_ref); |
| 2526 | |
| 2527 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2528 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2529 | spin_unlock(&locked_ref->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2530 | spin_lock(&delayed_refs->lock); |
| 2531 | locked_ref->processing = 0; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2532 | delayed_refs->num_heads_ready++; |
| 2533 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | d028099 | 2016-12-20 13:28:28 -0500 | [diff] [blame] | 2534 | btrfs_delayed_ref_unlock(locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2535 | locked_ref = NULL; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2536 | cond_resched(); |
Josef Bacik | 27a377d | 2014-02-07 13:57:59 -0500 | [diff] [blame] | 2537 | count++; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2538 | continue; |
| 2539 | } |
| 2540 | |
| 2541 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2542 | * record the must insert reserved flag before we |
| 2543 | * drop the spin lock. |
| 2544 | */ |
| 2545 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2546 | locked_ref->must_insert_reserved = 0; |
| 2547 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2548 | extent_op = locked_ref->extent_op; |
| 2549 | locked_ref->extent_op = NULL; |
| 2550 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2551 | if (!ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2552 | |
| 2553 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2554 | /* All delayed refs have been processed, Go ahead |
| 2555 | * and send the head node to run_one_delayed_ref, |
| 2556 | * so that any accounting fixes can happen |
| 2557 | */ |
| 2558 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2559 | |
| 2560 | if (extent_op && must_insert_reserved) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2561 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2562 | extent_op = NULL; |
| 2563 | } |
| 2564 | |
| 2565 | if (extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2566 | spin_unlock(&locked_ref->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2567 | ret = run_delayed_extent_op(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2568 | ref, extent_op); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2569 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2570 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2571 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2572 | /* |
| 2573 | * Need to reset must_insert_reserved if |
| 2574 | * there was an error so the abort stuff |
| 2575 | * can cleanup the reserved space |
| 2576 | * properly. |
| 2577 | */ |
| 2578 | if (must_insert_reserved) |
| 2579 | locked_ref->must_insert_reserved = 1; |
Jeff Mahoney | aa7c8da | 2016-12-20 13:28:27 -0500 | [diff] [blame] | 2580 | spin_lock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2581 | locked_ref->processing = 0; |
Jeff Mahoney | aa7c8da | 2016-12-20 13:28:27 -0500 | [diff] [blame] | 2582 | delayed_refs->num_heads_ready++; |
| 2583 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2584 | btrfs_debug(fs_info, |
| 2585 | "run_delayed_extent_op returned %d", |
| 2586 | ret); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2587 | btrfs_delayed_ref_unlock(locked_ref); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2588 | return ret; |
| 2589 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2590 | continue; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2591 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2592 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2593 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2594 | * Need to drop our head ref lock and re-acquire the |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2595 | * delayed ref lock and then re-check to make sure |
| 2596 | * nobody got added. |
| 2597 | */ |
| 2598 | spin_unlock(&locked_ref->lock); |
| 2599 | spin_lock(&delayed_refs->lock); |
| 2600 | spin_lock(&locked_ref->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2601 | if (!list_empty(&locked_ref->ref_list) || |
Josef Bacik | 573a075 | 2014-03-27 19:41:34 -0400 | [diff] [blame] | 2602 | locked_ref->extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2603 | spin_unlock(&locked_ref->lock); |
| 2604 | spin_unlock(&delayed_refs->lock); |
| 2605 | continue; |
| 2606 | } |
| 2607 | ref->in_tree = 0; |
| 2608 | delayed_refs->num_heads--; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2609 | rb_erase(&locked_ref->href_node, |
| 2610 | &delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2611 | spin_unlock(&delayed_refs->lock); |
| 2612 | } else { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2613 | actual_count++; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2614 | ref->in_tree = 0; |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2615 | list_del(&ref->list); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2616 | if (!list_empty(&ref->add_list)) |
| 2617 | list_del(&ref->add_list); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2618 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2619 | atomic_dec(&delayed_refs->num_entries); |
| 2620 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2621 | if (!btrfs_delayed_ref_is_head(ref)) { |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2622 | /* |
| 2623 | * when we play the delayed ref, also correct the |
| 2624 | * ref_mod on head |
| 2625 | */ |
| 2626 | switch (ref->action) { |
| 2627 | case BTRFS_ADD_DELAYED_REF: |
| 2628 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2629 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2630 | break; |
| 2631 | case BTRFS_DROP_DELAYED_REF: |
| 2632 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2633 | break; |
| 2634 | default: |
| 2635 | WARN_ON(1); |
| 2636 | } |
| 2637 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2638 | spin_unlock(&locked_ref->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2639 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2640 | ret = run_one_delayed_ref(trans, fs_info, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2641 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2642 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2643 | btrfs_free_delayed_extent_op(extent_op); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2644 | if (ret) { |
Wang Xiaoguang | 9d1032c | 2016-06-20 09:18:52 +0800 | [diff] [blame] | 2645 | spin_lock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2646 | locked_ref->processing = 0; |
Wang Xiaoguang | 9d1032c | 2016-06-20 09:18:52 +0800 | [diff] [blame] | 2647 | delayed_refs->num_heads_ready++; |
| 2648 | spin_unlock(&delayed_refs->lock); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2649 | btrfs_delayed_ref_unlock(locked_ref); |
| 2650 | btrfs_put_delayed_ref(ref); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2651 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 2652 | ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2653 | return ret; |
| 2654 | } |
| 2655 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2656 | /* |
| 2657 | * If this node is a head, that means all the refs in this head |
| 2658 | * have been dealt with, and we will pick the next head to deal |
| 2659 | * with, so we must unlock the head and drop it from the cluster |
| 2660 | * list before we release it. |
| 2661 | */ |
| 2662 | if (btrfs_delayed_ref_is_head(ref)) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2663 | if (locked_ref->is_data && |
| 2664 | locked_ref->total_ref_mod < 0) { |
| 2665 | spin_lock(&delayed_refs->lock); |
| 2666 | delayed_refs->pending_csums -= ref->num_bytes; |
| 2667 | spin_unlock(&delayed_refs->lock); |
| 2668 | } |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2669 | btrfs_delayed_ref_unlock(locked_ref); |
| 2670 | locked_ref = NULL; |
| 2671 | } |
| 2672 | btrfs_put_delayed_ref(ref); |
| 2673 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2674 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2675 | } |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2676 | |
| 2677 | /* |
| 2678 | * We don't want to include ref heads since we can have empty ref heads |
| 2679 | * and those will drastically skew our runtime down since we just do |
| 2680 | * accounting, no actual extent tree updates. |
| 2681 | */ |
| 2682 | if (actual_count > 0) { |
| 2683 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2684 | u64 avg; |
| 2685 | |
| 2686 | /* |
| 2687 | * We weigh the current average higher than our current runtime |
| 2688 | * to avoid large swings in the average. |
| 2689 | */ |
| 2690 | spin_lock(&delayed_refs->lock); |
| 2691 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2692 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2693 | spin_unlock(&delayed_refs->lock); |
| 2694 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2695 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2696 | } |
| 2697 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2698 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2699 | /* |
| 2700 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2701 | * correlates in most cases to the order added. To expose dependencies on this |
| 2702 | * order, we start to process the tree in the middle instead of the beginning |
| 2703 | */ |
| 2704 | static u64 find_middle(struct rb_root *root) |
| 2705 | { |
| 2706 | struct rb_node *n = root->rb_node; |
| 2707 | struct btrfs_delayed_ref_node *entry; |
| 2708 | int alt = 1; |
| 2709 | u64 middle; |
| 2710 | u64 first = 0, last = 0; |
| 2711 | |
| 2712 | n = rb_first(root); |
| 2713 | if (n) { |
| 2714 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2715 | first = entry->bytenr; |
| 2716 | } |
| 2717 | n = rb_last(root); |
| 2718 | if (n) { |
| 2719 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2720 | last = entry->bytenr; |
| 2721 | } |
| 2722 | n = root->rb_node; |
| 2723 | |
| 2724 | while (n) { |
| 2725 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2726 | WARN_ON(!entry->in_tree); |
| 2727 | |
| 2728 | middle = entry->bytenr; |
| 2729 | |
| 2730 | if (alt) |
| 2731 | n = n->rb_left; |
| 2732 | else |
| 2733 | n = n->rb_right; |
| 2734 | |
| 2735 | alt = 1 - alt; |
| 2736 | } |
| 2737 | return middle; |
| 2738 | } |
| 2739 | #endif |
| 2740 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2741 | 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] | 2742 | { |
| 2743 | u64 num_bytes; |
| 2744 | |
| 2745 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2746 | sizeof(struct btrfs_extent_inline_ref)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2747 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2748 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2749 | |
| 2750 | /* |
| 2751 | * 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] | 2752 | * closer to what we're really going to want to use. |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2753 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2754 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2755 | } |
| 2756 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2757 | /* |
| 2758 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2759 | * would require to store the csums for that many bytes. |
| 2760 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2761 | 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] | 2762 | { |
| 2763 | u64 csum_size; |
| 2764 | u64 num_csums_per_leaf; |
| 2765 | u64 num_csums; |
| 2766 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2767 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2768 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2769 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2770 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2771 | num_csums += num_csums_per_leaf - 1; |
| 2772 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2773 | return num_csums; |
| 2774 | } |
| 2775 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2776 | int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2777 | struct btrfs_fs_info *fs_info) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2778 | { |
| 2779 | struct btrfs_block_rsv *global_rsv; |
| 2780 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2781 | u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2782 | u64 num_dirty_bgs = trans->transaction->num_dirty_bgs; |
| 2783 | u64 num_bytes, num_dirty_bgs_bytes; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2784 | int ret = 0; |
| 2785 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2786 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2787 | num_heads = heads_to_leaves(fs_info, num_heads); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2788 | if (num_heads > 1) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2789 | num_bytes += (num_heads - 1) * fs_info->nodesize; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2790 | num_bytes <<= 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2791 | num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) * |
| 2792 | fs_info->nodesize; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2793 | num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info, |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2794 | num_dirty_bgs); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2795 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2796 | |
| 2797 | /* |
| 2798 | * If we can't allocate any more chunks lets make sure we have _lots_ of |
| 2799 | * wiggle room since running delayed refs can create more delayed refs. |
| 2800 | */ |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2801 | if (global_rsv->space_info->full) { |
| 2802 | num_dirty_bgs_bytes <<= 1; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2803 | num_bytes <<= 1; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2804 | } |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2805 | |
| 2806 | spin_lock(&global_rsv->lock); |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2807 | if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2808 | ret = 1; |
| 2809 | spin_unlock(&global_rsv->lock); |
| 2810 | return ret; |
| 2811 | } |
| 2812 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2813 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2814 | struct btrfs_fs_info *fs_info) |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2815 | { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2816 | u64 num_entries = |
| 2817 | atomic_read(&trans->transaction->delayed_refs.num_entries); |
| 2818 | u64 avg_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2819 | u64 val; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2820 | |
| 2821 | smp_mb(); |
| 2822 | avg_runtime = fs_info->avg_delayed_ref_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2823 | val = num_entries * avg_runtime; |
Wang Xiaoguang | dc1a90c | 2016-10-26 15:23:01 +0800 | [diff] [blame] | 2824 | if (val >= NSEC_PER_SEC) |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2825 | return 1; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2826 | if (val >= NSEC_PER_SEC / 2) |
| 2827 | return 2; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2828 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2829 | return btrfs_check_space_for_delayed_refs(trans, fs_info); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2830 | } |
| 2831 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2832 | struct async_delayed_refs { |
| 2833 | struct btrfs_root *root; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2834 | u64 transid; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2835 | int count; |
| 2836 | int error; |
| 2837 | int sync; |
| 2838 | struct completion wait; |
| 2839 | struct btrfs_work work; |
| 2840 | }; |
| 2841 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2842 | static inline struct async_delayed_refs * |
| 2843 | to_async_delayed_refs(struct btrfs_work *work) |
| 2844 | { |
| 2845 | return container_of(work, struct async_delayed_refs, work); |
| 2846 | } |
| 2847 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2848 | static void delayed_ref_async_start(struct btrfs_work *work) |
| 2849 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2850 | struct async_delayed_refs *async = to_async_delayed_refs(work); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2851 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2852 | struct btrfs_fs_info *fs_info = async->root->fs_info; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2853 | int ret; |
| 2854 | |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2855 | /* if the commit is already started, we don't need to wait here */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2856 | if (btrfs_transaction_blocked(fs_info)) |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2857 | goto done; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2858 | |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2859 | trans = btrfs_join_transaction(async->root); |
| 2860 | if (IS_ERR(trans)) { |
| 2861 | async->error = PTR_ERR(trans); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2862 | goto done; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2866 | * trans->sync means that when we call end_transaction, we won't |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2867 | * wait on delayed refs |
| 2868 | */ |
| 2869 | trans->sync = true; |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2870 | |
| 2871 | /* Don't bother flushing if we got into a different transaction */ |
| 2872 | if (trans->transid > async->transid) |
| 2873 | goto end; |
| 2874 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2875 | ret = btrfs_run_delayed_refs(trans, fs_info, async->count); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2876 | if (ret) |
| 2877 | async->error = ret; |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2878 | end: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2879 | ret = btrfs_end_transaction(trans); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2880 | if (ret && !async->error) |
| 2881 | async->error = ret; |
| 2882 | done: |
| 2883 | if (async->sync) |
| 2884 | complete(&async->wait); |
| 2885 | else |
| 2886 | kfree(async); |
| 2887 | } |
| 2888 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2889 | int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info, |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2890 | unsigned long count, u64 transid, int wait) |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2891 | { |
| 2892 | struct async_delayed_refs *async; |
| 2893 | int ret; |
| 2894 | |
| 2895 | async = kmalloc(sizeof(*async), GFP_NOFS); |
| 2896 | if (!async) |
| 2897 | return -ENOMEM; |
| 2898 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2899 | async->root = fs_info->tree_root; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2900 | async->count = count; |
| 2901 | async->error = 0; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2902 | async->transid = transid; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2903 | if (wait) |
| 2904 | async->sync = 1; |
| 2905 | else |
| 2906 | async->sync = 0; |
| 2907 | init_completion(&async->wait); |
| 2908 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2909 | btrfs_init_work(&async->work, btrfs_extent_refs_helper, |
| 2910 | delayed_ref_async_start, NULL, NULL); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2911 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2912 | btrfs_queue_work(fs_info->extent_workers, &async->work); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2913 | |
| 2914 | if (wait) { |
| 2915 | wait_for_completion(&async->wait); |
| 2916 | ret = async->error; |
| 2917 | kfree(async); |
| 2918 | return ret; |
| 2919 | } |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2923 | /* |
| 2924 | * this starts processing the delayed reference count updates and |
| 2925 | * extent insertions we have queued up so far. count can be |
| 2926 | * 0, which means to process everything in the tree at the start |
| 2927 | * of the run (but not newly added entries), or it can be some target |
| 2928 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2929 | * |
| 2930 | * Returns 0 on success or if called with an aborted transaction |
| 2931 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2932 | */ |
| 2933 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2934 | struct btrfs_fs_info *fs_info, unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2935 | { |
| 2936 | struct rb_node *node; |
| 2937 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2938 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2939 | int ret; |
| 2940 | int run_all = count == (unsigned long)-1; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2941 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2942 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2943 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2944 | if (trans->aborted) |
| 2945 | return 0; |
| 2946 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2947 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2948 | return 0; |
| 2949 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2950 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2951 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2952 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2953 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2954 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2955 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2956 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2957 | #endif |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2958 | trans->can_flush_pending_bgs = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2959 | ret = __btrfs_run_delayed_refs(trans, fs_info, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2960 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2961 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2962 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2963 | } |
| 2964 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2965 | if (run_all) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2966 | if (!list_empty(&trans->new_bgs)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2967 | btrfs_create_pending_block_groups(trans, fs_info); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2968 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2969 | spin_lock(&delayed_refs->lock); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2970 | node = rb_first(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2971 | if (!node) { |
| 2972 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2973 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2974 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2975 | |
| 2976 | while (node) { |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2977 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2978 | href_node); |
| 2979 | if (btrfs_delayed_ref_is_head(&head->node)) { |
| 2980 | struct btrfs_delayed_ref_node *ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2981 | |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2982 | ref = &head->node; |
Elena Reshetova | 6df8cdf | 2017-03-03 10:55:15 +0200 | [diff] [blame^] | 2983 | refcount_inc(&ref->refs); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2984 | |
| 2985 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2986 | /* |
| 2987 | * Mutex was contended, block until it's |
| 2988 | * released and try again |
| 2989 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2990 | mutex_lock(&head->mutex); |
| 2991 | mutex_unlock(&head->mutex); |
| 2992 | |
| 2993 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2994 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2995 | goto again; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2996 | } else { |
| 2997 | WARN_ON(1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2998 | } |
| 2999 | node = rb_next(node); |
| 3000 | } |
| 3001 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3002 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3003 | goto again; |
| 3004 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3005 | out: |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 3006 | assert_qgroups_uptodate(trans); |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 3007 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3008 | return 0; |
| 3009 | } |
| 3010 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3011 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3012 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3013 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 3014 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3015 | { |
| 3016 | struct btrfs_delayed_extent_op *extent_op; |
| 3017 | int ret; |
| 3018 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 3019 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3020 | if (!extent_op) |
| 3021 | return -ENOMEM; |
| 3022 | |
| 3023 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 3024 | extent_op->update_flags = true; |
| 3025 | extent_op->update_key = false; |
| 3026 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 3027 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3028 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3029 | ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3030 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3031 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 3032 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3033 | return ret; |
| 3034 | } |
| 3035 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3036 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3037 | struct btrfs_path *path, |
| 3038 | u64 objectid, u64 offset, u64 bytenr) |
| 3039 | { |
| 3040 | struct btrfs_delayed_ref_head *head; |
| 3041 | struct btrfs_delayed_ref_node *ref; |
| 3042 | struct btrfs_delayed_data_ref *data_ref; |
| 3043 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3044 | struct btrfs_transaction *cur_trans; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3045 | int ret = 0; |
| 3046 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3047 | cur_trans = root->fs_info->running_transaction; |
| 3048 | if (!cur_trans) |
| 3049 | return 0; |
| 3050 | |
| 3051 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3052 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 3053 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3054 | if (!head) { |
| 3055 | spin_unlock(&delayed_refs->lock); |
| 3056 | return 0; |
| 3057 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3058 | |
| 3059 | if (!mutex_trylock(&head->mutex)) { |
Elena Reshetova | 6df8cdf | 2017-03-03 10:55:15 +0200 | [diff] [blame^] | 3060 | refcount_inc(&head->node.refs); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3061 | spin_unlock(&delayed_refs->lock); |
| 3062 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3063 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3064 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 3065 | /* |
| 3066 | * Mutex was contended, block until it's released and let |
| 3067 | * caller try again |
| 3068 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3069 | mutex_lock(&head->mutex); |
| 3070 | mutex_unlock(&head->mutex); |
| 3071 | btrfs_put_delayed_ref(&head->node); |
| 3072 | return -EAGAIN; |
| 3073 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3074 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3075 | |
| 3076 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 3077 | list_for_each_entry(ref, &head->ref_list, list) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3078 | /* If it's a shared ref we know a cross reference exists */ |
| 3079 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 3080 | ret = 1; |
| 3081 | break; |
| 3082 | } |
| 3083 | |
| 3084 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 3085 | |
| 3086 | /* |
| 3087 | * If our ref doesn't match the one we're currently looking at |
| 3088 | * then we have a cross reference. |
| 3089 | */ |
| 3090 | if (data_ref->root != root->root_key.objectid || |
| 3091 | data_ref->objectid != objectid || |
| 3092 | data_ref->offset != offset) { |
| 3093 | ret = 1; |
| 3094 | break; |
| 3095 | } |
| 3096 | } |
| 3097 | spin_unlock(&head->lock); |
| 3098 | mutex_unlock(&head->mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3099 | return ret; |
| 3100 | } |
| 3101 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3102 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3103 | struct btrfs_path *path, |
| 3104 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3105 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3106 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3107 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3108 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3109 | struct btrfs_extent_data_ref *ref; |
| 3110 | struct btrfs_extent_inline_ref *iref; |
| 3111 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3112 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3113 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3114 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3115 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3116 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3117 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3118 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3119 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3120 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 3121 | if (ret < 0) |
| 3122 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3123 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 3124 | |
| 3125 | ret = -ENOENT; |
| 3126 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3127 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3128 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3129 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3130 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3131 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3132 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3133 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3134 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3135 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3136 | ret = 1; |
| 3137 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 3138 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3139 | if (item_size < sizeof(*ei)) { |
| 3140 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 3141 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3142 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3143 | #endif |
| 3144 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 3145 | |
| 3146 | if (item_size != sizeof(*ei) + |
| 3147 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 3148 | goto out; |
| 3149 | |
| 3150 | if (btrfs_extent_generation(leaf, ei) <= |
| 3151 | btrfs_root_last_snapshot(&root->root_item)) |
| 3152 | goto out; |
| 3153 | |
| 3154 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 3155 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 3156 | BTRFS_EXTENT_DATA_REF_KEY) |
| 3157 | goto out; |
| 3158 | |
| 3159 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3160 | if (btrfs_extent_refs(leaf, ei) != |
| 3161 | btrfs_extent_data_ref_count(leaf, ref) || |
| 3162 | btrfs_extent_data_ref_root(leaf, ref) != |
| 3163 | root->root_key.objectid || |
| 3164 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 3165 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 3166 | goto out; |
| 3167 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3168 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3169 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3170 | return ret; |
| 3171 | } |
| 3172 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3173 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
| 3174 | u64 bytenr) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3175 | { |
| 3176 | struct btrfs_path *path; |
| 3177 | int ret; |
| 3178 | int ret2; |
| 3179 | |
| 3180 | path = btrfs_alloc_path(); |
| 3181 | if (!path) |
| 3182 | return -ENOENT; |
| 3183 | |
| 3184 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3185 | ret = check_committed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3186 | offset, bytenr); |
| 3187 | if (ret && ret != -ENOENT) |
| 3188 | goto out; |
| 3189 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3190 | ret2 = check_delayed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3191 | offset, bytenr); |
| 3192 | } while (ret2 == -EAGAIN); |
| 3193 | |
| 3194 | if (ret2 && ret2 != -ENOENT) { |
| 3195 | ret = ret2; |
| 3196 | goto out; |
| 3197 | } |
| 3198 | |
| 3199 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 3200 | ret = 0; |
| 3201 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3202 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3203 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 3204 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3205 | return ret; |
| 3206 | } |
| 3207 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3208 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3209 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3210 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3211 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3212 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3213 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3214 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3215 | u64 num_bytes; |
| 3216 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3217 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3218 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3219 | struct btrfs_key key; |
| 3220 | struct btrfs_file_extent_item *fi; |
| 3221 | int i; |
| 3222 | int level; |
| 3223 | int ret = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3224 | int (*process_func)(struct btrfs_trans_handle *, |
| 3225 | struct btrfs_fs_info *, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3226 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3227 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3228 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3229 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3230 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3231 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3232 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3233 | nritems = btrfs_header_nritems(buf); |
| 3234 | level = btrfs_header_level(buf); |
| 3235 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3236 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3237 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3238 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3239 | if (inc) |
| 3240 | process_func = btrfs_inc_extent_ref; |
| 3241 | else |
| 3242 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3243 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3244 | if (full_backref) |
| 3245 | parent = buf->start; |
| 3246 | else |
| 3247 | parent = 0; |
| 3248 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3249 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3250 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3251 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3252 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3253 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3254 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3255 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3256 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3257 | BTRFS_FILE_EXTENT_INLINE) |
| 3258 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3259 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 3260 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3261 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3262 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3263 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 3264 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3265 | ret = process_func(trans, fs_info, bytenr, num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3266 | parent, ref_root, key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3267 | key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3268 | if (ret) |
| 3269 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3270 | } else { |
| 3271 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3272 | num_bytes = fs_info->nodesize; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3273 | ret = process_func(trans, fs_info, bytenr, num_bytes, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3274 | parent, ref_root, level - 1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3275 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3276 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3277 | } |
| 3278 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3279 | return 0; |
| 3280 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3281 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3282 | } |
| 3283 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3284 | 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] | 3285 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3286 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3287 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3288 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3289 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3290 | 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] | 3291 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3292 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3293 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3294 | } |
| 3295 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3296 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3297 | struct btrfs_fs_info *fs_info, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3298 | struct btrfs_path *path, |
| 3299 | struct btrfs_block_group_cache *cache) |
| 3300 | { |
| 3301 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3302 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3303 | unsigned long bi; |
| 3304 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3305 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3306 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3307 | if (ret) { |
| 3308 | if (ret > 0) |
| 3309 | ret = -ENOENT; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3310 | goto fail; |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3311 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3312 | |
| 3313 | leaf = path->nodes[0]; |
| 3314 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3315 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3316 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3317 | fail: |
Filipe Manana | 24b89d0 | 2015-04-25 18:31:05 +0100 | [diff] [blame] | 3318 | btrfs_release_path(path); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3319 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3320 | |
| 3321 | } |
| 3322 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3323 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3324 | next_block_group(struct btrfs_fs_info *fs_info, |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3325 | struct btrfs_block_group_cache *cache) |
| 3326 | { |
| 3327 | struct rb_node *node; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3328 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3329 | spin_lock(&fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3330 | |
| 3331 | /* If our block group was removed, we need a full search. */ |
| 3332 | if (RB_EMPTY_NODE(&cache->cache_node)) { |
| 3333 | const u64 next_bytenr = cache->key.objectid + cache->key.offset; |
| 3334 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3335 | spin_unlock(&fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3336 | btrfs_put_block_group(cache); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3337 | cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3338 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3339 | node = rb_next(&cache->cache_node); |
| 3340 | btrfs_put_block_group(cache); |
| 3341 | if (node) { |
| 3342 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 3343 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 3344 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3345 | } else |
| 3346 | cache = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3347 | spin_unlock(&fs_info->block_group_cache_lock); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3348 | return cache; |
| 3349 | } |
| 3350 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3351 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3352 | struct btrfs_trans_handle *trans, |
| 3353 | struct btrfs_path *path) |
| 3354 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3355 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 3356 | struct btrfs_root *root = fs_info->tree_root; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3357 | struct inode *inode = NULL; |
| 3358 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3359 | int dcs = BTRFS_DC_ERROR; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 3360 | u64 num_pages = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3361 | int retries = 0; |
| 3362 | int ret = 0; |
| 3363 | |
| 3364 | /* |
| 3365 | * If this block group is smaller than 100 megs don't bother caching the |
| 3366 | * block group. |
| 3367 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3368 | if (block_group->key.offset < (100 * SZ_1M)) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3369 | spin_lock(&block_group->lock); |
| 3370 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3371 | spin_unlock(&block_group->lock); |
| 3372 | return 0; |
| 3373 | } |
| 3374 | |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3375 | if (trans->aborted) |
| 3376 | return 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3377 | again: |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3378 | inode = lookup_free_space_inode(fs_info, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3379 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3380 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3381 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3382 | goto out; |
| 3383 | } |
| 3384 | |
| 3385 | if (IS_ERR(inode)) { |
| 3386 | BUG_ON(retries); |
| 3387 | retries++; |
| 3388 | |
| 3389 | if (block_group->ro) |
| 3390 | goto out_free; |
| 3391 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3392 | ret = create_free_space_inode(fs_info, trans, block_group, |
| 3393 | path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3394 | if (ret) |
| 3395 | goto out_free; |
| 3396 | goto again; |
| 3397 | } |
| 3398 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3399 | /* We've already setup this transaction, go ahead and exit */ |
| 3400 | if (block_group->cache_generation == trans->transid && |
| 3401 | i_size_read(inode)) { |
| 3402 | dcs = BTRFS_DC_SETUP; |
| 3403 | goto out_put; |
| 3404 | } |
| 3405 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3406 | /* |
| 3407 | * We want to set the generation to 0, that way if anything goes wrong |
| 3408 | * from here on out we know not to trust this cache when we load up next |
| 3409 | * time. |
| 3410 | */ |
| 3411 | BTRFS_I(inode)->generation = 0; |
| 3412 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3413 | if (ret) { |
| 3414 | /* |
| 3415 | * So theoretically we could recover from this, simply set the |
| 3416 | * super cache generation to 0 so we know to invalidate the |
| 3417 | * cache, but then we'd have to keep track of the block groups |
| 3418 | * that fail this way so we know we _have_ to reset this cache |
| 3419 | * before the next commit or risk reading stale cache. So to |
| 3420 | * limit our exposure to horrible edge cases lets just abort the |
| 3421 | * transaction, this only happens in really bad situations |
| 3422 | * anyway. |
| 3423 | */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3424 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3425 | goto out_put; |
| 3426 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3427 | WARN_ON(ret); |
| 3428 | |
| 3429 | if (i_size_read(inode) > 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3430 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3431 | &fs_info->global_block_rsv); |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3432 | if (ret) |
| 3433 | goto out_put; |
| 3434 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3435 | ret = btrfs_truncate_free_space_cache(trans, NULL, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3436 | if (ret) |
| 3437 | goto out_put; |
| 3438 | } |
| 3439 | |
| 3440 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3441 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3442 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3443 | /* |
| 3444 | * don't bother trying to write stuff out _if_ |
| 3445 | * a) we're not cached, |
| 3446 | * b) we're with nospace_cache mount option. |
| 3447 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3448 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3449 | spin_unlock(&block_group->lock); |
| 3450 | goto out_put; |
| 3451 | } |
| 3452 | spin_unlock(&block_group->lock); |
| 3453 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3454 | /* |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3455 | * We hit an ENOSPC when setting up the cache in this transaction, just |
| 3456 | * skip doing the setup, we've already cleared the cache so we're safe. |
| 3457 | */ |
| 3458 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { |
| 3459 | ret = -ENOSPC; |
| 3460 | goto out_put; |
| 3461 | } |
| 3462 | |
| 3463 | /* |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3464 | * Try to preallocate enough space based on how big the block group is. |
| 3465 | * Keep in mind this has to include any pinned space which could end up |
| 3466 | * taking up quite a bit since it's not folded into the other space |
| 3467 | * cache. |
| 3468 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3469 | num_pages = div_u64(block_group->key.offset, SZ_256M); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3470 | if (!num_pages) |
| 3471 | num_pages = 1; |
| 3472 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3473 | num_pages *= 16; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3474 | num_pages *= PAGE_SIZE; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3475 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 3476 | ret = btrfs_check_data_free_space(inode, 0, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3477 | if (ret) |
| 3478 | goto out_put; |
| 3479 | |
| 3480 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3481 | num_pages, num_pages, |
| 3482 | &alloc_hint); |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3483 | /* |
| 3484 | * Our cache requires contiguous chunks so that we don't modify a bunch |
| 3485 | * of metadata or split extents when writing the cache out, which means |
| 3486 | * we can enospc if we are heavily fragmented in addition to just normal |
| 3487 | * out of space conditions. So if we hit this just skip setting up any |
| 3488 | * other block groups for this transaction, maybe we'll unpin enough |
| 3489 | * space the next time around. |
| 3490 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3491 | if (!ret) |
| 3492 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3493 | else if (ret == -ENOSPC) |
| 3494 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3495 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3496 | out_put: |
| 3497 | iput(inode); |
| 3498 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3499 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3500 | out: |
| 3501 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3502 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3503 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3504 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3505 | spin_unlock(&block_group->lock); |
| 3506 | |
| 3507 | return ret; |
| 3508 | } |
| 3509 | |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3510 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3511 | struct btrfs_fs_info *fs_info) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3512 | { |
| 3513 | struct btrfs_block_group_cache *cache, *tmp; |
| 3514 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3515 | struct btrfs_path *path; |
| 3516 | |
| 3517 | if (list_empty(&cur_trans->dirty_bgs) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3518 | !btrfs_test_opt(fs_info, SPACE_CACHE)) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3519 | return 0; |
| 3520 | |
| 3521 | path = btrfs_alloc_path(); |
| 3522 | if (!path) |
| 3523 | return -ENOMEM; |
| 3524 | |
| 3525 | /* Could add new block groups, use _safe just in case */ |
| 3526 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, |
| 3527 | dirty_list) { |
| 3528 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3529 | cache_save_setup(cache, trans, path); |
| 3530 | } |
| 3531 | |
| 3532 | btrfs_free_path(path); |
| 3533 | return 0; |
| 3534 | } |
| 3535 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3536 | /* |
| 3537 | * transaction commit does final block group cache writeback during a |
| 3538 | * critical section where nothing is allowed to change the FS. This is |
| 3539 | * required in order for the cache to actually match the block group, |
| 3540 | * but can introduce a lot of latency into the commit. |
| 3541 | * |
| 3542 | * So, btrfs_start_dirty_block_groups is here to kick off block group |
| 3543 | * cache IO. There's a chance we'll have to redo some of it if the |
| 3544 | * block group changes again during the commit, but it greatly reduces |
| 3545 | * the commit latency by getting rid of the easy block groups while |
| 3546 | * we're still allowing others to join the commit. |
| 3547 | */ |
| 3548 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3549 | struct btrfs_fs_info *fs_info) |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3550 | { |
| 3551 | struct btrfs_block_group_cache *cache; |
| 3552 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3553 | int ret = 0; |
| 3554 | int should_put; |
| 3555 | struct btrfs_path *path = NULL; |
| 3556 | LIST_HEAD(dirty); |
| 3557 | struct list_head *io = &cur_trans->io_bgs; |
| 3558 | int num_started = 0; |
| 3559 | int loops = 0; |
| 3560 | |
| 3561 | spin_lock(&cur_trans->dirty_bgs_lock); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3562 | if (list_empty(&cur_trans->dirty_bgs)) { |
| 3563 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3564 | return 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3565 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3566 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3567 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3568 | |
| 3569 | again: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3570 | /* |
| 3571 | * make sure all the block groups on our dirty list actually |
| 3572 | * exist |
| 3573 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3574 | btrfs_create_pending_block_groups(trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3575 | |
| 3576 | if (!path) { |
| 3577 | path = btrfs_alloc_path(); |
| 3578 | if (!path) |
| 3579 | return -ENOMEM; |
| 3580 | } |
| 3581 | |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3582 | /* |
| 3583 | * cache_write_mutex is here only to save us from balance or automatic |
| 3584 | * removal of empty block groups deleting this block group while we are |
| 3585 | * writing out the cache |
| 3586 | */ |
| 3587 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3588 | while (!list_empty(&dirty)) { |
| 3589 | cache = list_first_entry(&dirty, |
| 3590 | struct btrfs_block_group_cache, |
| 3591 | dirty_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3592 | /* |
| 3593 | * this can happen if something re-dirties a block |
| 3594 | * group that is already under IO. Just wait for it to |
| 3595 | * finish and then do it all again |
| 3596 | */ |
| 3597 | if (!list_empty(&cache->io_list)) { |
| 3598 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3599 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3600 | btrfs_put_block_group(cache); |
| 3601 | } |
| 3602 | |
| 3603 | |
| 3604 | /* |
| 3605 | * btrfs_wait_cache_io uses the cache->dirty_list to decide |
| 3606 | * if it should update the cache_state. Don't delete |
| 3607 | * until after we wait. |
| 3608 | * |
| 3609 | * Since we're not running in the commit critical section |
| 3610 | * we need the dirty_bgs_lock to protect from update_block_group |
| 3611 | */ |
| 3612 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3613 | list_del_init(&cache->dirty_list); |
| 3614 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3615 | |
| 3616 | should_put = 1; |
| 3617 | |
| 3618 | cache_save_setup(cache, trans, path); |
| 3619 | |
| 3620 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3621 | cache->io_ctl.inode = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3622 | ret = btrfs_write_out_cache(fs_info, trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3623 | cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3624 | if (ret == 0 && cache->io_ctl.inode) { |
| 3625 | num_started++; |
| 3626 | should_put = 0; |
| 3627 | |
| 3628 | /* |
| 3629 | * the cache_write_mutex is protecting |
| 3630 | * the io_list |
| 3631 | */ |
| 3632 | list_add_tail(&cache->io_list, io); |
| 3633 | } else { |
| 3634 | /* |
| 3635 | * if we failed to write the cache, the |
| 3636 | * generation will be bad and life goes on |
| 3637 | */ |
| 3638 | ret = 0; |
| 3639 | } |
| 3640 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3641 | if (!ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3642 | ret = write_one_cache_group(trans, fs_info, |
| 3643 | path, cache); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3644 | /* |
| 3645 | * Our block group might still be attached to the list |
| 3646 | * of new block groups in the transaction handle of some |
| 3647 | * other task (struct btrfs_trans_handle->new_bgs). This |
| 3648 | * means its block group item isn't yet in the extent |
| 3649 | * tree. If this happens ignore the error, as we will |
| 3650 | * try again later in the critical section of the |
| 3651 | * transaction commit. |
| 3652 | */ |
| 3653 | if (ret == -ENOENT) { |
| 3654 | ret = 0; |
| 3655 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3656 | if (list_empty(&cache->dirty_list)) { |
| 3657 | list_add_tail(&cache->dirty_list, |
| 3658 | &cur_trans->dirty_bgs); |
| 3659 | btrfs_get_block_group(cache); |
| 3660 | } |
| 3661 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3662 | } else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3663 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3664 | } |
| 3665 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3666 | |
| 3667 | /* if its not on the io list, we need to put the block group */ |
| 3668 | if (should_put) |
| 3669 | btrfs_put_block_group(cache); |
| 3670 | |
| 3671 | if (ret) |
| 3672 | break; |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3673 | |
| 3674 | /* |
| 3675 | * Avoid blocking other tasks for too long. It might even save |
| 3676 | * us from writing caches for block groups that are going to be |
| 3677 | * removed. |
| 3678 | */ |
| 3679 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 3680 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3681 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3682 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3683 | |
| 3684 | /* |
| 3685 | * go through delayed refs for all the stuff we've just kicked off |
| 3686 | * and then loop back (just once) |
| 3687 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3688 | ret = btrfs_run_delayed_refs(trans, fs_info, 0); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3689 | if (!ret && loops == 0) { |
| 3690 | loops++; |
| 3691 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3692 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3693 | /* |
| 3694 | * dirty_bgs_lock protects us from concurrent block group |
| 3695 | * deletes too (not just cache_write_mutex). |
| 3696 | */ |
| 3697 | if (!list_empty(&dirty)) { |
| 3698 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3699 | goto again; |
| 3700 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3701 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Liu Bo | c79a175 | 2016-07-20 17:44:12 -0700 | [diff] [blame] | 3702 | } else if (ret < 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3703 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | btrfs_free_path(path); |
| 3707 | return ret; |
| 3708 | } |
| 3709 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3710 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3711 | struct btrfs_fs_info *fs_info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3712 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3713 | struct btrfs_block_group_cache *cache; |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3714 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3715 | int ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3716 | int should_put; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3717 | struct btrfs_path *path; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3718 | struct list_head *io = &cur_trans->io_bgs; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3719 | int num_started = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3720 | |
| 3721 | path = btrfs_alloc_path(); |
| 3722 | if (!path) |
| 3723 | return -ENOMEM; |
| 3724 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3725 | /* |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3726 | * Even though we are in the critical section of the transaction commit, |
| 3727 | * we can still have concurrent tasks adding elements to this |
| 3728 | * transaction's list of dirty block groups. These tasks correspond to |
| 3729 | * endio free space workers started when writeback finishes for a |
| 3730 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can |
| 3731 | * allocate new block groups as a result of COWing nodes of the root |
| 3732 | * tree when updating the free space inode. The writeback for the space |
| 3733 | * caches is triggered by an earlier call to |
| 3734 | * btrfs_start_dirty_block_groups() and iterations of the following |
| 3735 | * loop. |
| 3736 | * Also we want to do the cache_save_setup first and then run the |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3737 | * delayed refs to make sure we have the best chance at doing this all |
| 3738 | * in one shot. |
| 3739 | */ |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3740 | spin_lock(&cur_trans->dirty_bgs_lock); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3741 | while (!list_empty(&cur_trans->dirty_bgs)) { |
| 3742 | cache = list_first_entry(&cur_trans->dirty_bgs, |
| 3743 | struct btrfs_block_group_cache, |
| 3744 | dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3745 | |
| 3746 | /* |
| 3747 | * this can happen if cache_save_setup re-dirties a block |
| 3748 | * group that is already under IO. Just wait for it to |
| 3749 | * finish and then do it all again |
| 3750 | */ |
| 3751 | if (!list_empty(&cache->io_list)) { |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3752 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3753 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3754 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3755 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3756 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3759 | /* |
| 3760 | * don't remove from the dirty list until after we've waited |
| 3761 | * on any pending IO |
| 3762 | */ |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3763 | list_del_init(&cache->dirty_list); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3764 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3765 | should_put = 1; |
| 3766 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3767 | cache_save_setup(cache, trans, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3768 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3769 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3770 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 3771 | (unsigned long) -1); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3772 | |
| 3773 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3774 | cache->io_ctl.inode = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3775 | ret = btrfs_write_out_cache(fs_info, trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3776 | cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3777 | if (ret == 0 && cache->io_ctl.inode) { |
| 3778 | num_started++; |
| 3779 | should_put = 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3780 | list_add_tail(&cache->io_list, io); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3781 | } else { |
| 3782 | /* |
| 3783 | * if we failed to write the cache, the |
| 3784 | * generation will be bad and life goes on |
| 3785 | */ |
| 3786 | ret = 0; |
| 3787 | } |
| 3788 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3789 | if (!ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3790 | ret = write_one_cache_group(trans, fs_info, |
| 3791 | path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3792 | /* |
| 3793 | * One of the free space endio workers might have |
| 3794 | * created a new block group while updating a free space |
| 3795 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) |
| 3796 | * and hasn't released its transaction handle yet, in |
| 3797 | * which case the new block group is still attached to |
| 3798 | * its transaction handle and its creation has not |
| 3799 | * finished yet (no block group item in the extent tree |
| 3800 | * yet, etc). If this is the case, wait for all free |
| 3801 | * space endio workers to finish and retry. This is a |
| 3802 | * a very rare case so no need for a more efficient and |
| 3803 | * complex approach. |
| 3804 | */ |
| 3805 | if (ret == -ENOENT) { |
| 3806 | wait_event(cur_trans->writer_wait, |
| 3807 | atomic_read(&cur_trans->num_writers) == 1); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3808 | ret = write_one_cache_group(trans, fs_info, |
| 3809 | path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3810 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3811 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3812 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3813 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3814 | |
| 3815 | /* if its not on the io list, we need to put the block group */ |
| 3816 | if (should_put) |
| 3817 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3818 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3819 | } |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3820 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3821 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3822 | while (!list_empty(io)) { |
| 3823 | cache = list_first_entry(io, struct btrfs_block_group_cache, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3824 | io_list); |
| 3825 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3826 | btrfs_wait_cache_io(trans, cache, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3827 | btrfs_put_block_group(cache); |
| 3828 | } |
| 3829 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3830 | btrfs_free_path(path); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3831 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3832 | } |
| 3833 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3834 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3835 | { |
| 3836 | struct btrfs_block_group_cache *block_group; |
| 3837 | int readonly = 0; |
| 3838 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3839 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3840 | if (!block_group || block_group->ro) |
| 3841 | readonly = 1; |
| 3842 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3843 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3844 | return readonly; |
| 3845 | } |
| 3846 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 3847 | bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 3848 | { |
| 3849 | struct btrfs_block_group_cache *bg; |
| 3850 | bool ret = true; |
| 3851 | |
| 3852 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 3853 | if (!bg) |
| 3854 | return false; |
| 3855 | |
| 3856 | spin_lock(&bg->lock); |
| 3857 | if (bg->ro) |
| 3858 | ret = false; |
| 3859 | else |
| 3860 | atomic_inc(&bg->nocow_writers); |
| 3861 | spin_unlock(&bg->lock); |
| 3862 | |
| 3863 | /* no put on block group, done by btrfs_dec_nocow_writers */ |
| 3864 | if (!ret) |
| 3865 | btrfs_put_block_group(bg); |
| 3866 | |
| 3867 | return ret; |
| 3868 | |
| 3869 | } |
| 3870 | |
| 3871 | void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 3872 | { |
| 3873 | struct btrfs_block_group_cache *bg; |
| 3874 | |
| 3875 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 3876 | ASSERT(bg); |
| 3877 | if (atomic_dec_and_test(&bg->nocow_writers)) |
| 3878 | wake_up_atomic_t(&bg->nocow_writers); |
| 3879 | /* |
| 3880 | * Once for our lookup and once for the lookup done by a previous call |
| 3881 | * to btrfs_inc_nocow_writers() |
| 3882 | */ |
| 3883 | btrfs_put_block_group(bg); |
| 3884 | btrfs_put_block_group(bg); |
| 3885 | } |
| 3886 | |
| 3887 | static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a) |
| 3888 | { |
| 3889 | schedule(); |
| 3890 | return 0; |
| 3891 | } |
| 3892 | |
| 3893 | void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg) |
| 3894 | { |
| 3895 | wait_on_atomic_t(&bg->nocow_writers, |
| 3896 | btrfs_wait_nocow_writers_atomic_t, |
| 3897 | TASK_UNINTERRUPTIBLE); |
| 3898 | } |
| 3899 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3900 | static const char *alloc_name(u64 flags) |
| 3901 | { |
| 3902 | switch (flags) { |
| 3903 | case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA: |
| 3904 | return "mixed"; |
| 3905 | case BTRFS_BLOCK_GROUP_METADATA: |
| 3906 | return "metadata"; |
| 3907 | case BTRFS_BLOCK_GROUP_DATA: |
| 3908 | return "data"; |
| 3909 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 3910 | return "system"; |
| 3911 | default: |
| 3912 | WARN_ON(1); |
| 3913 | return "invalid-combination"; |
| 3914 | }; |
| 3915 | } |
| 3916 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3917 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3918 | u64 total_bytes, u64 bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3919 | u64 bytes_readonly, |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3920 | struct btrfs_space_info **space_info) |
| 3921 | { |
| 3922 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3923 | int i; |
| 3924 | int factor; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3925 | int ret; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3926 | |
| 3927 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3928 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3929 | factor = 2; |
| 3930 | else |
| 3931 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3932 | |
| 3933 | found = __find_space_info(info, flags); |
| 3934 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3935 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3936 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3937 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3938 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3939 | found->disk_used += bytes_used * factor; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3940 | found->bytes_readonly += bytes_readonly; |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 3941 | if (total_bytes > 0) |
| 3942 | found->full = 0; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 3943 | space_info_add_new_bytes(info, found, total_bytes - |
| 3944 | bytes_used - bytes_readonly); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3945 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3946 | *space_info = found; |
| 3947 | return 0; |
| 3948 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3949 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3950 | if (!found) |
| 3951 | return -ENOMEM; |
| 3952 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3953 | ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3954 | if (ret) { |
| 3955 | kfree(found); |
| 3956 | return ret; |
| 3957 | } |
| 3958 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 3959 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3960 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3961 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3962 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3963 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3964 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3965 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3966 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3967 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3968 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3969 | found->bytes_reserved = 0; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3970 | found->bytes_readonly = bytes_readonly; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3971 | found->bytes_may_use = 0; |
Filipe Manana | 6af3e3a | 2015-09-07 10:41:12 +0100 | [diff] [blame] | 3972 | found->full = 0; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3973 | found->max_extent_size = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3974 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3975 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3976 | found->flush = 0; |
| 3977 | init_waitqueue_head(&found->wait); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 3978 | INIT_LIST_HEAD(&found->ro_bgs); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 3979 | INIT_LIST_HEAD(&found->tickets); |
| 3980 | INIT_LIST_HEAD(&found->priority_tickets); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3981 | |
| 3982 | ret = kobject_init_and_add(&found->kobj, &space_info_ktype, |
| 3983 | info->space_info_kobj, "%s", |
| 3984 | alloc_name(found->flags)); |
| 3985 | if (ret) { |
| 3986 | kfree(found); |
| 3987 | return ret; |
| 3988 | } |
| 3989 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3990 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3991 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3992 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3993 | info->data_sinfo = found; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3994 | |
| 3995 | return ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3996 | } |
| 3997 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3998 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3999 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 4000 | u64 extra_flags = chunk_to_extended(flags) & |
| 4001 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4002 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4003 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4004 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 4005 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 4006 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 4007 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 4008 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 4009 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4010 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4011 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4012 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4013 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4014 | * returns target flags in extended format or 0 if restripe for this |
| 4015 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4016 | * |
| 4017 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4018 | */ |
| 4019 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 4020 | { |
| 4021 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 4022 | u64 target = 0; |
| 4023 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4024 | if (!bctl) |
| 4025 | return 0; |
| 4026 | |
| 4027 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 4028 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4029 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 4030 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 4031 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4032 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 4033 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 4034 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4035 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 4036 | } |
| 4037 | |
| 4038 | return target; |
| 4039 | } |
| 4040 | |
| 4041 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4042 | * @flags: available profiles in extended format (see ctree.h) |
| 4043 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4044 | * Returns reduced profile in chunk format. If profile changing is in |
| 4045 | * progress (either running or paused) picks the target profile (if it's |
| 4046 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4047 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4048 | static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4049 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4050 | u64 num_devices = fs_info->fs_devices->rw_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4051 | u64 target; |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4052 | u64 raid_type; |
| 4053 | u64 allowed = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4054 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4055 | /* |
| 4056 | * see if restripe for this chunk_type is in progress, if so |
| 4057 | * try to reduce to the target profile |
| 4058 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4059 | spin_lock(&fs_info->balance_lock); |
| 4060 | target = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4061 | if (target) { |
| 4062 | /* pick target profile only if it's already available */ |
| 4063 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4064 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4065 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4066 | } |
| 4067 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4068 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4069 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4070 | /* First, mask out the RAID levels which aren't possible */ |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4071 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
| 4072 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) |
| 4073 | allowed |= btrfs_raid_group[raid_type]; |
| 4074 | } |
| 4075 | allowed &= flags; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4076 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4077 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) |
| 4078 | allowed = BTRFS_BLOCK_GROUP_RAID6; |
| 4079 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) |
| 4080 | allowed = BTRFS_BLOCK_GROUP_RAID5; |
| 4081 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) |
| 4082 | allowed = BTRFS_BLOCK_GROUP_RAID10; |
| 4083 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) |
| 4084 | allowed = BTRFS_BLOCK_GROUP_RAID1; |
| 4085 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) |
| 4086 | allowed = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4087 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4088 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4089 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4090 | return extended_to_chunk(flags | allowed); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4091 | } |
| 4092 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4093 | static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4094 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4095 | unsigned seq; |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 4096 | u64 flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4097 | |
| 4098 | do { |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 4099 | flags = orig_flags; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4100 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4101 | |
| 4102 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4103 | flags |= fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4104 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4105 | flags |= fs_info->avail_system_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4106 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4107 | flags |= fs_info->avail_metadata_alloc_bits; |
| 4108 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4109 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4110 | return btrfs_reduce_alloc_profile(fs_info, flags); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4111 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4112 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 4113 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4114 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4115 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4116 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4117 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4118 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4119 | if (data) |
| 4120 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4121 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4122 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 4123 | else |
| 4124 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 4125 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4126 | ret = get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4127 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4128 | } |
| 4129 | |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4130 | static u64 btrfs_space_info_used(struct btrfs_space_info *s_info, |
| 4131 | bool may_use_included) |
| 4132 | { |
| 4133 | ASSERT(s_info); |
| 4134 | return s_info->bytes_used + s_info->bytes_reserved + |
| 4135 | s_info->bytes_pinned + s_info->bytes_readonly + |
| 4136 | (may_use_included ? s_info->bytes_may_use : 0); |
| 4137 | } |
| 4138 | |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4139 | int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4140 | { |
| 4141 | struct btrfs_space_info *data_sinfo; |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4142 | struct btrfs_root *root = inode->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4143 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4144 | u64 used; |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4145 | int ret = 0; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4146 | int need_commit = 2; |
| 4147 | int have_pinned_space; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4148 | |
| 4149 | /* make sure bytes are sectorsize aligned */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4150 | bytes = ALIGN(bytes, fs_info->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4151 | |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4152 | if (btrfs_is_free_space_inode(inode)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4153 | need_commit = 0; |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4154 | ASSERT(current->journal_info); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4155 | } |
| 4156 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4157 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4158 | if (!data_sinfo) |
| 4159 | goto alloc; |
| 4160 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4161 | again: |
| 4162 | /* make sure we have enough space to handle the data first */ |
| 4163 | spin_lock(&data_sinfo->lock); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4164 | used = btrfs_space_info_used(data_sinfo, true); |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4165 | |
| 4166 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4167 | struct btrfs_trans_handle *trans; |
| 4168 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4169 | /* |
| 4170 | * if we don't have enough free bytes in this space then we need |
| 4171 | * to alloc a new chunk. |
| 4172 | */ |
Zhao Lei | b9fd47c | 2015-02-09 14:40:20 +0800 | [diff] [blame] | 4173 | if (!data_sinfo->full) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4174 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4175 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4176 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4177 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4178 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4179 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4180 | /* |
| 4181 | * It is ugly that we don't call nolock join |
| 4182 | * transaction for the free space inode case here. |
| 4183 | * But it is safe because we only do the data space |
| 4184 | * reservation for the free space cache in the |
| 4185 | * transaction context, the common join transaction |
| 4186 | * just increase the counter of the current transaction |
| 4187 | * handler, doesn't try to acquire the trans_lock of |
| 4188 | * the fs. |
| 4189 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4190 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4191 | if (IS_ERR(trans)) |
| 4192 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4193 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4194 | ret = do_chunk_alloc(trans, fs_info, alloc_target, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4195 | CHUNK_ALLOC_NO_FORCE); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4196 | btrfs_end_transaction(trans); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4197 | if (ret < 0) { |
| 4198 | if (ret != -ENOSPC) |
| 4199 | return ret; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4200 | else { |
| 4201 | have_pinned_space = 1; |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4202 | goto commit_trans; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4203 | } |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4204 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4205 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4206 | if (!data_sinfo) |
| 4207 | data_sinfo = fs_info->data_sinfo; |
| 4208 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4209 | goto again; |
| 4210 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4211 | |
| 4212 | /* |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4213 | * If we don't have enough pinned space to deal with this |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4214 | * allocation, and no removed chunk in current transaction, |
| 4215 | * don't bother committing the transaction. |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4216 | */ |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4217 | have_pinned_space = percpu_counter_compare( |
| 4218 | &data_sinfo->total_bytes_pinned, |
| 4219 | used + bytes - data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4220 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4221 | |
| 4222 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4223 | commit_trans: |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4224 | if (need_commit && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4225 | !atomic_read(&fs_info->open_ioctl_trans)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4226 | need_commit--; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4227 | |
Zhao Lei | e1746e8 | 2015-12-01 18:39:40 +0800 | [diff] [blame] | 4228 | if (need_commit > 0) { |
| 4229 | btrfs_start_delalloc_roots(fs_info, 0, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4230 | btrfs_wait_ordered_roots(fs_info, -1, 0, |
| 4231 | (u64)-1); |
Zhao Lei | e1746e8 | 2015-12-01 18:39:40 +0800 | [diff] [blame] | 4232 | } |
Zhao Lei | 9a4e727 | 2015-04-09 12:34:43 +0800 | [diff] [blame] | 4233 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4234 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4235 | if (IS_ERR(trans)) |
| 4236 | return PTR_ERR(trans); |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4237 | if (have_pinned_space >= 0 || |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 4238 | test_bit(BTRFS_TRANS_HAVE_FREE_BGS, |
| 4239 | &trans->transaction->flags) || |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4240 | need_commit > 0) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4241 | ret = btrfs_commit_transaction(trans); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4242 | if (ret) |
| 4243 | return ret; |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4244 | /* |
Filipe Manana | c2d6cb1 | 2016-01-15 11:05:12 +0000 | [diff] [blame] | 4245 | * The cleaner kthread might still be doing iput |
| 4246 | * operations. Wait for it to finish so that |
| 4247 | * more space is released. |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4248 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4249 | mutex_lock(&fs_info->cleaner_delayed_iput_mutex); |
| 4250 | mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4251 | goto again; |
| 4252 | } else { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4253 | btrfs_end_transaction(trans); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4254 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4255 | } |
| 4256 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4257 | trace_btrfs_space_reservation(fs_info, |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 4258 | "space_info:enospc", |
| 4259 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4260 | return -ENOSPC; |
| 4261 | } |
| 4262 | data_sinfo->bytes_may_use += bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4263 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4264 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4265 | spin_unlock(&data_sinfo->lock); |
| 4266 | |
Dongsheng Yang | 237c0e9 | 2014-12-29 06:23:05 -0500 | [diff] [blame] | 4267 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4271 | * New check_data_free_space() with ability for precious data reservation |
| 4272 | * Will replace old btrfs_check_data_free_space(), but for patch split, |
| 4273 | * add a new function first and then replace it. |
| 4274 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4275 | int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4276 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4277 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4278 | int ret; |
| 4279 | |
| 4280 | /* align the range */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4281 | len = round_up(start + len, fs_info->sectorsize) - |
| 4282 | round_down(start, fs_info->sectorsize); |
| 4283 | start = round_down(start, fs_info->sectorsize); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4284 | |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4285 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4286 | if (ret < 0) |
| 4287 | return ret; |
| 4288 | |
Josef Bacik | 1e5ec2e | 2016-09-15 14:57:48 -0400 | [diff] [blame] | 4289 | /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */ |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4290 | ret = btrfs_qgroup_reserve_data(inode, start, len); |
Josef Bacik | 1e5ec2e | 2016-09-15 14:57:48 -0400 | [diff] [blame] | 4291 | if (ret) |
| 4292 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4293 | return ret; |
| 4294 | } |
| 4295 | |
| 4296 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4297 | * Called if we need to clear a data reservation for this inode |
| 4298 | * Normally in a error case. |
| 4299 | * |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4300 | * This one will *NOT* use accurate qgroup reserved space API, just for case |
| 4301 | * which we can't sleep and is sure it won't affect qgroup reserved space. |
| 4302 | * Like clear_bit_hook(). |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4303 | */ |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4304 | void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, |
| 4305 | u64 len) |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4306 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4307 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4308 | struct btrfs_space_info *data_sinfo; |
| 4309 | |
| 4310 | /* Make sure the range is aligned to sectorsize */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4311 | len = round_up(start + len, fs_info->sectorsize) - |
| 4312 | round_down(start, fs_info->sectorsize); |
| 4313 | start = round_down(start, fs_info->sectorsize); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4314 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4315 | data_sinfo = fs_info->data_sinfo; |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4316 | spin_lock(&data_sinfo->lock); |
| 4317 | if (WARN_ON(data_sinfo->bytes_may_use < len)) |
| 4318 | data_sinfo->bytes_may_use = 0; |
| 4319 | else |
| 4320 | data_sinfo->bytes_may_use -= len; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4321 | trace_btrfs_space_reservation(fs_info, "space_info", |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4322 | data_sinfo->flags, len, 0); |
| 4323 | spin_unlock(&data_sinfo->lock); |
| 4324 | } |
| 4325 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4326 | /* |
| 4327 | * Called if we need to clear a data reservation for this inode |
| 4328 | * Normally in a error case. |
| 4329 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4330 | * This one will handle the per-inode data rsv map for accurate reserved |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4331 | * space framework. |
| 4332 | */ |
| 4333 | void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len) |
| 4334 | { |
Jeff Mahoney | 0c476a5 | 2016-11-18 21:52:40 -0500 | [diff] [blame] | 4335 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4336 | |
| 4337 | /* Make sure the range is aligned to sectorsize */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4338 | len = round_up(start + len, root->fs_info->sectorsize) - |
| 4339 | round_down(start, root->fs_info->sectorsize); |
| 4340 | start = round_down(start, root->fs_info->sectorsize); |
Jeff Mahoney | 0c476a5 | 2016-11-18 21:52:40 -0500 | [diff] [blame] | 4341 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4342 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
| 4343 | btrfs_qgroup_free_data(inode, start, len); |
| 4344 | } |
| 4345 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4346 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 4347 | { |
| 4348 | struct list_head *head = &info->space_info; |
| 4349 | struct btrfs_space_info *found; |
| 4350 | |
| 4351 | rcu_read_lock(); |
| 4352 | list_for_each_entry_rcu(found, head, list) { |
| 4353 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4354 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4355 | } |
| 4356 | rcu_read_unlock(); |
| 4357 | } |
| 4358 | |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4359 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
| 4360 | { |
| 4361 | return (global->size << 1); |
| 4362 | } |
| 4363 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4364 | static int should_alloc_chunk(struct btrfs_fs_info *fs_info, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4365 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4366 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4367 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4368 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4369 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 4370 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4371 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4372 | if (force == CHUNK_ALLOC_FORCE) |
| 4373 | return 1; |
| 4374 | |
| 4375 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4376 | * We need to take into account the global rsv because for all intents |
| 4377 | * and purposes it's used space. Don't worry about locking the |
| 4378 | * global_rsv, it doesn't change except when the transaction commits. |
| 4379 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 4380 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4381 | num_allocated += calc_global_rsv_need_space(global_rsv); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4382 | |
| 4383 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4384 | * in limited mode, we want to have some free space up to |
| 4385 | * about 1% of the FS size. |
| 4386 | */ |
| 4387 | if (force == CHUNK_ALLOC_LIMITED) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4388 | thresh = btrfs_super_total_bytes(fs_info->super_copy); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4389 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4390 | |
| 4391 | if (num_bytes - num_allocated < thresh) |
| 4392 | return 1; |
| 4393 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4394 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4395 | if (num_allocated + SZ_2M < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 4396 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4397 | return 1; |
| 4398 | } |
| 4399 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4400 | static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4401 | { |
| 4402 | u64 num_dev; |
| 4403 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4404 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
| 4405 | BTRFS_BLOCK_GROUP_RAID0 | |
| 4406 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4407 | BTRFS_BLOCK_GROUP_RAID6)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4408 | num_dev = fs_info->fs_devices->rw_devices; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4409 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 4410 | num_dev = 2; |
| 4411 | else |
| 4412 | num_dev = 1; /* DUP or single */ |
| 4413 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4414 | return num_dev; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4415 | } |
| 4416 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4417 | /* |
| 4418 | * If @is_allocation is true, reserve space in the system space info necessary |
| 4419 | * for allocating a chunk, otherwise if it's false, reserve space necessary for |
| 4420 | * removing a chunk. |
| 4421 | */ |
| 4422 | void check_system_chunk(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4423 | struct btrfs_fs_info *fs_info, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4424 | { |
| 4425 | struct btrfs_space_info *info; |
| 4426 | u64 left; |
| 4427 | u64 thresh; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4428 | int ret = 0; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4429 | u64 num_devs; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* |
| 4432 | * Needed because we can end up allocating a system chunk and for an |
| 4433 | * atomic and race free space reservation in the chunk block reserve. |
| 4434 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4435 | ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4436 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4437 | info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4438 | spin_lock(&info->lock); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4439 | left = info->total_bytes - btrfs_space_info_used(info, true); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4440 | spin_unlock(&info->lock); |
| 4441 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4442 | num_devs = get_profile_num_devs(fs_info, type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4443 | |
| 4444 | /* num_devs device items to update and 1 chunk item to add or remove */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4445 | thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) + |
| 4446 | btrfs_calc_trans_metadata_size(fs_info, 1); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4447 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4448 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
| 4449 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", |
| 4450 | left, thresh, type); |
| 4451 | dump_space_info(fs_info, info, 0, 0); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | if (left < thresh) { |
| 4455 | u64 flags; |
| 4456 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4457 | flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4458 | /* |
| 4459 | * Ignore failure to create system chunk. We might end up not |
| 4460 | * needing it, as we might not need to COW all nodes/leafs from |
| 4461 | * the paths we visit in the chunk tree (they were already COWed |
| 4462 | * or created in the current transaction for example). |
| 4463 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4464 | ret = btrfs_alloc_chunk(trans, fs_info, flags); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4468 | ret = btrfs_block_rsv_add(fs_info->chunk_root, |
| 4469 | &fs_info->chunk_block_rsv, |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4470 | thresh, BTRFS_RESERVE_NO_FLUSH); |
| 4471 | if (!ret) |
| 4472 | trans->chunk_bytes_reserved += thresh; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
Liu Bo | 28b737f | 2016-07-29 11:09:50 -0700 | [diff] [blame] | 4476 | /* |
| 4477 | * If force is CHUNK_ALLOC_FORCE: |
| 4478 | * - return 1 if it successfully allocates a chunk, |
| 4479 | * - return errors including -ENOSPC otherwise. |
| 4480 | * If force is NOT CHUNK_ALLOC_FORCE: |
| 4481 | * - return 0 if it doesn't need to allocate a new chunk, |
| 4482 | * - return 1 if it successfully allocates a chunk, |
| 4483 | * - return errors including -ENOSPC otherwise. |
| 4484 | */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4485 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4486 | struct btrfs_fs_info *fs_info, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4487 | { |
| 4488 | struct btrfs_space_info *space_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4489 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4490 | int ret = 0; |
| 4491 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4492 | /* Don't re-enter if we're already allocating a chunk */ |
| 4493 | if (trans->allocating_chunk) |
| 4494 | return -ENOSPC; |
| 4495 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4496 | space_info = __find_space_info(fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4497 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4498 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4499 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4500 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4501 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4502 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4503 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4504 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4505 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4506 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4507 | if (space_info->full) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4508 | if (should_alloc_chunk(fs_info, space_info, force)) |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4509 | ret = -ENOSPC; |
| 4510 | else |
| 4511 | ret = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4512 | spin_unlock(&space_info->lock); |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4513 | return ret; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4514 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4515 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4516 | if (!should_alloc_chunk(fs_info, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4517 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4518 | return 0; |
| 4519 | } else if (space_info->chunk_alloc) { |
| 4520 | wait_for_alloc = 1; |
| 4521 | } else { |
| 4522 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4523 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4524 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4525 | spin_unlock(&space_info->lock); |
| 4526 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4527 | mutex_lock(&fs_info->chunk_mutex); |
| 4528 | |
| 4529 | /* |
| 4530 | * The chunk_mutex is held throughout the entirety of a chunk |
| 4531 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 4532 | * other guy is done and we need to recheck and see if we should |
| 4533 | * allocate. |
| 4534 | */ |
| 4535 | if (wait_for_alloc) { |
| 4536 | mutex_unlock(&fs_info->chunk_mutex); |
| 4537 | wait_for_alloc = 0; |
| 4538 | goto again; |
| 4539 | } |
| 4540 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4541 | trans->allocating_chunk = true; |
| 4542 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4543 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4544 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 4545 | * allocating mixed chunks instead of individual chunks. |
| 4546 | */ |
| 4547 | if (btrfs_mixed_space_info(space_info)) |
| 4548 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 4549 | |
| 4550 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4551 | * if we're doing a data chunk, go ahead and make sure that |
| 4552 | * we keep a reasonable number of metadata chunks allocated in the |
| 4553 | * FS as well. |
| 4554 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4555 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4556 | fs_info->data_chunk_allocations++; |
| 4557 | if (!(fs_info->data_chunk_allocations % |
| 4558 | fs_info->metadata_ratio)) |
| 4559 | force_metadata_allocation(fs_info); |
| 4560 | } |
| 4561 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4562 | /* |
| 4563 | * Check if we have enough space in SYSTEM chunk because we may need |
| 4564 | * to update devices. |
| 4565 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4566 | check_system_chunk(trans, fs_info, flags); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4567 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4568 | ret = btrfs_alloc_chunk(trans, fs_info, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4569 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 4570 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4571 | spin_lock(&space_info->lock); |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4572 | if (ret < 0 && ret != -ENOSPC) |
| 4573 | goto out; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4574 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4575 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4576 | else |
| 4577 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4578 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4579 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4580 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4581 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4582 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 4583 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4584 | /* |
| 4585 | * When we allocate a new chunk we reserve space in the chunk block |
| 4586 | * reserve to make sure we can COW nodes/leafs in the chunk tree or |
| 4587 | * add new nodes/leafs to it if we end up needing to do it when |
| 4588 | * inserting the chunk item and updating device items as part of the |
| 4589 | * second phase of chunk allocation, performed by |
| 4590 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a |
| 4591 | * large number of new block groups to create in our transaction |
| 4592 | * handle's new_bgs list to avoid exhausting the chunk block reserve |
| 4593 | * in extreme cases - like having a single transaction create many new |
| 4594 | * block groups when starting to write out the free space caches of all |
| 4595 | * the block groups that were made dirty during the lifetime of the |
| 4596 | * transaction. |
| 4597 | */ |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 4598 | if (trans->can_flush_pending_bgs && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4599 | trans->chunk_bytes_reserved >= (u64)SZ_2M) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4600 | btrfs_create_pending_block_groups(trans, fs_info); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4601 | btrfs_trans_release_chunk_metadata(trans); |
| 4602 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4603 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4604 | } |
| 4605 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4606 | static int can_overcommit(struct btrfs_root *root, |
| 4607 | struct btrfs_space_info *space_info, u64 bytes, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4608 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4609 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4610 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4611 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4612 | u64 profile; |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4613 | u64 space_size; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4614 | u64 avail; |
| 4615 | u64 used; |
| 4616 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4617 | /* Don't overcommit when in mixed mode. */ |
| 4618 | if (space_info->flags & BTRFS_BLOCK_GROUP_DATA) |
| 4619 | return 0; |
| 4620 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4621 | profile = btrfs_get_alloc_profile(root, 0); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4622 | used = btrfs_space_info_used(space_info, false); |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4623 | |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4624 | /* |
| 4625 | * We only want to allow over committing if we have lots of actual space |
| 4626 | * free, but if we don't have enough space to handle the global reserve |
| 4627 | * space then we could end up having a real enospc problem when trying |
| 4628 | * to allocate a chunk or some other such important allocation. |
| 4629 | */ |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4630 | spin_lock(&global_rsv->lock); |
| 4631 | space_size = calc_global_rsv_need_space(global_rsv); |
| 4632 | spin_unlock(&global_rsv->lock); |
| 4633 | if (used + space_size >= space_info->total_bytes) |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4634 | return 0; |
| 4635 | |
| 4636 | used += space_info->bytes_may_use; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4637 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4638 | spin_lock(&fs_info->free_chunk_lock); |
| 4639 | avail = fs_info->free_chunk_space; |
| 4640 | spin_unlock(&fs_info->free_chunk_lock); |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4641 | |
| 4642 | /* |
| 4643 | * If we have dup, raid1 or raid10 then only half of the free |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4644 | * space is actually useable. For raid56, the space info used |
| 4645 | * doesn't include the parity drive, so we don't have to |
| 4646 | * change the math |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4647 | */ |
| 4648 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 4649 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4650 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4651 | avail >>= 1; |
| 4652 | |
| 4653 | /* |
Miao Xie | 561c294 | 2012-10-16 11:32:18 +0000 | [diff] [blame] | 4654 | * If we aren't flushing all things, let us overcommit up to |
| 4655 | * 1/2th of the space. If we can flush, don't let us overcommit |
| 4656 | * too much, let it overcommit up to 1/8 of the space. |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4657 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4658 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4659 | avail >>= 3; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4660 | else |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4661 | avail >>= 1; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4662 | |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4663 | if (used + bytes < space_info->total_bytes + avail) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4664 | return 1; |
| 4665 | return 0; |
| 4666 | } |
| 4667 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4668 | static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info, |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4669 | unsigned long nr_pages, int nr_items) |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4670 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4671 | struct super_block *sb = fs_info->sb; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4672 | |
Josef Bacik | 925a6ef | 2013-06-20 12:31:27 -0400 | [diff] [blame] | 4673 | if (down_read_trylock(&sb->s_umount)) { |
| 4674 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); |
| 4675 | up_read(&sb->s_umount); |
| 4676 | } else { |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4677 | /* |
| 4678 | * We needn't worry the filesystem going from r/w to r/o though |
| 4679 | * we don't acquire ->s_umount mutex, because the filesystem |
| 4680 | * should guarantee the delalloc inodes list be empty after |
| 4681 | * the filesystem is readonly(all dirty pages are written to |
| 4682 | * the disk). |
| 4683 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4684 | btrfs_start_delalloc_roots(fs_info, 0, nr_items); |
Josef Bacik | 98ad69c | 2013-04-04 11:55:49 -0400 | [diff] [blame] | 4685 | if (!current->journal_info) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4686 | btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1); |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4687 | } |
| 4688 | } |
| 4689 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4690 | static inline int calc_reclaim_items_nr(struct btrfs_fs_info *fs_info, |
| 4691 | u64 to_reclaim) |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4692 | { |
| 4693 | u64 bytes; |
| 4694 | int nr; |
| 4695 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4696 | bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4697 | nr = (int)div64_u64(to_reclaim, bytes); |
| 4698 | if (!nr) |
| 4699 | nr = 1; |
| 4700 | return nr; |
| 4701 | } |
| 4702 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4703 | #define EXTENT_SIZE_PER_ITEM SZ_256K |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4704 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4705 | /* |
| 4706 | * shrink metadata reservation for delalloc |
| 4707 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4708 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 4709 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4710 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4711 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4712 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4713 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4714 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4715 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4716 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 4717 | long time_left; |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4718 | unsigned long nr_pages; |
| 4719 | int loops; |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4720 | int items; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4721 | enum btrfs_reserve_flush_enum flush; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4722 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4723 | /* Calc the number of the pages we need flush for space reservation */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4724 | items = calc_reclaim_items_nr(fs_info, to_reclaim); |
Adam Borowski | 8eb0dfd | 2016-05-08 15:08:00 +0200 | [diff] [blame] | 4725 | to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM; |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4726 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4727 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4728 | block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4729 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 4730 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4731 | delalloc_bytes = percpu_counter_sum_positive( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4732 | &fs_info->delalloc_bytes); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4733 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4734 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4735 | return; |
Miao Xie | 38c135a | 2013-11-04 23:13:21 +0800 | [diff] [blame] | 4736 | if (wait_ordered) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4737 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4738 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4739 | } |
| 4740 | |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4741 | loops = 0; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4742 | while (delalloc_bytes && loops < 3) { |
| 4743 | max_reclaim = min(delalloc_bytes, to_reclaim); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4744 | nr_pages = max_reclaim >> PAGE_SHIFT; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4745 | btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items); |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4746 | /* |
| 4747 | * We need to wait for the async pages to actually start before |
| 4748 | * we do anything. |
| 4749 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4750 | max_reclaim = atomic_read(&fs_info->async_delalloc_pages); |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4751 | if (!max_reclaim) |
| 4752 | goto skip_async; |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4753 | |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4754 | if (max_reclaim <= nr_pages) |
| 4755 | max_reclaim = 0; |
| 4756 | else |
| 4757 | max_reclaim -= nr_pages; |
| 4758 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4759 | wait_event(fs_info->async_submit_wait, |
| 4760 | atomic_read(&fs_info->async_delalloc_pages) <= |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4761 | (int)max_reclaim); |
| 4762 | skip_async: |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4763 | if (!trans) |
| 4764 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 4765 | else |
| 4766 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4767 | spin_lock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4768 | if (can_overcommit(root, space_info, orig, flush)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4769 | spin_unlock(&space_info->lock); |
| 4770 | break; |
| 4771 | } |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4772 | if (list_empty(&space_info->tickets) && |
| 4773 | list_empty(&space_info->priority_tickets)) { |
| 4774 | spin_unlock(&space_info->lock); |
| 4775 | break; |
| 4776 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4777 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4778 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4779 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4780 | if (wait_ordered && !trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4781 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4782 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4783 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4784 | if (time_left) |
| 4785 | break; |
| 4786 | } |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4787 | delalloc_bytes = percpu_counter_sum_positive( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4788 | &fs_info->delalloc_bytes); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4789 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4790 | } |
| 4791 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4792 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4793 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 4794 | * @root - the root we're allocating for |
| 4795 | * @bytes - the number of bytes we want to reserve |
| 4796 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4797 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4798 | * This will check to make sure that committing the transaction will actually |
| 4799 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 4800 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4801 | */ |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4802 | static int may_commit_transaction(struct btrfs_fs_info *fs_info, |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4803 | struct btrfs_space_info *space_info, |
| 4804 | u64 bytes, int force) |
| 4805 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4806 | struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4807 | struct btrfs_trans_handle *trans; |
| 4808 | |
| 4809 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 4810 | if (trans) |
| 4811 | return -EAGAIN; |
| 4812 | |
| 4813 | if (force) |
| 4814 | goto commit; |
| 4815 | |
| 4816 | /* See if there is enough pinned space to make this reservation */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4817 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
Miao Xie | 0424c54 | 2014-03-06 13:54:59 +0800 | [diff] [blame] | 4818 | bytes) >= 0) |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4819 | goto commit; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4820 | |
| 4821 | /* |
| 4822 | * See if there is some space in the delayed insertion reservation for |
| 4823 | * this reservation. |
| 4824 | */ |
| 4825 | if (space_info != delayed_rsv->space_info) |
| 4826 | return -ENOSPC; |
| 4827 | |
| 4828 | spin_lock(&delayed_rsv->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4829 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4830 | bytes - delayed_rsv->size) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4831 | spin_unlock(&delayed_rsv->lock); |
| 4832 | return -ENOSPC; |
| 4833 | } |
| 4834 | spin_unlock(&delayed_rsv->lock); |
| 4835 | |
| 4836 | commit: |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4837 | trans = btrfs_join_transaction(fs_info->fs_root); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4838 | if (IS_ERR(trans)) |
| 4839 | return -ENOSPC; |
| 4840 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4841 | return btrfs_commit_transaction(trans); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4842 | } |
| 4843 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4844 | struct reserve_ticket { |
| 4845 | u64 bytes; |
| 4846 | int error; |
| 4847 | struct list_head list; |
| 4848 | wait_queue_head_t wait; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4849 | }; |
| 4850 | |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4851 | static int flush_space(struct btrfs_fs_info *fs_info, |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4852 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 4853 | u64 orig_bytes, int state) |
| 4854 | { |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4855 | struct btrfs_root *root = fs_info->fs_root; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4856 | struct btrfs_trans_handle *trans; |
| 4857 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4858 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4859 | |
| 4860 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4861 | case FLUSH_DELAYED_ITEMS_NR: |
| 4862 | case FLUSH_DELAYED_ITEMS: |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4863 | if (state == FLUSH_DELAYED_ITEMS_NR) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4864 | nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4865 | else |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4866 | nr = -1; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4867 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4868 | trans = btrfs_join_transaction(root); |
| 4869 | if (IS_ERR(trans)) { |
| 4870 | ret = PTR_ERR(trans); |
| 4871 | break; |
| 4872 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4873 | ret = btrfs_run_delayed_items_nr(trans, fs_info, nr); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4874 | btrfs_end_transaction(trans); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4875 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4876 | case FLUSH_DELALLOC: |
| 4877 | case FLUSH_DELALLOC_WAIT: |
Miao Xie | 24af7dd | 2014-03-06 13:55:00 +0800 | [diff] [blame] | 4878 | shrink_delalloc(root, num_bytes * 2, orig_bytes, |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4879 | state == FLUSH_DELALLOC_WAIT); |
| 4880 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4881 | case ALLOC_CHUNK: |
| 4882 | trans = btrfs_join_transaction(root); |
| 4883 | if (IS_ERR(trans)) { |
| 4884 | ret = PTR_ERR(trans); |
| 4885 | break; |
| 4886 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4887 | ret = do_chunk_alloc(trans, fs_info, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4888 | btrfs_get_alloc_profile(root, 0), |
| 4889 | CHUNK_ALLOC_NO_FORCE); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4890 | btrfs_end_transaction(trans); |
Alex Lyakas | eecba89 | 2015-12-06 12:32:31 +0200 | [diff] [blame] | 4891 | if (ret > 0 || ret == -ENOSPC) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4892 | ret = 0; |
| 4893 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4894 | case COMMIT_TRANS: |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4895 | ret = may_commit_transaction(fs_info, space_info, |
| 4896 | orig_bytes, 0); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4897 | break; |
| 4898 | default: |
| 4899 | ret = -ENOSPC; |
| 4900 | break; |
| 4901 | } |
| 4902 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4903 | trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 4904 | orig_bytes, state, ret); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4905 | return ret; |
| 4906 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4907 | |
| 4908 | static inline u64 |
| 4909 | btrfs_calc_reclaim_metadata_size(struct btrfs_root *root, |
| 4910 | struct btrfs_space_info *space_info) |
| 4911 | { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4912 | struct reserve_ticket *ticket; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4913 | u64 used; |
| 4914 | u64 expected; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4915 | u64 to_reclaim = 0; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4916 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4917 | list_for_each_entry(ticket, &space_info->tickets, list) |
| 4918 | to_reclaim += ticket->bytes; |
| 4919 | list_for_each_entry(ticket, &space_info->priority_tickets, list) |
| 4920 | to_reclaim += ticket->bytes; |
| 4921 | if (to_reclaim) |
| 4922 | return to_reclaim; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4923 | |
Wang Xiaoguang | e0af248 | 2016-08-31 19:46:16 +0800 | [diff] [blame] | 4924 | to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M); |
| 4925 | if (can_overcommit(root, space_info, to_reclaim, |
| 4926 | BTRFS_RESERVE_FLUSH_ALL)) |
| 4927 | return 0; |
| 4928 | |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4929 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4930 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4931 | space_info->bytes_may_use; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4932 | if (can_overcommit(root, space_info, SZ_1M, BTRFS_RESERVE_FLUSH_ALL)) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4933 | expected = div_factor_fine(space_info->total_bytes, 95); |
| 4934 | else |
| 4935 | expected = div_factor_fine(space_info->total_bytes, 90); |
| 4936 | |
| 4937 | if (used > expected) |
| 4938 | to_reclaim = used - expected; |
| 4939 | else |
| 4940 | to_reclaim = 0; |
| 4941 | to_reclaim = min(to_reclaim, space_info->bytes_may_use + |
| 4942 | space_info->bytes_reserved); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4943 | return to_reclaim; |
| 4944 | } |
| 4945 | |
| 4946 | static inline int need_do_async_reclaim(struct btrfs_space_info *space_info, |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 4947 | struct btrfs_root *root, u64 used) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4948 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4949 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4950 | u64 thresh = div_factor_fine(space_info->total_bytes, 98); |
| 4951 | |
| 4952 | /* If we're just plain full then async reclaim just slows us down. */ |
Josef Bacik | baee879 | 2016-01-26 09:35:38 -0500 | [diff] [blame] | 4953 | if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh) |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4954 | return 0; |
| 4955 | |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 4956 | if (!btrfs_calc_reclaim_metadata_size(root, space_info)) |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4957 | return 0; |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4958 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4959 | return (used >= thresh && !btrfs_fs_closing(fs_info) && |
| 4960 | !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4961 | } |
| 4962 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4963 | static void wake_all_tickets(struct list_head *head) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4964 | { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4965 | struct reserve_ticket *ticket; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4966 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4967 | while (!list_empty(head)) { |
| 4968 | ticket = list_first_entry(head, struct reserve_ticket, list); |
| 4969 | list_del_init(&ticket->list); |
| 4970 | ticket->error = -ENOSPC; |
| 4971 | wake_up(&ticket->wait); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4972 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4973 | } |
| 4974 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4975 | /* |
| 4976 | * This is for normal flushers, we can wait all goddamned day if we want to. We |
| 4977 | * will loop and continuously try to flush as long as we are making progress. |
| 4978 | * We count progress as clearing off tickets each time we have to loop. |
| 4979 | */ |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4980 | static void btrfs_async_reclaim_metadata_space(struct work_struct *work) |
| 4981 | { |
| 4982 | struct btrfs_fs_info *fs_info; |
| 4983 | struct btrfs_space_info *space_info; |
| 4984 | u64 to_reclaim; |
| 4985 | int flush_state; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4986 | int commit_cycles = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 4987 | u64 last_tickets_id; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4988 | |
| 4989 | fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); |
| 4990 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4991 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4992 | spin_lock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4993 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 4994 | space_info); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4995 | if (!to_reclaim) { |
| 4996 | space_info->flush = 0; |
| 4997 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4998 | return; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4999 | } |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5000 | last_tickets_id = space_info->tickets_id; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5001 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5002 | |
| 5003 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5004 | do { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5005 | struct reserve_ticket *ticket; |
| 5006 | int ret; |
| 5007 | |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 5008 | ret = flush_space(fs_info, space_info, to_reclaim, to_reclaim, |
| 5009 | flush_state); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5010 | spin_lock(&space_info->lock); |
| 5011 | if (list_empty(&space_info->tickets)) { |
| 5012 | space_info->flush = 0; |
| 5013 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5014 | return; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5015 | } |
| 5016 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 5017 | space_info); |
| 5018 | ticket = list_first_entry(&space_info->tickets, |
| 5019 | struct reserve_ticket, list); |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5020 | if (last_tickets_id == space_info->tickets_id) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5021 | flush_state++; |
| 5022 | } else { |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5023 | last_tickets_id = space_info->tickets_id; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5024 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5025 | if (commit_cycles) |
| 5026 | commit_cycles--; |
| 5027 | } |
| 5028 | |
| 5029 | if (flush_state > COMMIT_TRANS) { |
| 5030 | commit_cycles++; |
| 5031 | if (commit_cycles > 2) { |
| 5032 | wake_all_tickets(&space_info->tickets); |
| 5033 | space_info->flush = 0; |
| 5034 | } else { |
| 5035 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5036 | } |
| 5037 | } |
| 5038 | spin_unlock(&space_info->lock); |
| 5039 | } while (flush_state <= COMMIT_TRANS); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5040 | } |
| 5041 | |
| 5042 | void btrfs_init_async_reclaim_work(struct work_struct *work) |
| 5043 | { |
| 5044 | INIT_WORK(work, btrfs_async_reclaim_metadata_space); |
| 5045 | } |
| 5046 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5047 | static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, |
| 5048 | struct btrfs_space_info *space_info, |
| 5049 | struct reserve_ticket *ticket) |
| 5050 | { |
| 5051 | u64 to_reclaim; |
| 5052 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5053 | |
| 5054 | spin_lock(&space_info->lock); |
| 5055 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 5056 | space_info); |
| 5057 | if (!to_reclaim) { |
| 5058 | spin_unlock(&space_info->lock); |
| 5059 | return; |
| 5060 | } |
| 5061 | spin_unlock(&space_info->lock); |
| 5062 | |
| 5063 | do { |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 5064 | flush_space(fs_info, space_info, to_reclaim, to_reclaim, |
| 5065 | flush_state); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5066 | flush_state++; |
| 5067 | spin_lock(&space_info->lock); |
| 5068 | if (ticket->bytes == 0) { |
| 5069 | spin_unlock(&space_info->lock); |
| 5070 | return; |
| 5071 | } |
| 5072 | spin_unlock(&space_info->lock); |
| 5073 | |
| 5074 | /* |
| 5075 | * Priority flushers can't wait on delalloc without |
| 5076 | * deadlocking. |
| 5077 | */ |
| 5078 | if (flush_state == FLUSH_DELALLOC || |
| 5079 | flush_state == FLUSH_DELALLOC_WAIT) |
| 5080 | flush_state = ALLOC_CHUNK; |
| 5081 | } while (flush_state < COMMIT_TRANS); |
| 5082 | } |
| 5083 | |
| 5084 | static int wait_reserve_ticket(struct btrfs_fs_info *fs_info, |
| 5085 | struct btrfs_space_info *space_info, |
| 5086 | struct reserve_ticket *ticket, u64 orig_bytes) |
| 5087 | |
| 5088 | { |
| 5089 | DEFINE_WAIT(wait); |
| 5090 | int ret = 0; |
| 5091 | |
| 5092 | spin_lock(&space_info->lock); |
| 5093 | while (ticket->bytes > 0 && ticket->error == 0) { |
| 5094 | ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE); |
| 5095 | if (ret) { |
| 5096 | ret = -EINTR; |
| 5097 | break; |
| 5098 | } |
| 5099 | spin_unlock(&space_info->lock); |
| 5100 | |
| 5101 | schedule(); |
| 5102 | |
| 5103 | finish_wait(&ticket->wait, &wait); |
| 5104 | spin_lock(&space_info->lock); |
| 5105 | } |
| 5106 | if (!ret) |
| 5107 | ret = ticket->error; |
| 5108 | if (!list_empty(&ticket->list)) |
| 5109 | list_del_init(&ticket->list); |
| 5110 | if (ticket->bytes && ticket->bytes < orig_bytes) { |
| 5111 | u64 num_bytes = orig_bytes - ticket->bytes; |
| 5112 | space_info->bytes_may_use -= num_bytes; |
| 5113 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5114 | space_info->flags, num_bytes, 0); |
| 5115 | } |
| 5116 | spin_unlock(&space_info->lock); |
| 5117 | |
| 5118 | return ret; |
| 5119 | } |
| 5120 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5121 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5122 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 5123 | * @root - the root we're allocating for |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5124 | * @space_info - the space info we want to allocate from |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5125 | * @orig_bytes - the number of bytes we want |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 5126 | * @flush - whether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5127 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5128 | * This will reserve orig_bytes number of bytes from the space info associated |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5129 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 5130 | * flush out space to make room. It will do this by flushing delalloc if |
| 5131 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 5132 | * regain reservations will be made and this will fail if there is not enough |
| 5133 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5134 | */ |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5135 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
| 5136 | struct btrfs_space_info *space_info, |
| 5137 | u64 orig_bytes, |
| 5138 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5139 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5140 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5141 | struct reserve_ticket ticket; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5142 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5143 | int ret = 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5144 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5145 | ASSERT(orig_bytes); |
Josef Bacik | 8ca17f0 | 2016-05-27 13:24:13 -0400 | [diff] [blame] | 5146 | ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL); |
| 5147 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5148 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 5149 | ret = -ENOSPC; |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 5150 | used = btrfs_space_info_used(space_info, true); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5151 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5152 | /* |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5153 | * If we have enough space then hooray, make our reservation and carry |
| 5154 | * on. If not see if we can overcommit, and if we can, hooray carry on. |
| 5155 | * If not things get more complicated. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5156 | */ |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5157 | if (used + orig_bytes <= space_info->total_bytes) { |
| 5158 | space_info->bytes_may_use += orig_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5159 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5160 | space_info->flags, orig_bytes, 1); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5161 | ret = 0; |
| 5162 | } else if (can_overcommit(root, space_info, orig_bytes, flush)) { |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 5163 | space_info->bytes_may_use += orig_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5164 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5165 | space_info->flags, orig_bytes, 1); |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 5166 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5167 | } |
| 5168 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5169 | /* |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5170 | * If we couldn't make a reservation then setup our reservation ticket |
| 5171 | * and kick the async worker if it's not already running. |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5172 | * |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5173 | * If we are a priority flusher then we just need to add our ticket to |
| 5174 | * the list and we will do our own flushing further down. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5175 | */ |
Josef Bacik | 72bcd99 | 2012-12-18 15:16:34 -0500 | [diff] [blame] | 5176 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5177 | ticket.bytes = orig_bytes; |
| 5178 | ticket.error = 0; |
| 5179 | init_waitqueue_head(&ticket.wait); |
| 5180 | if (flush == BTRFS_RESERVE_FLUSH_ALL) { |
| 5181 | list_add_tail(&ticket.list, &space_info->tickets); |
| 5182 | if (!space_info->flush) { |
| 5183 | space_info->flush = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5184 | trace_btrfs_trigger_flush(fs_info, |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 5185 | space_info->flags, |
| 5186 | orig_bytes, flush, |
| 5187 | "enospc"); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5188 | queue_work(system_unbound_wq, |
| 5189 | &root->fs_info->async_reclaim_work); |
| 5190 | } |
| 5191 | } else { |
| 5192 | list_add_tail(&ticket.list, |
| 5193 | &space_info->priority_tickets); |
| 5194 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5195 | } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
| 5196 | used += orig_bytes; |
Josef Bacik | f6acfd5 | 2014-09-18 11:27:17 -0400 | [diff] [blame] | 5197 | /* |
| 5198 | * We will do the space reservation dance during log replay, |
| 5199 | * which means we won't have fs_info->fs_root set, so don't do |
| 5200 | * the async reclaim as we will panic. |
| 5201 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5202 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) && |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 5203 | need_do_async_reclaim(space_info, root, used) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5204 | !work_busy(&fs_info->async_reclaim_work)) { |
| 5205 | trace_btrfs_trigger_flush(fs_info, space_info->flags, |
| 5206 | orig_bytes, flush, "preempt"); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5207 | queue_work(system_unbound_wq, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5208 | &fs_info->async_reclaim_work); |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 5209 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5210 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5211 | spin_unlock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5212 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5213 | return ret; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5214 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5215 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5216 | return wait_reserve_ticket(fs_info, space_info, &ticket, |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5217 | orig_bytes); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5218 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5219 | ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5220 | priority_reclaim_metadata_space(fs_info, space_info, &ticket); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5221 | spin_lock(&space_info->lock); |
| 5222 | if (ticket.bytes) { |
| 5223 | if (ticket.bytes < orig_bytes) { |
| 5224 | u64 num_bytes = orig_bytes - ticket.bytes; |
| 5225 | space_info->bytes_may_use -= num_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5226 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5227 | space_info->flags, |
| 5228 | num_bytes, 0); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5229 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5230 | } |
| 5231 | list_del_init(&ticket.list); |
| 5232 | ret = -ENOSPC; |
| 5233 | } |
| 5234 | spin_unlock(&space_info->lock); |
| 5235 | ASSERT(list_empty(&ticket.list)); |
| 5236 | return ret; |
| 5237 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5238 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5239 | /** |
| 5240 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 5241 | * @root - the root we're allocating for |
| 5242 | * @block_rsv - the block_rsv we're allocating for |
| 5243 | * @orig_bytes - the number of bytes we want |
| 5244 | * @flush - whether or not we can flush to make our reservation |
| 5245 | * |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5246 | * This will reserve orgi_bytes number of bytes from the space info associated |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5247 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 5248 | * flush out space to make room. It will do this by flushing delalloc if |
| 5249 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 5250 | * regain reservations will be made and this will fail if there is not enough |
| 5251 | * space already. |
| 5252 | */ |
| 5253 | static int reserve_metadata_bytes(struct btrfs_root *root, |
| 5254 | struct btrfs_block_rsv *block_rsv, |
| 5255 | u64 orig_bytes, |
| 5256 | enum btrfs_reserve_flush_enum flush) |
| 5257 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5258 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5259 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5260 | int ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5261 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5262 | ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes, |
| 5263 | flush); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5264 | if (ret == -ENOSPC && |
| 5265 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5266 | if (block_rsv != global_rsv && |
| 5267 | !block_rsv_use_bytes(global_rsv, orig_bytes)) |
| 5268 | ret = 0; |
| 5269 | } |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 5270 | if (ret == -ENOSPC) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5271 | trace_btrfs_space_reservation(fs_info, "space_info:enospc", |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5272 | block_rsv->space_info->flags, |
| 5273 | orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5274 | return ret; |
| 5275 | } |
| 5276 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5277 | static struct btrfs_block_rsv *get_block_rsv( |
| 5278 | const struct btrfs_trans_handle *trans, |
| 5279 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5280 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5281 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5282 | struct btrfs_block_rsv *block_rsv = NULL; |
| 5283 | |
Alexandru Moise | e9cf439 | 2015-09-09 00:18:50 +0000 | [diff] [blame] | 5284 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5285 | (root == fs_info->csum_root && trans->adding_csums) || |
| 5286 | (root == fs_info->uuid_root)) |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 5287 | block_rsv = trans->block_rsv; |
| 5288 | |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5289 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5290 | block_rsv = root->block_rsv; |
| 5291 | |
| 5292 | if (!block_rsv) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5293 | block_rsv = &fs_info->empty_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5294 | |
| 5295 | return block_rsv; |
| 5296 | } |
| 5297 | |
| 5298 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 5299 | u64 num_bytes) |
| 5300 | { |
| 5301 | int ret = -ENOSPC; |
| 5302 | spin_lock(&block_rsv->lock); |
| 5303 | if (block_rsv->reserved >= num_bytes) { |
| 5304 | block_rsv->reserved -= num_bytes; |
| 5305 | if (block_rsv->reserved < block_rsv->size) |
| 5306 | block_rsv->full = 0; |
| 5307 | ret = 0; |
| 5308 | } |
| 5309 | spin_unlock(&block_rsv->lock); |
| 5310 | return ret; |
| 5311 | } |
| 5312 | |
| 5313 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 5314 | u64 num_bytes, int update_size) |
| 5315 | { |
| 5316 | spin_lock(&block_rsv->lock); |
| 5317 | block_rsv->reserved += num_bytes; |
| 5318 | if (update_size) |
| 5319 | block_rsv->size += num_bytes; |
| 5320 | else if (block_rsv->reserved >= block_rsv->size) |
| 5321 | block_rsv->full = 1; |
| 5322 | spin_unlock(&block_rsv->lock); |
| 5323 | } |
| 5324 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 5325 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
| 5326 | struct btrfs_block_rsv *dest, u64 num_bytes, |
| 5327 | int min_factor) |
| 5328 | { |
| 5329 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5330 | u64 min_bytes; |
| 5331 | |
| 5332 | if (global_rsv->space_info != dest->space_info) |
| 5333 | return -ENOSPC; |
| 5334 | |
| 5335 | spin_lock(&global_rsv->lock); |
| 5336 | min_bytes = div_factor(global_rsv->size, min_factor); |
| 5337 | if (global_rsv->reserved < min_bytes + num_bytes) { |
| 5338 | spin_unlock(&global_rsv->lock); |
| 5339 | return -ENOSPC; |
| 5340 | } |
| 5341 | global_rsv->reserved -= num_bytes; |
| 5342 | if (global_rsv->reserved < global_rsv->size) |
| 5343 | global_rsv->full = 0; |
| 5344 | spin_unlock(&global_rsv->lock); |
| 5345 | |
| 5346 | block_rsv_add_bytes(dest, num_bytes, 1); |
| 5347 | return 0; |
| 5348 | } |
| 5349 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5350 | /* |
| 5351 | * This is for space we already have accounted in space_info->bytes_may_use, so |
| 5352 | * basically when we're returning space from block_rsv's. |
| 5353 | */ |
| 5354 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, |
| 5355 | struct btrfs_space_info *space_info, |
| 5356 | u64 num_bytes) |
| 5357 | { |
| 5358 | struct reserve_ticket *ticket; |
| 5359 | struct list_head *head; |
| 5360 | u64 used; |
| 5361 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH; |
| 5362 | bool check_overcommit = false; |
| 5363 | |
| 5364 | spin_lock(&space_info->lock); |
| 5365 | head = &space_info->priority_tickets; |
| 5366 | |
| 5367 | /* |
| 5368 | * If we are over our limit then we need to check and see if we can |
| 5369 | * overcommit, and if we can't then we just need to free up our space |
| 5370 | * and not satisfy any requests. |
| 5371 | */ |
| 5372 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 5373 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 5374 | space_info->bytes_may_use; |
| 5375 | if (used - num_bytes >= space_info->total_bytes) |
| 5376 | check_overcommit = true; |
| 5377 | again: |
| 5378 | while (!list_empty(head) && num_bytes) { |
| 5379 | ticket = list_first_entry(head, struct reserve_ticket, |
| 5380 | list); |
| 5381 | /* |
| 5382 | * We use 0 bytes because this space is already reserved, so |
| 5383 | * adding the ticket space would be a double count. |
| 5384 | */ |
| 5385 | if (check_overcommit && |
| 5386 | !can_overcommit(fs_info->extent_root, space_info, 0, |
| 5387 | flush)) |
| 5388 | break; |
| 5389 | if (num_bytes >= ticket->bytes) { |
| 5390 | list_del_init(&ticket->list); |
| 5391 | num_bytes -= ticket->bytes; |
| 5392 | ticket->bytes = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5393 | space_info->tickets_id++; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5394 | wake_up(&ticket->wait); |
| 5395 | } else { |
| 5396 | ticket->bytes -= num_bytes; |
| 5397 | num_bytes = 0; |
| 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | if (num_bytes && head == &space_info->priority_tickets) { |
| 5402 | head = &space_info->tickets; |
| 5403 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 5404 | goto again; |
| 5405 | } |
| 5406 | space_info->bytes_may_use -= num_bytes; |
| 5407 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5408 | space_info->flags, num_bytes, 0); |
| 5409 | spin_unlock(&space_info->lock); |
| 5410 | } |
| 5411 | |
| 5412 | /* |
| 5413 | * This is for newly allocated space that isn't accounted in |
| 5414 | * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent |
| 5415 | * we use this helper. |
| 5416 | */ |
| 5417 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, |
| 5418 | struct btrfs_space_info *space_info, |
| 5419 | u64 num_bytes) |
| 5420 | { |
| 5421 | struct reserve_ticket *ticket; |
| 5422 | struct list_head *head = &space_info->priority_tickets; |
| 5423 | |
| 5424 | again: |
| 5425 | while (!list_empty(head) && num_bytes) { |
| 5426 | ticket = list_first_entry(head, struct reserve_ticket, |
| 5427 | list); |
| 5428 | if (num_bytes >= ticket->bytes) { |
| 5429 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5430 | space_info->flags, |
| 5431 | ticket->bytes, 1); |
| 5432 | list_del_init(&ticket->list); |
| 5433 | num_bytes -= ticket->bytes; |
| 5434 | space_info->bytes_may_use += ticket->bytes; |
| 5435 | ticket->bytes = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5436 | space_info->tickets_id++; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5437 | wake_up(&ticket->wait); |
| 5438 | } else { |
| 5439 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5440 | space_info->flags, |
| 5441 | num_bytes, 1); |
| 5442 | space_info->bytes_may_use += num_bytes; |
| 5443 | ticket->bytes -= num_bytes; |
| 5444 | num_bytes = 0; |
| 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | if (num_bytes && head == &space_info->priority_tickets) { |
| 5449 | head = &space_info->tickets; |
| 5450 | goto again; |
| 5451 | } |
| 5452 | } |
| 5453 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5454 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 5455 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 5456 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5457 | { |
| 5458 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 5459 | |
| 5460 | spin_lock(&block_rsv->lock); |
| 5461 | if (num_bytes == (u64)-1) |
| 5462 | num_bytes = block_rsv->size; |
| 5463 | block_rsv->size -= num_bytes; |
| 5464 | if (block_rsv->reserved >= block_rsv->size) { |
| 5465 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 5466 | block_rsv->reserved = block_rsv->size; |
| 5467 | block_rsv->full = 1; |
| 5468 | } else { |
| 5469 | num_bytes = 0; |
| 5470 | } |
| 5471 | spin_unlock(&block_rsv->lock); |
| 5472 | |
| 5473 | if (num_bytes > 0) { |
| 5474 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 5475 | spin_lock(&dest->lock); |
| 5476 | if (!dest->full) { |
| 5477 | u64 bytes_to_add; |
| 5478 | |
| 5479 | bytes_to_add = dest->size - dest->reserved; |
| 5480 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 5481 | dest->reserved += bytes_to_add; |
| 5482 | if (dest->reserved >= dest->size) |
| 5483 | dest->full = 1; |
| 5484 | num_bytes -= bytes_to_add; |
| 5485 | } |
| 5486 | spin_unlock(&dest->lock); |
| 5487 | } |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5488 | if (num_bytes) |
| 5489 | space_info_add_old_bytes(fs_info, space_info, |
| 5490 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5491 | } |
| 5492 | } |
| 5493 | |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5494 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src, |
| 5495 | struct btrfs_block_rsv *dst, u64 num_bytes, |
| 5496 | int update_size) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5497 | { |
| 5498 | int ret; |
| 5499 | |
| 5500 | ret = block_rsv_use_bytes(src, num_bytes); |
| 5501 | if (ret) |
| 5502 | return ret; |
| 5503 | |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5504 | block_rsv_add_bytes(dst, num_bytes, update_size); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5505 | return 0; |
| 5506 | } |
| 5507 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5508 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5509 | { |
| 5510 | memset(rsv, 0, sizeof(*rsv)); |
| 5511 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5512 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5513 | } |
| 5514 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5515 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info, |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5516 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5517 | { |
| 5518 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5519 | |
| 5520 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 5521 | if (!block_rsv) |
| 5522 | return NULL; |
| 5523 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5524 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5525 | block_rsv->space_info = __find_space_info(fs_info, |
| 5526 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5527 | return block_rsv; |
| 5528 | } |
| 5529 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5530 | void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5531 | struct btrfs_block_rsv *rsv) |
| 5532 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5533 | if (!rsv) |
| 5534 | return; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5535 | btrfs_block_rsv_release(fs_info, rsv, (u64)-1); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5536 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5537 | } |
| 5538 | |
Chris Mason | cdfb080 | 2015-04-06 18:17:00 -0700 | [diff] [blame] | 5539 | void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv) |
| 5540 | { |
| 5541 | kfree(rsv); |
| 5542 | } |
| 5543 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5544 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 5545 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, |
| 5546 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5547 | { |
| 5548 | int ret; |
| 5549 | |
| 5550 | if (num_bytes == 0) |
| 5551 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5552 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 5553 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5554 | if (!ret) { |
| 5555 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 5556 | return 0; |
| 5557 | } |
| 5558 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5559 | return ret; |
| 5560 | } |
| 5561 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5562 | int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5563 | { |
| 5564 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5565 | int ret = -ENOSPC; |
| 5566 | |
| 5567 | if (!block_rsv) |
| 5568 | return 0; |
| 5569 | |
| 5570 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5571 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 5572 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5573 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5574 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5575 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5576 | return ret; |
| 5577 | } |
| 5578 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5579 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 5580 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, |
| 5581 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5582 | { |
| 5583 | u64 num_bytes = 0; |
| 5584 | int ret = -ENOSPC; |
| 5585 | |
| 5586 | if (!block_rsv) |
| 5587 | return 0; |
| 5588 | |
| 5589 | spin_lock(&block_rsv->lock); |
| 5590 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5591 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5592 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5593 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5594 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5595 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5596 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5597 | if (!ret) |
| 5598 | return 0; |
| 5599 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 5600 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5601 | if (!ret) { |
| 5602 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5603 | return 0; |
| 5604 | } |
| 5605 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5606 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5607 | } |
| 5608 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5609 | void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5610 | struct btrfs_block_rsv *block_rsv, |
| 5611 | u64 num_bytes) |
| 5612 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5613 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5614 | |
Liu Bo | 1750458 | 2013-12-29 21:44:50 +0800 | [diff] [blame] | 5615 | if (global_rsv == block_rsv || |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5616 | block_rsv->space_info != global_rsv->space_info) |
| 5617 | global_rsv = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5618 | block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5619 | } |
| 5620 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5621 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5622 | { |
| 5623 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 5624 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 5625 | u64 num_bytes; |
| 5626 | |
Josef Bacik | ae2e472 | 2016-05-27 12:58:35 -0400 | [diff] [blame] | 5627 | /* |
| 5628 | * The global block rsv is based on the size of the extent tree, the |
| 5629 | * checksum tree and the root tree. If the fs is empty we want to set |
| 5630 | * it to a minimal amount for safety. |
| 5631 | */ |
| 5632 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) + |
| 5633 | btrfs_root_used(&fs_info->csum_root->root_item) + |
| 5634 | btrfs_root_used(&fs_info->tree_root->root_item); |
| 5635 | num_bytes = max_t(u64, num_bytes, SZ_16M); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5636 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5637 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5638 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5639 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 5640 | block_rsv->size = min_t(u64, num_bytes, SZ_512M); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5641 | |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5642 | if (block_rsv->reserved < block_rsv->size) { |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 5643 | num_bytes = btrfs_space_info_used(sinfo, true); |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5644 | if (sinfo->total_bytes > num_bytes) { |
| 5645 | num_bytes = sinfo->total_bytes - num_bytes; |
| 5646 | num_bytes = min(num_bytes, |
| 5647 | block_rsv->size - block_rsv->reserved); |
| 5648 | block_rsv->reserved += num_bytes; |
| 5649 | sinfo->bytes_may_use += num_bytes; |
| 5650 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5651 | sinfo->flags, num_bytes, |
| 5652 | 1); |
| 5653 | } |
| 5654 | } else if (block_rsv->reserved > block_rsv->size) { |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5655 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5656 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5657 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5658 | sinfo->flags, num_bytes, 0); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5659 | block_rsv->reserved = block_rsv->size; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5660 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 5661 | |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5662 | if (block_rsv->reserved == block_rsv->size) |
| 5663 | block_rsv->full = 1; |
| 5664 | else |
| 5665 | block_rsv->full = 0; |
| 5666 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5667 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5668 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5669 | } |
| 5670 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5671 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5672 | { |
| 5673 | struct btrfs_space_info *space_info; |
| 5674 | |
| 5675 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 5676 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5677 | |
| 5678 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5679 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5680 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5681 | fs_info->trans_block_rsv.space_info = space_info; |
| 5682 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5683 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5684 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5685 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 5686 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 5687 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 5688 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Stefan Behrens | 3a6cad9 | 2013-05-16 14:48:19 +0000 | [diff] [blame] | 5689 | if (fs_info->quota_root) |
| 5690 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5691 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5692 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5693 | update_global_block_rsv(fs_info); |
| 5694 | } |
| 5695 | |
| 5696 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5697 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5698 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 5699 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5700 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 5701 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 5702 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 5703 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 5704 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 5705 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5706 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 5707 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5708 | } |
| 5709 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5710 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5711 | struct btrfs_fs_info *fs_info) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5712 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 5713 | if (!trans->block_rsv) |
| 5714 | return; |
| 5715 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5716 | if (!trans->bytes_reserved) |
| 5717 | return; |
| 5718 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5719 | trace_btrfs_space_reservation(fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5720 | trans->transid, trans->bytes_reserved, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5721 | btrfs_block_rsv_release(fs_info, trans->block_rsv, |
| 5722 | trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5723 | trans->bytes_reserved = 0; |
| 5724 | } |
| 5725 | |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5726 | /* |
| 5727 | * To be called after all the new block groups attached to the transaction |
| 5728 | * handle have been created (btrfs_create_pending_block_groups()). |
| 5729 | */ |
| 5730 | void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) |
| 5731 | { |
Jeff Mahoney | 64b6358 | 2016-06-20 17:23:41 -0400 | [diff] [blame] | 5732 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5733 | |
| 5734 | if (!trans->chunk_bytes_reserved) |
| 5735 | return; |
| 5736 | |
| 5737 | WARN_ON_ONCE(!list_empty(&trans->new_bgs)); |
| 5738 | |
| 5739 | block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL, |
| 5740 | trans->chunk_bytes_reserved); |
| 5741 | trans->chunk_bytes_reserved = 0; |
| 5742 | } |
| 5743 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5744 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5745 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 8ed7a2a | 2017-02-20 13:50:39 +0200 | [diff] [blame] | 5746 | struct btrfs_inode *inode) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5747 | { |
Nikolay Borisov | 8ed7a2a | 2017-02-20 13:50:39 +0200 | [diff] [blame] | 5748 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 5749 | struct btrfs_root *root = inode->root; |
Josef Bacik | 40acc3e | 2016-05-27 13:01:08 -0400 | [diff] [blame] | 5750 | /* |
| 5751 | * We always use trans->block_rsv here as we will have reserved space |
| 5752 | * for our orphan when starting the transaction, using get_block_rsv() |
| 5753 | * here will sometimes make us choose the wrong block rsv as we could be |
| 5754 | * doing a reloc inode for a non refcounted root. |
| 5755 | */ |
| 5756 | struct btrfs_block_rsv *src_rsv = trans->block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5757 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 5758 | |
| 5759 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5760 | * We need to hold space in order to delete our orphan item once we've |
| 5761 | * added it, so this takes the reservation so we can release it later |
| 5762 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5763 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5764 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
| 5765 | |
Nikolay Borisov | 8ed7a2a | 2017-02-20 13:50:39 +0200 | [diff] [blame] | 5766 | trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode), |
| 5767 | num_bytes, 1); |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5768 | return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5769 | } |
| 5770 | |
Nikolay Borisov | 703b391 | 2017-02-20 13:50:40 +0200 | [diff] [blame] | 5771 | void btrfs_orphan_release_metadata(struct btrfs_inode *inode) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5772 | { |
Nikolay Borisov | 703b391 | 2017-02-20 13:50:40 +0200 | [diff] [blame] | 5773 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 5774 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5775 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
| 5776 | |
Nikolay Borisov | 703b391 | 2017-02-20 13:50:40 +0200 | [diff] [blame] | 5777 | trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode), |
| 5778 | num_bytes, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5779 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5780 | } |
| 5781 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5782 | /* |
| 5783 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation |
| 5784 | * root: the root of the parent directory |
| 5785 | * rsv: block reservation |
| 5786 | * items: the number of items that we need do reservation |
| 5787 | * qgroup_reserved: used to return the reserved size in qgroup |
| 5788 | * |
| 5789 | * This function is used to reserve the space for snapshot/subvolume |
| 5790 | * creation and deletion. Those operations are different with the |
| 5791 | * common file/directory operations, they change two fs/file trees |
| 5792 | * and root tree, the number of items that the qgroup reserves is |
| 5793 | * different with the free space reservation. So we can not use |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5794 | * the space reservation mechanism in start_transaction(). |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5795 | */ |
| 5796 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, |
| 5797 | struct btrfs_block_rsv *rsv, |
| 5798 | int items, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5799 | u64 *qgroup_reserved, |
| 5800 | bool use_global_rsv) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5801 | { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5802 | u64 num_bytes; |
| 5803 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5804 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5805 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5806 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5807 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5808 | /* One for parent inode, two for dir entries */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5809 | num_bytes = 3 * fs_info->nodesize; |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 5810 | ret = btrfs_qgroup_reserve_meta(root, num_bytes, true); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5811 | if (ret) |
| 5812 | return ret; |
| 5813 | } else { |
| 5814 | num_bytes = 0; |
| 5815 | } |
| 5816 | |
| 5817 | *qgroup_reserved = num_bytes; |
| 5818 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5819 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, items); |
| 5820 | rsv->space_info = __find_space_info(fs_info, |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5821 | BTRFS_BLOCK_GROUP_METADATA); |
| 5822 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, |
| 5823 | BTRFS_RESERVE_FLUSH_ALL); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5824 | |
| 5825 | if (ret == -ENOSPC && use_global_rsv) |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5826 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5827 | |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5828 | if (ret && *qgroup_reserved) |
| 5829 | btrfs_qgroup_free_meta(root, *qgroup_reserved); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5830 | |
| 5831 | return ret; |
| 5832 | } |
| 5833 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5834 | void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info, |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 5835 | struct btrfs_block_rsv *rsv) |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5836 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5837 | btrfs_block_rsv_release(fs_info, rsv, (u64)-1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5838 | } |
| 5839 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5840 | /** |
| 5841 | * drop_outstanding_extent - drop an outstanding extent |
| 5842 | * @inode: the inode we're dropping the extent for |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5843 | * @num_bytes: the number of bytes we're releasing. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5844 | * |
| 5845 | * This is called when we are freeing up an outstanding extent, either called |
| 5846 | * after an error or after an extent is written. This will return the number of |
| 5847 | * reserved extents that need to be freed. This must be called with |
| 5848 | * BTRFS_I(inode)->lock held. |
| 5849 | */ |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5850 | static unsigned drop_outstanding_extent(struct btrfs_inode *inode, |
| 5851 | u64 num_bytes) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5852 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5853 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5854 | unsigned dropped_extents = 0; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5855 | unsigned num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5856 | |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5857 | num_extents = count_max_extents(num_bytes); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5858 | ASSERT(num_extents); |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5859 | ASSERT(inode->outstanding_extents >= num_extents); |
| 5860 | inode->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5861 | |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5862 | if (inode->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5863 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5864 | &inode->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5865 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5866 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5867 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5868 | * If we have more or the same amount of outstanding extents than we have |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5869 | * reserved then we need to leave the reserved extents count alone. |
| 5870 | */ |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5871 | if (inode->outstanding_extents >= inode->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5872 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5873 | |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5874 | dropped_extents = inode->reserved_extents - inode->outstanding_extents; |
| 5875 | inode->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5876 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5879 | /** |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5880 | * calc_csum_metadata_size - return the amount of metadata space that must be |
| 5881 | * reserved/freed for the given bytes. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5882 | * @inode: the inode we're manipulating |
| 5883 | * @num_bytes: the number of bytes in question |
| 5884 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 5885 | * |
| 5886 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 5887 | * correct amount of metadata that must either be reserved or freed. We |
| 5888 | * calculate how many checksums we can fit into one leaf and then divide the |
| 5889 | * number of bytes that will need to be checksumed by this value to figure out |
| 5890 | * how many checksums will be required. If we are adding bytes then the number |
| 5891 | * may go up and we will return the number of additional bytes that must be |
| 5892 | * reserved. If it is going down we will return the number of bytes that must |
| 5893 | * be freed. |
| 5894 | * |
| 5895 | * This must be called with BTRFS_I(inode)->lock held. |
| 5896 | */ |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5897 | static u64 calc_csum_metadata_size(struct btrfs_inode *inode, u64 num_bytes, |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5898 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5899 | { |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5900 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 5901 | u64 old_csums, num_csums; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5902 | |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5903 | if (inode->flags & BTRFS_INODE_NODATASUM && inode->csum_bytes == 0) |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5904 | return 0; |
| 5905 | |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5906 | old_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5907 | if (reserve) |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5908 | inode->csum_bytes += num_bytes; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5909 | else |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5910 | inode->csum_bytes -= num_bytes; |
| 5911 | num_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5912 | |
| 5913 | /* No change, no need to reserve more */ |
| 5914 | if (old_csums == num_csums) |
| 5915 | return 0; |
| 5916 | |
| 5917 | if (reserve) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5918 | return btrfs_calc_trans_metadata_size(fs_info, |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5919 | num_csums - old_csums); |
| 5920 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5921 | return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5922 | } |
| 5923 | |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5924 | int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5925 | { |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5926 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 5927 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5928 | struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5929 | u64 to_reserve = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5930 | u64 csum_bytes; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5931 | unsigned nr_extents; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5932 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; |
Jan Schmidt | eb6b88d | 2013-01-27 23:26:00 -0700 | [diff] [blame] | 5933 | int ret = 0; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5934 | bool delalloc_lock = true; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5935 | u64 to_free = 0; |
| 5936 | unsigned dropped; |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5937 | bool release_extra = false; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5938 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5939 | /* If we are a free space inode we need to not flush since we will be in |
| 5940 | * the middle of a transaction commit. We also don't need the delalloc |
| 5941 | * mutex since we won't race with anybody. We need this mostly to make |
| 5942 | * lockdep shut its filthy mouth. |
Josef Bacik | bac357dc | 2016-07-20 16:48:45 -0700 | [diff] [blame] | 5943 | * |
| 5944 | * If we have a transaction open (can happen if we call truncate_block |
| 5945 | * from truncate), then we need FLUSH_LIMIT so we don't deadlock. |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5946 | */ |
| 5947 | if (btrfs_is_free_space_inode(inode)) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5948 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5949 | delalloc_lock = false; |
Josef Bacik | bac357dc | 2016-07-20 16:48:45 -0700 | [diff] [blame] | 5950 | } else if (current->journal_info) { |
| 5951 | flush = BTRFS_RESERVE_FLUSH_LIMIT; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5952 | } |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 5953 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5954 | if (flush != BTRFS_RESERVE_NO_FLUSH && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5955 | btrfs_transaction_in_commit(fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5956 | schedule_timeout(1); |
| 5957 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5958 | if (delalloc_lock) |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5959 | mutex_lock(&inode->delalloc_mutex); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5960 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5961 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5962 | |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5963 | spin_lock(&inode->lock); |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5964 | nr_extents = count_max_extents(num_bytes); |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5965 | inode->outstanding_extents += nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5966 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5967 | nr_extents = 0; |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5968 | if (inode->outstanding_extents > inode->reserved_extents) |
| 5969 | nr_extents += inode->outstanding_extents - |
| 5970 | inode->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5971 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5972 | /* We always want to reserve a slot for updating the inode. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5973 | to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5974 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5975 | csum_bytes = inode->csum_bytes; |
| 5976 | spin_unlock(&inode->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5977 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5978 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5979 | ret = btrfs_qgroup_reserve_meta(root, |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 5980 | nr_extents * fs_info->nodesize, true); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5981 | if (ret) |
| 5982 | goto out_fail; |
Wang Shilong | a9870c0 | 2013-03-01 11:33:01 +0000 | [diff] [blame] | 5983 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5984 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5985 | ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5986 | if (unlikely(ret)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5987 | btrfs_qgroup_free_meta(root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5988 | nr_extents * fs_info->nodesize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5989 | goto out_fail; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5990 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5991 | |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5992 | spin_lock(&inode->lock); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5993 | if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5994 | &inode->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5995 | to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5996 | release_extra = true; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5997 | } |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5998 | inode->reserved_extents += nr_extents; |
| 5999 | spin_unlock(&inode->lock); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 6000 | |
| 6001 | if (delalloc_lock) |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6002 | mutex_unlock(&inode->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 6003 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6004 | if (to_reserve) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6005 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6006 | btrfs_ino(inode), to_reserve, 1); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 6007 | if (release_extra) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6008 | btrfs_block_rsv_release(fs_info, block_rsv, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6009 | btrfs_calc_trans_metadata_size(fs_info, 1)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6010 | return 0; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6011 | |
| 6012 | out_fail: |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6013 | spin_lock(&inode->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 6014 | dropped = drop_outstanding_extent(inode, num_bytes); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6015 | /* |
| 6016 | * If the inodes csum_bytes is the same as the original |
| 6017 | * csum_bytes then we know we haven't raced with any free()ers |
| 6018 | * so we can just reduce our inodes csum bytes and carry on. |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6019 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6020 | if (inode->csum_bytes == csum_bytes) { |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6021 | calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6022 | } else { |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6023 | u64 orig_csum_bytes = inode->csum_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6024 | u64 bytes; |
| 6025 | |
| 6026 | /* |
| 6027 | * This is tricky, but first we need to figure out how much we |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6028 | * freed from any free-ers that occurred during this |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6029 | * reservation, so we reset ->csum_bytes to the csum_bytes |
| 6030 | * before we dropped our lock, and then call the free for the |
| 6031 | * number of bytes that were freed while we were trying our |
| 6032 | * reservation. |
| 6033 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6034 | bytes = csum_bytes - inode->csum_bytes; |
| 6035 | inode->csum_bytes = csum_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6036 | to_free = calc_csum_metadata_size(inode, bytes, 0); |
| 6037 | |
| 6038 | |
| 6039 | /* |
| 6040 | * Now we need to see how much we would have freed had we not |
| 6041 | * been making this reservation and our ->csum_bytes were not |
| 6042 | * artificially inflated. |
| 6043 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6044 | inode->csum_bytes = csum_bytes - num_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6045 | bytes = csum_bytes - orig_csum_bytes; |
| 6046 | bytes = calc_csum_metadata_size(inode, bytes, 0); |
| 6047 | |
| 6048 | /* |
| 6049 | * Now reset ->csum_bytes to what it should be. If bytes is |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6050 | * more than to_free then we would have freed more space had we |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6051 | * not had an artificially high ->csum_bytes, so we need to free |
| 6052 | * the remainder. If bytes is the same or less then we don't |
| 6053 | * need to do anything, the other free-ers did the correct |
| 6054 | * thing. |
| 6055 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6056 | inode->csum_bytes = orig_csum_bytes - num_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6057 | if (bytes > to_free) |
| 6058 | to_free = bytes - to_free; |
| 6059 | else |
| 6060 | to_free = 0; |
| 6061 | } |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6062 | spin_unlock(&inode->lock); |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 6063 | if (dropped) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6064 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6065 | |
| 6066 | if (to_free) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6067 | btrfs_block_rsv_release(fs_info, block_rsv, to_free); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6068 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6069 | btrfs_ino(inode), to_free, 0); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6070 | } |
| 6071 | if (delalloc_lock) |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6072 | mutex_unlock(&inode->delalloc_mutex); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6073 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6074 | } |
| 6075 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6076 | /** |
| 6077 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 6078 | * @inode: the inode to release the reservation for |
| 6079 | * @num_bytes: the number of bytes we're releasing |
| 6080 | * |
| 6081 | * This will release the metadata reservation for an inode. This can be called |
| 6082 | * once we complete IO for a given set of bytes to release their metadata |
| 6083 | * reservations. |
| 6084 | */ |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 6085 | void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6086 | { |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 6087 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6088 | u64 to_free = 0; |
| 6089 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6090 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6091 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 6092 | spin_lock(&inode->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 6093 | dropped = drop_outstanding_extent(inode, num_bytes); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6094 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6095 | if (num_bytes) |
| 6096 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 6097 | spin_unlock(&inode->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6098 | if (dropped > 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6099 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6100 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6101 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 6102 | return; |
| 6103 | |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 6104 | trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode), |
| 6105 | to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 6106 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6107 | btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6108 | } |
| 6109 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6110 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6111 | * btrfs_delalloc_reserve_space - reserve data and metadata space for |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6112 | * delalloc |
| 6113 | * @inode: inode we're writing to |
| 6114 | * @start: start range we are writing to |
| 6115 | * @len: how long the range we are writing to |
| 6116 | * |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6117 | * This will do the following things |
| 6118 | * |
| 6119 | * o reserve space in data space info for num bytes |
| 6120 | * and reserve precious corresponding qgroup space |
| 6121 | * (Done in check_data_free_space) |
| 6122 | * |
| 6123 | * o reserve space for metadata space, based on the number of outstanding |
| 6124 | * extents and how much csums will be needed |
| 6125 | * also reserve metadata space in a per root over-reserve method. |
| 6126 | * o add to the inodes->delalloc_bytes |
| 6127 | * o add it to the fs_info's delalloc inodes list. |
| 6128 | * (Above 3 all done in delalloc_reserve_metadata) |
| 6129 | * |
| 6130 | * Return 0 for success |
| 6131 | * Return <0 for error(-ENOSPC or -EQUOT) |
| 6132 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6133 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6134 | { |
| 6135 | int ret; |
| 6136 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6137 | ret = btrfs_check_data_free_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6138 | if (ret < 0) |
| 6139 | return ret; |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6140 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6141 | if (ret < 0) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6142 | btrfs_free_reserved_data_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6143 | return ret; |
| 6144 | } |
| 6145 | |
| 6146 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6147 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6148 | * @inode: inode we're releasing space for |
| 6149 | * @start: start position of the space already reserved |
| 6150 | * @len: the len of the space already reserved |
| 6151 | * |
| 6152 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 6153 | * called in the case that we don't need the metadata AND data reservations |
| 6154 | * anymore. So if there is an error or we insert an inline extent. |
| 6155 | * |
| 6156 | * This function will release the metadata space that was not used and will |
| 6157 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 6158 | * list if there are no delalloc bytes left. |
| 6159 | * Also it will handle the qgroup reserved space. |
| 6160 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6161 | void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6162 | { |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame] | 6163 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len); |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6164 | btrfs_free_reserved_data_space(inode, start, len); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6165 | } |
| 6166 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6167 | static int update_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 6168 | struct btrfs_fs_info *info, u64 bytenr, |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6169 | u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6170 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6171 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6172 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6173 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6174 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6175 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 6176 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6177 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 6178 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6179 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6180 | if (alloc) |
| 6181 | old_val += num_bytes; |
| 6182 | else |
| 6183 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6184 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 6185 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6186 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6187 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6188 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6189 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6190 | return -ENOENT; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6191 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 6192 | BTRFS_BLOCK_GROUP_RAID1 | |
| 6193 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6194 | factor = 2; |
| 6195 | else |
| 6196 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 6197 | /* |
| 6198 | * If this block group has free space cache written out, we |
| 6199 | * need to make sure to load it if we are removing space. This |
| 6200 | * is because we need the unpinning stage to actually add the |
| 6201 | * space back to the block group, otherwise we will leak space. |
| 6202 | */ |
| 6203 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6204 | cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6205 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6206 | byte_in_group = bytenr - cache->key.objectid; |
| 6207 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6208 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6209 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6210 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6211 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 6212 | if (btrfs_test_opt(info, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6213 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 6214 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 6215 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6216 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6217 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6218 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6219 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6220 | btrfs_set_block_group_used(&cache->item, old_val); |
| 6221 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6222 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6223 | cache->space_info->bytes_used += num_bytes; |
| 6224 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6225 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6226 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6227 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6228 | old_val -= num_bytes; |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 6229 | btrfs_set_block_group_used(&cache->item, old_val); |
| 6230 | cache->pinned += num_bytes; |
| 6231 | cache->space_info->bytes_pinned += num_bytes; |
| 6232 | cache->space_info->bytes_used -= num_bytes; |
| 6233 | cache->space_info->disk_used -= num_bytes * factor; |
| 6234 | spin_unlock(&cache->lock); |
| 6235 | spin_unlock(&cache->space_info->lock); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 6236 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6237 | trace_btrfs_space_reservation(info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6238 | cache->space_info->flags, |
| 6239 | num_bytes, 1); |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 6240 | set_extent_dirty(info->pinned_extents, |
| 6241 | bytenr, bytenr + num_bytes - 1, |
| 6242 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6243 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 6244 | |
| 6245 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 6246 | if (list_empty(&cache->dirty_list)) { |
| 6247 | list_add_tail(&cache->dirty_list, |
| 6248 | &trans->transaction->dirty_bgs); |
| 6249 | trans->transaction->num_dirty_bgs++; |
| 6250 | btrfs_get_block_group(cache); |
| 6251 | } |
| 6252 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 6253 | |
Filipe Manana | 036a934 | 2015-11-23 15:25:16 +0000 | [diff] [blame] | 6254 | /* |
| 6255 | * No longer have used bytes in this block group, queue it for |
| 6256 | * deletion. We do this after adding the block group to the |
| 6257 | * dirty list to avoid races between cleaner kthread and space |
| 6258 | * cache writeout. |
| 6259 | */ |
| 6260 | if (!alloc && old_val == 0) { |
| 6261 | spin_lock(&info->unused_bgs_lock); |
| 6262 | if (list_empty(&cache->bg_list)) { |
| 6263 | btrfs_get_block_group(cache); |
| 6264 | list_add_tail(&cache->bg_list, |
| 6265 | &info->unused_bgs); |
| 6266 | } |
| 6267 | spin_unlock(&info->unused_bgs_lock); |
| 6268 | } |
| 6269 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6270 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6271 | total -= num_bytes; |
| 6272 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6273 | } |
| 6274 | return 0; |
| 6275 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6276 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6277 | 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] | 6278 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6279 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6280 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6281 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6282 | spin_lock(&fs_info->block_group_cache_lock); |
| 6283 | bytenr = fs_info->first_logical_byte; |
| 6284 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 6285 | |
| 6286 | if (bytenr < (u64)-1) |
| 6287 | return bytenr; |
| 6288 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6289 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6290 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6291 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6292 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6293 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6294 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6295 | |
| 6296 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6297 | } |
| 6298 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6299 | static int pin_down_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6300 | struct btrfs_block_group_cache *cache, |
| 6301 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6302 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6303 | spin_lock(&cache->space_info->lock); |
| 6304 | spin_lock(&cache->lock); |
| 6305 | cache->pinned += num_bytes; |
| 6306 | cache->space_info->bytes_pinned += num_bytes; |
| 6307 | if (reserved) { |
| 6308 | cache->reserved -= num_bytes; |
| 6309 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6310 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6311 | spin_unlock(&cache->lock); |
| 6312 | spin_unlock(&cache->space_info->lock); |
| 6313 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6314 | trace_btrfs_space_reservation(fs_info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6315 | cache->space_info->flags, num_bytes, 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6316 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6317 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6318 | return 0; |
| 6319 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6320 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6321 | /* |
| 6322 | * this function must be called within transaction |
| 6323 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6324 | int btrfs_pin_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6325 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6326 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6327 | struct btrfs_block_group_cache *cache; |
| 6328 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6329 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6330 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6331 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6332 | pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6333 | |
| 6334 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6335 | return 0; |
| 6336 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6337 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6338 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6339 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6340 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6341 | 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] | 6342 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6343 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6344 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6345 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6346 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6347 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6348 | if (!cache) |
| 6349 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6350 | |
| 6351 | /* |
| 6352 | * pull in the free space cache (if any) so that our pin |
| 6353 | * removes the free space from the cache. We have load_only set |
| 6354 | * to one because the slow code to read in the free extents does check |
| 6355 | * the pinned extents. |
| 6356 | */ |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6357 | cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6358 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6359 | pin_down_extent(fs_info, cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6360 | |
| 6361 | /* 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] | 6362 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6363 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6364 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6365 | } |
| 6366 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6367 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 6368 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6369 | { |
| 6370 | int ret; |
| 6371 | struct btrfs_block_group_cache *block_group; |
| 6372 | struct btrfs_caching_control *caching_ctl; |
| 6373 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6374 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6375 | if (!block_group) |
| 6376 | return -EINVAL; |
| 6377 | |
| 6378 | cache_block_group(block_group, 0); |
| 6379 | caching_ctl = get_caching_control(block_group); |
| 6380 | |
| 6381 | if (!caching_ctl) { |
| 6382 | /* Logic error */ |
| 6383 | BUG_ON(!block_group_cache_done(block_group)); |
| 6384 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 6385 | } else { |
| 6386 | mutex_lock(&caching_ctl->mutex); |
| 6387 | |
| 6388 | if (start >= caching_ctl->progress) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6389 | ret = add_excluded_extent(fs_info, start, num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6390 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 6391 | ret = btrfs_remove_free_space(block_group, |
| 6392 | start, num_bytes); |
| 6393 | } else { |
| 6394 | num_bytes = caching_ctl->progress - start; |
| 6395 | ret = btrfs_remove_free_space(block_group, |
| 6396 | start, num_bytes); |
| 6397 | if (ret) |
| 6398 | goto out_lock; |
| 6399 | |
| 6400 | num_bytes = (start + num_bytes) - |
| 6401 | caching_ctl->progress; |
| 6402 | start = caching_ctl->progress; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6403 | ret = add_excluded_extent(fs_info, start, num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6404 | } |
| 6405 | out_lock: |
| 6406 | mutex_unlock(&caching_ctl->mutex); |
| 6407 | put_caching_control(caching_ctl); |
| 6408 | } |
| 6409 | btrfs_put_block_group(block_group); |
| 6410 | return ret; |
| 6411 | } |
| 6412 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6413 | int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info, |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6414 | struct extent_buffer *eb) |
| 6415 | { |
| 6416 | struct btrfs_file_extent_item *item; |
| 6417 | struct btrfs_key key; |
| 6418 | int found_type; |
| 6419 | int i; |
| 6420 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6421 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6422 | return 0; |
| 6423 | |
| 6424 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 6425 | btrfs_item_key_to_cpu(eb, &key, i); |
| 6426 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 6427 | continue; |
| 6428 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 6429 | found_type = btrfs_file_extent_type(eb, item); |
| 6430 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 6431 | continue; |
| 6432 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 6433 | continue; |
| 6434 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 6435 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6436 | __exclude_logged_extent(fs_info, key.objectid, key.offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6437 | } |
| 6438 | |
| 6439 | return 0; |
| 6440 | } |
| 6441 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 6442 | static void |
| 6443 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 6444 | { |
| 6445 | atomic_inc(&bg->reservations); |
| 6446 | } |
| 6447 | |
| 6448 | void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, |
| 6449 | const u64 start) |
| 6450 | { |
| 6451 | struct btrfs_block_group_cache *bg; |
| 6452 | |
| 6453 | bg = btrfs_lookup_block_group(fs_info, start); |
| 6454 | ASSERT(bg); |
| 6455 | if (atomic_dec_and_test(&bg->reservations)) |
| 6456 | wake_up_atomic_t(&bg->reservations); |
| 6457 | btrfs_put_block_group(bg); |
| 6458 | } |
| 6459 | |
| 6460 | static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a) |
| 6461 | { |
| 6462 | schedule(); |
| 6463 | return 0; |
| 6464 | } |
| 6465 | |
| 6466 | void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 6467 | { |
| 6468 | struct btrfs_space_info *space_info = bg->space_info; |
| 6469 | |
| 6470 | ASSERT(bg->ro); |
| 6471 | |
| 6472 | if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 6473 | return; |
| 6474 | |
| 6475 | /* |
| 6476 | * Our block group is read only but before we set it to read only, |
| 6477 | * some task might have had allocated an extent from it already, but it |
| 6478 | * has not yet created a respective ordered extent (and added it to a |
| 6479 | * root's list of ordered extents). |
| 6480 | * Therefore wait for any task currently allocating extents, since the |
| 6481 | * block group's reservations counter is incremented while a read lock |
| 6482 | * on the groups' semaphore is held and decremented after releasing |
| 6483 | * the read access on that semaphore and creating the ordered extent. |
| 6484 | */ |
| 6485 | down_write(&space_info->groups_sem); |
| 6486 | up_write(&space_info->groups_sem); |
| 6487 | |
| 6488 | wait_on_atomic_t(&bg->reservations, |
| 6489 | btrfs_wait_bg_reservations_atomic_t, |
| 6490 | TASK_UNINTERRUPTIBLE); |
| 6491 | } |
| 6492 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6493 | /** |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6494 | * btrfs_add_reserved_bytes - update the block_group and space info counters |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6495 | * @cache: The cache we are manipulating |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6496 | * @ram_bytes: The number of bytes of file content, and will be same to |
| 6497 | * @num_bytes except for the compress path. |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6498 | * @num_bytes: The number of bytes in question |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6499 | * @delalloc: The blocks are allocated for the delalloc write |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6500 | * |
Xiaoguang Wang | 745699e | 2016-09-23 12:38:50 +0800 | [diff] [blame] | 6501 | * This is called by the allocator when it reserves space. If this is a |
| 6502 | * reservation and the block group has become read only we cannot make the |
| 6503 | * reservation and return -EAGAIN, otherwise this function always succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6504 | */ |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6505 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6506 | u64 ram_bytes, u64 num_bytes, int delalloc) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6507 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6508 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6509 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6510 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6511 | spin_lock(&space_info->lock); |
| 6512 | spin_lock(&cache->lock); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6513 | if (cache->ro) { |
| 6514 | ret = -EAGAIN; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6515 | } else { |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6516 | cache->reserved += num_bytes; |
| 6517 | space_info->bytes_reserved += num_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6518 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6519 | trace_btrfs_space_reservation(cache->fs_info, |
| 6520 | "space_info", space_info->flags, |
| 6521 | ram_bytes, 0); |
| 6522 | space_info->bytes_may_use -= ram_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6523 | if (delalloc) |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6524 | cache->delalloc_bytes += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6525 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6526 | spin_unlock(&cache->lock); |
| 6527 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6528 | return ret; |
| 6529 | } |
| 6530 | |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6531 | /** |
| 6532 | * btrfs_free_reserved_bytes - update the block_group and space info counters |
| 6533 | * @cache: The cache we are manipulating |
| 6534 | * @num_bytes: The number of bytes in question |
| 6535 | * @delalloc: The blocks are allocated for the delalloc write |
| 6536 | * |
| 6537 | * This is called by somebody who is freeing space that was never actually used |
| 6538 | * on disk. For example if you reserve some space for a new leaf in transaction |
| 6539 | * A and before transaction A commits you free that leaf, you call this with |
| 6540 | * reserve set to 0 in order to clear the reservation. |
| 6541 | */ |
| 6542 | |
| 6543 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 6544 | u64 num_bytes, int delalloc) |
| 6545 | { |
| 6546 | struct btrfs_space_info *space_info = cache->space_info; |
| 6547 | int ret = 0; |
| 6548 | |
| 6549 | spin_lock(&space_info->lock); |
| 6550 | spin_lock(&cache->lock); |
| 6551 | if (cache->ro) |
| 6552 | space_info->bytes_readonly += num_bytes; |
| 6553 | cache->reserved -= num_bytes; |
| 6554 | space_info->bytes_reserved -= num_bytes; |
| 6555 | |
| 6556 | if (delalloc) |
| 6557 | cache->delalloc_bytes -= num_bytes; |
| 6558 | spin_unlock(&cache->lock); |
| 6559 | spin_unlock(&space_info->lock); |
| 6560 | return ret; |
| 6561 | } |
David Sterba | 8b74c03 | 2017-02-10 19:20:56 +0100 | [diff] [blame] | 6562 | void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6563 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6564 | struct btrfs_caching_control *next; |
| 6565 | struct btrfs_caching_control *caching_ctl; |
| 6566 | struct btrfs_block_group_cache *cache; |
| 6567 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6568 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6569 | |
| 6570 | list_for_each_entry_safe(caching_ctl, next, |
| 6571 | &fs_info->caching_block_groups, list) { |
| 6572 | cache = caching_ctl->block_group; |
| 6573 | if (block_group_cache_done(cache)) { |
| 6574 | cache->last_byte_to_unpin = (u64)-1; |
| 6575 | list_del_init(&caching_ctl->list); |
| 6576 | put_caching_control(caching_ctl); |
| 6577 | } else { |
| 6578 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 6579 | } |
| 6580 | } |
| 6581 | |
| 6582 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6583 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 6584 | else |
| 6585 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 6586 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6587 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6588 | |
| 6589 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6590 | } |
| 6591 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6592 | /* |
| 6593 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 6594 | * what it should be based on the mount options. |
| 6595 | */ |
| 6596 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6597 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 6598 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6599 | { |
| 6600 | struct btrfs_free_cluster *ret = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6601 | bool ssd = btrfs_test_opt(fs_info, SSD); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6602 | |
| 6603 | *empty_cluster = 0; |
| 6604 | if (btrfs_mixed_space_info(space_info)) |
| 6605 | return ret; |
| 6606 | |
| 6607 | if (ssd) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 6608 | *empty_cluster = SZ_2M; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6609 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6610 | ret = &fs_info->meta_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6611 | if (!ssd) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 6612 | *empty_cluster = SZ_64K; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6613 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6614 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6615 | } |
| 6616 | |
| 6617 | return ret; |
| 6618 | } |
| 6619 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6620 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 6621 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6622 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6623 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6624 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6625 | struct btrfs_space_info *space_info; |
| 6626 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6627 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6628 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6629 | u64 total_unpinned = 0; |
| 6630 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6631 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6632 | |
| 6633 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6634 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6635 | if (!cache || |
| 6636 | start >= cache->key.objectid + cache->key.offset) { |
| 6637 | if (cache) |
| 6638 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6639 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6640 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6641 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6642 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6643 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6644 | cache->space_info, |
| 6645 | &empty_cluster); |
| 6646 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6647 | } |
| 6648 | |
| 6649 | len = cache->key.objectid + cache->key.offset - start; |
| 6650 | len = min(len, end + 1 - start); |
| 6651 | |
| 6652 | if (start < cache->last_byte_to_unpin) { |
| 6653 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6654 | if (return_free_space) |
| 6655 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6656 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6657 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6658 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6659 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6660 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6661 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6662 | /* |
| 6663 | * If this space cluster has been marked as fragmented and we've |
| 6664 | * unpinned enough in this block group to potentially allow a |
| 6665 | * cluster to be created inside of it go ahead and clear the |
| 6666 | * fragmented check. |
| 6667 | */ |
| 6668 | if (cluster && cluster->fragmented && |
| 6669 | total_unpinned > empty_cluster) { |
| 6670 | spin_lock(&cluster->lock); |
| 6671 | cluster->fragmented = 0; |
| 6672 | spin_unlock(&cluster->lock); |
| 6673 | } |
| 6674 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6675 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6676 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6677 | cache->pinned -= len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6678 | space_info->bytes_pinned -= len; |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6679 | |
| 6680 | trace_btrfs_space_reservation(fs_info, "pinned", |
| 6681 | space_info->flags, len, 0); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 6682 | space_info->max_extent_size = 0; |
Liu Bo | d288db5 | 2014-07-02 16:58:01 +0800 | [diff] [blame] | 6683 | percpu_counter_add(&space_info->total_bytes_pinned, -len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6684 | if (cache->ro) { |
| 6685 | space_info->bytes_readonly += len; |
| 6686 | readonly = true; |
| 6687 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6688 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6689 | if (!readonly && return_free_space && |
| 6690 | global_rsv->space_info == space_info) { |
| 6691 | u64 to_add = len; |
| 6692 | WARN_ON(!return_free_space); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6693 | spin_lock(&global_rsv->lock); |
| 6694 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6695 | to_add = min(len, global_rsv->size - |
| 6696 | global_rsv->reserved); |
| 6697 | global_rsv->reserved += to_add; |
| 6698 | space_info->bytes_may_use += to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6699 | if (global_rsv->reserved >= global_rsv->size) |
| 6700 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6701 | trace_btrfs_space_reservation(fs_info, |
| 6702 | "space_info", |
| 6703 | space_info->flags, |
| 6704 | to_add, 1); |
| 6705 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6706 | } |
| 6707 | spin_unlock(&global_rsv->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6708 | /* Add to any tickets we may have */ |
| 6709 | if (len) |
| 6710 | space_info_add_new_bytes(fs_info, space_info, |
| 6711 | len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6712 | } |
| 6713 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6717 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 6718 | return 0; |
| 6719 | } |
| 6720 | |
| 6721 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6722 | struct btrfs_fs_info *fs_info) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6723 | { |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6724 | struct btrfs_block_group_cache *block_group, *tmp; |
| 6725 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6726 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6727 | u64 start; |
| 6728 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6729 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6730 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6731 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6732 | unpin = &fs_info->freed_extents[1]; |
| 6733 | else |
| 6734 | unpin = &fs_info->freed_extents[0]; |
| 6735 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6736 | while (!trans->aborted) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6737 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6738 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 6739 | EXTENT_DIRTY, NULL); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6740 | if (ret) { |
| 6741 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6742 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6743 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6744 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6745 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6746 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 6747 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6748 | |
David Sterba | af6f8f6 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 6749 | clear_extent_dirty(unpin, start, end); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6750 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6751 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6752 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6753 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6754 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6755 | /* |
| 6756 | * Transaction is finished. We don't need the lock anymore. We |
| 6757 | * do need to clean up the block groups in case of a transaction |
| 6758 | * abort. |
| 6759 | */ |
| 6760 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 6761 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 6762 | u64 trimmed = 0; |
| 6763 | |
| 6764 | ret = -EROFS; |
| 6765 | if (!trans->aborted) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6766 | ret = btrfs_discard_extent(fs_info, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6767 | block_group->key.objectid, |
| 6768 | block_group->key.offset, |
| 6769 | &trimmed); |
| 6770 | |
| 6771 | list_del_init(&block_group->bg_list); |
| 6772 | btrfs_put_block_group_trimming(block_group); |
| 6773 | btrfs_put_block_group(block_group); |
| 6774 | |
| 6775 | if (ret) { |
| 6776 | const char *errstr = btrfs_decode_error(ret); |
| 6777 | btrfs_warn(fs_info, |
| 6778 | "Discard failed while removing blockgroup: errno=%d %s\n", |
| 6779 | ret, errstr); |
| 6780 | } |
| 6781 | } |
| 6782 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 6783 | return 0; |
| 6784 | } |
| 6785 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6786 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
| 6787 | u64 owner, u64 root_objectid) |
| 6788 | { |
| 6789 | struct btrfs_space_info *space_info; |
| 6790 | u64 flags; |
| 6791 | |
| 6792 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 6793 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) |
| 6794 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 6795 | else |
| 6796 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 6797 | } else { |
| 6798 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 6799 | } |
| 6800 | |
| 6801 | space_info = __find_space_info(fs_info, flags); |
| 6802 | BUG_ON(!space_info); /* Logic bug */ |
| 6803 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); |
| 6804 | } |
| 6805 | |
| 6806 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6807 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6808 | struct btrfs_fs_info *info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6809 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6810 | u64 root_objectid, u64 owner_objectid, |
| 6811 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6812 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6813 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6814 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6815 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 6816 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6817 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6818 | struct btrfs_extent_item *ei; |
| 6819 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6820 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6821 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6822 | int extent_slot = 0; |
| 6823 | int found_extent = 0; |
| 6824 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6825 | u32 item_size; |
| 6826 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6827 | u64 bytenr = node->bytenr; |
| 6828 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6829 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6830 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 6831 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6832 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6833 | if (!path) |
| 6834 | return -ENOMEM; |
| 6835 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6836 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6837 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6838 | |
| 6839 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 6840 | BUG_ON(!is_data && refs_to_drop != 1); |
| 6841 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6842 | if (is_data) |
| 6843 | skinny_metadata = 0; |
| 6844 | |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 6845 | ret = lookup_extent_backref(trans, info, path, &iref, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6846 | bytenr, num_bytes, parent, |
| 6847 | root_objectid, owner_objectid, |
| 6848 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6849 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6850 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6851 | while (extent_slot >= 0) { |
| 6852 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6853 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6854 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6855 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6856 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6857 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6858 | found_extent = 1; |
| 6859 | break; |
| 6860 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6861 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 6862 | key.offset == owner_objectid) { |
| 6863 | found_extent = 1; |
| 6864 | break; |
| 6865 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6866 | if (path->slots[0] - extent_slot > 5) |
| 6867 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6868 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6869 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6870 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6871 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 6872 | if (found_extent && item_size < sizeof(*ei)) |
| 6873 | found_extent = 0; |
| 6874 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6875 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6876 | BUG_ON(iref); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 6877 | ret = remove_extent_backref(trans, info, path, NULL, |
| 6878 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6879 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6880 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6881 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6882 | goto out; |
| 6883 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6884 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6885 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6886 | |
| 6887 | key.objectid = bytenr; |
| 6888 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6889 | key.offset = num_bytes; |
| 6890 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6891 | if (!is_data && skinny_metadata) { |
| 6892 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 6893 | key.offset = owner_objectid; |
| 6894 | } |
| 6895 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6896 | ret = btrfs_search_slot(trans, extent_root, |
| 6897 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6898 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 6899 | /* |
| 6900 | * Couldn't find our skinny metadata item, |
| 6901 | * see if we have ye olde extent item. |
| 6902 | */ |
| 6903 | path->slots[0]--; |
| 6904 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 6905 | path->slots[0]); |
| 6906 | if (key.objectid == bytenr && |
| 6907 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6908 | key.offset == num_bytes) |
| 6909 | ret = 0; |
| 6910 | } |
| 6911 | |
| 6912 | if (ret > 0 && skinny_metadata) { |
| 6913 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 6914 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6915 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6916 | key.offset = num_bytes; |
| 6917 | btrfs_release_path(path); |
| 6918 | ret = btrfs_search_slot(trans, extent_root, |
| 6919 | &key, path, -1, 1); |
| 6920 | } |
| 6921 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6922 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6923 | btrfs_err(info, |
| 6924 | "umm, got %d back from search, was looking for %llu", |
| 6925 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 6926 | if (ret > 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6927 | btrfs_print_leaf(info, path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6928 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6929 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6930 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6931 | goto out; |
| 6932 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6933 | extent_slot = path->slots[0]; |
| 6934 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6935 | } else if (WARN_ON(ret == -ENOENT)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6936 | btrfs_print_leaf(info, path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6937 | btrfs_err(info, |
| 6938 | "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] | 6939 | bytenr, parent, root_objectid, owner_objectid, |
| 6940 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6941 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 6942 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6943 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6944 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6945 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6946 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6947 | |
| 6948 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6949 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6950 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6951 | if (item_size < sizeof(*ei)) { |
| 6952 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 6953 | ret = convert_extent_item_v0(trans, info, path, owner_objectid, |
| 6954 | 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6955 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6956 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6957 | goto out; |
| 6958 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6959 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6960 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6961 | path->leave_spinning = 1; |
| 6962 | |
| 6963 | key.objectid = bytenr; |
| 6964 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6965 | key.offset = num_bytes; |
| 6966 | |
| 6967 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 6968 | -1, 1); |
| 6969 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6970 | btrfs_err(info, |
| 6971 | "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6972 | ret, bytenr); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6973 | btrfs_print_leaf(info, path->nodes[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6974 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6975 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6976 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6977 | goto out; |
| 6978 | } |
| 6979 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6980 | extent_slot = path->slots[0]; |
| 6981 | leaf = path->nodes[0]; |
| 6982 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6983 | } |
| 6984 | #endif |
| 6985 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6986 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 6987 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6988 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 6989 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6990 | struct btrfs_tree_block_info *bi; |
| 6991 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 6992 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 6993 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6994 | } |
| 6995 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6996 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 6997 | if (refs < refs_to_drop) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6998 | btrfs_err(info, |
| 6999 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", |
| 7000 | refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7001 | ret = -EINVAL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7002 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7003 | goto out; |
| 7004 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7005 | refs -= refs_to_drop; |
| 7006 | |
| 7007 | if (refs > 0) { |
| 7008 | if (extent_op) |
| 7009 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 7010 | /* |
| 7011 | * In the case of inline back ref, reference count will |
| 7012 | * be updated by remove_extent_backref |
| 7013 | */ |
| 7014 | if (iref) { |
| 7015 | BUG_ON(!found_extent); |
| 7016 | } else { |
| 7017 | btrfs_set_extent_refs(leaf, ei, refs); |
| 7018 | btrfs_mark_buffer_dirty(leaf); |
| 7019 | } |
| 7020 | if (found_extent) { |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 7021 | ret = remove_extent_backref(trans, info, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7022 | iref, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7023 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7024 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7025 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7026 | goto out; |
| 7027 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7028 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7029 | add_pinned_bytes(info, -num_bytes, owner_objectid, |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7030 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7031 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7032 | if (found_extent) { |
| 7033 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 7034 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7035 | if (iref) { |
| 7036 | BUG_ON(path->slots[0] != extent_slot); |
| 7037 | } else { |
| 7038 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 7039 | path->slots[0] = extent_slot; |
| 7040 | num_to_del = 2; |
| 7041 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 7042 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7043 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7044 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 7045 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 7046 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7047 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7048 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7049 | goto out; |
| 7050 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7051 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 7052 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7053 | if (is_data) { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7054 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7055 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7056 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7057 | goto out; |
| 7058 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 7059 | } |
| 7060 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7061 | ret = add_to_free_space_tree(trans, info, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7062 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7063 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7064 | goto out; |
| 7065 | } |
| 7066 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7067 | ret = update_block_group(trans, info, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7068 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7069 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7070 | goto out; |
| 7071 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7072 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7073 | btrfs_release_path(path); |
| 7074 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7075 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7076 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7077 | return ret; |
| 7078 | } |
| 7079 | |
| 7080 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7081 | * 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] | 7082 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 7083 | * a given extent, and if there are no other delayed refs to be processed, it |
| 7084 | * removes it from the tree. |
| 7085 | */ |
| 7086 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7087 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7088 | { |
| 7089 | struct btrfs_delayed_ref_head *head; |
| 7090 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7091 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7092 | |
| 7093 | delayed_refs = &trans->transaction->delayed_refs; |
| 7094 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 7095 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7096 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 7097 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7098 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7099 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 7100 | if (!list_empty(&head->ref_list)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7101 | goto out; |
| 7102 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7103 | if (head->extent_op) { |
| 7104 | if (!head->must_insert_reserved) |
| 7105 | goto out; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 7106 | btrfs_free_delayed_extent_op(head->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7107 | head->extent_op = NULL; |
| 7108 | } |
| 7109 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7110 | /* |
| 7111 | * waiting for the lock here would deadlock. If someone else has it |
| 7112 | * locked they are already in the process of dropping it anyway |
| 7113 | */ |
| 7114 | if (!mutex_trylock(&head->mutex)) |
| 7115 | goto out; |
| 7116 | |
| 7117 | /* |
| 7118 | * at this point we have a head with no other entries. Go |
| 7119 | * ahead and process it. |
| 7120 | */ |
| 7121 | head->node.in_tree = 0; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 7122 | rb_erase(&head->href_node, &delayed_refs->href_root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7123 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7124 | atomic_dec(&delayed_refs->num_entries); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7125 | |
| 7126 | /* |
| 7127 | * we don't take a ref on the node because we're removing it from the |
| 7128 | * tree, so we just steal the ref the tree was holding. |
| 7129 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7130 | delayed_refs->num_heads--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7131 | if (head->processing == 0) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7132 | delayed_refs->num_heads_ready--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7133 | head->processing = 0; |
| 7134 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7135 | spin_unlock(&delayed_refs->lock); |
| 7136 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7137 | BUG_ON(head->extent_op); |
| 7138 | if (head->must_insert_reserved) |
| 7139 | ret = 1; |
| 7140 | |
| 7141 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7142 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7143 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7144 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7145 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 7146 | |
| 7147 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7148 | spin_unlock(&delayed_refs->lock); |
| 7149 | return 0; |
| 7150 | } |
| 7151 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7152 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 7153 | struct btrfs_root *root, |
| 7154 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7155 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7156 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7157 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7158 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7159 | int ret; |
| 7160 | |
| 7161 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7162 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
| 7163 | buf->start, buf->len, |
| 7164 | parent, |
| 7165 | root->root_key.objectid, |
| 7166 | btrfs_header_level(buf), |
| 7167 | BTRFS_DROP_DELAYED_REF, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7168 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | if (!last_ref) |
| 7172 | return; |
| 7173 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7174 | if (btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7175 | struct btrfs_block_group_cache *cache; |
| 7176 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7177 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7178 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7179 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7180 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7181 | } |
| 7182 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7183 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7184 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7185 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7186 | pin_down_extent(fs_info, cache, buf->start, |
| 7187 | buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7188 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7189 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 7193 | |
| 7194 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 7195 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7196 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7197 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7198 | pin = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7199 | } |
| 7200 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7201 | if (pin) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7202 | add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf), |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7203 | root->root_key.objectid); |
| 7204 | |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 7205 | /* |
| 7206 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 7207 | * anymore. |
| 7208 | */ |
| 7209 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7210 | } |
| 7211 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7212 | /* Can return -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7213 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 7214 | struct btrfs_fs_info *fs_info, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7215 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7216 | u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7217 | { |
| 7218 | int ret; |
| 7219 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 7220 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7221 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 7222 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7223 | add_pinned_bytes(fs_info, num_bytes, owner, root_objectid); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7224 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7225 | /* |
| 7226 | * tree log blocks never actually go into the extent allocation |
| 7227 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7228 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7229 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 7230 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7231 | /* unlocks the pinned mutex */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7232 | btrfs_pin_extent(fs_info, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7233 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7234 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7235 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 7236 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7237 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7238 | BTRFS_DROP_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7239 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7240 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 7241 | num_bytes, |
| 7242 | parent, root_objectid, owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 7243 | offset, 0, |
Jeff Mahoney | fef394f | 2016-12-13 14:39:34 -0500 | [diff] [blame] | 7244 | BTRFS_DROP_DELAYED_REF); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7245 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7246 | return ret; |
| 7247 | } |
| 7248 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7249 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7250 | * when we wait for progress in the block group caching, its because |
| 7251 | * our allocation attempt failed at least once. So, we must sleep |
| 7252 | * and let some progress happen before we try again. |
| 7253 | * |
| 7254 | * This function will sleep at least once waiting for new free space to |
| 7255 | * show up, and then it will check the block group free space numbers |
| 7256 | * for our min num_bytes. Another option is to have it go ahead |
| 7257 | * and look in the rbtree for a free extent of a given size, but this |
| 7258 | * is a good start. |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7259 | * |
| 7260 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 7261 | * any of the information in this block group. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7262 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7263 | static noinline void |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7264 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 7265 | u64 num_bytes) |
| 7266 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7267 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7268 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7269 | caching_ctl = get_caching_control(cache); |
| 7270 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7271 | return; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7272 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7273 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7274 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7275 | |
| 7276 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7277 | } |
| 7278 | |
| 7279 | static noinline int |
| 7280 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 7281 | { |
| 7282 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7283 | int ret = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7284 | |
| 7285 | caching_ctl = get_caching_control(cache); |
| 7286 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7287 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7288 | |
| 7289 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7290 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 7291 | ret = -EIO; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7292 | put_caching_control(caching_ctl); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7293 | return ret; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7294 | } |
| 7295 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 7296 | int __get_raid_index(u64 flags) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7297 | { |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7298 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7299 | return BTRFS_RAID_RAID10; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7300 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7301 | return BTRFS_RAID_RAID1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7302 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7303 | return BTRFS_RAID_DUP; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7304 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7305 | return BTRFS_RAID_RAID0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7306 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7307 | return BTRFS_RAID_RAID5; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7308 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7309 | return BTRFS_RAID_RAID6; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7310 | |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7311 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7312 | } |
| 7313 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7314 | int get_block_group_index(struct btrfs_block_group_cache *cache) |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7315 | { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 7316 | return __get_raid_index(cache->flags); |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7317 | } |
| 7318 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7319 | static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = { |
| 7320 | [BTRFS_RAID_RAID10] = "raid10", |
| 7321 | [BTRFS_RAID_RAID1] = "raid1", |
| 7322 | [BTRFS_RAID_DUP] = "dup", |
| 7323 | [BTRFS_RAID_RAID0] = "raid0", |
| 7324 | [BTRFS_RAID_SINGLE] = "single", |
| 7325 | [BTRFS_RAID_RAID5] = "raid5", |
| 7326 | [BTRFS_RAID_RAID6] = "raid6", |
| 7327 | }; |
| 7328 | |
Jeff Mahoney | 1b8e5df | 2013-11-20 16:50:23 -0500 | [diff] [blame] | 7329 | static const char *get_raid_name(enum btrfs_raid_types type) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7330 | { |
| 7331 | if (type >= BTRFS_NR_RAID_TYPES) |
| 7332 | return NULL; |
| 7333 | |
| 7334 | return btrfs_raid_type_names[type]; |
| 7335 | } |
| 7336 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7337 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7338 | LOOP_CACHING_NOWAIT = 0, |
| 7339 | LOOP_CACHING_WAIT = 1, |
| 7340 | LOOP_ALLOC_CHUNK = 2, |
| 7341 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7342 | }; |
| 7343 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7344 | static inline void |
| 7345 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 7346 | int delalloc) |
| 7347 | { |
| 7348 | if (delalloc) |
| 7349 | down_read(&cache->data_rwsem); |
| 7350 | } |
| 7351 | |
| 7352 | static inline void |
| 7353 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 7354 | int delalloc) |
| 7355 | { |
| 7356 | btrfs_get_block_group(cache); |
| 7357 | if (delalloc) |
| 7358 | down_read(&cache->data_rwsem); |
| 7359 | } |
| 7360 | |
| 7361 | static struct btrfs_block_group_cache * |
| 7362 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 7363 | struct btrfs_free_cluster *cluster, |
| 7364 | int delalloc) |
| 7365 | { |
Sudip Mukherjee | 89771cc | 2016-02-16 13:32:47 +0530 | [diff] [blame] | 7366 | struct btrfs_block_group_cache *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7367 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7368 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7369 | while (1) { |
| 7370 | used_bg = cluster->block_group; |
| 7371 | if (!used_bg) |
| 7372 | return NULL; |
| 7373 | |
| 7374 | if (used_bg == block_group) |
| 7375 | return used_bg; |
| 7376 | |
| 7377 | btrfs_get_block_group(used_bg); |
| 7378 | |
| 7379 | if (!delalloc) |
| 7380 | return used_bg; |
| 7381 | |
| 7382 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 7383 | return used_bg; |
| 7384 | |
| 7385 | spin_unlock(&cluster->refill_lock); |
| 7386 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 7387 | /* We should only have one-level nested. */ |
| 7388 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7389 | |
| 7390 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7391 | if (used_bg == cluster->block_group) |
| 7392 | return used_bg; |
| 7393 | |
| 7394 | up_read(&used_bg->data_rwsem); |
| 7395 | btrfs_put_block_group(used_bg); |
| 7396 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7397 | } |
| 7398 | |
| 7399 | static inline void |
| 7400 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 7401 | int delalloc) |
| 7402 | { |
| 7403 | if (delalloc) |
| 7404 | up_read(&cache->data_rwsem); |
| 7405 | btrfs_put_block_group(cache); |
| 7406 | } |
| 7407 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7408 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7409 | * walks the btree of allocated extents and find a hole of a given size. |
| 7410 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7411 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 7412 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7413 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7414 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7415 | * |
| 7416 | * If there is no suitable free space, we will record the max size of |
| 7417 | * the free space extent currently. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7418 | */ |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 7419 | static noinline int find_free_extent(struct btrfs_fs_info *fs_info, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7420 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
| 7421 | u64 hint_byte, struct btrfs_key *ins, |
| 7422 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7423 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7424 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7425 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7426 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7427 | struct btrfs_block_group_cache *block_group = NULL; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7428 | u64 search_start = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7429 | u64 max_extent_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7430 | u64 empty_cluster = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7431 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7432 | int loop = 0; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7433 | int index = __get_raid_index(flags); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7434 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7435 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7436 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7437 | bool have_caching_bg = false; |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7438 | bool orig_have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7439 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7440 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7441 | WARN_ON(num_bytes < fs_info->sectorsize); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7442 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7443 | ins->objectid = 0; |
| 7444 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 7445 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7446 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7447 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7448 | space_info = __find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7449 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7450 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7451 | return -ENOSPC; |
| 7452 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7453 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7454 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7455 | * If our free space is heavily fragmented we may not be able to make |
| 7456 | * big contiguous allocations, so instead of doing the expensive search |
| 7457 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 7458 | * can go ahead and search for a more manageable chunk. |
| 7459 | * |
| 7460 | * If our max_extent_size is large enough for our allocation simply |
| 7461 | * disable clustering since we will likely not be able to find enough |
| 7462 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7463 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7464 | if (unlikely(space_info->max_extent_size)) { |
| 7465 | spin_lock(&space_info->lock); |
| 7466 | if (space_info->max_extent_size && |
| 7467 | num_bytes > space_info->max_extent_size) { |
| 7468 | ins->offset = space_info->max_extent_size; |
| 7469 | spin_unlock(&space_info->lock); |
| 7470 | return -ENOSPC; |
| 7471 | } else if (space_info->max_extent_size) { |
| 7472 | use_cluster = false; |
| 7473 | } |
| 7474 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7475 | } |
| 7476 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7477 | last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7478 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7479 | spin_lock(&last_ptr->lock); |
| 7480 | if (last_ptr->block_group) |
| 7481 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7482 | if (last_ptr->fragmented) { |
| 7483 | /* |
| 7484 | * We still set window_start so we can keep track of the |
| 7485 | * last place we found an allocation to try and save |
| 7486 | * some time. |
| 7487 | */ |
| 7488 | hint_byte = last_ptr->window_start; |
| 7489 | use_cluster = false; |
| 7490 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7491 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7492 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7493 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7494 | search_start = max(search_start, first_logical_byte(fs_info, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7495 | search_start = max(search_start, hint_byte); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7496 | if (search_start == hint_byte) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7497 | block_group = btrfs_lookup_block_group(fs_info, search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7498 | /* |
| 7499 | * we don't want to use the block group if it doesn't match our |
| 7500 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7501 | * |
| 7502 | * However if we are re-searching with an ideal block group |
| 7503 | * 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] | 7504 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7505 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7506 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7507 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7508 | if (list_empty(&block_group->list) || |
| 7509 | block_group->ro) { |
| 7510 | /* |
| 7511 | * someone is removing this block group, |
| 7512 | * we can't jump into the have_block_group |
| 7513 | * target because our list pointers are not |
| 7514 | * valid |
| 7515 | */ |
| 7516 | btrfs_put_block_group(block_group); |
| 7517 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7518 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7519 | index = get_block_group_index(block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7520 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7521 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7522 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7523 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7524 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7525 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7526 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7527 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7528 | have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7529 | if (index == 0 || index == __get_raid_index(flags)) |
| 7530 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7531 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7532 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 7533 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7534 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7535 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 7536 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7537 | btrfs_grab_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7538 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7539 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7540 | /* |
| 7541 | * this can happen if we end up cycling through all the |
| 7542 | * raid types, but we want to make sure we only allocate |
| 7543 | * for the proper type. |
| 7544 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7545 | if (!block_group_bits(block_group, flags)) { |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7546 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 7547 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7548 | BTRFS_BLOCK_GROUP_RAID5 | |
| 7549 | BTRFS_BLOCK_GROUP_RAID6 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7550 | BTRFS_BLOCK_GROUP_RAID10; |
| 7551 | |
| 7552 | /* |
| 7553 | * if they asked for extra copies and this block group |
| 7554 | * doesn't provide them, bail. This does allow us to |
| 7555 | * fill raid0 from raid1. |
| 7556 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7557 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7558 | goto loop; |
| 7559 | } |
| 7560 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7561 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 7562 | cached = block_group_cache_done(block_group); |
| 7563 | if (unlikely(!cached)) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7564 | have_caching_bg = true; |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 7565 | ret = cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 7566 | BUG_ON(ret < 0); |
| 7567 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7568 | } |
| 7569 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7570 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 7571 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7572 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7573 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7574 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7575 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7576 | * Ok we want to try and use the cluster allocator, so |
| 7577 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7578 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7579 | if (last_ptr && use_cluster) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7580 | struct btrfs_block_group_cache *used_block_group; |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7581 | unsigned long aligned_cluster; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7582 | /* |
| 7583 | * the refill lock keeps out other |
| 7584 | * people trying to start a new cluster |
| 7585 | */ |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7586 | used_block_group = btrfs_lock_cluster(block_group, |
| 7587 | last_ptr, |
| 7588 | delalloc); |
| 7589 | if (!used_block_group) |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7590 | goto refill_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7591 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7592 | if (used_block_group != block_group && |
| 7593 | (used_block_group->ro || |
| 7594 | !block_group_bits(used_block_group, flags))) |
| 7595 | goto release_cluster; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7596 | |
| 7597 | offset = btrfs_alloc_from_cluster(used_block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7598 | last_ptr, |
| 7599 | num_bytes, |
| 7600 | used_block_group->key.objectid, |
| 7601 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7602 | if (offset) { |
| 7603 | /* we have a block, we're done */ |
| 7604 | spin_unlock(&last_ptr->refill_lock); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7605 | trace_btrfs_reserve_extent_cluster(fs_info, |
Miao Xie | 89d4346 | 2014-01-15 20:00:57 +0800 | [diff] [blame] | 7606 | used_block_group, |
| 7607 | search_start, num_bytes); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7608 | if (used_block_group != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7609 | btrfs_release_block_group(block_group, |
| 7610 | delalloc); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7611 | block_group = used_block_group; |
| 7612 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7613 | goto checks; |
| 7614 | } |
| 7615 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7616 | WARN_ON(last_ptr->block_group != used_block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7617 | release_cluster: |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7618 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 7619 | * set up a new clusters, so lets just skip it |
| 7620 | * and let the allocator find whatever block |
| 7621 | * it can find. If we reach this point, we |
| 7622 | * will have tried the cluster allocator |
| 7623 | * plenty of times and not have found |
| 7624 | * anything, so we are likely way too |
| 7625 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7626 | * anything. |
| 7627 | * |
| 7628 | * However, if the cluster is taken from the |
| 7629 | * current block group, release the cluster |
| 7630 | * first, so that we stand a better chance of |
| 7631 | * succeeding in the unclustered |
| 7632 | * allocation. */ |
| 7633 | if (loop >= LOOP_NO_EMPTY_SIZE && |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7634 | used_block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7635 | spin_unlock(&last_ptr->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7636 | btrfs_release_block_group(used_block_group, |
| 7637 | delalloc); |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7638 | goto unclustered_alloc; |
| 7639 | } |
| 7640 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7641 | /* |
| 7642 | * this cluster didn't work out, free it and |
| 7643 | * start over |
| 7644 | */ |
| 7645 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 7646 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7647 | if (used_block_group != block_group) |
| 7648 | btrfs_release_block_group(used_block_group, |
| 7649 | delalloc); |
| 7650 | refill_cluster: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7651 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 7652 | spin_unlock(&last_ptr->refill_lock); |
| 7653 | goto unclustered_alloc; |
| 7654 | } |
| 7655 | |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7656 | aligned_cluster = max_t(unsigned long, |
| 7657 | empty_cluster + empty_size, |
| 7658 | block_group->full_stripe_len); |
| 7659 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7660 | /* allocate a cluster in this block group */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7661 | ret = btrfs_find_space_cluster(fs_info, block_group, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7662 | last_ptr, search_start, |
| 7663 | num_bytes, |
| 7664 | aligned_cluster); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7665 | if (ret == 0) { |
| 7666 | /* |
| 7667 | * now pull our allocation out of this |
| 7668 | * cluster |
| 7669 | */ |
| 7670 | offset = btrfs_alloc_from_cluster(block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7671 | last_ptr, |
| 7672 | num_bytes, |
| 7673 | search_start, |
| 7674 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7675 | if (offset) { |
| 7676 | /* we found one, proceed */ |
| 7677 | spin_unlock(&last_ptr->refill_lock); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7678 | trace_btrfs_reserve_extent_cluster(fs_info, |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7679 | block_group, search_start, |
| 7680 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7681 | goto checks; |
| 7682 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7683 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 7684 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7685 | spin_unlock(&last_ptr->refill_lock); |
| 7686 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7687 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7688 | wait_block_group_cache_progress(block_group, |
| 7689 | num_bytes + empty_cluster + empty_size); |
| 7690 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7691 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7692 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7693 | /* |
| 7694 | * at this point we either didn't find a cluster |
| 7695 | * or we weren't able to allocate a block from our |
| 7696 | * cluster. Free the cluster we've been trying |
| 7697 | * to use, and go to the next block group |
| 7698 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7699 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7700 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7701 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7702 | } |
| 7703 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7704 | unclustered_alloc: |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7705 | /* |
| 7706 | * We are doing an unclustered alloc, set the fragmented flag so |
| 7707 | * we don't bother trying to setup a cluster again until we get |
| 7708 | * more space. |
| 7709 | */ |
| 7710 | if (unlikely(last_ptr)) { |
| 7711 | spin_lock(&last_ptr->lock); |
| 7712 | last_ptr->fragmented = 1; |
| 7713 | spin_unlock(&last_ptr->lock); |
| 7714 | } |
Liu Bo | 0c9b36e | 2017-02-13 15:42:30 -0800 | [diff] [blame] | 7715 | if (cached) { |
| 7716 | struct btrfs_free_space_ctl *ctl = |
| 7717 | block_group->free_space_ctl; |
| 7718 | |
| 7719 | spin_lock(&ctl->tree_lock); |
| 7720 | if (ctl->free_space < |
| 7721 | num_bytes + empty_cluster + empty_size) { |
| 7722 | if (ctl->free_space > max_extent_size) |
| 7723 | max_extent_size = ctl->free_space; |
| 7724 | spin_unlock(&ctl->tree_lock); |
| 7725 | goto loop; |
| 7726 | } |
| 7727 | spin_unlock(&ctl->tree_lock); |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7728 | } |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7729 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7730 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7731 | num_bytes, empty_size, |
| 7732 | &max_extent_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7733 | /* |
| 7734 | * If we didn't find a chunk, and we haven't failed on this |
| 7735 | * block group before, and this block group is in the middle of |
| 7736 | * caching and we are ok with waiting, then go ahead and wait |
| 7737 | * for progress to be made, and set failed_alloc to true. |
| 7738 | * |
| 7739 | * If failed_alloc is true then we've already waited on this |
| 7740 | * block group once and should move on to the next block group. |
| 7741 | */ |
| 7742 | if (!offset && !failed_alloc && !cached && |
| 7743 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7744 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7745 | num_bytes + empty_size); |
| 7746 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7747 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7748 | } else if (!offset) { |
| 7749 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7750 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7751 | checks: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7752 | search_start = ALIGN(offset, fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 7753 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7754 | /* move on to the next group */ |
| 7755 | if (search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7756 | block_group->key.objectid + block_group->key.offset) { |
| 7757 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7758 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7759 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7760 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7761 | if (offset < search_start) |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7762 | btrfs_add_free_space(block_group, offset, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7763 | search_start - offset); |
| 7764 | BUG_ON(offset > search_start); |
| 7765 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7766 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 7767 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7768 | if (ret == -EAGAIN) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7769 | btrfs_add_free_space(block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7770 | goto loop; |
| 7771 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7772 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7773 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7774 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7775 | ins->objectid = search_start; |
| 7776 | ins->offset = num_bytes; |
| 7777 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7778 | trace_btrfs_reserve_extent(fs_info, block_group, |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7779 | search_start, num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7780 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7781 | break; |
| 7782 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7783 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7784 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7785 | BUG_ON(index != get_block_group_index(block_group)); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7786 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7787 | } |
| 7788 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 7789 | |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7790 | if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg |
| 7791 | && !orig_have_caching_bg) |
| 7792 | orig_have_caching_bg = true; |
| 7793 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7794 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 7795 | goto search; |
| 7796 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7797 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 7798 | goto search; |
| 7799 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7800 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7801 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 7802 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7803 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 7804 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 7805 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 7806 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7807 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7808 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7809 | index = 0; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7810 | if (loop == LOOP_CACHING_NOWAIT) { |
| 7811 | /* |
| 7812 | * We want to skip the LOOP_CACHING_WAIT step if we |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7813 | * don't have any uncached bgs and we've already done a |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7814 | * full search through. |
| 7815 | */ |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7816 | if (orig_have_caching_bg || !full_search) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7817 | loop = LOOP_CACHING_WAIT; |
| 7818 | else |
| 7819 | loop = LOOP_ALLOC_CHUNK; |
| 7820 | } else { |
| 7821 | loop++; |
| 7822 | } |
| 7823 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7824 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7825 | struct btrfs_trans_handle *trans; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7826 | int exist = 0; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7827 | |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7828 | trans = current->journal_info; |
| 7829 | if (trans) |
| 7830 | exist = 1; |
| 7831 | else |
| 7832 | trans = btrfs_join_transaction(root); |
| 7833 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7834 | if (IS_ERR(trans)) { |
| 7835 | ret = PTR_ERR(trans); |
| 7836 | goto out; |
| 7837 | } |
| 7838 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7839 | ret = do_chunk_alloc(trans, fs_info, flags, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7840 | CHUNK_ALLOC_FORCE); |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7841 | |
| 7842 | /* |
| 7843 | * If we can't allocate a new chunk we've already looped |
| 7844 | * through at least once, move on to the NO_EMPTY_SIZE |
| 7845 | * case. |
| 7846 | */ |
| 7847 | if (ret == -ENOSPC) |
| 7848 | loop = LOOP_NO_EMPTY_SIZE; |
| 7849 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7850 | /* |
| 7851 | * Do not bail out on ENOSPC since we |
| 7852 | * can do more things. |
| 7853 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7854 | if (ret < 0 && ret != -ENOSPC) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7855 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7856 | else |
| 7857 | ret = 0; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7858 | if (!exist) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7859 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7860 | if (ret) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7861 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7865 | /* |
| 7866 | * Don't loop again if we already have no empty_size and |
| 7867 | * no empty_cluster. |
| 7868 | */ |
| 7869 | if (empty_size == 0 && |
| 7870 | empty_cluster == 0) { |
| 7871 | ret = -ENOSPC; |
| 7872 | goto out; |
| 7873 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7874 | empty_size = 0; |
| 7875 | empty_cluster = 0; |
| 7876 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7877 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7878 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7879 | } else if (!ins->objectid) { |
| 7880 | ret = -ENOSPC; |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7881 | } else if (ins->objectid) { |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7882 | if (!use_cluster && last_ptr) { |
| 7883 | spin_lock(&last_ptr->lock); |
| 7884 | last_ptr->window_start = ins->objectid; |
| 7885 | spin_unlock(&last_ptr->lock); |
| 7886 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7887 | ret = 0; |
| 7888 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7889 | out: |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7890 | if (ret == -ENOSPC) { |
| 7891 | spin_lock(&space_info->lock); |
| 7892 | space_info->max_extent_size = max_extent_size; |
| 7893 | spin_unlock(&space_info->lock); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7894 | ins->offset = max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7895 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 7896 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7897 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7898 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7899 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
| 7900 | struct btrfs_space_info *info, u64 bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7901 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7902 | { |
| 7903 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7904 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7905 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7906 | spin_lock(&info->lock); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7907 | btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull", |
| 7908 | info->flags, |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 7909 | info->total_bytes - btrfs_space_info_used(info, true), |
| 7910 | info->full ? "" : "not "); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7911 | btrfs_info(fs_info, |
| 7912 | "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu", |
| 7913 | info->total_bytes, info->bytes_used, info->bytes_pinned, |
| 7914 | info->bytes_reserved, info->bytes_may_use, |
| 7915 | info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7916 | spin_unlock(&info->lock); |
| 7917 | |
| 7918 | if (!dump_block_groups) |
| 7919 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7920 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7921 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7922 | again: |
| 7923 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7924 | spin_lock(&cache->lock); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7925 | btrfs_info(fs_info, |
| 7926 | "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s", |
| 7927 | cache->key.objectid, cache->key.offset, |
| 7928 | btrfs_block_group_used(&cache->item), cache->pinned, |
| 7929 | cache->reserved, cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7930 | btrfs_dump_free_space(cache, bytes); |
| 7931 | spin_unlock(&cache->lock); |
| 7932 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7933 | if (++index < BTRFS_NR_RAID_TYPES) |
| 7934 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7935 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7936 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 7937 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7938 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7939 | u64 num_bytes, u64 min_alloc_size, |
| 7940 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7941 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7942 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7943 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 7944 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7945 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7946 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7947 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7948 | flags = btrfs_get_alloc_profile(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 7949 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7950 | WARN_ON(num_bytes < fs_info->sectorsize); |
Jeff Mahoney | 87bde3c | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 7951 | ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7952 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7953 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7954 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7955 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7956 | if (!final_tried && ins->offset) { |
| 7957 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7958 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7959 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7960 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7961 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7962 | if (num_bytes == min_alloc_size) |
| 7963 | final_tried = true; |
| 7964 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7965 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7966 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7967 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7968 | sinfo = __find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7969 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7970 | "allocation failed flags %llu, wanted %llu", |
| 7971 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 7972 | if (sinfo) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7973 | dump_space_info(fs_info, sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7974 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7975 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7976 | |
| 7977 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7978 | } |
| 7979 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7980 | static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7981 | u64 start, u64 len, |
| 7982 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7983 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7984 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7985 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7986 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7987 | cache = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7988 | if (!cache) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7989 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 7990 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7991 | return -ENOSPC; |
| 7992 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7993 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7994 | if (pin) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7995 | pin_down_extent(fs_info, cache, start, len, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7996 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7997 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7998 | ret = btrfs_discard_extent(fs_info, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7999 | btrfs_add_free_space(cache, start, len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 8000 | btrfs_free_reserved_bytes(cache, len, delalloc); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8001 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8002 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 8003 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8004 | btrfs_put_block_group(cache); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8005 | return ret; |
| 8006 | } |
| 8007 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8008 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8009 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8010 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8011 | return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8012 | } |
| 8013 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8014 | 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] | 8015 | u64 start, u64 len) |
| 8016 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8017 | return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8018 | } |
| 8019 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8020 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8021 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8022 | u64 parent, u64 root_objectid, |
| 8023 | u64 flags, u64 owner, u64 offset, |
| 8024 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8025 | { |
| 8026 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8027 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8028 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8029 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8030 | struct extent_buffer *leaf; |
| 8031 | int type; |
| 8032 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 8033 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8034 | if (parent > 0) |
| 8035 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 8036 | else |
| 8037 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 8038 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8039 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 8040 | |
| 8041 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 8042 | if (!path) |
| 8043 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8044 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 8045 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8046 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 8047 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8048 | if (ret) { |
| 8049 | btrfs_free_path(path); |
| 8050 | return ret; |
| 8051 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8052 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8053 | leaf = path->nodes[0]; |
| 8054 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8055 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8056 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 8057 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 8058 | btrfs_set_extent_flags(leaf, extent_item, |
| 8059 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8060 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8061 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 8062 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 8063 | if (parent > 0) { |
| 8064 | struct btrfs_shared_data_ref *ref; |
| 8065 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 8066 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 8067 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 8068 | } else { |
| 8069 | struct btrfs_extent_data_ref *ref; |
| 8070 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 8071 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 8072 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 8073 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 8074 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 8075 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8076 | |
| 8077 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 8078 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 8079 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8080 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
| 8081 | ins->offset); |
| 8082 | if (ret) |
| 8083 | return ret; |
| 8084 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 8085 | ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8086 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8087 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8088 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 8089 | BUG(); |
| 8090 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8091 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8092 | return ret; |
| 8093 | } |
| 8094 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8095 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8096 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8097 | u64 parent, u64 root_objectid, |
| 8098 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8099 | int level, struct btrfs_key *ins) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8100 | { |
| 8101 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8102 | struct btrfs_extent_item *extent_item; |
| 8103 | struct btrfs_tree_block_info *block_info; |
| 8104 | struct btrfs_extent_inline_ref *iref; |
| 8105 | struct btrfs_path *path; |
| 8106 | struct extent_buffer *leaf; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8107 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 8108 | u64 num_bytes = ins->offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8109 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8110 | |
| 8111 | if (!skinny_metadata) |
| 8112 | size += sizeof(*block_info); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8113 | |
| 8114 | path = btrfs_alloc_path(); |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8115 | if (!path) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8116 | btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8117 | fs_info->nodesize); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8118 | return -ENOMEM; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8119 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8120 | |
| 8121 | path->leave_spinning = 1; |
| 8122 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 8123 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8124 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 8125 | btrfs_free_path(path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8126 | btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8127 | fs_info->nodesize); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8128 | return ret; |
| 8129 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8130 | |
| 8131 | leaf = path->nodes[0]; |
| 8132 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 8133 | struct btrfs_extent_item); |
| 8134 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 8135 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 8136 | btrfs_set_extent_flags(leaf, extent_item, |
| 8137 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8138 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8139 | if (skinny_metadata) { |
| 8140 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8141 | num_bytes = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8142 | } else { |
| 8143 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 8144 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 8145 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 8146 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 8147 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8148 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8149 | if (parent > 0) { |
| 8150 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 8151 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 8152 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 8153 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 8154 | } else { |
| 8155 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 8156 | BTRFS_TREE_BLOCK_REF_KEY); |
| 8157 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 8158 | } |
| 8159 | |
| 8160 | btrfs_mark_buffer_dirty(leaf); |
| 8161 | btrfs_free_path(path); |
| 8162 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8163 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
| 8164 | num_bytes); |
| 8165 | if (ret) |
| 8166 | return ret; |
| 8167 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 8168 | ret = update_block_group(trans, fs_info, ins->objectid, |
| 8169 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8170 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8171 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8172 | ins->objectid, ins->offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8173 | BUG(); |
| 8174 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 8175 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8176 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8177 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8178 | return ret; |
| 8179 | } |
| 8180 | |
| 8181 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8182 | u64 root_objectid, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 8183 | u64 offset, u64 ram_bytes, |
| 8184 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8185 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8186 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8187 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 8188 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8189 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 8190 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8191 | ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8192 | ins->offset, 0, |
| 8193 | root_objectid, owner, offset, |
Jeff Mahoney | fef394f | 2016-12-13 14:39:34 -0500 | [diff] [blame] | 8194 | ram_bytes, BTRFS_ADD_DELAYED_EXTENT); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8195 | return ret; |
| 8196 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8197 | |
| 8198 | /* |
| 8199 | * this is used by the tree logging recovery code. It records that |
| 8200 | * an extent has been allocated and makes sure to clear the free |
| 8201 | * space cache bits as well |
| 8202 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8203 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8204 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8205 | u64 root_objectid, u64 owner, u64 offset, |
| 8206 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8207 | { |
| 8208 | int ret; |
| 8209 | struct btrfs_block_group_cache *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 8210 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8211 | |
| 8212 | /* |
| 8213 | * 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] | 8214 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8215 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8216 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8217 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 8218 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8219 | if (ret) |
| 8220 | return ret; |
| 8221 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8222 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8223 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8224 | if (!block_group) |
| 8225 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8226 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 8227 | space_info = block_group->space_info; |
| 8228 | spin_lock(&space_info->lock); |
| 8229 | spin_lock(&block_group->lock); |
| 8230 | space_info->bytes_reserved += ins->offset; |
| 8231 | block_group->reserved += ins->offset; |
| 8232 | spin_unlock(&block_group->lock); |
| 8233 | spin_unlock(&space_info->lock); |
| 8234 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8235 | ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8236 | 0, owner, offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 8237 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8238 | return ret; |
| 8239 | } |
| 8240 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8241 | static struct extent_buffer * |
| 8242 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8243 | u64 bytenr, int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8244 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8245 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8246 | struct extent_buffer *buf; |
| 8247 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8248 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 8249 | if (IS_ERR(buf)) |
| 8250 | return buf; |
| 8251 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8252 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 8253 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8254 | btrfs_tree_lock(buf); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 8255 | clean_tree_block(fs_info, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 8256 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8257 | |
| 8258 | btrfs_set_lock_blocking(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 8259 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8260 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8261 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8262 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 8263 | /* |
| 8264 | * we allow two log transactions at a time, use different |
| 8265 | * EXENT bit to differentiate dirty pages. |
| 8266 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8267 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 8268 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 8269 | buf->start + buf->len - 1, GFP_NOFS); |
| 8270 | else |
| 8271 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 8272 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8273 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8274 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8275 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 8276 | buf->start + buf->len - 1, GFP_NOFS); |
| 8277 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 8278 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8279 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8280 | return buf; |
| 8281 | } |
| 8282 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8283 | static struct btrfs_block_rsv * |
| 8284 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 8285 | struct btrfs_root *root, u32 blocksize) |
| 8286 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8287 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8288 | struct btrfs_block_rsv *block_rsv; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8289 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8290 | int ret; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8291 | bool global_updated = false; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8292 | |
| 8293 | block_rsv = get_block_rsv(trans, root); |
| 8294 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8295 | if (unlikely(block_rsv->size == 0)) |
| 8296 | goto try_reserve; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8297 | again: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8298 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 8299 | if (!ret) |
| 8300 | return block_rsv; |
| 8301 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8302 | if (block_rsv->failfast) |
| 8303 | return ERR_PTR(ret); |
| 8304 | |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8305 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
| 8306 | global_updated = true; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8307 | update_global_block_rsv(fs_info); |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8308 | goto again; |
| 8309 | } |
| 8310 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8311 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8312 | static DEFINE_RATELIMIT_STATE(_rs, |
| 8313 | DEFAULT_RATELIMIT_INTERVAL * 10, |
| 8314 | /*DEFAULT_RATELIMIT_BURST*/ 1); |
| 8315 | if (__ratelimit(&_rs)) |
| 8316 | WARN(1, KERN_DEBUG |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 8317 | "BTRFS: block rsv returned %d\n", ret); |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8318 | } |
| 8319 | try_reserve: |
| 8320 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, |
| 8321 | BTRFS_RESERVE_NO_FLUSH); |
| 8322 | if (!ret) |
| 8323 | return block_rsv; |
| 8324 | /* |
| 8325 | * If we couldn't reserve metadata bytes try and use some from |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 8326 | * the global reserve if its space type is the same as the global |
| 8327 | * reservation. |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8328 | */ |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 8329 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
| 8330 | block_rsv->space_info == global_rsv->space_info) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8331 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 8332 | if (!ret) |
| 8333 | return global_rsv; |
| 8334 | } |
| 8335 | return ERR_PTR(ret); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8336 | } |
| 8337 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 8338 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 8339 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8340 | { |
| 8341 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 8342 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8343 | } |
| 8344 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8345 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8346 | * 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] | 8347 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8348 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 8349 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 8350 | struct btrfs_root *root, |
| 8351 | u64 parent, u64 root_objectid, |
| 8352 | const struct btrfs_disk_key *key, |
| 8353 | int level, u64 hint, |
| 8354 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8355 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8356 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 8357 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8358 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8359 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8360 | struct btrfs_delayed_extent_op *extent_op; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8361 | u64 flags = 0; |
| 8362 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8363 | u32 blocksize = fs_info->nodesize; |
| 8364 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8365 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 8366 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8367 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 8368 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8369 | level); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 8370 | if (!IS_ERR(buf)) |
| 8371 | root->alloc_bytenr += blocksize; |
| 8372 | return buf; |
| 8373 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 8374 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 8375 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8376 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 8377 | if (IS_ERR(block_rsv)) |
| 8378 | return ERR_CAST(block_rsv); |
| 8379 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 8380 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8381 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8382 | if (ret) |
| 8383 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 8384 | |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8385 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8386 | if (IS_ERR(buf)) { |
| 8387 | ret = PTR_ERR(buf); |
| 8388 | goto out_free_reserved; |
| 8389 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8390 | |
| 8391 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 8392 | if (parent == 0) |
| 8393 | parent = ins.objectid; |
| 8394 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8395 | } else |
| 8396 | BUG_ON(parent > 0); |
| 8397 | |
| 8398 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 8399 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8400 | if (!extent_op) { |
| 8401 | ret = -ENOMEM; |
| 8402 | goto out_free_buf; |
| 8403 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8404 | if (key) |
| 8405 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 8406 | else |
| 8407 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 8408 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 8409 | extent_op->update_key = skinny_metadata ? false : true; |
| 8410 | extent_op->update_flags = true; |
| 8411 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8412 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8413 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8414 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8415 | ins.objectid, ins.offset, |
| 8416 | parent, root_objectid, level, |
| 8417 | BTRFS_ADD_DELAYED_EXTENT, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8418 | extent_op); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8419 | if (ret) |
| 8420 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8421 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8422 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8423 | |
| 8424 | out_free_delayed: |
| 8425 | btrfs_free_delayed_extent_op(extent_op); |
| 8426 | out_free_buf: |
| 8427 | free_extent_buffer(buf); |
| 8428 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8429 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8430 | out_unuse: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8431 | unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8432 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8433 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 8434 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8435 | struct walk_control { |
| 8436 | u64 refs[BTRFS_MAX_LEVEL]; |
| 8437 | u64 flags[BTRFS_MAX_LEVEL]; |
| 8438 | struct btrfs_key update_progress; |
| 8439 | int stage; |
| 8440 | int level; |
| 8441 | int shared_level; |
| 8442 | int update_ref; |
| 8443 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8444 | int reada_slot; |
| 8445 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8446 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8447 | }; |
| 8448 | |
| 8449 | #define DROP_REFERENCE 1 |
| 8450 | #define UPDATE_BACKREF 2 |
| 8451 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8452 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 8453 | struct btrfs_root *root, |
| 8454 | struct walk_control *wc, |
| 8455 | struct btrfs_path *path) |
| 8456 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8457 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8458 | u64 bytenr; |
| 8459 | u64 generation; |
| 8460 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8461 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8462 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8463 | struct btrfs_key key; |
| 8464 | struct extent_buffer *eb; |
| 8465 | int ret; |
| 8466 | int slot; |
| 8467 | int nread = 0; |
| 8468 | |
| 8469 | if (path->slots[wc->level] < wc->reada_slot) { |
| 8470 | wc->reada_count = wc->reada_count * 2 / 3; |
| 8471 | wc->reada_count = max(wc->reada_count, 2); |
| 8472 | } else { |
| 8473 | wc->reada_count = wc->reada_count * 3 / 2; |
| 8474 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8475 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | eb = path->nodes[wc->level]; |
| 8479 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8480 | |
| 8481 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 8482 | if (nread >= wc->reada_count) |
| 8483 | break; |
| 8484 | |
| 8485 | cond_resched(); |
| 8486 | bytenr = btrfs_node_blockptr(eb, slot); |
| 8487 | generation = btrfs_node_ptr_generation(eb, slot); |
| 8488 | |
| 8489 | if (slot == path->slots[wc->level]) |
| 8490 | goto reada; |
| 8491 | |
| 8492 | if (wc->stage == UPDATE_BACKREF && |
| 8493 | generation <= root->root_key.offset) |
| 8494 | continue; |
| 8495 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8496 | /* 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] | 8497 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8498 | wc->level - 1, 1, &refs, |
| 8499 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8500 | /* We don't care about errors in readahead. */ |
| 8501 | if (ret < 0) |
| 8502 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8503 | BUG_ON(refs == 0); |
| 8504 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8505 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8506 | if (refs == 1) |
| 8507 | goto reada; |
| 8508 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8509 | if (wc->level == 1 && |
| 8510 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8511 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8512 | if (!wc->update_ref || |
| 8513 | generation <= root->root_key.offset) |
| 8514 | continue; |
| 8515 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 8516 | ret = btrfs_comp_cpu_keys(&key, |
| 8517 | &wc->update_progress); |
| 8518 | if (ret < 0) |
| 8519 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8520 | } else { |
| 8521 | if (wc->level == 1 && |
| 8522 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8523 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8524 | } |
| 8525 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8526 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8527 | nread++; |
| 8528 | } |
| 8529 | wc->reada_slot = slot; |
| 8530 | } |
| 8531 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8532 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8533 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8534 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8535 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 8536 | * back refs for pointers in the block. |
| 8537 | * |
| 8538 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8539 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8540 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 8541 | struct btrfs_root *root, |
| 8542 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8543 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8544 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8545 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8546 | int level = wc->level; |
| 8547 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8548 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8549 | int ret; |
| 8550 | |
| 8551 | if (wc->stage == UPDATE_BACKREF && |
| 8552 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 8553 | return 1; |
| 8554 | |
| 8555 | /* |
| 8556 | * when reference count of tree block is 1, it won't increase |
| 8557 | * again. once full backref flag is set, we never clear it. |
| 8558 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8559 | if (lookup_info && |
| 8560 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 8561 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8562 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8563 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8564 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8565 | &wc->refs[level], |
| 8566 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8567 | BUG_ON(ret == -ENOMEM); |
| 8568 | if (ret) |
| 8569 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8570 | BUG_ON(wc->refs[level] == 0); |
| 8571 | } |
| 8572 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8573 | if (wc->stage == DROP_REFERENCE) { |
| 8574 | if (wc->refs[level] > 1) |
| 8575 | return 1; |
| 8576 | |
| 8577 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8578 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8579 | path->locks[level] = 0; |
| 8580 | } |
| 8581 | return 0; |
| 8582 | } |
| 8583 | |
| 8584 | /* wc->stage == UPDATE_BACKREF */ |
| 8585 | if (!(wc->flags[level] & flag)) { |
| 8586 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8587 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8588 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8589 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8590 | BUG_ON(ret); /* -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8591 | ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8592 | eb->len, flag, |
| 8593 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8594 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8595 | wc->flags[level] |= flag; |
| 8596 | } |
| 8597 | |
| 8598 | /* |
| 8599 | * the block is shared by multiple trees, so it's not good to |
| 8600 | * keep the tree lock |
| 8601 | */ |
| 8602 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8603 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8604 | path->locks[level] = 0; |
| 8605 | } |
| 8606 | return 0; |
| 8607 | } |
| 8608 | |
| 8609 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8610 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8611 | * |
| 8612 | * when wc->stage == DROP_REFERENCE, this function checks |
| 8613 | * reference count of the block pointed to. if the block |
| 8614 | * is shared and we need update back refs for the subtree |
| 8615 | * rooted at the block, this function changes wc->stage to |
| 8616 | * UPDATE_BACKREF. if the block is shared and there is no |
| 8617 | * need to update back, this function drops the reference |
| 8618 | * to the block. |
| 8619 | * |
| 8620 | * NOTE: return value 1 means we should stop walking down. |
| 8621 | */ |
| 8622 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 8623 | struct btrfs_root *root, |
| 8624 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8625 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8626 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8627 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8628 | u64 bytenr; |
| 8629 | u64 generation; |
| 8630 | u64 parent; |
| 8631 | u32 blocksize; |
| 8632 | struct btrfs_key key; |
| 8633 | struct extent_buffer *next; |
| 8634 | int level = wc->level; |
| 8635 | int reada = 0; |
| 8636 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8637 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8638 | |
| 8639 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 8640 | path->slots[level]); |
| 8641 | /* |
| 8642 | * if the lower level block was created before the snapshot |
| 8643 | * was created, we know there is no need to update back refs |
| 8644 | * for the subtree |
| 8645 | */ |
| 8646 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8647 | generation <= root->root_key.offset) { |
| 8648 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8649 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8650 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8651 | |
| 8652 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8653 | blocksize = fs_info->nodesize; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8654 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8655 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8656 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8657 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 8658 | if (IS_ERR(next)) |
| 8659 | return PTR_ERR(next); |
| 8660 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 8661 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 8662 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8663 | reada = 1; |
| 8664 | } |
| 8665 | btrfs_tree_lock(next); |
| 8666 | btrfs_set_lock_blocking(next); |
| 8667 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8668 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8669 | &wc->refs[level - 1], |
| 8670 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8671 | if (ret < 0) |
| 8672 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8673 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8674 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8675 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8676 | ret = -EIO; |
| 8677 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8678 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8679 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8680 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8681 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8682 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8683 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8684 | if (level == 1 && |
| 8685 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8686 | goto skip; |
| 8687 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8688 | if (!wc->update_ref || |
| 8689 | generation <= root->root_key.offset) |
| 8690 | goto skip; |
| 8691 | |
| 8692 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 8693 | path->slots[level]); |
| 8694 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 8695 | if (ret < 0) |
| 8696 | goto skip; |
| 8697 | |
| 8698 | wc->stage = UPDATE_BACKREF; |
| 8699 | wc->shared_level = level - 1; |
| 8700 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8701 | } else { |
| 8702 | if (level == 1 && |
| 8703 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8704 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8705 | } |
| 8706 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 8707 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8708 | btrfs_tree_unlock(next); |
| 8709 | free_extent_buffer(next); |
| 8710 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8711 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8712 | } |
| 8713 | |
| 8714 | if (!next) { |
| 8715 | if (reada && level == 1) |
| 8716 | reada_walk_down(trans, root, wc, path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8717 | next = read_tree_block(fs_info, bytenr, generation); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 8718 | if (IS_ERR(next)) { |
| 8719 | return PTR_ERR(next); |
| 8720 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8721 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 8722 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8723 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8724 | btrfs_tree_lock(next); |
| 8725 | btrfs_set_lock_blocking(next); |
| 8726 | } |
| 8727 | |
| 8728 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8729 | ASSERT(level == btrfs_header_level(next)); |
| 8730 | if (level != btrfs_header_level(next)) { |
| 8731 | btrfs_err(root->fs_info, "mismatched level"); |
| 8732 | ret = -EIO; |
| 8733 | goto out_unlock; |
| 8734 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8735 | path->nodes[level] = next; |
| 8736 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8737 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8738 | wc->level = level; |
| 8739 | if (wc->level == 1) |
| 8740 | wc->reada_slot = 0; |
| 8741 | return 0; |
| 8742 | skip: |
| 8743 | wc->refs[level - 1] = 0; |
| 8744 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8745 | if (wc->stage == DROP_REFERENCE) { |
| 8746 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 8747 | parent = path->nodes[level]->start; |
| 8748 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8749 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8750 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8751 | if (root->root_key.objectid != |
| 8752 | btrfs_header_owner(path->nodes[level])) { |
| 8753 | btrfs_err(root->fs_info, |
| 8754 | "mismatched block owner"); |
| 8755 | ret = -EIO; |
| 8756 | goto out_unlock; |
| 8757 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8758 | parent = 0; |
| 8759 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8760 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8761 | if (need_account) { |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 8762 | ret = btrfs_qgroup_trace_subtree(trans, root, next, |
| 8763 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8764 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8765 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8766 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 8767 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8768 | } |
| 8769 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8770 | ret = btrfs_free_extent(trans, fs_info, bytenr, blocksize, |
| 8771 | parent, root->root_key.objectid, |
| 8772 | level - 1, 0); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8773 | if (ret) |
| 8774 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8775 | } |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8776 | |
| 8777 | *lookup_info = 1; |
| 8778 | ret = 1; |
| 8779 | |
| 8780 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8781 | btrfs_tree_unlock(next); |
| 8782 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8783 | |
| 8784 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8785 | } |
| 8786 | |
| 8787 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8788 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8789 | * |
| 8790 | * when wc->stage == DROP_REFERENCE, this function drops |
| 8791 | * reference count on the block. |
| 8792 | * |
| 8793 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 8794 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 8795 | * to UPDATE_BACKREF previously while processing the block. |
| 8796 | * |
| 8797 | * NOTE: return value 1 means we should stop walking up. |
| 8798 | */ |
| 8799 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 8800 | struct btrfs_root *root, |
| 8801 | struct btrfs_path *path, |
| 8802 | struct walk_control *wc) |
| 8803 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8804 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8805 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8806 | int level = wc->level; |
| 8807 | struct extent_buffer *eb = path->nodes[level]; |
| 8808 | u64 parent = 0; |
| 8809 | |
| 8810 | if (wc->stage == UPDATE_BACKREF) { |
| 8811 | BUG_ON(wc->shared_level < level); |
| 8812 | if (level < wc->shared_level) |
| 8813 | goto out; |
| 8814 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8815 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 8816 | if (ret > 0) |
| 8817 | wc->update_ref = 0; |
| 8818 | |
| 8819 | wc->stage = DROP_REFERENCE; |
| 8820 | wc->shared_level = -1; |
| 8821 | path->slots[level] = 0; |
| 8822 | |
| 8823 | /* |
| 8824 | * check reference count again if the block isn't locked. |
| 8825 | * we should start walking down the tree again if reference |
| 8826 | * count is one. |
| 8827 | */ |
| 8828 | if (!path->locks[level]) { |
| 8829 | BUG_ON(level == 0); |
| 8830 | btrfs_tree_lock(eb); |
| 8831 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8832 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8833 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8834 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8835 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8836 | &wc->refs[level], |
| 8837 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8838 | if (ret < 0) { |
| 8839 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8840 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8841 | return ret; |
| 8842 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8843 | BUG_ON(wc->refs[level] == 0); |
| 8844 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8845 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8846 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8847 | return 1; |
| 8848 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8849 | } |
| 8850 | } |
| 8851 | |
| 8852 | /* wc->stage == DROP_REFERENCE */ |
| 8853 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 8854 | |
| 8855 | if (wc->refs[level] == 1) { |
| 8856 | if (level == 0) { |
| 8857 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8858 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8859 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8860 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8861 | BUG_ON(ret); /* -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8862 | ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8863 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8864 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8865 | "error %d accounting leaf items. Quota is out of sync, rescan required.", |
| 8866 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8867 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8868 | } |
| 8869 | /* make block locked assertion in clean_tree_block happy */ |
| 8870 | if (!path->locks[level] && |
| 8871 | btrfs_header_generation(eb) == trans->transid) { |
| 8872 | btrfs_tree_lock(eb); |
| 8873 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8874 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8875 | } |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 8876 | clean_tree_block(fs_info, eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8877 | } |
| 8878 | |
| 8879 | if (eb == root->node) { |
| 8880 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8881 | parent = eb->start; |
| 8882 | else |
| 8883 | BUG_ON(root->root_key.objectid != |
| 8884 | btrfs_header_owner(eb)); |
| 8885 | } else { |
| 8886 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8887 | parent = path->nodes[level + 1]->start; |
| 8888 | else |
| 8889 | BUG_ON(root->root_key.objectid != |
| 8890 | btrfs_header_owner(path->nodes[level + 1])); |
| 8891 | } |
| 8892 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 8893 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8894 | out: |
| 8895 | wc->refs[level] = 0; |
| 8896 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8897 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8898 | } |
| 8899 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8900 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 8901 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8902 | struct btrfs_path *path, |
| 8903 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8904 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8905 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8906 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8907 | int ret; |
| 8908 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8909 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8910 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8911 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8912 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8913 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8914 | if (level == 0) |
| 8915 | break; |
| 8916 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 8917 | if (path->slots[level] >= |
| 8918 | btrfs_header_nritems(path->nodes[level])) |
| 8919 | break; |
| 8920 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8921 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8922 | if (ret > 0) { |
| 8923 | path->slots[level]++; |
| 8924 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 8925 | } else if (ret < 0) |
| 8926 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8927 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8928 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8929 | return 0; |
| 8930 | } |
| 8931 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8932 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 8933 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8934 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8935 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8936 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8937 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8938 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8939 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8940 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 8941 | while (level < max_level && path->nodes[level]) { |
| 8942 | wc->level = level; |
| 8943 | if (path->slots[level] + 1 < |
| 8944 | btrfs_header_nritems(path->nodes[level])) { |
| 8945 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8946 | return 0; |
| 8947 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8948 | ret = walk_up_proc(trans, root, path, wc); |
| 8949 | if (ret > 0) |
| 8950 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 8951 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8952 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8953 | btrfs_tree_unlock_rw(path->nodes[level], |
| 8954 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8955 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8956 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8957 | free_extent_buffer(path->nodes[level]); |
| 8958 | path->nodes[level] = NULL; |
| 8959 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8960 | } |
| 8961 | } |
| 8962 | return 1; |
| 8963 | } |
| 8964 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8965 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8966 | * drop a subvolume tree. |
| 8967 | * |
| 8968 | * this function traverses the tree freeing any blocks that only |
| 8969 | * referenced by the tree. |
| 8970 | * |
| 8971 | * when a shared tree block is found. this function decreases its |
| 8972 | * reference count by one. if update_ref is true, this function |
| 8973 | * also make sure backrefs for the shared block and all lower level |
| 8974 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 8975 | * |
| 8976 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8977 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 8978 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8979 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 8980 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8981 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8982 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8983 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8984 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8985 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8986 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8987 | struct walk_control *wc; |
| 8988 | struct btrfs_key key; |
| 8989 | int err = 0; |
| 8990 | int ret; |
| 8991 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8992 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8993 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8994 | btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8995 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8996 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8997 | if (!path) { |
| 8998 | err = -ENOMEM; |
| 8999 | goto out; |
| 9000 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9001 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9002 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 9003 | if (!wc) { |
| 9004 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9005 | err = -ENOMEM; |
| 9006 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 9007 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9008 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9009 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9010 | if (IS_ERR(trans)) { |
| 9011 | err = PTR_ERR(trans); |
| 9012 | goto out_free; |
| 9013 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 9014 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9015 | if (block_rsv) |
| 9016 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9017 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9018 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9019 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9020 | path->nodes[level] = btrfs_lock_root_node(root); |
| 9021 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9022 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9023 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9024 | memset(&wc->update_progress, 0, |
| 9025 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9026 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9027 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9028 | memcpy(&wc->update_progress, &key, |
| 9029 | sizeof(wc->update_progress)); |
| 9030 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 9031 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9032 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 9033 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9034 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 9035 | path->lowest_level = 0; |
| 9036 | if (ret < 0) { |
| 9037 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9038 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9039 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 9040 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9041 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9042 | /* |
| 9043 | * unlock our path, this is safe because only this |
| 9044 | * function is allowed to delete this snapshot |
| 9045 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9046 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 9047 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9048 | level = btrfs_header_level(root->node); |
| 9049 | while (1) { |
| 9050 | btrfs_tree_lock(path->nodes[level]); |
| 9051 | btrfs_set_lock_blocking(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 9052 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9053 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9054 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9055 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 9056 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9057 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9058 | if (ret < 0) { |
| 9059 | err = ret; |
| 9060 | goto out_end_trans; |
| 9061 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9062 | BUG_ON(wc->refs[level] == 0); |
| 9063 | |
| 9064 | if (level == root_item->drop_level) |
| 9065 | break; |
| 9066 | |
| 9067 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 9068 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9069 | WARN_ON(wc->refs[level] != 1); |
| 9070 | level--; |
| 9071 | } |
| 9072 | } |
| 9073 | |
| 9074 | wc->level = level; |
| 9075 | wc->shared_level = -1; |
| 9076 | wc->stage = DROP_REFERENCE; |
| 9077 | wc->update_ref = update_ref; |
| 9078 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9079 | wc->for_reloc = for_reloc; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9080 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9081 | |
| 9082 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 9083 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9084 | ret = walk_down_tree(trans, root, path, wc); |
| 9085 | if (ret < 0) { |
| 9086 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 9087 | break; |
| 9088 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9089 | |
| 9090 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 9091 | if (ret < 0) { |
| 9092 | err = ret; |
| 9093 | break; |
| 9094 | } |
| 9095 | |
| 9096 | if (ret > 0) { |
| 9097 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 9098 | break; |
| 9099 | } |
| 9100 | |
| 9101 | if (wc->stage == DROP_REFERENCE) { |
| 9102 | level = wc->level; |
| 9103 | btrfs_node_key(path->nodes[level], |
| 9104 | &root_item->drop_progress, |
| 9105 | path->slots[level]); |
| 9106 | root_item->drop_level = level; |
| 9107 | } |
| 9108 | |
| 9109 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9110 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9111 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9112 | ret = btrfs_update_root(trans, tree_root, |
| 9113 | &root->root_key, |
| 9114 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9115 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9116 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9117 | err = ret; |
| 9118 | goto out_end_trans; |
| 9119 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9120 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9121 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9122 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9123 | btrfs_debug(fs_info, |
| 9124 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 9125 | err = -EAGAIN; |
| 9126 | goto out_free; |
| 9127 | } |
| 9128 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9129 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9130 | if (IS_ERR(trans)) { |
| 9131 | err = PTR_ERR(trans); |
| 9132 | goto out_free; |
| 9133 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9134 | if (block_rsv) |
| 9135 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 9136 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9137 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9138 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9139 | if (err) |
| 9140 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9141 | |
| 9142 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9143 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9144 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9145 | goto out_end_trans; |
| 9146 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9147 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9148 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 9149 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 9150 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9151 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9152 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9153 | err = ret; |
| 9154 | goto out_end_trans; |
| 9155 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 9156 | /* if we fail to delete the orphan item this time |
| 9157 | * around, it'll get picked up the next time. |
| 9158 | * |
| 9159 | * The most common failure here is just -ENOENT. |
| 9160 | */ |
| 9161 | btrfs_del_orphan_item(trans, tree_root, |
| 9162 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9163 | } |
| 9164 | } |
| 9165 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 9166 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 9167 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9168 | } else { |
| 9169 | free_extent_buffer(root->node); |
| 9170 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 9171 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9172 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9173 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9174 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9175 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9176 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9177 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 9178 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9179 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9180 | /* |
| 9181 | * So if we need to stop dropping the snapshot for whatever reason we |
| 9182 | * need to make sure to add it back to the dead root list so that we |
| 9183 | * keep trying to do the work later. This also cleans up roots if we |
| 9184 | * don't have it in the radix (like when we recover after a power fail |
| 9185 | * or unmount) so we don't leak memory. |
| 9186 | */ |
Josef Bacik | b37b39c | 2013-07-23 16:57:15 -0400 | [diff] [blame] | 9187 | if (!for_reloc && root_dropped == false) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9188 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 9189 | if (err && err != -EAGAIN) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9190 | btrfs_handle_fs_error(fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 9191 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9192 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9193 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9194 | /* |
| 9195 | * drop subtree rooted at tree block 'node'. |
| 9196 | * |
| 9197 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9198 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9199 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9200 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 9201 | struct btrfs_root *root, |
| 9202 | struct extent_buffer *node, |
| 9203 | struct extent_buffer *parent) |
| 9204 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9205 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9206 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9207 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9208 | int level; |
| 9209 | int parent_level; |
| 9210 | int ret = 0; |
| 9211 | int wret; |
| 9212 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9213 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 9214 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9215 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9216 | if (!path) |
| 9217 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9218 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9219 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9220 | if (!wc) { |
| 9221 | btrfs_free_path(path); |
| 9222 | return -ENOMEM; |
| 9223 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9224 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9225 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9226 | parent_level = btrfs_header_level(parent); |
| 9227 | extent_buffer_get(parent); |
| 9228 | path->nodes[parent_level] = parent; |
| 9229 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 9230 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9231 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9232 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9233 | path->nodes[level] = node; |
| 9234 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9235 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9236 | |
| 9237 | wc->refs[parent_level] = 1; |
| 9238 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 9239 | wc->level = level; |
| 9240 | wc->shared_level = -1; |
| 9241 | wc->stage = DROP_REFERENCE; |
| 9242 | wc->update_ref = 0; |
| 9243 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9244 | wc->for_reloc = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9245 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9246 | |
| 9247 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9248 | wret = walk_down_tree(trans, root, path, wc); |
| 9249 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9250 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9251 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9252 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9253 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9254 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9255 | if (wret < 0) |
| 9256 | ret = wret; |
| 9257 | if (wret != 0) |
| 9258 | break; |
| 9259 | } |
| 9260 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9261 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9262 | btrfs_free_path(path); |
| 9263 | return ret; |
| 9264 | } |
| 9265 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9266 | static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9267 | { |
| 9268 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9269 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9270 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9271 | /* |
| 9272 | * if restripe for this chunk_type is on pick target profile and |
| 9273 | * return, otherwise do the usual balance |
| 9274 | */ |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9275 | stripped = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9276 | if (stripped) |
| 9277 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 9278 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9279 | num_devices = fs_info->fs_devices->rw_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 9280 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9281 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 9282 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9283 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 9284 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9285 | if (num_devices == 1) { |
| 9286 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9287 | stripped = flags & ~stripped; |
| 9288 | |
| 9289 | /* turn raid0 into single device chunks */ |
| 9290 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 9291 | return stripped; |
| 9292 | |
| 9293 | /* turn mirroring into duplication */ |
| 9294 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9295 | BTRFS_BLOCK_GROUP_RAID10)) |
| 9296 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9297 | } else { |
| 9298 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9299 | if (flags & stripped) |
| 9300 | return flags; |
| 9301 | |
| 9302 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9303 | stripped = flags & ~stripped; |
| 9304 | |
| 9305 | /* switch duplicated blocks with raid1 */ |
| 9306 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 9307 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 9308 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9309 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9310 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9311 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9312 | return flags; |
| 9313 | } |
| 9314 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9315 | static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9316 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9317 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9318 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9319 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9320 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9321 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9322 | /* |
| 9323 | * We need some metadata space and system metadata space for |
| 9324 | * allocating chunks in some corner cases until we force to set |
| 9325 | * it to be readonly. |
| 9326 | */ |
| 9327 | if ((sinfo->flags & |
| 9328 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 9329 | !force) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9330 | min_allocable_bytes = SZ_1M; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9331 | else |
| 9332 | min_allocable_bytes = 0; |
| 9333 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9334 | spin_lock(&sinfo->lock); |
| 9335 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9336 | |
| 9337 | if (cache->ro) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9338 | cache->ro++; |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9339 | ret = 0; |
| 9340 | goto out; |
| 9341 | } |
| 9342 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9343 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 9344 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9345 | |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 9346 | if (btrfs_space_info_used(sinfo, true) + num_bytes + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 9347 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9348 | sinfo->bytes_readonly += num_bytes; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9349 | cache->ro++; |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9350 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9351 | ret = 0; |
| 9352 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9353 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9354 | spin_unlock(&cache->lock); |
| 9355 | spin_unlock(&sinfo->lock); |
| 9356 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9357 | } |
| 9358 | |
Jeff Mahoney | 5e00f19 | 2017-02-15 16:28:29 -0500 | [diff] [blame] | 9359 | int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9360 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9361 | |
| 9362 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9363 | struct btrfs_trans_handle *trans; |
| 9364 | u64 alloc_flags; |
| 9365 | int ret; |
| 9366 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9367 | again: |
Jeff Mahoney | 5e00f19 | 2017-02-15 16:28:29 -0500 | [diff] [blame] | 9368 | trans = btrfs_join_transaction(fs_info->extent_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9369 | if (IS_ERR(trans)) |
| 9370 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9371 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9372 | /* |
| 9373 | * we're not allowed to set block groups readonly after the dirty |
| 9374 | * block groups cache has started writing. If it already started, |
| 9375 | * back off and let this transaction commit |
| 9376 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9377 | mutex_lock(&fs_info->ro_block_group_mutex); |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 9378 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9379 | u64 transid = trans->transid; |
| 9380 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9381 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9382 | btrfs_end_transaction(trans); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9383 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9384 | ret = btrfs_wait_for_commit(fs_info, transid); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9385 | if (ret) |
| 9386 | return ret; |
| 9387 | goto again; |
| 9388 | } |
| 9389 | |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9390 | /* |
| 9391 | * if we are changing raid levels, try to allocate a corresponding |
| 9392 | * block group with the new raid level. |
| 9393 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9394 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9395 | if (alloc_flags != cache->flags) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9396 | ret = do_chunk_alloc(trans, fs_info, alloc_flags, |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9397 | CHUNK_ALLOC_FORCE); |
| 9398 | /* |
| 9399 | * ENOSPC is allowed here, we may have enough space |
| 9400 | * already allocated at the new raid level to |
| 9401 | * carry on |
| 9402 | */ |
| 9403 | if (ret == -ENOSPC) |
| 9404 | ret = 0; |
| 9405 | if (ret < 0) |
| 9406 | goto out; |
| 9407 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9408 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9409 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9410 | if (!ret) |
| 9411 | goto out; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9412 | alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags); |
| 9413 | ret = do_chunk_alloc(trans, fs_info, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 9414 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9415 | if (ret < 0) |
| 9416 | goto out; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9417 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9418 | out: |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9419 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9420 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 9421 | mutex_lock(&fs_info->chunk_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9422 | check_system_chunk(trans, fs_info, alloc_flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 9423 | mutex_unlock(&fs_info->chunk_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9424 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9425 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9426 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9427 | btrfs_end_transaction(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9428 | return ret; |
| 9429 | } |
| 9430 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9431 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9432 | struct btrfs_fs_info *fs_info, u64 type) |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9433 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9434 | u64 alloc_flags = get_alloc_profile(fs_info, type); |
| 9435 | |
| 9436 | return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9437 | } |
| 9438 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9439 | /* |
| 9440 | * 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] | 9441 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9442 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9443 | 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] | 9444 | { |
| 9445 | struct btrfs_block_group_cache *block_group; |
| 9446 | u64 free_bytes = 0; |
| 9447 | int factor; |
| 9448 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9449 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9450 | if (list_empty(&sinfo->ro_bgs)) |
| 9451 | return 0; |
| 9452 | |
| 9453 | spin_lock(&sinfo->lock); |
| 9454 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9455 | spin_lock(&block_group->lock); |
| 9456 | |
| 9457 | if (!block_group->ro) { |
| 9458 | spin_unlock(&block_group->lock); |
| 9459 | continue; |
| 9460 | } |
| 9461 | |
| 9462 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9463 | BTRFS_BLOCK_GROUP_RAID10 | |
| 9464 | BTRFS_BLOCK_GROUP_DUP)) |
| 9465 | factor = 2; |
| 9466 | else |
| 9467 | factor = 1; |
| 9468 | |
| 9469 | free_bytes += (block_group->key.offset - |
| 9470 | btrfs_block_group_used(&block_group->item)) * |
| 9471 | factor; |
| 9472 | |
| 9473 | spin_unlock(&block_group->lock); |
| 9474 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9475 | spin_unlock(&sinfo->lock); |
| 9476 | |
| 9477 | return free_bytes; |
| 9478 | } |
| 9479 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9480 | void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9481 | { |
| 9482 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9483 | u64 num_bytes; |
| 9484 | |
| 9485 | BUG_ON(!cache->ro); |
| 9486 | |
| 9487 | spin_lock(&sinfo->lock); |
| 9488 | spin_lock(&cache->lock); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9489 | if (!--cache->ro) { |
| 9490 | num_bytes = cache->key.offset - cache->reserved - |
| 9491 | cache->pinned - cache->bytes_super - |
| 9492 | btrfs_block_group_used(&cache->item); |
| 9493 | sinfo->bytes_readonly -= num_bytes; |
| 9494 | list_del_init(&cache->ro_list); |
| 9495 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9496 | spin_unlock(&cache->lock); |
| 9497 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9498 | } |
| 9499 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9500 | /* |
| 9501 | * checks to see if its even possible to relocate this block group. |
| 9502 | * |
| 9503 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 9504 | * ok to go ahead and try. |
| 9505 | */ |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9506 | int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9507 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9508 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9509 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9510 | struct btrfs_space_info *space_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9511 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9512 | struct btrfs_device *device; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9513 | struct btrfs_trans_handle *trans; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9514 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9515 | u64 dev_min = 1; |
| 9516 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9517 | u64 target; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9518 | int debug; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9519 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9520 | int full = 0; |
| 9521 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9522 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9523 | debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG); |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9524 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9525 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9526 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9527 | /* odd, couldn't find the block group, leave it alone */ |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9528 | if (!block_group) { |
| 9529 | if (debug) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9530 | btrfs_warn(fs_info, |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9531 | "can't find block group for bytenr %llu", |
| 9532 | bytenr); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9533 | return -1; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9534 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9535 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9536 | min_free = btrfs_block_group_used(&block_group->item); |
| 9537 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9538 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9539 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9540 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9541 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9542 | space_info = block_group->space_info; |
| 9543 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9544 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9545 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9546 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9547 | /* |
| 9548 | * if this is the last block group we have in this space, we can't |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 9549 | * relocate it unless we're able to allocate a new chunk below. |
| 9550 | * |
| 9551 | * Otherwise, we need to make sure we have room in the space to handle |
| 9552 | * all of the extents from this block group. If we can, we're good |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9553 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 9554 | if ((space_info->total_bytes != block_group->key.offset) && |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 9555 | (btrfs_space_info_used(space_info, false) + min_free < |
| 9556 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9557 | spin_unlock(&space_info->lock); |
| 9558 | goto out; |
| 9559 | } |
| 9560 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9561 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9562 | /* |
| 9563 | * ok we don't have enough space, but maybe we have free space on our |
| 9564 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9565 | * alloc devices and guess if we have enough space. if this block |
| 9566 | * group is going to be restriped, run checks against the target |
| 9567 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9568 | */ |
| 9569 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 9570 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9571 | /* |
| 9572 | * index: |
| 9573 | * 0: raid10 |
| 9574 | * 1: raid1 |
| 9575 | * 2: dup |
| 9576 | * 3: raid0 |
| 9577 | * 4: single |
| 9578 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9579 | target = get_restripe_target(fs_info, block_group->flags); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9580 | if (target) { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 9581 | index = __get_raid_index(extended_to_chunk(target)); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9582 | } else { |
| 9583 | /* |
| 9584 | * this is just a balance, so if we were marked as full |
| 9585 | * we know there is no space for a new chunk |
| 9586 | */ |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9587 | if (full) { |
| 9588 | if (debug) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9589 | btrfs_warn(fs_info, |
| 9590 | "no space to alloc new chunk for block group %llu", |
| 9591 | block_group->key.objectid); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9592 | goto out; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9593 | } |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9594 | |
| 9595 | index = get_block_group_index(block_group); |
| 9596 | } |
| 9597 | |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9598 | if (index == BTRFS_RAID_RAID10) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9599 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9600 | /* Divide by 2 */ |
| 9601 | min_free >>= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9602 | } else if (index == BTRFS_RAID_RAID1) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9603 | dev_min = 2; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9604 | } else if (index == BTRFS_RAID_DUP) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9605 | /* Multiply by 2 */ |
| 9606 | min_free <<= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9607 | } else if (index == BTRFS_RAID_RAID0) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9608 | dev_min = fs_devices->rw_devices; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 9609 | min_free = div64_u64(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9610 | } |
| 9611 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9612 | /* We need to do this so that we can look at pending chunks */ |
| 9613 | trans = btrfs_join_transaction(root); |
| 9614 | if (IS_ERR(trans)) { |
| 9615 | ret = PTR_ERR(trans); |
| 9616 | goto out; |
| 9617 | } |
| 9618 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9619 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9620 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9621 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9622 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9623 | /* |
| 9624 | * check to make sure we can actually find a chunk with enough |
| 9625 | * space to fit our block group in. |
| 9626 | */ |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 9627 | if (device->total_bytes > device->bytes_used + min_free && |
| 9628 | !device->is_tgtdev_for_dev_replace) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9629 | ret = find_free_dev_extent(trans, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9630 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9631 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9632 | dev_nr++; |
| 9633 | |
| 9634 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9635 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9636 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9637 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9638 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9639 | } |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9640 | if (debug && ret == -1) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9641 | btrfs_warn(fs_info, |
| 9642 | "no space to allocate a new chunk for block group %llu", |
| 9643 | block_group->key.objectid); |
| 9644 | mutex_unlock(&fs_info->chunk_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9645 | btrfs_end_transaction(trans); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9646 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9647 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9648 | return ret; |
| 9649 | } |
| 9650 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9651 | static int find_first_block_group(struct btrfs_fs_info *fs_info, |
| 9652 | struct btrfs_path *path, |
| 9653 | struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9654 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9655 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9656 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9657 | struct btrfs_key found_key; |
| 9658 | struct extent_buffer *leaf; |
| 9659 | int slot; |
| 9660 | |
| 9661 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 9662 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9663 | goto out; |
| 9664 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9665 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9666 | slot = path->slots[0]; |
| 9667 | leaf = path->nodes[0]; |
| 9668 | if (slot >= btrfs_header_nritems(leaf)) { |
| 9669 | ret = btrfs_next_leaf(root, path); |
| 9670 | if (ret == 0) |
| 9671 | continue; |
| 9672 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9673 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9674 | break; |
| 9675 | } |
| 9676 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 9677 | |
| 9678 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9679 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 9680 | struct extent_map_tree *em_tree; |
| 9681 | struct extent_map *em; |
| 9682 | |
| 9683 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 9684 | read_lock(&em_tree->lock); |
| 9685 | em = lookup_extent_mapping(em_tree, found_key.objectid, |
| 9686 | found_key.offset); |
| 9687 | read_unlock(&em_tree->lock); |
| 9688 | if (!em) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9689 | btrfs_err(fs_info, |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 9690 | "logical %llu len %llu found bg but no related chunk", |
| 9691 | found_key.objectid, found_key.offset); |
| 9692 | ret = -ENOENT; |
| 9693 | } else { |
| 9694 | ret = 0; |
| 9695 | } |
Josef Bacik | 187ee58 | 2016-08-18 15:30:06 -0400 | [diff] [blame] | 9696 | free_extent_map(em); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9697 | goto out; |
| 9698 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9699 | path->slots[0]++; |
| 9700 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9701 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9702 | return ret; |
| 9703 | } |
| 9704 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9705 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 9706 | { |
| 9707 | struct btrfs_block_group_cache *block_group; |
| 9708 | u64 last = 0; |
| 9709 | |
| 9710 | while (1) { |
| 9711 | struct inode *inode; |
| 9712 | |
| 9713 | block_group = btrfs_lookup_first_block_group(info, last); |
| 9714 | while (block_group) { |
| 9715 | spin_lock(&block_group->lock); |
| 9716 | if (block_group->iref) |
| 9717 | break; |
| 9718 | spin_unlock(&block_group->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9719 | block_group = next_block_group(info, block_group); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9720 | } |
| 9721 | if (!block_group) { |
| 9722 | if (last == 0) |
| 9723 | break; |
| 9724 | last = 0; |
| 9725 | continue; |
| 9726 | } |
| 9727 | |
| 9728 | inode = block_group->inode; |
| 9729 | block_group->iref = 0; |
| 9730 | block_group->inode = NULL; |
| 9731 | spin_unlock(&block_group->lock); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 9732 | ASSERT(block_group->io_ctl.inode == NULL); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9733 | iput(inode); |
| 9734 | last = block_group->key.objectid + block_group->key.offset; |
| 9735 | btrfs_put_block_group(block_group); |
| 9736 | } |
| 9737 | } |
| 9738 | |
Filipe Manana | 5cdd7db | 2017-02-01 22:39:50 +0000 | [diff] [blame] | 9739 | /* |
| 9740 | * Must be called only after stopping all workers, since we could have block |
| 9741 | * group caching kthreads running, and therefore they could race with us if we |
| 9742 | * freed the block groups before stopping them. |
| 9743 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9744 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 9745 | { |
| 9746 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9747 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9748 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9749 | struct rb_node *n; |
| 9750 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9751 | down_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9752 | while (!list_empty(&info->caching_block_groups)) { |
| 9753 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 9754 | struct btrfs_caching_control, list); |
| 9755 | list_del(&caching_ctl->list); |
| 9756 | put_caching_control(caching_ctl); |
| 9757 | } |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9758 | up_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9759 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9760 | spin_lock(&info->unused_bgs_lock); |
| 9761 | while (!list_empty(&info->unused_bgs)) { |
| 9762 | block_group = list_first_entry(&info->unused_bgs, |
| 9763 | struct btrfs_block_group_cache, |
| 9764 | bg_list); |
| 9765 | list_del_init(&block_group->bg_list); |
| 9766 | btrfs_put_block_group(block_group); |
| 9767 | } |
| 9768 | spin_unlock(&info->unused_bgs_lock); |
| 9769 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9770 | spin_lock(&info->block_group_cache_lock); |
| 9771 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 9772 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 9773 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9774 | rb_erase(&block_group->cache_node, |
| 9775 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 9776 | RB_CLEAR_NODE(&block_group->cache_node); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9777 | spin_unlock(&info->block_group_cache_lock); |
| 9778 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9779 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9780 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9781 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9782 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9783 | /* |
| 9784 | * We haven't cached this block group, which means we could |
| 9785 | * possibly have excluded extents on this block group. |
| 9786 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 9787 | if (block_group->cached == BTRFS_CACHE_NO || |
| 9788 | block_group->cached == BTRFS_CACHE_ERROR) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9789 | free_excluded_extents(info, block_group); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9790 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9791 | btrfs_remove_free_space_cache(block_group); |
Filipe Manana | 5cdd7db | 2017-02-01 22:39:50 +0000 | [diff] [blame] | 9792 | ASSERT(block_group->cached != BTRFS_CACHE_STARTED); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 9793 | ASSERT(list_empty(&block_group->dirty_list)); |
| 9794 | ASSERT(list_empty(&block_group->io_list)); |
| 9795 | ASSERT(list_empty(&block_group->bg_list)); |
| 9796 | ASSERT(atomic_read(&block_group->count) == 1); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 9797 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9798 | |
| 9799 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9800 | } |
| 9801 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9802 | |
| 9803 | /* now that all the block groups are freed, go through and |
| 9804 | * free all the space_info structs. This is only called during |
| 9805 | * the final stages of unmount, and so we know nobody is |
| 9806 | * using them. We call synchronize_rcu() once before we start, |
| 9807 | * just to be on the safe side. |
| 9808 | */ |
| 9809 | synchronize_rcu(); |
| 9810 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 9811 | release_global_block_rsv(info); |
| 9812 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9813 | while (!list_empty(&info->space_info)) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9814 | int i; |
| 9815 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9816 | space_info = list_entry(info->space_info.next, |
| 9817 | struct btrfs_space_info, |
| 9818 | list); |
Josef Bacik | d555b6c | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 9819 | |
| 9820 | /* |
| 9821 | * Do not hide this behind enospc_debug, this is actually |
| 9822 | * important and indicates a real bug if this happens. |
| 9823 | */ |
| 9824 | if (WARN_ON(space_info->bytes_pinned > 0 || |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 9825 | space_info->bytes_reserved > 0 || |
Josef Bacik | d555b6c | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 9826 | space_info->bytes_may_use > 0)) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9827 | dump_space_info(info, space_info, 0, 0); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9828 | list_del(&space_info->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9829 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 9830 | struct kobject *kobj; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9831 | kobj = space_info->block_group_kobjs[i]; |
| 9832 | space_info->block_group_kobjs[i] = NULL; |
| 9833 | if (kobj) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9834 | kobject_del(kobj); |
| 9835 | kobject_put(kobj); |
| 9836 | } |
| 9837 | } |
| 9838 | kobject_del(&space_info->kobj); |
| 9839 | kobject_put(&space_info->kobj); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9840 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9841 | return 0; |
| 9842 | } |
| 9843 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9844 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 9845 | struct btrfs_block_group_cache *cache) |
| 9846 | { |
| 9847 | int index = get_block_group_index(cache); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9848 | bool first = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9849 | |
| 9850 | down_write(&space_info->groups_sem); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9851 | if (list_empty(&space_info->block_groups[index])) |
| 9852 | first = true; |
| 9853 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 9854 | up_write(&space_info->groups_sem); |
| 9855 | |
| 9856 | if (first) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9857 | struct raid_kobject *rkobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9858 | int ret; |
| 9859 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9860 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 9861 | if (!rkobj) |
| 9862 | goto out_err; |
| 9863 | rkobj->raid_type = index; |
| 9864 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 9865 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, |
| 9866 | "%s", get_raid_name(index)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9867 | if (ret) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9868 | kobject_put(&rkobj->kobj); |
| 9869 | goto out_err; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9870 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9871 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9872 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9873 | |
| 9874 | return; |
| 9875 | out_err: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9876 | btrfs_warn(cache->fs_info, |
| 9877 | "failed to add kobject for block cache, ignoring"); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9878 | } |
| 9879 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9880 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9881 | btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info, |
| 9882 | u64 start, u64 size) |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9883 | { |
| 9884 | struct btrfs_block_group_cache *cache; |
| 9885 | |
| 9886 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 9887 | if (!cache) |
| 9888 | return NULL; |
| 9889 | |
| 9890 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 9891 | GFP_NOFS); |
| 9892 | if (!cache->free_space_ctl) { |
| 9893 | kfree(cache); |
| 9894 | return NULL; |
| 9895 | } |
| 9896 | |
| 9897 | cache->key.objectid = start; |
| 9898 | cache->key.offset = size; |
| 9899 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 9900 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9901 | cache->sectorsize = fs_info->sectorsize; |
| 9902 | cache->fs_info = fs_info; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9903 | cache->full_stripe_len = btrfs_full_stripe_len(fs_info, |
| 9904 | &fs_info->mapping_tree, |
| 9905 | start); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 9906 | set_free_space_tree_thresholds(cache); |
| 9907 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9908 | atomic_set(&cache->count, 1); |
| 9909 | spin_lock_init(&cache->lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9910 | init_rwsem(&cache->data_rwsem); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9911 | INIT_LIST_HEAD(&cache->list); |
| 9912 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9913 | INIT_LIST_HEAD(&cache->bg_list); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9914 | INIT_LIST_HEAD(&cache->ro_list); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9915 | INIT_LIST_HEAD(&cache->dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 9916 | INIT_LIST_HEAD(&cache->io_list); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9917 | btrfs_init_free_space_ctl(cache); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 9918 | atomic_set(&cache->trimming, 0); |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 9919 | mutex_init(&cache->free_space_lock); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9920 | |
| 9921 | return cache; |
| 9922 | } |
| 9923 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 9924 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9925 | { |
| 9926 | struct btrfs_path *path; |
| 9927 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9928 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9929 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9930 | struct btrfs_key key; |
| 9931 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9932 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9933 | int need_clear = 0; |
| 9934 | u64 cache_gen; |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 9935 | u64 feature; |
| 9936 | int mixed; |
| 9937 | |
| 9938 | feature = btrfs_super_incompat_flags(info->super_copy); |
| 9939 | mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 9940 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9941 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9942 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9943 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9944 | path = btrfs_alloc_path(); |
| 9945 | if (!path) |
| 9946 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 9947 | path->reada = READA_FORWARD; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9948 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9949 | cache_gen = btrfs_super_cache_generation(info->super_copy); |
| 9950 | if (btrfs_test_opt(info, SPACE_CACHE) && |
| 9951 | btrfs_super_generation(info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9952 | need_clear = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9953 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 9954 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9955 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9956 | while (1) { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9957 | ret = find_first_block_group(info, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9958 | if (ret > 0) |
| 9959 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9960 | if (ret != 0) |
| 9961 | goto error; |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9962 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9963 | leaf = path->nodes[0]; |
| 9964 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9965 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9966 | cache = btrfs_create_block_group_cache(info, found_key.objectid, |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9967 | found_key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9968 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9969 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9970 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9971 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9972 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9973 | if (need_clear) { |
| 9974 | /* |
| 9975 | * When we mount with old space cache, we need to |
| 9976 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 9977 | * |
| 9978 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 9979 | * truncate the old free space cache inode and |
| 9980 | * setup a new one. |
| 9981 | * b) Setting 'dirty flag' makes sure that we flush |
| 9982 | * the new space cache info onto disk. |
| 9983 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9984 | if (btrfs_test_opt(info, SPACE_CACHE)) |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9985 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9986 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9987 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9988 | read_extent_buffer(leaf, &cache->item, |
| 9989 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 9990 | sizeof(cache->item)); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9991 | cache->flags = btrfs_block_group_flags(&cache->item); |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 9992 | if (!mixed && |
| 9993 | ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && |
| 9994 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { |
| 9995 | btrfs_err(info, |
| 9996 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", |
| 9997 | cache->key.objectid); |
| 9998 | ret = -EINVAL; |
| 9999 | goto error; |
| 10000 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10001 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10002 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10003 | btrfs_release_path(path); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 10004 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10005 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 10006 | * We need to exclude the super stripes now so that the space |
| 10007 | * info has super bytes accounted for, otherwise we'll think |
| 10008 | * we have more space than we actually do. |
| 10009 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10010 | ret = exclude_super_stripes(info, cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10011 | if (ret) { |
| 10012 | /* |
| 10013 | * We may have excluded something, so call this just in |
| 10014 | * case. |
| 10015 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10016 | free_excluded_extents(info, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10017 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10018 | goto error; |
| 10019 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 10020 | |
| 10021 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10022 | * check for two cases, either we are full, and therefore |
| 10023 | * don't need to bother with the caching work since we won't |
| 10024 | * find any space, or we are empty, and we can just add all |
| 10025 | * the space in and be done with it. This saves us _alot_ of |
| 10026 | * time, particularly in the full case. |
| 10027 | */ |
| 10028 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10029 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10030 | cache->cached = BTRFS_CACHE_FINISHED; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10031 | free_excluded_extents(info, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10032 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10033 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10034 | cache->cached = BTRFS_CACHE_FINISHED; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10035 | add_new_free_space(cache, info, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10036 | found_key.objectid, |
| 10037 | found_key.objectid + |
| 10038 | found_key.offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10039 | free_excluded_extents(info, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10040 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 10041 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10042 | ret = btrfs_add_block_group_cache(info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10043 | if (ret) { |
| 10044 | btrfs_remove_free_space_cache(cache); |
| 10045 | btrfs_put_block_group(cache); |
| 10046 | goto error; |
| 10047 | } |
| 10048 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10049 | trace_btrfs_add_block_group(info, cache, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10050 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 10051 | btrfs_block_group_used(&cache->item), |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10052 | cache->bytes_super, &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10053 | if (ret) { |
| 10054 | btrfs_remove_free_space_cache(cache); |
| 10055 | spin_lock(&info->block_group_cache_lock); |
| 10056 | rb_erase(&cache->cache_node, |
| 10057 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 10058 | RB_CLEAR_NODE(&cache->cache_node); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10059 | spin_unlock(&info->block_group_cache_lock); |
| 10060 | btrfs_put_block_group(cache); |
| 10061 | goto error; |
| 10062 | } |
| 10063 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10064 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 10065 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10066 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10067 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10068 | set_avail_alloc_bits(info, cache->flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10069 | if (btrfs_chunk_readonly(info, cache->key.objectid)) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10070 | inc_block_group_ro(cache, 1); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10071 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
| 10072 | spin_lock(&info->unused_bgs_lock); |
| 10073 | /* Should always be true but just in case. */ |
| 10074 | if (list_empty(&cache->bg_list)) { |
| 10075 | btrfs_get_block_group(cache); |
| 10076 | list_add_tail(&cache->bg_list, |
| 10077 | &info->unused_bgs); |
| 10078 | } |
| 10079 | spin_unlock(&info->unused_bgs_lock); |
| 10080 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10081 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10082 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10083 | list_for_each_entry_rcu(space_info, &info->space_info, list) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10084 | if (!(get_alloc_profile(info, space_info->flags) & |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10085 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 10086 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 10087 | BTRFS_BLOCK_GROUP_RAID5 | |
| 10088 | BTRFS_BLOCK_GROUP_RAID6 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10089 | BTRFS_BLOCK_GROUP_DUP))) |
| 10090 | continue; |
| 10091 | /* |
| 10092 | * avoid allocating from un-mirrored block group if there are |
| 10093 | * mirrored block groups. |
| 10094 | */ |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 10095 | list_for_each_entry(cache, |
| 10096 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 10097 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10098 | inc_block_group_ro(cache, 1); |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 10099 | list_for_each_entry(cache, |
| 10100 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 10101 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10102 | inc_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10103 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 10104 | |
| 10105 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10106 | ret = 0; |
| 10107 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10108 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10109 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10110 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10111 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10112 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10113 | struct btrfs_fs_info *fs_info) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10114 | { |
| 10115 | struct btrfs_block_group_cache *block_group, *tmp; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10116 | struct btrfs_root *extent_root = fs_info->extent_root; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10117 | struct btrfs_block_group_item item; |
| 10118 | struct btrfs_key key; |
| 10119 | int ret = 0; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10120 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10121 | |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10122 | trans->can_flush_pending_bgs = false; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10123 | list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10124 | if (ret) |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 10125 | goto next; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10126 | |
| 10127 | spin_lock(&block_group->lock); |
| 10128 | memcpy(&item, &block_group->item, sizeof(item)); |
| 10129 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10130 | spin_unlock(&block_group->lock); |
| 10131 | |
| 10132 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 10133 | sizeof(item)); |
| 10134 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10135 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10136 | ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid, |
| 10137 | key.offset); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 10138 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10139 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10140 | add_block_group_free_space(trans, fs_info, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10141 | /* already aborted the transaction if it failed. */ |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 10142 | next: |
| 10143 | list_del_init(&block_group->bg_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10144 | } |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10145 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10146 | } |
| 10147 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10148 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10149 | struct btrfs_fs_info *fs_info, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 10150 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10151 | u64 size) |
| 10152 | { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10153 | struct btrfs_block_group_cache *cache; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10154 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10155 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10156 | btrfs_set_log_full_commit(fs_info, trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10157 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10158 | cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 10159 | if (!cache) |
| 10160 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 10161 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10162 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10163 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10164 | btrfs_set_block_group_flags(&cache->item, type); |
| 10165 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10166 | cache->flags = type; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10167 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10168 | cache->cached = BTRFS_CACHE_FINISHED; |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10169 | cache->needs_free_space = 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10170 | ret = exclude_super_stripes(fs_info, cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10171 | if (ret) { |
| 10172 | /* |
| 10173 | * We may have excluded something, so call this just in |
| 10174 | * case. |
| 10175 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10176 | free_excluded_extents(fs_info, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10177 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10178 | return ret; |
| 10179 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10180 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10181 | add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10182 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10183 | free_excluded_extents(fs_info, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10184 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10185 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10186 | if (btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10187 | u64 new_bytes_used = size - bytes_used; |
| 10188 | |
| 10189 | bytes_used += new_bytes_used >> 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10190 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10191 | } |
| 10192 | #endif |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10193 | /* |
| 10194 | * Call to ensure the corresponding space_info object is created and |
| 10195 | * assigned to our block group, but don't update its counters just yet. |
| 10196 | * We want our bg to be added to the rbtree with its ->space_info set. |
| 10197 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10198 | ret = update_space_info(fs_info, cache->flags, 0, 0, 0, |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10199 | &cache->space_info); |
| 10200 | if (ret) { |
| 10201 | btrfs_remove_free_space_cache(cache); |
| 10202 | btrfs_put_block_group(cache); |
| 10203 | return ret; |
| 10204 | } |
| 10205 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10206 | ret = btrfs_add_block_group_cache(fs_info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10207 | if (ret) { |
| 10208 | btrfs_remove_free_space_cache(cache); |
| 10209 | btrfs_put_block_group(cache); |
| 10210 | return ret; |
| 10211 | } |
| 10212 | |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10213 | /* |
| 10214 | * Now that our block group has its ->space_info set and is inserted in |
| 10215 | * the rbtree, update the space info's counters. |
| 10216 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10217 | trace_btrfs_add_block_group(fs_info, cache, 1); |
| 10218 | ret = update_space_info(fs_info, cache->flags, size, bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10219 | cache->bytes_super, &cache->space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10220 | if (ret) { |
| 10221 | btrfs_remove_free_space_cache(cache); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10222 | spin_lock(&fs_info->block_group_cache_lock); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10223 | rb_erase(&cache->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10224 | &fs_info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 10225 | RB_CLEAR_NODE(&cache->cache_node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10226 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10227 | btrfs_put_block_group(cache); |
| 10228 | return ret; |
| 10229 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10230 | update_global_block_rsv(fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 10231 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10232 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10233 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10234 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10235 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10236 | set_avail_alloc_bits(fs_info, type); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10237 | return 0; |
| 10238 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10239 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10240 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 10241 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 10242 | u64 extra_flags = chunk_to_extended(flags) & |
| 10243 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10244 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10245 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10246 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 10247 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 10248 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 10249 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 10250 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 10251 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10252 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10253 | } |
| 10254 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10255 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10256 | struct btrfs_fs_info *fs_info, u64 group_start, |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10257 | struct extent_map *em) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10258 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10259 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10260 | struct btrfs_path *path; |
| 10261 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10262 | struct btrfs_free_cluster *cluster; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10263 | struct btrfs_root *tree_root = fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10264 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10265 | struct inode *inode; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10266 | struct kobject *kobj = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10267 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10268 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10269 | int factor; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10270 | struct btrfs_caching_control *caching_ctl = NULL; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10271 | bool remove_em; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10272 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10273 | block_group = btrfs_lookup_block_group(fs_info, group_start); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10274 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10275 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10276 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10277 | /* |
| 10278 | * Free the reserved super bytes from this block group before |
| 10279 | * remove it. |
| 10280 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10281 | free_excluded_extents(fs_info, block_group); |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10282 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10283 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10284 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10285 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 10286 | BTRFS_BLOCK_GROUP_RAID1 | |
| 10287 | BTRFS_BLOCK_GROUP_RAID10)) |
| 10288 | factor = 2; |
| 10289 | else |
| 10290 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10291 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10292 | /* make sure this block group isn't part of an allocation cluster */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10293 | cluster = &fs_info->data_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10294 | spin_lock(&cluster->refill_lock); |
| 10295 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10296 | spin_unlock(&cluster->refill_lock); |
| 10297 | |
| 10298 | /* |
| 10299 | * make sure this block group isn't part of a metadata |
| 10300 | * allocation cluster |
| 10301 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10302 | cluster = &fs_info->meta_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10303 | spin_lock(&cluster->refill_lock); |
| 10304 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10305 | spin_unlock(&cluster->refill_lock); |
| 10306 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10307 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10308 | if (!path) { |
| 10309 | ret = -ENOMEM; |
| 10310 | goto out; |
| 10311 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10312 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10313 | /* |
| 10314 | * get the inode first so any iput calls done for the io_list |
| 10315 | * aren't the final iput (no unlinks allowed now) |
| 10316 | */ |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 10317 | inode = lookup_free_space_inode(fs_info, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10318 | |
| 10319 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 10320 | /* |
| 10321 | * make sure our free spache cache IO is done before remove the |
| 10322 | * free space inode |
| 10323 | */ |
| 10324 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10325 | if (!list_empty(&block_group->io_list)) { |
| 10326 | list_del_init(&block_group->io_list); |
| 10327 | |
| 10328 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); |
| 10329 | |
| 10330 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 10331 | btrfs_wait_cache_io(trans, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10332 | btrfs_put_block_group(block_group); |
| 10333 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10334 | } |
| 10335 | |
| 10336 | if (!list_empty(&block_group->dirty_list)) { |
| 10337 | list_del_init(&block_group->dirty_list); |
| 10338 | btrfs_put_block_group(block_group); |
| 10339 | } |
| 10340 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 10341 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 10342 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10343 | if (!IS_ERR(inode)) { |
Nikolay Borisov | 73f2e54 | 2017-02-20 13:50:59 +0200 | [diff] [blame] | 10344 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10345 | if (ret) { |
| 10346 | btrfs_add_delayed_iput(inode); |
| 10347 | goto out; |
| 10348 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10349 | clear_nlink(inode); |
| 10350 | /* One for the block groups ref */ |
| 10351 | spin_lock(&block_group->lock); |
| 10352 | if (block_group->iref) { |
| 10353 | block_group->iref = 0; |
| 10354 | block_group->inode = NULL; |
| 10355 | spin_unlock(&block_group->lock); |
| 10356 | iput(inode); |
| 10357 | } else { |
| 10358 | spin_unlock(&block_group->lock); |
| 10359 | } |
| 10360 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 10361 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10362 | } |
| 10363 | |
| 10364 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 10365 | key.offset = block_group->key.objectid; |
| 10366 | key.type = 0; |
| 10367 | |
| 10368 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 10369 | if (ret < 0) |
| 10370 | goto out; |
| 10371 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10372 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10373 | if (ret == 0) { |
| 10374 | ret = btrfs_del_item(trans, tree_root, path); |
| 10375 | if (ret) |
| 10376 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10377 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10378 | } |
| 10379 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10380 | spin_lock(&fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10381 | rb_erase(&block_group->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10382 | &fs_info->block_group_cache_tree); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 10383 | RB_CLEAR_NODE(&block_group->cache_node); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 10384 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10385 | if (fs_info->first_logical_byte == block_group->key.objectid) |
| 10386 | fs_info->first_logical_byte = (u64)-1; |
| 10387 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10388 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10389 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10390 | /* |
| 10391 | * we must use list_del_init so people can check to see if they |
| 10392 | * are still on the list after taking the semaphore |
| 10393 | */ |
| 10394 | list_del_init(&block_group->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10395 | if (list_empty(&block_group->space_info->block_groups[index])) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10396 | kobj = block_group->space_info->block_group_kobjs[index]; |
| 10397 | block_group->space_info->block_group_kobjs[index] = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10398 | clear_avail_alloc_bits(fs_info, block_group->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10399 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10400 | up_write(&block_group->space_info->groups_sem); |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10401 | if (kobj) { |
| 10402 | kobject_del(kobj); |
| 10403 | kobject_put(kobj); |
| 10404 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10405 | |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10406 | if (block_group->has_caching_ctl) |
| 10407 | caching_ctl = get_caching_control(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10408 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10409 | wait_block_group_cache_done(block_group); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10410 | if (block_group->has_caching_ctl) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10411 | down_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10412 | if (!caching_ctl) { |
| 10413 | struct btrfs_caching_control *ctl; |
| 10414 | |
| 10415 | list_for_each_entry(ctl, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10416 | &fs_info->caching_block_groups, list) |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10417 | if (ctl->block_group == block_group) { |
| 10418 | caching_ctl = ctl; |
Elena Reshetova | 1e4f471 | 2017-03-03 10:55:14 +0200 | [diff] [blame] | 10419 | refcount_inc(&caching_ctl->count); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10420 | break; |
| 10421 | } |
| 10422 | } |
| 10423 | if (caching_ctl) |
| 10424 | list_del_init(&caching_ctl->list); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10425 | up_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10426 | if (caching_ctl) { |
| 10427 | /* Once for the caching bgs list and once for us. */ |
| 10428 | put_caching_control(caching_ctl); |
| 10429 | put_caching_control(caching_ctl); |
| 10430 | } |
| 10431 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10432 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10433 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10434 | if (!list_empty(&block_group->dirty_list)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10435 | WARN_ON(1); |
| 10436 | } |
| 10437 | if (!list_empty(&block_group->io_list)) { |
| 10438 | WARN_ON(1); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10439 | } |
| 10440 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10441 | btrfs_remove_free_space_cache(block_group); |
| 10442 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10443 | spin_lock(&block_group->space_info->lock); |
Filipe Manana | 75c68e9 | 2015-01-16 13:24:40 +0000 | [diff] [blame] | 10444 | list_del_init(&block_group->ro_list); |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10445 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10446 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10447 | WARN_ON(block_group->space_info->total_bytes |
| 10448 | < block_group->key.offset); |
| 10449 | WARN_ON(block_group->space_info->bytes_readonly |
| 10450 | < block_group->key.offset); |
| 10451 | WARN_ON(block_group->space_info->disk_total |
| 10452 | < block_group->key.offset * factor); |
| 10453 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10454 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 10455 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10456 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10457 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10458 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 10459 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10460 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10461 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 10462 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 10463 | if (!list_empty(&em->list)) { |
| 10464 | /* We're in the transaction->pending_chunks list. */ |
| 10465 | free_extent_map(em); |
| 10466 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10467 | spin_lock(&block_group->lock); |
| 10468 | block_group->removed = 1; |
| 10469 | /* |
| 10470 | * At this point trimming can't start on this block group, because we |
| 10471 | * removed the block group from the tree fs_info->block_group_cache_tree |
| 10472 | * so no one can't find it anymore and even if someone already got this |
| 10473 | * block group before we removed it from the rbtree, they have already |
| 10474 | * incremented block_group->trimming - if they didn't, they won't find |
| 10475 | * any free space entries because we already removed them all when we |
| 10476 | * called btrfs_remove_free_space_cache(). |
| 10477 | * |
| 10478 | * And we must not remove the extent map from the fs_info->mapping_tree |
| 10479 | * to prevent the same logical address range and physical device space |
| 10480 | * ranges from being reused for a new block group. This is because our |
| 10481 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is |
| 10482 | * completely transactionless, so while it is trimming a range the |
| 10483 | * currently running transaction might finish and a new one start, |
| 10484 | * allowing for new block groups to be created that can reuse the same |
| 10485 | * physical device locations unless we take this special care. |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10486 | * |
| 10487 | * There may also be an implicit trim operation if the file system |
| 10488 | * is mounted with -odiscard. The same protections must remain |
| 10489 | * in place until the extents have been discarded completely when |
| 10490 | * the transaction commit has completed. |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10491 | */ |
| 10492 | remove_em = (atomic_read(&block_group->trimming) == 0); |
| 10493 | /* |
| 10494 | * Make sure a trimmer task always sees the em in the pinned_chunks list |
| 10495 | * if it sees block_group->removed == 1 (needs to lock block_group->lock |
| 10496 | * before checking block_group->removed). |
| 10497 | */ |
| 10498 | if (!remove_em) { |
| 10499 | /* |
| 10500 | * Our em might be in trans->transaction->pending_chunks which |
| 10501 | * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks), |
| 10502 | * and so is the fs_info->pinned_chunks list. |
| 10503 | * |
| 10504 | * So at this point we must be holding the chunk_mutex to avoid |
| 10505 | * any races with chunk allocation (more specifically at |
| 10506 | * volumes.c:contains_pending_extent()), to ensure it always |
| 10507 | * sees the em, either in the pending_chunks list or in the |
| 10508 | * pinned_chunks list. |
| 10509 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10510 | list_move_tail(&em->list, &fs_info->pinned_chunks); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10511 | } |
| 10512 | spin_unlock(&block_group->lock); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10513 | |
| 10514 | if (remove_em) { |
| 10515 | struct extent_map_tree *em_tree; |
| 10516 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10517 | em_tree = &fs_info->mapping_tree.map_tree; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10518 | write_lock(&em_tree->lock); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10519 | /* |
| 10520 | * The em might be in the pending_chunks list, so make sure the |
| 10521 | * chunk mutex is locked, since remove_extent_mapping() will |
| 10522 | * delete us from that list. |
| 10523 | */ |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10524 | remove_extent_mapping(em_tree, em); |
| 10525 | write_unlock(&em_tree->lock); |
| 10526 | /* once for the tree */ |
| 10527 | free_extent_map(em); |
| 10528 | } |
| 10529 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 10530 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10531 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10532 | ret = remove_block_group_free_space(trans, fs_info, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10533 | if (ret) |
| 10534 | goto out; |
| 10535 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 10536 | btrfs_put_block_group(block_group); |
| 10537 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10538 | |
| 10539 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 10540 | if (ret > 0) |
| 10541 | ret = -EIO; |
| 10542 | if (ret < 0) |
| 10543 | goto out; |
| 10544 | |
| 10545 | ret = btrfs_del_item(trans, root, path); |
| 10546 | out: |
| 10547 | btrfs_free_path(path); |
| 10548 | return ret; |
| 10549 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10550 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10551 | struct btrfs_trans_handle * |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10552 | btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info, |
| 10553 | const u64 chunk_offset) |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10554 | { |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10555 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 10556 | struct extent_map *em; |
| 10557 | struct map_lookup *map; |
| 10558 | unsigned int num_items; |
| 10559 | |
| 10560 | read_lock(&em_tree->lock); |
| 10561 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 10562 | read_unlock(&em_tree->lock); |
| 10563 | ASSERT(em && em->start == chunk_offset); |
| 10564 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10565 | /* |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10566 | * We need to reserve 3 + N units from the metadata space info in order |
| 10567 | * to remove a block group (done at btrfs_remove_chunk() and at |
| 10568 | * btrfs_remove_block_group()), which are used for: |
| 10569 | * |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10570 | * 1 unit for adding the free space inode's orphan (located in the tree |
| 10571 | * of tree roots). |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10572 | * 1 unit for deleting the block group item (located in the extent |
| 10573 | * tree). |
| 10574 | * 1 unit for deleting the free space item (located in tree of tree |
| 10575 | * roots). |
| 10576 | * N units for deleting N device extent items corresponding to each |
| 10577 | * stripe (located in the device tree). |
| 10578 | * |
| 10579 | * In order to remove a block group we also need to reserve units in the |
| 10580 | * system space info in order to update the chunk tree (update one or |
| 10581 | * more device items and remove one chunk item), but this is done at |
| 10582 | * btrfs_remove_chunk() through a call to check_system_chunk(). |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10583 | */ |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 10584 | map = em->map_lookup; |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10585 | num_items = 3 + map->num_stripes; |
| 10586 | free_extent_map(em); |
| 10587 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10588 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10589 | num_items, 1); |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10590 | } |
| 10591 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10592 | /* |
| 10593 | * Process the unused_bgs list and remove any that don't have any allocated |
| 10594 | * space inside of them. |
| 10595 | */ |
| 10596 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) |
| 10597 | { |
| 10598 | struct btrfs_block_group_cache *block_group; |
| 10599 | struct btrfs_space_info *space_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10600 | struct btrfs_trans_handle *trans; |
| 10601 | int ret = 0; |
| 10602 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 10603 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10604 | return; |
| 10605 | |
| 10606 | spin_lock(&fs_info->unused_bgs_lock); |
| 10607 | while (!list_empty(&fs_info->unused_bgs)) { |
| 10608 | u64 start, end; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10609 | int trimming; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10610 | |
| 10611 | block_group = list_first_entry(&fs_info->unused_bgs, |
| 10612 | struct btrfs_block_group_cache, |
| 10613 | bg_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10614 | list_del_init(&block_group->bg_list); |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10615 | |
| 10616 | space_info = block_group->space_info; |
| 10617 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10618 | if (ret || btrfs_mixed_space_info(space_info)) { |
| 10619 | btrfs_put_block_group(block_group); |
| 10620 | continue; |
| 10621 | } |
| 10622 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10623 | |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10624 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 10625 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10626 | /* Don't want to race with allocators so take the groups_sem */ |
| 10627 | down_write(&space_info->groups_sem); |
| 10628 | spin_lock(&block_group->lock); |
| 10629 | if (block_group->reserved || |
| 10630 | btrfs_block_group_used(&block_group->item) || |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 10631 | block_group->ro || |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10632 | list_is_singular(&block_group->list)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10633 | /* |
| 10634 | * We want to bail if we made new allocations or have |
| 10635 | * outstanding allocations in this block group. We do |
| 10636 | * the ro check in case balance is currently acting on |
| 10637 | * this block group. |
| 10638 | */ |
| 10639 | spin_unlock(&block_group->lock); |
| 10640 | up_write(&space_info->groups_sem); |
| 10641 | goto next; |
| 10642 | } |
| 10643 | spin_unlock(&block_group->lock); |
| 10644 | |
| 10645 | /* We don't want to force the issue, only flip if it's ok. */ |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10646 | ret = inc_block_group_ro(block_group, 0); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10647 | up_write(&space_info->groups_sem); |
| 10648 | if (ret < 0) { |
| 10649 | ret = 0; |
| 10650 | goto next; |
| 10651 | } |
| 10652 | |
| 10653 | /* |
| 10654 | * Want to do this before we do anything else so we can recover |
| 10655 | * properly if we fail to join the transaction. |
| 10656 | */ |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10657 | trans = btrfs_start_trans_remove_block_group(fs_info, |
| 10658 | block_group->key.objectid); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10659 | if (IS_ERR(trans)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10660 | btrfs_dec_block_group_ro(block_group); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10661 | ret = PTR_ERR(trans); |
| 10662 | goto next; |
| 10663 | } |
| 10664 | |
| 10665 | /* |
| 10666 | * We could have pending pinned extents for this block group, |
| 10667 | * just delete them, we don't care about them anymore. |
| 10668 | */ |
| 10669 | start = block_group->key.objectid; |
| 10670 | end = start + block_group->key.offset - 1; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10671 | /* |
| 10672 | * Hold the unused_bg_unpin_mutex lock to avoid racing with |
| 10673 | * btrfs_finish_extent_commit(). If we are at transaction N, |
| 10674 | * another task might be running finish_extent_commit() for the |
| 10675 | * previous transaction N - 1, and have seen a range belonging |
| 10676 | * to the block group in freed_extents[] before we were able to |
| 10677 | * clear the whole block group range from freed_extents[]. This |
| 10678 | * means that task can lookup for the block group after we |
| 10679 | * unpinned it from freed_extents[] and removed it, leading to |
| 10680 | * a BUG_ON() at btrfs_unpin_extent_range(). |
| 10681 | */ |
| 10682 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10683 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 10684 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10685 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10686 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10687 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10688 | goto end_trans; |
| 10689 | } |
| 10690 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 10691 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10692 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10693 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10694 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10695 | goto end_trans; |
| 10696 | } |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10697 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10698 | |
| 10699 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10700 | spin_lock(&space_info->lock); |
| 10701 | spin_lock(&block_group->lock); |
| 10702 | |
| 10703 | space_info->bytes_pinned -= block_group->pinned; |
| 10704 | space_info->bytes_readonly += block_group->pinned; |
| 10705 | percpu_counter_add(&space_info->total_bytes_pinned, |
| 10706 | -block_group->pinned); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10707 | block_group->pinned = 0; |
| 10708 | |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10709 | spin_unlock(&block_group->lock); |
| 10710 | spin_unlock(&space_info->lock); |
| 10711 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10712 | /* DISCARD can flip during remount */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10713 | trimming = btrfs_test_opt(fs_info, DISCARD); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10714 | |
| 10715 | /* Implicit trim during transaction commit. */ |
| 10716 | if (trimming) |
| 10717 | btrfs_get_block_group_trimming(block_group); |
| 10718 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10719 | /* |
| 10720 | * Btrfs_remove_chunk will abort the transaction if things go |
| 10721 | * horribly wrong. |
| 10722 | */ |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 10723 | ret = btrfs_remove_chunk(trans, fs_info, |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10724 | block_group->key.objectid); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10725 | |
| 10726 | if (ret) { |
| 10727 | if (trimming) |
| 10728 | btrfs_put_block_group_trimming(block_group); |
| 10729 | goto end_trans; |
| 10730 | } |
| 10731 | |
| 10732 | /* |
| 10733 | * If we're not mounted with -odiscard, we can just forget |
| 10734 | * about this block group. Otherwise we'll need to wait |
| 10735 | * until transaction commit to do the actual discard. |
| 10736 | */ |
| 10737 | if (trimming) { |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10738 | spin_lock(&fs_info->unused_bgs_lock); |
| 10739 | /* |
| 10740 | * A concurrent scrub might have added us to the list |
| 10741 | * fs_info->unused_bgs, so use a list_move operation |
| 10742 | * to add the block group to the deleted_bgs list. |
| 10743 | */ |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10744 | list_move(&block_group->bg_list, |
| 10745 | &trans->transaction->deleted_bgs); |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10746 | spin_unlock(&fs_info->unused_bgs_lock); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10747 | btrfs_get_block_group(block_group); |
| 10748 | } |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10749 | end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10750 | btrfs_end_transaction(trans); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10751 | next: |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10752 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10753 | btrfs_put_block_group(block_group); |
| 10754 | spin_lock(&fs_info->unused_bgs_lock); |
| 10755 | } |
| 10756 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10757 | } |
| 10758 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10759 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 10760 | { |
| 10761 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10762 | struct btrfs_super_block *disk_super; |
| 10763 | u64 features; |
| 10764 | u64 flags; |
| 10765 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10766 | int ret; |
| 10767 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 10768 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10769 | if (!btrfs_super_root(disk_super)) |
Dan Carpenter | 0dc924c5 | 2016-01-13 15:21:17 +0300 | [diff] [blame] | 10770 | return -EINVAL; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10771 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10772 | features = btrfs_super_incompat_flags(disk_super); |
| 10773 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 10774 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10775 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10776 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10777 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10778 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10779 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10780 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10781 | if (mixed) { |
| 10782 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10783 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10784 | } else { |
| 10785 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10786 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10787 | if (ret) |
| 10788 | goto out; |
| 10789 | |
| 10790 | flags = BTRFS_BLOCK_GROUP_DATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10791 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10792 | } |
| 10793 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10794 | return ret; |
| 10795 | } |
| 10796 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10797 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 10798 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10799 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10800 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10801 | } |
| 10802 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10803 | /* |
| 10804 | * It used to be that old block groups would be left around forever. |
| 10805 | * Iterating over them would be enough to trim unused space. Since we |
| 10806 | * now automatically remove them, we also need to iterate over unallocated |
| 10807 | * space. |
| 10808 | * |
| 10809 | * We don't want a transaction for this since the discard may take a |
| 10810 | * substantial amount of time. We don't require that a transaction be |
| 10811 | * running, but we do need to take a running transaction into account |
| 10812 | * to ensure that we're not discarding chunks that were released in |
| 10813 | * the current transaction. |
| 10814 | * |
| 10815 | * Holding the chunks lock will prevent other threads from allocating |
| 10816 | * or releasing chunks, but it won't prevent a running transaction |
| 10817 | * from committing and releasing the memory that the pending chunks |
| 10818 | * list head uses. For that, we need to take a reference to the |
| 10819 | * transaction. |
| 10820 | */ |
| 10821 | static int btrfs_trim_free_extents(struct btrfs_device *device, |
| 10822 | u64 minlen, u64 *trimmed) |
| 10823 | { |
| 10824 | u64 start = 0, len = 0; |
| 10825 | int ret; |
| 10826 | |
| 10827 | *trimmed = 0; |
| 10828 | |
| 10829 | /* Not writeable = nothing to do. */ |
| 10830 | if (!device->writeable) |
| 10831 | return 0; |
| 10832 | |
| 10833 | /* No free space = nothing to do. */ |
| 10834 | if (device->total_bytes <= device->bytes_used) |
| 10835 | return 0; |
| 10836 | |
| 10837 | ret = 0; |
| 10838 | |
| 10839 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 10840 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10841 | struct btrfs_transaction *trans; |
| 10842 | u64 bytes; |
| 10843 | |
| 10844 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 10845 | if (ret) |
| 10846 | return ret; |
| 10847 | |
| 10848 | down_read(&fs_info->commit_root_sem); |
| 10849 | |
| 10850 | spin_lock(&fs_info->trans_lock); |
| 10851 | trans = fs_info->running_transaction; |
| 10852 | if (trans) |
Elena Reshetova | 9b64f57 | 2017-03-03 10:55:11 +0200 | [diff] [blame] | 10853 | refcount_inc(&trans->use_count); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10854 | spin_unlock(&fs_info->trans_lock); |
| 10855 | |
| 10856 | ret = find_free_dev_extent_start(trans, device, minlen, start, |
| 10857 | &start, &len); |
| 10858 | if (trans) |
| 10859 | btrfs_put_transaction(trans); |
| 10860 | |
| 10861 | if (ret) { |
| 10862 | up_read(&fs_info->commit_root_sem); |
| 10863 | mutex_unlock(&fs_info->chunk_mutex); |
| 10864 | if (ret == -ENOSPC) |
| 10865 | ret = 0; |
| 10866 | break; |
| 10867 | } |
| 10868 | |
| 10869 | ret = btrfs_issue_discard(device->bdev, start, len, &bytes); |
| 10870 | up_read(&fs_info->commit_root_sem); |
| 10871 | mutex_unlock(&fs_info->chunk_mutex); |
| 10872 | |
| 10873 | if (ret) |
| 10874 | break; |
| 10875 | |
| 10876 | start += len; |
| 10877 | *trimmed += bytes; |
| 10878 | |
| 10879 | if (fatal_signal_pending(current)) { |
| 10880 | ret = -ERESTARTSYS; |
| 10881 | break; |
| 10882 | } |
| 10883 | |
| 10884 | cond_resched(); |
| 10885 | } |
| 10886 | |
| 10887 | return ret; |
| 10888 | } |
| 10889 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10890 | 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] | 10891 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10892 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10893 | struct btrfs_device *device; |
| 10894 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10895 | u64 group_trimmed; |
| 10896 | u64 start; |
| 10897 | u64 end; |
| 10898 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10899 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10900 | int ret = 0; |
| 10901 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10902 | /* |
| 10903 | * try to trim all FS space, our block group may start from non-zero. |
| 10904 | */ |
| 10905 | if (range->len == total_bytes) |
| 10906 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 10907 | else |
| 10908 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10909 | |
| 10910 | while (cache) { |
| 10911 | if (cache->key.objectid >= (range->start + range->len)) { |
| 10912 | btrfs_put_block_group(cache); |
| 10913 | break; |
| 10914 | } |
| 10915 | |
| 10916 | start = max(range->start, cache->key.objectid); |
| 10917 | end = min(range->start + range->len, |
| 10918 | cache->key.objectid + cache->key.offset); |
| 10919 | |
| 10920 | if (end - start >= range->minlen) { |
| 10921 | if (!block_group_cache_done(cache)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 10922 | ret = cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 10923 | if (ret) { |
| 10924 | btrfs_put_block_group(cache); |
| 10925 | break; |
| 10926 | } |
| 10927 | ret = wait_block_group_cache_done(cache); |
| 10928 | if (ret) { |
| 10929 | btrfs_put_block_group(cache); |
| 10930 | break; |
| 10931 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10932 | } |
| 10933 | ret = btrfs_trim_block_group(cache, |
| 10934 | &group_trimmed, |
| 10935 | start, |
| 10936 | end, |
| 10937 | range->minlen); |
| 10938 | |
| 10939 | trimmed += group_trimmed; |
| 10940 | if (ret) { |
| 10941 | btrfs_put_block_group(cache); |
| 10942 | break; |
| 10943 | } |
| 10944 | } |
| 10945 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10946 | cache = next_block_group(fs_info, cache); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10947 | } |
| 10948 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10949 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 10950 | devices = &fs_info->fs_devices->alloc_list; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10951 | list_for_each_entry(device, devices, dev_alloc_list) { |
| 10952 | ret = btrfs_trim_free_extents(device, range->minlen, |
| 10953 | &group_trimmed); |
| 10954 | if (ret) |
| 10955 | break; |
| 10956 | |
| 10957 | trimmed += group_trimmed; |
| 10958 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10959 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10960 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10961 | range->len = trimmed; |
| 10962 | return ret; |
| 10963 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10964 | |
| 10965 | /* |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10966 | * btrfs_{start,end}_write_no_snapshoting() are similar to |
| 10967 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 10968 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 10969 | * but flush the data into disk after the snapshot creation, or to prevent |
| 10970 | * operations while snapshoting is ongoing and that cause the snapshot to be |
| 10971 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10972 | */ |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10973 | void btrfs_end_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10974 | { |
| 10975 | percpu_counter_dec(&root->subv_writers->counter); |
| 10976 | /* |
David Sterba | a83342a | 2015-02-16 19:36:47 +0100 | [diff] [blame] | 10977 | * Make sure counter is updated before we wake up waiters. |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10978 | */ |
| 10979 | smp_mb(); |
| 10980 | if (waitqueue_active(&root->subv_writers->wait)) |
| 10981 | wake_up(&root->subv_writers->wait); |
| 10982 | } |
| 10983 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10984 | int btrfs_start_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10985 | { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10986 | if (atomic_read(&root->will_be_snapshoted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10987 | return 0; |
| 10988 | |
| 10989 | percpu_counter_inc(&root->subv_writers->counter); |
| 10990 | /* |
| 10991 | * Make sure counter is updated before we check for snapshot creation. |
| 10992 | */ |
| 10993 | smp_mb(); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10994 | if (atomic_read(&root->will_be_snapshoted)) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10995 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10996 | return 0; |
| 10997 | } |
| 10998 | return 1; |
| 10999 | } |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 11000 | |
| 11001 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 11002 | { |
| 11003 | schedule(); |
| 11004 | return 0; |
| 11005 | } |
| 11006 | |
| 11007 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) |
| 11008 | { |
| 11009 | while (true) { |
| 11010 | int ret; |
| 11011 | |
| 11012 | ret = btrfs_start_write_no_snapshoting(root); |
| 11013 | if (ret) |
| 11014 | break; |
| 11015 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 11016 | wait_snapshoting_atomic_t, |
| 11017 | TASK_UNINTERRUPTIBLE); |
| 11018 | } |
| 11019 | } |