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> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 20 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 22 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 23 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 27 | #include <linux/percpu_counter.h> |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 28 | #include "hash.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 29 | #include "tree-log.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 30 | #include "disk-io.h" |
| 31 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 33 | #include "raid56.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 34 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 35 | #include "free-space-cache.h" |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 36 | #include "free-space-tree.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 37 | #include "math.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 38 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 39 | #include "qgroup.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 40 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 41 | #undef SCRAMBLE_DELAYED_REFS |
| 42 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 43 | /* |
| 44 | * control flags for do_chunk_alloc's force field |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 45 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 46 | * if we really need one. |
| 47 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 48 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 49 | * if we have very few chunks already allocated. This is |
| 50 | * used as part of the clustering code to help make sure |
| 51 | * we have a good pool of storage to cluster in, without |
| 52 | * filling the FS with empty chunks |
| 53 | * |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 54 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 55 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 56 | */ |
| 57 | enum { |
| 58 | CHUNK_ALLOC_NO_FORCE = 0, |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 59 | CHUNK_ALLOC_LIMITED = 1, |
| 60 | CHUNK_ALLOC_FORCE = 2, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 63 | static int update_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 64 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 65 | u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 66 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 67 | struct btrfs_fs_info *fs_info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 68 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 69 | u64 root_objectid, u64 owner_objectid, |
| 70 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 71 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 72 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 73 | struct extent_buffer *leaf, |
| 74 | struct btrfs_extent_item *ei); |
| 75 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 76 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 77 | u64 parent, u64 root_objectid, |
| 78 | u64 flags, u64 owner, u64 offset, |
| 79 | struct btrfs_key *ins, int ref_mod); |
| 80 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 81 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 82 | u64 parent, u64 root_objectid, |
| 83 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 84 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 85 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 86 | struct btrfs_fs_info *fs_info, u64 flags, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 87 | int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 88 | static int find_next_key(struct btrfs_path *path, int level, |
| 89 | struct btrfs_key *key); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 90 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
| 91 | struct btrfs_space_info *info, u64 bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 92 | int dump_block_groups); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 93 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 94 | u64 ram_bytes, u64 num_bytes, int delalloc); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 95 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 96 | u64 num_bytes, int delalloc); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 97 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 98 | u64 num_bytes); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 99 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
| 100 | struct btrfs_space_info *space_info, |
| 101 | u64 orig_bytes, |
| 102 | enum btrfs_reserve_flush_enum flush); |
| 103 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, |
| 104 | struct btrfs_space_info *space_info, |
| 105 | u64 num_bytes); |
| 106 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, |
| 107 | struct btrfs_space_info *space_info, |
| 108 | u64 num_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 109 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 110 | static noinline int |
| 111 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 112 | { |
| 113 | smp_mb(); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 114 | return cache->cached == BTRFS_CACHE_FINISHED || |
| 115 | cache->cached == BTRFS_CACHE_ERROR; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 118 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 119 | { |
| 120 | return (cache->flags & bits) == bits; |
| 121 | } |
| 122 | |
Filipe Manana | 758f2df | 2015-11-19 11:45:48 +0000 | [diff] [blame] | 123 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 124 | { |
| 125 | atomic_inc(&cache->count); |
| 126 | } |
| 127 | |
| 128 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 129 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 130 | if (atomic_dec_and_test(&cache->count)) { |
| 131 | WARN_ON(cache->pinned > 0); |
| 132 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 133 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 134 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 135 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 138 | /* |
| 139 | * this adds the block group to the fs_info rb tree for the block group |
| 140 | * cache |
| 141 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 142 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 143 | struct btrfs_block_group_cache *block_group) |
| 144 | { |
| 145 | struct rb_node **p; |
| 146 | struct rb_node *parent = NULL; |
| 147 | struct btrfs_block_group_cache *cache; |
| 148 | |
| 149 | spin_lock(&info->block_group_cache_lock); |
| 150 | p = &info->block_group_cache_tree.rb_node; |
| 151 | |
| 152 | while (*p) { |
| 153 | parent = *p; |
| 154 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 155 | cache_node); |
| 156 | if (block_group->key.objectid < cache->key.objectid) { |
| 157 | p = &(*p)->rb_left; |
| 158 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 159 | p = &(*p)->rb_right; |
| 160 | } else { |
| 161 | spin_unlock(&info->block_group_cache_lock); |
| 162 | return -EEXIST; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | rb_link_node(&block_group->cache_node, parent, p); |
| 167 | rb_insert_color(&block_group->cache_node, |
| 168 | &info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 169 | |
| 170 | if (info->first_logical_byte > block_group->key.objectid) |
| 171 | info->first_logical_byte = block_group->key.objectid; |
| 172 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 173 | spin_unlock(&info->block_group_cache_lock); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * This will return the block group at or after bytenr if contains is 0, else |
| 180 | * it will return the block group that contains the bytenr |
| 181 | */ |
| 182 | static struct btrfs_block_group_cache * |
| 183 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 184 | int contains) |
| 185 | { |
| 186 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 187 | struct rb_node *n; |
| 188 | u64 end, start; |
| 189 | |
| 190 | spin_lock(&info->block_group_cache_lock); |
| 191 | n = info->block_group_cache_tree.rb_node; |
| 192 | |
| 193 | while (n) { |
| 194 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 195 | cache_node); |
| 196 | end = cache->key.objectid + cache->key.offset - 1; |
| 197 | start = cache->key.objectid; |
| 198 | |
| 199 | if (bytenr < start) { |
| 200 | if (!contains && (!ret || start < ret->key.objectid)) |
| 201 | ret = cache; |
| 202 | n = n->rb_left; |
| 203 | } else if (bytenr > start) { |
| 204 | if (contains && bytenr <= end) { |
| 205 | ret = cache; |
| 206 | break; |
| 207 | } |
| 208 | n = n->rb_right; |
| 209 | } else { |
| 210 | ret = cache; |
| 211 | break; |
| 212 | } |
| 213 | } |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 214 | if (ret) { |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 215 | btrfs_get_block_group(ret); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 216 | if (bytenr == 0 && info->first_logical_byte > ret->key.objectid) |
| 217 | info->first_logical_byte = ret->key.objectid; |
| 218 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 219 | spin_unlock(&info->block_group_cache_lock); |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 224 | static int add_excluded_extent(struct btrfs_fs_info *fs_info, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 225 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 226 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 227 | u64 end = start + num_bytes - 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 228 | set_extent_bits(&fs_info->freed_extents[0], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 229 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 230 | set_extent_bits(&fs_info->freed_extents[1], |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 231 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 232 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 235 | static void free_excluded_extents(struct btrfs_fs_info *fs_info, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 236 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 237 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 238 | u64 start, end; |
| 239 | |
| 240 | start = cache->key.objectid; |
| 241 | end = start + cache->key.offset - 1; |
| 242 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 243 | clear_extent_bits(&fs_info->freed_extents[0], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 244 | start, end, EXTENT_UPTODATE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 245 | clear_extent_bits(&fs_info->freed_extents[1], |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 246 | start, end, EXTENT_UPTODATE); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 249 | static int exclude_super_stripes(struct btrfs_fs_info *fs_info, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 250 | struct btrfs_block_group_cache *cache) |
| 251 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 252 | u64 bytenr; |
| 253 | u64 *logical; |
| 254 | int stripe_len; |
| 255 | int i, nr, ret; |
| 256 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 257 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 258 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 259 | cache->bytes_super += stripe_len; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 260 | ret = add_excluded_extent(fs_info, cache->key.objectid, |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 261 | stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 262 | if (ret) |
| 263 | return ret; |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 266 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 267 | bytenr = btrfs_sb_offset(i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 268 | ret = btrfs_rmap_block(fs_info, cache->key.objectid, |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 269 | bytenr, 0, &logical, &nr, &stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 270 | if (ret) |
| 271 | return ret; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 272 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 273 | while (nr--) { |
Josef Bacik | 51bf5f0 | 2013-04-23 12:55:21 -0400 | [diff] [blame] | 274 | u64 start, len; |
| 275 | |
| 276 | if (logical[nr] > cache->key.objectid + |
| 277 | cache->key.offset) |
| 278 | continue; |
| 279 | |
| 280 | if (logical[nr] + stripe_len <= cache->key.objectid) |
| 281 | continue; |
| 282 | |
| 283 | start = logical[nr]; |
| 284 | if (start < cache->key.objectid) { |
| 285 | start = cache->key.objectid; |
| 286 | len = (logical[nr] + stripe_len) - start; |
| 287 | } else { |
| 288 | len = min_t(u64, stripe_len, |
| 289 | cache->key.objectid + |
| 290 | cache->key.offset - start); |
| 291 | } |
| 292 | |
| 293 | cache->bytes_super += len; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 294 | ret = add_excluded_extent(fs_info, start, len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 295 | if (ret) { |
| 296 | kfree(logical); |
| 297 | return ret; |
| 298 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 299 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 300 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 301 | kfree(logical); |
| 302 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 306 | static struct btrfs_caching_control * |
| 307 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 308 | { |
| 309 | struct btrfs_caching_control *ctl; |
| 310 | |
| 311 | spin_lock(&cache->lock); |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 312 | if (!cache->caching_ctl) { |
| 313 | spin_unlock(&cache->lock); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 317 | ctl = cache->caching_ctl; |
| 318 | atomic_inc(&ctl->count); |
| 319 | spin_unlock(&cache->lock); |
| 320 | return ctl; |
| 321 | } |
| 322 | |
| 323 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 324 | { |
| 325 | if (atomic_dec_and_test(&ctl->count)) |
| 326 | kfree(ctl); |
| 327 | } |
| 328 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 329 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 330 | static void fragment_free_space(struct btrfs_block_group_cache *block_group) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 331 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 332 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 333 | u64 start = block_group->key.objectid; |
| 334 | u64 len = block_group->key.offset; |
| 335 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 336 | fs_info->nodesize : fs_info->sectorsize; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 337 | u64 step = chunk << 1; |
| 338 | |
| 339 | while (len > chunk) { |
| 340 | btrfs_remove_free_space(block_group, start, chunk); |
| 341 | start += step; |
| 342 | if (len < step) |
| 343 | len = 0; |
| 344 | else |
| 345 | len -= step; |
| 346 | } |
| 347 | } |
| 348 | #endif |
| 349 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 350 | /* |
| 351 | * this is only called by cache_block_group, since we could have freed extents |
| 352 | * we need to check the pinned_extents for any extents that can't be used yet |
| 353 | * since their free space will be released as soon as the transaction commits. |
| 354 | */ |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 355 | u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 356 | struct btrfs_fs_info *info, u64 start, u64 end) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 357 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 358 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 359 | int ret; |
| 360 | |
| 361 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 362 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 363 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 364 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 365 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 366 | if (ret) |
| 367 | break; |
| 368 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 369 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 370 | start = extent_end + 1; |
| 371 | } else if (extent_start > start && extent_start < end) { |
| 372 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 373 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 374 | ret = btrfs_add_free_space(block_group, start, |
| 375 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 376 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 377 | start = extent_end + 1; |
| 378 | } else { |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (start < end) { |
| 384 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 385 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 386 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 387 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 390 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 393 | static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 394 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 395 | struct btrfs_block_group_cache *block_group = caching_ctl->block_group; |
| 396 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 397 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 398 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 399 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 400 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 401 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 402 | u64 last = 0; |
| 403 | u32 nritems; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 404 | int ret; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 405 | bool wakeup = true; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 406 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 407 | path = btrfs_alloc_path(); |
| 408 | if (!path) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 409 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 410 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 411 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 412 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 413 | #ifdef CONFIG_BTRFS_DEBUG |
| 414 | /* |
| 415 | * If we're fragmenting we don't want to make anybody think we can |
| 416 | * allocate from this block group until we've had a chance to fragment |
| 417 | * the free space. |
| 418 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 419 | if (btrfs_should_fragment_free_space(block_group)) |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 420 | wakeup = false; |
| 421 | #endif |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 422 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 423 | * We don't want to deadlock with somebody trying to allocate a new |
| 424 | * extent for the extent root while also trying to search the extent |
| 425 | * root to add free space. So we skip locking and search the commit |
| 426 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 427 | */ |
| 428 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 429 | path->search_commit_root = 1; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 430 | path->reada = READA_FORWARD; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 431 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 432 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 433 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 434 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 435 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 436 | next: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 437 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 438 | if (ret < 0) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 439 | goto out; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 440 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 441 | leaf = path->nodes[0]; |
| 442 | nritems = btrfs_header_nritems(leaf); |
| 443 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 444 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 445 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 446 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 447 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 448 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 449 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 450 | if (path->slots[0] < nritems) { |
| 451 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 452 | } else { |
| 453 | ret = find_next_key(path, 0, &key); |
| 454 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 455 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 456 | |
Josef Bacik | c9ea7b2 | 2013-09-19 10:02:11 -0400 | [diff] [blame] | 457 | if (need_resched() || |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 458 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 459 | if (wakeup) |
| 460 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 461 | btrfs_release_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 462 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 463 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 464 | cond_resched(); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 465 | mutex_lock(&caching_ctl->mutex); |
| 466 | down_read(&fs_info->commit_root_sem); |
| 467 | goto next; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 468 | } |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 469 | |
| 470 | ret = btrfs_next_leaf(extent_root, path); |
| 471 | if (ret < 0) |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 472 | goto out; |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 473 | if (ret) |
| 474 | break; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 475 | leaf = path->nodes[0]; |
| 476 | nritems = btrfs_header_nritems(leaf); |
| 477 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 478 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 479 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 480 | if (key.objectid < last) { |
| 481 | key.objectid = last; |
| 482 | key.offset = 0; |
| 483 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 484 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 485 | if (wakeup) |
| 486 | caching_ctl->progress = last; |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 487 | btrfs_release_path(path); |
| 488 | goto next; |
| 489 | } |
| 490 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 491 | if (key.objectid < block_group->key.objectid) { |
| 492 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 493 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 494 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 495 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 496 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 497 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 498 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 499 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 500 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 501 | key.type == BTRFS_METADATA_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 502 | total_found += add_new_free_space(block_group, |
| 503 | fs_info, last, |
| 504 | key.objectid); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 505 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 506 | last = key.objectid + |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 507 | fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 508 | else |
| 509 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 510 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 511 | if (total_found > CACHING_CTL_WAKE_UP) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 512 | total_found = 0; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 513 | if (wakeup) |
| 514 | wake_up(&caching_ctl->wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 515 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 516 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 517 | path->slots[0]++; |
| 518 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 519 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 520 | |
| 521 | total_found += add_new_free_space(block_group, fs_info, last, |
| 522 | block_group->key.objectid + |
| 523 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 524 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 525 | |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 526 | out: |
| 527 | btrfs_free_path(path); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | static noinline void caching_thread(struct btrfs_work *work) |
| 532 | { |
| 533 | struct btrfs_block_group_cache *block_group; |
| 534 | struct btrfs_fs_info *fs_info; |
| 535 | struct btrfs_caching_control *caching_ctl; |
Chris Mason | b4570aa | 2015-12-30 07:37:26 -0800 | [diff] [blame] | 536 | struct btrfs_root *extent_root; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 537 | int ret; |
| 538 | |
| 539 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 540 | block_group = caching_ctl->block_group; |
| 541 | fs_info = block_group->fs_info; |
Chris Mason | b4570aa | 2015-12-30 07:37:26 -0800 | [diff] [blame] | 542 | extent_root = fs_info->extent_root; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 543 | |
| 544 | mutex_lock(&caching_ctl->mutex); |
| 545 | down_read(&fs_info->commit_root_sem); |
| 546 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 547 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) |
| 548 | ret = load_free_space_tree(caching_ctl); |
| 549 | else |
| 550 | ret = load_extent_tree_free(caching_ctl); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 551 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 552 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 553 | block_group->caching_ctl = NULL; |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 554 | block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 555 | spin_unlock(&block_group->lock); |
| 556 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 557 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 558 | if (btrfs_should_fragment_free_space(block_group)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 559 | u64 bytes_used; |
| 560 | |
| 561 | spin_lock(&block_group->space_info->lock); |
| 562 | spin_lock(&block_group->lock); |
| 563 | bytes_used = block_group->key.offset - |
| 564 | btrfs_block_group_used(&block_group->item); |
| 565 | block_group->space_info->bytes_used += bytes_used >> 1; |
| 566 | spin_unlock(&block_group->lock); |
| 567 | spin_unlock(&block_group->space_info->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 568 | fragment_free_space(block_group); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 569 | } |
| 570 | #endif |
| 571 | |
| 572 | caching_ctl->progress = (u64)-1; |
Chris Mason | f7d3d2f | 2015-12-18 11:11:10 -0800 | [diff] [blame] | 573 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 574 | up_read(&fs_info->commit_root_sem); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 575 | free_excluded_extents(fs_info, block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 576 | mutex_unlock(&caching_ctl->mutex); |
Omar Sandoval | 73fa48b | 2015-09-29 20:50:33 -0700 | [diff] [blame] | 577 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 578 | wake_up(&caching_ctl->wait); |
| 579 | |
| 580 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 581 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 582 | } |
| 583 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 584 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 585 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 586 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 587 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 588 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 589 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 590 | int ret = 0; |
| 591 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 592 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 593 | if (!caching_ctl) |
| 594 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 595 | |
| 596 | INIT_LIST_HEAD(&caching_ctl->list); |
| 597 | mutex_init(&caching_ctl->mutex); |
| 598 | init_waitqueue_head(&caching_ctl->wait); |
| 599 | caching_ctl->block_group = cache; |
| 600 | caching_ctl->progress = cache->key.objectid; |
| 601 | atomic_set(&caching_ctl->count, 1); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 602 | btrfs_init_work(&caching_ctl->work, btrfs_cache_helper, |
| 603 | caching_thread, NULL, NULL); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 604 | |
| 605 | spin_lock(&cache->lock); |
| 606 | /* |
| 607 | * This should be a rare occasion, but this could happen I think in the |
| 608 | * case where one thread starts to load the space cache info, and then |
| 609 | * some other thread starts a transaction commit which tries to do an |
| 610 | * allocation while the other thread is still loading the space cache |
| 611 | * info. The previous loop should have kept us from choosing this block |
| 612 | * group, but if we've moved to the state where we will wait on caching |
| 613 | * block groups we need to first check if we're doing a fast load here, |
| 614 | * so we can wait for it to finish, otherwise we could end up allocating |
| 615 | * from a block group who's cache gets evicted for one reason or |
| 616 | * another. |
| 617 | */ |
| 618 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 619 | struct btrfs_caching_control *ctl; |
| 620 | |
| 621 | ctl = cache->caching_ctl; |
| 622 | atomic_inc(&ctl->count); |
| 623 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 624 | spin_unlock(&cache->lock); |
| 625 | |
| 626 | schedule(); |
| 627 | |
| 628 | finish_wait(&ctl->wait, &wait); |
| 629 | put_caching_control(ctl); |
| 630 | spin_lock(&cache->lock); |
| 631 | } |
| 632 | |
| 633 | if (cache->cached != BTRFS_CACHE_NO) { |
| 634 | spin_unlock(&cache->lock); |
| 635 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 636 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 637 | } |
| 638 | WARN_ON(cache->caching_ctl); |
| 639 | cache->caching_ctl = caching_ctl; |
| 640 | cache->cached = BTRFS_CACHE_FAST; |
| 641 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 642 | |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 643 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 644 | mutex_lock(&caching_ctl->mutex); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 645 | ret = load_free_space_cache(fs_info, cache); |
| 646 | |
| 647 | spin_lock(&cache->lock); |
| 648 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 649 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 650 | cache->cached = BTRFS_CACHE_FINISHED; |
| 651 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 652 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 653 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 654 | if (load_cache_only) { |
| 655 | cache->caching_ctl = NULL; |
| 656 | cache->cached = BTRFS_CACHE_NO; |
| 657 | } else { |
| 658 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 659 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 660 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 661 | } |
| 662 | spin_unlock(&cache->lock); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 663 | #ifdef CONFIG_BTRFS_DEBUG |
| 664 | if (ret == 1 && |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 665 | btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 666 | u64 bytes_used; |
| 667 | |
| 668 | spin_lock(&cache->space_info->lock); |
| 669 | spin_lock(&cache->lock); |
| 670 | bytes_used = cache->key.offset - |
| 671 | btrfs_block_group_used(&cache->item); |
| 672 | cache->space_info->bytes_used += bytes_used >> 1; |
| 673 | spin_unlock(&cache->lock); |
| 674 | spin_unlock(&cache->space_info->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 675 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 676 | } |
| 677 | #endif |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 678 | mutex_unlock(&caching_ctl->mutex); |
| 679 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 680 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 681 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 682 | put_caching_control(caching_ctl); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 683 | free_excluded_extents(fs_info, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 684 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 685 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 686 | } else { |
| 687 | /* |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 688 | * We're either using the free space tree or no caching at all. |
| 689 | * Set cached to the appropriate value and wakeup any waiters. |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 690 | */ |
| 691 | spin_lock(&cache->lock); |
| 692 | if (load_cache_only) { |
| 693 | cache->caching_ctl = NULL; |
| 694 | cache->cached = BTRFS_CACHE_NO; |
| 695 | } else { |
| 696 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 697 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 698 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 699 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 700 | wake_up(&caching_ctl->wait); |
| 701 | } |
| 702 | |
| 703 | if (load_cache_only) { |
| 704 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 705 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 706 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 707 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 708 | down_write(&fs_info->commit_root_sem); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 709 | atomic_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 710 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 711 | up_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 712 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 713 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 714 | |
Qu Wenruo | e66f0bb | 2014-02-28 10:46:12 +0800 | [diff] [blame] | 715 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 716 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 717 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 720 | /* |
| 721 | * return the block group that starts at or after bytenr |
| 722 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 723 | static struct btrfs_block_group_cache * |
| 724 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 725 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 726 | return block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 729 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 730 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 731 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 732 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 733 | struct btrfs_fs_info *info, |
| 734 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 735 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 736 | return block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 737 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 738 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 739 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 740 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 741 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 742 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 743 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 744 | |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 745 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 746 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 747 | rcu_read_lock(); |
| 748 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 749 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 750 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 751 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 752 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 753 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 754 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 755 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 756 | } |
| 757 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 758 | /* |
| 759 | * after adding space to the filesystem, we need to clear the full flags |
| 760 | * on all the space infos. |
| 761 | */ |
| 762 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 763 | { |
| 764 | struct list_head *head = &info->space_info; |
| 765 | struct btrfs_space_info *found; |
| 766 | |
| 767 | rcu_read_lock(); |
| 768 | list_for_each_entry_rcu(found, head, list) |
| 769 | found->full = 0; |
| 770 | rcu_read_unlock(); |
| 771 | } |
| 772 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 773 | /* 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] | 774 | 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] | 775 | { |
| 776 | int ret; |
| 777 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 778 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 779 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 780 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 781 | if (!path) |
| 782 | return -ENOMEM; |
| 783 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 784 | key.objectid = start; |
| 785 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 786 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 787 | 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] | 788 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 789 | return ret; |
| 790 | } |
| 791 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 792 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 793 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 794 | * |
| 795 | * the head node for delayed ref is used to store the sum of all the |
| 796 | * reference count modifications queued up in the rbtree. the head |
| 797 | * node may also store the extent flags to set. This way you can check |
| 798 | * to see what the reference count and extent flags would be if all of |
| 799 | * the delayed refs are not processed. |
| 800 | */ |
| 801 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 802 | struct btrfs_fs_info *fs_info, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 803 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 804 | { |
| 805 | struct btrfs_delayed_ref_head *head; |
| 806 | struct btrfs_delayed_ref_root *delayed_refs; |
| 807 | struct btrfs_path *path; |
| 808 | struct btrfs_extent_item *ei; |
| 809 | struct extent_buffer *leaf; |
| 810 | struct btrfs_key key; |
| 811 | u32 item_size; |
| 812 | u64 num_refs; |
| 813 | u64 extent_flags; |
| 814 | int ret; |
| 815 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 816 | /* |
| 817 | * If we don't have skinny metadata, don't bother doing anything |
| 818 | * different |
| 819 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 820 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
| 821 | offset = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 822 | metadata = 0; |
| 823 | } |
| 824 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 825 | path = btrfs_alloc_path(); |
| 826 | if (!path) |
| 827 | return -ENOMEM; |
| 828 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 829 | if (!trans) { |
| 830 | path->skip_locking = 1; |
| 831 | path->search_commit_root = 1; |
| 832 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 833 | |
| 834 | search_again: |
| 835 | key.objectid = bytenr; |
| 836 | key.offset = offset; |
| 837 | if (metadata) |
| 838 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 839 | else |
| 840 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 841 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 842 | 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] | 843 | if (ret < 0) |
| 844 | goto out_free; |
| 845 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 846 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 847 | if (path->slots[0]) { |
| 848 | path->slots[0]--; |
| 849 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 850 | path->slots[0]); |
| 851 | if (key.objectid == bytenr && |
| 852 | key.type == BTRFS_EXTENT_ITEM_KEY && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 853 | key.offset == fs_info->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 854 | ret = 0; |
| 855 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 856 | } |
| 857 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 858 | if (ret == 0) { |
| 859 | leaf = path->nodes[0]; |
| 860 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 861 | if (item_size >= sizeof(*ei)) { |
| 862 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 863 | struct btrfs_extent_item); |
| 864 | num_refs = btrfs_extent_refs(leaf, ei); |
| 865 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 866 | } else { |
| 867 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 868 | struct btrfs_extent_item_v0 *ei0; |
| 869 | BUG_ON(item_size != sizeof(*ei0)); |
| 870 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 871 | struct btrfs_extent_item_v0); |
| 872 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 873 | /* FIXME: this isn't correct for data */ |
| 874 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 875 | #else |
| 876 | BUG(); |
| 877 | #endif |
| 878 | } |
| 879 | BUG_ON(num_refs == 0); |
| 880 | } else { |
| 881 | num_refs = 0; |
| 882 | extent_flags = 0; |
| 883 | ret = 0; |
| 884 | } |
| 885 | |
| 886 | if (!trans) |
| 887 | goto out; |
| 888 | |
| 889 | delayed_refs = &trans->transaction->delayed_refs; |
| 890 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 891 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 892 | if (head) { |
| 893 | if (!mutex_trylock(&head->mutex)) { |
| 894 | atomic_inc(&head->node.refs); |
| 895 | spin_unlock(&delayed_refs->lock); |
| 896 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 897 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 898 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 899 | /* |
| 900 | * Mutex was contended, block until it's released and try |
| 901 | * again |
| 902 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 903 | mutex_lock(&head->mutex); |
| 904 | mutex_unlock(&head->mutex); |
| 905 | btrfs_put_delayed_ref(&head->node); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 906 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 907 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 908 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 909 | if (head->extent_op && head->extent_op->update_flags) |
| 910 | extent_flags |= head->extent_op->flags_to_set; |
| 911 | else |
| 912 | BUG_ON(num_refs == 0); |
| 913 | |
| 914 | num_refs += head->node.ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 915 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 916 | mutex_unlock(&head->mutex); |
| 917 | } |
| 918 | spin_unlock(&delayed_refs->lock); |
| 919 | out: |
| 920 | WARN_ON(num_refs == 0); |
| 921 | if (refs) |
| 922 | *refs = num_refs; |
| 923 | if (flags) |
| 924 | *flags = extent_flags; |
| 925 | out_free: |
| 926 | btrfs_free_path(path); |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 931 | * Back reference rules. Back refs have three main goals: |
| 932 | * |
| 933 | * 1) differentiate between all holders of references to an extent so that |
| 934 | * when a reference is dropped we can make sure it was a valid reference |
| 935 | * before freeing the extent. |
| 936 | * |
| 937 | * 2) Provide enough information to quickly find the holders of an extent |
| 938 | * if we notice a given block is corrupted or bad. |
| 939 | * |
| 940 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 941 | * maintenance. This is actually the same as #2, but with a slightly |
| 942 | * different use case. |
| 943 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 944 | * There are two kinds of back refs. The implicit back refs is optimized |
| 945 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 946 | * back refs of this kind provide information about the block's owner tree |
| 947 | * and the pointer's key. These information allow us to find the block by |
| 948 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 949 | * referenced by their owner trees. The location of tree block is recorded |
| 950 | * in the back refs. Actually the full back refs is generic, and can be |
| 951 | * used in all cases the implicit back refs is used. The major shortcoming |
| 952 | * of the full back refs is its overhead. Every time a tree block gets |
| 953 | * COWed, we have to update back refs entry for all pointers in it. |
| 954 | * |
| 955 | * For a newly allocated tree block, we use implicit back refs for |
| 956 | * pointers in it. This means most tree related operations only involve |
| 957 | * implicit back refs. For a tree block created in old transaction, the |
| 958 | * only way to drop a reference to it is COW it. So we can detect the |
| 959 | * event that tree block loses its owner tree's reference and do the |
| 960 | * back refs conversion. |
| 961 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 962 | * 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] | 963 | * |
| 964 | * The reference count of the block is one and the tree is the block's |
| 965 | * owner tree. Nothing to do in this case. |
| 966 | * |
| 967 | * The reference count of the block is one and the tree is not the |
| 968 | * block's owner tree. In this case, full back refs is used for pointers |
| 969 | * in the block. Remove these full back refs, add implicit back refs for |
| 970 | * every pointers in the new block. |
| 971 | * |
| 972 | * The reference count of the block is greater than one and the tree is |
| 973 | * the block's owner tree. In this case, implicit back refs is used for |
| 974 | * pointers in the block. Add full back refs for every pointers in the |
| 975 | * block, increase lower level extents' reference counts. The original |
| 976 | * implicit back refs are entailed to the new block. |
| 977 | * |
| 978 | * The reference count of the block is greater than one and the tree is |
| 979 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 980 | * the new block, increase lower level extents' reference count. |
| 981 | * |
| 982 | * Back Reference Key composing: |
| 983 | * |
| 984 | * The key objectid corresponds to the first byte in the extent, |
| 985 | * The key type is used to differentiate between types of back refs. |
| 986 | * There are different meanings of the key offset for different types |
| 987 | * of back refs. |
| 988 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 989 | * File extents can be referenced by: |
| 990 | * |
| 991 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 992 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 993 | * - different offsets inside a file (bookend extents in file.c) |
| 994 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 995 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 996 | * |
| 997 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 998 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 999 | * - original offset in the file |
| 1000 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1001 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1002 | * The key offset for the implicit back refs is hash of the first |
| 1003 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1004 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1005 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1006 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1007 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1008 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1009 | * The key offset for the implicit back refs is the first byte of |
| 1010 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1011 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1012 | * When a file extent is allocated, The implicit back refs is used. |
| 1013 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1014 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1015 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 1016 | * |
| 1017 | * When a file extent is removed file truncation, we find the |
| 1018 | * corresponding implicit back refs and check the following fields: |
| 1019 | * |
| 1020 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1021 | * |
| 1022 | * Btree extents can be referenced by: |
| 1023 | * |
| 1024 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1025 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1026 | * Both the implicit back refs and the full back refs for tree blocks |
| 1027 | * only consist of key. The key offset for the implicit back refs is |
| 1028 | * objectid of block's owner tree. The key offset for the full back refs |
| 1029 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1030 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1031 | * When implicit back refs is used, information about the lowest key and |
| 1032 | * level of the tree block are required. These information are stored in |
| 1033 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1034 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1035 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1036 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1037 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1038 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1039 | struct btrfs_path *path, |
| 1040 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1041 | { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1042 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1043 | struct btrfs_extent_item *item; |
| 1044 | struct btrfs_extent_item_v0 *ei0; |
| 1045 | struct btrfs_extent_ref_v0 *ref0; |
| 1046 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1047 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1048 | struct btrfs_key key; |
| 1049 | struct btrfs_key found_key; |
| 1050 | u32 new_size = sizeof(*item); |
| 1051 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1052 | int ret; |
| 1053 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1054 | leaf = path->nodes[0]; |
| 1055 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1056 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1057 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1058 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1059 | struct btrfs_extent_item_v0); |
| 1060 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 1061 | |
| 1062 | if (owner == (u64)-1) { |
| 1063 | while (1) { |
| 1064 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1065 | ret = btrfs_next_leaf(root, path); |
| 1066 | if (ret < 0) |
| 1067 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1068 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1069 | leaf = path->nodes[0]; |
| 1070 | } |
| 1071 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1072 | path->slots[0]); |
| 1073 | BUG_ON(key.objectid != found_key.objectid); |
| 1074 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 1075 | path->slots[0]++; |
| 1076 | continue; |
| 1077 | } |
| 1078 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1079 | struct btrfs_extent_ref_v0); |
| 1080 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1084 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1085 | |
| 1086 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 1087 | new_size += sizeof(*bi); |
| 1088 | |
| 1089 | new_size -= sizeof(*ei0); |
| 1090 | ret = btrfs_search_slot(trans, root, &key, path, |
| 1091 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1092 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1093 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1094 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1095 | |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1096 | btrfs_extend_item(fs_info, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1097 | |
| 1098 | leaf = path->nodes[0]; |
| 1099 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1100 | btrfs_set_extent_refs(leaf, item, refs); |
| 1101 | /* FIXME: get real generation */ |
| 1102 | btrfs_set_extent_generation(leaf, item, 0); |
| 1103 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1104 | btrfs_set_extent_flags(leaf, item, |
| 1105 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1106 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1107 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1108 | /* FIXME: get first key of the block */ |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 1109 | memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1110 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1111 | } else { |
| 1112 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1113 | } |
| 1114 | btrfs_mark_buffer_dirty(leaf); |
| 1115 | return 0; |
| 1116 | } |
| 1117 | #endif |
| 1118 | |
| 1119 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1120 | { |
| 1121 | u32 high_crc = ~(u32)0; |
| 1122 | u32 low_crc = ~(u32)0; |
| 1123 | __le64 lenum; |
| 1124 | |
| 1125 | lenum = cpu_to_le64(root_objectid); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1126 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1127 | lenum = cpu_to_le64(owner); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1128 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1129 | lenum = cpu_to_le64(offset); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1130 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1131 | |
| 1132 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1133 | } |
| 1134 | |
| 1135 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1136 | struct btrfs_extent_data_ref *ref) |
| 1137 | { |
| 1138 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1139 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1140 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1141 | } |
| 1142 | |
| 1143 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1144 | struct btrfs_extent_data_ref *ref, |
| 1145 | u64 root_objectid, u64 owner, u64 offset) |
| 1146 | { |
| 1147 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1148 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1149 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1150 | return 0; |
| 1151 | return 1; |
| 1152 | } |
| 1153 | |
| 1154 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1155 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1156 | struct btrfs_path *path, |
| 1157 | u64 bytenr, u64 parent, |
| 1158 | u64 root_objectid, |
| 1159 | u64 owner, u64 offset) |
| 1160 | { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1161 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1162 | struct btrfs_key key; |
| 1163 | struct btrfs_extent_data_ref *ref; |
| 1164 | struct extent_buffer *leaf; |
| 1165 | u32 nritems; |
| 1166 | int ret; |
| 1167 | int recow; |
| 1168 | int err = -ENOENT; |
| 1169 | |
| 1170 | key.objectid = bytenr; |
| 1171 | if (parent) { |
| 1172 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1173 | key.offset = parent; |
| 1174 | } else { |
| 1175 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1176 | key.offset = hash_extent_data_ref(root_objectid, |
| 1177 | owner, offset); |
| 1178 | } |
| 1179 | again: |
| 1180 | recow = 0; |
| 1181 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1182 | if (ret < 0) { |
| 1183 | err = ret; |
| 1184 | goto fail; |
| 1185 | } |
| 1186 | |
| 1187 | if (parent) { |
| 1188 | if (!ret) |
| 1189 | return 0; |
| 1190 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1191 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1192 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1193 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1194 | if (ret < 0) { |
| 1195 | err = ret; |
| 1196 | goto fail; |
| 1197 | } |
| 1198 | if (!ret) |
| 1199 | return 0; |
| 1200 | #endif |
| 1201 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1205 | nritems = btrfs_header_nritems(leaf); |
| 1206 | while (1) { |
| 1207 | if (path->slots[0] >= nritems) { |
| 1208 | ret = btrfs_next_leaf(root, path); |
| 1209 | if (ret < 0) |
| 1210 | err = ret; |
| 1211 | if (ret) |
| 1212 | goto fail; |
| 1213 | |
| 1214 | leaf = path->nodes[0]; |
| 1215 | nritems = btrfs_header_nritems(leaf); |
| 1216 | recow = 1; |
| 1217 | } |
| 1218 | |
| 1219 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1220 | if (key.objectid != bytenr || |
| 1221 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1222 | goto fail; |
| 1223 | |
| 1224 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1225 | struct btrfs_extent_data_ref); |
| 1226 | |
| 1227 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1228 | owner, offset)) { |
| 1229 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1230 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1231 | goto again; |
| 1232 | } |
| 1233 | err = 0; |
| 1234 | break; |
| 1235 | } |
| 1236 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1237 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1238 | fail: |
| 1239 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1242 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1243 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1244 | struct btrfs_path *path, |
| 1245 | u64 bytenr, u64 parent, |
| 1246 | u64 root_objectid, u64 owner, |
| 1247 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1248 | { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1249 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1250 | struct btrfs_key key; |
| 1251 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1252 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1253 | u32 num_refs; |
| 1254 | int ret; |
| 1255 | |
| 1256 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1257 | if (parent) { |
| 1258 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1259 | key.offset = parent; |
| 1260 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1261 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1262 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1263 | key.offset = hash_extent_data_ref(root_objectid, |
| 1264 | owner, offset); |
| 1265 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1266 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1267 | |
| 1268 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1269 | if (ret && ret != -EEXIST) |
| 1270 | goto fail; |
| 1271 | |
| 1272 | leaf = path->nodes[0]; |
| 1273 | if (parent) { |
| 1274 | struct btrfs_shared_data_ref *ref; |
| 1275 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1276 | struct btrfs_shared_data_ref); |
| 1277 | if (ret == 0) { |
| 1278 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1279 | } else { |
| 1280 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1281 | num_refs += refs_to_add; |
| 1282 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1283 | } |
| 1284 | } else { |
| 1285 | struct btrfs_extent_data_ref *ref; |
| 1286 | while (ret == -EEXIST) { |
| 1287 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1288 | struct btrfs_extent_data_ref); |
| 1289 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1290 | owner, offset)) |
| 1291 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1292 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1293 | key.offset++; |
| 1294 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1295 | size); |
| 1296 | if (ret && ret != -EEXIST) |
| 1297 | goto fail; |
| 1298 | |
| 1299 | leaf = path->nodes[0]; |
| 1300 | } |
| 1301 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1302 | struct btrfs_extent_data_ref); |
| 1303 | if (ret == 0) { |
| 1304 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1305 | root_objectid); |
| 1306 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1307 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1308 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1309 | } else { |
| 1310 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1311 | num_refs += refs_to_add; |
| 1312 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1313 | } |
| 1314 | } |
| 1315 | btrfs_mark_buffer_dirty(leaf); |
| 1316 | ret = 0; |
| 1317 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1318 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1319 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1320 | } |
| 1321 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1322 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1323 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1324 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1325 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1326 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1327 | struct btrfs_key key; |
| 1328 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1329 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1330 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1331 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1332 | int ret = 0; |
| 1333 | |
| 1334 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1335 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1336 | |
| 1337 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1338 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1339 | struct btrfs_extent_data_ref); |
| 1340 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1341 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1342 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1343 | struct btrfs_shared_data_ref); |
| 1344 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1345 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1346 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1347 | struct btrfs_extent_ref_v0 *ref0; |
| 1348 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1349 | struct btrfs_extent_ref_v0); |
| 1350 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1351 | #endif |
| 1352 | } else { |
| 1353 | BUG(); |
| 1354 | } |
| 1355 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1356 | BUG_ON(num_refs < refs_to_drop); |
| 1357 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1358 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1359 | if (num_refs == 0) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1360 | ret = btrfs_del_item(trans, fs_info->extent_root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1361 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1362 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1363 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1364 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1365 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1366 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1367 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1368 | else { |
| 1369 | struct btrfs_extent_ref_v0 *ref0; |
| 1370 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1371 | struct btrfs_extent_ref_v0); |
| 1372 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1373 | } |
| 1374 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1375 | btrfs_mark_buffer_dirty(leaf); |
| 1376 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1377 | return ret; |
| 1378 | } |
| 1379 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 1380 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1381 | struct btrfs_extent_inline_ref *iref) |
| 1382 | { |
| 1383 | struct btrfs_key key; |
| 1384 | struct extent_buffer *leaf; |
| 1385 | struct btrfs_extent_data_ref *ref1; |
| 1386 | struct btrfs_shared_data_ref *ref2; |
| 1387 | u32 num_refs = 0; |
| 1388 | |
| 1389 | leaf = path->nodes[0]; |
| 1390 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1391 | if (iref) { |
| 1392 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1393 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1394 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1395 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1396 | } else { |
| 1397 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1398 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1399 | } |
| 1400 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1401 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1402 | struct btrfs_extent_data_ref); |
| 1403 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1404 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1405 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1406 | struct btrfs_shared_data_ref); |
| 1407 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1408 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1409 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1410 | struct btrfs_extent_ref_v0 *ref0; |
| 1411 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1412 | struct btrfs_extent_ref_v0); |
| 1413 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1414 | #endif |
| 1415 | } else { |
| 1416 | WARN_ON(1); |
| 1417 | } |
| 1418 | return num_refs; |
| 1419 | } |
| 1420 | |
| 1421 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1422 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1423 | struct btrfs_path *path, |
| 1424 | u64 bytenr, u64 parent, |
| 1425 | u64 root_objectid) |
| 1426 | { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1427 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1428 | struct btrfs_key key; |
| 1429 | int ret; |
| 1430 | |
| 1431 | key.objectid = bytenr; |
| 1432 | if (parent) { |
| 1433 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1434 | key.offset = parent; |
| 1435 | } else { |
| 1436 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1437 | key.offset = root_objectid; |
| 1438 | } |
| 1439 | |
| 1440 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1441 | if (ret > 0) |
| 1442 | ret = -ENOENT; |
| 1443 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1444 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1445 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1446 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1447 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1448 | if (ret > 0) |
| 1449 | ret = -ENOENT; |
| 1450 | } |
| 1451 | #endif |
| 1452 | return ret; |
| 1453 | } |
| 1454 | |
| 1455 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1456 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1457 | struct btrfs_path *path, |
| 1458 | u64 bytenr, u64 parent, |
| 1459 | u64 root_objectid) |
| 1460 | { |
| 1461 | struct btrfs_key key; |
| 1462 | int ret; |
| 1463 | |
| 1464 | key.objectid = bytenr; |
| 1465 | if (parent) { |
| 1466 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1467 | key.offset = parent; |
| 1468 | } else { |
| 1469 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1470 | key.offset = root_objectid; |
| 1471 | } |
| 1472 | |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1473 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, |
| 1474 | path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1475 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1476 | return ret; |
| 1477 | } |
| 1478 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1479 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1480 | { |
| 1481 | int type; |
| 1482 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1483 | if (parent > 0) |
| 1484 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1485 | else |
| 1486 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1487 | } else { |
| 1488 | if (parent > 0) |
| 1489 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1490 | else |
| 1491 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1492 | } |
| 1493 | return type; |
| 1494 | } |
| 1495 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1496 | static int find_next_key(struct btrfs_path *path, int level, |
| 1497 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1498 | |
| 1499 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1500 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1501 | if (!path->nodes[level]) |
| 1502 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1503 | if (path->slots[level] + 1 >= |
| 1504 | btrfs_header_nritems(path->nodes[level])) |
| 1505 | continue; |
| 1506 | if (level == 0) |
| 1507 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1508 | path->slots[level] + 1); |
| 1509 | else |
| 1510 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1511 | path->slots[level] + 1); |
| 1512 | return 0; |
| 1513 | } |
| 1514 | return 1; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1519 | * to the address of inline back ref, and 0 is returned. |
| 1520 | * |
| 1521 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1522 | * should be inserted, and -ENOENT is returned. |
| 1523 | * |
| 1524 | * if insert is true and there are too many inline back refs, the path |
| 1525 | * points to the extent item, and -EAGAIN is returned. |
| 1526 | * |
| 1527 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1528 | * items in the tree are ordered. |
| 1529 | */ |
| 1530 | static noinline_for_stack |
| 1531 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1532 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1533 | struct btrfs_path *path, |
| 1534 | struct btrfs_extent_inline_ref **ref_ret, |
| 1535 | u64 bytenr, u64 num_bytes, |
| 1536 | u64 parent, u64 root_objectid, |
| 1537 | u64 owner, u64 offset, int insert) |
| 1538 | { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1539 | struct btrfs_root *root = fs_info->extent_root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1540 | struct btrfs_key key; |
| 1541 | struct extent_buffer *leaf; |
| 1542 | struct btrfs_extent_item *ei; |
| 1543 | struct btrfs_extent_inline_ref *iref; |
| 1544 | u64 flags; |
| 1545 | u64 item_size; |
| 1546 | unsigned long ptr; |
| 1547 | unsigned long end; |
| 1548 | int extra_size; |
| 1549 | int type; |
| 1550 | int want; |
| 1551 | int ret; |
| 1552 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1553 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1554 | |
| 1555 | key.objectid = bytenr; |
| 1556 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1557 | key.offset = num_bytes; |
| 1558 | |
| 1559 | want = extent_ref_type(parent, owner); |
| 1560 | if (insert) { |
| 1561 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1562 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1563 | } else |
| 1564 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * Owner is our parent level, so we can just add one to get the level |
| 1568 | * for the block we are interested in. |
| 1569 | */ |
| 1570 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1571 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1572 | key.offset = owner; |
| 1573 | } |
| 1574 | |
| 1575 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1576 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1577 | if (ret < 0) { |
| 1578 | err = ret; |
| 1579 | goto out; |
| 1580 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1581 | |
| 1582 | /* |
| 1583 | * We may be a newly converted file system which still has the old fat |
| 1584 | * extent entries for metadata, so try and see if we have one of those. |
| 1585 | */ |
| 1586 | if (ret > 0 && skinny_metadata) { |
| 1587 | skinny_metadata = false; |
| 1588 | if (path->slots[0]) { |
| 1589 | path->slots[0]--; |
| 1590 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1591 | path->slots[0]); |
| 1592 | if (key.objectid == bytenr && |
| 1593 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1594 | key.offset == num_bytes) |
| 1595 | ret = 0; |
| 1596 | } |
| 1597 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 1598 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1599 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1600 | key.offset = num_bytes; |
| 1601 | btrfs_release_path(path); |
| 1602 | goto again; |
| 1603 | } |
| 1604 | } |
| 1605 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1606 | if (ret && !insert) { |
| 1607 | err = -ENOENT; |
| 1608 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1609 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1610 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1611 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1612 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1613 | |
| 1614 | leaf = path->nodes[0]; |
| 1615 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1616 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1617 | if (item_size < sizeof(*ei)) { |
| 1618 | if (!insert) { |
| 1619 | err = -ENOENT; |
| 1620 | goto out; |
| 1621 | } |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1622 | ret = convert_extent_item_v0(trans, fs_info, path, owner, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1623 | extra_size); |
| 1624 | if (ret < 0) { |
| 1625 | err = ret; |
| 1626 | goto out; |
| 1627 | } |
| 1628 | leaf = path->nodes[0]; |
| 1629 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1630 | } |
| 1631 | #endif |
| 1632 | BUG_ON(item_size < sizeof(*ei)); |
| 1633 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1634 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1635 | flags = btrfs_extent_flags(leaf, ei); |
| 1636 | |
| 1637 | ptr = (unsigned long)(ei + 1); |
| 1638 | end = (unsigned long)ei + item_size; |
| 1639 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1640 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1641 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1642 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | err = -ENOENT; |
| 1646 | while (1) { |
| 1647 | if (ptr >= end) { |
| 1648 | WARN_ON(ptr > end); |
| 1649 | break; |
| 1650 | } |
| 1651 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1652 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1653 | if (want < type) |
| 1654 | break; |
| 1655 | if (want > type) { |
| 1656 | ptr += btrfs_extent_inline_ref_size(type); |
| 1657 | continue; |
| 1658 | } |
| 1659 | |
| 1660 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1661 | struct btrfs_extent_data_ref *dref; |
| 1662 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1663 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1664 | owner, offset)) { |
| 1665 | err = 0; |
| 1666 | break; |
| 1667 | } |
| 1668 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1669 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1670 | break; |
| 1671 | } else { |
| 1672 | u64 ref_offset; |
| 1673 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1674 | if (parent > 0) { |
| 1675 | if (parent == ref_offset) { |
| 1676 | err = 0; |
| 1677 | break; |
| 1678 | } |
| 1679 | if (ref_offset < parent) |
| 1680 | break; |
| 1681 | } else { |
| 1682 | if (root_objectid == ref_offset) { |
| 1683 | err = 0; |
| 1684 | break; |
| 1685 | } |
| 1686 | if (ref_offset < root_objectid) |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | ptr += btrfs_extent_inline_ref_size(type); |
| 1691 | } |
| 1692 | if (err == -ENOENT && insert) { |
| 1693 | if (item_size + extra_size >= |
| 1694 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1695 | err = -EAGAIN; |
| 1696 | goto out; |
| 1697 | } |
| 1698 | /* |
| 1699 | * To add new inline back ref, we have to make sure |
| 1700 | * there is no corresponding back ref item. |
| 1701 | * For simplicity, we just do not add new inline back |
| 1702 | * ref if there is any kind of item for this block |
| 1703 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1704 | if (find_next_key(path, 0, &key) == 0 && |
| 1705 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1706 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1707 | err = -EAGAIN; |
| 1708 | goto out; |
| 1709 | } |
| 1710 | } |
| 1711 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1712 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1713 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1714 | path->keep_locks = 0; |
| 1715 | btrfs_unlock_up_safe(path, 1); |
| 1716 | } |
| 1717 | return err; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * helper to add new inline back ref |
| 1722 | */ |
| 1723 | static noinline_for_stack |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1724 | void setup_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1725 | struct btrfs_path *path, |
| 1726 | struct btrfs_extent_inline_ref *iref, |
| 1727 | u64 parent, u64 root_objectid, |
| 1728 | u64 owner, u64 offset, int refs_to_add, |
| 1729 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1730 | { |
| 1731 | struct extent_buffer *leaf; |
| 1732 | struct btrfs_extent_item *ei; |
| 1733 | unsigned long ptr; |
| 1734 | unsigned long end; |
| 1735 | unsigned long item_offset; |
| 1736 | u64 refs; |
| 1737 | int size; |
| 1738 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1739 | |
| 1740 | leaf = path->nodes[0]; |
| 1741 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1742 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1743 | |
| 1744 | type = extent_ref_type(parent, owner); |
| 1745 | size = btrfs_extent_inline_ref_size(type); |
| 1746 | |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1747 | btrfs_extend_item(fs_info, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1748 | |
| 1749 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1750 | refs = btrfs_extent_refs(leaf, ei); |
| 1751 | refs += refs_to_add; |
| 1752 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1753 | if (extent_op) |
| 1754 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1755 | |
| 1756 | ptr = (unsigned long)ei + item_offset; |
| 1757 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1758 | if (ptr < end - size) |
| 1759 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1760 | end - size - ptr); |
| 1761 | |
| 1762 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1763 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1764 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1765 | struct btrfs_extent_data_ref *dref; |
| 1766 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1767 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1768 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1769 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1770 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1771 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1772 | struct btrfs_shared_data_ref *sref; |
| 1773 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1774 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1775 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1776 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1777 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1778 | } else { |
| 1779 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1780 | } |
| 1781 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1785 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1786 | struct btrfs_path *path, |
| 1787 | struct btrfs_extent_inline_ref **ref_ret, |
| 1788 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1789 | u64 root_objectid, u64 owner, u64 offset) |
| 1790 | { |
| 1791 | int ret; |
| 1792 | |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1793 | ret = lookup_inline_extent_backref(trans, fs_info, path, ref_ret, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1794 | bytenr, num_bytes, parent, |
| 1795 | root_objectid, owner, offset, 0); |
| 1796 | if (ret != -ENOENT) |
| 1797 | return ret; |
| 1798 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1799 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1800 | *ref_ret = NULL; |
| 1801 | |
| 1802 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1803 | ret = lookup_tree_block_ref(trans, fs_info, path, bytenr, |
| 1804 | parent, root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1805 | } else { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1806 | ret = lookup_extent_data_ref(trans, fs_info, path, bytenr, |
| 1807 | parent, root_objectid, owner, |
| 1808 | offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1809 | } |
| 1810 | return ret; |
| 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * helper to update/remove inline back ref |
| 1815 | */ |
| 1816 | static noinline_for_stack |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1817 | void update_inline_extent_backref(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1818 | struct btrfs_path *path, |
| 1819 | struct btrfs_extent_inline_ref *iref, |
| 1820 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1821 | struct btrfs_delayed_extent_op *extent_op, |
| 1822 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1823 | { |
| 1824 | struct extent_buffer *leaf; |
| 1825 | struct btrfs_extent_item *ei; |
| 1826 | struct btrfs_extent_data_ref *dref = NULL; |
| 1827 | struct btrfs_shared_data_ref *sref = NULL; |
| 1828 | unsigned long ptr; |
| 1829 | unsigned long end; |
| 1830 | u32 item_size; |
| 1831 | int size; |
| 1832 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1833 | u64 refs; |
| 1834 | |
| 1835 | leaf = path->nodes[0]; |
| 1836 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1837 | refs = btrfs_extent_refs(leaf, ei); |
| 1838 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1839 | refs += refs_to_mod; |
| 1840 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1841 | if (extent_op) |
| 1842 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1843 | |
| 1844 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1845 | |
| 1846 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1847 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1848 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1849 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1850 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1851 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1852 | } else { |
| 1853 | refs = 1; |
| 1854 | BUG_ON(refs_to_mod != -1); |
| 1855 | } |
| 1856 | |
| 1857 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1858 | refs += refs_to_mod; |
| 1859 | |
| 1860 | if (refs > 0) { |
| 1861 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1862 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1863 | else |
| 1864 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1865 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1866 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1867 | size = btrfs_extent_inline_ref_size(type); |
| 1868 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1869 | ptr = (unsigned long)iref; |
| 1870 | end = (unsigned long)ei + item_size; |
| 1871 | if (ptr + size < end) |
| 1872 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1873 | end - ptr - size); |
| 1874 | item_size -= size; |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1875 | btrfs_truncate_item(fs_info, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1876 | } |
| 1877 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | static noinline_for_stack |
| 1881 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1882 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1883 | struct btrfs_path *path, |
| 1884 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1885 | u64 root_objectid, u64 owner, |
| 1886 | u64 offset, int refs_to_add, |
| 1887 | struct btrfs_delayed_extent_op *extent_op) |
| 1888 | { |
| 1889 | struct btrfs_extent_inline_ref *iref; |
| 1890 | int ret; |
| 1891 | |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1892 | ret = lookup_inline_extent_backref(trans, fs_info, path, &iref, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1893 | bytenr, num_bytes, parent, |
| 1894 | root_objectid, owner, offset, 1); |
| 1895 | if (ret == 0) { |
| 1896 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1897 | update_inline_extent_backref(fs_info, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1898 | refs_to_add, extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1899 | } else if (ret == -ENOENT) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1900 | setup_inline_extent_backref(fs_info, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1901 | root_objectid, owner, offset, |
| 1902 | refs_to_add, extent_op); |
| 1903 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1904 | } |
| 1905 | return ret; |
| 1906 | } |
| 1907 | |
| 1908 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1909 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1910 | struct btrfs_path *path, |
| 1911 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1912 | u64 owner, u64 offset, int refs_to_add) |
| 1913 | { |
| 1914 | int ret; |
| 1915 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1916 | BUG_ON(refs_to_add != 1); |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1917 | ret = insert_tree_block_ref(trans, fs_info, path, bytenr, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1918 | parent, root_objectid); |
| 1919 | } else { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1920 | ret = insert_extent_data_ref(trans, fs_info, path, bytenr, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1921 | parent, root_objectid, |
| 1922 | owner, offset, refs_to_add); |
| 1923 | } |
| 1924 | return ret; |
| 1925 | } |
| 1926 | |
| 1927 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1928 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1929 | struct btrfs_path *path, |
| 1930 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1931 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1932 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1933 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1934 | |
| 1935 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1936 | if (iref) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1937 | update_inline_extent_backref(fs_info, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1938 | -refs_to_drop, NULL, last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1939 | } else if (is_data) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1940 | ret = remove_extent_data_ref(trans, fs_info, path, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1941 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1942 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1943 | *last_ref = 1; |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 1944 | ret = btrfs_del_item(trans, fs_info->extent_root, path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1945 | } |
| 1946 | return ret; |
| 1947 | } |
| 1948 | |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1949 | #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len)) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1950 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1951 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1952 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1953 | int j, ret = 0; |
| 1954 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1955 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1956 | |
| 1957 | if (WARN_ON(start != aligned_start)) { |
| 1958 | len -= aligned_start - start; |
| 1959 | len = round_down(len, 1 << 9); |
| 1960 | start = aligned_start; |
| 1961 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1962 | |
| 1963 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1964 | |
| 1965 | if (!len) |
| 1966 | return 0; |
| 1967 | |
| 1968 | end = start + len; |
| 1969 | bytes_left = len; |
| 1970 | |
| 1971 | /* Skip any superblocks on this device. */ |
| 1972 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1973 | u64 sb_start = btrfs_sb_offset(j); |
| 1974 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1975 | u64 size = sb_start - start; |
| 1976 | |
| 1977 | if (!in_range(sb_start, start, bytes_left) && |
| 1978 | !in_range(sb_end, start, bytes_left) && |
| 1979 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1980 | continue; |
| 1981 | |
| 1982 | /* |
| 1983 | * Superblock spans beginning of range. Adjust start and |
| 1984 | * try again. |
| 1985 | */ |
| 1986 | if (sb_start <= start) { |
| 1987 | start += sb_end - start; |
| 1988 | if (start > end) { |
| 1989 | bytes_left = 0; |
| 1990 | break; |
| 1991 | } |
| 1992 | bytes_left = end - start; |
| 1993 | continue; |
| 1994 | } |
| 1995 | |
| 1996 | if (size) { |
| 1997 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1998 | GFP_NOFS, 0); |
| 1999 | if (!ret) |
| 2000 | *discarded_bytes += size; |
| 2001 | else if (ret != -EOPNOTSUPP) |
| 2002 | return ret; |
| 2003 | } |
| 2004 | |
| 2005 | start = sb_end; |
| 2006 | if (start > end) { |
| 2007 | bytes_left = 0; |
| 2008 | break; |
| 2009 | } |
| 2010 | bytes_left = end - start; |
| 2011 | } |
| 2012 | |
| 2013 | if (bytes_left) { |
| 2014 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2015 | GFP_NOFS, 0); |
| 2016 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 2017 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2018 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2019 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2020 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2021 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2022 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 2023 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2024 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2025 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2026 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2027 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2028 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2029 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 2030 | /* |
| 2031 | * Avoid races with device replace and make sure our bbio has devices |
| 2032 | * associated to its stripes that don't go away while we are discarding. |
| 2033 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2034 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2035 | /* Tell the block device(s) that the sectors can be discarded */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2036 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
| 2037 | &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2038 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2039 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2040 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2041 | int i; |
| 2042 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2043 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2044 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2045 | u64 bytes; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2046 | if (!stripe->dev->can_discard) |
| 2047 | continue; |
| 2048 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2049 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 2050 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2051 | stripe->length, |
| 2052 | &bytes); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2053 | if (!ret) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2054 | discarded_bytes += bytes; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2055 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2056 | 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] | 2057 | |
| 2058 | /* |
| 2059 | * Just in case we get back EOPNOTSUPP for some reason, |
| 2060 | * just ignore the return value so we don't screw up |
| 2061 | * people calling discard_extent. |
| 2062 | */ |
| 2063 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2064 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2065 | btrfs_put_bbio(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2066 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2067 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2068 | |
| 2069 | if (actual_bytes) |
| 2070 | *actual_bytes = discarded_bytes; |
| 2071 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2072 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2073 | if (ret == -EOPNOTSUPP) |
| 2074 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2075 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2076 | } |
| 2077 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2078 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2079 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2080 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2081 | u64 bytenr, u64 num_bytes, u64 parent, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2082 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2083 | { |
| 2084 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2085 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2086 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 2087 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2088 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2089 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2090 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 2091 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2092 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2093 | BTRFS_ADD_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2094 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2095 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2096 | num_bytes, parent, root_objectid, |
| 2097 | owner, offset, 0, |
Jeff Mahoney | fef394f | 2016-12-13 14:39:34 -0500 | [diff] [blame] | 2098 | BTRFS_ADD_DELAYED_REF); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2099 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2100 | return ret; |
| 2101 | } |
| 2102 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2103 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2104 | struct btrfs_fs_info *fs_info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2105 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2106 | u64 parent, u64 root_objectid, |
| 2107 | u64 owner, u64 offset, int refs_to_add, |
| 2108 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2109 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2110 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2111 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2112 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2113 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2114 | u64 bytenr = node->bytenr; |
| 2115 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2116 | u64 refs; |
| 2117 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2118 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2119 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2120 | if (!path) |
| 2121 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2122 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2123 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2124 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2125 | /* this will setup the path even if it fails to insert the back ref */ |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 2126 | ret = insert_inline_extent_backref(trans, fs_info, path, bytenr, |
| 2127 | num_bytes, parent, root_objectid, |
| 2128 | owner, offset, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2129 | refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 2130 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2131 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2132 | |
| 2133 | /* |
| 2134 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 2135 | * inline extent ref, so just update the reference count and add a |
| 2136 | * normal backref. |
| 2137 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2138 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2139 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2140 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2141 | refs = btrfs_extent_refs(leaf, item); |
| 2142 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 2143 | if (extent_op) |
| 2144 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2145 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2146 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2147 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2148 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2149 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2150 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2151 | /* now insert the actual backref */ |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 2152 | ret = insert_extent_backref(trans, fs_info, path, bytenr, parent, |
| 2153 | root_objectid, owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2154 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2155 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2156 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2157 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 2158 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2159 | } |
| 2160 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2161 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2162 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2163 | struct btrfs_delayed_ref_node *node, |
| 2164 | struct btrfs_delayed_extent_op *extent_op, |
| 2165 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2166 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2167 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2168 | struct btrfs_delayed_data_ref *ref; |
| 2169 | struct btrfs_key ins; |
| 2170 | u64 parent = 0; |
| 2171 | u64 ref_root = 0; |
| 2172 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2173 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2174 | ins.objectid = node->bytenr; |
| 2175 | ins.offset = node->num_bytes; |
| 2176 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2177 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2178 | ref = btrfs_delayed_node_to_data_ref(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2179 | trace_run_delayed_data_ref(fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2180 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2181 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2182 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2183 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2184 | |
| 2185 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2186 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2187 | flags |= extent_op->flags_to_set; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2188 | ret = alloc_reserved_file_extent(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2189 | parent, ref_root, flags, |
| 2190 | ref->objectid, ref->offset, |
| 2191 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2192 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2193 | ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2194 | ref_root, ref->objectid, |
| 2195 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2196 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2197 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2198 | ret = __btrfs_free_extent(trans, fs_info, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2199 | ref_root, ref->objectid, |
| 2200 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2201 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2202 | } else { |
| 2203 | BUG(); |
| 2204 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2205 | return ret; |
| 2206 | } |
| 2207 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2208 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2209 | struct extent_buffer *leaf, |
| 2210 | struct btrfs_extent_item *ei) |
| 2211 | { |
| 2212 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2213 | if (extent_op->update_flags) { |
| 2214 | flags |= extent_op->flags_to_set; |
| 2215 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2216 | } |
| 2217 | |
| 2218 | if (extent_op->update_key) { |
| 2219 | struct btrfs_tree_block_info *bi; |
| 2220 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2221 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2222 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2227 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2228 | struct btrfs_delayed_ref_node *node, |
| 2229 | struct btrfs_delayed_extent_op *extent_op) |
| 2230 | { |
| 2231 | struct btrfs_key key; |
| 2232 | struct btrfs_path *path; |
| 2233 | struct btrfs_extent_item *ei; |
| 2234 | struct extent_buffer *leaf; |
| 2235 | u32 item_size; |
| 2236 | int ret; |
| 2237 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2238 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2239 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2240 | if (trans->aborted) |
| 2241 | return 0; |
| 2242 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2243 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2244 | metadata = 0; |
| 2245 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2246 | path = btrfs_alloc_path(); |
| 2247 | if (!path) |
| 2248 | return -ENOMEM; |
| 2249 | |
| 2250 | key.objectid = node->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2251 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2252 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2253 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2254 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2255 | } else { |
| 2256 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2257 | key.offset = node->num_bytes; |
| 2258 | } |
| 2259 | |
| 2260 | again: |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2261 | path->reada = READA_FORWARD; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2262 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2263 | 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] | 2264 | if (ret < 0) { |
| 2265 | err = ret; |
| 2266 | goto out; |
| 2267 | } |
| 2268 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2269 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2270 | if (path->slots[0] > 0) { |
| 2271 | path->slots[0]--; |
| 2272 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 2273 | path->slots[0]); |
| 2274 | if (key.objectid == node->bytenr && |
| 2275 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2276 | key.offset == node->num_bytes) |
| 2277 | ret = 0; |
| 2278 | } |
| 2279 | if (ret > 0) { |
| 2280 | btrfs_release_path(path); |
| 2281 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2282 | |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2283 | key.objectid = node->bytenr; |
| 2284 | key.offset = node->num_bytes; |
| 2285 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2286 | goto again; |
| 2287 | } |
| 2288 | } else { |
| 2289 | err = -EIO; |
| 2290 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2291 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | leaf = path->nodes[0]; |
| 2295 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2296 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2297 | if (item_size < sizeof(*ei)) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 2298 | ret = convert_extent_item_v0(trans, fs_info, path, (u64)-1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2299 | if (ret < 0) { |
| 2300 | err = ret; |
| 2301 | goto out; |
| 2302 | } |
| 2303 | leaf = path->nodes[0]; |
| 2304 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2305 | } |
| 2306 | #endif |
| 2307 | BUG_ON(item_size < sizeof(*ei)); |
| 2308 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2309 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2310 | |
| 2311 | btrfs_mark_buffer_dirty(leaf); |
| 2312 | out: |
| 2313 | btrfs_free_path(path); |
| 2314 | return err; |
| 2315 | } |
| 2316 | |
| 2317 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2318 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2319 | struct btrfs_delayed_ref_node *node, |
| 2320 | struct btrfs_delayed_extent_op *extent_op, |
| 2321 | int insert_reserved) |
| 2322 | { |
| 2323 | int ret = 0; |
| 2324 | struct btrfs_delayed_tree_ref *ref; |
| 2325 | struct btrfs_key ins; |
| 2326 | u64 parent = 0; |
| 2327 | u64 ref_root = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2328 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2329 | |
| 2330 | ref = btrfs_delayed_node_to_tree_ref(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2331 | trace_run_delayed_tree_ref(fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2332 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2333 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2334 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2335 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2336 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2337 | ins.objectid = node->bytenr; |
| 2338 | if (skinny_metadata) { |
| 2339 | ins.offset = ref->level; |
| 2340 | ins.type = BTRFS_METADATA_ITEM_KEY; |
| 2341 | } else { |
| 2342 | ins.offset = node->num_bytes; |
| 2343 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2344 | } |
| 2345 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2346 | if (node->ref_mod != 1) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2347 | btrfs_err(fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2348 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 2349 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 2350 | parent); |
| 2351 | return -EIO; |
| 2352 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2353 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2354 | BUG_ON(!extent_op || !extent_op->update_flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2355 | ret = alloc_reserved_tree_block(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2356 | parent, ref_root, |
| 2357 | extent_op->flags_to_set, |
| 2358 | &extent_op->key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2359 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2360 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2361 | ret = __btrfs_inc_extent_ref(trans, fs_info, node, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2362 | parent, ref_root, |
| 2363 | ref->level, 0, 1, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2364 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2365 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2366 | ret = __btrfs_free_extent(trans, fs_info, node, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2367 | parent, ref_root, |
| 2368 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2369 | } else { |
| 2370 | BUG(); |
| 2371 | } |
| 2372 | return ret; |
| 2373 | } |
| 2374 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2375 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2376 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2377 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2378 | struct btrfs_delayed_ref_node *node, |
| 2379 | struct btrfs_delayed_extent_op *extent_op, |
| 2380 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2381 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2382 | int ret = 0; |
| 2383 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2384 | if (trans->aborted) { |
| 2385 | if (insert_reserved) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2386 | btrfs_pin_extent(fs_info, node->bytenr, |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2387 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2388 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2389 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2390 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2391 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2392 | struct btrfs_delayed_ref_head *head; |
| 2393 | /* |
| 2394 | * we've hit the end of the chain and we were supposed |
| 2395 | * to insert this extent into the tree. But, it got |
| 2396 | * deleted before we ever needed to insert it, so all |
| 2397 | * we have to do is clean up the accounting |
| 2398 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2399 | BUG_ON(extent_op); |
| 2400 | head = btrfs_delayed_node_to_head(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2401 | trace_run_delayed_ref_head(fs_info, node, head, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2402 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2403 | if (insert_reserved) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2404 | btrfs_pin_extent(fs_info, node->bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2405 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2406 | if (head->is_data) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2407 | ret = btrfs_del_csums(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2408 | node->bytenr, |
| 2409 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2410 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2411 | } |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2412 | |
| 2413 | /* Also free its reserved qgroup space */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2414 | btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root, |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2415 | head->qgroup_reserved); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2416 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2417 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2418 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2419 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2420 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2421 | ret = run_delayed_tree_ref(trans, fs_info, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2422 | insert_reserved); |
| 2423 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2424 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2425 | ret = run_delayed_data_ref(trans, fs_info, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2426 | insert_reserved); |
| 2427 | else |
| 2428 | BUG(); |
| 2429 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2430 | } |
| 2431 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2432 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2433 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2434 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2435 | struct btrfs_delayed_ref_node *ref; |
| 2436 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2437 | if (list_empty(&head->ref_list)) |
| 2438 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2439 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2440 | /* |
| 2441 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2442 | * This is to prevent a ref count from going down to zero, which deletes |
| 2443 | * the extent item from the extent tree, when there still are references |
| 2444 | * to add, which would fail because they would not find the extent item. |
| 2445 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2446 | if (!list_empty(&head->ref_add_list)) |
| 2447 | return list_first_entry(&head->ref_add_list, |
| 2448 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2449 | |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2450 | ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node, |
| 2451 | list); |
| 2452 | ASSERT(list_empty(&ref->add_list)); |
| 2453 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2454 | } |
| 2455 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2456 | /* |
| 2457 | * Returns 0 on success or if called with an already aborted transaction. |
| 2458 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2459 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2460 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2461 | struct btrfs_fs_info *fs_info, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2462 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2463 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2464 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2465 | struct btrfs_delayed_ref_node *ref; |
| 2466 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2467 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2468 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2469 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2470 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2471 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2472 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2473 | |
| 2474 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2475 | while (1) { |
| 2476 | if (!locked_ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2477 | if (count >= nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2478 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2479 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2480 | spin_lock(&delayed_refs->lock); |
| 2481 | locked_ref = btrfs_select_ref_head(trans); |
| 2482 | if (!locked_ref) { |
| 2483 | spin_unlock(&delayed_refs->lock); |
| 2484 | break; |
| 2485 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2486 | |
| 2487 | /* grab the lock that says we are going to process |
| 2488 | * all the refs for this head */ |
| 2489 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2490 | spin_unlock(&delayed_refs->lock); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2491 | /* |
| 2492 | * we may have dropped the spin lock to get the head |
| 2493 | * mutex lock, and that might have given someone else |
| 2494 | * time to free the head. If that's true, it has been |
| 2495 | * removed from our list and we can move on. |
| 2496 | */ |
| 2497 | if (ret == -EAGAIN) { |
| 2498 | locked_ref = NULL; |
| 2499 | count++; |
| 2500 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2504 | /* |
| 2505 | * We need to try and merge add/drops of the same ref since we |
| 2506 | * can run into issues with relocate dropping the implicit ref |
| 2507 | * and then it being added back again before the drop can |
| 2508 | * finish. If we merged anything we need to re-loop so we can |
| 2509 | * get a good ref. |
| 2510 | * Or we can get node references of the same type that weren't |
| 2511 | * merged when created due to bumps in the tree mod seq, and |
| 2512 | * we need to merge them to prevent adding an inline extent |
| 2513 | * backref before dropping it (triggering a BUG_ON at |
| 2514 | * insert_inline_extent_backref()). |
| 2515 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2516 | spin_lock(&locked_ref->lock); |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2517 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2518 | locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2519 | |
| 2520 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2521 | * locked_ref is the head node, so we have to go one |
| 2522 | * node back for any delayed ref updates |
| 2523 | */ |
| 2524 | ref = select_delayed_ref(locked_ref); |
| 2525 | |
| 2526 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2527 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2528 | spin_unlock(&locked_ref->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2529 | spin_lock(&delayed_refs->lock); |
| 2530 | locked_ref->processing = 0; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2531 | delayed_refs->num_heads_ready++; |
| 2532 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | d028099 | 2016-12-20 13:28:28 -0500 | [diff] [blame] | 2533 | btrfs_delayed_ref_unlock(locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2534 | locked_ref = NULL; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2535 | cond_resched(); |
Josef Bacik | 27a377d | 2014-02-07 13:57:59 -0500 | [diff] [blame] | 2536 | count++; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2537 | continue; |
| 2538 | } |
| 2539 | |
| 2540 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2541 | * record the must insert reserved flag before we |
| 2542 | * drop the spin lock. |
| 2543 | */ |
| 2544 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2545 | locked_ref->must_insert_reserved = 0; |
| 2546 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2547 | extent_op = locked_ref->extent_op; |
| 2548 | locked_ref->extent_op = NULL; |
| 2549 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2550 | if (!ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2551 | |
| 2552 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2553 | /* All delayed refs have been processed, Go ahead |
| 2554 | * and send the head node to run_one_delayed_ref, |
| 2555 | * so that any accounting fixes can happen |
| 2556 | */ |
| 2557 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2558 | |
| 2559 | if (extent_op && must_insert_reserved) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2560 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2561 | extent_op = NULL; |
| 2562 | } |
| 2563 | |
| 2564 | if (extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2565 | spin_unlock(&locked_ref->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2566 | ret = run_delayed_extent_op(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2567 | ref, extent_op); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2568 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2569 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2570 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2571 | /* |
| 2572 | * Need to reset must_insert_reserved if |
| 2573 | * there was an error so the abort stuff |
| 2574 | * can cleanup the reserved space |
| 2575 | * properly. |
| 2576 | */ |
| 2577 | if (must_insert_reserved) |
| 2578 | locked_ref->must_insert_reserved = 1; |
Jeff Mahoney | aa7c8da | 2016-12-20 13:28:27 -0500 | [diff] [blame] | 2579 | spin_lock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2580 | locked_ref->processing = 0; |
Jeff Mahoney | aa7c8da | 2016-12-20 13:28:27 -0500 | [diff] [blame] | 2581 | delayed_refs->num_heads_ready++; |
| 2582 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2583 | btrfs_debug(fs_info, |
| 2584 | "run_delayed_extent_op returned %d", |
| 2585 | ret); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2586 | btrfs_delayed_ref_unlock(locked_ref); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2587 | return ret; |
| 2588 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2589 | continue; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2590 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2591 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2592 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2593 | * Need to drop our head ref lock and re-acquire the |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2594 | * delayed ref lock and then re-check to make sure |
| 2595 | * nobody got added. |
| 2596 | */ |
| 2597 | spin_unlock(&locked_ref->lock); |
| 2598 | spin_lock(&delayed_refs->lock); |
| 2599 | spin_lock(&locked_ref->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2600 | if (!list_empty(&locked_ref->ref_list) || |
Josef Bacik | 573a075 | 2014-03-27 19:41:34 -0400 | [diff] [blame] | 2601 | locked_ref->extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2602 | spin_unlock(&locked_ref->lock); |
| 2603 | spin_unlock(&delayed_refs->lock); |
| 2604 | continue; |
| 2605 | } |
| 2606 | ref->in_tree = 0; |
| 2607 | delayed_refs->num_heads--; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2608 | rb_erase(&locked_ref->href_node, |
| 2609 | &delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2610 | spin_unlock(&delayed_refs->lock); |
| 2611 | } else { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2612 | actual_count++; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2613 | ref->in_tree = 0; |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2614 | list_del(&ref->list); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2615 | if (!list_empty(&ref->add_list)) |
| 2616 | list_del(&ref->add_list); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2617 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2618 | atomic_dec(&delayed_refs->num_entries); |
| 2619 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2620 | if (!btrfs_delayed_ref_is_head(ref)) { |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2621 | /* |
| 2622 | * when we play the delayed ref, also correct the |
| 2623 | * ref_mod on head |
| 2624 | */ |
| 2625 | switch (ref->action) { |
| 2626 | case BTRFS_ADD_DELAYED_REF: |
| 2627 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2628 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2629 | break; |
| 2630 | case BTRFS_DROP_DELAYED_REF: |
| 2631 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2632 | break; |
| 2633 | default: |
| 2634 | WARN_ON(1); |
| 2635 | } |
| 2636 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2637 | spin_unlock(&locked_ref->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2638 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2639 | ret = run_one_delayed_ref(trans, fs_info, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2640 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2641 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2642 | btrfs_free_delayed_extent_op(extent_op); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2643 | if (ret) { |
Wang Xiaoguang | 9d1032c | 2016-06-20 09:18:52 +0800 | [diff] [blame] | 2644 | spin_lock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2645 | locked_ref->processing = 0; |
Wang Xiaoguang | 9d1032c | 2016-06-20 09:18:52 +0800 | [diff] [blame] | 2646 | delayed_refs->num_heads_ready++; |
| 2647 | spin_unlock(&delayed_refs->lock); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2648 | btrfs_delayed_ref_unlock(locked_ref); |
| 2649 | btrfs_put_delayed_ref(ref); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2650 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 2651 | ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2652 | return ret; |
| 2653 | } |
| 2654 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2655 | /* |
| 2656 | * If this node is a head, that means all the refs in this head |
| 2657 | * have been dealt with, and we will pick the next head to deal |
| 2658 | * with, so we must unlock the head and drop it from the cluster |
| 2659 | * list before we release it. |
| 2660 | */ |
| 2661 | if (btrfs_delayed_ref_is_head(ref)) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2662 | if (locked_ref->is_data && |
| 2663 | locked_ref->total_ref_mod < 0) { |
| 2664 | spin_lock(&delayed_refs->lock); |
| 2665 | delayed_refs->pending_csums -= ref->num_bytes; |
| 2666 | spin_unlock(&delayed_refs->lock); |
| 2667 | } |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2668 | btrfs_delayed_ref_unlock(locked_ref); |
| 2669 | locked_ref = NULL; |
| 2670 | } |
| 2671 | btrfs_put_delayed_ref(ref); |
| 2672 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2673 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2674 | } |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2675 | |
| 2676 | /* |
| 2677 | * We don't want to include ref heads since we can have empty ref heads |
| 2678 | * and those will drastically skew our runtime down since we just do |
| 2679 | * accounting, no actual extent tree updates. |
| 2680 | */ |
| 2681 | if (actual_count > 0) { |
| 2682 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2683 | u64 avg; |
| 2684 | |
| 2685 | /* |
| 2686 | * We weigh the current average higher than our current runtime |
| 2687 | * to avoid large swings in the average. |
| 2688 | */ |
| 2689 | spin_lock(&delayed_refs->lock); |
| 2690 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2691 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2692 | spin_unlock(&delayed_refs->lock); |
| 2693 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2694 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2695 | } |
| 2696 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2697 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2698 | /* |
| 2699 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2700 | * correlates in most cases to the order added. To expose dependencies on this |
| 2701 | * order, we start to process the tree in the middle instead of the beginning |
| 2702 | */ |
| 2703 | static u64 find_middle(struct rb_root *root) |
| 2704 | { |
| 2705 | struct rb_node *n = root->rb_node; |
| 2706 | struct btrfs_delayed_ref_node *entry; |
| 2707 | int alt = 1; |
| 2708 | u64 middle; |
| 2709 | u64 first = 0, last = 0; |
| 2710 | |
| 2711 | n = rb_first(root); |
| 2712 | if (n) { |
| 2713 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2714 | first = entry->bytenr; |
| 2715 | } |
| 2716 | n = rb_last(root); |
| 2717 | if (n) { |
| 2718 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2719 | last = entry->bytenr; |
| 2720 | } |
| 2721 | n = root->rb_node; |
| 2722 | |
| 2723 | while (n) { |
| 2724 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2725 | WARN_ON(!entry->in_tree); |
| 2726 | |
| 2727 | middle = entry->bytenr; |
| 2728 | |
| 2729 | if (alt) |
| 2730 | n = n->rb_left; |
| 2731 | else |
| 2732 | n = n->rb_right; |
| 2733 | |
| 2734 | alt = 1 - alt; |
| 2735 | } |
| 2736 | return middle; |
| 2737 | } |
| 2738 | #endif |
| 2739 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2740 | 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] | 2741 | { |
| 2742 | u64 num_bytes; |
| 2743 | |
| 2744 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2745 | sizeof(struct btrfs_extent_inline_ref)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2746 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2747 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2748 | |
| 2749 | /* |
| 2750 | * 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] | 2751 | * closer to what we're really going to want to use. |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2752 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2753 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2754 | } |
| 2755 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2756 | /* |
| 2757 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2758 | * would require to store the csums for that many bytes. |
| 2759 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2760 | 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] | 2761 | { |
| 2762 | u64 csum_size; |
| 2763 | u64 num_csums_per_leaf; |
| 2764 | u64 num_csums; |
| 2765 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2766 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2767 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2768 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2769 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2770 | num_csums += num_csums_per_leaf - 1; |
| 2771 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2772 | return num_csums; |
| 2773 | } |
| 2774 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2775 | int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2776 | struct btrfs_fs_info *fs_info) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2777 | { |
| 2778 | struct btrfs_block_rsv *global_rsv; |
| 2779 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2780 | u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2781 | u64 num_dirty_bgs = trans->transaction->num_dirty_bgs; |
| 2782 | u64 num_bytes, num_dirty_bgs_bytes; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2783 | int ret = 0; |
| 2784 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2785 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2786 | num_heads = heads_to_leaves(fs_info, num_heads); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2787 | if (num_heads > 1) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2788 | num_bytes += (num_heads - 1) * fs_info->nodesize; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2789 | num_bytes <<= 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2790 | num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) * |
| 2791 | fs_info->nodesize; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2792 | num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info, |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2793 | num_dirty_bgs); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2794 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2795 | |
| 2796 | /* |
| 2797 | * If we can't allocate any more chunks lets make sure we have _lots_ of |
| 2798 | * wiggle room since running delayed refs can create more delayed refs. |
| 2799 | */ |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2800 | if (global_rsv->space_info->full) { |
| 2801 | num_dirty_bgs_bytes <<= 1; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2802 | num_bytes <<= 1; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2803 | } |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2804 | |
| 2805 | spin_lock(&global_rsv->lock); |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2806 | if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2807 | ret = 1; |
| 2808 | spin_unlock(&global_rsv->lock); |
| 2809 | return ret; |
| 2810 | } |
| 2811 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2812 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2813 | struct btrfs_fs_info *fs_info) |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2814 | { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2815 | u64 num_entries = |
| 2816 | atomic_read(&trans->transaction->delayed_refs.num_entries); |
| 2817 | u64 avg_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2818 | u64 val; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2819 | |
| 2820 | smp_mb(); |
| 2821 | avg_runtime = fs_info->avg_delayed_ref_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2822 | val = num_entries * avg_runtime; |
Wang Xiaoguang | dc1a90c | 2016-10-26 15:23:01 +0800 | [diff] [blame] | 2823 | if (val >= NSEC_PER_SEC) |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2824 | return 1; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2825 | if (val >= NSEC_PER_SEC / 2) |
| 2826 | return 2; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2827 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2828 | return btrfs_check_space_for_delayed_refs(trans, fs_info); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2829 | } |
| 2830 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2831 | struct async_delayed_refs { |
| 2832 | struct btrfs_root *root; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2833 | u64 transid; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2834 | int count; |
| 2835 | int error; |
| 2836 | int sync; |
| 2837 | struct completion wait; |
| 2838 | struct btrfs_work work; |
| 2839 | }; |
| 2840 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2841 | static inline struct async_delayed_refs * |
| 2842 | to_async_delayed_refs(struct btrfs_work *work) |
| 2843 | { |
| 2844 | return container_of(work, struct async_delayed_refs, work); |
| 2845 | } |
| 2846 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2847 | static void delayed_ref_async_start(struct btrfs_work *work) |
| 2848 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2849 | struct async_delayed_refs *async = to_async_delayed_refs(work); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2850 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2851 | struct btrfs_fs_info *fs_info = async->root->fs_info; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2852 | int ret; |
| 2853 | |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2854 | /* 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] | 2855 | if (btrfs_transaction_blocked(fs_info)) |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2856 | goto done; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2857 | |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2858 | trans = btrfs_join_transaction(async->root); |
| 2859 | if (IS_ERR(trans)) { |
| 2860 | async->error = PTR_ERR(trans); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2861 | goto done; |
| 2862 | } |
| 2863 | |
| 2864 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2865 | * trans->sync means that when we call end_transaction, we won't |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2866 | * wait on delayed refs |
| 2867 | */ |
| 2868 | trans->sync = true; |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2869 | |
| 2870 | /* Don't bother flushing if we got into a different transaction */ |
| 2871 | if (trans->transid > async->transid) |
| 2872 | goto end; |
| 2873 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2874 | ret = btrfs_run_delayed_refs(trans, fs_info, async->count); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2875 | if (ret) |
| 2876 | async->error = ret; |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2877 | end: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2878 | ret = btrfs_end_transaction(trans); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2879 | if (ret && !async->error) |
| 2880 | async->error = ret; |
| 2881 | done: |
| 2882 | if (async->sync) |
| 2883 | complete(&async->wait); |
| 2884 | else |
| 2885 | kfree(async); |
| 2886 | } |
| 2887 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2888 | int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info, |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2889 | unsigned long count, u64 transid, int wait) |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2890 | { |
| 2891 | struct async_delayed_refs *async; |
| 2892 | int ret; |
| 2893 | |
| 2894 | async = kmalloc(sizeof(*async), GFP_NOFS); |
| 2895 | if (!async) |
| 2896 | return -ENOMEM; |
| 2897 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2898 | async->root = fs_info->tree_root; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2899 | async->count = count; |
| 2900 | async->error = 0; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2901 | async->transid = transid; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2902 | if (wait) |
| 2903 | async->sync = 1; |
| 2904 | else |
| 2905 | async->sync = 0; |
| 2906 | init_completion(&async->wait); |
| 2907 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2908 | btrfs_init_work(&async->work, btrfs_extent_refs_helper, |
| 2909 | delayed_ref_async_start, NULL, NULL); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2910 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2911 | btrfs_queue_work(fs_info->extent_workers, &async->work); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2912 | |
| 2913 | if (wait) { |
| 2914 | wait_for_completion(&async->wait); |
| 2915 | ret = async->error; |
| 2916 | kfree(async); |
| 2917 | return ret; |
| 2918 | } |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2922 | /* |
| 2923 | * this starts processing the delayed reference count updates and |
| 2924 | * extent insertions we have queued up so far. count can be |
| 2925 | * 0, which means to process everything in the tree at the start |
| 2926 | * of the run (but not newly added entries), or it can be some target |
| 2927 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2928 | * |
| 2929 | * Returns 0 on success or if called with an aborted transaction |
| 2930 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2931 | */ |
| 2932 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2933 | struct btrfs_fs_info *fs_info, unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2934 | { |
| 2935 | struct rb_node *node; |
| 2936 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2937 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2938 | int ret; |
| 2939 | int run_all = count == (unsigned long)-1; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2940 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2941 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2942 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2943 | if (trans->aborted) |
| 2944 | return 0; |
| 2945 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2946 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2947 | return 0; |
| 2948 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2949 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2950 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2951 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2952 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2953 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2954 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2955 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2956 | #endif |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2957 | trans->can_flush_pending_bgs = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2958 | ret = __btrfs_run_delayed_refs(trans, fs_info, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2959 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2960 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2961 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2962 | } |
| 2963 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2964 | if (run_all) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2965 | if (!list_empty(&trans->new_bgs)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2966 | btrfs_create_pending_block_groups(trans, fs_info); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2967 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2968 | spin_lock(&delayed_refs->lock); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2969 | node = rb_first(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2970 | if (!node) { |
| 2971 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2972 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2973 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2974 | |
| 2975 | while (node) { |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2976 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2977 | href_node); |
| 2978 | if (btrfs_delayed_ref_is_head(&head->node)) { |
| 2979 | struct btrfs_delayed_ref_node *ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2980 | |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2981 | ref = &head->node; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2982 | atomic_inc(&ref->refs); |
| 2983 | |
| 2984 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2985 | /* |
| 2986 | * Mutex was contended, block until it's |
| 2987 | * released and try again |
| 2988 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2989 | mutex_lock(&head->mutex); |
| 2990 | mutex_unlock(&head->mutex); |
| 2991 | |
| 2992 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2993 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2994 | goto again; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2995 | } else { |
| 2996 | WARN_ON(1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2997 | } |
| 2998 | node = rb_next(node); |
| 2999 | } |
| 3000 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3001 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3002 | goto again; |
| 3003 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3004 | out: |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 3005 | assert_qgroups_uptodate(trans); |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 3006 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3007 | return 0; |
| 3008 | } |
| 3009 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3010 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3011 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3012 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 3013 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3014 | { |
| 3015 | struct btrfs_delayed_extent_op *extent_op; |
| 3016 | int ret; |
| 3017 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 3018 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3019 | if (!extent_op) |
| 3020 | return -ENOMEM; |
| 3021 | |
| 3022 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 3023 | extent_op->update_flags = true; |
| 3024 | extent_op->update_key = false; |
| 3025 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 3026 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3027 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3028 | ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3029 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3030 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 3031 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3032 | return ret; |
| 3033 | } |
| 3034 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3035 | static noinline int check_delayed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3036 | struct btrfs_path *path, |
| 3037 | u64 objectid, u64 offset, u64 bytenr) |
| 3038 | { |
| 3039 | struct btrfs_delayed_ref_head *head; |
| 3040 | struct btrfs_delayed_ref_node *ref; |
| 3041 | struct btrfs_delayed_data_ref *data_ref; |
| 3042 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3043 | struct btrfs_transaction *cur_trans; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3044 | int ret = 0; |
| 3045 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3046 | cur_trans = root->fs_info->running_transaction; |
| 3047 | if (!cur_trans) |
| 3048 | return 0; |
| 3049 | |
| 3050 | delayed_refs = &cur_trans->delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3051 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 3052 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3053 | if (!head) { |
| 3054 | spin_unlock(&delayed_refs->lock); |
| 3055 | return 0; |
| 3056 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3057 | |
| 3058 | if (!mutex_trylock(&head->mutex)) { |
| 3059 | atomic_inc(&head->node.refs); |
| 3060 | spin_unlock(&delayed_refs->lock); |
| 3061 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3062 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3063 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 3064 | /* |
| 3065 | * Mutex was contended, block until it's released and let |
| 3066 | * caller try again |
| 3067 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3068 | mutex_lock(&head->mutex); |
| 3069 | mutex_unlock(&head->mutex); |
| 3070 | btrfs_put_delayed_ref(&head->node); |
| 3071 | return -EAGAIN; |
| 3072 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3073 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3074 | |
| 3075 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 3076 | list_for_each_entry(ref, &head->ref_list, list) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3077 | /* If it's a shared ref we know a cross reference exists */ |
| 3078 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 3079 | ret = 1; |
| 3080 | break; |
| 3081 | } |
| 3082 | |
| 3083 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 3084 | |
| 3085 | /* |
| 3086 | * If our ref doesn't match the one we're currently looking at |
| 3087 | * then we have a cross reference. |
| 3088 | */ |
| 3089 | if (data_ref->root != root->root_key.objectid || |
| 3090 | data_ref->objectid != objectid || |
| 3091 | data_ref->offset != offset) { |
| 3092 | ret = 1; |
| 3093 | break; |
| 3094 | } |
| 3095 | } |
| 3096 | spin_unlock(&head->lock); |
| 3097 | mutex_unlock(&head->mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3098 | return ret; |
| 3099 | } |
| 3100 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3101 | static noinline int check_committed_ref(struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3102 | struct btrfs_path *path, |
| 3103 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3104 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3105 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3106 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3107 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3108 | struct btrfs_extent_data_ref *ref; |
| 3109 | struct btrfs_extent_inline_ref *iref; |
| 3110 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3111 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3112 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3113 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3114 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3115 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3116 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3117 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3118 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3119 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 3120 | if (ret < 0) |
| 3121 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3122 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 3123 | |
| 3124 | ret = -ENOENT; |
| 3125 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3126 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3127 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3128 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3129 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3130 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3131 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3132 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3133 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3134 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3135 | ret = 1; |
| 3136 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 3137 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3138 | if (item_size < sizeof(*ei)) { |
| 3139 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 3140 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3141 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3142 | #endif |
| 3143 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 3144 | |
| 3145 | if (item_size != sizeof(*ei) + |
| 3146 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 3147 | goto out; |
| 3148 | |
| 3149 | if (btrfs_extent_generation(leaf, ei) <= |
| 3150 | btrfs_root_last_snapshot(&root->root_item)) |
| 3151 | goto out; |
| 3152 | |
| 3153 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 3154 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 3155 | BTRFS_EXTENT_DATA_REF_KEY) |
| 3156 | goto out; |
| 3157 | |
| 3158 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3159 | if (btrfs_extent_refs(leaf, ei) != |
| 3160 | btrfs_extent_data_ref_count(leaf, ref) || |
| 3161 | btrfs_extent_data_ref_root(leaf, ref) != |
| 3162 | root->root_key.objectid || |
| 3163 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 3164 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 3165 | goto out; |
| 3166 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3167 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3168 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3169 | return ret; |
| 3170 | } |
| 3171 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3172 | int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset, |
| 3173 | u64 bytenr) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3174 | { |
| 3175 | struct btrfs_path *path; |
| 3176 | int ret; |
| 3177 | int ret2; |
| 3178 | |
| 3179 | path = btrfs_alloc_path(); |
| 3180 | if (!path) |
| 3181 | return -ENOENT; |
| 3182 | |
| 3183 | do { |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3184 | ret = check_committed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3185 | offset, bytenr); |
| 3186 | if (ret && ret != -ENOENT) |
| 3187 | goto out; |
| 3188 | |
Liu Bo | e4c3b2d | 2017-01-30 12:25:28 -0800 | [diff] [blame] | 3189 | ret2 = check_delayed_ref(root, path, objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3190 | offset, bytenr); |
| 3191 | } while (ret2 == -EAGAIN); |
| 3192 | |
| 3193 | if (ret2 && ret2 != -ENOENT) { |
| 3194 | ret = ret2; |
| 3195 | goto out; |
| 3196 | } |
| 3197 | |
| 3198 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 3199 | ret = 0; |
| 3200 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3201 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3202 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 3203 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3204 | return ret; |
| 3205 | } |
| 3206 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3207 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3208 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3209 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3210 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3211 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3212 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3213 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3214 | u64 num_bytes; |
| 3215 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3216 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3217 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3218 | struct btrfs_key key; |
| 3219 | struct btrfs_file_extent_item *fi; |
| 3220 | int i; |
| 3221 | int level; |
| 3222 | int ret = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3223 | int (*process_func)(struct btrfs_trans_handle *, |
| 3224 | struct btrfs_fs_info *, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3225 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3226 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3227 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3228 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3229 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3230 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3231 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3232 | nritems = btrfs_header_nritems(buf); |
| 3233 | level = btrfs_header_level(buf); |
| 3234 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3235 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3236 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3237 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3238 | if (inc) |
| 3239 | process_func = btrfs_inc_extent_ref; |
| 3240 | else |
| 3241 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3242 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3243 | if (full_backref) |
| 3244 | parent = buf->start; |
| 3245 | else |
| 3246 | parent = 0; |
| 3247 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3248 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3249 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3250 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3251 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3252 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3253 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3254 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3255 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3256 | BTRFS_FILE_EXTENT_INLINE) |
| 3257 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3258 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 3259 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3260 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3261 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3262 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 3263 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3264 | ret = process_func(trans, fs_info, bytenr, num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3265 | parent, ref_root, key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3266 | key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3267 | if (ret) |
| 3268 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3269 | } else { |
| 3270 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3271 | num_bytes = fs_info->nodesize; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3272 | ret = process_func(trans, fs_info, bytenr, num_bytes, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3273 | parent, ref_root, level - 1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3274 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3275 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3276 | } |
| 3277 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3278 | return 0; |
| 3279 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3280 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3281 | } |
| 3282 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3283 | 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] | 3284 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3285 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3286 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3287 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3288 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3289 | 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] | 3290 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3291 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3292 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3293 | } |
| 3294 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3295 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3296 | struct btrfs_fs_info *fs_info, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3297 | struct btrfs_path *path, |
| 3298 | struct btrfs_block_group_cache *cache) |
| 3299 | { |
| 3300 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3301 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3302 | unsigned long bi; |
| 3303 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3304 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3305 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3306 | if (ret) { |
| 3307 | if (ret > 0) |
| 3308 | ret = -ENOENT; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3309 | goto fail; |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3310 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3311 | |
| 3312 | leaf = path->nodes[0]; |
| 3313 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3314 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3315 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3316 | fail: |
Filipe Manana | 24b89d0 | 2015-04-25 18:31:05 +0100 | [diff] [blame] | 3317 | btrfs_release_path(path); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3318 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3319 | |
| 3320 | } |
| 3321 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3322 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3323 | next_block_group(struct btrfs_fs_info *fs_info, |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3324 | struct btrfs_block_group_cache *cache) |
| 3325 | { |
| 3326 | struct rb_node *node; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3327 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3328 | spin_lock(&fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3329 | |
| 3330 | /* If our block group was removed, we need a full search. */ |
| 3331 | if (RB_EMPTY_NODE(&cache->cache_node)) { |
| 3332 | const u64 next_bytenr = cache->key.objectid + cache->key.offset; |
| 3333 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3334 | spin_unlock(&fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3335 | btrfs_put_block_group(cache); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3336 | cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3337 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3338 | node = rb_next(&cache->cache_node); |
| 3339 | btrfs_put_block_group(cache); |
| 3340 | if (node) { |
| 3341 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 3342 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 3343 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3344 | } else |
| 3345 | cache = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3346 | spin_unlock(&fs_info->block_group_cache_lock); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3347 | return cache; |
| 3348 | } |
| 3349 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3350 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3351 | struct btrfs_trans_handle *trans, |
| 3352 | struct btrfs_path *path) |
| 3353 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3354 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 3355 | struct btrfs_root *root = fs_info->tree_root; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3356 | struct inode *inode = NULL; |
| 3357 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3358 | int dcs = BTRFS_DC_ERROR; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 3359 | u64 num_pages = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3360 | int retries = 0; |
| 3361 | int ret = 0; |
| 3362 | |
| 3363 | /* |
| 3364 | * If this block group is smaller than 100 megs don't bother caching the |
| 3365 | * block group. |
| 3366 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3367 | if (block_group->key.offset < (100 * SZ_1M)) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3368 | spin_lock(&block_group->lock); |
| 3369 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3370 | spin_unlock(&block_group->lock); |
| 3371 | return 0; |
| 3372 | } |
| 3373 | |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3374 | if (trans->aborted) |
| 3375 | return 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3376 | again: |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3377 | inode = lookup_free_space_inode(fs_info, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3378 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3379 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3380 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3381 | goto out; |
| 3382 | } |
| 3383 | |
| 3384 | if (IS_ERR(inode)) { |
| 3385 | BUG_ON(retries); |
| 3386 | retries++; |
| 3387 | |
| 3388 | if (block_group->ro) |
| 3389 | goto out_free; |
| 3390 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3391 | ret = create_free_space_inode(fs_info, trans, block_group, |
| 3392 | path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3393 | if (ret) |
| 3394 | goto out_free; |
| 3395 | goto again; |
| 3396 | } |
| 3397 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3398 | /* We've already setup this transaction, go ahead and exit */ |
| 3399 | if (block_group->cache_generation == trans->transid && |
| 3400 | i_size_read(inode)) { |
| 3401 | dcs = BTRFS_DC_SETUP; |
| 3402 | goto out_put; |
| 3403 | } |
| 3404 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3405 | /* |
| 3406 | * We want to set the generation to 0, that way if anything goes wrong |
| 3407 | * from here on out we know not to trust this cache when we load up next |
| 3408 | * time. |
| 3409 | */ |
| 3410 | BTRFS_I(inode)->generation = 0; |
| 3411 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3412 | if (ret) { |
| 3413 | /* |
| 3414 | * So theoretically we could recover from this, simply set the |
| 3415 | * super cache generation to 0 so we know to invalidate the |
| 3416 | * cache, but then we'd have to keep track of the block groups |
| 3417 | * that fail this way so we know we _have_ to reset this cache |
| 3418 | * before the next commit or risk reading stale cache. So to |
| 3419 | * limit our exposure to horrible edge cases lets just abort the |
| 3420 | * transaction, this only happens in really bad situations |
| 3421 | * anyway. |
| 3422 | */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3423 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3424 | goto out_put; |
| 3425 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3426 | WARN_ON(ret); |
| 3427 | |
| 3428 | if (i_size_read(inode) > 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3429 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3430 | &fs_info->global_block_rsv); |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3431 | if (ret) |
| 3432 | goto out_put; |
| 3433 | |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 3434 | ret = btrfs_truncate_free_space_cache(trans, NULL, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3435 | if (ret) |
| 3436 | goto out_put; |
| 3437 | } |
| 3438 | |
| 3439 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3440 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3441 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3442 | /* |
| 3443 | * don't bother trying to write stuff out _if_ |
| 3444 | * a) we're not cached, |
| 3445 | * b) we're with nospace_cache mount option. |
| 3446 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3447 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3448 | spin_unlock(&block_group->lock); |
| 3449 | goto out_put; |
| 3450 | } |
| 3451 | spin_unlock(&block_group->lock); |
| 3452 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3453 | /* |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3454 | * We hit an ENOSPC when setting up the cache in this transaction, just |
| 3455 | * skip doing the setup, we've already cleared the cache so we're safe. |
| 3456 | */ |
| 3457 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { |
| 3458 | ret = -ENOSPC; |
| 3459 | goto out_put; |
| 3460 | } |
| 3461 | |
| 3462 | /* |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3463 | * Try to preallocate enough space based on how big the block group is. |
| 3464 | * Keep in mind this has to include any pinned space which could end up |
| 3465 | * taking up quite a bit since it's not folded into the other space |
| 3466 | * cache. |
| 3467 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3468 | num_pages = div_u64(block_group->key.offset, SZ_256M); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3469 | if (!num_pages) |
| 3470 | num_pages = 1; |
| 3471 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3472 | num_pages *= 16; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3473 | num_pages *= PAGE_SIZE; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3474 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 3475 | ret = btrfs_check_data_free_space(inode, 0, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3476 | if (ret) |
| 3477 | goto out_put; |
| 3478 | |
| 3479 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3480 | num_pages, num_pages, |
| 3481 | &alloc_hint); |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3482 | /* |
| 3483 | * Our cache requires contiguous chunks so that we don't modify a bunch |
| 3484 | * of metadata or split extents when writing the cache out, which means |
| 3485 | * we can enospc if we are heavily fragmented in addition to just normal |
| 3486 | * out of space conditions. So if we hit this just skip setting up any |
| 3487 | * other block groups for this transaction, maybe we'll unpin enough |
| 3488 | * space the next time around. |
| 3489 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3490 | if (!ret) |
| 3491 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3492 | else if (ret == -ENOSPC) |
| 3493 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3494 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3495 | out_put: |
| 3496 | iput(inode); |
| 3497 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3498 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3499 | out: |
| 3500 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3501 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3502 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3503 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3504 | spin_unlock(&block_group->lock); |
| 3505 | |
| 3506 | return ret; |
| 3507 | } |
| 3508 | |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3509 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3510 | struct btrfs_fs_info *fs_info) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3511 | { |
| 3512 | struct btrfs_block_group_cache *cache, *tmp; |
| 3513 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3514 | struct btrfs_path *path; |
| 3515 | |
| 3516 | if (list_empty(&cur_trans->dirty_bgs) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3517 | !btrfs_test_opt(fs_info, SPACE_CACHE)) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3518 | return 0; |
| 3519 | |
| 3520 | path = btrfs_alloc_path(); |
| 3521 | if (!path) |
| 3522 | return -ENOMEM; |
| 3523 | |
| 3524 | /* Could add new block groups, use _safe just in case */ |
| 3525 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, |
| 3526 | dirty_list) { |
| 3527 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3528 | cache_save_setup(cache, trans, path); |
| 3529 | } |
| 3530 | |
| 3531 | btrfs_free_path(path); |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3535 | /* |
| 3536 | * transaction commit does final block group cache writeback during a |
| 3537 | * critical section where nothing is allowed to change the FS. This is |
| 3538 | * required in order for the cache to actually match the block group, |
| 3539 | * but can introduce a lot of latency into the commit. |
| 3540 | * |
| 3541 | * So, btrfs_start_dirty_block_groups is here to kick off block group |
| 3542 | * cache IO. There's a chance we'll have to redo some of it if the |
| 3543 | * block group changes again during the commit, but it greatly reduces |
| 3544 | * the commit latency by getting rid of the easy block groups while |
| 3545 | * we're still allowing others to join the commit. |
| 3546 | */ |
| 3547 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3548 | struct btrfs_fs_info *fs_info) |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3549 | { |
| 3550 | struct btrfs_block_group_cache *cache; |
| 3551 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3552 | int ret = 0; |
| 3553 | int should_put; |
| 3554 | struct btrfs_path *path = NULL; |
| 3555 | LIST_HEAD(dirty); |
| 3556 | struct list_head *io = &cur_trans->io_bgs; |
| 3557 | int num_started = 0; |
| 3558 | int loops = 0; |
| 3559 | |
| 3560 | spin_lock(&cur_trans->dirty_bgs_lock); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3561 | if (list_empty(&cur_trans->dirty_bgs)) { |
| 3562 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3563 | return 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3564 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3565 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3566 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3567 | |
| 3568 | again: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3569 | /* |
| 3570 | * make sure all the block groups on our dirty list actually |
| 3571 | * exist |
| 3572 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3573 | btrfs_create_pending_block_groups(trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3574 | |
| 3575 | if (!path) { |
| 3576 | path = btrfs_alloc_path(); |
| 3577 | if (!path) |
| 3578 | return -ENOMEM; |
| 3579 | } |
| 3580 | |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3581 | /* |
| 3582 | * cache_write_mutex is here only to save us from balance or automatic |
| 3583 | * removal of empty block groups deleting this block group while we are |
| 3584 | * writing out the cache |
| 3585 | */ |
| 3586 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3587 | while (!list_empty(&dirty)) { |
| 3588 | cache = list_first_entry(&dirty, |
| 3589 | struct btrfs_block_group_cache, |
| 3590 | dirty_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3591 | /* |
| 3592 | * this can happen if something re-dirties a block |
| 3593 | * group that is already under IO. Just wait for it to |
| 3594 | * finish and then do it all again |
| 3595 | */ |
| 3596 | if (!list_empty(&cache->io_list)) { |
| 3597 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3598 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3599 | btrfs_put_block_group(cache); |
| 3600 | } |
| 3601 | |
| 3602 | |
| 3603 | /* |
| 3604 | * btrfs_wait_cache_io uses the cache->dirty_list to decide |
| 3605 | * if it should update the cache_state. Don't delete |
| 3606 | * until after we wait. |
| 3607 | * |
| 3608 | * Since we're not running in the commit critical section |
| 3609 | * we need the dirty_bgs_lock to protect from update_block_group |
| 3610 | */ |
| 3611 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3612 | list_del_init(&cache->dirty_list); |
| 3613 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3614 | |
| 3615 | should_put = 1; |
| 3616 | |
| 3617 | cache_save_setup(cache, trans, path); |
| 3618 | |
| 3619 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3620 | cache->io_ctl.inode = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3621 | ret = btrfs_write_out_cache(fs_info, trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3622 | cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3623 | if (ret == 0 && cache->io_ctl.inode) { |
| 3624 | num_started++; |
| 3625 | should_put = 0; |
| 3626 | |
| 3627 | /* |
| 3628 | * the cache_write_mutex is protecting |
| 3629 | * the io_list |
| 3630 | */ |
| 3631 | list_add_tail(&cache->io_list, io); |
| 3632 | } else { |
| 3633 | /* |
| 3634 | * if we failed to write the cache, the |
| 3635 | * generation will be bad and life goes on |
| 3636 | */ |
| 3637 | ret = 0; |
| 3638 | } |
| 3639 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3640 | if (!ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3641 | ret = write_one_cache_group(trans, fs_info, |
| 3642 | path, cache); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3643 | /* |
| 3644 | * Our block group might still be attached to the list |
| 3645 | * of new block groups in the transaction handle of some |
| 3646 | * other task (struct btrfs_trans_handle->new_bgs). This |
| 3647 | * means its block group item isn't yet in the extent |
| 3648 | * tree. If this happens ignore the error, as we will |
| 3649 | * try again later in the critical section of the |
| 3650 | * transaction commit. |
| 3651 | */ |
| 3652 | if (ret == -ENOENT) { |
| 3653 | ret = 0; |
| 3654 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3655 | if (list_empty(&cache->dirty_list)) { |
| 3656 | list_add_tail(&cache->dirty_list, |
| 3657 | &cur_trans->dirty_bgs); |
| 3658 | btrfs_get_block_group(cache); |
| 3659 | } |
| 3660 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3661 | } else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3662 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3663 | } |
| 3664 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3665 | |
| 3666 | /* if its not on the io list, we need to put the block group */ |
| 3667 | if (should_put) |
| 3668 | btrfs_put_block_group(cache); |
| 3669 | |
| 3670 | if (ret) |
| 3671 | break; |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3672 | |
| 3673 | /* |
| 3674 | * Avoid blocking other tasks for too long. It might even save |
| 3675 | * us from writing caches for block groups that are going to be |
| 3676 | * removed. |
| 3677 | */ |
| 3678 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 3679 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3680 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3681 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3682 | |
| 3683 | /* |
| 3684 | * go through delayed refs for all the stuff we've just kicked off |
| 3685 | * and then loop back (just once) |
| 3686 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3687 | ret = btrfs_run_delayed_refs(trans, fs_info, 0); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3688 | if (!ret && loops == 0) { |
| 3689 | loops++; |
| 3690 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3691 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3692 | /* |
| 3693 | * dirty_bgs_lock protects us from concurrent block group |
| 3694 | * deletes too (not just cache_write_mutex). |
| 3695 | */ |
| 3696 | if (!list_empty(&dirty)) { |
| 3697 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3698 | goto again; |
| 3699 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3700 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Liu Bo | c79a175 | 2016-07-20 17:44:12 -0700 | [diff] [blame] | 3701 | } else if (ret < 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3702 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | btrfs_free_path(path); |
| 3706 | return ret; |
| 3707 | } |
| 3708 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3709 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3710 | struct btrfs_fs_info *fs_info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3711 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3712 | struct btrfs_block_group_cache *cache; |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3713 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3714 | int ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3715 | int should_put; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3716 | struct btrfs_path *path; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3717 | struct list_head *io = &cur_trans->io_bgs; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3718 | int num_started = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3719 | |
| 3720 | path = btrfs_alloc_path(); |
| 3721 | if (!path) |
| 3722 | return -ENOMEM; |
| 3723 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3724 | /* |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3725 | * Even though we are in the critical section of the transaction commit, |
| 3726 | * we can still have concurrent tasks adding elements to this |
| 3727 | * transaction's list of dirty block groups. These tasks correspond to |
| 3728 | * endio free space workers started when writeback finishes for a |
| 3729 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can |
| 3730 | * allocate new block groups as a result of COWing nodes of the root |
| 3731 | * tree when updating the free space inode. The writeback for the space |
| 3732 | * caches is triggered by an earlier call to |
| 3733 | * btrfs_start_dirty_block_groups() and iterations of the following |
| 3734 | * loop. |
| 3735 | * 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] | 3736 | * delayed refs to make sure we have the best chance at doing this all |
| 3737 | * in one shot. |
| 3738 | */ |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3739 | spin_lock(&cur_trans->dirty_bgs_lock); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3740 | while (!list_empty(&cur_trans->dirty_bgs)) { |
| 3741 | cache = list_first_entry(&cur_trans->dirty_bgs, |
| 3742 | struct btrfs_block_group_cache, |
| 3743 | dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3744 | |
| 3745 | /* |
| 3746 | * this can happen if cache_save_setup re-dirties a block |
| 3747 | * group that is already under IO. Just wait for it to |
| 3748 | * finish and then do it all again |
| 3749 | */ |
| 3750 | if (!list_empty(&cache->io_list)) { |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3751 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3752 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3753 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3754 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3755 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3758 | /* |
| 3759 | * don't remove from the dirty list until after we've waited |
| 3760 | * on any pending IO |
| 3761 | */ |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3762 | list_del_init(&cache->dirty_list); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3763 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3764 | should_put = 1; |
| 3765 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3766 | cache_save_setup(cache, trans, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3767 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3768 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3769 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 3770 | (unsigned long) -1); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3771 | |
| 3772 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3773 | cache->io_ctl.inode = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3774 | ret = btrfs_write_out_cache(fs_info, trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3775 | cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3776 | if (ret == 0 && cache->io_ctl.inode) { |
| 3777 | num_started++; |
| 3778 | should_put = 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3779 | list_add_tail(&cache->io_list, io); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3780 | } else { |
| 3781 | /* |
| 3782 | * if we failed to write the cache, the |
| 3783 | * generation will be bad and life goes on |
| 3784 | */ |
| 3785 | ret = 0; |
| 3786 | } |
| 3787 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3788 | if (!ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3789 | ret = write_one_cache_group(trans, fs_info, |
| 3790 | path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3791 | /* |
| 3792 | * One of the free space endio workers might have |
| 3793 | * created a new block group while updating a free space |
| 3794 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) |
| 3795 | * and hasn't released its transaction handle yet, in |
| 3796 | * which case the new block group is still attached to |
| 3797 | * its transaction handle and its creation has not |
| 3798 | * finished yet (no block group item in the extent tree |
| 3799 | * yet, etc). If this is the case, wait for all free |
| 3800 | * space endio workers to finish and retry. This is a |
| 3801 | * a very rare case so no need for a more efficient and |
| 3802 | * complex approach. |
| 3803 | */ |
| 3804 | if (ret == -ENOENT) { |
| 3805 | wait_event(cur_trans->writer_wait, |
| 3806 | atomic_read(&cur_trans->num_writers) == 1); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3807 | ret = write_one_cache_group(trans, fs_info, |
| 3808 | path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3809 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3810 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3811 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3812 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3813 | |
| 3814 | /* if its not on the io list, we need to put the block group */ |
| 3815 | if (should_put) |
| 3816 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3817 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3818 | } |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3819 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3820 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3821 | while (!list_empty(io)) { |
| 3822 | cache = list_first_entry(io, struct btrfs_block_group_cache, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3823 | io_list); |
| 3824 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3825 | btrfs_wait_cache_io(trans, cache, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3826 | btrfs_put_block_group(cache); |
| 3827 | } |
| 3828 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3829 | btrfs_free_path(path); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3830 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3831 | } |
| 3832 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3833 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3834 | { |
| 3835 | struct btrfs_block_group_cache *block_group; |
| 3836 | int readonly = 0; |
| 3837 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3838 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3839 | if (!block_group || block_group->ro) |
| 3840 | readonly = 1; |
| 3841 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3842 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3843 | return readonly; |
| 3844 | } |
| 3845 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 3846 | bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 3847 | { |
| 3848 | struct btrfs_block_group_cache *bg; |
| 3849 | bool ret = true; |
| 3850 | |
| 3851 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 3852 | if (!bg) |
| 3853 | return false; |
| 3854 | |
| 3855 | spin_lock(&bg->lock); |
| 3856 | if (bg->ro) |
| 3857 | ret = false; |
| 3858 | else |
| 3859 | atomic_inc(&bg->nocow_writers); |
| 3860 | spin_unlock(&bg->lock); |
| 3861 | |
| 3862 | /* no put on block group, done by btrfs_dec_nocow_writers */ |
| 3863 | if (!ret) |
| 3864 | btrfs_put_block_group(bg); |
| 3865 | |
| 3866 | return ret; |
| 3867 | |
| 3868 | } |
| 3869 | |
| 3870 | void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 3871 | { |
| 3872 | struct btrfs_block_group_cache *bg; |
| 3873 | |
| 3874 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 3875 | ASSERT(bg); |
| 3876 | if (atomic_dec_and_test(&bg->nocow_writers)) |
| 3877 | wake_up_atomic_t(&bg->nocow_writers); |
| 3878 | /* |
| 3879 | * Once for our lookup and once for the lookup done by a previous call |
| 3880 | * to btrfs_inc_nocow_writers() |
| 3881 | */ |
| 3882 | btrfs_put_block_group(bg); |
| 3883 | btrfs_put_block_group(bg); |
| 3884 | } |
| 3885 | |
| 3886 | static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a) |
| 3887 | { |
| 3888 | schedule(); |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
| 3892 | void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg) |
| 3893 | { |
| 3894 | wait_on_atomic_t(&bg->nocow_writers, |
| 3895 | btrfs_wait_nocow_writers_atomic_t, |
| 3896 | TASK_UNINTERRUPTIBLE); |
| 3897 | } |
| 3898 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3899 | static const char *alloc_name(u64 flags) |
| 3900 | { |
| 3901 | switch (flags) { |
| 3902 | case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA: |
| 3903 | return "mixed"; |
| 3904 | case BTRFS_BLOCK_GROUP_METADATA: |
| 3905 | return "metadata"; |
| 3906 | case BTRFS_BLOCK_GROUP_DATA: |
| 3907 | return "data"; |
| 3908 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 3909 | return "system"; |
| 3910 | default: |
| 3911 | WARN_ON(1); |
| 3912 | return "invalid-combination"; |
| 3913 | }; |
| 3914 | } |
| 3915 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3916 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3917 | u64 total_bytes, u64 bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3918 | u64 bytes_readonly, |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3919 | struct btrfs_space_info **space_info) |
| 3920 | { |
| 3921 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3922 | int i; |
| 3923 | int factor; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3924 | int ret; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3925 | |
| 3926 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3927 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3928 | factor = 2; |
| 3929 | else |
| 3930 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3931 | |
| 3932 | found = __find_space_info(info, flags); |
| 3933 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3934 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3935 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3936 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3937 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3938 | found->disk_used += bytes_used * factor; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3939 | found->bytes_readonly += bytes_readonly; |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 3940 | if (total_bytes > 0) |
| 3941 | found->full = 0; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 3942 | space_info_add_new_bytes(info, found, total_bytes - |
| 3943 | bytes_used - bytes_readonly); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3944 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3945 | *space_info = found; |
| 3946 | return 0; |
| 3947 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3948 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3949 | if (!found) |
| 3950 | return -ENOMEM; |
| 3951 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3952 | ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3953 | if (ret) { |
| 3954 | kfree(found); |
| 3955 | return ret; |
| 3956 | } |
| 3957 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 3958 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3959 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3960 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3961 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3962 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3963 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3964 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3965 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3966 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3967 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3968 | found->bytes_reserved = 0; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3969 | found->bytes_readonly = bytes_readonly; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3970 | found->bytes_may_use = 0; |
Filipe Manana | 6af3e3a | 2015-09-07 10:41:12 +0100 | [diff] [blame] | 3971 | found->full = 0; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3972 | found->max_extent_size = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3973 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3974 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3975 | found->flush = 0; |
| 3976 | init_waitqueue_head(&found->wait); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 3977 | INIT_LIST_HEAD(&found->ro_bgs); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 3978 | INIT_LIST_HEAD(&found->tickets); |
| 3979 | INIT_LIST_HEAD(&found->priority_tickets); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3980 | |
| 3981 | ret = kobject_init_and_add(&found->kobj, &space_info_ktype, |
| 3982 | info->space_info_kobj, "%s", |
| 3983 | alloc_name(found->flags)); |
| 3984 | if (ret) { |
| 3985 | kfree(found); |
| 3986 | return ret; |
| 3987 | } |
| 3988 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3989 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3990 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3991 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3992 | info->data_sinfo = found; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3993 | |
| 3994 | return ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3995 | } |
| 3996 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3997 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3998 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3999 | u64 extra_flags = chunk_to_extended(flags) & |
| 4000 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4001 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4002 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4003 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 4004 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 4005 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 4006 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 4007 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 4008 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4009 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4010 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4011 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4012 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4013 | * returns target flags in extended format or 0 if restripe for this |
| 4014 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4015 | * |
| 4016 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4017 | */ |
| 4018 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 4019 | { |
| 4020 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 4021 | u64 target = 0; |
| 4022 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4023 | if (!bctl) |
| 4024 | return 0; |
| 4025 | |
| 4026 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 4027 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4028 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 4029 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 4030 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4031 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 4032 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 4033 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4034 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 4035 | } |
| 4036 | |
| 4037 | return target; |
| 4038 | } |
| 4039 | |
| 4040 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4041 | * @flags: available profiles in extended format (see ctree.h) |
| 4042 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4043 | * Returns reduced profile in chunk format. If profile changing is in |
| 4044 | * progress (either running or paused) picks the target profile (if it's |
| 4045 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4046 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4047 | 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] | 4048 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4049 | u64 num_devices = fs_info->fs_devices->rw_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4050 | u64 target; |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4051 | u64 raid_type; |
| 4052 | u64 allowed = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4053 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4054 | /* |
| 4055 | * see if restripe for this chunk_type is in progress, if so |
| 4056 | * try to reduce to the target profile |
| 4057 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4058 | spin_lock(&fs_info->balance_lock); |
| 4059 | target = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4060 | if (target) { |
| 4061 | /* pick target profile only if it's already available */ |
| 4062 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4063 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4064 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4065 | } |
| 4066 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4067 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4068 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4069 | /* First, mask out the RAID levels which aren't possible */ |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4070 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
| 4071 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) |
| 4072 | allowed |= btrfs_raid_group[raid_type]; |
| 4073 | } |
| 4074 | allowed &= flags; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4075 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4076 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) |
| 4077 | allowed = BTRFS_BLOCK_GROUP_RAID6; |
| 4078 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) |
| 4079 | allowed = BTRFS_BLOCK_GROUP_RAID5; |
| 4080 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) |
| 4081 | allowed = BTRFS_BLOCK_GROUP_RAID10; |
| 4082 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) |
| 4083 | allowed = BTRFS_BLOCK_GROUP_RAID1; |
| 4084 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) |
| 4085 | allowed = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4086 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4087 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4088 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4089 | return extended_to_chunk(flags | allowed); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4090 | } |
| 4091 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4092 | 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] | 4093 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4094 | unsigned seq; |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 4095 | u64 flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4096 | |
| 4097 | do { |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 4098 | flags = orig_flags; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4099 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4100 | |
| 4101 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4102 | flags |= fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4103 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4104 | flags |= fs_info->avail_system_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4105 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4106 | flags |= fs_info->avail_metadata_alloc_bits; |
| 4107 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4108 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4109 | return btrfs_reduce_alloc_profile(fs_info, flags); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4110 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4111 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 4112 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4113 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4114 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4115 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4116 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4117 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4118 | if (data) |
| 4119 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4120 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4121 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 4122 | else |
| 4123 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 4124 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4125 | ret = get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4126 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4127 | } |
| 4128 | |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4129 | static u64 btrfs_space_info_used(struct btrfs_space_info *s_info, |
| 4130 | bool may_use_included) |
| 4131 | { |
| 4132 | ASSERT(s_info); |
| 4133 | return s_info->bytes_used + s_info->bytes_reserved + |
| 4134 | s_info->bytes_pinned + s_info->bytes_readonly + |
| 4135 | (may_use_included ? s_info->bytes_may_use : 0); |
| 4136 | } |
| 4137 | |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4138 | int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4139 | { |
| 4140 | struct btrfs_space_info *data_sinfo; |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4141 | struct btrfs_root *root = inode->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4142 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4143 | u64 used; |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4144 | int ret = 0; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4145 | int need_commit = 2; |
| 4146 | int have_pinned_space; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4147 | |
| 4148 | /* make sure bytes are sectorsize aligned */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4149 | bytes = ALIGN(bytes, fs_info->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4150 | |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4151 | if (btrfs_is_free_space_inode(inode)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4152 | need_commit = 0; |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4153 | ASSERT(current->journal_info); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4154 | } |
| 4155 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4156 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4157 | if (!data_sinfo) |
| 4158 | goto alloc; |
| 4159 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4160 | again: |
| 4161 | /* make sure we have enough space to handle the data first */ |
| 4162 | spin_lock(&data_sinfo->lock); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4163 | used = btrfs_space_info_used(data_sinfo, true); |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4164 | |
| 4165 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4166 | struct btrfs_trans_handle *trans; |
| 4167 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4168 | /* |
| 4169 | * if we don't have enough free bytes in this space then we need |
| 4170 | * to alloc a new chunk. |
| 4171 | */ |
Zhao Lei | b9fd47c | 2015-02-09 14:40:20 +0800 | [diff] [blame] | 4172 | if (!data_sinfo->full) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4173 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4174 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4175 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4176 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4177 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4178 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4179 | /* |
| 4180 | * It is ugly that we don't call nolock join |
| 4181 | * transaction for the free space inode case here. |
| 4182 | * But it is safe because we only do the data space |
| 4183 | * reservation for the free space cache in the |
| 4184 | * transaction context, the common join transaction |
| 4185 | * just increase the counter of the current transaction |
| 4186 | * handler, doesn't try to acquire the trans_lock of |
| 4187 | * the fs. |
| 4188 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4189 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4190 | if (IS_ERR(trans)) |
| 4191 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4192 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4193 | ret = do_chunk_alloc(trans, fs_info, alloc_target, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4194 | CHUNK_ALLOC_NO_FORCE); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4195 | btrfs_end_transaction(trans); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4196 | if (ret < 0) { |
| 4197 | if (ret != -ENOSPC) |
| 4198 | return ret; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4199 | else { |
| 4200 | have_pinned_space = 1; |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4201 | goto commit_trans; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4202 | } |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4203 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4204 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4205 | if (!data_sinfo) |
| 4206 | data_sinfo = fs_info->data_sinfo; |
| 4207 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4208 | goto again; |
| 4209 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4210 | |
| 4211 | /* |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4212 | * If we don't have enough pinned space to deal with this |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4213 | * allocation, and no removed chunk in current transaction, |
| 4214 | * don't bother committing the transaction. |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4215 | */ |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4216 | have_pinned_space = percpu_counter_compare( |
| 4217 | &data_sinfo->total_bytes_pinned, |
| 4218 | used + bytes - data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4219 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4220 | |
| 4221 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4222 | commit_trans: |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4223 | if (need_commit && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4224 | !atomic_read(&fs_info->open_ioctl_trans)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4225 | need_commit--; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4226 | |
Zhao Lei | e1746e8 | 2015-12-01 18:39:40 +0800 | [diff] [blame] | 4227 | if (need_commit > 0) { |
| 4228 | btrfs_start_delalloc_roots(fs_info, 0, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4229 | btrfs_wait_ordered_roots(fs_info, -1, 0, |
| 4230 | (u64)-1); |
Zhao Lei | e1746e8 | 2015-12-01 18:39:40 +0800 | [diff] [blame] | 4231 | } |
Zhao Lei | 9a4e727 | 2015-04-09 12:34:43 +0800 | [diff] [blame] | 4232 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4233 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4234 | if (IS_ERR(trans)) |
| 4235 | return PTR_ERR(trans); |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4236 | if (have_pinned_space >= 0 || |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 4237 | test_bit(BTRFS_TRANS_HAVE_FREE_BGS, |
| 4238 | &trans->transaction->flags) || |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4239 | need_commit > 0) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4240 | ret = btrfs_commit_transaction(trans); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4241 | if (ret) |
| 4242 | return ret; |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4243 | /* |
Filipe Manana | c2d6cb1 | 2016-01-15 11:05:12 +0000 | [diff] [blame] | 4244 | * The cleaner kthread might still be doing iput |
| 4245 | * operations. Wait for it to finish so that |
| 4246 | * more space is released. |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4247 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4248 | mutex_lock(&fs_info->cleaner_delayed_iput_mutex); |
| 4249 | mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4250 | goto again; |
| 4251 | } else { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4252 | btrfs_end_transaction(trans); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4253 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4254 | } |
| 4255 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4256 | trace_btrfs_space_reservation(fs_info, |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 4257 | "space_info:enospc", |
| 4258 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4259 | return -ENOSPC; |
| 4260 | } |
| 4261 | data_sinfo->bytes_may_use += bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4262 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4263 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4264 | spin_unlock(&data_sinfo->lock); |
| 4265 | |
Dongsheng Yang | 237c0e9 | 2014-12-29 06:23:05 -0500 | [diff] [blame] | 4266 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4270 | * New check_data_free_space() with ability for precious data reservation |
| 4271 | * Will replace old btrfs_check_data_free_space(), but for patch split, |
| 4272 | * add a new function first and then replace it. |
| 4273 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4274 | 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] | 4275 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4276 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4277 | int ret; |
| 4278 | |
| 4279 | /* align the range */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4280 | len = round_up(start + len, fs_info->sectorsize) - |
| 4281 | round_down(start, fs_info->sectorsize); |
| 4282 | start = round_down(start, fs_info->sectorsize); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4283 | |
Nikolay Borisov | 04f4f91 | 2017-02-20 13:50:36 +0200 | [diff] [blame] | 4284 | ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4285 | if (ret < 0) |
| 4286 | return ret; |
| 4287 | |
Josef Bacik | 1e5ec2e | 2016-09-15 14:57:48 -0400 | [diff] [blame] | 4288 | /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */ |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4289 | ret = btrfs_qgroup_reserve_data(inode, start, len); |
Josef Bacik | 1e5ec2e | 2016-09-15 14:57:48 -0400 | [diff] [blame] | 4290 | if (ret) |
| 4291 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4292 | return ret; |
| 4293 | } |
| 4294 | |
| 4295 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4296 | * Called if we need to clear a data reservation for this inode |
| 4297 | * Normally in a error case. |
| 4298 | * |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4299 | * This one will *NOT* use accurate qgroup reserved space API, just for case |
| 4300 | * which we can't sleep and is sure it won't affect qgroup reserved space. |
| 4301 | * Like clear_bit_hook(). |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4302 | */ |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4303 | void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, |
| 4304 | u64 len) |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4305 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4306 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4307 | struct btrfs_space_info *data_sinfo; |
| 4308 | |
| 4309 | /* Make sure the range is aligned to sectorsize */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4310 | len = round_up(start + len, fs_info->sectorsize) - |
| 4311 | round_down(start, fs_info->sectorsize); |
| 4312 | start = round_down(start, fs_info->sectorsize); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4313 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4314 | data_sinfo = fs_info->data_sinfo; |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4315 | spin_lock(&data_sinfo->lock); |
| 4316 | if (WARN_ON(data_sinfo->bytes_may_use < len)) |
| 4317 | data_sinfo->bytes_may_use = 0; |
| 4318 | else |
| 4319 | data_sinfo->bytes_may_use -= len; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4320 | trace_btrfs_space_reservation(fs_info, "space_info", |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4321 | data_sinfo->flags, len, 0); |
| 4322 | spin_unlock(&data_sinfo->lock); |
| 4323 | } |
| 4324 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4325 | /* |
| 4326 | * Called if we need to clear a data reservation for this inode |
| 4327 | * Normally in a error case. |
| 4328 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4329 | * 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] | 4330 | * space framework. |
| 4331 | */ |
| 4332 | void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len) |
| 4333 | { |
Jeff Mahoney | 0c476a5 | 2016-11-18 21:52:40 -0500 | [diff] [blame] | 4334 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4335 | |
| 4336 | /* Make sure the range is aligned to sectorsize */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4337 | len = round_up(start + len, root->fs_info->sectorsize) - |
| 4338 | round_down(start, root->fs_info->sectorsize); |
| 4339 | start = round_down(start, root->fs_info->sectorsize); |
Jeff Mahoney | 0c476a5 | 2016-11-18 21:52:40 -0500 | [diff] [blame] | 4340 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4341 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
| 4342 | btrfs_qgroup_free_data(inode, start, len); |
| 4343 | } |
| 4344 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4345 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 4346 | { |
| 4347 | struct list_head *head = &info->space_info; |
| 4348 | struct btrfs_space_info *found; |
| 4349 | |
| 4350 | rcu_read_lock(); |
| 4351 | list_for_each_entry_rcu(found, head, list) { |
| 4352 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4353 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4354 | } |
| 4355 | rcu_read_unlock(); |
| 4356 | } |
| 4357 | |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4358 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
| 4359 | { |
| 4360 | return (global->size << 1); |
| 4361 | } |
| 4362 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4363 | static int should_alloc_chunk(struct btrfs_fs_info *fs_info, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4364 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4365 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4366 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4367 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4368 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 4369 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4370 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4371 | if (force == CHUNK_ALLOC_FORCE) |
| 4372 | return 1; |
| 4373 | |
| 4374 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4375 | * We need to take into account the global rsv because for all intents |
| 4376 | * and purposes it's used space. Don't worry about locking the |
| 4377 | * global_rsv, it doesn't change except when the transaction commits. |
| 4378 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 4379 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4380 | num_allocated += calc_global_rsv_need_space(global_rsv); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4381 | |
| 4382 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4383 | * in limited mode, we want to have some free space up to |
| 4384 | * about 1% of the FS size. |
| 4385 | */ |
| 4386 | if (force == CHUNK_ALLOC_LIMITED) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4387 | thresh = btrfs_super_total_bytes(fs_info->super_copy); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4388 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4389 | |
| 4390 | if (num_bytes - num_allocated < thresh) |
| 4391 | return 1; |
| 4392 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4393 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4394 | if (num_allocated + SZ_2M < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 4395 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4396 | return 1; |
| 4397 | } |
| 4398 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4399 | 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] | 4400 | { |
| 4401 | u64 num_dev; |
| 4402 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4403 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
| 4404 | BTRFS_BLOCK_GROUP_RAID0 | |
| 4405 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4406 | BTRFS_BLOCK_GROUP_RAID6)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4407 | num_dev = fs_info->fs_devices->rw_devices; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4408 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 4409 | num_dev = 2; |
| 4410 | else |
| 4411 | num_dev = 1; /* DUP or single */ |
| 4412 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4413 | return num_dev; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4414 | } |
| 4415 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4416 | /* |
| 4417 | * If @is_allocation is true, reserve space in the system space info necessary |
| 4418 | * for allocating a chunk, otherwise if it's false, reserve space necessary for |
| 4419 | * removing a chunk. |
| 4420 | */ |
| 4421 | void check_system_chunk(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4422 | struct btrfs_fs_info *fs_info, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4423 | { |
| 4424 | struct btrfs_space_info *info; |
| 4425 | u64 left; |
| 4426 | u64 thresh; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4427 | int ret = 0; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4428 | u64 num_devs; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4429 | |
| 4430 | /* |
| 4431 | * Needed because we can end up allocating a system chunk and for an |
| 4432 | * atomic and race free space reservation in the chunk block reserve. |
| 4433 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4434 | ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4435 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4436 | info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4437 | spin_lock(&info->lock); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4438 | left = info->total_bytes - btrfs_space_info_used(info, true); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4439 | spin_unlock(&info->lock); |
| 4440 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4441 | num_devs = get_profile_num_devs(fs_info, type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4442 | |
| 4443 | /* 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] | 4444 | thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) + |
| 4445 | btrfs_calc_trans_metadata_size(fs_info, 1); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4446 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4447 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
| 4448 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", |
| 4449 | left, thresh, type); |
| 4450 | dump_space_info(fs_info, info, 0, 0); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | if (left < thresh) { |
| 4454 | u64 flags; |
| 4455 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4456 | flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4457 | /* |
| 4458 | * Ignore failure to create system chunk. We might end up not |
| 4459 | * needing it, as we might not need to COW all nodes/leafs from |
| 4460 | * the paths we visit in the chunk tree (they were already COWed |
| 4461 | * or created in the current transaction for example). |
| 4462 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4463 | ret = btrfs_alloc_chunk(trans, fs_info, flags); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4467 | ret = btrfs_block_rsv_add(fs_info->chunk_root, |
| 4468 | &fs_info->chunk_block_rsv, |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4469 | thresh, BTRFS_RESERVE_NO_FLUSH); |
| 4470 | if (!ret) |
| 4471 | trans->chunk_bytes_reserved += thresh; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4472 | } |
| 4473 | } |
| 4474 | |
Liu Bo | 28b737f | 2016-07-29 11:09:50 -0700 | [diff] [blame] | 4475 | /* |
| 4476 | * If force is CHUNK_ALLOC_FORCE: |
| 4477 | * - return 1 if it successfully allocates a chunk, |
| 4478 | * - return errors including -ENOSPC otherwise. |
| 4479 | * If force is NOT CHUNK_ALLOC_FORCE: |
| 4480 | * - return 0 if it doesn't need to allocate a new chunk, |
| 4481 | * - return 1 if it successfully allocates a chunk, |
| 4482 | * - return errors including -ENOSPC otherwise. |
| 4483 | */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4484 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4485 | struct btrfs_fs_info *fs_info, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4486 | { |
| 4487 | struct btrfs_space_info *space_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4488 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4489 | int ret = 0; |
| 4490 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4491 | /* Don't re-enter if we're already allocating a chunk */ |
| 4492 | if (trans->allocating_chunk) |
| 4493 | return -ENOSPC; |
| 4494 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4495 | space_info = __find_space_info(fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4496 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4497 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4498 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4499 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4500 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4501 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4502 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4503 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4504 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4505 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4506 | if (space_info->full) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4507 | if (should_alloc_chunk(fs_info, space_info, force)) |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4508 | ret = -ENOSPC; |
| 4509 | else |
| 4510 | ret = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4511 | spin_unlock(&space_info->lock); |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4512 | return ret; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4513 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4514 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4515 | if (!should_alloc_chunk(fs_info, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4516 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4517 | return 0; |
| 4518 | } else if (space_info->chunk_alloc) { |
| 4519 | wait_for_alloc = 1; |
| 4520 | } else { |
| 4521 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4522 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4523 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4524 | spin_unlock(&space_info->lock); |
| 4525 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4526 | mutex_lock(&fs_info->chunk_mutex); |
| 4527 | |
| 4528 | /* |
| 4529 | * The chunk_mutex is held throughout the entirety of a chunk |
| 4530 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 4531 | * other guy is done and we need to recheck and see if we should |
| 4532 | * allocate. |
| 4533 | */ |
| 4534 | if (wait_for_alloc) { |
| 4535 | mutex_unlock(&fs_info->chunk_mutex); |
| 4536 | wait_for_alloc = 0; |
| 4537 | goto again; |
| 4538 | } |
| 4539 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4540 | trans->allocating_chunk = true; |
| 4541 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4542 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4543 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 4544 | * allocating mixed chunks instead of individual chunks. |
| 4545 | */ |
| 4546 | if (btrfs_mixed_space_info(space_info)) |
| 4547 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 4548 | |
| 4549 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4550 | * if we're doing a data chunk, go ahead and make sure that |
| 4551 | * we keep a reasonable number of metadata chunks allocated in the |
| 4552 | * FS as well. |
| 4553 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4554 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4555 | fs_info->data_chunk_allocations++; |
| 4556 | if (!(fs_info->data_chunk_allocations % |
| 4557 | fs_info->metadata_ratio)) |
| 4558 | force_metadata_allocation(fs_info); |
| 4559 | } |
| 4560 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4561 | /* |
| 4562 | * Check if we have enough space in SYSTEM chunk because we may need |
| 4563 | * to update devices. |
| 4564 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4565 | check_system_chunk(trans, fs_info, flags); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4566 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4567 | ret = btrfs_alloc_chunk(trans, fs_info, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4568 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 4569 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4570 | spin_lock(&space_info->lock); |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4571 | if (ret < 0 && ret != -ENOSPC) |
| 4572 | goto out; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4573 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4574 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4575 | else |
| 4576 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4577 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4578 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4579 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4580 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4581 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 4582 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4583 | /* |
| 4584 | * When we allocate a new chunk we reserve space in the chunk block |
| 4585 | * reserve to make sure we can COW nodes/leafs in the chunk tree or |
| 4586 | * add new nodes/leafs to it if we end up needing to do it when |
| 4587 | * inserting the chunk item and updating device items as part of the |
| 4588 | * second phase of chunk allocation, performed by |
| 4589 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a |
| 4590 | * large number of new block groups to create in our transaction |
| 4591 | * handle's new_bgs list to avoid exhausting the chunk block reserve |
| 4592 | * in extreme cases - like having a single transaction create many new |
| 4593 | * block groups when starting to write out the free space caches of all |
| 4594 | * the block groups that were made dirty during the lifetime of the |
| 4595 | * transaction. |
| 4596 | */ |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 4597 | if (trans->can_flush_pending_bgs && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4598 | trans->chunk_bytes_reserved >= (u64)SZ_2M) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4599 | btrfs_create_pending_block_groups(trans, fs_info); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4600 | btrfs_trans_release_chunk_metadata(trans); |
| 4601 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4602 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4603 | } |
| 4604 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4605 | static int can_overcommit(struct btrfs_root *root, |
| 4606 | struct btrfs_space_info *space_info, u64 bytes, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4607 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4608 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4609 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4610 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4611 | u64 profile; |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4612 | u64 space_size; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4613 | u64 avail; |
| 4614 | u64 used; |
| 4615 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4616 | /* Don't overcommit when in mixed mode. */ |
| 4617 | if (space_info->flags & BTRFS_BLOCK_GROUP_DATA) |
| 4618 | return 0; |
| 4619 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4620 | profile = btrfs_get_alloc_profile(root, 0); |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 4621 | used = btrfs_space_info_used(space_info, false); |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4622 | |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4623 | /* |
| 4624 | * We only want to allow over committing if we have lots of actual space |
| 4625 | * free, but if we don't have enough space to handle the global reserve |
| 4626 | * space then we could end up having a real enospc problem when trying |
| 4627 | * to allocate a chunk or some other such important allocation. |
| 4628 | */ |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4629 | spin_lock(&global_rsv->lock); |
| 4630 | space_size = calc_global_rsv_need_space(global_rsv); |
| 4631 | spin_unlock(&global_rsv->lock); |
| 4632 | if (used + space_size >= space_info->total_bytes) |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4633 | return 0; |
| 4634 | |
| 4635 | used += space_info->bytes_may_use; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4636 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4637 | spin_lock(&fs_info->free_chunk_lock); |
| 4638 | avail = fs_info->free_chunk_space; |
| 4639 | spin_unlock(&fs_info->free_chunk_lock); |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4640 | |
| 4641 | /* |
| 4642 | * 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] | 4643 | * space is actually useable. For raid56, the space info used |
| 4644 | * doesn't include the parity drive, so we don't have to |
| 4645 | * change the math |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4646 | */ |
| 4647 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 4648 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4649 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4650 | avail >>= 1; |
| 4651 | |
| 4652 | /* |
Miao Xie | 561c294 | 2012-10-16 11:32:18 +0000 | [diff] [blame] | 4653 | * If we aren't flushing all things, let us overcommit up to |
| 4654 | * 1/2th of the space. If we can flush, don't let us overcommit |
| 4655 | * too much, let it overcommit up to 1/8 of the space. |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4656 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4657 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4658 | avail >>= 3; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4659 | else |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4660 | avail >>= 1; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4661 | |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4662 | if (used + bytes < space_info->total_bytes + avail) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4663 | return 1; |
| 4664 | return 0; |
| 4665 | } |
| 4666 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4667 | 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] | 4668 | unsigned long nr_pages, int nr_items) |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4669 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4670 | struct super_block *sb = fs_info->sb; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4671 | |
Josef Bacik | 925a6ef | 2013-06-20 12:31:27 -0400 | [diff] [blame] | 4672 | if (down_read_trylock(&sb->s_umount)) { |
| 4673 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); |
| 4674 | up_read(&sb->s_umount); |
| 4675 | } else { |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4676 | /* |
| 4677 | * We needn't worry the filesystem going from r/w to r/o though |
| 4678 | * we don't acquire ->s_umount mutex, because the filesystem |
| 4679 | * should guarantee the delalloc inodes list be empty after |
| 4680 | * the filesystem is readonly(all dirty pages are written to |
| 4681 | * the disk). |
| 4682 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4683 | btrfs_start_delalloc_roots(fs_info, 0, nr_items); |
Josef Bacik | 98ad69c | 2013-04-04 11:55:49 -0400 | [diff] [blame] | 4684 | if (!current->journal_info) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4685 | btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1); |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4689 | static inline int calc_reclaim_items_nr(struct btrfs_fs_info *fs_info, |
| 4690 | u64 to_reclaim) |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4691 | { |
| 4692 | u64 bytes; |
| 4693 | int nr; |
| 4694 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4695 | bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4696 | nr = (int)div64_u64(to_reclaim, bytes); |
| 4697 | if (!nr) |
| 4698 | nr = 1; |
| 4699 | return nr; |
| 4700 | } |
| 4701 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4702 | #define EXTENT_SIZE_PER_ITEM SZ_256K |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4703 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4704 | /* |
| 4705 | * shrink metadata reservation for delalloc |
| 4706 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4707 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 4708 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4709 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4710 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4711 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4712 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4713 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4714 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4715 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 4716 | long time_left; |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4717 | unsigned long nr_pages; |
| 4718 | int loops; |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4719 | int items; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4720 | enum btrfs_reserve_flush_enum flush; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4721 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4722 | /* Calc the number of the pages we need flush for space reservation */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4723 | items = calc_reclaim_items_nr(fs_info, to_reclaim); |
Adam Borowski | 8eb0dfd | 2016-05-08 15:08:00 +0200 | [diff] [blame] | 4724 | to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM; |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4725 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4726 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4727 | block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4728 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 4729 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4730 | delalloc_bytes = percpu_counter_sum_positive( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4731 | &fs_info->delalloc_bytes); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4732 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4733 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4734 | return; |
Miao Xie | 38c135a | 2013-11-04 23:13:21 +0800 | [diff] [blame] | 4735 | if (wait_ordered) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4736 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4737 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4738 | } |
| 4739 | |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4740 | loops = 0; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4741 | while (delalloc_bytes && loops < 3) { |
| 4742 | max_reclaim = min(delalloc_bytes, to_reclaim); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4743 | nr_pages = max_reclaim >> PAGE_SHIFT; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4744 | btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items); |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4745 | /* |
| 4746 | * We need to wait for the async pages to actually start before |
| 4747 | * we do anything. |
| 4748 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4749 | max_reclaim = atomic_read(&fs_info->async_delalloc_pages); |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4750 | if (!max_reclaim) |
| 4751 | goto skip_async; |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4752 | |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4753 | if (max_reclaim <= nr_pages) |
| 4754 | max_reclaim = 0; |
| 4755 | else |
| 4756 | max_reclaim -= nr_pages; |
| 4757 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4758 | wait_event(fs_info->async_submit_wait, |
| 4759 | atomic_read(&fs_info->async_delalloc_pages) <= |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4760 | (int)max_reclaim); |
| 4761 | skip_async: |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4762 | if (!trans) |
| 4763 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 4764 | else |
| 4765 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4766 | spin_lock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4767 | if (can_overcommit(root, space_info, orig, flush)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4768 | spin_unlock(&space_info->lock); |
| 4769 | break; |
| 4770 | } |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4771 | if (list_empty(&space_info->tickets) && |
| 4772 | list_empty(&space_info->priority_tickets)) { |
| 4773 | spin_unlock(&space_info->lock); |
| 4774 | break; |
| 4775 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4776 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4777 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4778 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4779 | if (wait_ordered && !trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4780 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4781 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4782 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4783 | if (time_left) |
| 4784 | break; |
| 4785 | } |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4786 | delalloc_bytes = percpu_counter_sum_positive( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4787 | &fs_info->delalloc_bytes); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4788 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4789 | } |
| 4790 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4791 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4792 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 4793 | * @root - the root we're allocating for |
| 4794 | * @bytes - the number of bytes we want to reserve |
| 4795 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4796 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4797 | * This will check to make sure that committing the transaction will actually |
| 4798 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 4799 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4800 | */ |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4801 | static int may_commit_transaction(struct btrfs_fs_info *fs_info, |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4802 | struct btrfs_space_info *space_info, |
| 4803 | u64 bytes, int force) |
| 4804 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4805 | struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4806 | struct btrfs_trans_handle *trans; |
| 4807 | |
| 4808 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 4809 | if (trans) |
| 4810 | return -EAGAIN; |
| 4811 | |
| 4812 | if (force) |
| 4813 | goto commit; |
| 4814 | |
| 4815 | /* See if there is enough pinned space to make this reservation */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4816 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
Miao Xie | 0424c54 | 2014-03-06 13:54:59 +0800 | [diff] [blame] | 4817 | bytes) >= 0) |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4818 | goto commit; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4819 | |
| 4820 | /* |
| 4821 | * See if there is some space in the delayed insertion reservation for |
| 4822 | * this reservation. |
| 4823 | */ |
| 4824 | if (space_info != delayed_rsv->space_info) |
| 4825 | return -ENOSPC; |
| 4826 | |
| 4827 | spin_lock(&delayed_rsv->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4828 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4829 | bytes - delayed_rsv->size) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4830 | spin_unlock(&delayed_rsv->lock); |
| 4831 | return -ENOSPC; |
| 4832 | } |
| 4833 | spin_unlock(&delayed_rsv->lock); |
| 4834 | |
| 4835 | commit: |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4836 | trans = btrfs_join_transaction(fs_info->fs_root); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4837 | if (IS_ERR(trans)) |
| 4838 | return -ENOSPC; |
| 4839 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4840 | return btrfs_commit_transaction(trans); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4841 | } |
| 4842 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4843 | struct reserve_ticket { |
| 4844 | u64 bytes; |
| 4845 | int error; |
| 4846 | struct list_head list; |
| 4847 | wait_queue_head_t wait; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4848 | }; |
| 4849 | |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4850 | static int flush_space(struct btrfs_fs_info *fs_info, |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4851 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 4852 | u64 orig_bytes, int state) |
| 4853 | { |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4854 | struct btrfs_root *root = fs_info->fs_root; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4855 | struct btrfs_trans_handle *trans; |
| 4856 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4857 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4858 | |
| 4859 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4860 | case FLUSH_DELAYED_ITEMS_NR: |
| 4861 | case FLUSH_DELAYED_ITEMS: |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4862 | if (state == FLUSH_DELAYED_ITEMS_NR) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4863 | nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4864 | else |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4865 | nr = -1; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4866 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4867 | trans = btrfs_join_transaction(root); |
| 4868 | if (IS_ERR(trans)) { |
| 4869 | ret = PTR_ERR(trans); |
| 4870 | break; |
| 4871 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4872 | ret = btrfs_run_delayed_items_nr(trans, fs_info, nr); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4873 | btrfs_end_transaction(trans); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4874 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4875 | case FLUSH_DELALLOC: |
| 4876 | case FLUSH_DELALLOC_WAIT: |
Miao Xie | 24af7dd | 2014-03-06 13:55:00 +0800 | [diff] [blame] | 4877 | shrink_delalloc(root, num_bytes * 2, orig_bytes, |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4878 | state == FLUSH_DELALLOC_WAIT); |
| 4879 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4880 | case ALLOC_CHUNK: |
| 4881 | trans = btrfs_join_transaction(root); |
| 4882 | if (IS_ERR(trans)) { |
| 4883 | ret = PTR_ERR(trans); |
| 4884 | break; |
| 4885 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4886 | ret = do_chunk_alloc(trans, fs_info, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4887 | btrfs_get_alloc_profile(root, 0), |
| 4888 | CHUNK_ALLOC_NO_FORCE); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4889 | btrfs_end_transaction(trans); |
Alex Lyakas | eecba89 | 2015-12-06 12:32:31 +0200 | [diff] [blame] | 4890 | if (ret > 0 || ret == -ENOSPC) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4891 | ret = 0; |
| 4892 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4893 | case COMMIT_TRANS: |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 4894 | ret = may_commit_transaction(fs_info, space_info, |
| 4895 | orig_bytes, 0); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4896 | break; |
| 4897 | default: |
| 4898 | ret = -ENOSPC; |
| 4899 | break; |
| 4900 | } |
| 4901 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4902 | trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 4903 | orig_bytes, state, ret); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4904 | return ret; |
| 4905 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4906 | |
| 4907 | static inline u64 |
| 4908 | btrfs_calc_reclaim_metadata_size(struct btrfs_root *root, |
| 4909 | struct btrfs_space_info *space_info) |
| 4910 | { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4911 | struct reserve_ticket *ticket; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4912 | u64 used; |
| 4913 | u64 expected; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4914 | u64 to_reclaim = 0; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4915 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4916 | list_for_each_entry(ticket, &space_info->tickets, list) |
| 4917 | to_reclaim += ticket->bytes; |
| 4918 | list_for_each_entry(ticket, &space_info->priority_tickets, list) |
| 4919 | to_reclaim += ticket->bytes; |
| 4920 | if (to_reclaim) |
| 4921 | return to_reclaim; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4922 | |
Wang Xiaoguang | e0af248 | 2016-08-31 19:46:16 +0800 | [diff] [blame] | 4923 | to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M); |
| 4924 | if (can_overcommit(root, space_info, to_reclaim, |
| 4925 | BTRFS_RESERVE_FLUSH_ALL)) |
| 4926 | return 0; |
| 4927 | |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4928 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4929 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4930 | space_info->bytes_may_use; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4931 | if (can_overcommit(root, space_info, SZ_1M, BTRFS_RESERVE_FLUSH_ALL)) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4932 | expected = div_factor_fine(space_info->total_bytes, 95); |
| 4933 | else |
| 4934 | expected = div_factor_fine(space_info->total_bytes, 90); |
| 4935 | |
| 4936 | if (used > expected) |
| 4937 | to_reclaim = used - expected; |
| 4938 | else |
| 4939 | to_reclaim = 0; |
| 4940 | to_reclaim = min(to_reclaim, space_info->bytes_may_use + |
| 4941 | space_info->bytes_reserved); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4942 | return to_reclaim; |
| 4943 | } |
| 4944 | |
| 4945 | 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] | 4946 | struct btrfs_root *root, u64 used) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4947 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4948 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4949 | u64 thresh = div_factor_fine(space_info->total_bytes, 98); |
| 4950 | |
| 4951 | /* 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] | 4952 | if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh) |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4953 | return 0; |
| 4954 | |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 4955 | if (!btrfs_calc_reclaim_metadata_size(root, space_info)) |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4956 | return 0; |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4957 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4958 | return (used >= thresh && !btrfs_fs_closing(fs_info) && |
| 4959 | !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4960 | } |
| 4961 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4962 | static void wake_all_tickets(struct list_head *head) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4963 | { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4964 | struct reserve_ticket *ticket; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4965 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4966 | while (!list_empty(head)) { |
| 4967 | ticket = list_first_entry(head, struct reserve_ticket, list); |
| 4968 | list_del_init(&ticket->list); |
| 4969 | ticket->error = -ENOSPC; |
| 4970 | wake_up(&ticket->wait); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4971 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4972 | } |
| 4973 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4974 | /* |
| 4975 | * This is for normal flushers, we can wait all goddamned day if we want to. We |
| 4976 | * will loop and continuously try to flush as long as we are making progress. |
| 4977 | * We count progress as clearing off tickets each time we have to loop. |
| 4978 | */ |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4979 | static void btrfs_async_reclaim_metadata_space(struct work_struct *work) |
| 4980 | { |
| 4981 | struct btrfs_fs_info *fs_info; |
| 4982 | struct btrfs_space_info *space_info; |
| 4983 | u64 to_reclaim; |
| 4984 | int flush_state; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4985 | int commit_cycles = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 4986 | u64 last_tickets_id; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4987 | |
| 4988 | fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); |
| 4989 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4990 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4991 | spin_lock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4992 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 4993 | space_info); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4994 | if (!to_reclaim) { |
| 4995 | space_info->flush = 0; |
| 4996 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4997 | return; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4998 | } |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 4999 | last_tickets_id = space_info->tickets_id; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5000 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5001 | |
| 5002 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5003 | do { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5004 | struct reserve_ticket *ticket; |
| 5005 | int ret; |
| 5006 | |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 5007 | ret = flush_space(fs_info, space_info, to_reclaim, to_reclaim, |
| 5008 | flush_state); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5009 | spin_lock(&space_info->lock); |
| 5010 | if (list_empty(&space_info->tickets)) { |
| 5011 | space_info->flush = 0; |
| 5012 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5013 | return; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5014 | } |
| 5015 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 5016 | space_info); |
| 5017 | ticket = list_first_entry(&space_info->tickets, |
| 5018 | struct reserve_ticket, list); |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5019 | if (last_tickets_id == space_info->tickets_id) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5020 | flush_state++; |
| 5021 | } else { |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5022 | last_tickets_id = space_info->tickets_id; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5023 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5024 | if (commit_cycles) |
| 5025 | commit_cycles--; |
| 5026 | } |
| 5027 | |
| 5028 | if (flush_state > COMMIT_TRANS) { |
| 5029 | commit_cycles++; |
| 5030 | if (commit_cycles > 2) { |
| 5031 | wake_all_tickets(&space_info->tickets); |
| 5032 | space_info->flush = 0; |
| 5033 | } else { |
| 5034 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5035 | } |
| 5036 | } |
| 5037 | spin_unlock(&space_info->lock); |
| 5038 | } while (flush_state <= COMMIT_TRANS); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | void btrfs_init_async_reclaim_work(struct work_struct *work) |
| 5042 | { |
| 5043 | INIT_WORK(work, btrfs_async_reclaim_metadata_space); |
| 5044 | } |
| 5045 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5046 | static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, |
| 5047 | struct btrfs_space_info *space_info, |
| 5048 | struct reserve_ticket *ticket) |
| 5049 | { |
| 5050 | u64 to_reclaim; |
| 5051 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5052 | |
| 5053 | spin_lock(&space_info->lock); |
| 5054 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 5055 | space_info); |
| 5056 | if (!to_reclaim) { |
| 5057 | spin_unlock(&space_info->lock); |
| 5058 | return; |
| 5059 | } |
| 5060 | spin_unlock(&space_info->lock); |
| 5061 | |
| 5062 | do { |
Jeff Mahoney | 0c9ab34 | 2017-02-15 16:28:28 -0500 | [diff] [blame] | 5063 | flush_space(fs_info, space_info, to_reclaim, to_reclaim, |
| 5064 | flush_state); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5065 | flush_state++; |
| 5066 | spin_lock(&space_info->lock); |
| 5067 | if (ticket->bytes == 0) { |
| 5068 | spin_unlock(&space_info->lock); |
| 5069 | return; |
| 5070 | } |
| 5071 | spin_unlock(&space_info->lock); |
| 5072 | |
| 5073 | /* |
| 5074 | * Priority flushers can't wait on delalloc without |
| 5075 | * deadlocking. |
| 5076 | */ |
| 5077 | if (flush_state == FLUSH_DELALLOC || |
| 5078 | flush_state == FLUSH_DELALLOC_WAIT) |
| 5079 | flush_state = ALLOC_CHUNK; |
| 5080 | } while (flush_state < COMMIT_TRANS); |
| 5081 | } |
| 5082 | |
| 5083 | static int wait_reserve_ticket(struct btrfs_fs_info *fs_info, |
| 5084 | struct btrfs_space_info *space_info, |
| 5085 | struct reserve_ticket *ticket, u64 orig_bytes) |
| 5086 | |
| 5087 | { |
| 5088 | DEFINE_WAIT(wait); |
| 5089 | int ret = 0; |
| 5090 | |
| 5091 | spin_lock(&space_info->lock); |
| 5092 | while (ticket->bytes > 0 && ticket->error == 0) { |
| 5093 | ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE); |
| 5094 | if (ret) { |
| 5095 | ret = -EINTR; |
| 5096 | break; |
| 5097 | } |
| 5098 | spin_unlock(&space_info->lock); |
| 5099 | |
| 5100 | schedule(); |
| 5101 | |
| 5102 | finish_wait(&ticket->wait, &wait); |
| 5103 | spin_lock(&space_info->lock); |
| 5104 | } |
| 5105 | if (!ret) |
| 5106 | ret = ticket->error; |
| 5107 | if (!list_empty(&ticket->list)) |
| 5108 | list_del_init(&ticket->list); |
| 5109 | if (ticket->bytes && ticket->bytes < orig_bytes) { |
| 5110 | u64 num_bytes = orig_bytes - ticket->bytes; |
| 5111 | space_info->bytes_may_use -= num_bytes; |
| 5112 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5113 | space_info->flags, num_bytes, 0); |
| 5114 | } |
| 5115 | spin_unlock(&space_info->lock); |
| 5116 | |
| 5117 | return ret; |
| 5118 | } |
| 5119 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5120 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5121 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 5122 | * @root - the root we're allocating for |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5123 | * @space_info - the space info we want to allocate from |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5124 | * @orig_bytes - the number of bytes we want |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 5125 | * @flush - whether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5126 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5127 | * 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] | 5128 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 5129 | * flush out space to make room. It will do this by flushing delalloc if |
| 5130 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 5131 | * regain reservations will be made and this will fail if there is not enough |
| 5132 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5133 | */ |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5134 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
| 5135 | struct btrfs_space_info *space_info, |
| 5136 | u64 orig_bytes, |
| 5137 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5138 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5139 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5140 | struct reserve_ticket ticket; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5141 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5142 | int ret = 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5143 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5144 | ASSERT(orig_bytes); |
Josef Bacik | 8ca17f0 | 2016-05-27 13:24:13 -0400 | [diff] [blame] | 5145 | ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL); |
| 5146 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5147 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 5148 | ret = -ENOSPC; |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 5149 | used = btrfs_space_info_used(space_info, true); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5150 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5151 | /* |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5152 | * If we have enough space then hooray, make our reservation and carry |
| 5153 | * on. If not see if we can overcommit, and if we can, hooray carry on. |
| 5154 | * If not things get more complicated. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5155 | */ |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5156 | if (used + orig_bytes <= space_info->total_bytes) { |
| 5157 | space_info->bytes_may_use += orig_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5158 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5159 | space_info->flags, orig_bytes, 1); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5160 | ret = 0; |
| 5161 | } else if (can_overcommit(root, space_info, orig_bytes, flush)) { |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 5162 | space_info->bytes_may_use += orig_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5163 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5164 | space_info->flags, orig_bytes, 1); |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 5165 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5166 | } |
| 5167 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5168 | /* |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5169 | * If we couldn't make a reservation then setup our reservation ticket |
| 5170 | * and kick the async worker if it's not already running. |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5171 | * |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5172 | * If we are a priority flusher then we just need to add our ticket to |
| 5173 | * the list and we will do our own flushing further down. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5174 | */ |
Josef Bacik | 72bcd99 | 2012-12-18 15:16:34 -0500 | [diff] [blame] | 5175 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5176 | ticket.bytes = orig_bytes; |
| 5177 | ticket.error = 0; |
| 5178 | init_waitqueue_head(&ticket.wait); |
| 5179 | if (flush == BTRFS_RESERVE_FLUSH_ALL) { |
| 5180 | list_add_tail(&ticket.list, &space_info->tickets); |
| 5181 | if (!space_info->flush) { |
| 5182 | space_info->flush = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5183 | trace_btrfs_trigger_flush(fs_info, |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 5184 | space_info->flags, |
| 5185 | orig_bytes, flush, |
| 5186 | "enospc"); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5187 | queue_work(system_unbound_wq, |
| 5188 | &root->fs_info->async_reclaim_work); |
| 5189 | } |
| 5190 | } else { |
| 5191 | list_add_tail(&ticket.list, |
| 5192 | &space_info->priority_tickets); |
| 5193 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5194 | } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
| 5195 | used += orig_bytes; |
Josef Bacik | f6acfd5 | 2014-09-18 11:27:17 -0400 | [diff] [blame] | 5196 | /* |
| 5197 | * We will do the space reservation dance during log replay, |
| 5198 | * which means we won't have fs_info->fs_root set, so don't do |
| 5199 | * the async reclaim as we will panic. |
| 5200 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5201 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) && |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 5202 | need_do_async_reclaim(space_info, root, used) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5203 | !work_busy(&fs_info->async_reclaim_work)) { |
| 5204 | trace_btrfs_trigger_flush(fs_info, space_info->flags, |
| 5205 | orig_bytes, flush, "preempt"); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5206 | queue_work(system_unbound_wq, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5207 | &fs_info->async_reclaim_work); |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 5208 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5209 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5210 | spin_unlock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5211 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5212 | return ret; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5213 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5214 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5215 | return wait_reserve_ticket(fs_info, space_info, &ticket, |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5216 | orig_bytes); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5217 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5218 | ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5219 | priority_reclaim_metadata_space(fs_info, space_info, &ticket); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5220 | spin_lock(&space_info->lock); |
| 5221 | if (ticket.bytes) { |
| 5222 | if (ticket.bytes < orig_bytes) { |
| 5223 | u64 num_bytes = orig_bytes - ticket.bytes; |
| 5224 | space_info->bytes_may_use -= num_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5225 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5226 | space_info->flags, |
| 5227 | num_bytes, 0); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5228 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5229 | } |
| 5230 | list_del_init(&ticket.list); |
| 5231 | ret = -ENOSPC; |
| 5232 | } |
| 5233 | spin_unlock(&space_info->lock); |
| 5234 | ASSERT(list_empty(&ticket.list)); |
| 5235 | return ret; |
| 5236 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5237 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5238 | /** |
| 5239 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 5240 | * @root - the root we're allocating for |
| 5241 | * @block_rsv - the block_rsv we're allocating for |
| 5242 | * @orig_bytes - the number of bytes we want |
| 5243 | * @flush - whether or not we can flush to make our reservation |
| 5244 | * |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5245 | * 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] | 5246 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 5247 | * flush out space to make room. It will do this by flushing delalloc if |
| 5248 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 5249 | * regain reservations will be made and this will fail if there is not enough |
| 5250 | * space already. |
| 5251 | */ |
| 5252 | static int reserve_metadata_bytes(struct btrfs_root *root, |
| 5253 | struct btrfs_block_rsv *block_rsv, |
| 5254 | u64 orig_bytes, |
| 5255 | enum btrfs_reserve_flush_enum flush) |
| 5256 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5257 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5258 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5259 | int ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5260 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5261 | ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes, |
| 5262 | flush); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5263 | if (ret == -ENOSPC && |
| 5264 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5265 | if (block_rsv != global_rsv && |
| 5266 | !block_rsv_use_bytes(global_rsv, orig_bytes)) |
| 5267 | ret = 0; |
| 5268 | } |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 5269 | if (ret == -ENOSPC) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5270 | trace_btrfs_space_reservation(fs_info, "space_info:enospc", |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5271 | block_rsv->space_info->flags, |
| 5272 | orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5273 | return ret; |
| 5274 | } |
| 5275 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5276 | static struct btrfs_block_rsv *get_block_rsv( |
| 5277 | const struct btrfs_trans_handle *trans, |
| 5278 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5279 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5280 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5281 | struct btrfs_block_rsv *block_rsv = NULL; |
| 5282 | |
Alexandru Moise | e9cf439 | 2015-09-09 00:18:50 +0000 | [diff] [blame] | 5283 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5284 | (root == fs_info->csum_root && trans->adding_csums) || |
| 5285 | (root == fs_info->uuid_root)) |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 5286 | block_rsv = trans->block_rsv; |
| 5287 | |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5288 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5289 | block_rsv = root->block_rsv; |
| 5290 | |
| 5291 | if (!block_rsv) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5292 | block_rsv = &fs_info->empty_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5293 | |
| 5294 | return block_rsv; |
| 5295 | } |
| 5296 | |
| 5297 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 5298 | u64 num_bytes) |
| 5299 | { |
| 5300 | int ret = -ENOSPC; |
| 5301 | spin_lock(&block_rsv->lock); |
| 5302 | if (block_rsv->reserved >= num_bytes) { |
| 5303 | block_rsv->reserved -= num_bytes; |
| 5304 | if (block_rsv->reserved < block_rsv->size) |
| 5305 | block_rsv->full = 0; |
| 5306 | ret = 0; |
| 5307 | } |
| 5308 | spin_unlock(&block_rsv->lock); |
| 5309 | return ret; |
| 5310 | } |
| 5311 | |
| 5312 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 5313 | u64 num_bytes, int update_size) |
| 5314 | { |
| 5315 | spin_lock(&block_rsv->lock); |
| 5316 | block_rsv->reserved += num_bytes; |
| 5317 | if (update_size) |
| 5318 | block_rsv->size += num_bytes; |
| 5319 | else if (block_rsv->reserved >= block_rsv->size) |
| 5320 | block_rsv->full = 1; |
| 5321 | spin_unlock(&block_rsv->lock); |
| 5322 | } |
| 5323 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 5324 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
| 5325 | struct btrfs_block_rsv *dest, u64 num_bytes, |
| 5326 | int min_factor) |
| 5327 | { |
| 5328 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5329 | u64 min_bytes; |
| 5330 | |
| 5331 | if (global_rsv->space_info != dest->space_info) |
| 5332 | return -ENOSPC; |
| 5333 | |
| 5334 | spin_lock(&global_rsv->lock); |
| 5335 | min_bytes = div_factor(global_rsv->size, min_factor); |
| 5336 | if (global_rsv->reserved < min_bytes + num_bytes) { |
| 5337 | spin_unlock(&global_rsv->lock); |
| 5338 | return -ENOSPC; |
| 5339 | } |
| 5340 | global_rsv->reserved -= num_bytes; |
| 5341 | if (global_rsv->reserved < global_rsv->size) |
| 5342 | global_rsv->full = 0; |
| 5343 | spin_unlock(&global_rsv->lock); |
| 5344 | |
| 5345 | block_rsv_add_bytes(dest, num_bytes, 1); |
| 5346 | return 0; |
| 5347 | } |
| 5348 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5349 | /* |
| 5350 | * This is for space we already have accounted in space_info->bytes_may_use, so |
| 5351 | * basically when we're returning space from block_rsv's. |
| 5352 | */ |
| 5353 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, |
| 5354 | struct btrfs_space_info *space_info, |
| 5355 | u64 num_bytes) |
| 5356 | { |
| 5357 | struct reserve_ticket *ticket; |
| 5358 | struct list_head *head; |
| 5359 | u64 used; |
| 5360 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH; |
| 5361 | bool check_overcommit = false; |
| 5362 | |
| 5363 | spin_lock(&space_info->lock); |
| 5364 | head = &space_info->priority_tickets; |
| 5365 | |
| 5366 | /* |
| 5367 | * If we are over our limit then we need to check and see if we can |
| 5368 | * overcommit, and if we can't then we just need to free up our space |
| 5369 | * and not satisfy any requests. |
| 5370 | */ |
| 5371 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 5372 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 5373 | space_info->bytes_may_use; |
| 5374 | if (used - num_bytes >= space_info->total_bytes) |
| 5375 | check_overcommit = true; |
| 5376 | again: |
| 5377 | while (!list_empty(head) && num_bytes) { |
| 5378 | ticket = list_first_entry(head, struct reserve_ticket, |
| 5379 | list); |
| 5380 | /* |
| 5381 | * We use 0 bytes because this space is already reserved, so |
| 5382 | * adding the ticket space would be a double count. |
| 5383 | */ |
| 5384 | if (check_overcommit && |
| 5385 | !can_overcommit(fs_info->extent_root, space_info, 0, |
| 5386 | flush)) |
| 5387 | break; |
| 5388 | if (num_bytes >= ticket->bytes) { |
| 5389 | list_del_init(&ticket->list); |
| 5390 | num_bytes -= ticket->bytes; |
| 5391 | ticket->bytes = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5392 | space_info->tickets_id++; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5393 | wake_up(&ticket->wait); |
| 5394 | } else { |
| 5395 | ticket->bytes -= num_bytes; |
| 5396 | num_bytes = 0; |
| 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | if (num_bytes && head == &space_info->priority_tickets) { |
| 5401 | head = &space_info->tickets; |
| 5402 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 5403 | goto again; |
| 5404 | } |
| 5405 | space_info->bytes_may_use -= num_bytes; |
| 5406 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5407 | space_info->flags, num_bytes, 0); |
| 5408 | spin_unlock(&space_info->lock); |
| 5409 | } |
| 5410 | |
| 5411 | /* |
| 5412 | * This is for newly allocated space that isn't accounted in |
| 5413 | * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent |
| 5414 | * we use this helper. |
| 5415 | */ |
| 5416 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, |
| 5417 | struct btrfs_space_info *space_info, |
| 5418 | u64 num_bytes) |
| 5419 | { |
| 5420 | struct reserve_ticket *ticket; |
| 5421 | struct list_head *head = &space_info->priority_tickets; |
| 5422 | |
| 5423 | again: |
| 5424 | while (!list_empty(head) && num_bytes) { |
| 5425 | ticket = list_first_entry(head, struct reserve_ticket, |
| 5426 | list); |
| 5427 | if (num_bytes >= ticket->bytes) { |
| 5428 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5429 | space_info->flags, |
| 5430 | ticket->bytes, 1); |
| 5431 | list_del_init(&ticket->list); |
| 5432 | num_bytes -= ticket->bytes; |
| 5433 | space_info->bytes_may_use += ticket->bytes; |
| 5434 | ticket->bytes = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5435 | space_info->tickets_id++; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5436 | wake_up(&ticket->wait); |
| 5437 | } else { |
| 5438 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5439 | space_info->flags, |
| 5440 | num_bytes, 1); |
| 5441 | space_info->bytes_may_use += num_bytes; |
| 5442 | ticket->bytes -= num_bytes; |
| 5443 | num_bytes = 0; |
| 5444 | } |
| 5445 | } |
| 5446 | |
| 5447 | if (num_bytes && head == &space_info->priority_tickets) { |
| 5448 | head = &space_info->tickets; |
| 5449 | goto again; |
| 5450 | } |
| 5451 | } |
| 5452 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5453 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 5454 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 5455 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5456 | { |
| 5457 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 5458 | |
| 5459 | spin_lock(&block_rsv->lock); |
| 5460 | if (num_bytes == (u64)-1) |
| 5461 | num_bytes = block_rsv->size; |
| 5462 | block_rsv->size -= num_bytes; |
| 5463 | if (block_rsv->reserved >= block_rsv->size) { |
| 5464 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 5465 | block_rsv->reserved = block_rsv->size; |
| 5466 | block_rsv->full = 1; |
| 5467 | } else { |
| 5468 | num_bytes = 0; |
| 5469 | } |
| 5470 | spin_unlock(&block_rsv->lock); |
| 5471 | |
| 5472 | if (num_bytes > 0) { |
| 5473 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 5474 | spin_lock(&dest->lock); |
| 5475 | if (!dest->full) { |
| 5476 | u64 bytes_to_add; |
| 5477 | |
| 5478 | bytes_to_add = dest->size - dest->reserved; |
| 5479 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 5480 | dest->reserved += bytes_to_add; |
| 5481 | if (dest->reserved >= dest->size) |
| 5482 | dest->full = 1; |
| 5483 | num_bytes -= bytes_to_add; |
| 5484 | } |
| 5485 | spin_unlock(&dest->lock); |
| 5486 | } |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5487 | if (num_bytes) |
| 5488 | space_info_add_old_bytes(fs_info, space_info, |
| 5489 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5490 | } |
| 5491 | } |
| 5492 | |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5493 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src, |
| 5494 | struct btrfs_block_rsv *dst, u64 num_bytes, |
| 5495 | int update_size) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5496 | { |
| 5497 | int ret; |
| 5498 | |
| 5499 | ret = block_rsv_use_bytes(src, num_bytes); |
| 5500 | if (ret) |
| 5501 | return ret; |
| 5502 | |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5503 | block_rsv_add_bytes(dst, num_bytes, update_size); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5504 | return 0; |
| 5505 | } |
| 5506 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5507 | 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] | 5508 | { |
| 5509 | memset(rsv, 0, sizeof(*rsv)); |
| 5510 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5511 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5512 | } |
| 5513 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5514 | 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] | 5515 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5516 | { |
| 5517 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5518 | |
| 5519 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 5520 | if (!block_rsv) |
| 5521 | return NULL; |
| 5522 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5523 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5524 | block_rsv->space_info = __find_space_info(fs_info, |
| 5525 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5526 | return block_rsv; |
| 5527 | } |
| 5528 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5529 | void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5530 | struct btrfs_block_rsv *rsv) |
| 5531 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5532 | if (!rsv) |
| 5533 | return; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5534 | btrfs_block_rsv_release(fs_info, rsv, (u64)-1); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5535 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5536 | } |
| 5537 | |
Chris Mason | cdfb080 | 2015-04-06 18:17:00 -0700 | [diff] [blame] | 5538 | void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv) |
| 5539 | { |
| 5540 | kfree(rsv); |
| 5541 | } |
| 5542 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5543 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 5544 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, |
| 5545 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5546 | { |
| 5547 | int ret; |
| 5548 | |
| 5549 | if (num_bytes == 0) |
| 5550 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5551 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 5552 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5553 | if (!ret) { |
| 5554 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 5555 | return 0; |
| 5556 | } |
| 5557 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5558 | return ret; |
| 5559 | } |
| 5560 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5561 | 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] | 5562 | { |
| 5563 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5564 | int ret = -ENOSPC; |
| 5565 | |
| 5566 | if (!block_rsv) |
| 5567 | return 0; |
| 5568 | |
| 5569 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5570 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 5571 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5572 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5573 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5574 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5575 | return ret; |
| 5576 | } |
| 5577 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5578 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 5579 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, |
| 5580 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5581 | { |
| 5582 | u64 num_bytes = 0; |
| 5583 | int ret = -ENOSPC; |
| 5584 | |
| 5585 | if (!block_rsv) |
| 5586 | return 0; |
| 5587 | |
| 5588 | spin_lock(&block_rsv->lock); |
| 5589 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5590 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5591 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5592 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5593 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5594 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5595 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5596 | if (!ret) |
| 5597 | return 0; |
| 5598 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 5599 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5600 | if (!ret) { |
| 5601 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5602 | return 0; |
| 5603 | } |
| 5604 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5605 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5606 | } |
| 5607 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5608 | void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5609 | struct btrfs_block_rsv *block_rsv, |
| 5610 | u64 num_bytes) |
| 5611 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5612 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5613 | |
Liu Bo | 1750458 | 2013-12-29 21:44:50 +0800 | [diff] [blame] | 5614 | if (global_rsv == block_rsv || |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5615 | block_rsv->space_info != global_rsv->space_info) |
| 5616 | global_rsv = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5617 | block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5618 | } |
| 5619 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5620 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5621 | { |
| 5622 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 5623 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 5624 | u64 num_bytes; |
| 5625 | |
Josef Bacik | ae2e472 | 2016-05-27 12:58:35 -0400 | [diff] [blame] | 5626 | /* |
| 5627 | * The global block rsv is based on the size of the extent tree, the |
| 5628 | * checksum tree and the root tree. If the fs is empty we want to set |
| 5629 | * it to a minimal amount for safety. |
| 5630 | */ |
| 5631 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) + |
| 5632 | btrfs_root_used(&fs_info->csum_root->root_item) + |
| 5633 | btrfs_root_used(&fs_info->tree_root->root_item); |
| 5634 | num_bytes = max_t(u64, num_bytes, SZ_16M); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5635 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5636 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5637 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5638 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 5639 | block_rsv->size = min_t(u64, num_bytes, SZ_512M); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5640 | |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5641 | if (block_rsv->reserved < block_rsv->size) { |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 5642 | num_bytes = btrfs_space_info_used(sinfo, true); |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5643 | if (sinfo->total_bytes > num_bytes) { |
| 5644 | num_bytes = sinfo->total_bytes - num_bytes; |
| 5645 | num_bytes = min(num_bytes, |
| 5646 | block_rsv->size - block_rsv->reserved); |
| 5647 | block_rsv->reserved += num_bytes; |
| 5648 | sinfo->bytes_may_use += num_bytes; |
| 5649 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5650 | sinfo->flags, num_bytes, |
| 5651 | 1); |
| 5652 | } |
| 5653 | } else if (block_rsv->reserved > block_rsv->size) { |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5654 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5655 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5656 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5657 | sinfo->flags, num_bytes, 0); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5658 | block_rsv->reserved = block_rsv->size; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5659 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 5660 | |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5661 | if (block_rsv->reserved == block_rsv->size) |
| 5662 | block_rsv->full = 1; |
| 5663 | else |
| 5664 | block_rsv->full = 0; |
| 5665 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5666 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5667 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5668 | } |
| 5669 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5670 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5671 | { |
| 5672 | struct btrfs_space_info *space_info; |
| 5673 | |
| 5674 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 5675 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5676 | |
| 5677 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5678 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5679 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5680 | fs_info->trans_block_rsv.space_info = space_info; |
| 5681 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5682 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5683 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5684 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 5685 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 5686 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 5687 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Stefan Behrens | 3a6cad9 | 2013-05-16 14:48:19 +0000 | [diff] [blame] | 5688 | if (fs_info->quota_root) |
| 5689 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5690 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5691 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5692 | update_global_block_rsv(fs_info); |
| 5693 | } |
| 5694 | |
| 5695 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5696 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5697 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 5698 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5699 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 5700 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 5701 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 5702 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 5703 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 5704 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5705 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 5706 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5707 | } |
| 5708 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5709 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5710 | struct btrfs_fs_info *fs_info) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5711 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 5712 | if (!trans->block_rsv) |
| 5713 | return; |
| 5714 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5715 | if (!trans->bytes_reserved) |
| 5716 | return; |
| 5717 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5718 | trace_btrfs_space_reservation(fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5719 | trans->transid, trans->bytes_reserved, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5720 | btrfs_block_rsv_release(fs_info, trans->block_rsv, |
| 5721 | trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5722 | trans->bytes_reserved = 0; |
| 5723 | } |
| 5724 | |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5725 | /* |
| 5726 | * To be called after all the new block groups attached to the transaction |
| 5727 | * handle have been created (btrfs_create_pending_block_groups()). |
| 5728 | */ |
| 5729 | void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) |
| 5730 | { |
Jeff Mahoney | 64b6358 | 2016-06-20 17:23:41 -0400 | [diff] [blame] | 5731 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5732 | |
| 5733 | if (!trans->chunk_bytes_reserved) |
| 5734 | return; |
| 5735 | |
| 5736 | WARN_ON_ONCE(!list_empty(&trans->new_bgs)); |
| 5737 | |
| 5738 | block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL, |
| 5739 | trans->chunk_bytes_reserved); |
| 5740 | trans->chunk_bytes_reserved = 0; |
| 5741 | } |
| 5742 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5743 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5744 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 8ed7a2a | 2017-02-20 13:50:39 +0200 | [diff] [blame] | 5745 | struct btrfs_inode *inode) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5746 | { |
Nikolay Borisov | 8ed7a2a | 2017-02-20 13:50:39 +0200 | [diff] [blame] | 5747 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 5748 | struct btrfs_root *root = inode->root; |
Josef Bacik | 40acc3ee | 2016-05-27 13:01:08 -0400 | [diff] [blame] | 5749 | /* |
| 5750 | * We always use trans->block_rsv here as we will have reserved space |
| 5751 | * for our orphan when starting the transaction, using get_block_rsv() |
| 5752 | * here will sometimes make us choose the wrong block rsv as we could be |
| 5753 | * doing a reloc inode for a non refcounted root. |
| 5754 | */ |
| 5755 | struct btrfs_block_rsv *src_rsv = trans->block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5756 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 5757 | |
| 5758 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5759 | * We need to hold space in order to delete our orphan item once we've |
| 5760 | * added it, so this takes the reservation so we can release it later |
| 5761 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5762 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5763 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
| 5764 | |
Nikolay Borisov | 8ed7a2a | 2017-02-20 13:50:39 +0200 | [diff] [blame] | 5765 | trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode), |
| 5766 | num_bytes, 1); |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5767 | return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5768 | } |
| 5769 | |
Nikolay Borisov | 703b391 | 2017-02-20 13:50:40 +0200 | [diff] [blame] | 5770 | void btrfs_orphan_release_metadata(struct btrfs_inode *inode) |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5771 | { |
Nikolay Borisov | 703b391 | 2017-02-20 13:50:40 +0200 | [diff] [blame] | 5772 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 5773 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5774 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
| 5775 | |
Nikolay Borisov | 703b391 | 2017-02-20 13:50:40 +0200 | [diff] [blame] | 5776 | trace_btrfs_space_reservation(fs_info, "orphan", btrfs_ino(inode), |
| 5777 | num_bytes, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5778 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5779 | } |
| 5780 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5781 | /* |
| 5782 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation |
| 5783 | * root: the root of the parent directory |
| 5784 | * rsv: block reservation |
| 5785 | * items: the number of items that we need do reservation |
| 5786 | * qgroup_reserved: used to return the reserved size in qgroup |
| 5787 | * |
| 5788 | * This function is used to reserve the space for snapshot/subvolume |
| 5789 | * creation and deletion. Those operations are different with the |
| 5790 | * common file/directory operations, they change two fs/file trees |
| 5791 | * and root tree, the number of items that the qgroup reserves is |
| 5792 | * different with the free space reservation. So we can not use |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5793 | * the space reservation mechanism in start_transaction(). |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5794 | */ |
| 5795 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, |
| 5796 | struct btrfs_block_rsv *rsv, |
| 5797 | int items, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5798 | u64 *qgroup_reserved, |
| 5799 | bool use_global_rsv) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5800 | { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5801 | u64 num_bytes; |
| 5802 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5803 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5804 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5805 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5806 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5807 | /* One for parent inode, two for dir entries */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5808 | num_bytes = 3 * fs_info->nodesize; |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 5809 | ret = btrfs_qgroup_reserve_meta(root, num_bytes, true); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5810 | if (ret) |
| 5811 | return ret; |
| 5812 | } else { |
| 5813 | num_bytes = 0; |
| 5814 | } |
| 5815 | |
| 5816 | *qgroup_reserved = num_bytes; |
| 5817 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5818 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, items); |
| 5819 | rsv->space_info = __find_space_info(fs_info, |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5820 | BTRFS_BLOCK_GROUP_METADATA); |
| 5821 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, |
| 5822 | BTRFS_RESERVE_FLUSH_ALL); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5823 | |
| 5824 | if (ret == -ENOSPC && use_global_rsv) |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5825 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5826 | |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5827 | if (ret && *qgroup_reserved) |
| 5828 | btrfs_qgroup_free_meta(root, *qgroup_reserved); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5829 | |
| 5830 | return ret; |
| 5831 | } |
| 5832 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5833 | void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info, |
David Sterba | 7775c81 | 2017-02-10 19:18:18 +0100 | [diff] [blame] | 5834 | struct btrfs_block_rsv *rsv) |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5835 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5836 | btrfs_block_rsv_release(fs_info, rsv, (u64)-1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5837 | } |
| 5838 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5839 | /** |
| 5840 | * drop_outstanding_extent - drop an outstanding extent |
| 5841 | * @inode: the inode we're dropping the extent for |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5842 | * @num_bytes: the number of bytes we're releasing. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5843 | * |
| 5844 | * This is called when we are freeing up an outstanding extent, either called |
| 5845 | * after an error or after an extent is written. This will return the number of |
| 5846 | * reserved extents that need to be freed. This must be called with |
| 5847 | * BTRFS_I(inode)->lock held. |
| 5848 | */ |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5849 | static unsigned drop_outstanding_extent(struct btrfs_inode *inode, |
| 5850 | u64 num_bytes) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5851 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5852 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5853 | unsigned dropped_extents = 0; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5854 | unsigned num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5855 | |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5856 | num_extents = count_max_extents(num_bytes); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5857 | ASSERT(num_extents); |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5858 | ASSERT(inode->outstanding_extents >= num_extents); |
| 5859 | inode->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5860 | |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5861 | if (inode->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5862 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5863 | &inode->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5864 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5865 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5866 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5867 | * 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] | 5868 | * reserved then we need to leave the reserved extents count alone. |
| 5869 | */ |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5870 | if (inode->outstanding_extents >= inode->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5871 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5872 | |
Nikolay Borisov | baa3ba3 | 2017-02-20 13:50:37 +0200 | [diff] [blame] | 5873 | dropped_extents = inode->reserved_extents - inode->outstanding_extents; |
| 5874 | inode->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5875 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5878 | /** |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5879 | * calc_csum_metadata_size - return the amount of metadata space that must be |
| 5880 | * reserved/freed for the given bytes. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5881 | * @inode: the inode we're manipulating |
| 5882 | * @num_bytes: the number of bytes in question |
| 5883 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 5884 | * |
| 5885 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 5886 | * correct amount of metadata that must either be reserved or freed. We |
| 5887 | * calculate how many checksums we can fit into one leaf and then divide the |
| 5888 | * number of bytes that will need to be checksumed by this value to figure out |
| 5889 | * how many checksums will be required. If we are adding bytes then the number |
| 5890 | * may go up and we will return the number of additional bytes that must be |
| 5891 | * reserved. If it is going down we will return the number of bytes that must |
| 5892 | * be freed. |
| 5893 | * |
| 5894 | * This must be called with BTRFS_I(inode)->lock held. |
| 5895 | */ |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5896 | 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] | 5897 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5898 | { |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5899 | 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] | 5900 | u64 old_csums, num_csums; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5901 | |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5902 | if (inode->flags & BTRFS_INODE_NODATASUM && inode->csum_bytes == 0) |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5903 | return 0; |
| 5904 | |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5905 | old_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5906 | if (reserve) |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5907 | inode->csum_bytes += num_bytes; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5908 | else |
Nikolay Borisov | 0e6bf9b | 2017-02-20 13:50:38 +0200 | [diff] [blame] | 5909 | inode->csum_bytes -= num_bytes; |
| 5910 | num_csums = btrfs_csum_bytes_to_leaves(fs_info, inode->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5911 | |
| 5912 | /* No change, no need to reserve more */ |
| 5913 | if (old_csums == num_csums) |
| 5914 | return 0; |
| 5915 | |
| 5916 | if (reserve) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5917 | return btrfs_calc_trans_metadata_size(fs_info, |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5918 | num_csums - old_csums); |
| 5919 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5920 | return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5921 | } |
| 5922 | |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5923 | int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5924 | { |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5925 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb); |
| 5926 | struct btrfs_root *root = inode->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5927 | struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5928 | u64 to_reserve = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5929 | u64 csum_bytes; |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5930 | unsigned nr_extents; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5931 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; |
Jan Schmidt | eb6b88d | 2013-01-27 23:26:00 -0700 | [diff] [blame] | 5932 | int ret = 0; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5933 | bool delalloc_lock = true; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5934 | u64 to_free = 0; |
| 5935 | unsigned dropped; |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5936 | bool release_extra = false; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5937 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5938 | /* If we are a free space inode we need to not flush since we will be in |
| 5939 | * the middle of a transaction commit. We also don't need the delalloc |
| 5940 | * mutex since we won't race with anybody. We need this mostly to make |
| 5941 | * lockdep shut its filthy mouth. |
Josef Bacik | bac357dc | 2016-07-20 16:48:45 -0700 | [diff] [blame] | 5942 | * |
| 5943 | * If we have a transaction open (can happen if we call truncate_block |
| 5944 | * from truncate), then we need FLUSH_LIMIT so we don't deadlock. |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5945 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5946 | if (btrfs_is_free_space_inode(inode)) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5947 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5948 | delalloc_lock = false; |
Josef Bacik | bac357dc | 2016-07-20 16:48:45 -0700 | [diff] [blame] | 5949 | } else if (current->journal_info) { |
| 5950 | flush = BTRFS_RESERVE_FLUSH_LIMIT; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5951 | } |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 5952 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5953 | if (flush != BTRFS_RESERVE_NO_FLUSH && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5954 | btrfs_transaction_in_commit(fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5955 | schedule_timeout(1); |
| 5956 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5957 | if (delalloc_lock) |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5958 | mutex_lock(&inode->delalloc_mutex); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5959 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5960 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5961 | |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5962 | spin_lock(&inode->lock); |
David Sterba | 823bb20 | 2017-01-04 11:09:51 +0100 | [diff] [blame] | 5963 | nr_extents = count_max_extents(num_bytes); |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5964 | inode->outstanding_extents += nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5965 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5966 | nr_extents = 0; |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5967 | if (inode->outstanding_extents > inode->reserved_extents) |
| 5968 | nr_extents += inode->outstanding_extents - |
| 5969 | inode->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5970 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5971 | /* We always want to reserve a slot for updating the inode. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5972 | to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1); |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5973 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
| 5974 | csum_bytes = inode->csum_bytes; |
| 5975 | spin_unlock(&inode->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5976 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5977 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5978 | ret = btrfs_qgroup_reserve_meta(root, |
Jeff Mahoney | 003d7c5 | 2017-01-25 09:50:33 -0500 | [diff] [blame] | 5979 | nr_extents * fs_info->nodesize, true); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5980 | if (ret) |
| 5981 | goto out_fail; |
Wang Shilong | a9870c0 | 2013-03-01 11:33:01 +0000 | [diff] [blame] | 5982 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5983 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5984 | ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5985 | if (unlikely(ret)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5986 | btrfs_qgroup_free_meta(root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5987 | nr_extents * fs_info->nodesize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5988 | goto out_fail; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5989 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5990 | |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5991 | spin_lock(&inode->lock); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5992 | if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5993 | &inode->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5994 | to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5995 | release_extra = true; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5996 | } |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 5997 | inode->reserved_extents += nr_extents; |
| 5998 | spin_unlock(&inode->lock); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5999 | |
| 6000 | if (delalloc_lock) |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6001 | mutex_unlock(&inode->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 6002 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6003 | if (to_reserve) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6004 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6005 | btrfs_ino(inode), to_reserve, 1); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 6006 | if (release_extra) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6007 | btrfs_block_rsv_release(fs_info, block_rsv, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6008 | btrfs_calc_trans_metadata_size(fs_info, 1)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6009 | return 0; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6010 | |
| 6011 | out_fail: |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6012 | spin_lock(&inode->lock); |
| 6013 | dropped = drop_outstanding_extent(inode, num_bytes); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6014 | /* |
| 6015 | * If the inodes csum_bytes is the same as the original |
| 6016 | * csum_bytes then we know we haven't raced with any free()ers |
| 6017 | * so we can just reduce our inodes csum bytes and carry on. |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6018 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6019 | if (inode->csum_bytes == csum_bytes) { |
| 6020 | calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6021 | } else { |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6022 | u64 orig_csum_bytes = inode->csum_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6023 | u64 bytes; |
| 6024 | |
| 6025 | /* |
| 6026 | * 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] | 6027 | * freed from any free-ers that occurred during this |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6028 | * reservation, so we reset ->csum_bytes to the csum_bytes |
| 6029 | * before we dropped our lock, and then call the free for the |
| 6030 | * number of bytes that were freed while we were trying our |
| 6031 | * reservation. |
| 6032 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6033 | bytes = csum_bytes - inode->csum_bytes; |
| 6034 | inode->csum_bytes = csum_bytes; |
| 6035 | to_free = calc_csum_metadata_size(inode, bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6036 | |
| 6037 | |
| 6038 | /* |
| 6039 | * Now we need to see how much we would have freed had we not |
| 6040 | * been making this reservation and our ->csum_bytes were not |
| 6041 | * artificially inflated. |
| 6042 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6043 | inode->csum_bytes = csum_bytes - num_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6044 | bytes = csum_bytes - orig_csum_bytes; |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6045 | bytes = calc_csum_metadata_size(inode, bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6046 | |
| 6047 | /* |
| 6048 | * 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] | 6049 | * 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] | 6050 | * not had an artificially high ->csum_bytes, so we need to free |
| 6051 | * the remainder. If bytes is the same or less then we don't |
| 6052 | * need to do anything, the other free-ers did the correct |
| 6053 | * thing. |
| 6054 | */ |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6055 | inode->csum_bytes = orig_csum_bytes - num_bytes; |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6056 | if (bytes > to_free) |
| 6057 | to_free = bytes - to_free; |
| 6058 | else |
| 6059 | to_free = 0; |
| 6060 | } |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6061 | spin_unlock(&inode->lock); |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 6062 | if (dropped) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6063 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6064 | |
| 6065 | if (to_free) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6066 | btrfs_block_rsv_release(fs_info, block_rsv, to_free); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6067 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6068 | btrfs_ino(inode), to_free, 0); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6069 | } |
| 6070 | if (delalloc_lock) |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6071 | mutex_unlock(&inode->delalloc_mutex); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6072 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6073 | } |
| 6074 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6075 | /** |
| 6076 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 6077 | * @inode: the inode to release the reservation for |
| 6078 | * @num_bytes: the number of bytes we're releasing |
| 6079 | * |
| 6080 | * This will release the metadata reservation for an inode. This can be called |
| 6081 | * once we complete IO for a given set of bytes to release their metadata |
| 6082 | * reservations. |
| 6083 | */ |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame^] | 6084 | void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6085 | { |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame^] | 6086 | 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] | 6087 | u64 to_free = 0; |
| 6088 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6089 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6090 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame^] | 6091 | spin_lock(&inode->lock); |
| 6092 | dropped = drop_outstanding_extent(inode, num_bytes); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6093 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6094 | if (num_bytes) |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame^] | 6095 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
| 6096 | spin_unlock(&inode->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6097 | if (dropped > 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6098 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6099 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6100 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 6101 | return; |
| 6102 | |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame^] | 6103 | trace_btrfs_space_reservation(fs_info, "delalloc", btrfs_ino(inode), |
| 6104 | to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 6105 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6106 | 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] | 6107 | } |
| 6108 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6109 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6110 | * btrfs_delalloc_reserve_space - reserve data and metadata space for |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6111 | * delalloc |
| 6112 | * @inode: inode we're writing to |
| 6113 | * @start: start range we are writing to |
| 6114 | * @len: how long the range we are writing to |
| 6115 | * |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6116 | * This will do the following things |
| 6117 | * |
| 6118 | * o reserve space in data space info for num bytes |
| 6119 | * and reserve precious corresponding qgroup space |
| 6120 | * (Done in check_data_free_space) |
| 6121 | * |
| 6122 | * o reserve space for metadata space, based on the number of outstanding |
| 6123 | * extents and how much csums will be needed |
| 6124 | * also reserve metadata space in a per root over-reserve method. |
| 6125 | * o add to the inodes->delalloc_bytes |
| 6126 | * o add it to the fs_info's delalloc inodes list. |
| 6127 | * (Above 3 all done in delalloc_reserve_metadata) |
| 6128 | * |
| 6129 | * Return 0 for success |
| 6130 | * Return <0 for error(-ENOSPC or -EQUOT) |
| 6131 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6132 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6133 | { |
| 6134 | int ret; |
| 6135 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6136 | ret = btrfs_check_data_free_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6137 | if (ret < 0) |
| 6138 | return ret; |
Nikolay Borisov | 9f3db42 | 2017-02-20 13:50:41 +0200 | [diff] [blame] | 6139 | ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6140 | if (ret < 0) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6141 | btrfs_free_reserved_data_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6142 | return ret; |
| 6143 | } |
| 6144 | |
| 6145 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6146 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6147 | * @inode: inode we're releasing space for |
| 6148 | * @start: start position of the space already reserved |
| 6149 | * @len: the len of the space already reserved |
| 6150 | * |
| 6151 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 6152 | * called in the case that we don't need the metadata AND data reservations |
| 6153 | * anymore. So if there is an error or we insert an inline extent. |
| 6154 | * |
| 6155 | * This function will release the metadata space that was not used and will |
| 6156 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 6157 | * list if there are no delalloc bytes left. |
| 6158 | * Also it will handle the qgroup reserved space. |
| 6159 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6160 | void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6161 | { |
Nikolay Borisov | 691fa05 | 2017-02-20 13:50:42 +0200 | [diff] [blame^] | 6162 | btrfs_delalloc_release_metadata(BTRFS_I(inode), len); |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6163 | btrfs_free_reserved_data_space(inode, start, len); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6164 | } |
| 6165 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6166 | static int update_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 6167 | struct btrfs_fs_info *info, u64 bytenr, |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6168 | u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6169 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6170 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6171 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6172 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6173 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6174 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 6175 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6176 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 6177 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6178 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6179 | if (alloc) |
| 6180 | old_val += num_bytes; |
| 6181 | else |
| 6182 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6183 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 6184 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6185 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6186 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6187 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6188 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6189 | return -ENOENT; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6190 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 6191 | BTRFS_BLOCK_GROUP_RAID1 | |
| 6192 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6193 | factor = 2; |
| 6194 | else |
| 6195 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 6196 | /* |
| 6197 | * If this block group has free space cache written out, we |
| 6198 | * need to make sure to load it if we are removing space. This |
| 6199 | * is because we need the unpinning stage to actually add the |
| 6200 | * space back to the block group, otherwise we will leak space. |
| 6201 | */ |
| 6202 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6203 | cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6204 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6205 | byte_in_group = bytenr - cache->key.objectid; |
| 6206 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6207 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6208 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6209 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6210 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 6211 | if (btrfs_test_opt(info, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6212 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 6213 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 6214 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6215 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6216 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6217 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6218 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6219 | btrfs_set_block_group_used(&cache->item, old_val); |
| 6220 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6221 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6222 | cache->space_info->bytes_used += num_bytes; |
| 6223 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6224 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6225 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6226 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6227 | old_val -= num_bytes; |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 6228 | btrfs_set_block_group_used(&cache->item, old_val); |
| 6229 | cache->pinned += num_bytes; |
| 6230 | cache->space_info->bytes_pinned += num_bytes; |
| 6231 | cache->space_info->bytes_used -= num_bytes; |
| 6232 | cache->space_info->disk_used -= num_bytes * factor; |
| 6233 | spin_unlock(&cache->lock); |
| 6234 | spin_unlock(&cache->space_info->lock); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 6235 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6236 | trace_btrfs_space_reservation(info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6237 | cache->space_info->flags, |
| 6238 | num_bytes, 1); |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 6239 | set_extent_dirty(info->pinned_extents, |
| 6240 | bytenr, bytenr + num_bytes - 1, |
| 6241 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6242 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 6243 | |
| 6244 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 6245 | if (list_empty(&cache->dirty_list)) { |
| 6246 | list_add_tail(&cache->dirty_list, |
| 6247 | &trans->transaction->dirty_bgs); |
| 6248 | trans->transaction->num_dirty_bgs++; |
| 6249 | btrfs_get_block_group(cache); |
| 6250 | } |
| 6251 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 6252 | |
Filipe Manana | 036a934 | 2015-11-23 15:25:16 +0000 | [diff] [blame] | 6253 | /* |
| 6254 | * No longer have used bytes in this block group, queue it for |
| 6255 | * deletion. We do this after adding the block group to the |
| 6256 | * dirty list to avoid races between cleaner kthread and space |
| 6257 | * cache writeout. |
| 6258 | */ |
| 6259 | if (!alloc && old_val == 0) { |
| 6260 | spin_lock(&info->unused_bgs_lock); |
| 6261 | if (list_empty(&cache->bg_list)) { |
| 6262 | btrfs_get_block_group(cache); |
| 6263 | list_add_tail(&cache->bg_list, |
| 6264 | &info->unused_bgs); |
| 6265 | } |
| 6266 | spin_unlock(&info->unused_bgs_lock); |
| 6267 | } |
| 6268 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6269 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6270 | total -= num_bytes; |
| 6271 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6272 | } |
| 6273 | return 0; |
| 6274 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6275 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6276 | 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] | 6277 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6278 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6279 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6280 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6281 | spin_lock(&fs_info->block_group_cache_lock); |
| 6282 | bytenr = fs_info->first_logical_byte; |
| 6283 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 6284 | |
| 6285 | if (bytenr < (u64)-1) |
| 6286 | return bytenr; |
| 6287 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6288 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6289 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6290 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6291 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6292 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6293 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6294 | |
| 6295 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6296 | } |
| 6297 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6298 | static int pin_down_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6299 | struct btrfs_block_group_cache *cache, |
| 6300 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6301 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6302 | spin_lock(&cache->space_info->lock); |
| 6303 | spin_lock(&cache->lock); |
| 6304 | cache->pinned += num_bytes; |
| 6305 | cache->space_info->bytes_pinned += num_bytes; |
| 6306 | if (reserved) { |
| 6307 | cache->reserved -= num_bytes; |
| 6308 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6309 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6310 | spin_unlock(&cache->lock); |
| 6311 | spin_unlock(&cache->space_info->lock); |
| 6312 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6313 | trace_btrfs_space_reservation(fs_info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6314 | cache->space_info->flags, num_bytes, 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6315 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6316 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6317 | return 0; |
| 6318 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6319 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6320 | /* |
| 6321 | * this function must be called within transaction |
| 6322 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6323 | int btrfs_pin_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6324 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6325 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6326 | struct btrfs_block_group_cache *cache; |
| 6327 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6328 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6329 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6330 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6331 | pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6332 | |
| 6333 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6334 | return 0; |
| 6335 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6336 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6337 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6338 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6339 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6340 | 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] | 6341 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6342 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6343 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6344 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6345 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6346 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6347 | if (!cache) |
| 6348 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6349 | |
| 6350 | /* |
| 6351 | * pull in the free space cache (if any) so that our pin |
| 6352 | * removes the free space from the cache. We have load_only set |
| 6353 | * to one because the slow code to read in the free extents does check |
| 6354 | * the pinned extents. |
| 6355 | */ |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6356 | cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6357 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6358 | pin_down_extent(fs_info, cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6359 | |
| 6360 | /* 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] | 6361 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6362 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6363 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6364 | } |
| 6365 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6366 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 6367 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6368 | { |
| 6369 | int ret; |
| 6370 | struct btrfs_block_group_cache *block_group; |
| 6371 | struct btrfs_caching_control *caching_ctl; |
| 6372 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6373 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6374 | if (!block_group) |
| 6375 | return -EINVAL; |
| 6376 | |
| 6377 | cache_block_group(block_group, 0); |
| 6378 | caching_ctl = get_caching_control(block_group); |
| 6379 | |
| 6380 | if (!caching_ctl) { |
| 6381 | /* Logic error */ |
| 6382 | BUG_ON(!block_group_cache_done(block_group)); |
| 6383 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 6384 | } else { |
| 6385 | mutex_lock(&caching_ctl->mutex); |
| 6386 | |
| 6387 | if (start >= caching_ctl->progress) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6388 | ret = add_excluded_extent(fs_info, start, num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6389 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 6390 | ret = btrfs_remove_free_space(block_group, |
| 6391 | start, num_bytes); |
| 6392 | } else { |
| 6393 | num_bytes = caching_ctl->progress - start; |
| 6394 | ret = btrfs_remove_free_space(block_group, |
| 6395 | start, num_bytes); |
| 6396 | if (ret) |
| 6397 | goto out_lock; |
| 6398 | |
| 6399 | num_bytes = (start + num_bytes) - |
| 6400 | caching_ctl->progress; |
| 6401 | start = caching_ctl->progress; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6402 | ret = add_excluded_extent(fs_info, start, num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6403 | } |
| 6404 | out_lock: |
| 6405 | mutex_unlock(&caching_ctl->mutex); |
| 6406 | put_caching_control(caching_ctl); |
| 6407 | } |
| 6408 | btrfs_put_block_group(block_group); |
| 6409 | return ret; |
| 6410 | } |
| 6411 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6412 | int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info, |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6413 | struct extent_buffer *eb) |
| 6414 | { |
| 6415 | struct btrfs_file_extent_item *item; |
| 6416 | struct btrfs_key key; |
| 6417 | int found_type; |
| 6418 | int i; |
| 6419 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6420 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6421 | return 0; |
| 6422 | |
| 6423 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 6424 | btrfs_item_key_to_cpu(eb, &key, i); |
| 6425 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 6426 | continue; |
| 6427 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 6428 | found_type = btrfs_file_extent_type(eb, item); |
| 6429 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 6430 | continue; |
| 6431 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 6432 | continue; |
| 6433 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 6434 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6435 | __exclude_logged_extent(fs_info, key.objectid, key.offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6436 | } |
| 6437 | |
| 6438 | return 0; |
| 6439 | } |
| 6440 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 6441 | static void |
| 6442 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 6443 | { |
| 6444 | atomic_inc(&bg->reservations); |
| 6445 | } |
| 6446 | |
| 6447 | void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, |
| 6448 | const u64 start) |
| 6449 | { |
| 6450 | struct btrfs_block_group_cache *bg; |
| 6451 | |
| 6452 | bg = btrfs_lookup_block_group(fs_info, start); |
| 6453 | ASSERT(bg); |
| 6454 | if (atomic_dec_and_test(&bg->reservations)) |
| 6455 | wake_up_atomic_t(&bg->reservations); |
| 6456 | btrfs_put_block_group(bg); |
| 6457 | } |
| 6458 | |
| 6459 | static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a) |
| 6460 | { |
| 6461 | schedule(); |
| 6462 | return 0; |
| 6463 | } |
| 6464 | |
| 6465 | void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 6466 | { |
| 6467 | struct btrfs_space_info *space_info = bg->space_info; |
| 6468 | |
| 6469 | ASSERT(bg->ro); |
| 6470 | |
| 6471 | if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 6472 | return; |
| 6473 | |
| 6474 | /* |
| 6475 | * Our block group is read only but before we set it to read only, |
| 6476 | * some task might have had allocated an extent from it already, but it |
| 6477 | * has not yet created a respective ordered extent (and added it to a |
| 6478 | * root's list of ordered extents). |
| 6479 | * Therefore wait for any task currently allocating extents, since the |
| 6480 | * block group's reservations counter is incremented while a read lock |
| 6481 | * on the groups' semaphore is held and decremented after releasing |
| 6482 | * the read access on that semaphore and creating the ordered extent. |
| 6483 | */ |
| 6484 | down_write(&space_info->groups_sem); |
| 6485 | up_write(&space_info->groups_sem); |
| 6486 | |
| 6487 | wait_on_atomic_t(&bg->reservations, |
| 6488 | btrfs_wait_bg_reservations_atomic_t, |
| 6489 | TASK_UNINTERRUPTIBLE); |
| 6490 | } |
| 6491 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6492 | /** |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6493 | * btrfs_add_reserved_bytes - update the block_group and space info counters |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6494 | * @cache: The cache we are manipulating |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6495 | * @ram_bytes: The number of bytes of file content, and will be same to |
| 6496 | * @num_bytes except for the compress path. |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6497 | * @num_bytes: The number of bytes in question |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6498 | * @delalloc: The blocks are allocated for the delalloc write |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6499 | * |
Xiaoguang Wang | 745699e | 2016-09-23 12:38:50 +0800 | [diff] [blame] | 6500 | * This is called by the allocator when it reserves space. If this is a |
| 6501 | * reservation and the block group has become read only we cannot make the |
| 6502 | * reservation and return -EAGAIN, otherwise this function always succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6503 | */ |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6504 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6505 | u64 ram_bytes, u64 num_bytes, int delalloc) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6506 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6507 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6508 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6509 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6510 | spin_lock(&space_info->lock); |
| 6511 | spin_lock(&cache->lock); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6512 | if (cache->ro) { |
| 6513 | ret = -EAGAIN; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6514 | } else { |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6515 | cache->reserved += num_bytes; |
| 6516 | space_info->bytes_reserved += num_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6517 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6518 | trace_btrfs_space_reservation(cache->fs_info, |
| 6519 | "space_info", space_info->flags, |
| 6520 | ram_bytes, 0); |
| 6521 | space_info->bytes_may_use -= ram_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6522 | if (delalloc) |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6523 | cache->delalloc_bytes += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6524 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6525 | spin_unlock(&cache->lock); |
| 6526 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6527 | return ret; |
| 6528 | } |
| 6529 | |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6530 | /** |
| 6531 | * btrfs_free_reserved_bytes - update the block_group and space info counters |
| 6532 | * @cache: The cache we are manipulating |
| 6533 | * @num_bytes: The number of bytes in question |
| 6534 | * @delalloc: The blocks are allocated for the delalloc write |
| 6535 | * |
| 6536 | * This is called by somebody who is freeing space that was never actually used |
| 6537 | * on disk. For example if you reserve some space for a new leaf in transaction |
| 6538 | * A and before transaction A commits you free that leaf, you call this with |
| 6539 | * reserve set to 0 in order to clear the reservation. |
| 6540 | */ |
| 6541 | |
| 6542 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 6543 | u64 num_bytes, int delalloc) |
| 6544 | { |
| 6545 | struct btrfs_space_info *space_info = cache->space_info; |
| 6546 | int ret = 0; |
| 6547 | |
| 6548 | spin_lock(&space_info->lock); |
| 6549 | spin_lock(&cache->lock); |
| 6550 | if (cache->ro) |
| 6551 | space_info->bytes_readonly += num_bytes; |
| 6552 | cache->reserved -= num_bytes; |
| 6553 | space_info->bytes_reserved -= num_bytes; |
| 6554 | |
| 6555 | if (delalloc) |
| 6556 | cache->delalloc_bytes -= num_bytes; |
| 6557 | spin_unlock(&cache->lock); |
| 6558 | spin_unlock(&space_info->lock); |
| 6559 | return ret; |
| 6560 | } |
David Sterba | 8b74c03 | 2017-02-10 19:20:56 +0100 | [diff] [blame] | 6561 | void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6562 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6563 | struct btrfs_caching_control *next; |
| 6564 | struct btrfs_caching_control *caching_ctl; |
| 6565 | struct btrfs_block_group_cache *cache; |
| 6566 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6567 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6568 | |
| 6569 | list_for_each_entry_safe(caching_ctl, next, |
| 6570 | &fs_info->caching_block_groups, list) { |
| 6571 | cache = caching_ctl->block_group; |
| 6572 | if (block_group_cache_done(cache)) { |
| 6573 | cache->last_byte_to_unpin = (u64)-1; |
| 6574 | list_del_init(&caching_ctl->list); |
| 6575 | put_caching_control(caching_ctl); |
| 6576 | } else { |
| 6577 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 6578 | } |
| 6579 | } |
| 6580 | |
| 6581 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6582 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 6583 | else |
| 6584 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 6585 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6586 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6587 | |
| 6588 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6589 | } |
| 6590 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6591 | /* |
| 6592 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 6593 | * what it should be based on the mount options. |
| 6594 | */ |
| 6595 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6596 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 6597 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6598 | { |
| 6599 | struct btrfs_free_cluster *ret = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6600 | bool ssd = btrfs_test_opt(fs_info, SSD); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6601 | |
| 6602 | *empty_cluster = 0; |
| 6603 | if (btrfs_mixed_space_info(space_info)) |
| 6604 | return ret; |
| 6605 | |
| 6606 | if (ssd) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 6607 | *empty_cluster = SZ_2M; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6608 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6609 | ret = &fs_info->meta_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6610 | if (!ssd) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 6611 | *empty_cluster = SZ_64K; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6612 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6613 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6614 | } |
| 6615 | |
| 6616 | return ret; |
| 6617 | } |
| 6618 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6619 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 6620 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6621 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6622 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6623 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6624 | struct btrfs_space_info *space_info; |
| 6625 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6626 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6627 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6628 | u64 total_unpinned = 0; |
| 6629 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6630 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6631 | |
| 6632 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6633 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6634 | if (!cache || |
| 6635 | start >= cache->key.objectid + cache->key.offset) { |
| 6636 | if (cache) |
| 6637 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6638 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6639 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6640 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6641 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6642 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6643 | cache->space_info, |
| 6644 | &empty_cluster); |
| 6645 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6646 | } |
| 6647 | |
| 6648 | len = cache->key.objectid + cache->key.offset - start; |
| 6649 | len = min(len, end + 1 - start); |
| 6650 | |
| 6651 | if (start < cache->last_byte_to_unpin) { |
| 6652 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6653 | if (return_free_space) |
| 6654 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6655 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6656 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6657 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6658 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6659 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6660 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6661 | /* |
| 6662 | * If this space cluster has been marked as fragmented and we've |
| 6663 | * unpinned enough in this block group to potentially allow a |
| 6664 | * cluster to be created inside of it go ahead and clear the |
| 6665 | * fragmented check. |
| 6666 | */ |
| 6667 | if (cluster && cluster->fragmented && |
| 6668 | total_unpinned > empty_cluster) { |
| 6669 | spin_lock(&cluster->lock); |
| 6670 | cluster->fragmented = 0; |
| 6671 | spin_unlock(&cluster->lock); |
| 6672 | } |
| 6673 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6674 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6675 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6676 | cache->pinned -= len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6677 | space_info->bytes_pinned -= len; |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6678 | |
| 6679 | trace_btrfs_space_reservation(fs_info, "pinned", |
| 6680 | space_info->flags, len, 0); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 6681 | space_info->max_extent_size = 0; |
Liu Bo | d288db5 | 2014-07-02 16:58:01 +0800 | [diff] [blame] | 6682 | percpu_counter_add(&space_info->total_bytes_pinned, -len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6683 | if (cache->ro) { |
| 6684 | space_info->bytes_readonly += len; |
| 6685 | readonly = true; |
| 6686 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6687 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6688 | if (!readonly && return_free_space && |
| 6689 | global_rsv->space_info == space_info) { |
| 6690 | u64 to_add = len; |
| 6691 | WARN_ON(!return_free_space); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6692 | spin_lock(&global_rsv->lock); |
| 6693 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6694 | to_add = min(len, global_rsv->size - |
| 6695 | global_rsv->reserved); |
| 6696 | global_rsv->reserved += to_add; |
| 6697 | space_info->bytes_may_use += to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6698 | if (global_rsv->reserved >= global_rsv->size) |
| 6699 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6700 | trace_btrfs_space_reservation(fs_info, |
| 6701 | "space_info", |
| 6702 | space_info->flags, |
| 6703 | to_add, 1); |
| 6704 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6705 | } |
| 6706 | spin_unlock(&global_rsv->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6707 | /* Add to any tickets we may have */ |
| 6708 | if (len) |
| 6709 | space_info_add_new_bytes(fs_info, space_info, |
| 6710 | len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6711 | } |
| 6712 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6716 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 6717 | return 0; |
| 6718 | } |
| 6719 | |
| 6720 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6721 | struct btrfs_fs_info *fs_info) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6722 | { |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6723 | struct btrfs_block_group_cache *block_group, *tmp; |
| 6724 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6725 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6726 | u64 start; |
| 6727 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6728 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6729 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6730 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6731 | unpin = &fs_info->freed_extents[1]; |
| 6732 | else |
| 6733 | unpin = &fs_info->freed_extents[0]; |
| 6734 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6735 | while (!trans->aborted) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6736 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6737 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 6738 | EXTENT_DIRTY, NULL); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6739 | if (ret) { |
| 6740 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6741 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6742 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6743 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6744 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6745 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 6746 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6747 | |
David Sterba | af6f8f6 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 6748 | clear_extent_dirty(unpin, start, end); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6749 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6750 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6751 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6752 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6753 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6754 | /* |
| 6755 | * Transaction is finished. We don't need the lock anymore. We |
| 6756 | * do need to clean up the block groups in case of a transaction |
| 6757 | * abort. |
| 6758 | */ |
| 6759 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 6760 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 6761 | u64 trimmed = 0; |
| 6762 | |
| 6763 | ret = -EROFS; |
| 6764 | if (!trans->aborted) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6765 | ret = btrfs_discard_extent(fs_info, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6766 | block_group->key.objectid, |
| 6767 | block_group->key.offset, |
| 6768 | &trimmed); |
| 6769 | |
| 6770 | list_del_init(&block_group->bg_list); |
| 6771 | btrfs_put_block_group_trimming(block_group); |
| 6772 | btrfs_put_block_group(block_group); |
| 6773 | |
| 6774 | if (ret) { |
| 6775 | const char *errstr = btrfs_decode_error(ret); |
| 6776 | btrfs_warn(fs_info, |
| 6777 | "Discard failed while removing blockgroup: errno=%d %s\n", |
| 6778 | ret, errstr); |
| 6779 | } |
| 6780 | } |
| 6781 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 6782 | return 0; |
| 6783 | } |
| 6784 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6785 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
| 6786 | u64 owner, u64 root_objectid) |
| 6787 | { |
| 6788 | struct btrfs_space_info *space_info; |
| 6789 | u64 flags; |
| 6790 | |
| 6791 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 6792 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) |
| 6793 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 6794 | else |
| 6795 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 6796 | } else { |
| 6797 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 6798 | } |
| 6799 | |
| 6800 | space_info = __find_space_info(fs_info, flags); |
| 6801 | BUG_ON(!space_info); /* Logic bug */ |
| 6802 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); |
| 6803 | } |
| 6804 | |
| 6805 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6806 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6807 | struct btrfs_fs_info *info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6808 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6809 | u64 root_objectid, u64 owner_objectid, |
| 6810 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6811 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6812 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6813 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6814 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 6815 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6816 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6817 | struct btrfs_extent_item *ei; |
| 6818 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6819 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6820 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6821 | int extent_slot = 0; |
| 6822 | int found_extent = 0; |
| 6823 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6824 | u32 item_size; |
| 6825 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6826 | u64 bytenr = node->bytenr; |
| 6827 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6828 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6829 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 6830 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6831 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6832 | if (!path) |
| 6833 | return -ENOMEM; |
| 6834 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6835 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6836 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6837 | |
| 6838 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 6839 | BUG_ON(!is_data && refs_to_drop != 1); |
| 6840 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6841 | if (is_data) |
| 6842 | skinny_metadata = 0; |
| 6843 | |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 6844 | ret = lookup_extent_backref(trans, info, path, &iref, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6845 | bytenr, num_bytes, parent, |
| 6846 | root_objectid, owner_objectid, |
| 6847 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6848 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6849 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6850 | while (extent_slot >= 0) { |
| 6851 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6852 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6853 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6854 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6855 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6856 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6857 | found_extent = 1; |
| 6858 | break; |
| 6859 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6860 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 6861 | key.offset == owner_objectid) { |
| 6862 | found_extent = 1; |
| 6863 | break; |
| 6864 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6865 | if (path->slots[0] - extent_slot > 5) |
| 6866 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6867 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6868 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6869 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6870 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 6871 | if (found_extent && item_size < sizeof(*ei)) |
| 6872 | found_extent = 0; |
| 6873 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6874 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6875 | BUG_ON(iref); |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 6876 | ret = remove_extent_backref(trans, info, path, NULL, |
| 6877 | refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6878 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6879 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6880 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6881 | goto out; |
| 6882 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6883 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6884 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6885 | |
| 6886 | key.objectid = bytenr; |
| 6887 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6888 | key.offset = num_bytes; |
| 6889 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6890 | if (!is_data && skinny_metadata) { |
| 6891 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 6892 | key.offset = owner_objectid; |
| 6893 | } |
| 6894 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6895 | ret = btrfs_search_slot(trans, extent_root, |
| 6896 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6897 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 6898 | /* |
| 6899 | * Couldn't find our skinny metadata item, |
| 6900 | * see if we have ye olde extent item. |
| 6901 | */ |
| 6902 | path->slots[0]--; |
| 6903 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 6904 | path->slots[0]); |
| 6905 | if (key.objectid == bytenr && |
| 6906 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6907 | key.offset == num_bytes) |
| 6908 | ret = 0; |
| 6909 | } |
| 6910 | |
| 6911 | if (ret > 0 && skinny_metadata) { |
| 6912 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 6913 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6914 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6915 | key.offset = num_bytes; |
| 6916 | btrfs_release_path(path); |
| 6917 | ret = btrfs_search_slot(trans, extent_root, |
| 6918 | &key, path, -1, 1); |
| 6919 | } |
| 6920 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6921 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6922 | btrfs_err(info, |
| 6923 | "umm, got %d back from search, was looking for %llu", |
| 6924 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 6925 | if (ret > 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6926 | btrfs_print_leaf(info, path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6927 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6928 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6929 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6930 | goto out; |
| 6931 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6932 | extent_slot = path->slots[0]; |
| 6933 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6934 | } else if (WARN_ON(ret == -ENOENT)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6935 | btrfs_print_leaf(info, path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6936 | btrfs_err(info, |
| 6937 | "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] | 6938 | bytenr, parent, root_objectid, owner_objectid, |
| 6939 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6940 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 6941 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6942 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6943 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6944 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6945 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6946 | |
| 6947 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6948 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6949 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6950 | if (item_size < sizeof(*ei)) { |
| 6951 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 6952 | ret = convert_extent_item_v0(trans, info, path, owner_objectid, |
| 6953 | 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6954 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6955 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6956 | goto out; |
| 6957 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6958 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6959 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6960 | path->leave_spinning = 1; |
| 6961 | |
| 6962 | key.objectid = bytenr; |
| 6963 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6964 | key.offset = num_bytes; |
| 6965 | |
| 6966 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 6967 | -1, 1); |
| 6968 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6969 | btrfs_err(info, |
| 6970 | "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6971 | ret, bytenr); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6972 | btrfs_print_leaf(info, path->nodes[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6973 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6974 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6975 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6976 | goto out; |
| 6977 | } |
| 6978 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6979 | extent_slot = path->slots[0]; |
| 6980 | leaf = path->nodes[0]; |
| 6981 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6982 | } |
| 6983 | #endif |
| 6984 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6985 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 6986 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6987 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 6988 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6989 | struct btrfs_tree_block_info *bi; |
| 6990 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 6991 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 6992 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6993 | } |
| 6994 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6995 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 6996 | if (refs < refs_to_drop) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6997 | btrfs_err(info, |
| 6998 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", |
| 6999 | refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7000 | ret = -EINVAL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7001 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7002 | goto out; |
| 7003 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7004 | refs -= refs_to_drop; |
| 7005 | |
| 7006 | if (refs > 0) { |
| 7007 | if (extent_op) |
| 7008 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 7009 | /* |
| 7010 | * In the case of inline back ref, reference count will |
| 7011 | * be updated by remove_extent_backref |
| 7012 | */ |
| 7013 | if (iref) { |
| 7014 | BUG_ON(!found_extent); |
| 7015 | } else { |
| 7016 | btrfs_set_extent_refs(leaf, ei, refs); |
| 7017 | btrfs_mark_buffer_dirty(leaf); |
| 7018 | } |
| 7019 | if (found_extent) { |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 7020 | ret = remove_extent_backref(trans, info, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7021 | iref, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7022 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7023 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7024 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7025 | goto out; |
| 7026 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7027 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7028 | add_pinned_bytes(info, -num_bytes, owner_objectid, |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7029 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7030 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7031 | if (found_extent) { |
| 7032 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 7033 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7034 | if (iref) { |
| 7035 | BUG_ON(path->slots[0] != extent_slot); |
| 7036 | } else { |
| 7037 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 7038 | path->slots[0] = extent_slot; |
| 7039 | num_to_del = 2; |
| 7040 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 7041 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7042 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7043 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 7044 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 7045 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7046 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7047 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7048 | goto out; |
| 7049 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7050 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 7051 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7052 | if (is_data) { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7053 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7054 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7055 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7056 | goto out; |
| 7057 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 7058 | } |
| 7059 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7060 | ret = add_to_free_space_tree(trans, info, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7061 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7062 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7063 | goto out; |
| 7064 | } |
| 7065 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7066 | ret = update_block_group(trans, info, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7067 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7068 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7069 | goto out; |
| 7070 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7071 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7072 | btrfs_release_path(path); |
| 7073 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7074 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7075 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7076 | return ret; |
| 7077 | } |
| 7078 | |
| 7079 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7080 | * 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] | 7081 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 7082 | * a given extent, and if there are no other delayed refs to be processed, it |
| 7083 | * removes it from the tree. |
| 7084 | */ |
| 7085 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7086 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7087 | { |
| 7088 | struct btrfs_delayed_ref_head *head; |
| 7089 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7090 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7091 | |
| 7092 | delayed_refs = &trans->transaction->delayed_refs; |
| 7093 | spin_lock(&delayed_refs->lock); |
Liu Bo | f72ad18e | 2017-01-30 12:24:37 -0800 | [diff] [blame] | 7094 | head = btrfs_find_delayed_ref_head(delayed_refs, bytenr); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7095 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 7096 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7097 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7098 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 7099 | if (!list_empty(&head->ref_list)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7100 | goto out; |
| 7101 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7102 | if (head->extent_op) { |
| 7103 | if (!head->must_insert_reserved) |
| 7104 | goto out; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 7105 | btrfs_free_delayed_extent_op(head->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7106 | head->extent_op = NULL; |
| 7107 | } |
| 7108 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7109 | /* |
| 7110 | * waiting for the lock here would deadlock. If someone else has it |
| 7111 | * locked they are already in the process of dropping it anyway |
| 7112 | */ |
| 7113 | if (!mutex_trylock(&head->mutex)) |
| 7114 | goto out; |
| 7115 | |
| 7116 | /* |
| 7117 | * at this point we have a head with no other entries. Go |
| 7118 | * ahead and process it. |
| 7119 | */ |
| 7120 | head->node.in_tree = 0; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 7121 | rb_erase(&head->href_node, &delayed_refs->href_root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7122 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7123 | atomic_dec(&delayed_refs->num_entries); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7124 | |
| 7125 | /* |
| 7126 | * we don't take a ref on the node because we're removing it from the |
| 7127 | * tree, so we just steal the ref the tree was holding. |
| 7128 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7129 | delayed_refs->num_heads--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7130 | if (head->processing == 0) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7131 | delayed_refs->num_heads_ready--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7132 | head->processing = 0; |
| 7133 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7134 | spin_unlock(&delayed_refs->lock); |
| 7135 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7136 | BUG_ON(head->extent_op); |
| 7137 | if (head->must_insert_reserved) |
| 7138 | ret = 1; |
| 7139 | |
| 7140 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7141 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7142 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7143 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7144 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 7145 | |
| 7146 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7147 | spin_unlock(&delayed_refs->lock); |
| 7148 | return 0; |
| 7149 | } |
| 7150 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7151 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 7152 | struct btrfs_root *root, |
| 7153 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7154 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7155 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7156 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7157 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7158 | int ret; |
| 7159 | |
| 7160 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7161 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
| 7162 | buf->start, buf->len, |
| 7163 | parent, |
| 7164 | root->root_key.objectid, |
| 7165 | btrfs_header_level(buf), |
| 7166 | BTRFS_DROP_DELAYED_REF, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7167 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7168 | } |
| 7169 | |
| 7170 | if (!last_ref) |
| 7171 | return; |
| 7172 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7173 | if (btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7174 | struct btrfs_block_group_cache *cache; |
| 7175 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7176 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7177 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7178 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7179 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7180 | } |
| 7181 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7182 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7183 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7184 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7185 | pin_down_extent(fs_info, cache, buf->start, |
| 7186 | buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7187 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7188 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7189 | } |
| 7190 | |
| 7191 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 7192 | |
| 7193 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 7194 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7195 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7196 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7197 | pin = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7198 | } |
| 7199 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7200 | if (pin) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7201 | add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf), |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7202 | root->root_key.objectid); |
| 7203 | |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 7204 | /* |
| 7205 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 7206 | * anymore. |
| 7207 | */ |
| 7208 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7209 | } |
| 7210 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7211 | /* Can return -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7212 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 7213 | struct btrfs_fs_info *fs_info, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7214 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7215 | u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7216 | { |
| 7217 | int ret; |
| 7218 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 7219 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7220 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 7221 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7222 | add_pinned_bytes(fs_info, num_bytes, owner, root_objectid); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7223 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7224 | /* |
| 7225 | * tree log blocks never actually go into the extent allocation |
| 7226 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7227 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7228 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 7229 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7230 | /* unlocks the pinned mutex */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7231 | btrfs_pin_extent(fs_info, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7232 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7233 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7234 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 7235 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7236 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7237 | BTRFS_DROP_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7238 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7239 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 7240 | num_bytes, |
| 7241 | parent, root_objectid, owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 7242 | offset, 0, |
Jeff Mahoney | fef394f | 2016-12-13 14:39:34 -0500 | [diff] [blame] | 7243 | BTRFS_DROP_DELAYED_REF); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7244 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7245 | return ret; |
| 7246 | } |
| 7247 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7248 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7249 | * when we wait for progress in the block group caching, its because |
| 7250 | * our allocation attempt failed at least once. So, we must sleep |
| 7251 | * and let some progress happen before we try again. |
| 7252 | * |
| 7253 | * This function will sleep at least once waiting for new free space to |
| 7254 | * show up, and then it will check the block group free space numbers |
| 7255 | * for our min num_bytes. Another option is to have it go ahead |
| 7256 | * and look in the rbtree for a free extent of a given size, but this |
| 7257 | * is a good start. |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7258 | * |
| 7259 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 7260 | * any of the information in this block group. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7261 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7262 | static noinline void |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7263 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 7264 | u64 num_bytes) |
| 7265 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7266 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7267 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7268 | caching_ctl = get_caching_control(cache); |
| 7269 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7270 | return; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7271 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7272 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7273 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7274 | |
| 7275 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7276 | } |
| 7277 | |
| 7278 | static noinline int |
| 7279 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 7280 | { |
| 7281 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7282 | int ret = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7283 | |
| 7284 | caching_ctl = get_caching_control(cache); |
| 7285 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7286 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7287 | |
| 7288 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7289 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 7290 | ret = -EIO; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7291 | put_caching_control(caching_ctl); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7292 | return ret; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7293 | } |
| 7294 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 7295 | int __get_raid_index(u64 flags) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7296 | { |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7297 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7298 | return BTRFS_RAID_RAID10; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7299 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7300 | return BTRFS_RAID_RAID1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7301 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7302 | return BTRFS_RAID_DUP; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7303 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7304 | return BTRFS_RAID_RAID0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7305 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7306 | return BTRFS_RAID_RAID5; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7307 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7308 | return BTRFS_RAID_RAID6; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7309 | |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7310 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7311 | } |
| 7312 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7313 | int get_block_group_index(struct btrfs_block_group_cache *cache) |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7314 | { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 7315 | return __get_raid_index(cache->flags); |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7316 | } |
| 7317 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7318 | static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = { |
| 7319 | [BTRFS_RAID_RAID10] = "raid10", |
| 7320 | [BTRFS_RAID_RAID1] = "raid1", |
| 7321 | [BTRFS_RAID_DUP] = "dup", |
| 7322 | [BTRFS_RAID_RAID0] = "raid0", |
| 7323 | [BTRFS_RAID_SINGLE] = "single", |
| 7324 | [BTRFS_RAID_RAID5] = "raid5", |
| 7325 | [BTRFS_RAID_RAID6] = "raid6", |
| 7326 | }; |
| 7327 | |
Jeff Mahoney | 1b8e5df | 2013-11-20 16:50:23 -0500 | [diff] [blame] | 7328 | static const char *get_raid_name(enum btrfs_raid_types type) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7329 | { |
| 7330 | if (type >= BTRFS_NR_RAID_TYPES) |
| 7331 | return NULL; |
| 7332 | |
| 7333 | return btrfs_raid_type_names[type]; |
| 7334 | } |
| 7335 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7336 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7337 | LOOP_CACHING_NOWAIT = 0, |
| 7338 | LOOP_CACHING_WAIT = 1, |
| 7339 | LOOP_ALLOC_CHUNK = 2, |
| 7340 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7341 | }; |
| 7342 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7343 | static inline void |
| 7344 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 7345 | int delalloc) |
| 7346 | { |
| 7347 | if (delalloc) |
| 7348 | down_read(&cache->data_rwsem); |
| 7349 | } |
| 7350 | |
| 7351 | static inline void |
| 7352 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 7353 | int delalloc) |
| 7354 | { |
| 7355 | btrfs_get_block_group(cache); |
| 7356 | if (delalloc) |
| 7357 | down_read(&cache->data_rwsem); |
| 7358 | } |
| 7359 | |
| 7360 | static struct btrfs_block_group_cache * |
| 7361 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 7362 | struct btrfs_free_cluster *cluster, |
| 7363 | int delalloc) |
| 7364 | { |
Sudip Mukherjee | 89771cc | 2016-02-16 13:32:47 +0530 | [diff] [blame] | 7365 | struct btrfs_block_group_cache *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7366 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7367 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7368 | while (1) { |
| 7369 | used_bg = cluster->block_group; |
| 7370 | if (!used_bg) |
| 7371 | return NULL; |
| 7372 | |
| 7373 | if (used_bg == block_group) |
| 7374 | return used_bg; |
| 7375 | |
| 7376 | btrfs_get_block_group(used_bg); |
| 7377 | |
| 7378 | if (!delalloc) |
| 7379 | return used_bg; |
| 7380 | |
| 7381 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 7382 | return used_bg; |
| 7383 | |
| 7384 | spin_unlock(&cluster->refill_lock); |
| 7385 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 7386 | /* We should only have one-level nested. */ |
| 7387 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7388 | |
| 7389 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7390 | if (used_bg == cluster->block_group) |
| 7391 | return used_bg; |
| 7392 | |
| 7393 | up_read(&used_bg->data_rwsem); |
| 7394 | btrfs_put_block_group(used_bg); |
| 7395 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | static inline void |
| 7399 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 7400 | int delalloc) |
| 7401 | { |
| 7402 | if (delalloc) |
| 7403 | up_read(&cache->data_rwsem); |
| 7404 | btrfs_put_block_group(cache); |
| 7405 | } |
| 7406 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7407 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7408 | * walks the btree of allocated extents and find a hole of a given size. |
| 7409 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7410 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 7411 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7412 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7413 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7414 | * |
| 7415 | * If there is no suitable free space, we will record the max size of |
| 7416 | * the free space extent currently. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7417 | */ |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 7418 | static noinline int find_free_extent(struct btrfs_fs_info *fs_info, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7419 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
| 7420 | u64 hint_byte, struct btrfs_key *ins, |
| 7421 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7422 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7423 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7424 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7425 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7426 | struct btrfs_block_group_cache *block_group = NULL; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7427 | u64 search_start = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7428 | u64 max_extent_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7429 | u64 empty_cluster = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7430 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7431 | int loop = 0; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7432 | int index = __get_raid_index(flags); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7433 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7434 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7435 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7436 | bool have_caching_bg = false; |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7437 | bool orig_have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7438 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7439 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7440 | WARN_ON(num_bytes < fs_info->sectorsize); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7441 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7442 | ins->objectid = 0; |
| 7443 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 7444 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7445 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7446 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7447 | space_info = __find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7448 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7449 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7450 | return -ENOSPC; |
| 7451 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7452 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7453 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7454 | * If our free space is heavily fragmented we may not be able to make |
| 7455 | * big contiguous allocations, so instead of doing the expensive search |
| 7456 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 7457 | * can go ahead and search for a more manageable chunk. |
| 7458 | * |
| 7459 | * If our max_extent_size is large enough for our allocation simply |
| 7460 | * disable clustering since we will likely not be able to find enough |
| 7461 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7462 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7463 | if (unlikely(space_info->max_extent_size)) { |
| 7464 | spin_lock(&space_info->lock); |
| 7465 | if (space_info->max_extent_size && |
| 7466 | num_bytes > space_info->max_extent_size) { |
| 7467 | ins->offset = space_info->max_extent_size; |
| 7468 | spin_unlock(&space_info->lock); |
| 7469 | return -ENOSPC; |
| 7470 | } else if (space_info->max_extent_size) { |
| 7471 | use_cluster = false; |
| 7472 | } |
| 7473 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7474 | } |
| 7475 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7476 | last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7477 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7478 | spin_lock(&last_ptr->lock); |
| 7479 | if (last_ptr->block_group) |
| 7480 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7481 | if (last_ptr->fragmented) { |
| 7482 | /* |
| 7483 | * We still set window_start so we can keep track of the |
| 7484 | * last place we found an allocation to try and save |
| 7485 | * some time. |
| 7486 | */ |
| 7487 | hint_byte = last_ptr->window_start; |
| 7488 | use_cluster = false; |
| 7489 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7490 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7491 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7492 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7493 | search_start = max(search_start, first_logical_byte(fs_info, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7494 | search_start = max(search_start, hint_byte); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7495 | if (search_start == hint_byte) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7496 | block_group = btrfs_lookup_block_group(fs_info, search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7497 | /* |
| 7498 | * we don't want to use the block group if it doesn't match our |
| 7499 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7500 | * |
| 7501 | * However if we are re-searching with an ideal block group |
| 7502 | * 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] | 7503 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7504 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7505 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7506 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7507 | if (list_empty(&block_group->list) || |
| 7508 | block_group->ro) { |
| 7509 | /* |
| 7510 | * someone is removing this block group, |
| 7511 | * we can't jump into the have_block_group |
| 7512 | * target because our list pointers are not |
| 7513 | * valid |
| 7514 | */ |
| 7515 | btrfs_put_block_group(block_group); |
| 7516 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7517 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7518 | index = get_block_group_index(block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7519 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7520 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7521 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7522 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7523 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7524 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7525 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7526 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7527 | have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7528 | if (index == 0 || index == __get_raid_index(flags)) |
| 7529 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7530 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7531 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 7532 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7533 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7534 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 7535 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7536 | btrfs_grab_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7537 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7538 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7539 | /* |
| 7540 | * this can happen if we end up cycling through all the |
| 7541 | * raid types, but we want to make sure we only allocate |
| 7542 | * for the proper type. |
| 7543 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7544 | if (!block_group_bits(block_group, flags)) { |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7545 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 7546 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7547 | BTRFS_BLOCK_GROUP_RAID5 | |
| 7548 | BTRFS_BLOCK_GROUP_RAID6 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7549 | BTRFS_BLOCK_GROUP_RAID10; |
| 7550 | |
| 7551 | /* |
| 7552 | * if they asked for extra copies and this block group |
| 7553 | * doesn't provide them, bail. This does allow us to |
| 7554 | * fill raid0 from raid1. |
| 7555 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7556 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7557 | goto loop; |
| 7558 | } |
| 7559 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7560 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 7561 | cached = block_group_cache_done(block_group); |
| 7562 | if (unlikely(!cached)) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7563 | have_caching_bg = true; |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 7564 | ret = cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 7565 | BUG_ON(ret < 0); |
| 7566 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7567 | } |
| 7568 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7569 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 7570 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7571 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7572 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7573 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7574 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7575 | * Ok we want to try and use the cluster allocator, so |
| 7576 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7577 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7578 | if (last_ptr && use_cluster) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7579 | struct btrfs_block_group_cache *used_block_group; |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7580 | unsigned long aligned_cluster; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7581 | /* |
| 7582 | * the refill lock keeps out other |
| 7583 | * people trying to start a new cluster |
| 7584 | */ |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7585 | used_block_group = btrfs_lock_cluster(block_group, |
| 7586 | last_ptr, |
| 7587 | delalloc); |
| 7588 | if (!used_block_group) |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7589 | goto refill_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7590 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7591 | if (used_block_group != block_group && |
| 7592 | (used_block_group->ro || |
| 7593 | !block_group_bits(used_block_group, flags))) |
| 7594 | goto release_cluster; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7595 | |
| 7596 | offset = btrfs_alloc_from_cluster(used_block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7597 | last_ptr, |
| 7598 | num_bytes, |
| 7599 | used_block_group->key.objectid, |
| 7600 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7601 | if (offset) { |
| 7602 | /* we have a block, we're done */ |
| 7603 | spin_unlock(&last_ptr->refill_lock); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7604 | trace_btrfs_reserve_extent_cluster(fs_info, |
Miao Xie | 89d4346 | 2014-01-15 20:00:57 +0800 | [diff] [blame] | 7605 | used_block_group, |
| 7606 | search_start, num_bytes); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7607 | if (used_block_group != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7608 | btrfs_release_block_group(block_group, |
| 7609 | delalloc); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7610 | block_group = used_block_group; |
| 7611 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7612 | goto checks; |
| 7613 | } |
| 7614 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7615 | WARN_ON(last_ptr->block_group != used_block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7616 | release_cluster: |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7617 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 7618 | * set up a new clusters, so lets just skip it |
| 7619 | * and let the allocator find whatever block |
| 7620 | * it can find. If we reach this point, we |
| 7621 | * will have tried the cluster allocator |
| 7622 | * plenty of times and not have found |
| 7623 | * anything, so we are likely way too |
| 7624 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7625 | * anything. |
| 7626 | * |
| 7627 | * However, if the cluster is taken from the |
| 7628 | * current block group, release the cluster |
| 7629 | * first, so that we stand a better chance of |
| 7630 | * succeeding in the unclustered |
| 7631 | * allocation. */ |
| 7632 | if (loop >= LOOP_NO_EMPTY_SIZE && |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7633 | used_block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7634 | spin_unlock(&last_ptr->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7635 | btrfs_release_block_group(used_block_group, |
| 7636 | delalloc); |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7637 | goto unclustered_alloc; |
| 7638 | } |
| 7639 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7640 | /* |
| 7641 | * this cluster didn't work out, free it and |
| 7642 | * start over |
| 7643 | */ |
| 7644 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 7645 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7646 | if (used_block_group != block_group) |
| 7647 | btrfs_release_block_group(used_block_group, |
| 7648 | delalloc); |
| 7649 | refill_cluster: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7650 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 7651 | spin_unlock(&last_ptr->refill_lock); |
| 7652 | goto unclustered_alloc; |
| 7653 | } |
| 7654 | |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7655 | aligned_cluster = max_t(unsigned long, |
| 7656 | empty_cluster + empty_size, |
| 7657 | block_group->full_stripe_len); |
| 7658 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7659 | /* allocate a cluster in this block group */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7660 | ret = btrfs_find_space_cluster(fs_info, block_group, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7661 | last_ptr, search_start, |
| 7662 | num_bytes, |
| 7663 | aligned_cluster); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7664 | if (ret == 0) { |
| 7665 | /* |
| 7666 | * now pull our allocation out of this |
| 7667 | * cluster |
| 7668 | */ |
| 7669 | offset = btrfs_alloc_from_cluster(block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7670 | last_ptr, |
| 7671 | num_bytes, |
| 7672 | search_start, |
| 7673 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7674 | if (offset) { |
| 7675 | /* we found one, proceed */ |
| 7676 | spin_unlock(&last_ptr->refill_lock); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7677 | trace_btrfs_reserve_extent_cluster(fs_info, |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7678 | block_group, search_start, |
| 7679 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7680 | goto checks; |
| 7681 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7682 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 7683 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7684 | spin_unlock(&last_ptr->refill_lock); |
| 7685 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7686 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7687 | wait_block_group_cache_progress(block_group, |
| 7688 | num_bytes + empty_cluster + empty_size); |
| 7689 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7690 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7691 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7692 | /* |
| 7693 | * at this point we either didn't find a cluster |
| 7694 | * or we weren't able to allocate a block from our |
| 7695 | * cluster. Free the cluster we've been trying |
| 7696 | * to use, and go to the next block group |
| 7697 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7698 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7699 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7700 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7701 | } |
| 7702 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7703 | unclustered_alloc: |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7704 | /* |
| 7705 | * We are doing an unclustered alloc, set the fragmented flag so |
| 7706 | * we don't bother trying to setup a cluster again until we get |
| 7707 | * more space. |
| 7708 | */ |
| 7709 | if (unlikely(last_ptr)) { |
| 7710 | spin_lock(&last_ptr->lock); |
| 7711 | last_ptr->fragmented = 1; |
| 7712 | spin_unlock(&last_ptr->lock); |
| 7713 | } |
Liu Bo | 0c9b36e | 2017-02-13 15:42:30 -0800 | [diff] [blame] | 7714 | if (cached) { |
| 7715 | struct btrfs_free_space_ctl *ctl = |
| 7716 | block_group->free_space_ctl; |
| 7717 | |
| 7718 | spin_lock(&ctl->tree_lock); |
| 7719 | if (ctl->free_space < |
| 7720 | num_bytes + empty_cluster + empty_size) { |
| 7721 | if (ctl->free_space > max_extent_size) |
| 7722 | max_extent_size = ctl->free_space; |
| 7723 | spin_unlock(&ctl->tree_lock); |
| 7724 | goto loop; |
| 7725 | } |
| 7726 | spin_unlock(&ctl->tree_lock); |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7727 | } |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7728 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7729 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7730 | num_bytes, empty_size, |
| 7731 | &max_extent_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7732 | /* |
| 7733 | * If we didn't find a chunk, and we haven't failed on this |
| 7734 | * block group before, and this block group is in the middle of |
| 7735 | * caching and we are ok with waiting, then go ahead and wait |
| 7736 | * for progress to be made, and set failed_alloc to true. |
| 7737 | * |
| 7738 | * If failed_alloc is true then we've already waited on this |
| 7739 | * block group once and should move on to the next block group. |
| 7740 | */ |
| 7741 | if (!offset && !failed_alloc && !cached && |
| 7742 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7743 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7744 | num_bytes + empty_size); |
| 7745 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7746 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7747 | } else if (!offset) { |
| 7748 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7749 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7750 | checks: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7751 | search_start = ALIGN(offset, fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 7752 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7753 | /* move on to the next group */ |
| 7754 | if (search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7755 | block_group->key.objectid + block_group->key.offset) { |
| 7756 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7757 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7758 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7759 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7760 | if (offset < search_start) |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7761 | btrfs_add_free_space(block_group, offset, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7762 | search_start - offset); |
| 7763 | BUG_ON(offset > search_start); |
| 7764 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7765 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 7766 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7767 | if (ret == -EAGAIN) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7768 | btrfs_add_free_space(block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7769 | goto loop; |
| 7770 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7771 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7772 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7773 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7774 | ins->objectid = search_start; |
| 7775 | ins->offset = num_bytes; |
| 7776 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7777 | trace_btrfs_reserve_extent(fs_info, block_group, |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7778 | search_start, num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7779 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7780 | break; |
| 7781 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7782 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7783 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7784 | BUG_ON(index != get_block_group_index(block_group)); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7785 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7786 | } |
| 7787 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 7788 | |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7789 | if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg |
| 7790 | && !orig_have_caching_bg) |
| 7791 | orig_have_caching_bg = true; |
| 7792 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7793 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 7794 | goto search; |
| 7795 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7796 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 7797 | goto search; |
| 7798 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7799 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7800 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 7801 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7802 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 7803 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 7804 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 7805 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7806 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7807 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7808 | index = 0; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7809 | if (loop == LOOP_CACHING_NOWAIT) { |
| 7810 | /* |
| 7811 | * We want to skip the LOOP_CACHING_WAIT step if we |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7812 | * don't have any uncached bgs and we've already done a |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7813 | * full search through. |
| 7814 | */ |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7815 | if (orig_have_caching_bg || !full_search) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7816 | loop = LOOP_CACHING_WAIT; |
| 7817 | else |
| 7818 | loop = LOOP_ALLOC_CHUNK; |
| 7819 | } else { |
| 7820 | loop++; |
| 7821 | } |
| 7822 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7823 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7824 | struct btrfs_trans_handle *trans; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7825 | int exist = 0; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7826 | |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7827 | trans = current->journal_info; |
| 7828 | if (trans) |
| 7829 | exist = 1; |
| 7830 | else |
| 7831 | trans = btrfs_join_transaction(root); |
| 7832 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7833 | if (IS_ERR(trans)) { |
| 7834 | ret = PTR_ERR(trans); |
| 7835 | goto out; |
| 7836 | } |
| 7837 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7838 | ret = do_chunk_alloc(trans, fs_info, flags, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7839 | CHUNK_ALLOC_FORCE); |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7840 | |
| 7841 | /* |
| 7842 | * If we can't allocate a new chunk we've already looped |
| 7843 | * through at least once, move on to the NO_EMPTY_SIZE |
| 7844 | * case. |
| 7845 | */ |
| 7846 | if (ret == -ENOSPC) |
| 7847 | loop = LOOP_NO_EMPTY_SIZE; |
| 7848 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7849 | /* |
| 7850 | * Do not bail out on ENOSPC since we |
| 7851 | * can do more things. |
| 7852 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7853 | if (ret < 0 && ret != -ENOSPC) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7854 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7855 | else |
| 7856 | ret = 0; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7857 | if (!exist) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7858 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7859 | if (ret) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7860 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7861 | } |
| 7862 | |
| 7863 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7864 | /* |
| 7865 | * Don't loop again if we already have no empty_size and |
| 7866 | * no empty_cluster. |
| 7867 | */ |
| 7868 | if (empty_size == 0 && |
| 7869 | empty_cluster == 0) { |
| 7870 | ret = -ENOSPC; |
| 7871 | goto out; |
| 7872 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7873 | empty_size = 0; |
| 7874 | empty_cluster = 0; |
| 7875 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7876 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7877 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7878 | } else if (!ins->objectid) { |
| 7879 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7880 | } else if (ins->objectid) { |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7881 | if (!use_cluster && last_ptr) { |
| 7882 | spin_lock(&last_ptr->lock); |
| 7883 | last_ptr->window_start = ins->objectid; |
| 7884 | spin_unlock(&last_ptr->lock); |
| 7885 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7886 | ret = 0; |
| 7887 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7888 | out: |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7889 | if (ret == -ENOSPC) { |
| 7890 | spin_lock(&space_info->lock); |
| 7891 | space_info->max_extent_size = max_extent_size; |
| 7892 | spin_unlock(&space_info->lock); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7893 | ins->offset = max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7894 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 7895 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7896 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7897 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7898 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
| 7899 | struct btrfs_space_info *info, u64 bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7900 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7901 | { |
| 7902 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7903 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7904 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7905 | spin_lock(&info->lock); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7906 | btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull", |
| 7907 | info->flags, |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 7908 | info->total_bytes - btrfs_space_info_used(info, true), |
| 7909 | info->full ? "" : "not "); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7910 | btrfs_info(fs_info, |
| 7911 | "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu", |
| 7912 | info->total_bytes, info->bytes_used, info->bytes_pinned, |
| 7913 | info->bytes_reserved, info->bytes_may_use, |
| 7914 | info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7915 | spin_unlock(&info->lock); |
| 7916 | |
| 7917 | if (!dump_block_groups) |
| 7918 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7919 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7920 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7921 | again: |
| 7922 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7923 | spin_lock(&cache->lock); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7924 | btrfs_info(fs_info, |
| 7925 | "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s", |
| 7926 | cache->key.objectid, cache->key.offset, |
| 7927 | btrfs_block_group_used(&cache->item), cache->pinned, |
| 7928 | cache->reserved, cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7929 | btrfs_dump_free_space(cache, bytes); |
| 7930 | spin_unlock(&cache->lock); |
| 7931 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7932 | if (++index < BTRFS_NR_RAID_TYPES) |
| 7933 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7934 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7935 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 7936 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7937 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7938 | u64 num_bytes, u64 min_alloc_size, |
| 7939 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7940 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7941 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7942 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 7943 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7944 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7945 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7946 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7947 | flags = btrfs_get_alloc_profile(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 7948 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7949 | WARN_ON(num_bytes < fs_info->sectorsize); |
Jeff Mahoney | 87bde3cd | 2017-02-15 16:28:27 -0500 | [diff] [blame] | 7950 | ret = find_free_extent(fs_info, ram_bytes, num_bytes, empty_size, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7951 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7952 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7953 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7954 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7955 | if (!final_tried && ins->offset) { |
| 7956 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7957 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7958 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7959 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7960 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7961 | if (num_bytes == min_alloc_size) |
| 7962 | final_tried = true; |
| 7963 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7964 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7965 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7966 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7967 | sinfo = __find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7968 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7969 | "allocation failed flags %llu, wanted %llu", |
| 7970 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 7971 | if (sinfo) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7972 | dump_space_info(fs_info, sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7973 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7974 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7975 | |
| 7976 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7977 | } |
| 7978 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7979 | static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7980 | u64 start, u64 len, |
| 7981 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7982 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7983 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7984 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7985 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7986 | cache = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7987 | if (!cache) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7988 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 7989 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7990 | return -ENOSPC; |
| 7991 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7992 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7993 | if (pin) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7994 | pin_down_extent(fs_info, cache, start, len, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7995 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7996 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7997 | ret = btrfs_discard_extent(fs_info, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7998 | btrfs_add_free_space(cache, start, len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 7999 | btrfs_free_reserved_bytes(cache, len, delalloc); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8000 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8001 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 8002 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8003 | btrfs_put_block_group(cache); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8004 | return ret; |
| 8005 | } |
| 8006 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8007 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8008 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8009 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8010 | return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8011 | } |
| 8012 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8013 | 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] | 8014 | u64 start, u64 len) |
| 8015 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8016 | return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8017 | } |
| 8018 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8019 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8020 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8021 | u64 parent, u64 root_objectid, |
| 8022 | u64 flags, u64 owner, u64 offset, |
| 8023 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8024 | { |
| 8025 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8026 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8027 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8028 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8029 | struct extent_buffer *leaf; |
| 8030 | int type; |
| 8031 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 8032 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8033 | if (parent > 0) |
| 8034 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 8035 | else |
| 8036 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 8037 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8038 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 8039 | |
| 8040 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 8041 | if (!path) |
| 8042 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8043 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 8044 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8045 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 8046 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8047 | if (ret) { |
| 8048 | btrfs_free_path(path); |
| 8049 | return ret; |
| 8050 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8051 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8052 | leaf = path->nodes[0]; |
| 8053 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8054 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8055 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 8056 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 8057 | btrfs_set_extent_flags(leaf, extent_item, |
| 8058 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8059 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8060 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 8061 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 8062 | if (parent > 0) { |
| 8063 | struct btrfs_shared_data_ref *ref; |
| 8064 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 8065 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 8066 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 8067 | } else { |
| 8068 | struct btrfs_extent_data_ref *ref; |
| 8069 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 8070 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 8071 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 8072 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 8073 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 8074 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8075 | |
| 8076 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 8077 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 8078 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8079 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
| 8080 | ins->offset); |
| 8081 | if (ret) |
| 8082 | return ret; |
| 8083 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 8084 | ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8085 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8086 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8087 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 8088 | BUG(); |
| 8089 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8090 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8091 | return ret; |
| 8092 | } |
| 8093 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8094 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8095 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8096 | u64 parent, u64 root_objectid, |
| 8097 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8098 | int level, struct btrfs_key *ins) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8099 | { |
| 8100 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8101 | struct btrfs_extent_item *extent_item; |
| 8102 | struct btrfs_tree_block_info *block_info; |
| 8103 | struct btrfs_extent_inline_ref *iref; |
| 8104 | struct btrfs_path *path; |
| 8105 | struct extent_buffer *leaf; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8106 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 8107 | u64 num_bytes = ins->offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8108 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8109 | |
| 8110 | if (!skinny_metadata) |
| 8111 | size += sizeof(*block_info); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8112 | |
| 8113 | path = btrfs_alloc_path(); |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8114 | if (!path) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8115 | btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8116 | fs_info->nodesize); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8117 | return -ENOMEM; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8118 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8119 | |
| 8120 | path->leave_spinning = 1; |
| 8121 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 8122 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8123 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 8124 | btrfs_free_path(path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8125 | btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8126 | fs_info->nodesize); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8127 | return ret; |
| 8128 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8129 | |
| 8130 | leaf = path->nodes[0]; |
| 8131 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 8132 | struct btrfs_extent_item); |
| 8133 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 8134 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 8135 | btrfs_set_extent_flags(leaf, extent_item, |
| 8136 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8137 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8138 | if (skinny_metadata) { |
| 8139 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8140 | num_bytes = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8141 | } else { |
| 8142 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 8143 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 8144 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 8145 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 8146 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8147 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8148 | if (parent > 0) { |
| 8149 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 8150 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 8151 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 8152 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 8153 | } else { |
| 8154 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 8155 | BTRFS_TREE_BLOCK_REF_KEY); |
| 8156 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 8157 | } |
| 8158 | |
| 8159 | btrfs_mark_buffer_dirty(leaf); |
| 8160 | btrfs_free_path(path); |
| 8161 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8162 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
| 8163 | num_bytes); |
| 8164 | if (ret) |
| 8165 | return ret; |
| 8166 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 8167 | ret = update_block_group(trans, fs_info, ins->objectid, |
| 8168 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8169 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8170 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8171 | ins->objectid, ins->offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8172 | BUG(); |
| 8173 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 8174 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8175 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8176 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8177 | return ret; |
| 8178 | } |
| 8179 | |
| 8180 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8181 | u64 root_objectid, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 8182 | u64 offset, u64 ram_bytes, |
| 8183 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8184 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8185 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8186 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 8187 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8188 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 8189 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8190 | ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8191 | ins->offset, 0, |
| 8192 | root_objectid, owner, offset, |
Jeff Mahoney | fef394f | 2016-12-13 14:39:34 -0500 | [diff] [blame] | 8193 | ram_bytes, BTRFS_ADD_DELAYED_EXTENT); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8194 | return ret; |
| 8195 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8196 | |
| 8197 | /* |
| 8198 | * this is used by the tree logging recovery code. It records that |
| 8199 | * an extent has been allocated and makes sure to clear the free |
| 8200 | * space cache bits as well |
| 8201 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8202 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8203 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8204 | u64 root_objectid, u64 owner, u64 offset, |
| 8205 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8206 | { |
| 8207 | int ret; |
| 8208 | struct btrfs_block_group_cache *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 8209 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8210 | |
| 8211 | /* |
| 8212 | * 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] | 8213 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8214 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8215 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8216 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 8217 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8218 | if (ret) |
| 8219 | return ret; |
| 8220 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8221 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8222 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8223 | if (!block_group) |
| 8224 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8225 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 8226 | space_info = block_group->space_info; |
| 8227 | spin_lock(&space_info->lock); |
| 8228 | spin_lock(&block_group->lock); |
| 8229 | space_info->bytes_reserved += ins->offset; |
| 8230 | block_group->reserved += ins->offset; |
| 8231 | spin_unlock(&block_group->lock); |
| 8232 | spin_unlock(&space_info->lock); |
| 8233 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8234 | ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8235 | 0, owner, offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 8236 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8237 | return ret; |
| 8238 | } |
| 8239 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8240 | static struct extent_buffer * |
| 8241 | 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] | 8242 | u64 bytenr, int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8243 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8244 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8245 | struct extent_buffer *buf; |
| 8246 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8247 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 8248 | if (IS_ERR(buf)) |
| 8249 | return buf; |
| 8250 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8251 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 8252 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8253 | btrfs_tree_lock(buf); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 8254 | clean_tree_block(fs_info, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 8255 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8256 | |
| 8257 | btrfs_set_lock_blocking(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 8258 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8259 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8260 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8261 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 8262 | /* |
| 8263 | * we allow two log transactions at a time, use different |
| 8264 | * EXENT bit to differentiate dirty pages. |
| 8265 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8266 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 8267 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 8268 | buf->start + buf->len - 1, GFP_NOFS); |
| 8269 | else |
| 8270 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 8271 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8272 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8273 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8274 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 8275 | buf->start + buf->len - 1, GFP_NOFS); |
| 8276 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 8277 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8278 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8279 | return buf; |
| 8280 | } |
| 8281 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8282 | static struct btrfs_block_rsv * |
| 8283 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 8284 | struct btrfs_root *root, u32 blocksize) |
| 8285 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8286 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8287 | struct btrfs_block_rsv *block_rsv; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8288 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8289 | int ret; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8290 | bool global_updated = false; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8291 | |
| 8292 | block_rsv = get_block_rsv(trans, root); |
| 8293 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8294 | if (unlikely(block_rsv->size == 0)) |
| 8295 | goto try_reserve; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8296 | again: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8297 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 8298 | if (!ret) |
| 8299 | return block_rsv; |
| 8300 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8301 | if (block_rsv->failfast) |
| 8302 | return ERR_PTR(ret); |
| 8303 | |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8304 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
| 8305 | global_updated = true; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8306 | update_global_block_rsv(fs_info); |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8307 | goto again; |
| 8308 | } |
| 8309 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8310 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8311 | static DEFINE_RATELIMIT_STATE(_rs, |
| 8312 | DEFAULT_RATELIMIT_INTERVAL * 10, |
| 8313 | /*DEFAULT_RATELIMIT_BURST*/ 1); |
| 8314 | if (__ratelimit(&_rs)) |
| 8315 | WARN(1, KERN_DEBUG |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 8316 | "BTRFS: block rsv returned %d\n", ret); |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8317 | } |
| 8318 | try_reserve: |
| 8319 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, |
| 8320 | BTRFS_RESERVE_NO_FLUSH); |
| 8321 | if (!ret) |
| 8322 | return block_rsv; |
| 8323 | /* |
| 8324 | * If we couldn't reserve metadata bytes try and use some from |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 8325 | * the global reserve if its space type is the same as the global |
| 8326 | * reservation. |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8327 | */ |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 8328 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
| 8329 | block_rsv->space_info == global_rsv->space_info) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8330 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 8331 | if (!ret) |
| 8332 | return global_rsv; |
| 8333 | } |
| 8334 | return ERR_PTR(ret); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8335 | } |
| 8336 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 8337 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 8338 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8339 | { |
| 8340 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 8341 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8342 | } |
| 8343 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8344 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8345 | * 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] | 8346 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8347 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 8348 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 8349 | struct btrfs_root *root, |
| 8350 | u64 parent, u64 root_objectid, |
| 8351 | const struct btrfs_disk_key *key, |
| 8352 | int level, u64 hint, |
| 8353 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8354 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8355 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 8356 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8357 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8358 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8359 | struct btrfs_delayed_extent_op *extent_op; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8360 | u64 flags = 0; |
| 8361 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8362 | u32 blocksize = fs_info->nodesize; |
| 8363 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8364 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 8365 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8366 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 8367 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8368 | level); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 8369 | if (!IS_ERR(buf)) |
| 8370 | root->alloc_bytenr += blocksize; |
| 8371 | return buf; |
| 8372 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 8373 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 8374 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8375 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 8376 | if (IS_ERR(block_rsv)) |
| 8377 | return ERR_CAST(block_rsv); |
| 8378 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 8379 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8380 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8381 | if (ret) |
| 8382 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 8383 | |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8384 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8385 | if (IS_ERR(buf)) { |
| 8386 | ret = PTR_ERR(buf); |
| 8387 | goto out_free_reserved; |
| 8388 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8389 | |
| 8390 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 8391 | if (parent == 0) |
| 8392 | parent = ins.objectid; |
| 8393 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8394 | } else |
| 8395 | BUG_ON(parent > 0); |
| 8396 | |
| 8397 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 8398 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8399 | if (!extent_op) { |
| 8400 | ret = -ENOMEM; |
| 8401 | goto out_free_buf; |
| 8402 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8403 | if (key) |
| 8404 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 8405 | else |
| 8406 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 8407 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 8408 | extent_op->update_key = skinny_metadata ? false : true; |
| 8409 | extent_op->update_flags = true; |
| 8410 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8411 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8412 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8413 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8414 | ins.objectid, ins.offset, |
| 8415 | parent, root_objectid, level, |
| 8416 | BTRFS_ADD_DELAYED_EXTENT, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8417 | extent_op); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8418 | if (ret) |
| 8419 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8420 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8421 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8422 | |
| 8423 | out_free_delayed: |
| 8424 | btrfs_free_delayed_extent_op(extent_op); |
| 8425 | out_free_buf: |
| 8426 | free_extent_buffer(buf); |
| 8427 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8428 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8429 | out_unuse: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8430 | unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8431 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8432 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 8433 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8434 | struct walk_control { |
| 8435 | u64 refs[BTRFS_MAX_LEVEL]; |
| 8436 | u64 flags[BTRFS_MAX_LEVEL]; |
| 8437 | struct btrfs_key update_progress; |
| 8438 | int stage; |
| 8439 | int level; |
| 8440 | int shared_level; |
| 8441 | int update_ref; |
| 8442 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8443 | int reada_slot; |
| 8444 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8445 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8446 | }; |
| 8447 | |
| 8448 | #define DROP_REFERENCE 1 |
| 8449 | #define UPDATE_BACKREF 2 |
| 8450 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8451 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 8452 | struct btrfs_root *root, |
| 8453 | struct walk_control *wc, |
| 8454 | struct btrfs_path *path) |
| 8455 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8456 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8457 | u64 bytenr; |
| 8458 | u64 generation; |
| 8459 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8460 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8461 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8462 | struct btrfs_key key; |
| 8463 | struct extent_buffer *eb; |
| 8464 | int ret; |
| 8465 | int slot; |
| 8466 | int nread = 0; |
| 8467 | |
| 8468 | if (path->slots[wc->level] < wc->reada_slot) { |
| 8469 | wc->reada_count = wc->reada_count * 2 / 3; |
| 8470 | wc->reada_count = max(wc->reada_count, 2); |
| 8471 | } else { |
| 8472 | wc->reada_count = wc->reada_count * 3 / 2; |
| 8473 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8474 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8475 | } |
| 8476 | |
| 8477 | eb = path->nodes[wc->level]; |
| 8478 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8479 | |
| 8480 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 8481 | if (nread >= wc->reada_count) |
| 8482 | break; |
| 8483 | |
| 8484 | cond_resched(); |
| 8485 | bytenr = btrfs_node_blockptr(eb, slot); |
| 8486 | generation = btrfs_node_ptr_generation(eb, slot); |
| 8487 | |
| 8488 | if (slot == path->slots[wc->level]) |
| 8489 | goto reada; |
| 8490 | |
| 8491 | if (wc->stage == UPDATE_BACKREF && |
| 8492 | generation <= root->root_key.offset) |
| 8493 | continue; |
| 8494 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8495 | /* 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] | 8496 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8497 | wc->level - 1, 1, &refs, |
| 8498 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8499 | /* We don't care about errors in readahead. */ |
| 8500 | if (ret < 0) |
| 8501 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8502 | BUG_ON(refs == 0); |
| 8503 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8504 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8505 | if (refs == 1) |
| 8506 | goto reada; |
| 8507 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8508 | if (wc->level == 1 && |
| 8509 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8510 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8511 | if (!wc->update_ref || |
| 8512 | generation <= root->root_key.offset) |
| 8513 | continue; |
| 8514 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 8515 | ret = btrfs_comp_cpu_keys(&key, |
| 8516 | &wc->update_progress); |
| 8517 | if (ret < 0) |
| 8518 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8519 | } else { |
| 8520 | if (wc->level == 1 && |
| 8521 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8522 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8523 | } |
| 8524 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8525 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8526 | nread++; |
| 8527 | } |
| 8528 | wc->reada_slot = slot; |
| 8529 | } |
| 8530 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8531 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8532 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8533 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8534 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 8535 | * back refs for pointers in the block. |
| 8536 | * |
| 8537 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8538 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8539 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 8540 | struct btrfs_root *root, |
| 8541 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8542 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8543 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8544 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8545 | int level = wc->level; |
| 8546 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8547 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8548 | int ret; |
| 8549 | |
| 8550 | if (wc->stage == UPDATE_BACKREF && |
| 8551 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 8552 | return 1; |
| 8553 | |
| 8554 | /* |
| 8555 | * when reference count of tree block is 1, it won't increase |
| 8556 | * again. once full backref flag is set, we never clear it. |
| 8557 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8558 | if (lookup_info && |
| 8559 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 8560 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8561 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8562 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8563 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8564 | &wc->refs[level], |
| 8565 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8566 | BUG_ON(ret == -ENOMEM); |
| 8567 | if (ret) |
| 8568 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8569 | BUG_ON(wc->refs[level] == 0); |
| 8570 | } |
| 8571 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8572 | if (wc->stage == DROP_REFERENCE) { |
| 8573 | if (wc->refs[level] > 1) |
| 8574 | return 1; |
| 8575 | |
| 8576 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8577 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8578 | path->locks[level] = 0; |
| 8579 | } |
| 8580 | return 0; |
| 8581 | } |
| 8582 | |
| 8583 | /* wc->stage == UPDATE_BACKREF */ |
| 8584 | if (!(wc->flags[level] & flag)) { |
| 8585 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8586 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8587 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8588 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8589 | BUG_ON(ret); /* -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8590 | ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8591 | eb->len, flag, |
| 8592 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8593 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8594 | wc->flags[level] |= flag; |
| 8595 | } |
| 8596 | |
| 8597 | /* |
| 8598 | * the block is shared by multiple trees, so it's not good to |
| 8599 | * keep the tree lock |
| 8600 | */ |
| 8601 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8602 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8603 | path->locks[level] = 0; |
| 8604 | } |
| 8605 | return 0; |
| 8606 | } |
| 8607 | |
| 8608 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8609 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8610 | * |
| 8611 | * when wc->stage == DROP_REFERENCE, this function checks |
| 8612 | * reference count of the block pointed to. if the block |
| 8613 | * is shared and we need update back refs for the subtree |
| 8614 | * rooted at the block, this function changes wc->stage to |
| 8615 | * UPDATE_BACKREF. if the block is shared and there is no |
| 8616 | * need to update back, this function drops the reference |
| 8617 | * to the block. |
| 8618 | * |
| 8619 | * NOTE: return value 1 means we should stop walking down. |
| 8620 | */ |
| 8621 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 8622 | struct btrfs_root *root, |
| 8623 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8624 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8625 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8626 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8627 | u64 bytenr; |
| 8628 | u64 generation; |
| 8629 | u64 parent; |
| 8630 | u32 blocksize; |
| 8631 | struct btrfs_key key; |
| 8632 | struct extent_buffer *next; |
| 8633 | int level = wc->level; |
| 8634 | int reada = 0; |
| 8635 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8636 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8637 | |
| 8638 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 8639 | path->slots[level]); |
| 8640 | /* |
| 8641 | * if the lower level block was created before the snapshot |
| 8642 | * was created, we know there is no need to update back refs |
| 8643 | * for the subtree |
| 8644 | */ |
| 8645 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8646 | generation <= root->root_key.offset) { |
| 8647 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8648 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8649 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8650 | |
| 8651 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8652 | blocksize = fs_info->nodesize; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8653 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8654 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8655 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8656 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 8657 | if (IS_ERR(next)) |
| 8658 | return PTR_ERR(next); |
| 8659 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 8660 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 8661 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8662 | reada = 1; |
| 8663 | } |
| 8664 | btrfs_tree_lock(next); |
| 8665 | btrfs_set_lock_blocking(next); |
| 8666 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8667 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8668 | &wc->refs[level - 1], |
| 8669 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8670 | if (ret < 0) |
| 8671 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8672 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8673 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8674 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8675 | ret = -EIO; |
| 8676 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8677 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8678 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8679 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8680 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8681 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8682 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8683 | if (level == 1 && |
| 8684 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8685 | goto skip; |
| 8686 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8687 | if (!wc->update_ref || |
| 8688 | generation <= root->root_key.offset) |
| 8689 | goto skip; |
| 8690 | |
| 8691 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 8692 | path->slots[level]); |
| 8693 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 8694 | if (ret < 0) |
| 8695 | goto skip; |
| 8696 | |
| 8697 | wc->stage = UPDATE_BACKREF; |
| 8698 | wc->shared_level = level - 1; |
| 8699 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8700 | } else { |
| 8701 | if (level == 1 && |
| 8702 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8703 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8704 | } |
| 8705 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 8706 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8707 | btrfs_tree_unlock(next); |
| 8708 | free_extent_buffer(next); |
| 8709 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8710 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8711 | } |
| 8712 | |
| 8713 | if (!next) { |
| 8714 | if (reada && level == 1) |
| 8715 | reada_walk_down(trans, root, wc, path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8716 | next = read_tree_block(fs_info, bytenr, generation); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 8717 | if (IS_ERR(next)) { |
| 8718 | return PTR_ERR(next); |
| 8719 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8720 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 8721 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8722 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8723 | btrfs_tree_lock(next); |
| 8724 | btrfs_set_lock_blocking(next); |
| 8725 | } |
| 8726 | |
| 8727 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8728 | ASSERT(level == btrfs_header_level(next)); |
| 8729 | if (level != btrfs_header_level(next)) { |
| 8730 | btrfs_err(root->fs_info, "mismatched level"); |
| 8731 | ret = -EIO; |
| 8732 | goto out_unlock; |
| 8733 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8734 | path->nodes[level] = next; |
| 8735 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8736 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8737 | wc->level = level; |
| 8738 | if (wc->level == 1) |
| 8739 | wc->reada_slot = 0; |
| 8740 | return 0; |
| 8741 | skip: |
| 8742 | wc->refs[level - 1] = 0; |
| 8743 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8744 | if (wc->stage == DROP_REFERENCE) { |
| 8745 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 8746 | parent = path->nodes[level]->start; |
| 8747 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8748 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8749 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8750 | if (root->root_key.objectid != |
| 8751 | btrfs_header_owner(path->nodes[level])) { |
| 8752 | btrfs_err(root->fs_info, |
| 8753 | "mismatched block owner"); |
| 8754 | ret = -EIO; |
| 8755 | goto out_unlock; |
| 8756 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8757 | parent = 0; |
| 8758 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8759 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8760 | if (need_account) { |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 8761 | ret = btrfs_qgroup_trace_subtree(trans, root, next, |
| 8762 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8763 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8764 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8765 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 8766 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8767 | } |
| 8768 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8769 | ret = btrfs_free_extent(trans, fs_info, bytenr, blocksize, |
| 8770 | parent, root->root_key.objectid, |
| 8771 | level - 1, 0); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8772 | if (ret) |
| 8773 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8774 | } |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8775 | |
| 8776 | *lookup_info = 1; |
| 8777 | ret = 1; |
| 8778 | |
| 8779 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8780 | btrfs_tree_unlock(next); |
| 8781 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8782 | |
| 8783 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8784 | } |
| 8785 | |
| 8786 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8787 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8788 | * |
| 8789 | * when wc->stage == DROP_REFERENCE, this function drops |
| 8790 | * reference count on the block. |
| 8791 | * |
| 8792 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 8793 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 8794 | * to UPDATE_BACKREF previously while processing the block. |
| 8795 | * |
| 8796 | * NOTE: return value 1 means we should stop walking up. |
| 8797 | */ |
| 8798 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 8799 | struct btrfs_root *root, |
| 8800 | struct btrfs_path *path, |
| 8801 | struct walk_control *wc) |
| 8802 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8803 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8804 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8805 | int level = wc->level; |
| 8806 | struct extent_buffer *eb = path->nodes[level]; |
| 8807 | u64 parent = 0; |
| 8808 | |
| 8809 | if (wc->stage == UPDATE_BACKREF) { |
| 8810 | BUG_ON(wc->shared_level < level); |
| 8811 | if (level < wc->shared_level) |
| 8812 | goto out; |
| 8813 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8814 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 8815 | if (ret > 0) |
| 8816 | wc->update_ref = 0; |
| 8817 | |
| 8818 | wc->stage = DROP_REFERENCE; |
| 8819 | wc->shared_level = -1; |
| 8820 | path->slots[level] = 0; |
| 8821 | |
| 8822 | /* |
| 8823 | * check reference count again if the block isn't locked. |
| 8824 | * we should start walking down the tree again if reference |
| 8825 | * count is one. |
| 8826 | */ |
| 8827 | if (!path->locks[level]) { |
| 8828 | BUG_ON(level == 0); |
| 8829 | btrfs_tree_lock(eb); |
| 8830 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8831 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8832 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8833 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8834 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8835 | &wc->refs[level], |
| 8836 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8837 | if (ret < 0) { |
| 8838 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8839 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8840 | return ret; |
| 8841 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8842 | BUG_ON(wc->refs[level] == 0); |
| 8843 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8844 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8845 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8846 | return 1; |
| 8847 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8848 | } |
| 8849 | } |
| 8850 | |
| 8851 | /* wc->stage == DROP_REFERENCE */ |
| 8852 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 8853 | |
| 8854 | if (wc->refs[level] == 1) { |
| 8855 | if (level == 0) { |
| 8856 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8857 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8858 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8859 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8860 | BUG_ON(ret); /* -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8861 | ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8862 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8863 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8864 | "error %d accounting leaf items. Quota is out of sync, rescan required.", |
| 8865 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8866 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8867 | } |
| 8868 | /* make block locked assertion in clean_tree_block happy */ |
| 8869 | if (!path->locks[level] && |
| 8870 | btrfs_header_generation(eb) == trans->transid) { |
| 8871 | btrfs_tree_lock(eb); |
| 8872 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8873 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8874 | } |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 8875 | clean_tree_block(fs_info, eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8876 | } |
| 8877 | |
| 8878 | if (eb == root->node) { |
| 8879 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8880 | parent = eb->start; |
| 8881 | else |
| 8882 | BUG_ON(root->root_key.objectid != |
| 8883 | btrfs_header_owner(eb)); |
| 8884 | } else { |
| 8885 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8886 | parent = path->nodes[level + 1]->start; |
| 8887 | else |
| 8888 | BUG_ON(root->root_key.objectid != |
| 8889 | btrfs_header_owner(path->nodes[level + 1])); |
| 8890 | } |
| 8891 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 8892 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8893 | out: |
| 8894 | wc->refs[level] = 0; |
| 8895 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8896 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8897 | } |
| 8898 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8899 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 8900 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8901 | struct btrfs_path *path, |
| 8902 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8903 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8904 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8905 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8906 | int ret; |
| 8907 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8908 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8909 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8910 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8911 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8912 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8913 | if (level == 0) |
| 8914 | break; |
| 8915 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 8916 | if (path->slots[level] >= |
| 8917 | btrfs_header_nritems(path->nodes[level])) |
| 8918 | break; |
| 8919 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8920 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8921 | if (ret > 0) { |
| 8922 | path->slots[level]++; |
| 8923 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 8924 | } else if (ret < 0) |
| 8925 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8926 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8927 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8928 | return 0; |
| 8929 | } |
| 8930 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8931 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 8932 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8933 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8934 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8935 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8936 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8937 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8938 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8939 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 8940 | while (level < max_level && path->nodes[level]) { |
| 8941 | wc->level = level; |
| 8942 | if (path->slots[level] + 1 < |
| 8943 | btrfs_header_nritems(path->nodes[level])) { |
| 8944 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8945 | return 0; |
| 8946 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8947 | ret = walk_up_proc(trans, root, path, wc); |
| 8948 | if (ret > 0) |
| 8949 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 8950 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8951 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8952 | btrfs_tree_unlock_rw(path->nodes[level], |
| 8953 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8954 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8955 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8956 | free_extent_buffer(path->nodes[level]); |
| 8957 | path->nodes[level] = NULL; |
| 8958 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8959 | } |
| 8960 | } |
| 8961 | return 1; |
| 8962 | } |
| 8963 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8964 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8965 | * drop a subvolume tree. |
| 8966 | * |
| 8967 | * this function traverses the tree freeing any blocks that only |
| 8968 | * referenced by the tree. |
| 8969 | * |
| 8970 | * when a shared tree block is found. this function decreases its |
| 8971 | * reference count by one. if update_ref is true, this function |
| 8972 | * also make sure backrefs for the shared block and all lower level |
| 8973 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 8974 | * |
| 8975 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8976 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 8977 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8978 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 8979 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8980 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8981 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8982 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8983 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8984 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8985 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8986 | struct walk_control *wc; |
| 8987 | struct btrfs_key key; |
| 8988 | int err = 0; |
| 8989 | int ret; |
| 8990 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8991 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8992 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8993 | btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8994 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8995 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8996 | if (!path) { |
| 8997 | err = -ENOMEM; |
| 8998 | goto out; |
| 8999 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9000 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9001 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 9002 | if (!wc) { |
| 9003 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9004 | err = -ENOMEM; |
| 9005 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 9006 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9007 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9008 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9009 | if (IS_ERR(trans)) { |
| 9010 | err = PTR_ERR(trans); |
| 9011 | goto out_free; |
| 9012 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 9013 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9014 | if (block_rsv) |
| 9015 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9016 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9017 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9018 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9019 | path->nodes[level] = btrfs_lock_root_node(root); |
| 9020 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9021 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9022 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9023 | memset(&wc->update_progress, 0, |
| 9024 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9025 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9026 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9027 | memcpy(&wc->update_progress, &key, |
| 9028 | sizeof(wc->update_progress)); |
| 9029 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 9030 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9031 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 9032 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9033 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 9034 | path->lowest_level = 0; |
| 9035 | if (ret < 0) { |
| 9036 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9037 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9038 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 9039 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9040 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9041 | /* |
| 9042 | * unlock our path, this is safe because only this |
| 9043 | * function is allowed to delete this snapshot |
| 9044 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9045 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 9046 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9047 | level = btrfs_header_level(root->node); |
| 9048 | while (1) { |
| 9049 | btrfs_tree_lock(path->nodes[level]); |
| 9050 | btrfs_set_lock_blocking(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 9051 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9052 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9053 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9054 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 9055 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9056 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9057 | if (ret < 0) { |
| 9058 | err = ret; |
| 9059 | goto out_end_trans; |
| 9060 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9061 | BUG_ON(wc->refs[level] == 0); |
| 9062 | |
| 9063 | if (level == root_item->drop_level) |
| 9064 | break; |
| 9065 | |
| 9066 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 9067 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9068 | WARN_ON(wc->refs[level] != 1); |
| 9069 | level--; |
| 9070 | } |
| 9071 | } |
| 9072 | |
| 9073 | wc->level = level; |
| 9074 | wc->shared_level = -1; |
| 9075 | wc->stage = DROP_REFERENCE; |
| 9076 | wc->update_ref = update_ref; |
| 9077 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9078 | wc->for_reloc = for_reloc; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9079 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9080 | |
| 9081 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 9082 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9083 | ret = walk_down_tree(trans, root, path, wc); |
| 9084 | if (ret < 0) { |
| 9085 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 9086 | break; |
| 9087 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9088 | |
| 9089 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 9090 | if (ret < 0) { |
| 9091 | err = ret; |
| 9092 | break; |
| 9093 | } |
| 9094 | |
| 9095 | if (ret > 0) { |
| 9096 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 9097 | break; |
| 9098 | } |
| 9099 | |
| 9100 | if (wc->stage == DROP_REFERENCE) { |
| 9101 | level = wc->level; |
| 9102 | btrfs_node_key(path->nodes[level], |
| 9103 | &root_item->drop_progress, |
| 9104 | path->slots[level]); |
| 9105 | root_item->drop_level = level; |
| 9106 | } |
| 9107 | |
| 9108 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9109 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9110 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9111 | ret = btrfs_update_root(trans, tree_root, |
| 9112 | &root->root_key, |
| 9113 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9114 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9115 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9116 | err = ret; |
| 9117 | goto out_end_trans; |
| 9118 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9119 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9120 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9121 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9122 | btrfs_debug(fs_info, |
| 9123 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 9124 | err = -EAGAIN; |
| 9125 | goto out_free; |
| 9126 | } |
| 9127 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9128 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9129 | if (IS_ERR(trans)) { |
| 9130 | err = PTR_ERR(trans); |
| 9131 | goto out_free; |
| 9132 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9133 | if (block_rsv) |
| 9134 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 9135 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9136 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9137 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9138 | if (err) |
| 9139 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9140 | |
| 9141 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9142 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9143 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9144 | goto out_end_trans; |
| 9145 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9146 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9147 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 9148 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 9149 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9150 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9151 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9152 | err = ret; |
| 9153 | goto out_end_trans; |
| 9154 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 9155 | /* if we fail to delete the orphan item this time |
| 9156 | * around, it'll get picked up the next time. |
| 9157 | * |
| 9158 | * The most common failure here is just -ENOENT. |
| 9159 | */ |
| 9160 | btrfs_del_orphan_item(trans, tree_root, |
| 9161 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9162 | } |
| 9163 | } |
| 9164 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 9165 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 9166 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9167 | } else { |
| 9168 | free_extent_buffer(root->node); |
| 9169 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 9170 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9171 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9172 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9173 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9174 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9175 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9176 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 9177 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9178 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9179 | /* |
| 9180 | * So if we need to stop dropping the snapshot for whatever reason we |
| 9181 | * need to make sure to add it back to the dead root list so that we |
| 9182 | * keep trying to do the work later. This also cleans up roots if we |
| 9183 | * don't have it in the radix (like when we recover after a power fail |
| 9184 | * or unmount) so we don't leak memory. |
| 9185 | */ |
Josef Bacik | b37b39c | 2013-07-23 16:57:15 -0400 | [diff] [blame] | 9186 | if (!for_reloc && root_dropped == false) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9187 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 9188 | if (err && err != -EAGAIN) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9189 | btrfs_handle_fs_error(fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 9190 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9191 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9192 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9193 | /* |
| 9194 | * drop subtree rooted at tree block 'node'. |
| 9195 | * |
| 9196 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9197 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9198 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9199 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 9200 | struct btrfs_root *root, |
| 9201 | struct extent_buffer *node, |
| 9202 | struct extent_buffer *parent) |
| 9203 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9204 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9205 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9206 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9207 | int level; |
| 9208 | int parent_level; |
| 9209 | int ret = 0; |
| 9210 | int wret; |
| 9211 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9212 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 9213 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9214 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9215 | if (!path) |
| 9216 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9217 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9218 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9219 | if (!wc) { |
| 9220 | btrfs_free_path(path); |
| 9221 | return -ENOMEM; |
| 9222 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9223 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9224 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9225 | parent_level = btrfs_header_level(parent); |
| 9226 | extent_buffer_get(parent); |
| 9227 | path->nodes[parent_level] = parent; |
| 9228 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 9229 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9230 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9231 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9232 | path->nodes[level] = node; |
| 9233 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9234 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9235 | |
| 9236 | wc->refs[parent_level] = 1; |
| 9237 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 9238 | wc->level = level; |
| 9239 | wc->shared_level = -1; |
| 9240 | wc->stage = DROP_REFERENCE; |
| 9241 | wc->update_ref = 0; |
| 9242 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9243 | wc->for_reloc = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9244 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9245 | |
| 9246 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9247 | wret = walk_down_tree(trans, root, path, wc); |
| 9248 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9249 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9250 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9251 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9252 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9253 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9254 | if (wret < 0) |
| 9255 | ret = wret; |
| 9256 | if (wret != 0) |
| 9257 | break; |
| 9258 | } |
| 9259 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9260 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9261 | btrfs_free_path(path); |
| 9262 | return ret; |
| 9263 | } |
| 9264 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9265 | 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] | 9266 | { |
| 9267 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9268 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9269 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9270 | /* |
| 9271 | * if restripe for this chunk_type is on pick target profile and |
| 9272 | * return, otherwise do the usual balance |
| 9273 | */ |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9274 | stripped = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9275 | if (stripped) |
| 9276 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 9277 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9278 | num_devices = fs_info->fs_devices->rw_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 9279 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9280 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 9281 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9282 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 9283 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9284 | if (num_devices == 1) { |
| 9285 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9286 | stripped = flags & ~stripped; |
| 9287 | |
| 9288 | /* turn raid0 into single device chunks */ |
| 9289 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 9290 | return stripped; |
| 9291 | |
| 9292 | /* turn mirroring into duplication */ |
| 9293 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9294 | BTRFS_BLOCK_GROUP_RAID10)) |
| 9295 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9296 | } else { |
| 9297 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9298 | if (flags & stripped) |
| 9299 | return flags; |
| 9300 | |
| 9301 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9302 | stripped = flags & ~stripped; |
| 9303 | |
| 9304 | /* switch duplicated blocks with raid1 */ |
| 9305 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 9306 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 9307 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9308 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9309 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9310 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9311 | return flags; |
| 9312 | } |
| 9313 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9314 | 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] | 9315 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9316 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9317 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9318 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9319 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9320 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9321 | /* |
| 9322 | * We need some metadata space and system metadata space for |
| 9323 | * allocating chunks in some corner cases until we force to set |
| 9324 | * it to be readonly. |
| 9325 | */ |
| 9326 | if ((sinfo->flags & |
| 9327 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 9328 | !force) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9329 | min_allocable_bytes = SZ_1M; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9330 | else |
| 9331 | min_allocable_bytes = 0; |
| 9332 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9333 | spin_lock(&sinfo->lock); |
| 9334 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9335 | |
| 9336 | if (cache->ro) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9337 | cache->ro++; |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9338 | ret = 0; |
| 9339 | goto out; |
| 9340 | } |
| 9341 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9342 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 9343 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9344 | |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 9345 | if (btrfs_space_info_used(sinfo, true) + num_bytes + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 9346 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9347 | sinfo->bytes_readonly += num_bytes; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9348 | cache->ro++; |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9349 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9350 | ret = 0; |
| 9351 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9352 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9353 | spin_unlock(&cache->lock); |
| 9354 | spin_unlock(&sinfo->lock); |
| 9355 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9356 | } |
| 9357 | |
Jeff Mahoney | 5e00f19 | 2017-02-15 16:28:29 -0500 | [diff] [blame] | 9358 | int btrfs_inc_block_group_ro(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9359 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9360 | |
| 9361 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9362 | struct btrfs_trans_handle *trans; |
| 9363 | u64 alloc_flags; |
| 9364 | int ret; |
| 9365 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9366 | again: |
Jeff Mahoney | 5e00f19 | 2017-02-15 16:28:29 -0500 | [diff] [blame] | 9367 | trans = btrfs_join_transaction(fs_info->extent_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9368 | if (IS_ERR(trans)) |
| 9369 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9370 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9371 | /* |
| 9372 | * we're not allowed to set block groups readonly after the dirty |
| 9373 | * block groups cache has started writing. If it already started, |
| 9374 | * back off and let this transaction commit |
| 9375 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9376 | mutex_lock(&fs_info->ro_block_group_mutex); |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 9377 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9378 | u64 transid = trans->transid; |
| 9379 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9380 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9381 | btrfs_end_transaction(trans); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9382 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9383 | ret = btrfs_wait_for_commit(fs_info, transid); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9384 | if (ret) |
| 9385 | return ret; |
| 9386 | goto again; |
| 9387 | } |
| 9388 | |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9389 | /* |
| 9390 | * if we are changing raid levels, try to allocate a corresponding |
| 9391 | * block group with the new raid level. |
| 9392 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9393 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9394 | if (alloc_flags != cache->flags) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9395 | ret = do_chunk_alloc(trans, fs_info, alloc_flags, |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9396 | CHUNK_ALLOC_FORCE); |
| 9397 | /* |
| 9398 | * ENOSPC is allowed here, we may have enough space |
| 9399 | * already allocated at the new raid level to |
| 9400 | * carry on |
| 9401 | */ |
| 9402 | if (ret == -ENOSPC) |
| 9403 | ret = 0; |
| 9404 | if (ret < 0) |
| 9405 | goto out; |
| 9406 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9407 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9408 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9409 | if (!ret) |
| 9410 | goto out; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9411 | alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags); |
| 9412 | ret = do_chunk_alloc(trans, fs_info, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 9413 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9414 | if (ret < 0) |
| 9415 | goto out; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9416 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9417 | out: |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9418 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9419 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 9420 | mutex_lock(&fs_info->chunk_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9421 | check_system_chunk(trans, fs_info, alloc_flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 9422 | mutex_unlock(&fs_info->chunk_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9423 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9424 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9425 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9426 | btrfs_end_transaction(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9427 | return ret; |
| 9428 | } |
| 9429 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9430 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9431 | struct btrfs_fs_info *fs_info, u64 type) |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9432 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9433 | u64 alloc_flags = get_alloc_profile(fs_info, type); |
| 9434 | |
| 9435 | return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9436 | } |
| 9437 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9438 | /* |
| 9439 | * 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] | 9440 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9441 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9442 | 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] | 9443 | { |
| 9444 | struct btrfs_block_group_cache *block_group; |
| 9445 | u64 free_bytes = 0; |
| 9446 | int factor; |
| 9447 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9448 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9449 | if (list_empty(&sinfo->ro_bgs)) |
| 9450 | return 0; |
| 9451 | |
| 9452 | spin_lock(&sinfo->lock); |
| 9453 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9454 | spin_lock(&block_group->lock); |
| 9455 | |
| 9456 | if (!block_group->ro) { |
| 9457 | spin_unlock(&block_group->lock); |
| 9458 | continue; |
| 9459 | } |
| 9460 | |
| 9461 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9462 | BTRFS_BLOCK_GROUP_RAID10 | |
| 9463 | BTRFS_BLOCK_GROUP_DUP)) |
| 9464 | factor = 2; |
| 9465 | else |
| 9466 | factor = 1; |
| 9467 | |
| 9468 | free_bytes += (block_group->key.offset - |
| 9469 | btrfs_block_group_used(&block_group->item)) * |
| 9470 | factor; |
| 9471 | |
| 9472 | spin_unlock(&block_group->lock); |
| 9473 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9474 | spin_unlock(&sinfo->lock); |
| 9475 | |
| 9476 | return free_bytes; |
| 9477 | } |
| 9478 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9479 | void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9480 | { |
| 9481 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9482 | u64 num_bytes; |
| 9483 | |
| 9484 | BUG_ON(!cache->ro); |
| 9485 | |
| 9486 | spin_lock(&sinfo->lock); |
| 9487 | spin_lock(&cache->lock); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9488 | if (!--cache->ro) { |
| 9489 | num_bytes = cache->key.offset - cache->reserved - |
| 9490 | cache->pinned - cache->bytes_super - |
| 9491 | btrfs_block_group_used(&cache->item); |
| 9492 | sinfo->bytes_readonly -= num_bytes; |
| 9493 | list_del_init(&cache->ro_list); |
| 9494 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9495 | spin_unlock(&cache->lock); |
| 9496 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9497 | } |
| 9498 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9499 | /* |
| 9500 | * checks to see if its even possible to relocate this block group. |
| 9501 | * |
| 9502 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 9503 | * ok to go ahead and try. |
| 9504 | */ |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9505 | int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9506 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9507 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9508 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9509 | struct btrfs_space_info *space_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9510 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9511 | struct btrfs_device *device; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9512 | struct btrfs_trans_handle *trans; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9513 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9514 | u64 dev_min = 1; |
| 9515 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9516 | u64 target; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9517 | int debug; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9518 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9519 | int full = 0; |
| 9520 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9521 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9522 | debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG); |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9523 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9524 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9525 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9526 | /* odd, couldn't find the block group, leave it alone */ |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9527 | if (!block_group) { |
| 9528 | if (debug) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9529 | btrfs_warn(fs_info, |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9530 | "can't find block group for bytenr %llu", |
| 9531 | bytenr); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9532 | return -1; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9533 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9534 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9535 | min_free = btrfs_block_group_used(&block_group->item); |
| 9536 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9537 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9538 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9539 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9540 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9541 | space_info = block_group->space_info; |
| 9542 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9543 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9544 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9545 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9546 | /* |
| 9547 | * 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] | 9548 | * relocate it unless we're able to allocate a new chunk below. |
| 9549 | * |
| 9550 | * Otherwise, we need to make sure we have room in the space to handle |
| 9551 | * 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] | 9552 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 9553 | if ((space_info->total_bytes != block_group->key.offset) && |
Liu Bo | 4136135 | 2017-02-13 15:42:21 -0800 | [diff] [blame] | 9554 | (btrfs_space_info_used(space_info, false) + min_free < |
| 9555 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9556 | spin_unlock(&space_info->lock); |
| 9557 | goto out; |
| 9558 | } |
| 9559 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9560 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9561 | /* |
| 9562 | * ok we don't have enough space, but maybe we have free space on our |
| 9563 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9564 | * alloc devices and guess if we have enough space. if this block |
| 9565 | * group is going to be restriped, run checks against the target |
| 9566 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9567 | */ |
| 9568 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 9569 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9570 | /* |
| 9571 | * index: |
| 9572 | * 0: raid10 |
| 9573 | * 1: raid1 |
| 9574 | * 2: dup |
| 9575 | * 3: raid0 |
| 9576 | * 4: single |
| 9577 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9578 | target = get_restripe_target(fs_info, block_group->flags); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9579 | if (target) { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 9580 | index = __get_raid_index(extended_to_chunk(target)); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9581 | } else { |
| 9582 | /* |
| 9583 | * this is just a balance, so if we were marked as full |
| 9584 | * we know there is no space for a new chunk |
| 9585 | */ |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9586 | if (full) { |
| 9587 | if (debug) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9588 | btrfs_warn(fs_info, |
| 9589 | "no space to alloc new chunk for block group %llu", |
| 9590 | block_group->key.objectid); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9591 | goto out; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9592 | } |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9593 | |
| 9594 | index = get_block_group_index(block_group); |
| 9595 | } |
| 9596 | |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9597 | if (index == BTRFS_RAID_RAID10) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9598 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9599 | /* Divide by 2 */ |
| 9600 | min_free >>= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9601 | } else if (index == BTRFS_RAID_RAID1) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9602 | dev_min = 2; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9603 | } else if (index == BTRFS_RAID_DUP) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9604 | /* Multiply by 2 */ |
| 9605 | min_free <<= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9606 | } else if (index == BTRFS_RAID_RAID0) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9607 | dev_min = fs_devices->rw_devices; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 9608 | min_free = div64_u64(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9611 | /* We need to do this so that we can look at pending chunks */ |
| 9612 | trans = btrfs_join_transaction(root); |
| 9613 | if (IS_ERR(trans)) { |
| 9614 | ret = PTR_ERR(trans); |
| 9615 | goto out; |
| 9616 | } |
| 9617 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9618 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9619 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9620 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9621 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9622 | /* |
| 9623 | * check to make sure we can actually find a chunk with enough |
| 9624 | * space to fit our block group in. |
| 9625 | */ |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 9626 | if (device->total_bytes > device->bytes_used + min_free && |
| 9627 | !device->is_tgtdev_for_dev_replace) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9628 | ret = find_free_dev_extent(trans, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9629 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9630 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9631 | dev_nr++; |
| 9632 | |
| 9633 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9634 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9635 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9636 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9637 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9638 | } |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9639 | if (debug && ret == -1) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9640 | btrfs_warn(fs_info, |
| 9641 | "no space to allocate a new chunk for block group %llu", |
| 9642 | block_group->key.objectid); |
| 9643 | mutex_unlock(&fs_info->chunk_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9644 | btrfs_end_transaction(trans); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9645 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9646 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9647 | return ret; |
| 9648 | } |
| 9649 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9650 | static int find_first_block_group(struct btrfs_fs_info *fs_info, |
| 9651 | struct btrfs_path *path, |
| 9652 | struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9653 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9654 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9655 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9656 | struct btrfs_key found_key; |
| 9657 | struct extent_buffer *leaf; |
| 9658 | int slot; |
| 9659 | |
| 9660 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 9661 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9662 | goto out; |
| 9663 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9664 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9665 | slot = path->slots[0]; |
| 9666 | leaf = path->nodes[0]; |
| 9667 | if (slot >= btrfs_header_nritems(leaf)) { |
| 9668 | ret = btrfs_next_leaf(root, path); |
| 9669 | if (ret == 0) |
| 9670 | continue; |
| 9671 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9672 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9673 | break; |
| 9674 | } |
| 9675 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 9676 | |
| 9677 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9678 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 9679 | struct extent_map_tree *em_tree; |
| 9680 | struct extent_map *em; |
| 9681 | |
| 9682 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 9683 | read_lock(&em_tree->lock); |
| 9684 | em = lookup_extent_mapping(em_tree, found_key.objectid, |
| 9685 | found_key.offset); |
| 9686 | read_unlock(&em_tree->lock); |
| 9687 | if (!em) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9688 | btrfs_err(fs_info, |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 9689 | "logical %llu len %llu found bg but no related chunk", |
| 9690 | found_key.objectid, found_key.offset); |
| 9691 | ret = -ENOENT; |
| 9692 | } else { |
| 9693 | ret = 0; |
| 9694 | } |
Josef Bacik | 187ee58 | 2016-08-18 15:30:06 -0400 | [diff] [blame] | 9695 | free_extent_map(em); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9696 | goto out; |
| 9697 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9698 | path->slots[0]++; |
| 9699 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9700 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9701 | return ret; |
| 9702 | } |
| 9703 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9704 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 9705 | { |
| 9706 | struct btrfs_block_group_cache *block_group; |
| 9707 | u64 last = 0; |
| 9708 | |
| 9709 | while (1) { |
| 9710 | struct inode *inode; |
| 9711 | |
| 9712 | block_group = btrfs_lookup_first_block_group(info, last); |
| 9713 | while (block_group) { |
| 9714 | spin_lock(&block_group->lock); |
| 9715 | if (block_group->iref) |
| 9716 | break; |
| 9717 | spin_unlock(&block_group->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9718 | block_group = next_block_group(info, block_group); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9719 | } |
| 9720 | if (!block_group) { |
| 9721 | if (last == 0) |
| 9722 | break; |
| 9723 | last = 0; |
| 9724 | continue; |
| 9725 | } |
| 9726 | |
| 9727 | inode = block_group->inode; |
| 9728 | block_group->iref = 0; |
| 9729 | block_group->inode = NULL; |
| 9730 | spin_unlock(&block_group->lock); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 9731 | ASSERT(block_group->io_ctl.inode == NULL); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9732 | iput(inode); |
| 9733 | last = block_group->key.objectid + block_group->key.offset; |
| 9734 | btrfs_put_block_group(block_group); |
| 9735 | } |
| 9736 | } |
| 9737 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9738 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 9739 | { |
| 9740 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9741 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9742 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9743 | struct rb_node *n; |
| 9744 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9745 | down_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9746 | while (!list_empty(&info->caching_block_groups)) { |
| 9747 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 9748 | struct btrfs_caching_control, list); |
| 9749 | list_del(&caching_ctl->list); |
| 9750 | put_caching_control(caching_ctl); |
| 9751 | } |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9752 | up_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9753 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9754 | spin_lock(&info->unused_bgs_lock); |
| 9755 | while (!list_empty(&info->unused_bgs)) { |
| 9756 | block_group = list_first_entry(&info->unused_bgs, |
| 9757 | struct btrfs_block_group_cache, |
| 9758 | bg_list); |
| 9759 | list_del_init(&block_group->bg_list); |
| 9760 | btrfs_put_block_group(block_group); |
| 9761 | } |
| 9762 | spin_unlock(&info->unused_bgs_lock); |
| 9763 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9764 | spin_lock(&info->block_group_cache_lock); |
| 9765 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 9766 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 9767 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9768 | rb_erase(&block_group->cache_node, |
| 9769 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 9770 | RB_CLEAR_NODE(&block_group->cache_node); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9771 | spin_unlock(&info->block_group_cache_lock); |
| 9772 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9773 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9774 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9775 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9776 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9777 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9778 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9779 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9780 | /* |
| 9781 | * We haven't cached this block group, which means we could |
| 9782 | * possibly have excluded extents on this block group. |
| 9783 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 9784 | if (block_group->cached == BTRFS_CACHE_NO || |
| 9785 | block_group->cached == BTRFS_CACHE_ERROR) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9786 | free_excluded_extents(info, block_group); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9787 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9788 | btrfs_remove_free_space_cache(block_group); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 9789 | ASSERT(list_empty(&block_group->dirty_list)); |
| 9790 | ASSERT(list_empty(&block_group->io_list)); |
| 9791 | ASSERT(list_empty(&block_group->bg_list)); |
| 9792 | ASSERT(atomic_read(&block_group->count) == 1); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 9793 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9794 | |
| 9795 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9796 | } |
| 9797 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9798 | |
| 9799 | /* now that all the block groups are freed, go through and |
| 9800 | * free all the space_info structs. This is only called during |
| 9801 | * the final stages of unmount, and so we know nobody is |
| 9802 | * using them. We call synchronize_rcu() once before we start, |
| 9803 | * just to be on the safe side. |
| 9804 | */ |
| 9805 | synchronize_rcu(); |
| 9806 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 9807 | release_global_block_rsv(info); |
| 9808 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9809 | while (!list_empty(&info->space_info)) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9810 | int i; |
| 9811 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9812 | space_info = list_entry(info->space_info.next, |
| 9813 | struct btrfs_space_info, |
| 9814 | list); |
Josef Bacik | d555b6c | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 9815 | |
| 9816 | /* |
| 9817 | * Do not hide this behind enospc_debug, this is actually |
| 9818 | * important and indicates a real bug if this happens. |
| 9819 | */ |
| 9820 | if (WARN_ON(space_info->bytes_pinned > 0 || |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 9821 | space_info->bytes_reserved > 0 || |
Josef Bacik | d555b6c | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 9822 | space_info->bytes_may_use > 0)) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9823 | dump_space_info(info, space_info, 0, 0); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9824 | list_del(&space_info->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9825 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 9826 | struct kobject *kobj; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9827 | kobj = space_info->block_group_kobjs[i]; |
| 9828 | space_info->block_group_kobjs[i] = NULL; |
| 9829 | if (kobj) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9830 | kobject_del(kobj); |
| 9831 | kobject_put(kobj); |
| 9832 | } |
| 9833 | } |
| 9834 | kobject_del(&space_info->kobj); |
| 9835 | kobject_put(&space_info->kobj); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9836 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9837 | return 0; |
| 9838 | } |
| 9839 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9840 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 9841 | struct btrfs_block_group_cache *cache) |
| 9842 | { |
| 9843 | int index = get_block_group_index(cache); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9844 | bool first = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9845 | |
| 9846 | down_write(&space_info->groups_sem); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9847 | if (list_empty(&space_info->block_groups[index])) |
| 9848 | first = true; |
| 9849 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 9850 | up_write(&space_info->groups_sem); |
| 9851 | |
| 9852 | if (first) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9853 | struct raid_kobject *rkobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9854 | int ret; |
| 9855 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9856 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 9857 | if (!rkobj) |
| 9858 | goto out_err; |
| 9859 | rkobj->raid_type = index; |
| 9860 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 9861 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, |
| 9862 | "%s", get_raid_name(index)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9863 | if (ret) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9864 | kobject_put(&rkobj->kobj); |
| 9865 | goto out_err; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9866 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9867 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9868 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9869 | |
| 9870 | return; |
| 9871 | out_err: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9872 | btrfs_warn(cache->fs_info, |
| 9873 | "failed to add kobject for block cache, ignoring"); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9874 | } |
| 9875 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9876 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9877 | btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info, |
| 9878 | u64 start, u64 size) |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9879 | { |
| 9880 | struct btrfs_block_group_cache *cache; |
| 9881 | |
| 9882 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 9883 | if (!cache) |
| 9884 | return NULL; |
| 9885 | |
| 9886 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 9887 | GFP_NOFS); |
| 9888 | if (!cache->free_space_ctl) { |
| 9889 | kfree(cache); |
| 9890 | return NULL; |
| 9891 | } |
| 9892 | |
| 9893 | cache->key.objectid = start; |
| 9894 | cache->key.offset = size; |
| 9895 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 9896 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9897 | cache->sectorsize = fs_info->sectorsize; |
| 9898 | cache->fs_info = fs_info; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9899 | cache->full_stripe_len = btrfs_full_stripe_len(fs_info, |
| 9900 | &fs_info->mapping_tree, |
| 9901 | start); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 9902 | set_free_space_tree_thresholds(cache); |
| 9903 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9904 | atomic_set(&cache->count, 1); |
| 9905 | spin_lock_init(&cache->lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9906 | init_rwsem(&cache->data_rwsem); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9907 | INIT_LIST_HEAD(&cache->list); |
| 9908 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9909 | INIT_LIST_HEAD(&cache->bg_list); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9910 | INIT_LIST_HEAD(&cache->ro_list); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9911 | INIT_LIST_HEAD(&cache->dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 9912 | INIT_LIST_HEAD(&cache->io_list); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9913 | btrfs_init_free_space_ctl(cache); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 9914 | atomic_set(&cache->trimming, 0); |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 9915 | mutex_init(&cache->free_space_lock); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9916 | |
| 9917 | return cache; |
| 9918 | } |
| 9919 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 9920 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9921 | { |
| 9922 | struct btrfs_path *path; |
| 9923 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9924 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9925 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9926 | struct btrfs_key key; |
| 9927 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9928 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9929 | int need_clear = 0; |
| 9930 | u64 cache_gen; |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 9931 | u64 feature; |
| 9932 | int mixed; |
| 9933 | |
| 9934 | feature = btrfs_super_incompat_flags(info->super_copy); |
| 9935 | mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 9936 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9937 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9938 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9939 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9940 | path = btrfs_alloc_path(); |
| 9941 | if (!path) |
| 9942 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 9943 | path->reada = READA_FORWARD; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9944 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9945 | cache_gen = btrfs_super_cache_generation(info->super_copy); |
| 9946 | if (btrfs_test_opt(info, SPACE_CACHE) && |
| 9947 | btrfs_super_generation(info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9948 | need_clear = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9949 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 9950 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9951 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9952 | while (1) { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9953 | ret = find_first_block_group(info, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9954 | if (ret > 0) |
| 9955 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9956 | if (ret != 0) |
| 9957 | goto error; |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9958 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9959 | leaf = path->nodes[0]; |
| 9960 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9961 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9962 | cache = btrfs_create_block_group_cache(info, found_key.objectid, |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9963 | found_key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9964 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9965 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9966 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9967 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9968 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9969 | if (need_clear) { |
| 9970 | /* |
| 9971 | * When we mount with old space cache, we need to |
| 9972 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 9973 | * |
| 9974 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 9975 | * truncate the old free space cache inode and |
| 9976 | * setup a new one. |
| 9977 | * b) Setting 'dirty flag' makes sure that we flush |
| 9978 | * the new space cache info onto disk. |
| 9979 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9980 | if (btrfs_test_opt(info, SPACE_CACHE)) |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9981 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9982 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9983 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9984 | read_extent_buffer(leaf, &cache->item, |
| 9985 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 9986 | sizeof(cache->item)); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9987 | cache->flags = btrfs_block_group_flags(&cache->item); |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 9988 | if (!mixed && |
| 9989 | ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && |
| 9990 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { |
| 9991 | btrfs_err(info, |
| 9992 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", |
| 9993 | cache->key.objectid); |
| 9994 | ret = -EINVAL; |
| 9995 | goto error; |
| 9996 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9997 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9998 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9999 | btrfs_release_path(path); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 10000 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10001 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 10002 | * We need to exclude the super stripes now so that the space |
| 10003 | * info has super bytes accounted for, otherwise we'll think |
| 10004 | * we have more space than we actually do. |
| 10005 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10006 | ret = exclude_super_stripes(info, cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10007 | if (ret) { |
| 10008 | /* |
| 10009 | * We may have excluded something, so call this just in |
| 10010 | * case. |
| 10011 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10012 | free_excluded_extents(info, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10013 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10014 | goto error; |
| 10015 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 10016 | |
| 10017 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10018 | * check for two cases, either we are full, and therefore |
| 10019 | * don't need to bother with the caching work since we won't |
| 10020 | * find any space, or we are empty, and we can just add all |
| 10021 | * the space in and be done with it. This saves us _alot_ of |
| 10022 | * time, particularly in the full case. |
| 10023 | */ |
| 10024 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10025 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10026 | cache->cached = BTRFS_CACHE_FINISHED; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10027 | free_excluded_extents(info, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10028 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
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 | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10031 | add_new_free_space(cache, info, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10032 | found_key.objectid, |
| 10033 | found_key.objectid + |
| 10034 | found_key.offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10035 | free_excluded_extents(info, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10036 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 10037 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10038 | ret = btrfs_add_block_group_cache(info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10039 | if (ret) { |
| 10040 | btrfs_remove_free_space_cache(cache); |
| 10041 | btrfs_put_block_group(cache); |
| 10042 | goto error; |
| 10043 | } |
| 10044 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10045 | trace_btrfs_add_block_group(info, cache, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10046 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 10047 | btrfs_block_group_used(&cache->item), |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10048 | cache->bytes_super, &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10049 | if (ret) { |
| 10050 | btrfs_remove_free_space_cache(cache); |
| 10051 | spin_lock(&info->block_group_cache_lock); |
| 10052 | rb_erase(&cache->cache_node, |
| 10053 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 10054 | RB_CLEAR_NODE(&cache->cache_node); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10055 | spin_unlock(&info->block_group_cache_lock); |
| 10056 | btrfs_put_block_group(cache); |
| 10057 | goto error; |
| 10058 | } |
| 10059 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10060 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 10061 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10062 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10063 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10064 | set_avail_alloc_bits(info, cache->flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10065 | if (btrfs_chunk_readonly(info, cache->key.objectid)) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10066 | inc_block_group_ro(cache, 1); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10067 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
| 10068 | spin_lock(&info->unused_bgs_lock); |
| 10069 | /* Should always be true but just in case. */ |
| 10070 | if (list_empty(&cache->bg_list)) { |
| 10071 | btrfs_get_block_group(cache); |
| 10072 | list_add_tail(&cache->bg_list, |
| 10073 | &info->unused_bgs); |
| 10074 | } |
| 10075 | spin_unlock(&info->unused_bgs_lock); |
| 10076 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10077 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10078 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10079 | list_for_each_entry_rcu(space_info, &info->space_info, list) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10080 | if (!(get_alloc_profile(info, space_info->flags) & |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10081 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 10082 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 10083 | BTRFS_BLOCK_GROUP_RAID5 | |
| 10084 | BTRFS_BLOCK_GROUP_RAID6 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10085 | BTRFS_BLOCK_GROUP_DUP))) |
| 10086 | continue; |
| 10087 | /* |
| 10088 | * avoid allocating from un-mirrored block group if there are |
| 10089 | * mirrored block groups. |
| 10090 | */ |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 10091 | list_for_each_entry(cache, |
| 10092 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 10093 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10094 | inc_block_group_ro(cache, 1); |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 10095 | list_for_each_entry(cache, |
| 10096 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 10097 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10098 | inc_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10099 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 10100 | |
| 10101 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10102 | ret = 0; |
| 10103 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10104 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10105 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10106 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10107 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10108 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10109 | struct btrfs_fs_info *fs_info) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10110 | { |
| 10111 | struct btrfs_block_group_cache *block_group, *tmp; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10112 | struct btrfs_root *extent_root = fs_info->extent_root; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10113 | struct btrfs_block_group_item item; |
| 10114 | struct btrfs_key key; |
| 10115 | int ret = 0; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10116 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10117 | |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10118 | trans->can_flush_pending_bgs = false; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10119 | 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] | 10120 | if (ret) |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 10121 | goto next; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10122 | |
| 10123 | spin_lock(&block_group->lock); |
| 10124 | memcpy(&item, &block_group->item, sizeof(item)); |
| 10125 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10126 | spin_unlock(&block_group->lock); |
| 10127 | |
| 10128 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 10129 | sizeof(item)); |
| 10130 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10131 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10132 | ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid, |
| 10133 | key.offset); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 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 | add_block_group_free_space(trans, fs_info, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10137 | /* already aborted the transaction if it failed. */ |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 10138 | next: |
| 10139 | list_del_init(&block_group->bg_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10140 | } |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10141 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10142 | } |
| 10143 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10144 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10145 | struct btrfs_fs_info *fs_info, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 10146 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10147 | u64 size) |
| 10148 | { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10149 | struct btrfs_block_group_cache *cache; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10150 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10151 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10152 | btrfs_set_log_full_commit(fs_info, trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10153 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10154 | cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 10155 | if (!cache) |
| 10156 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 10157 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10158 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10159 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10160 | btrfs_set_block_group_flags(&cache->item, type); |
| 10161 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10162 | cache->flags = type; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10163 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10164 | cache->cached = BTRFS_CACHE_FINISHED; |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10165 | cache->needs_free_space = 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10166 | ret = exclude_super_stripes(fs_info, cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10167 | if (ret) { |
| 10168 | /* |
| 10169 | * We may have excluded something, so call this just in |
| 10170 | * case. |
| 10171 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10172 | free_excluded_extents(fs_info, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10173 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10174 | return ret; |
| 10175 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10176 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10177 | add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10178 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10179 | free_excluded_extents(fs_info, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10180 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10181 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10182 | if (btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10183 | u64 new_bytes_used = size - bytes_used; |
| 10184 | |
| 10185 | bytes_used += new_bytes_used >> 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10186 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10187 | } |
| 10188 | #endif |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10189 | /* |
| 10190 | * Call to ensure the corresponding space_info object is created and |
| 10191 | * assigned to our block group, but don't update its counters just yet. |
| 10192 | * We want our bg to be added to the rbtree with its ->space_info set. |
| 10193 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10194 | ret = update_space_info(fs_info, cache->flags, 0, 0, 0, |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10195 | &cache->space_info); |
| 10196 | if (ret) { |
| 10197 | btrfs_remove_free_space_cache(cache); |
| 10198 | btrfs_put_block_group(cache); |
| 10199 | return ret; |
| 10200 | } |
| 10201 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10202 | ret = btrfs_add_block_group_cache(fs_info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10203 | if (ret) { |
| 10204 | btrfs_remove_free_space_cache(cache); |
| 10205 | btrfs_put_block_group(cache); |
| 10206 | return ret; |
| 10207 | } |
| 10208 | |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10209 | /* |
| 10210 | * Now that our block group has its ->space_info set and is inserted in |
| 10211 | * the rbtree, update the space info's counters. |
| 10212 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10213 | trace_btrfs_add_block_group(fs_info, cache, 1); |
| 10214 | ret = update_space_info(fs_info, cache->flags, size, bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10215 | cache->bytes_super, &cache->space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10216 | if (ret) { |
| 10217 | btrfs_remove_free_space_cache(cache); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10218 | spin_lock(&fs_info->block_group_cache_lock); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10219 | rb_erase(&cache->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10220 | &fs_info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 10221 | RB_CLEAR_NODE(&cache->cache_node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10222 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10223 | btrfs_put_block_group(cache); |
| 10224 | return ret; |
| 10225 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10226 | update_global_block_rsv(fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 10227 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10228 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10229 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10230 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10231 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10232 | set_avail_alloc_bits(fs_info, type); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10233 | return 0; |
| 10234 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10235 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10236 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 10237 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 10238 | u64 extra_flags = chunk_to_extended(flags) & |
| 10239 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10240 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10241 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10242 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 10243 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 10244 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 10245 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 10246 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 10247 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10248 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10249 | } |
| 10250 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10251 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10252 | struct btrfs_fs_info *fs_info, u64 group_start, |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10253 | struct extent_map *em) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10254 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10255 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10256 | struct btrfs_path *path; |
| 10257 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10258 | struct btrfs_free_cluster *cluster; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10259 | struct btrfs_root *tree_root = fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10260 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10261 | struct inode *inode; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10262 | struct kobject *kobj = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10263 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10264 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10265 | int factor; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10266 | struct btrfs_caching_control *caching_ctl = NULL; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10267 | bool remove_em; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10268 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10269 | block_group = btrfs_lookup_block_group(fs_info, group_start); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10270 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10271 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10272 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10273 | /* |
| 10274 | * Free the reserved super bytes from this block group before |
| 10275 | * remove it. |
| 10276 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10277 | free_excluded_extents(fs_info, block_group); |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10278 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10279 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10280 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10281 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 10282 | BTRFS_BLOCK_GROUP_RAID1 | |
| 10283 | BTRFS_BLOCK_GROUP_RAID10)) |
| 10284 | factor = 2; |
| 10285 | else |
| 10286 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10287 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10288 | /* make sure this block group isn't part of an allocation cluster */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10289 | cluster = &fs_info->data_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10290 | spin_lock(&cluster->refill_lock); |
| 10291 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10292 | spin_unlock(&cluster->refill_lock); |
| 10293 | |
| 10294 | /* |
| 10295 | * make sure this block group isn't part of a metadata |
| 10296 | * allocation cluster |
| 10297 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10298 | cluster = &fs_info->meta_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10299 | spin_lock(&cluster->refill_lock); |
| 10300 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10301 | spin_unlock(&cluster->refill_lock); |
| 10302 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10303 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10304 | if (!path) { |
| 10305 | ret = -ENOMEM; |
| 10306 | goto out; |
| 10307 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10308 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10309 | /* |
| 10310 | * get the inode first so any iput calls done for the io_list |
| 10311 | * aren't the final iput (no unlinks allowed now) |
| 10312 | */ |
Jeff Mahoney | 77ab86b | 2017-02-15 16:28:30 -0500 | [diff] [blame] | 10313 | inode = lookup_free_space_inode(fs_info, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10314 | |
| 10315 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 10316 | /* |
| 10317 | * make sure our free spache cache IO is done before remove the |
| 10318 | * free space inode |
| 10319 | */ |
| 10320 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10321 | if (!list_empty(&block_group->io_list)) { |
| 10322 | list_del_init(&block_group->io_list); |
| 10323 | |
| 10324 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); |
| 10325 | |
| 10326 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 10327 | btrfs_wait_cache_io(trans, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10328 | btrfs_put_block_group(block_group); |
| 10329 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10330 | } |
| 10331 | |
| 10332 | if (!list_empty(&block_group->dirty_list)) { |
| 10333 | list_del_init(&block_group->dirty_list); |
| 10334 | btrfs_put_block_group(block_group); |
| 10335 | } |
| 10336 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 10337 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 10338 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10339 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 10340 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10341 | if (ret) { |
| 10342 | btrfs_add_delayed_iput(inode); |
| 10343 | goto out; |
| 10344 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10345 | clear_nlink(inode); |
| 10346 | /* One for the block groups ref */ |
| 10347 | spin_lock(&block_group->lock); |
| 10348 | if (block_group->iref) { |
| 10349 | block_group->iref = 0; |
| 10350 | block_group->inode = NULL; |
| 10351 | spin_unlock(&block_group->lock); |
| 10352 | iput(inode); |
| 10353 | } else { |
| 10354 | spin_unlock(&block_group->lock); |
| 10355 | } |
| 10356 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 10357 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10358 | } |
| 10359 | |
| 10360 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 10361 | key.offset = block_group->key.objectid; |
| 10362 | key.type = 0; |
| 10363 | |
| 10364 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 10365 | if (ret < 0) |
| 10366 | goto out; |
| 10367 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10368 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10369 | if (ret == 0) { |
| 10370 | ret = btrfs_del_item(trans, tree_root, path); |
| 10371 | if (ret) |
| 10372 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10373 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10374 | } |
| 10375 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10376 | spin_lock(&fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10377 | rb_erase(&block_group->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10378 | &fs_info->block_group_cache_tree); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 10379 | RB_CLEAR_NODE(&block_group->cache_node); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 10380 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10381 | if (fs_info->first_logical_byte == block_group->key.objectid) |
| 10382 | fs_info->first_logical_byte = (u64)-1; |
| 10383 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10384 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10385 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10386 | /* |
| 10387 | * we must use list_del_init so people can check to see if they |
| 10388 | * are still on the list after taking the semaphore |
| 10389 | */ |
| 10390 | list_del_init(&block_group->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10391 | if (list_empty(&block_group->space_info->block_groups[index])) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10392 | kobj = block_group->space_info->block_group_kobjs[index]; |
| 10393 | block_group->space_info->block_group_kobjs[index] = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10394 | clear_avail_alloc_bits(fs_info, block_group->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10395 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10396 | up_write(&block_group->space_info->groups_sem); |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10397 | if (kobj) { |
| 10398 | kobject_del(kobj); |
| 10399 | kobject_put(kobj); |
| 10400 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10401 | |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10402 | if (block_group->has_caching_ctl) |
| 10403 | caching_ctl = get_caching_control(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10404 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10405 | wait_block_group_cache_done(block_group); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10406 | if (block_group->has_caching_ctl) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10407 | down_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10408 | if (!caching_ctl) { |
| 10409 | struct btrfs_caching_control *ctl; |
| 10410 | |
| 10411 | list_for_each_entry(ctl, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10412 | &fs_info->caching_block_groups, list) |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10413 | if (ctl->block_group == block_group) { |
| 10414 | caching_ctl = ctl; |
| 10415 | atomic_inc(&caching_ctl->count); |
| 10416 | break; |
| 10417 | } |
| 10418 | } |
| 10419 | if (caching_ctl) |
| 10420 | list_del_init(&caching_ctl->list); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10421 | up_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10422 | if (caching_ctl) { |
| 10423 | /* Once for the caching bgs list and once for us. */ |
| 10424 | put_caching_control(caching_ctl); |
| 10425 | put_caching_control(caching_ctl); |
| 10426 | } |
| 10427 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10428 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10429 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10430 | if (!list_empty(&block_group->dirty_list)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10431 | WARN_ON(1); |
| 10432 | } |
| 10433 | if (!list_empty(&block_group->io_list)) { |
| 10434 | WARN_ON(1); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10435 | } |
| 10436 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10437 | btrfs_remove_free_space_cache(block_group); |
| 10438 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10439 | spin_lock(&block_group->space_info->lock); |
Filipe Manana | 75c68e9 | 2015-01-16 13:24:40 +0000 | [diff] [blame] | 10440 | list_del_init(&block_group->ro_list); |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10441 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10442 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10443 | WARN_ON(block_group->space_info->total_bytes |
| 10444 | < block_group->key.offset); |
| 10445 | WARN_ON(block_group->space_info->bytes_readonly |
| 10446 | < block_group->key.offset); |
| 10447 | WARN_ON(block_group->space_info->disk_total |
| 10448 | < block_group->key.offset * factor); |
| 10449 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10450 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 10451 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10452 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10453 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10454 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 10455 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10456 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10457 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 10458 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 10459 | if (!list_empty(&em->list)) { |
| 10460 | /* We're in the transaction->pending_chunks list. */ |
| 10461 | free_extent_map(em); |
| 10462 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10463 | spin_lock(&block_group->lock); |
| 10464 | block_group->removed = 1; |
| 10465 | /* |
| 10466 | * At this point trimming can't start on this block group, because we |
| 10467 | * removed the block group from the tree fs_info->block_group_cache_tree |
| 10468 | * so no one can't find it anymore and even if someone already got this |
| 10469 | * block group before we removed it from the rbtree, they have already |
| 10470 | * incremented block_group->trimming - if they didn't, they won't find |
| 10471 | * any free space entries because we already removed them all when we |
| 10472 | * called btrfs_remove_free_space_cache(). |
| 10473 | * |
| 10474 | * And we must not remove the extent map from the fs_info->mapping_tree |
| 10475 | * to prevent the same logical address range and physical device space |
| 10476 | * ranges from being reused for a new block group. This is because our |
| 10477 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is |
| 10478 | * completely transactionless, so while it is trimming a range the |
| 10479 | * currently running transaction might finish and a new one start, |
| 10480 | * allowing for new block groups to be created that can reuse the same |
| 10481 | * physical device locations unless we take this special care. |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10482 | * |
| 10483 | * There may also be an implicit trim operation if the file system |
| 10484 | * is mounted with -odiscard. The same protections must remain |
| 10485 | * in place until the extents have been discarded completely when |
| 10486 | * the transaction commit has completed. |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10487 | */ |
| 10488 | remove_em = (atomic_read(&block_group->trimming) == 0); |
| 10489 | /* |
| 10490 | * Make sure a trimmer task always sees the em in the pinned_chunks list |
| 10491 | * if it sees block_group->removed == 1 (needs to lock block_group->lock |
| 10492 | * before checking block_group->removed). |
| 10493 | */ |
| 10494 | if (!remove_em) { |
| 10495 | /* |
| 10496 | * Our em might be in trans->transaction->pending_chunks which |
| 10497 | * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks), |
| 10498 | * and so is the fs_info->pinned_chunks list. |
| 10499 | * |
| 10500 | * So at this point we must be holding the chunk_mutex to avoid |
| 10501 | * any races with chunk allocation (more specifically at |
| 10502 | * volumes.c:contains_pending_extent()), to ensure it always |
| 10503 | * sees the em, either in the pending_chunks list or in the |
| 10504 | * pinned_chunks list. |
| 10505 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10506 | list_move_tail(&em->list, &fs_info->pinned_chunks); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10507 | } |
| 10508 | spin_unlock(&block_group->lock); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10509 | |
| 10510 | if (remove_em) { |
| 10511 | struct extent_map_tree *em_tree; |
| 10512 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10513 | em_tree = &fs_info->mapping_tree.map_tree; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10514 | write_lock(&em_tree->lock); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10515 | /* |
| 10516 | * The em might be in the pending_chunks list, so make sure the |
| 10517 | * chunk mutex is locked, since remove_extent_mapping() will |
| 10518 | * delete us from that list. |
| 10519 | */ |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10520 | remove_extent_mapping(em_tree, em); |
| 10521 | write_unlock(&em_tree->lock); |
| 10522 | /* once for the tree */ |
| 10523 | free_extent_map(em); |
| 10524 | } |
| 10525 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 10526 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10527 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10528 | ret = remove_block_group_free_space(trans, fs_info, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10529 | if (ret) |
| 10530 | goto out; |
| 10531 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 10532 | btrfs_put_block_group(block_group); |
| 10533 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10534 | |
| 10535 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 10536 | if (ret > 0) |
| 10537 | ret = -EIO; |
| 10538 | if (ret < 0) |
| 10539 | goto out; |
| 10540 | |
| 10541 | ret = btrfs_del_item(trans, root, path); |
| 10542 | out: |
| 10543 | btrfs_free_path(path); |
| 10544 | return ret; |
| 10545 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10546 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10547 | struct btrfs_trans_handle * |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10548 | btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info, |
| 10549 | const u64 chunk_offset) |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10550 | { |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10551 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 10552 | struct extent_map *em; |
| 10553 | struct map_lookup *map; |
| 10554 | unsigned int num_items; |
| 10555 | |
| 10556 | read_lock(&em_tree->lock); |
| 10557 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 10558 | read_unlock(&em_tree->lock); |
| 10559 | ASSERT(em && em->start == chunk_offset); |
| 10560 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10561 | /* |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10562 | * We need to reserve 3 + N units from the metadata space info in order |
| 10563 | * to remove a block group (done at btrfs_remove_chunk() and at |
| 10564 | * btrfs_remove_block_group()), which are used for: |
| 10565 | * |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10566 | * 1 unit for adding the free space inode's orphan (located in the tree |
| 10567 | * of tree roots). |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10568 | * 1 unit for deleting the block group item (located in the extent |
| 10569 | * tree). |
| 10570 | * 1 unit for deleting the free space item (located in tree of tree |
| 10571 | * roots). |
| 10572 | * N units for deleting N device extent items corresponding to each |
| 10573 | * stripe (located in the device tree). |
| 10574 | * |
| 10575 | * In order to remove a block group we also need to reserve units in the |
| 10576 | * system space info in order to update the chunk tree (update one or |
| 10577 | * more device items and remove one chunk item), but this is done at |
| 10578 | * btrfs_remove_chunk() through a call to check_system_chunk(). |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10579 | */ |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 10580 | map = em->map_lookup; |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10581 | num_items = 3 + map->num_stripes; |
| 10582 | free_extent_map(em); |
| 10583 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10584 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10585 | num_items, 1); |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10586 | } |
| 10587 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10588 | /* |
| 10589 | * Process the unused_bgs list and remove any that don't have any allocated |
| 10590 | * space inside of them. |
| 10591 | */ |
| 10592 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) |
| 10593 | { |
| 10594 | struct btrfs_block_group_cache *block_group; |
| 10595 | struct btrfs_space_info *space_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10596 | struct btrfs_trans_handle *trans; |
| 10597 | int ret = 0; |
| 10598 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 10599 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10600 | return; |
| 10601 | |
| 10602 | spin_lock(&fs_info->unused_bgs_lock); |
| 10603 | while (!list_empty(&fs_info->unused_bgs)) { |
| 10604 | u64 start, end; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10605 | int trimming; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10606 | |
| 10607 | block_group = list_first_entry(&fs_info->unused_bgs, |
| 10608 | struct btrfs_block_group_cache, |
| 10609 | bg_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10610 | list_del_init(&block_group->bg_list); |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10611 | |
| 10612 | space_info = block_group->space_info; |
| 10613 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10614 | if (ret || btrfs_mixed_space_info(space_info)) { |
| 10615 | btrfs_put_block_group(block_group); |
| 10616 | continue; |
| 10617 | } |
| 10618 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10619 | |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10620 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 10621 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10622 | /* Don't want to race with allocators so take the groups_sem */ |
| 10623 | down_write(&space_info->groups_sem); |
| 10624 | spin_lock(&block_group->lock); |
| 10625 | if (block_group->reserved || |
| 10626 | btrfs_block_group_used(&block_group->item) || |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 10627 | block_group->ro || |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10628 | list_is_singular(&block_group->list)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10629 | /* |
| 10630 | * We want to bail if we made new allocations or have |
| 10631 | * outstanding allocations in this block group. We do |
| 10632 | * the ro check in case balance is currently acting on |
| 10633 | * this block group. |
| 10634 | */ |
| 10635 | spin_unlock(&block_group->lock); |
| 10636 | up_write(&space_info->groups_sem); |
| 10637 | goto next; |
| 10638 | } |
| 10639 | spin_unlock(&block_group->lock); |
| 10640 | |
| 10641 | /* 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] | 10642 | ret = inc_block_group_ro(block_group, 0); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10643 | up_write(&space_info->groups_sem); |
| 10644 | if (ret < 0) { |
| 10645 | ret = 0; |
| 10646 | goto next; |
| 10647 | } |
| 10648 | |
| 10649 | /* |
| 10650 | * Want to do this before we do anything else so we can recover |
| 10651 | * properly if we fail to join the transaction. |
| 10652 | */ |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10653 | trans = btrfs_start_trans_remove_block_group(fs_info, |
| 10654 | block_group->key.objectid); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10655 | if (IS_ERR(trans)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10656 | btrfs_dec_block_group_ro(block_group); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10657 | ret = PTR_ERR(trans); |
| 10658 | goto next; |
| 10659 | } |
| 10660 | |
| 10661 | /* |
| 10662 | * We could have pending pinned extents for this block group, |
| 10663 | * just delete them, we don't care about them anymore. |
| 10664 | */ |
| 10665 | start = block_group->key.objectid; |
| 10666 | end = start + block_group->key.offset - 1; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10667 | /* |
| 10668 | * Hold the unused_bg_unpin_mutex lock to avoid racing with |
| 10669 | * btrfs_finish_extent_commit(). If we are at transaction N, |
| 10670 | * another task might be running finish_extent_commit() for the |
| 10671 | * previous transaction N - 1, and have seen a range belonging |
| 10672 | * to the block group in freed_extents[] before we were able to |
| 10673 | * clear the whole block group range from freed_extents[]. This |
| 10674 | * means that task can lookup for the block group after we |
| 10675 | * unpinned it from freed_extents[] and removed it, leading to |
| 10676 | * a BUG_ON() at btrfs_unpin_extent_range(). |
| 10677 | */ |
| 10678 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10679 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 10680 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10681 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10682 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10683 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10684 | goto end_trans; |
| 10685 | } |
| 10686 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 10687 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10688 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10689 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10690 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10691 | goto end_trans; |
| 10692 | } |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10693 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10694 | |
| 10695 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10696 | spin_lock(&space_info->lock); |
| 10697 | spin_lock(&block_group->lock); |
| 10698 | |
| 10699 | space_info->bytes_pinned -= block_group->pinned; |
| 10700 | space_info->bytes_readonly += block_group->pinned; |
| 10701 | percpu_counter_add(&space_info->total_bytes_pinned, |
| 10702 | -block_group->pinned); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10703 | block_group->pinned = 0; |
| 10704 | |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10705 | spin_unlock(&block_group->lock); |
| 10706 | spin_unlock(&space_info->lock); |
| 10707 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10708 | /* DISCARD can flip during remount */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10709 | trimming = btrfs_test_opt(fs_info, DISCARD); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10710 | |
| 10711 | /* Implicit trim during transaction commit. */ |
| 10712 | if (trimming) |
| 10713 | btrfs_get_block_group_trimming(block_group); |
| 10714 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10715 | /* |
| 10716 | * Btrfs_remove_chunk will abort the transaction if things go |
| 10717 | * horribly wrong. |
| 10718 | */ |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 10719 | ret = btrfs_remove_chunk(trans, fs_info, |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10720 | block_group->key.objectid); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10721 | |
| 10722 | if (ret) { |
| 10723 | if (trimming) |
| 10724 | btrfs_put_block_group_trimming(block_group); |
| 10725 | goto end_trans; |
| 10726 | } |
| 10727 | |
| 10728 | /* |
| 10729 | * If we're not mounted with -odiscard, we can just forget |
| 10730 | * about this block group. Otherwise we'll need to wait |
| 10731 | * until transaction commit to do the actual discard. |
| 10732 | */ |
| 10733 | if (trimming) { |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10734 | spin_lock(&fs_info->unused_bgs_lock); |
| 10735 | /* |
| 10736 | * A concurrent scrub might have added us to the list |
| 10737 | * fs_info->unused_bgs, so use a list_move operation |
| 10738 | * to add the block group to the deleted_bgs list. |
| 10739 | */ |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10740 | list_move(&block_group->bg_list, |
| 10741 | &trans->transaction->deleted_bgs); |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10742 | spin_unlock(&fs_info->unused_bgs_lock); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10743 | btrfs_get_block_group(block_group); |
| 10744 | } |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10745 | end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10746 | btrfs_end_transaction(trans); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10747 | next: |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10748 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10749 | btrfs_put_block_group(block_group); |
| 10750 | spin_lock(&fs_info->unused_bgs_lock); |
| 10751 | } |
| 10752 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10753 | } |
| 10754 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10755 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 10756 | { |
| 10757 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10758 | struct btrfs_super_block *disk_super; |
| 10759 | u64 features; |
| 10760 | u64 flags; |
| 10761 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10762 | int ret; |
| 10763 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 10764 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10765 | if (!btrfs_super_root(disk_super)) |
Dan Carpenter | 0dc924c5 | 2016-01-13 15:21:17 +0300 | [diff] [blame] | 10766 | return -EINVAL; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10767 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10768 | features = btrfs_super_incompat_flags(disk_super); |
| 10769 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 10770 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10771 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10772 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10773 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10774 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10775 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10776 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10777 | if (mixed) { |
| 10778 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10779 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10780 | } else { |
| 10781 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10782 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10783 | if (ret) |
| 10784 | goto out; |
| 10785 | |
| 10786 | flags = BTRFS_BLOCK_GROUP_DATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10787 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10788 | } |
| 10789 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10790 | return ret; |
| 10791 | } |
| 10792 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10793 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 10794 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10795 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10796 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10797 | } |
| 10798 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10799 | /* |
| 10800 | * It used to be that old block groups would be left around forever. |
| 10801 | * Iterating over them would be enough to trim unused space. Since we |
| 10802 | * now automatically remove them, we also need to iterate over unallocated |
| 10803 | * space. |
| 10804 | * |
| 10805 | * We don't want a transaction for this since the discard may take a |
| 10806 | * substantial amount of time. We don't require that a transaction be |
| 10807 | * running, but we do need to take a running transaction into account |
| 10808 | * to ensure that we're not discarding chunks that were released in |
| 10809 | * the current transaction. |
| 10810 | * |
| 10811 | * Holding the chunks lock will prevent other threads from allocating |
| 10812 | * or releasing chunks, but it won't prevent a running transaction |
| 10813 | * from committing and releasing the memory that the pending chunks |
| 10814 | * list head uses. For that, we need to take a reference to the |
| 10815 | * transaction. |
| 10816 | */ |
| 10817 | static int btrfs_trim_free_extents(struct btrfs_device *device, |
| 10818 | u64 minlen, u64 *trimmed) |
| 10819 | { |
| 10820 | u64 start = 0, len = 0; |
| 10821 | int ret; |
| 10822 | |
| 10823 | *trimmed = 0; |
| 10824 | |
| 10825 | /* Not writeable = nothing to do. */ |
| 10826 | if (!device->writeable) |
| 10827 | return 0; |
| 10828 | |
| 10829 | /* No free space = nothing to do. */ |
| 10830 | if (device->total_bytes <= device->bytes_used) |
| 10831 | return 0; |
| 10832 | |
| 10833 | ret = 0; |
| 10834 | |
| 10835 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 10836 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10837 | struct btrfs_transaction *trans; |
| 10838 | u64 bytes; |
| 10839 | |
| 10840 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 10841 | if (ret) |
| 10842 | return ret; |
| 10843 | |
| 10844 | down_read(&fs_info->commit_root_sem); |
| 10845 | |
| 10846 | spin_lock(&fs_info->trans_lock); |
| 10847 | trans = fs_info->running_transaction; |
| 10848 | if (trans) |
| 10849 | atomic_inc(&trans->use_count); |
| 10850 | spin_unlock(&fs_info->trans_lock); |
| 10851 | |
| 10852 | ret = find_free_dev_extent_start(trans, device, minlen, start, |
| 10853 | &start, &len); |
| 10854 | if (trans) |
| 10855 | btrfs_put_transaction(trans); |
| 10856 | |
| 10857 | if (ret) { |
| 10858 | up_read(&fs_info->commit_root_sem); |
| 10859 | mutex_unlock(&fs_info->chunk_mutex); |
| 10860 | if (ret == -ENOSPC) |
| 10861 | ret = 0; |
| 10862 | break; |
| 10863 | } |
| 10864 | |
| 10865 | ret = btrfs_issue_discard(device->bdev, start, len, &bytes); |
| 10866 | up_read(&fs_info->commit_root_sem); |
| 10867 | mutex_unlock(&fs_info->chunk_mutex); |
| 10868 | |
| 10869 | if (ret) |
| 10870 | break; |
| 10871 | |
| 10872 | start += len; |
| 10873 | *trimmed += bytes; |
| 10874 | |
| 10875 | if (fatal_signal_pending(current)) { |
| 10876 | ret = -ERESTARTSYS; |
| 10877 | break; |
| 10878 | } |
| 10879 | |
| 10880 | cond_resched(); |
| 10881 | } |
| 10882 | |
| 10883 | return ret; |
| 10884 | } |
| 10885 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10886 | 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] | 10887 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10888 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10889 | struct btrfs_device *device; |
| 10890 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10891 | u64 group_trimmed; |
| 10892 | u64 start; |
| 10893 | u64 end; |
| 10894 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10895 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10896 | int ret = 0; |
| 10897 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10898 | /* |
| 10899 | * try to trim all FS space, our block group may start from non-zero. |
| 10900 | */ |
| 10901 | if (range->len == total_bytes) |
| 10902 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 10903 | else |
| 10904 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10905 | |
| 10906 | while (cache) { |
| 10907 | if (cache->key.objectid >= (range->start + range->len)) { |
| 10908 | btrfs_put_block_group(cache); |
| 10909 | break; |
| 10910 | } |
| 10911 | |
| 10912 | start = max(range->start, cache->key.objectid); |
| 10913 | end = min(range->start + range->len, |
| 10914 | cache->key.objectid + cache->key.offset); |
| 10915 | |
| 10916 | if (end - start >= range->minlen) { |
| 10917 | if (!block_group_cache_done(cache)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 10918 | ret = cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 10919 | if (ret) { |
| 10920 | btrfs_put_block_group(cache); |
| 10921 | break; |
| 10922 | } |
| 10923 | ret = wait_block_group_cache_done(cache); |
| 10924 | if (ret) { |
| 10925 | btrfs_put_block_group(cache); |
| 10926 | break; |
| 10927 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10928 | } |
| 10929 | ret = btrfs_trim_block_group(cache, |
| 10930 | &group_trimmed, |
| 10931 | start, |
| 10932 | end, |
| 10933 | range->minlen); |
| 10934 | |
| 10935 | trimmed += group_trimmed; |
| 10936 | if (ret) { |
| 10937 | btrfs_put_block_group(cache); |
| 10938 | break; |
| 10939 | } |
| 10940 | } |
| 10941 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10942 | cache = next_block_group(fs_info, cache); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10943 | } |
| 10944 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10945 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 10946 | devices = &fs_info->fs_devices->alloc_list; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10947 | list_for_each_entry(device, devices, dev_alloc_list) { |
| 10948 | ret = btrfs_trim_free_extents(device, range->minlen, |
| 10949 | &group_trimmed); |
| 10950 | if (ret) |
| 10951 | break; |
| 10952 | |
| 10953 | trimmed += group_trimmed; |
| 10954 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10955 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10956 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10957 | range->len = trimmed; |
| 10958 | return ret; |
| 10959 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10960 | |
| 10961 | /* |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10962 | * btrfs_{start,end}_write_no_snapshoting() are similar to |
| 10963 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 10964 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 10965 | * but flush the data into disk after the snapshot creation, or to prevent |
| 10966 | * operations while snapshoting is ongoing and that cause the snapshot to be |
| 10967 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10968 | */ |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10969 | void btrfs_end_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10970 | { |
| 10971 | percpu_counter_dec(&root->subv_writers->counter); |
| 10972 | /* |
David Sterba | a83342a | 2015-02-16 19:36:47 +0100 | [diff] [blame] | 10973 | * Make sure counter is updated before we wake up waiters. |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10974 | */ |
| 10975 | smp_mb(); |
| 10976 | if (waitqueue_active(&root->subv_writers->wait)) |
| 10977 | wake_up(&root->subv_writers->wait); |
| 10978 | } |
| 10979 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10980 | int btrfs_start_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10981 | { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10982 | if (atomic_read(&root->will_be_snapshoted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10983 | return 0; |
| 10984 | |
| 10985 | percpu_counter_inc(&root->subv_writers->counter); |
| 10986 | /* |
| 10987 | * Make sure counter is updated before we check for snapshot creation. |
| 10988 | */ |
| 10989 | smp_mb(); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10990 | if (atomic_read(&root->will_be_snapshoted)) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10991 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10992 | return 0; |
| 10993 | } |
| 10994 | return 1; |
| 10995 | } |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 10996 | |
| 10997 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 10998 | { |
| 10999 | schedule(); |
| 11000 | return 0; |
| 11001 | } |
| 11002 | |
| 11003 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) |
| 11004 | { |
| 11005 | while (true) { |
| 11006 | int ret; |
| 11007 | |
| 11008 | ret = btrfs_start_write_no_snapshoting(root); |
| 11009 | if (ret) |
| 11010 | break; |
| 11011 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 11012 | wait_snapshoting_atomic_t, |
| 11013 | TASK_UNINTERRUPTIBLE); |
| 11014 | } |
| 11015 | } |