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 | b742bb82 | 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); |
| 891 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 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, |
| 1038 | struct btrfs_root *root, |
| 1039 | struct btrfs_path *path, |
| 1040 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1041 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1042 | struct btrfs_extent_item *item; |
| 1043 | struct btrfs_extent_item_v0 *ei0; |
| 1044 | struct btrfs_extent_ref_v0 *ref0; |
| 1045 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1046 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1047 | struct btrfs_key key; |
| 1048 | struct btrfs_key found_key; |
| 1049 | u32 new_size = sizeof(*item); |
| 1050 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1051 | int ret; |
| 1052 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1053 | leaf = path->nodes[0]; |
| 1054 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1055 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1056 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1057 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1058 | struct btrfs_extent_item_v0); |
| 1059 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 1060 | |
| 1061 | if (owner == (u64)-1) { |
| 1062 | while (1) { |
| 1063 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1064 | ret = btrfs_next_leaf(root, path); |
| 1065 | if (ret < 0) |
| 1066 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1067 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1068 | leaf = path->nodes[0]; |
| 1069 | } |
| 1070 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1071 | path->slots[0]); |
| 1072 | BUG_ON(key.objectid != found_key.objectid); |
| 1073 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 1074 | path->slots[0]++; |
| 1075 | continue; |
| 1076 | } |
| 1077 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1078 | struct btrfs_extent_ref_v0); |
| 1079 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1083 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1084 | |
| 1085 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 1086 | new_size += sizeof(*bi); |
| 1087 | |
| 1088 | new_size -= sizeof(*ei0); |
| 1089 | ret = btrfs_search_slot(trans, root, &key, path, |
| 1090 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1091 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1092 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1093 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1094 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1095 | btrfs_extend_item(root->fs_info, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1096 | |
| 1097 | leaf = path->nodes[0]; |
| 1098 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1099 | btrfs_set_extent_refs(leaf, item, refs); |
| 1100 | /* FIXME: get real generation */ |
| 1101 | btrfs_set_extent_generation(leaf, item, 0); |
| 1102 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1103 | btrfs_set_extent_flags(leaf, item, |
| 1104 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1105 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1106 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1107 | /* FIXME: get first key of the block */ |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 1108 | memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1109 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1110 | } else { |
| 1111 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1112 | } |
| 1113 | btrfs_mark_buffer_dirty(leaf); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
| 1118 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1119 | { |
| 1120 | u32 high_crc = ~(u32)0; |
| 1121 | u32 low_crc = ~(u32)0; |
| 1122 | __le64 lenum; |
| 1123 | |
| 1124 | lenum = cpu_to_le64(root_objectid); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1125 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1126 | lenum = cpu_to_le64(owner); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1127 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1128 | lenum = cpu_to_le64(offset); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1129 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | |
| 1131 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1132 | } |
| 1133 | |
| 1134 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1135 | struct btrfs_extent_data_ref *ref) |
| 1136 | { |
| 1137 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1138 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1139 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1140 | } |
| 1141 | |
| 1142 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1143 | struct btrfs_extent_data_ref *ref, |
| 1144 | u64 root_objectid, u64 owner, u64 offset) |
| 1145 | { |
| 1146 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1147 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1148 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1149 | return 0; |
| 1150 | return 1; |
| 1151 | } |
| 1152 | |
| 1153 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1154 | struct btrfs_root *root, |
| 1155 | struct btrfs_path *path, |
| 1156 | u64 bytenr, u64 parent, |
| 1157 | u64 root_objectid, |
| 1158 | u64 owner, u64 offset) |
| 1159 | { |
| 1160 | struct btrfs_key key; |
| 1161 | struct btrfs_extent_data_ref *ref; |
| 1162 | struct extent_buffer *leaf; |
| 1163 | u32 nritems; |
| 1164 | int ret; |
| 1165 | int recow; |
| 1166 | int err = -ENOENT; |
| 1167 | |
| 1168 | key.objectid = bytenr; |
| 1169 | if (parent) { |
| 1170 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1171 | key.offset = parent; |
| 1172 | } else { |
| 1173 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1174 | key.offset = hash_extent_data_ref(root_objectid, |
| 1175 | owner, offset); |
| 1176 | } |
| 1177 | again: |
| 1178 | recow = 0; |
| 1179 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1180 | if (ret < 0) { |
| 1181 | err = ret; |
| 1182 | goto fail; |
| 1183 | } |
| 1184 | |
| 1185 | if (parent) { |
| 1186 | if (!ret) |
| 1187 | return 0; |
| 1188 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1189 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1190 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1191 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1192 | if (ret < 0) { |
| 1193 | err = ret; |
| 1194 | goto fail; |
| 1195 | } |
| 1196 | if (!ret) |
| 1197 | return 0; |
| 1198 | #endif |
| 1199 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1203 | nritems = btrfs_header_nritems(leaf); |
| 1204 | while (1) { |
| 1205 | if (path->slots[0] >= nritems) { |
| 1206 | ret = btrfs_next_leaf(root, path); |
| 1207 | if (ret < 0) |
| 1208 | err = ret; |
| 1209 | if (ret) |
| 1210 | goto fail; |
| 1211 | |
| 1212 | leaf = path->nodes[0]; |
| 1213 | nritems = btrfs_header_nritems(leaf); |
| 1214 | recow = 1; |
| 1215 | } |
| 1216 | |
| 1217 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1218 | if (key.objectid != bytenr || |
| 1219 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1220 | goto fail; |
| 1221 | |
| 1222 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1223 | struct btrfs_extent_data_ref); |
| 1224 | |
| 1225 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1226 | owner, offset)) { |
| 1227 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1228 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1229 | goto again; |
| 1230 | } |
| 1231 | err = 0; |
| 1232 | break; |
| 1233 | } |
| 1234 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1235 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1236 | fail: |
| 1237 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1238 | } |
| 1239 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1240 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1241 | struct btrfs_root *root, |
| 1242 | struct btrfs_path *path, |
| 1243 | u64 bytenr, u64 parent, |
| 1244 | u64 root_objectid, u64 owner, |
| 1245 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1246 | { |
| 1247 | struct btrfs_key key; |
| 1248 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1249 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1250 | u32 num_refs; |
| 1251 | int ret; |
| 1252 | |
| 1253 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1254 | if (parent) { |
| 1255 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1256 | key.offset = parent; |
| 1257 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1258 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1259 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1260 | key.offset = hash_extent_data_ref(root_objectid, |
| 1261 | owner, offset); |
| 1262 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1263 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1264 | |
| 1265 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1266 | if (ret && ret != -EEXIST) |
| 1267 | goto fail; |
| 1268 | |
| 1269 | leaf = path->nodes[0]; |
| 1270 | if (parent) { |
| 1271 | struct btrfs_shared_data_ref *ref; |
| 1272 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1273 | struct btrfs_shared_data_ref); |
| 1274 | if (ret == 0) { |
| 1275 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1276 | } else { |
| 1277 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1278 | num_refs += refs_to_add; |
| 1279 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1280 | } |
| 1281 | } else { |
| 1282 | struct btrfs_extent_data_ref *ref; |
| 1283 | while (ret == -EEXIST) { |
| 1284 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1285 | struct btrfs_extent_data_ref); |
| 1286 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1287 | owner, offset)) |
| 1288 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1289 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1290 | key.offset++; |
| 1291 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1292 | size); |
| 1293 | if (ret && ret != -EEXIST) |
| 1294 | goto fail; |
| 1295 | |
| 1296 | leaf = path->nodes[0]; |
| 1297 | } |
| 1298 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1299 | struct btrfs_extent_data_ref); |
| 1300 | if (ret == 0) { |
| 1301 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1302 | root_objectid); |
| 1303 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1304 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1305 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1306 | } else { |
| 1307 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1308 | num_refs += refs_to_add; |
| 1309 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1310 | } |
| 1311 | } |
| 1312 | btrfs_mark_buffer_dirty(leaf); |
| 1313 | ret = 0; |
| 1314 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1315 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1316 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1317 | } |
| 1318 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1319 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1320 | struct btrfs_root *root, |
| 1321 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1322 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1323 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1324 | struct btrfs_key key; |
| 1325 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1326 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1327 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1328 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1329 | int ret = 0; |
| 1330 | |
| 1331 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1332 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1333 | |
| 1334 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1335 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1336 | struct btrfs_extent_data_ref); |
| 1337 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1338 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1339 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1340 | struct btrfs_shared_data_ref); |
| 1341 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1342 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1343 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1344 | struct btrfs_extent_ref_v0 *ref0; |
| 1345 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1346 | struct btrfs_extent_ref_v0); |
| 1347 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1348 | #endif |
| 1349 | } else { |
| 1350 | BUG(); |
| 1351 | } |
| 1352 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1353 | BUG_ON(num_refs < refs_to_drop); |
| 1354 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1355 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1356 | if (num_refs == 0) { |
| 1357 | ret = btrfs_del_item(trans, root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1358 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1359 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1360 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1361 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1362 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1363 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1364 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1365 | else { |
| 1366 | struct btrfs_extent_ref_v0 *ref0; |
| 1367 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1368 | struct btrfs_extent_ref_v0); |
| 1369 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1370 | } |
| 1371 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1372 | btrfs_mark_buffer_dirty(leaf); |
| 1373 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1374 | return ret; |
| 1375 | } |
| 1376 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 1377 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1378 | struct btrfs_extent_inline_ref *iref) |
| 1379 | { |
| 1380 | struct btrfs_key key; |
| 1381 | struct extent_buffer *leaf; |
| 1382 | struct btrfs_extent_data_ref *ref1; |
| 1383 | struct btrfs_shared_data_ref *ref2; |
| 1384 | u32 num_refs = 0; |
| 1385 | |
| 1386 | leaf = path->nodes[0]; |
| 1387 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1388 | if (iref) { |
| 1389 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1390 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1391 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1392 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1393 | } else { |
| 1394 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1395 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1396 | } |
| 1397 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1398 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1399 | struct btrfs_extent_data_ref); |
| 1400 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1401 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1402 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1403 | struct btrfs_shared_data_ref); |
| 1404 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1405 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1406 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1407 | struct btrfs_extent_ref_v0 *ref0; |
| 1408 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1409 | struct btrfs_extent_ref_v0); |
| 1410 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1411 | #endif |
| 1412 | } else { |
| 1413 | WARN_ON(1); |
| 1414 | } |
| 1415 | return num_refs; |
| 1416 | } |
| 1417 | |
| 1418 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1419 | struct btrfs_root *root, |
| 1420 | struct btrfs_path *path, |
| 1421 | u64 bytenr, u64 parent, |
| 1422 | u64 root_objectid) |
| 1423 | { |
| 1424 | struct btrfs_key key; |
| 1425 | int ret; |
| 1426 | |
| 1427 | key.objectid = bytenr; |
| 1428 | if (parent) { |
| 1429 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1430 | key.offset = parent; |
| 1431 | } else { |
| 1432 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1433 | key.offset = root_objectid; |
| 1434 | } |
| 1435 | |
| 1436 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1437 | if (ret > 0) |
| 1438 | ret = -ENOENT; |
| 1439 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1440 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1441 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1442 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1443 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1444 | if (ret > 0) |
| 1445 | ret = -ENOENT; |
| 1446 | } |
| 1447 | #endif |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1452 | struct btrfs_root *root, |
| 1453 | struct btrfs_path *path, |
| 1454 | u64 bytenr, u64 parent, |
| 1455 | u64 root_objectid) |
| 1456 | { |
| 1457 | struct btrfs_key key; |
| 1458 | int ret; |
| 1459 | |
| 1460 | key.objectid = bytenr; |
| 1461 | if (parent) { |
| 1462 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1463 | key.offset = parent; |
| 1464 | } else { |
| 1465 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1466 | key.offset = root_objectid; |
| 1467 | } |
| 1468 | |
| 1469 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1470 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1471 | return ret; |
| 1472 | } |
| 1473 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1474 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1475 | { |
| 1476 | int type; |
| 1477 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1478 | if (parent > 0) |
| 1479 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1480 | else |
| 1481 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1482 | } else { |
| 1483 | if (parent > 0) |
| 1484 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1485 | else |
| 1486 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1487 | } |
| 1488 | return type; |
| 1489 | } |
| 1490 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1491 | static int find_next_key(struct btrfs_path *path, int level, |
| 1492 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1493 | |
| 1494 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1495 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1496 | if (!path->nodes[level]) |
| 1497 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1498 | if (path->slots[level] + 1 >= |
| 1499 | btrfs_header_nritems(path->nodes[level])) |
| 1500 | continue; |
| 1501 | if (level == 0) |
| 1502 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1503 | path->slots[level] + 1); |
| 1504 | else |
| 1505 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1506 | path->slots[level] + 1); |
| 1507 | return 0; |
| 1508 | } |
| 1509 | return 1; |
| 1510 | } |
| 1511 | |
| 1512 | /* |
| 1513 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1514 | * to the address of inline back ref, and 0 is returned. |
| 1515 | * |
| 1516 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1517 | * should be inserted, and -ENOENT is returned. |
| 1518 | * |
| 1519 | * if insert is true and there are too many inline back refs, the path |
| 1520 | * points to the extent item, and -EAGAIN is returned. |
| 1521 | * |
| 1522 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1523 | * items in the tree are ordered. |
| 1524 | */ |
| 1525 | static noinline_for_stack |
| 1526 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1527 | struct btrfs_root *root, |
| 1528 | struct btrfs_path *path, |
| 1529 | struct btrfs_extent_inline_ref **ref_ret, |
| 1530 | u64 bytenr, u64 num_bytes, |
| 1531 | u64 parent, u64 root_objectid, |
| 1532 | u64 owner, u64 offset, int insert) |
| 1533 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1534 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1535 | struct btrfs_key key; |
| 1536 | struct extent_buffer *leaf; |
| 1537 | struct btrfs_extent_item *ei; |
| 1538 | struct btrfs_extent_inline_ref *iref; |
| 1539 | u64 flags; |
| 1540 | u64 item_size; |
| 1541 | unsigned long ptr; |
| 1542 | unsigned long end; |
| 1543 | int extra_size; |
| 1544 | int type; |
| 1545 | int want; |
| 1546 | int ret; |
| 1547 | int err = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1548 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1549 | |
| 1550 | key.objectid = bytenr; |
| 1551 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1552 | key.offset = num_bytes; |
| 1553 | |
| 1554 | want = extent_ref_type(parent, owner); |
| 1555 | if (insert) { |
| 1556 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1557 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1558 | } else |
| 1559 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1560 | |
| 1561 | /* |
| 1562 | * Owner is our parent level, so we can just add one to get the level |
| 1563 | * for the block we are interested in. |
| 1564 | */ |
| 1565 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1566 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1567 | key.offset = owner; |
| 1568 | } |
| 1569 | |
| 1570 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1571 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1572 | if (ret < 0) { |
| 1573 | err = ret; |
| 1574 | goto out; |
| 1575 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1576 | |
| 1577 | /* |
| 1578 | * We may be a newly converted file system which still has the old fat |
| 1579 | * extent entries for metadata, so try and see if we have one of those. |
| 1580 | */ |
| 1581 | if (ret > 0 && skinny_metadata) { |
| 1582 | skinny_metadata = false; |
| 1583 | if (path->slots[0]) { |
| 1584 | path->slots[0]--; |
| 1585 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1586 | path->slots[0]); |
| 1587 | if (key.objectid == bytenr && |
| 1588 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1589 | key.offset == num_bytes) |
| 1590 | ret = 0; |
| 1591 | } |
| 1592 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 1593 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1594 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1595 | key.offset = num_bytes; |
| 1596 | btrfs_release_path(path); |
| 1597 | goto again; |
| 1598 | } |
| 1599 | } |
| 1600 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1601 | if (ret && !insert) { |
| 1602 | err = -ENOENT; |
| 1603 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1604 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1605 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1606 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1607 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1608 | |
| 1609 | leaf = path->nodes[0]; |
| 1610 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1611 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1612 | if (item_size < sizeof(*ei)) { |
| 1613 | if (!insert) { |
| 1614 | err = -ENOENT; |
| 1615 | goto out; |
| 1616 | } |
| 1617 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1618 | extra_size); |
| 1619 | if (ret < 0) { |
| 1620 | err = ret; |
| 1621 | goto out; |
| 1622 | } |
| 1623 | leaf = path->nodes[0]; |
| 1624 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1625 | } |
| 1626 | #endif |
| 1627 | BUG_ON(item_size < sizeof(*ei)); |
| 1628 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1629 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1630 | flags = btrfs_extent_flags(leaf, ei); |
| 1631 | |
| 1632 | ptr = (unsigned long)(ei + 1); |
| 1633 | end = (unsigned long)ei + item_size; |
| 1634 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1635 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1636 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1637 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | err = -ENOENT; |
| 1641 | while (1) { |
| 1642 | if (ptr >= end) { |
| 1643 | WARN_ON(ptr > end); |
| 1644 | break; |
| 1645 | } |
| 1646 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1647 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1648 | if (want < type) |
| 1649 | break; |
| 1650 | if (want > type) { |
| 1651 | ptr += btrfs_extent_inline_ref_size(type); |
| 1652 | continue; |
| 1653 | } |
| 1654 | |
| 1655 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1656 | struct btrfs_extent_data_ref *dref; |
| 1657 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1658 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1659 | owner, offset)) { |
| 1660 | err = 0; |
| 1661 | break; |
| 1662 | } |
| 1663 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1664 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1665 | break; |
| 1666 | } else { |
| 1667 | u64 ref_offset; |
| 1668 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1669 | if (parent > 0) { |
| 1670 | if (parent == ref_offset) { |
| 1671 | err = 0; |
| 1672 | break; |
| 1673 | } |
| 1674 | if (ref_offset < parent) |
| 1675 | break; |
| 1676 | } else { |
| 1677 | if (root_objectid == ref_offset) { |
| 1678 | err = 0; |
| 1679 | break; |
| 1680 | } |
| 1681 | if (ref_offset < root_objectid) |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | ptr += btrfs_extent_inline_ref_size(type); |
| 1686 | } |
| 1687 | if (err == -ENOENT && insert) { |
| 1688 | if (item_size + extra_size >= |
| 1689 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1690 | err = -EAGAIN; |
| 1691 | goto out; |
| 1692 | } |
| 1693 | /* |
| 1694 | * To add new inline back ref, we have to make sure |
| 1695 | * there is no corresponding back ref item. |
| 1696 | * For simplicity, we just do not add new inline back |
| 1697 | * ref if there is any kind of item for this block |
| 1698 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1699 | if (find_next_key(path, 0, &key) == 0 && |
| 1700 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1701 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1702 | err = -EAGAIN; |
| 1703 | goto out; |
| 1704 | } |
| 1705 | } |
| 1706 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1707 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1708 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1709 | path->keep_locks = 0; |
| 1710 | btrfs_unlock_up_safe(path, 1); |
| 1711 | } |
| 1712 | return err; |
| 1713 | } |
| 1714 | |
| 1715 | /* |
| 1716 | * helper to add new inline back ref |
| 1717 | */ |
| 1718 | static noinline_for_stack |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1719 | void setup_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1720 | struct btrfs_path *path, |
| 1721 | struct btrfs_extent_inline_ref *iref, |
| 1722 | u64 parent, u64 root_objectid, |
| 1723 | u64 owner, u64 offset, int refs_to_add, |
| 1724 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1725 | { |
| 1726 | struct extent_buffer *leaf; |
| 1727 | struct btrfs_extent_item *ei; |
| 1728 | unsigned long ptr; |
| 1729 | unsigned long end; |
| 1730 | unsigned long item_offset; |
| 1731 | u64 refs; |
| 1732 | int size; |
| 1733 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1734 | |
| 1735 | leaf = path->nodes[0]; |
| 1736 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1737 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1738 | |
| 1739 | type = extent_ref_type(parent, owner); |
| 1740 | size = btrfs_extent_inline_ref_size(type); |
| 1741 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1742 | btrfs_extend_item(root->fs_info, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1743 | |
| 1744 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1745 | refs = btrfs_extent_refs(leaf, ei); |
| 1746 | refs += refs_to_add; |
| 1747 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1748 | if (extent_op) |
| 1749 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1750 | |
| 1751 | ptr = (unsigned long)ei + item_offset; |
| 1752 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1753 | if (ptr < end - size) |
| 1754 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1755 | end - size - ptr); |
| 1756 | |
| 1757 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1758 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1759 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1760 | struct btrfs_extent_data_ref *dref; |
| 1761 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1762 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1763 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1764 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1765 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1766 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1767 | struct btrfs_shared_data_ref *sref; |
| 1768 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1769 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1770 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1771 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1772 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1773 | } else { |
| 1774 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1775 | } |
| 1776 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1780 | struct btrfs_root *root, |
| 1781 | struct btrfs_path *path, |
| 1782 | struct btrfs_extent_inline_ref **ref_ret, |
| 1783 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1784 | u64 root_objectid, u64 owner, u64 offset) |
| 1785 | { |
| 1786 | int ret; |
| 1787 | |
| 1788 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1789 | bytenr, num_bytes, parent, |
| 1790 | root_objectid, owner, offset, 0); |
| 1791 | if (ret != -ENOENT) |
| 1792 | return ret; |
| 1793 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1794 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1795 | *ref_ret = NULL; |
| 1796 | |
| 1797 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1798 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1799 | root_objectid); |
| 1800 | } else { |
| 1801 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1802 | root_objectid, owner, offset); |
| 1803 | } |
| 1804 | return ret; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * helper to update/remove inline back ref |
| 1809 | */ |
| 1810 | static noinline_for_stack |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1811 | void update_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1812 | struct btrfs_path *path, |
| 1813 | struct btrfs_extent_inline_ref *iref, |
| 1814 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1815 | struct btrfs_delayed_extent_op *extent_op, |
| 1816 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1817 | { |
| 1818 | struct extent_buffer *leaf; |
| 1819 | struct btrfs_extent_item *ei; |
| 1820 | struct btrfs_extent_data_ref *dref = NULL; |
| 1821 | struct btrfs_shared_data_ref *sref = NULL; |
| 1822 | unsigned long ptr; |
| 1823 | unsigned long end; |
| 1824 | u32 item_size; |
| 1825 | int size; |
| 1826 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1827 | u64 refs; |
| 1828 | |
| 1829 | leaf = path->nodes[0]; |
| 1830 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1831 | refs = btrfs_extent_refs(leaf, ei); |
| 1832 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1833 | refs += refs_to_mod; |
| 1834 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1835 | if (extent_op) |
| 1836 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1837 | |
| 1838 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1839 | |
| 1840 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1841 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1842 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1843 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1844 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1845 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1846 | } else { |
| 1847 | refs = 1; |
| 1848 | BUG_ON(refs_to_mod != -1); |
| 1849 | } |
| 1850 | |
| 1851 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1852 | refs += refs_to_mod; |
| 1853 | |
| 1854 | if (refs > 0) { |
| 1855 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1856 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1857 | else |
| 1858 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1859 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1860 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1861 | size = btrfs_extent_inline_ref_size(type); |
| 1862 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1863 | ptr = (unsigned long)iref; |
| 1864 | end = (unsigned long)ei + item_size; |
| 1865 | if (ptr + size < end) |
| 1866 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1867 | end - ptr - size); |
| 1868 | item_size -= size; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1869 | btrfs_truncate_item(root->fs_info, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1870 | } |
| 1871 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | static noinline_for_stack |
| 1875 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1876 | struct btrfs_root *root, |
| 1877 | struct btrfs_path *path, |
| 1878 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1879 | u64 root_objectid, u64 owner, |
| 1880 | u64 offset, int refs_to_add, |
| 1881 | struct btrfs_delayed_extent_op *extent_op) |
| 1882 | { |
| 1883 | struct btrfs_extent_inline_ref *iref; |
| 1884 | int ret; |
| 1885 | |
| 1886 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1887 | bytenr, num_bytes, parent, |
| 1888 | root_objectid, owner, offset, 1); |
| 1889 | if (ret == 0) { |
| 1890 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1891 | update_inline_extent_backref(root, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1892 | refs_to_add, extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1893 | } else if (ret == -ENOENT) { |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1894 | setup_inline_extent_backref(root, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1895 | root_objectid, owner, offset, |
| 1896 | refs_to_add, extent_op); |
| 1897 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1898 | } |
| 1899 | return ret; |
| 1900 | } |
| 1901 | |
| 1902 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1903 | struct btrfs_root *root, |
| 1904 | struct btrfs_path *path, |
| 1905 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1906 | u64 owner, u64 offset, int refs_to_add) |
| 1907 | { |
| 1908 | int ret; |
| 1909 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1910 | BUG_ON(refs_to_add != 1); |
| 1911 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1912 | parent, root_objectid); |
| 1913 | } else { |
| 1914 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1915 | parent, root_objectid, |
| 1916 | owner, offset, refs_to_add); |
| 1917 | } |
| 1918 | return ret; |
| 1919 | } |
| 1920 | |
| 1921 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1922 | struct btrfs_root *root, |
| 1923 | struct btrfs_path *path, |
| 1924 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1925 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1926 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1927 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1928 | |
| 1929 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1930 | if (iref) { |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1931 | update_inline_extent_backref(root, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1932 | -refs_to_drop, NULL, last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1933 | } else if (is_data) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1934 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop, |
| 1935 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1936 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1937 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1938 | ret = btrfs_del_item(trans, root, path); |
| 1939 | } |
| 1940 | return ret; |
| 1941 | } |
| 1942 | |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1943 | #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len)) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1944 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1945 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1946 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1947 | int j, ret = 0; |
| 1948 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1949 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1950 | |
| 1951 | if (WARN_ON(start != aligned_start)) { |
| 1952 | len -= aligned_start - start; |
| 1953 | len = round_down(len, 1 << 9); |
| 1954 | start = aligned_start; |
| 1955 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1956 | |
| 1957 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1958 | |
| 1959 | if (!len) |
| 1960 | return 0; |
| 1961 | |
| 1962 | end = start + len; |
| 1963 | bytes_left = len; |
| 1964 | |
| 1965 | /* Skip any superblocks on this device. */ |
| 1966 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1967 | u64 sb_start = btrfs_sb_offset(j); |
| 1968 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1969 | u64 size = sb_start - start; |
| 1970 | |
| 1971 | if (!in_range(sb_start, start, bytes_left) && |
| 1972 | !in_range(sb_end, start, bytes_left) && |
| 1973 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1974 | continue; |
| 1975 | |
| 1976 | /* |
| 1977 | * Superblock spans beginning of range. Adjust start and |
| 1978 | * try again. |
| 1979 | */ |
| 1980 | if (sb_start <= start) { |
| 1981 | start += sb_end - start; |
| 1982 | if (start > end) { |
| 1983 | bytes_left = 0; |
| 1984 | break; |
| 1985 | } |
| 1986 | bytes_left = end - start; |
| 1987 | continue; |
| 1988 | } |
| 1989 | |
| 1990 | if (size) { |
| 1991 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1992 | GFP_NOFS, 0); |
| 1993 | if (!ret) |
| 1994 | *discarded_bytes += size; |
| 1995 | else if (ret != -EOPNOTSUPP) |
| 1996 | return ret; |
| 1997 | } |
| 1998 | |
| 1999 | start = sb_end; |
| 2000 | if (start > end) { |
| 2001 | bytes_left = 0; |
| 2002 | break; |
| 2003 | } |
| 2004 | bytes_left = end - start; |
| 2005 | } |
| 2006 | |
| 2007 | if (bytes_left) { |
| 2008 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2009 | GFP_NOFS, 0); |
| 2010 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 2011 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2012 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2013 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2014 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2015 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2016 | int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr, |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 2017 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2018 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2019 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2020 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2021 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2022 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2023 | |
Filipe Manana | 2999241 | 2016-05-27 17:42:05 +0100 | [diff] [blame] | 2024 | /* |
| 2025 | * Avoid races with device replace and make sure our bbio has devices |
| 2026 | * associated to its stripes that don't go away while we are discarding. |
| 2027 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2028 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2029 | /* Tell the block device(s) that the sectors can be discarded */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2030 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
| 2031 | &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2032 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2033 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2034 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2035 | int i; |
| 2036 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2037 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2038 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2039 | u64 bytes; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2040 | if (!stripe->dev->can_discard) |
| 2041 | continue; |
| 2042 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2043 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 2044 | stripe->physical, |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2045 | stripe->length, |
| 2046 | &bytes); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2047 | if (!ret) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2048 | discarded_bytes += bytes; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2049 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2050 | 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] | 2051 | |
| 2052 | /* |
| 2053 | * Just in case we get back EOPNOTSUPP for some reason, |
| 2054 | * just ignore the return value so we don't screw up |
| 2055 | * people calling discard_extent. |
| 2056 | */ |
| 2057 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2058 | } |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 2059 | btrfs_put_bbio(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2060 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2061 | btrfs_bio_counter_dec(fs_info); |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2062 | |
| 2063 | if (actual_bytes) |
| 2064 | *actual_bytes = discarded_bytes; |
| 2065 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2066 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2067 | if (ret == -EOPNOTSUPP) |
| 2068 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2069 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2070 | } |
| 2071 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2072 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2073 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2074 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2075 | u64 bytenr, u64 num_bytes, u64 parent, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2076 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2077 | { |
| 2078 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2079 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2080 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 2081 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2082 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2083 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2084 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 2085 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2086 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2087 | BTRFS_ADD_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2088 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2089 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 2090 | num_bytes, parent, root_objectid, |
| 2091 | owner, offset, 0, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2092 | BTRFS_ADD_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2093 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2094 | return ret; |
| 2095 | } |
| 2096 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2097 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2098 | struct btrfs_fs_info *fs_info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2099 | struct btrfs_delayed_ref_node *node, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2100 | u64 parent, u64 root_objectid, |
| 2101 | u64 owner, u64 offset, int refs_to_add, |
| 2102 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2103 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2104 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2105 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2106 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2107 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2108 | u64 bytenr = node->bytenr; |
| 2109 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2110 | u64 refs; |
| 2111 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2112 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2113 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2114 | if (!path) |
| 2115 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2116 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2117 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2118 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2119 | /* this will setup the path even if it fails to insert the back ref */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2120 | ret = insert_inline_extent_backref(trans, fs_info->extent_root, path, |
| 2121 | bytenr, num_bytes, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2122 | root_objectid, owner, offset, |
| 2123 | refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 2124 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2125 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2126 | |
| 2127 | /* |
| 2128 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 2129 | * inline extent ref, so just update the reference count and add a |
| 2130 | * normal backref. |
| 2131 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2132 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2133 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2134 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2135 | refs = btrfs_extent_refs(leaf, item); |
| 2136 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 2137 | if (extent_op) |
| 2138 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2139 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2140 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2141 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2142 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2143 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2144 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2145 | /* now insert the actual backref */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2146 | ret = insert_extent_backref(trans, fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2147 | path, bytenr, parent, root_objectid, |
| 2148 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2149 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2150 | btrfs_abort_transaction(trans, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2151 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2152 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 2153 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2154 | } |
| 2155 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2156 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2157 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2158 | struct btrfs_delayed_ref_node *node, |
| 2159 | struct btrfs_delayed_extent_op *extent_op, |
| 2160 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2161 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2162 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2163 | struct btrfs_delayed_data_ref *ref; |
| 2164 | struct btrfs_key ins; |
| 2165 | u64 parent = 0; |
| 2166 | u64 ref_root = 0; |
| 2167 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2168 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2169 | ins.objectid = node->bytenr; |
| 2170 | ins.offset = node->num_bytes; |
| 2171 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2172 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2173 | ref = btrfs_delayed_node_to_data_ref(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2174 | trace_run_delayed_data_ref(fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2175 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2176 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2177 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2178 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2179 | |
| 2180 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2181 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2182 | flags |= extent_op->flags_to_set; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2183 | ret = alloc_reserved_file_extent(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2184 | parent, ref_root, flags, |
| 2185 | ref->objectid, ref->offset, |
| 2186 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2187 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2188 | ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2189 | ref_root, ref->objectid, |
| 2190 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2191 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2192 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2193 | ret = __btrfs_free_extent(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 { |
| 2198 | BUG(); |
| 2199 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2200 | return ret; |
| 2201 | } |
| 2202 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2203 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2204 | struct extent_buffer *leaf, |
| 2205 | struct btrfs_extent_item *ei) |
| 2206 | { |
| 2207 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2208 | if (extent_op->update_flags) { |
| 2209 | flags |= extent_op->flags_to_set; |
| 2210 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2211 | } |
| 2212 | |
| 2213 | if (extent_op->update_key) { |
| 2214 | struct btrfs_tree_block_info *bi; |
| 2215 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2216 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2217 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2222 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2223 | struct btrfs_delayed_ref_node *node, |
| 2224 | struct btrfs_delayed_extent_op *extent_op) |
| 2225 | { |
| 2226 | struct btrfs_key key; |
| 2227 | struct btrfs_path *path; |
| 2228 | struct btrfs_extent_item *ei; |
| 2229 | struct extent_buffer *leaf; |
| 2230 | u32 item_size; |
| 2231 | int ret; |
| 2232 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2233 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2234 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2235 | if (trans->aborted) |
| 2236 | return 0; |
| 2237 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2238 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2239 | metadata = 0; |
| 2240 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2241 | path = btrfs_alloc_path(); |
| 2242 | if (!path) |
| 2243 | return -ENOMEM; |
| 2244 | |
| 2245 | key.objectid = node->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2246 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2247 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2248 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2249 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2250 | } else { |
| 2251 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2252 | key.offset = node->num_bytes; |
| 2253 | } |
| 2254 | |
| 2255 | again: |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2256 | path->reada = READA_FORWARD; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2257 | path->leave_spinning = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2258 | 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] | 2259 | if (ret < 0) { |
| 2260 | err = ret; |
| 2261 | goto out; |
| 2262 | } |
| 2263 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2264 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2265 | if (path->slots[0] > 0) { |
| 2266 | path->slots[0]--; |
| 2267 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 2268 | path->slots[0]); |
| 2269 | if (key.objectid == node->bytenr && |
| 2270 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2271 | key.offset == node->num_bytes) |
| 2272 | ret = 0; |
| 2273 | } |
| 2274 | if (ret > 0) { |
| 2275 | btrfs_release_path(path); |
| 2276 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2277 | |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2278 | key.objectid = node->bytenr; |
| 2279 | key.offset = node->num_bytes; |
| 2280 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2281 | goto again; |
| 2282 | } |
| 2283 | } else { |
| 2284 | err = -EIO; |
| 2285 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2286 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | leaf = path->nodes[0]; |
| 2290 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2291 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2292 | if (item_size < sizeof(*ei)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2293 | ret = convert_extent_item_v0(trans, fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2294 | path, (u64)-1, 0); |
| 2295 | if (ret < 0) { |
| 2296 | err = ret; |
| 2297 | goto out; |
| 2298 | } |
| 2299 | leaf = path->nodes[0]; |
| 2300 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2301 | } |
| 2302 | #endif |
| 2303 | BUG_ON(item_size < sizeof(*ei)); |
| 2304 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2305 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2306 | |
| 2307 | btrfs_mark_buffer_dirty(leaf); |
| 2308 | out: |
| 2309 | btrfs_free_path(path); |
| 2310 | return err; |
| 2311 | } |
| 2312 | |
| 2313 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2314 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2315 | struct btrfs_delayed_ref_node *node, |
| 2316 | struct btrfs_delayed_extent_op *extent_op, |
| 2317 | int insert_reserved) |
| 2318 | { |
| 2319 | int ret = 0; |
| 2320 | struct btrfs_delayed_tree_ref *ref; |
| 2321 | struct btrfs_key ins; |
| 2322 | u64 parent = 0; |
| 2323 | u64 ref_root = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2324 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2325 | |
| 2326 | ref = btrfs_delayed_node_to_tree_ref(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2327 | trace_run_delayed_tree_ref(fs_info, node, ref, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2328 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2329 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2330 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2331 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2332 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2333 | ins.objectid = node->bytenr; |
| 2334 | if (skinny_metadata) { |
| 2335 | ins.offset = ref->level; |
| 2336 | ins.type = BTRFS_METADATA_ITEM_KEY; |
| 2337 | } else { |
| 2338 | ins.offset = node->num_bytes; |
| 2339 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2340 | } |
| 2341 | |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2342 | if (node->ref_mod != 1) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2343 | btrfs_err(fs_info, |
Liu Bo | 0279422 | 2016-09-14 19:19:05 -0700 | [diff] [blame] | 2344 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", |
| 2345 | node->bytenr, node->ref_mod, node->action, ref_root, |
| 2346 | parent); |
| 2347 | return -EIO; |
| 2348 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2349 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2350 | BUG_ON(!extent_op || !extent_op->update_flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2351 | ret = alloc_reserved_tree_block(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2352 | parent, ref_root, |
| 2353 | extent_op->flags_to_set, |
| 2354 | &extent_op->key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2355 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2356 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2357 | ret = __btrfs_inc_extent_ref(trans, fs_info, node, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2358 | parent, ref_root, |
| 2359 | ref->level, 0, 1, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2360 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2361 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2362 | ret = __btrfs_free_extent(trans, fs_info, node, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2363 | parent, ref_root, |
| 2364 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2365 | } else { |
| 2366 | BUG(); |
| 2367 | } |
| 2368 | return ret; |
| 2369 | } |
| 2370 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2371 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2372 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2373 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2374 | struct btrfs_delayed_ref_node *node, |
| 2375 | struct btrfs_delayed_extent_op *extent_op, |
| 2376 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2377 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2378 | int ret = 0; |
| 2379 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2380 | if (trans->aborted) { |
| 2381 | if (insert_reserved) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2382 | btrfs_pin_extent(fs_info, node->bytenr, |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2383 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2384 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2385 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2386 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2387 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2388 | struct btrfs_delayed_ref_head *head; |
| 2389 | /* |
| 2390 | * we've hit the end of the chain and we were supposed |
| 2391 | * to insert this extent into the tree. But, it got |
| 2392 | * deleted before we ever needed to insert it, so all |
| 2393 | * we have to do is clean up the accounting |
| 2394 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2395 | BUG_ON(extent_op); |
| 2396 | head = btrfs_delayed_node_to_head(node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2397 | trace_run_delayed_ref_head(fs_info, node, head, node->action); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2398 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2399 | if (insert_reserved) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2400 | btrfs_pin_extent(fs_info, node->bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2401 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2402 | if (head->is_data) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2403 | ret = btrfs_del_csums(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2404 | node->bytenr, |
| 2405 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2406 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2407 | } |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2408 | |
| 2409 | /* Also free its reserved qgroup space */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2410 | btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root, |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2411 | head->qgroup_reserved); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2412 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2413 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2414 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2415 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2416 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2417 | ret = run_delayed_tree_ref(trans, fs_info, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2418 | insert_reserved); |
| 2419 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2420 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2421 | ret = run_delayed_data_ref(trans, fs_info, node, extent_op, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2422 | insert_reserved); |
| 2423 | else |
| 2424 | BUG(); |
| 2425 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2426 | } |
| 2427 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2428 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2429 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2430 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2431 | struct btrfs_delayed_ref_node *ref; |
| 2432 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2433 | if (list_empty(&head->ref_list)) |
| 2434 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2435 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2436 | /* |
| 2437 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2438 | * This is to prevent a ref count from going down to zero, which deletes |
| 2439 | * the extent item from the extent tree, when there still are references |
| 2440 | * to add, which would fail because they would not find the extent item. |
| 2441 | */ |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2442 | if (!list_empty(&head->ref_add_list)) |
| 2443 | return list_first_entry(&head->ref_add_list, |
| 2444 | struct btrfs_delayed_ref_node, add_list); |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2445 | |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2446 | ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node, |
| 2447 | list); |
| 2448 | ASSERT(list_empty(&ref->add_list)); |
| 2449 | return ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2450 | } |
| 2451 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2452 | /* |
| 2453 | * Returns 0 on success or if called with an already aborted transaction. |
| 2454 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2455 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2456 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2457 | struct btrfs_fs_info *fs_info, |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2458 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2459 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2460 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2461 | struct btrfs_delayed_ref_node *ref; |
| 2462 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2463 | struct btrfs_delayed_extent_op *extent_op; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2464 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2465 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2466 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2467 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2468 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2469 | |
| 2470 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2471 | while (1) { |
| 2472 | if (!locked_ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2473 | if (count >= nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2474 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2475 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2476 | spin_lock(&delayed_refs->lock); |
| 2477 | locked_ref = btrfs_select_ref_head(trans); |
| 2478 | if (!locked_ref) { |
| 2479 | spin_unlock(&delayed_refs->lock); |
| 2480 | break; |
| 2481 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2482 | |
| 2483 | /* grab the lock that says we are going to process |
| 2484 | * all the refs for this head */ |
| 2485 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2486 | spin_unlock(&delayed_refs->lock); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2487 | /* |
| 2488 | * we may have dropped the spin lock to get the head |
| 2489 | * mutex lock, and that might have given someone else |
| 2490 | * time to free the head. If that's true, it has been |
| 2491 | * removed from our list and we can move on. |
| 2492 | */ |
| 2493 | if (ret == -EAGAIN) { |
| 2494 | locked_ref = NULL; |
| 2495 | count++; |
| 2496 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2500 | /* |
| 2501 | * We need to try and merge add/drops of the same ref since we |
| 2502 | * can run into issues with relocate dropping the implicit ref |
| 2503 | * and then it being added back again before the drop can |
| 2504 | * finish. If we merged anything we need to re-loop so we can |
| 2505 | * get a good ref. |
| 2506 | * Or we can get node references of the same type that weren't |
| 2507 | * merged when created due to bumps in the tree mod seq, and |
| 2508 | * we need to merge them to prevent adding an inline extent |
| 2509 | * backref before dropping it (triggering a BUG_ON at |
| 2510 | * insert_inline_extent_backref()). |
| 2511 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2512 | spin_lock(&locked_ref->lock); |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2513 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2514 | locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2515 | |
| 2516 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2517 | * locked_ref is the head node, so we have to go one |
| 2518 | * node back for any delayed ref updates |
| 2519 | */ |
| 2520 | ref = select_delayed_ref(locked_ref); |
| 2521 | |
| 2522 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2523 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2524 | spin_unlock(&locked_ref->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2525 | spin_lock(&delayed_refs->lock); |
| 2526 | locked_ref->processing = 0; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2527 | delayed_refs->num_heads_ready++; |
| 2528 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | d028099 | 2016-12-20 13:28:28 -0500 | [diff] [blame] | 2529 | btrfs_delayed_ref_unlock(locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2530 | locked_ref = NULL; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2531 | cond_resched(); |
Josef Bacik | 27a377d | 2014-02-07 13:57:59 -0500 | [diff] [blame] | 2532 | count++; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2533 | continue; |
| 2534 | } |
| 2535 | |
| 2536 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2537 | * record the must insert reserved flag before we |
| 2538 | * drop the spin lock. |
| 2539 | */ |
| 2540 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2541 | locked_ref->must_insert_reserved = 0; |
| 2542 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2543 | extent_op = locked_ref->extent_op; |
| 2544 | locked_ref->extent_op = NULL; |
| 2545 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2546 | if (!ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2547 | |
| 2548 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2549 | /* All delayed refs have been processed, Go ahead |
| 2550 | * and send the head node to run_one_delayed_ref, |
| 2551 | * so that any accounting fixes can happen |
| 2552 | */ |
| 2553 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2554 | |
| 2555 | if (extent_op && must_insert_reserved) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2556 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2557 | extent_op = NULL; |
| 2558 | } |
| 2559 | |
| 2560 | if (extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2561 | spin_unlock(&locked_ref->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2562 | ret = run_delayed_extent_op(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2563 | ref, extent_op); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2564 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2565 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2566 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2567 | /* |
| 2568 | * Need to reset must_insert_reserved if |
| 2569 | * there was an error so the abort stuff |
| 2570 | * can cleanup the reserved space |
| 2571 | * properly. |
| 2572 | */ |
| 2573 | if (must_insert_reserved) |
| 2574 | locked_ref->must_insert_reserved = 1; |
Jeff Mahoney | aa7c8da | 2016-12-20 13:28:27 -0500 | [diff] [blame] | 2575 | spin_lock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2576 | locked_ref->processing = 0; |
Jeff Mahoney | aa7c8da | 2016-12-20 13:28:27 -0500 | [diff] [blame] | 2577 | delayed_refs->num_heads_ready++; |
| 2578 | spin_unlock(&delayed_refs->lock); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2579 | btrfs_debug(fs_info, |
| 2580 | "run_delayed_extent_op returned %d", |
| 2581 | ret); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2582 | btrfs_delayed_ref_unlock(locked_ref); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2583 | return ret; |
| 2584 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2585 | continue; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2586 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2587 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2588 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2589 | * Need to drop our head ref lock and re-acquire the |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2590 | * delayed ref lock and then re-check to make sure |
| 2591 | * nobody got added. |
| 2592 | */ |
| 2593 | spin_unlock(&locked_ref->lock); |
| 2594 | spin_lock(&delayed_refs->lock); |
| 2595 | spin_lock(&locked_ref->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2596 | if (!list_empty(&locked_ref->ref_list) || |
Josef Bacik | 573a075 | 2014-03-27 19:41:34 -0400 | [diff] [blame] | 2597 | locked_ref->extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2598 | spin_unlock(&locked_ref->lock); |
| 2599 | spin_unlock(&delayed_refs->lock); |
| 2600 | continue; |
| 2601 | } |
| 2602 | ref->in_tree = 0; |
| 2603 | delayed_refs->num_heads--; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2604 | rb_erase(&locked_ref->href_node, |
| 2605 | &delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2606 | spin_unlock(&delayed_refs->lock); |
| 2607 | } else { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2608 | actual_count++; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2609 | ref->in_tree = 0; |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2610 | list_del(&ref->list); |
Wang Xiaoguang | 1d57ee9 | 2016-10-26 18:07:33 +0800 | [diff] [blame] | 2611 | if (!list_empty(&ref->add_list)) |
| 2612 | list_del(&ref->add_list); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2613 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2614 | atomic_dec(&delayed_refs->num_entries); |
| 2615 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2616 | if (!btrfs_delayed_ref_is_head(ref)) { |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2617 | /* |
| 2618 | * when we play the delayed ref, also correct the |
| 2619 | * ref_mod on head |
| 2620 | */ |
| 2621 | switch (ref->action) { |
| 2622 | case BTRFS_ADD_DELAYED_REF: |
| 2623 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2624 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2625 | break; |
| 2626 | case BTRFS_DROP_DELAYED_REF: |
| 2627 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2628 | break; |
| 2629 | default: |
| 2630 | WARN_ON(1); |
| 2631 | } |
| 2632 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2633 | spin_unlock(&locked_ref->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2634 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2635 | ret = run_one_delayed_ref(trans, fs_info, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2636 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2637 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2638 | btrfs_free_delayed_extent_op(extent_op); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2639 | if (ret) { |
Wang Xiaoguang | 9d1032c | 2016-06-20 09:18:52 +0800 | [diff] [blame] | 2640 | spin_lock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2641 | locked_ref->processing = 0; |
Wang Xiaoguang | 9d1032c | 2016-06-20 09:18:52 +0800 | [diff] [blame] | 2642 | delayed_refs->num_heads_ready++; |
| 2643 | spin_unlock(&delayed_refs->lock); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2644 | btrfs_delayed_ref_unlock(locked_ref); |
| 2645 | btrfs_put_delayed_ref(ref); |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2646 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
| 2647 | ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2648 | return ret; |
| 2649 | } |
| 2650 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2651 | /* |
| 2652 | * If this node is a head, that means all the refs in this head |
| 2653 | * have been dealt with, and we will pick the next head to deal |
| 2654 | * with, so we must unlock the head and drop it from the cluster |
| 2655 | * list before we release it. |
| 2656 | */ |
| 2657 | if (btrfs_delayed_ref_is_head(ref)) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2658 | if (locked_ref->is_data && |
| 2659 | locked_ref->total_ref_mod < 0) { |
| 2660 | spin_lock(&delayed_refs->lock); |
| 2661 | delayed_refs->pending_csums -= ref->num_bytes; |
| 2662 | spin_unlock(&delayed_refs->lock); |
| 2663 | } |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2664 | btrfs_delayed_ref_unlock(locked_ref); |
| 2665 | locked_ref = NULL; |
| 2666 | } |
| 2667 | btrfs_put_delayed_ref(ref); |
| 2668 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2669 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2670 | } |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2671 | |
| 2672 | /* |
| 2673 | * We don't want to include ref heads since we can have empty ref heads |
| 2674 | * and those will drastically skew our runtime down since we just do |
| 2675 | * accounting, no actual extent tree updates. |
| 2676 | */ |
| 2677 | if (actual_count > 0) { |
| 2678 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2679 | u64 avg; |
| 2680 | |
| 2681 | /* |
| 2682 | * We weigh the current average higher than our current runtime |
| 2683 | * to avoid large swings in the average. |
| 2684 | */ |
| 2685 | spin_lock(&delayed_refs->lock); |
| 2686 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2687 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2688 | spin_unlock(&delayed_refs->lock); |
| 2689 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2690 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2691 | } |
| 2692 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2693 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2694 | /* |
| 2695 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2696 | * correlates in most cases to the order added. To expose dependencies on this |
| 2697 | * order, we start to process the tree in the middle instead of the beginning |
| 2698 | */ |
| 2699 | static u64 find_middle(struct rb_root *root) |
| 2700 | { |
| 2701 | struct rb_node *n = root->rb_node; |
| 2702 | struct btrfs_delayed_ref_node *entry; |
| 2703 | int alt = 1; |
| 2704 | u64 middle; |
| 2705 | u64 first = 0, last = 0; |
| 2706 | |
| 2707 | n = rb_first(root); |
| 2708 | if (n) { |
| 2709 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2710 | first = entry->bytenr; |
| 2711 | } |
| 2712 | n = rb_last(root); |
| 2713 | if (n) { |
| 2714 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2715 | last = entry->bytenr; |
| 2716 | } |
| 2717 | n = root->rb_node; |
| 2718 | |
| 2719 | while (n) { |
| 2720 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2721 | WARN_ON(!entry->in_tree); |
| 2722 | |
| 2723 | middle = entry->bytenr; |
| 2724 | |
| 2725 | if (alt) |
| 2726 | n = n->rb_left; |
| 2727 | else |
| 2728 | n = n->rb_right; |
| 2729 | |
| 2730 | alt = 1 - alt; |
| 2731 | } |
| 2732 | return middle; |
| 2733 | } |
| 2734 | #endif |
| 2735 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2736 | 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] | 2737 | { |
| 2738 | u64 num_bytes; |
| 2739 | |
| 2740 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2741 | sizeof(struct btrfs_extent_inline_ref)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2742 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2743 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2744 | |
| 2745 | /* |
| 2746 | * 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] | 2747 | * closer to what we're really going to want to use. |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2748 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2749 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2750 | } |
| 2751 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2752 | /* |
| 2753 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2754 | * would require to store the csums for that many bytes. |
| 2755 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2756 | 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] | 2757 | { |
| 2758 | u64 csum_size; |
| 2759 | u64 num_csums_per_leaf; |
| 2760 | u64 num_csums; |
| 2761 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2762 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2763 | num_csums_per_leaf = div64_u64(csum_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2764 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
| 2765 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2766 | num_csums += num_csums_per_leaf - 1; |
| 2767 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2768 | return num_csums; |
| 2769 | } |
| 2770 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2771 | int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2772 | struct btrfs_fs_info *fs_info) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2773 | { |
| 2774 | struct btrfs_block_rsv *global_rsv; |
| 2775 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2776 | u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2777 | u64 num_dirty_bgs = trans->transaction->num_dirty_bgs; |
| 2778 | u64 num_bytes, num_dirty_bgs_bytes; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2779 | int ret = 0; |
| 2780 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2781 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2782 | num_heads = heads_to_leaves(fs_info, num_heads); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2783 | if (num_heads > 1) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2784 | num_bytes += (num_heads - 1) * fs_info->nodesize; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2785 | num_bytes <<= 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2786 | num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) * |
| 2787 | fs_info->nodesize; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2788 | num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info, |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2789 | num_dirty_bgs); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2790 | global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2791 | |
| 2792 | /* |
| 2793 | * If we can't allocate any more chunks lets make sure we have _lots_ of |
| 2794 | * wiggle room since running delayed refs can create more delayed refs. |
| 2795 | */ |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2796 | if (global_rsv->space_info->full) { |
| 2797 | num_dirty_bgs_bytes <<= 1; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2798 | num_bytes <<= 1; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2799 | } |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2800 | |
| 2801 | spin_lock(&global_rsv->lock); |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2802 | if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2803 | ret = 1; |
| 2804 | spin_unlock(&global_rsv->lock); |
| 2805 | return ret; |
| 2806 | } |
| 2807 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2808 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2809 | struct btrfs_fs_info *fs_info) |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2810 | { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2811 | u64 num_entries = |
| 2812 | atomic_read(&trans->transaction->delayed_refs.num_entries); |
| 2813 | u64 avg_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2814 | u64 val; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2815 | |
| 2816 | smp_mb(); |
| 2817 | avg_runtime = fs_info->avg_delayed_ref_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2818 | val = num_entries * avg_runtime; |
Wang Xiaoguang | dc1a90c | 2016-10-26 15:23:01 +0800 | [diff] [blame] | 2819 | if (val >= NSEC_PER_SEC) |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2820 | return 1; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2821 | if (val >= NSEC_PER_SEC / 2) |
| 2822 | return 2; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2823 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2824 | return btrfs_check_space_for_delayed_refs(trans, fs_info); |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2825 | } |
| 2826 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2827 | struct async_delayed_refs { |
| 2828 | struct btrfs_root *root; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2829 | u64 transid; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2830 | int count; |
| 2831 | int error; |
| 2832 | int sync; |
| 2833 | struct completion wait; |
| 2834 | struct btrfs_work work; |
| 2835 | }; |
| 2836 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2837 | static inline struct async_delayed_refs * |
| 2838 | to_async_delayed_refs(struct btrfs_work *work) |
| 2839 | { |
| 2840 | return container_of(work, struct async_delayed_refs, work); |
| 2841 | } |
| 2842 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2843 | static void delayed_ref_async_start(struct btrfs_work *work) |
| 2844 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2845 | struct async_delayed_refs *async = to_async_delayed_refs(work); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2846 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2847 | struct btrfs_fs_info *fs_info = async->root->fs_info; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2848 | int ret; |
| 2849 | |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2850 | /* 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] | 2851 | if (btrfs_transaction_blocked(fs_info)) |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2852 | goto done; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2853 | |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2854 | trans = btrfs_join_transaction(async->root); |
| 2855 | if (IS_ERR(trans)) { |
| 2856 | async->error = PTR_ERR(trans); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2857 | goto done; |
| 2858 | } |
| 2859 | |
| 2860 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2861 | * trans->sync means that when we call end_transaction, we won't |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2862 | * wait on delayed refs |
| 2863 | */ |
| 2864 | trans->sync = true; |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2865 | |
| 2866 | /* Don't bother flushing if we got into a different transaction */ |
| 2867 | if (trans->transid > async->transid) |
| 2868 | goto end; |
| 2869 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2870 | ret = btrfs_run_delayed_refs(trans, fs_info, async->count); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2871 | if (ret) |
| 2872 | async->error = ret; |
Chris Mason | 0f873ec | 2016-04-27 09:59:38 -0400 | [diff] [blame] | 2873 | end: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2874 | ret = btrfs_end_transaction(trans); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2875 | if (ret && !async->error) |
| 2876 | async->error = ret; |
| 2877 | done: |
| 2878 | if (async->sync) |
| 2879 | complete(&async->wait); |
| 2880 | else |
| 2881 | kfree(async); |
| 2882 | } |
| 2883 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2884 | int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info, |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2885 | unsigned long count, u64 transid, int wait) |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2886 | { |
| 2887 | struct async_delayed_refs *async; |
| 2888 | int ret; |
| 2889 | |
| 2890 | async = kmalloc(sizeof(*async), GFP_NOFS); |
| 2891 | if (!async) |
| 2892 | return -ENOMEM; |
| 2893 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2894 | async->root = fs_info->tree_root; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2895 | async->count = count; |
| 2896 | async->error = 0; |
Josef Bacik | 31b9655 | 2016-04-11 17:37:40 -0400 | [diff] [blame] | 2897 | async->transid = transid; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2898 | if (wait) |
| 2899 | async->sync = 1; |
| 2900 | else |
| 2901 | async->sync = 0; |
| 2902 | init_completion(&async->wait); |
| 2903 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2904 | btrfs_init_work(&async->work, btrfs_extent_refs_helper, |
| 2905 | delayed_ref_async_start, NULL, NULL); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2906 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2907 | btrfs_queue_work(fs_info->extent_workers, &async->work); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2908 | |
| 2909 | if (wait) { |
| 2910 | wait_for_completion(&async->wait); |
| 2911 | ret = async->error; |
| 2912 | kfree(async); |
| 2913 | return ret; |
| 2914 | } |
| 2915 | return 0; |
| 2916 | } |
| 2917 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2918 | /* |
| 2919 | * this starts processing the delayed reference count updates and |
| 2920 | * extent insertions we have queued up so far. count can be |
| 2921 | * 0, which means to process everything in the tree at the start |
| 2922 | * of the run (but not newly added entries), or it can be some target |
| 2923 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2924 | * |
| 2925 | * Returns 0 on success or if called with an aborted transaction |
| 2926 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2927 | */ |
| 2928 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2929 | struct btrfs_fs_info *fs_info, unsigned long count) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2930 | { |
| 2931 | struct rb_node *node; |
| 2932 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2933 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2934 | int ret; |
| 2935 | int run_all = count == (unsigned long)-1; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2936 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2937 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2938 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2939 | if (trans->aborted) |
| 2940 | return 0; |
| 2941 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2942 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
Chris Mason | 511711a | 2015-12-30 07:52:35 -0800 | [diff] [blame] | 2943 | return 0; |
| 2944 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2945 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2946 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2947 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2948 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2949 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2950 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2951 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2952 | #endif |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2953 | trans->can_flush_pending_bgs = false; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2954 | ret = __btrfs_run_delayed_refs(trans, fs_info, count); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2955 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2956 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2957 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2958 | } |
| 2959 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2960 | if (run_all) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2961 | if (!list_empty(&trans->new_bgs)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2962 | btrfs_create_pending_block_groups(trans, fs_info); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2963 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2964 | spin_lock(&delayed_refs->lock); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2965 | node = rb_first(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2966 | if (!node) { |
| 2967 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2968 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2969 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2970 | |
| 2971 | while (node) { |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2972 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2973 | href_node); |
| 2974 | if (btrfs_delayed_ref_is_head(&head->node)) { |
| 2975 | struct btrfs_delayed_ref_node *ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2976 | |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2977 | ref = &head->node; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2978 | atomic_inc(&ref->refs); |
| 2979 | |
| 2980 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2981 | /* |
| 2982 | * Mutex was contended, block until it's |
| 2983 | * released and try again |
| 2984 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2985 | mutex_lock(&head->mutex); |
| 2986 | mutex_unlock(&head->mutex); |
| 2987 | |
| 2988 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2989 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2990 | goto again; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2991 | } else { |
| 2992 | WARN_ON(1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2993 | } |
| 2994 | node = rb_next(node); |
| 2995 | } |
| 2996 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2997 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2998 | goto again; |
| 2999 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3000 | out: |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 3001 | assert_qgroups_uptodate(trans); |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 3002 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3003 | return 0; |
| 3004 | } |
| 3005 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3006 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3007 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3008 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 3009 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3010 | { |
| 3011 | struct btrfs_delayed_extent_op *extent_op; |
| 3012 | int ret; |
| 3013 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 3014 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3015 | if (!extent_op) |
| 3016 | return -ENOMEM; |
| 3017 | |
| 3018 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 3019 | extent_op->update_flags = true; |
| 3020 | extent_op->update_key = false; |
| 3021 | extent_op->is_data = is_data ? true : false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 3022 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3023 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3024 | ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3025 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3026 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 3027 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3028 | return ret; |
| 3029 | } |
| 3030 | |
| 3031 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 3032 | struct btrfs_root *root, |
| 3033 | struct btrfs_path *path, |
| 3034 | u64 objectid, u64 offset, u64 bytenr) |
| 3035 | { |
| 3036 | struct btrfs_delayed_ref_head *head; |
| 3037 | struct btrfs_delayed_ref_node *ref; |
| 3038 | struct btrfs_delayed_data_ref *data_ref; |
| 3039 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3040 | int ret = 0; |
| 3041 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3042 | delayed_refs = &trans->transaction->delayed_refs; |
| 3043 | spin_lock(&delayed_refs->lock); |
| 3044 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3045 | if (!head) { |
| 3046 | spin_unlock(&delayed_refs->lock); |
| 3047 | return 0; |
| 3048 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3049 | |
| 3050 | if (!mutex_trylock(&head->mutex)) { |
| 3051 | atomic_inc(&head->node.refs); |
| 3052 | spin_unlock(&delayed_refs->lock); |
| 3053 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3054 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3055 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 3056 | /* |
| 3057 | * Mutex was contended, block until it's released and let |
| 3058 | * caller try again |
| 3059 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3060 | mutex_lock(&head->mutex); |
| 3061 | mutex_unlock(&head->mutex); |
| 3062 | btrfs_put_delayed_ref(&head->node); |
| 3063 | return -EAGAIN; |
| 3064 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3065 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3066 | |
| 3067 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 3068 | list_for_each_entry(ref, &head->ref_list, list) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3069 | /* If it's a shared ref we know a cross reference exists */ |
| 3070 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 3071 | ret = 1; |
| 3072 | break; |
| 3073 | } |
| 3074 | |
| 3075 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 3076 | |
| 3077 | /* |
| 3078 | * If our ref doesn't match the one we're currently looking at |
| 3079 | * then we have a cross reference. |
| 3080 | */ |
| 3081 | if (data_ref->root != root->root_key.objectid || |
| 3082 | data_ref->objectid != objectid || |
| 3083 | data_ref->offset != offset) { |
| 3084 | ret = 1; |
| 3085 | break; |
| 3086 | } |
| 3087 | } |
| 3088 | spin_unlock(&head->lock); |
| 3089 | mutex_unlock(&head->mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3090 | return ret; |
| 3091 | } |
| 3092 | |
| 3093 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 3094 | struct btrfs_root *root, |
| 3095 | struct btrfs_path *path, |
| 3096 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3097 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3098 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3099 | struct btrfs_root *extent_root = fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3100 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3101 | struct btrfs_extent_data_ref *ref; |
| 3102 | struct btrfs_extent_inline_ref *iref; |
| 3103 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3104 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3105 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3106 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3107 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3108 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3109 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3110 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3111 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3112 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 3113 | if (ret < 0) |
| 3114 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3115 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 3116 | |
| 3117 | ret = -ENOENT; |
| 3118 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3119 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3120 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3121 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3122 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3123 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3124 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3125 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3126 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3127 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3128 | ret = 1; |
| 3129 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 3130 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3131 | if (item_size < sizeof(*ei)) { |
| 3132 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 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 | #endif |
| 3136 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 3137 | |
| 3138 | if (item_size != sizeof(*ei) + |
| 3139 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 3140 | goto out; |
| 3141 | |
| 3142 | if (btrfs_extent_generation(leaf, ei) <= |
| 3143 | btrfs_root_last_snapshot(&root->root_item)) |
| 3144 | goto out; |
| 3145 | |
| 3146 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 3147 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 3148 | BTRFS_EXTENT_DATA_REF_KEY) |
| 3149 | goto out; |
| 3150 | |
| 3151 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3152 | if (btrfs_extent_refs(leaf, ei) != |
| 3153 | btrfs_extent_data_ref_count(leaf, ref) || |
| 3154 | btrfs_extent_data_ref_root(leaf, ref) != |
| 3155 | root->root_key.objectid || |
| 3156 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 3157 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 3158 | goto out; |
| 3159 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3160 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3161 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3162 | return ret; |
| 3163 | } |
| 3164 | |
| 3165 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 3166 | struct btrfs_root *root, |
| 3167 | u64 objectid, u64 offset, u64 bytenr) |
| 3168 | { |
| 3169 | struct btrfs_path *path; |
| 3170 | int ret; |
| 3171 | int ret2; |
| 3172 | |
| 3173 | path = btrfs_alloc_path(); |
| 3174 | if (!path) |
| 3175 | return -ENOENT; |
| 3176 | |
| 3177 | do { |
| 3178 | ret = check_committed_ref(trans, root, path, objectid, |
| 3179 | offset, bytenr); |
| 3180 | if (ret && ret != -ENOENT) |
| 3181 | goto out; |
| 3182 | |
| 3183 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 3184 | offset, bytenr); |
| 3185 | } while (ret2 == -EAGAIN); |
| 3186 | |
| 3187 | if (ret2 && ret2 != -ENOENT) { |
| 3188 | ret = ret2; |
| 3189 | goto out; |
| 3190 | } |
| 3191 | |
| 3192 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 3193 | ret = 0; |
| 3194 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3195 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3196 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 3197 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3198 | return ret; |
| 3199 | } |
| 3200 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3201 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3202 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3203 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3204 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3205 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3206 | struct btrfs_fs_info *fs_info = root->fs_info; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3207 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3208 | u64 num_bytes; |
| 3209 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3210 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3211 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3212 | struct btrfs_key key; |
| 3213 | struct btrfs_file_extent_item *fi; |
| 3214 | int i; |
| 3215 | int level; |
| 3216 | int ret = 0; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3217 | int (*process_func)(struct btrfs_trans_handle *, |
| 3218 | struct btrfs_fs_info *, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3219 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3220 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3221 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3222 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3223 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3224 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3225 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3226 | nritems = btrfs_header_nritems(buf); |
| 3227 | level = btrfs_header_level(buf); |
| 3228 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3229 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3230 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3231 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3232 | if (inc) |
| 3233 | process_func = btrfs_inc_extent_ref; |
| 3234 | else |
| 3235 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3236 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3237 | if (full_backref) |
| 3238 | parent = buf->start; |
| 3239 | else |
| 3240 | parent = 0; |
| 3241 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3242 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3243 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3244 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3245 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3246 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3247 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3248 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3249 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3250 | BTRFS_FILE_EXTENT_INLINE) |
| 3251 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3252 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 3253 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3254 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3255 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3256 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 3257 | key.offset -= btrfs_file_extent_offset(buf, fi); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3258 | ret = process_func(trans, fs_info, bytenr, num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3259 | parent, ref_root, key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3260 | key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3261 | if (ret) |
| 3262 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3263 | } else { |
| 3264 | bytenr = btrfs_node_blockptr(buf, i); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3265 | num_bytes = fs_info->nodesize; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3266 | ret = process_func(trans, fs_info, bytenr, num_bytes, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3267 | parent, ref_root, level - 1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3268 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3269 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3270 | } |
| 3271 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3272 | return 0; |
| 3273 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3274 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3275 | } |
| 3276 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3277 | 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] | 3278 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3279 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3280 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3281 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3282 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3283 | 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] | 3284 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45: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, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3287 | } |
| 3288 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3289 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3290 | struct btrfs_fs_info *fs_info, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3291 | struct btrfs_path *path, |
| 3292 | struct btrfs_block_group_cache *cache) |
| 3293 | { |
| 3294 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3295 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3296 | unsigned long bi; |
| 3297 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3298 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3299 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3300 | if (ret) { |
| 3301 | if (ret > 0) |
| 3302 | ret = -ENOENT; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3303 | goto fail; |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3304 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3305 | |
| 3306 | leaf = path->nodes[0]; |
| 3307 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3308 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3309 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3310 | fail: |
Filipe Manana | 24b89d0 | 2015-04-25 18:31:05 +0100 | [diff] [blame] | 3311 | btrfs_release_path(path); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3312 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3313 | |
| 3314 | } |
| 3315 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3316 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3317 | next_block_group(struct btrfs_fs_info *fs_info, |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3318 | struct btrfs_block_group_cache *cache) |
| 3319 | { |
| 3320 | struct rb_node *node; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3321 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3322 | spin_lock(&fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3323 | |
| 3324 | /* If our block group was removed, we need a full search. */ |
| 3325 | if (RB_EMPTY_NODE(&cache->cache_node)) { |
| 3326 | const u64 next_bytenr = cache->key.objectid + cache->key.offset; |
| 3327 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3328 | spin_unlock(&fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3329 | btrfs_put_block_group(cache); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3330 | cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3331 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3332 | node = rb_next(&cache->cache_node); |
| 3333 | btrfs_put_block_group(cache); |
| 3334 | if (node) { |
| 3335 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 3336 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 3337 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3338 | } else |
| 3339 | cache = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3340 | spin_unlock(&fs_info->block_group_cache_lock); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3341 | return cache; |
| 3342 | } |
| 3343 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3344 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3345 | struct btrfs_trans_handle *trans, |
| 3346 | struct btrfs_path *path) |
| 3347 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3348 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 3349 | struct btrfs_root *root = fs_info->tree_root; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3350 | struct inode *inode = NULL; |
| 3351 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3352 | int dcs = BTRFS_DC_ERROR; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 3353 | u64 num_pages = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3354 | int retries = 0; |
| 3355 | int ret = 0; |
| 3356 | |
| 3357 | /* |
| 3358 | * If this block group is smaller than 100 megs don't bother caching the |
| 3359 | * block group. |
| 3360 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3361 | if (block_group->key.offset < (100 * SZ_1M)) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3362 | spin_lock(&block_group->lock); |
| 3363 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3364 | spin_unlock(&block_group->lock); |
| 3365 | return 0; |
| 3366 | } |
| 3367 | |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3368 | if (trans->aborted) |
| 3369 | return 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3370 | again: |
| 3371 | inode = lookup_free_space_inode(root, block_group, path); |
| 3372 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3373 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3374 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3375 | goto out; |
| 3376 | } |
| 3377 | |
| 3378 | if (IS_ERR(inode)) { |
| 3379 | BUG_ON(retries); |
| 3380 | retries++; |
| 3381 | |
| 3382 | if (block_group->ro) |
| 3383 | goto out_free; |
| 3384 | |
| 3385 | ret = create_free_space_inode(root, trans, block_group, path); |
| 3386 | if (ret) |
| 3387 | goto out_free; |
| 3388 | goto again; |
| 3389 | } |
| 3390 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3391 | /* We've already setup this transaction, go ahead and exit */ |
| 3392 | if (block_group->cache_generation == trans->transid && |
| 3393 | i_size_read(inode)) { |
| 3394 | dcs = BTRFS_DC_SETUP; |
| 3395 | goto out_put; |
| 3396 | } |
| 3397 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3398 | /* |
| 3399 | * We want to set the generation to 0, that way if anything goes wrong |
| 3400 | * from here on out we know not to trust this cache when we load up next |
| 3401 | * time. |
| 3402 | */ |
| 3403 | BTRFS_I(inode)->generation = 0; |
| 3404 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3405 | if (ret) { |
| 3406 | /* |
| 3407 | * So theoretically we could recover from this, simply set the |
| 3408 | * super cache generation to 0 so we know to invalidate the |
| 3409 | * cache, but then we'd have to keep track of the block groups |
| 3410 | * that fail this way so we know we _have_ to reset this cache |
| 3411 | * before the next commit or risk reading stale cache. So to |
| 3412 | * limit our exposure to horrible edge cases lets just abort the |
| 3413 | * transaction, this only happens in really bad situations |
| 3414 | * anyway. |
| 3415 | */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3416 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3417 | goto out_put; |
| 3418 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3419 | WARN_ON(ret); |
| 3420 | |
| 3421 | if (i_size_read(inode) > 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3422 | ret = btrfs_check_trunc_cache_free_space(fs_info, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3423 | &fs_info->global_block_rsv); |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3424 | if (ret) |
| 3425 | goto out_put; |
| 3426 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3427 | ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3428 | if (ret) |
| 3429 | goto out_put; |
| 3430 | } |
| 3431 | |
| 3432 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3433 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3434 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3435 | /* |
| 3436 | * don't bother trying to write stuff out _if_ |
| 3437 | * a) we're not cached, |
| 3438 | * b) we're with nospace_cache mount option. |
| 3439 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3440 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3441 | spin_unlock(&block_group->lock); |
| 3442 | goto out_put; |
| 3443 | } |
| 3444 | spin_unlock(&block_group->lock); |
| 3445 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3446 | /* |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3447 | * We hit an ENOSPC when setting up the cache in this transaction, just |
| 3448 | * skip doing the setup, we've already cleared the cache so we're safe. |
| 3449 | */ |
| 3450 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { |
| 3451 | ret = -ENOSPC; |
| 3452 | goto out_put; |
| 3453 | } |
| 3454 | |
| 3455 | /* |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3456 | * Try to preallocate enough space based on how big the block group is. |
| 3457 | * Keep in mind this has to include any pinned space which could end up |
| 3458 | * taking up quite a bit since it's not folded into the other space |
| 3459 | * cache. |
| 3460 | */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3461 | num_pages = div_u64(block_group->key.offset, SZ_256M); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3462 | if (!num_pages) |
| 3463 | num_pages = 1; |
| 3464 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3465 | num_pages *= 16; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3466 | num_pages *= PAGE_SIZE; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3467 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 3468 | ret = btrfs_check_data_free_space(inode, 0, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3469 | if (ret) |
| 3470 | goto out_put; |
| 3471 | |
| 3472 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3473 | num_pages, num_pages, |
| 3474 | &alloc_hint); |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3475 | /* |
| 3476 | * Our cache requires contiguous chunks so that we don't modify a bunch |
| 3477 | * of metadata or split extents when writing the cache out, which means |
| 3478 | * we can enospc if we are heavily fragmented in addition to just normal |
| 3479 | * out of space conditions. So if we hit this just skip setting up any |
| 3480 | * other block groups for this transaction, maybe we'll unpin enough |
| 3481 | * space the next time around. |
| 3482 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3483 | if (!ret) |
| 3484 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3485 | else if (ret == -ENOSPC) |
| 3486 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3487 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3488 | out_put: |
| 3489 | iput(inode); |
| 3490 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3491 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3492 | out: |
| 3493 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3494 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3495 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3496 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3497 | spin_unlock(&block_group->lock); |
| 3498 | |
| 3499 | return ret; |
| 3500 | } |
| 3501 | |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3502 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3503 | struct btrfs_fs_info *fs_info) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3504 | { |
| 3505 | struct btrfs_block_group_cache *cache, *tmp; |
| 3506 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3507 | struct btrfs_path *path; |
| 3508 | |
| 3509 | if (list_empty(&cur_trans->dirty_bgs) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3510 | !btrfs_test_opt(fs_info, SPACE_CACHE)) |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3511 | return 0; |
| 3512 | |
| 3513 | path = btrfs_alloc_path(); |
| 3514 | if (!path) |
| 3515 | return -ENOMEM; |
| 3516 | |
| 3517 | /* Could add new block groups, use _safe just in case */ |
| 3518 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, |
| 3519 | dirty_list) { |
| 3520 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3521 | cache_save_setup(cache, trans, path); |
| 3522 | } |
| 3523 | |
| 3524 | btrfs_free_path(path); |
| 3525 | return 0; |
| 3526 | } |
| 3527 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3528 | /* |
| 3529 | * transaction commit does final block group cache writeback during a |
| 3530 | * critical section where nothing is allowed to change the FS. This is |
| 3531 | * required in order for the cache to actually match the block group, |
| 3532 | * but can introduce a lot of latency into the commit. |
| 3533 | * |
| 3534 | * So, btrfs_start_dirty_block_groups is here to kick off block group |
| 3535 | * cache IO. There's a chance we'll have to redo some of it if the |
| 3536 | * block group changes again during the commit, but it greatly reduces |
| 3537 | * the commit latency by getting rid of the easy block groups while |
| 3538 | * we're still allowing others to join the commit. |
| 3539 | */ |
| 3540 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3541 | struct btrfs_fs_info *fs_info) |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3542 | { |
| 3543 | struct btrfs_block_group_cache *cache; |
| 3544 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3545 | int ret = 0; |
| 3546 | int should_put; |
| 3547 | struct btrfs_path *path = NULL; |
| 3548 | LIST_HEAD(dirty); |
| 3549 | struct list_head *io = &cur_trans->io_bgs; |
| 3550 | int num_started = 0; |
| 3551 | int loops = 0; |
| 3552 | |
| 3553 | spin_lock(&cur_trans->dirty_bgs_lock); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3554 | if (list_empty(&cur_trans->dirty_bgs)) { |
| 3555 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3556 | return 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3557 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3558 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3559 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3560 | |
| 3561 | again: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3562 | /* |
| 3563 | * make sure all the block groups on our dirty list actually |
| 3564 | * exist |
| 3565 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3566 | btrfs_create_pending_block_groups(trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3567 | |
| 3568 | if (!path) { |
| 3569 | path = btrfs_alloc_path(); |
| 3570 | if (!path) |
| 3571 | return -ENOMEM; |
| 3572 | } |
| 3573 | |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3574 | /* |
| 3575 | * cache_write_mutex is here only to save us from balance or automatic |
| 3576 | * removal of empty block groups deleting this block group while we are |
| 3577 | * writing out the cache |
| 3578 | */ |
| 3579 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3580 | while (!list_empty(&dirty)) { |
| 3581 | cache = list_first_entry(&dirty, |
| 3582 | struct btrfs_block_group_cache, |
| 3583 | dirty_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3584 | /* |
| 3585 | * this can happen if something re-dirties a block |
| 3586 | * group that is already under IO. Just wait for it to |
| 3587 | * finish and then do it all again |
| 3588 | */ |
| 3589 | if (!list_empty(&cache->io_list)) { |
| 3590 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3591 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3592 | btrfs_put_block_group(cache); |
| 3593 | } |
| 3594 | |
| 3595 | |
| 3596 | /* |
| 3597 | * btrfs_wait_cache_io uses the cache->dirty_list to decide |
| 3598 | * if it should update the cache_state. Don't delete |
| 3599 | * until after we wait. |
| 3600 | * |
| 3601 | * Since we're not running in the commit critical section |
| 3602 | * we need the dirty_bgs_lock to protect from update_block_group |
| 3603 | */ |
| 3604 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3605 | list_del_init(&cache->dirty_list); |
| 3606 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3607 | |
| 3608 | should_put = 1; |
| 3609 | |
| 3610 | cache_save_setup(cache, trans, path); |
| 3611 | |
| 3612 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3613 | cache->io_ctl.inode = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3614 | ret = btrfs_write_out_cache(fs_info, trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3615 | cache, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3616 | if (ret == 0 && cache->io_ctl.inode) { |
| 3617 | num_started++; |
| 3618 | should_put = 0; |
| 3619 | |
| 3620 | /* |
| 3621 | * the cache_write_mutex is protecting |
| 3622 | * the io_list |
| 3623 | */ |
| 3624 | list_add_tail(&cache->io_list, io); |
| 3625 | } else { |
| 3626 | /* |
| 3627 | * if we failed to write the cache, the |
| 3628 | * generation will be bad and life goes on |
| 3629 | */ |
| 3630 | ret = 0; |
| 3631 | } |
| 3632 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3633 | if (!ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3634 | ret = write_one_cache_group(trans, fs_info, |
| 3635 | path, cache); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3636 | /* |
| 3637 | * Our block group might still be attached to the list |
| 3638 | * of new block groups in the transaction handle of some |
| 3639 | * other task (struct btrfs_trans_handle->new_bgs). This |
| 3640 | * means its block group item isn't yet in the extent |
| 3641 | * tree. If this happens ignore the error, as we will |
| 3642 | * try again later in the critical section of the |
| 3643 | * transaction commit. |
| 3644 | */ |
| 3645 | if (ret == -ENOENT) { |
| 3646 | ret = 0; |
| 3647 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3648 | if (list_empty(&cache->dirty_list)) { |
| 3649 | list_add_tail(&cache->dirty_list, |
| 3650 | &cur_trans->dirty_bgs); |
| 3651 | btrfs_get_block_group(cache); |
| 3652 | } |
| 3653 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3654 | } else if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3655 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3656 | } |
| 3657 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3658 | |
| 3659 | /* if its not on the io list, we need to put the block group */ |
| 3660 | if (should_put) |
| 3661 | btrfs_put_block_group(cache); |
| 3662 | |
| 3663 | if (ret) |
| 3664 | break; |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3665 | |
| 3666 | /* |
| 3667 | * Avoid blocking other tasks for too long. It might even save |
| 3668 | * us from writing caches for block groups that are going to be |
| 3669 | * removed. |
| 3670 | */ |
| 3671 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 3672 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3673 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3674 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3675 | |
| 3676 | /* |
| 3677 | * go through delayed refs for all the stuff we've just kicked off |
| 3678 | * and then loop back (just once) |
| 3679 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3680 | ret = btrfs_run_delayed_refs(trans, fs_info, 0); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3681 | if (!ret && loops == 0) { |
| 3682 | loops++; |
| 3683 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3684 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3685 | /* |
| 3686 | * dirty_bgs_lock protects us from concurrent block group |
| 3687 | * deletes too (not just cache_write_mutex). |
| 3688 | */ |
| 3689 | if (!list_empty(&dirty)) { |
| 3690 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3691 | goto again; |
| 3692 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3693 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Liu Bo | c79a175 | 2016-07-20 17:44:12 -0700 | [diff] [blame] | 3694 | } else if (ret < 0) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3695 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | btrfs_free_path(path); |
| 3699 | return ret; |
| 3700 | } |
| 3701 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3702 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3703 | struct btrfs_fs_info *fs_info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3704 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3705 | struct btrfs_block_group_cache *cache; |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3706 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3707 | int ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3708 | int should_put; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3709 | struct btrfs_path *path; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3710 | struct list_head *io = &cur_trans->io_bgs; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3711 | int num_started = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3712 | |
| 3713 | path = btrfs_alloc_path(); |
| 3714 | if (!path) |
| 3715 | return -ENOMEM; |
| 3716 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3717 | /* |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3718 | * Even though we are in the critical section of the transaction commit, |
| 3719 | * we can still have concurrent tasks adding elements to this |
| 3720 | * transaction's list of dirty block groups. These tasks correspond to |
| 3721 | * endio free space workers started when writeback finishes for a |
| 3722 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can |
| 3723 | * allocate new block groups as a result of COWing nodes of the root |
| 3724 | * tree when updating the free space inode. The writeback for the space |
| 3725 | * caches is triggered by an earlier call to |
| 3726 | * btrfs_start_dirty_block_groups() and iterations of the following |
| 3727 | * loop. |
| 3728 | * 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] | 3729 | * delayed refs to make sure we have the best chance at doing this all |
| 3730 | * in one shot. |
| 3731 | */ |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3732 | spin_lock(&cur_trans->dirty_bgs_lock); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3733 | while (!list_empty(&cur_trans->dirty_bgs)) { |
| 3734 | cache = list_first_entry(&cur_trans->dirty_bgs, |
| 3735 | struct btrfs_block_group_cache, |
| 3736 | dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3737 | |
| 3738 | /* |
| 3739 | * this can happen if cache_save_setup re-dirties a block |
| 3740 | * group that is already under IO. Just wait for it to |
| 3741 | * finish and then do it all again |
| 3742 | */ |
| 3743 | if (!list_empty(&cache->io_list)) { |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3744 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3745 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3746 | btrfs_wait_cache_io(trans, cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3747 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3748 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3751 | /* |
| 3752 | * don't remove from the dirty list until after we've waited |
| 3753 | * on any pending IO |
| 3754 | */ |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3755 | list_del_init(&cache->dirty_list); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3756 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3757 | should_put = 1; |
| 3758 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3759 | cache_save_setup(cache, trans, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3760 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3761 | if (!ret) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3762 | ret = btrfs_run_delayed_refs(trans, fs_info, |
| 3763 | (unsigned long) -1); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3764 | |
| 3765 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3766 | cache->io_ctl.inode = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3767 | ret = btrfs_write_out_cache(fs_info, trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3768 | cache, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3769 | if (ret == 0 && cache->io_ctl.inode) { |
| 3770 | num_started++; |
| 3771 | should_put = 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3772 | list_add_tail(&cache->io_list, io); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3773 | } else { |
| 3774 | /* |
| 3775 | * if we failed to write the cache, the |
| 3776 | * generation will be bad and life goes on |
| 3777 | */ |
| 3778 | ret = 0; |
| 3779 | } |
| 3780 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3781 | if (!ret) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3782 | ret = write_one_cache_group(trans, fs_info, |
| 3783 | path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3784 | /* |
| 3785 | * One of the free space endio workers might have |
| 3786 | * created a new block group while updating a free space |
| 3787 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) |
| 3788 | * and hasn't released its transaction handle yet, in |
| 3789 | * which case the new block group is still attached to |
| 3790 | * its transaction handle and its creation has not |
| 3791 | * finished yet (no block group item in the extent tree |
| 3792 | * yet, etc). If this is the case, wait for all free |
| 3793 | * space endio workers to finish and retry. This is a |
| 3794 | * a very rare case so no need for a more efficient and |
| 3795 | * complex approach. |
| 3796 | */ |
| 3797 | if (ret == -ENOENT) { |
| 3798 | wait_event(cur_trans->writer_wait, |
| 3799 | atomic_read(&cur_trans->num_writers) == 1); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3800 | ret = write_one_cache_group(trans, fs_info, |
| 3801 | path, cache); |
Filipe Manana | 2bc0bb5 | 2015-12-30 02:42:30 +0000 | [diff] [blame] | 3802 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3803 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3804 | btrfs_abort_transaction(trans, ret); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3805 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3806 | |
| 3807 | /* if its not on the io list, we need to put the block group */ |
| 3808 | if (should_put) |
| 3809 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3810 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3811 | } |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame] | 3812 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3813 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3814 | while (!list_empty(io)) { |
| 3815 | cache = list_first_entry(io, struct btrfs_block_group_cache, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3816 | io_list); |
| 3817 | list_del_init(&cache->io_list); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 3818 | btrfs_wait_cache_io(trans, cache, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3819 | btrfs_put_block_group(cache); |
| 3820 | } |
| 3821 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3822 | btrfs_free_path(path); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3823 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3824 | } |
| 3825 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3826 | int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3827 | { |
| 3828 | struct btrfs_block_group_cache *block_group; |
| 3829 | int readonly = 0; |
| 3830 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3831 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3832 | if (!block_group || block_group->ro) |
| 3833 | readonly = 1; |
| 3834 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3835 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3836 | return readonly; |
| 3837 | } |
| 3838 | |
Filipe Manana | f78c436 | 2016-05-09 13:15:41 +0100 | [diff] [blame] | 3839 | bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 3840 | { |
| 3841 | struct btrfs_block_group_cache *bg; |
| 3842 | bool ret = true; |
| 3843 | |
| 3844 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 3845 | if (!bg) |
| 3846 | return false; |
| 3847 | |
| 3848 | spin_lock(&bg->lock); |
| 3849 | if (bg->ro) |
| 3850 | ret = false; |
| 3851 | else |
| 3852 | atomic_inc(&bg->nocow_writers); |
| 3853 | spin_unlock(&bg->lock); |
| 3854 | |
| 3855 | /* no put on block group, done by btrfs_dec_nocow_writers */ |
| 3856 | if (!ret) |
| 3857 | btrfs_put_block_group(bg); |
| 3858 | |
| 3859 | return ret; |
| 3860 | |
| 3861 | } |
| 3862 | |
| 3863 | void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
| 3864 | { |
| 3865 | struct btrfs_block_group_cache *bg; |
| 3866 | |
| 3867 | bg = btrfs_lookup_block_group(fs_info, bytenr); |
| 3868 | ASSERT(bg); |
| 3869 | if (atomic_dec_and_test(&bg->nocow_writers)) |
| 3870 | wake_up_atomic_t(&bg->nocow_writers); |
| 3871 | /* |
| 3872 | * Once for our lookup and once for the lookup done by a previous call |
| 3873 | * to btrfs_inc_nocow_writers() |
| 3874 | */ |
| 3875 | btrfs_put_block_group(bg); |
| 3876 | btrfs_put_block_group(bg); |
| 3877 | } |
| 3878 | |
| 3879 | static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a) |
| 3880 | { |
| 3881 | schedule(); |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
| 3885 | void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg) |
| 3886 | { |
| 3887 | wait_on_atomic_t(&bg->nocow_writers, |
| 3888 | btrfs_wait_nocow_writers_atomic_t, |
| 3889 | TASK_UNINTERRUPTIBLE); |
| 3890 | } |
| 3891 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3892 | static const char *alloc_name(u64 flags) |
| 3893 | { |
| 3894 | switch (flags) { |
| 3895 | case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA: |
| 3896 | return "mixed"; |
| 3897 | case BTRFS_BLOCK_GROUP_METADATA: |
| 3898 | return "metadata"; |
| 3899 | case BTRFS_BLOCK_GROUP_DATA: |
| 3900 | return "data"; |
| 3901 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 3902 | return "system"; |
| 3903 | default: |
| 3904 | WARN_ON(1); |
| 3905 | return "invalid-combination"; |
| 3906 | }; |
| 3907 | } |
| 3908 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3909 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3910 | u64 total_bytes, u64 bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3911 | u64 bytes_readonly, |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3912 | struct btrfs_space_info **space_info) |
| 3913 | { |
| 3914 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3915 | int i; |
| 3916 | int factor; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3917 | int ret; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3918 | |
| 3919 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3920 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3921 | factor = 2; |
| 3922 | else |
| 3923 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3924 | |
| 3925 | found = __find_space_info(info, flags); |
| 3926 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3927 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3928 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3929 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3930 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3931 | found->disk_used += bytes_used * factor; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3932 | found->bytes_readonly += bytes_readonly; |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 3933 | if (total_bytes > 0) |
| 3934 | found->full = 0; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 3935 | space_info_add_new_bytes(info, found, total_bytes - |
| 3936 | bytes_used - bytes_readonly); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3937 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3938 | *space_info = found; |
| 3939 | return 0; |
| 3940 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3941 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3942 | if (!found) |
| 3943 | return -ENOMEM; |
| 3944 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3945 | ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3946 | if (ret) { |
| 3947 | kfree(found); |
| 3948 | return ret; |
| 3949 | } |
| 3950 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 3951 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3952 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3953 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3954 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3955 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3956 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3957 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3958 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3959 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3960 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3961 | found->bytes_reserved = 0; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 3962 | found->bytes_readonly = bytes_readonly; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3963 | found->bytes_may_use = 0; |
Filipe Manana | 6af3e3a | 2015-09-07 10:41:12 +0100 | [diff] [blame] | 3964 | found->full = 0; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3965 | found->max_extent_size = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3966 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3967 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3968 | found->flush = 0; |
| 3969 | init_waitqueue_head(&found->wait); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 3970 | INIT_LIST_HEAD(&found->ro_bgs); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 3971 | INIT_LIST_HEAD(&found->tickets); |
| 3972 | INIT_LIST_HEAD(&found->priority_tickets); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3973 | |
| 3974 | ret = kobject_init_and_add(&found->kobj, &space_info_ktype, |
| 3975 | info->space_info_kobj, "%s", |
| 3976 | alloc_name(found->flags)); |
| 3977 | if (ret) { |
| 3978 | kfree(found); |
| 3979 | return ret; |
| 3980 | } |
| 3981 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3982 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3983 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3984 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3985 | info->data_sinfo = found; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3986 | |
| 3987 | return ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3988 | } |
| 3989 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3990 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3991 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3992 | u64 extra_flags = chunk_to_extended(flags) & |
| 3993 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3994 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3995 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3996 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3997 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3998 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3999 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 4000 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 4001 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4002 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4003 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4004 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4005 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4006 | * returns target flags in extended format or 0 if restripe for this |
| 4007 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 4008 | * |
| 4009 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4010 | */ |
| 4011 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 4012 | { |
| 4013 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 4014 | u64 target = 0; |
| 4015 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4016 | if (!bctl) |
| 4017 | return 0; |
| 4018 | |
| 4019 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 4020 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4021 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 4022 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 4023 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4024 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 4025 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 4026 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 4027 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 4028 | } |
| 4029 | |
| 4030 | return target; |
| 4031 | } |
| 4032 | |
| 4033 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4034 | * @flags: available profiles in extended format (see ctree.h) |
| 4035 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4036 | * Returns reduced profile in chunk format. If profile changing is in |
| 4037 | * progress (either running or paused) picks the target profile (if it's |
| 4038 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4039 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4040 | 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] | 4041 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4042 | u64 num_devices = fs_info->fs_devices->rw_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4043 | u64 target; |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4044 | u64 raid_type; |
| 4045 | u64 allowed = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4046 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4047 | /* |
| 4048 | * see if restripe for this chunk_type is in progress, if so |
| 4049 | * try to reduce to the target profile |
| 4050 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4051 | spin_lock(&fs_info->balance_lock); |
| 4052 | target = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4053 | if (target) { |
| 4054 | /* pick target profile only if it's already available */ |
| 4055 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4056 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4057 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4058 | } |
| 4059 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4060 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4061 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4062 | /* First, mask out the RAID levels which aren't possible */ |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4063 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
| 4064 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) |
| 4065 | allowed |= btrfs_raid_group[raid_type]; |
| 4066 | } |
| 4067 | allowed &= flags; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4068 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4069 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) |
| 4070 | allowed = BTRFS_BLOCK_GROUP_RAID6; |
| 4071 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) |
| 4072 | allowed = BTRFS_BLOCK_GROUP_RAID5; |
| 4073 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) |
| 4074 | allowed = BTRFS_BLOCK_GROUP_RAID10; |
| 4075 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) |
| 4076 | allowed = BTRFS_BLOCK_GROUP_RAID1; |
| 4077 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) |
| 4078 | allowed = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4079 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4080 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4081 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 4082 | return extended_to_chunk(flags | allowed); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4083 | } |
| 4084 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4085 | 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] | 4086 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4087 | unsigned seq; |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 4088 | u64 flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4089 | |
| 4090 | do { |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 4091 | flags = orig_flags; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4092 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4093 | |
| 4094 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4095 | flags |= fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4096 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4097 | flags |= fs_info->avail_system_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4098 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4099 | flags |= fs_info->avail_metadata_alloc_bits; |
| 4100 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4101 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4102 | return btrfs_reduce_alloc_profile(fs_info, flags); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4103 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4104 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 4105 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4106 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4107 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4108 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4109 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4110 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4111 | if (data) |
| 4112 | flags = BTRFS_BLOCK_GROUP_DATA; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4113 | else if (root == fs_info->chunk_root) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4114 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 4115 | else |
| 4116 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 4117 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4118 | ret = get_alloc_profile(fs_info, flags); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4119 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4120 | } |
| 4121 | |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4122 | int btrfs_alloc_data_chunk_ondemand(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4123 | { |
| 4124 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4125 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4126 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4127 | u64 used; |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4128 | int ret = 0; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4129 | int need_commit = 2; |
| 4130 | int have_pinned_space; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4131 | |
| 4132 | /* make sure bytes are sectorsize aligned */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4133 | bytes = ALIGN(bytes, fs_info->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4134 | |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4135 | if (btrfs_is_free_space_inode(inode)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4136 | need_commit = 0; |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4137 | ASSERT(current->journal_info); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4138 | } |
| 4139 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4140 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4141 | if (!data_sinfo) |
| 4142 | goto alloc; |
| 4143 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4144 | again: |
| 4145 | /* make sure we have enough space to handle the data first */ |
| 4146 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4147 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 4148 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 4149 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4150 | |
| 4151 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4152 | struct btrfs_trans_handle *trans; |
| 4153 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4154 | /* |
| 4155 | * if we don't have enough free bytes in this space then we need |
| 4156 | * to alloc a new chunk. |
| 4157 | */ |
Zhao Lei | b9fd47c | 2015-02-09 14:40:20 +0800 | [diff] [blame] | 4158 | if (!data_sinfo->full) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4159 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4160 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4161 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4162 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4163 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4164 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4165 | /* |
| 4166 | * It is ugly that we don't call nolock join |
| 4167 | * transaction for the free space inode case here. |
| 4168 | * But it is safe because we only do the data space |
| 4169 | * reservation for the free space cache in the |
| 4170 | * transaction context, the common join transaction |
| 4171 | * just increase the counter of the current transaction |
| 4172 | * handler, doesn't try to acquire the trans_lock of |
| 4173 | * the fs. |
| 4174 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4175 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4176 | if (IS_ERR(trans)) |
| 4177 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4178 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4179 | ret = do_chunk_alloc(trans, fs_info, alloc_target, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4180 | CHUNK_ALLOC_NO_FORCE); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4181 | btrfs_end_transaction(trans); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4182 | if (ret < 0) { |
| 4183 | if (ret != -ENOSPC) |
| 4184 | return ret; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4185 | else { |
| 4186 | have_pinned_space = 1; |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4187 | goto commit_trans; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4188 | } |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4189 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4190 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4191 | if (!data_sinfo) |
| 4192 | data_sinfo = fs_info->data_sinfo; |
| 4193 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4194 | goto again; |
| 4195 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4196 | |
| 4197 | /* |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4198 | * If we don't have enough pinned space to deal with this |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4199 | * allocation, and no removed chunk in current transaction, |
| 4200 | * don't bother committing the transaction. |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4201 | */ |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4202 | have_pinned_space = percpu_counter_compare( |
| 4203 | &data_sinfo->total_bytes_pinned, |
| 4204 | used + bytes - data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4205 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4206 | |
| 4207 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4208 | commit_trans: |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4209 | if (need_commit && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4210 | !atomic_read(&fs_info->open_ioctl_trans)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4211 | need_commit--; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4212 | |
Zhao Lei | e1746e8 | 2015-12-01 18:39:40 +0800 | [diff] [blame] | 4213 | if (need_commit > 0) { |
| 4214 | btrfs_start_delalloc_roots(fs_info, 0, -1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4215 | btrfs_wait_ordered_roots(fs_info, -1, 0, |
| 4216 | (u64)-1); |
Zhao Lei | e1746e8 | 2015-12-01 18:39:40 +0800 | [diff] [blame] | 4217 | } |
Zhao Lei | 9a4e727 | 2015-04-09 12:34:43 +0800 | [diff] [blame] | 4218 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4219 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4220 | if (IS_ERR(trans)) |
| 4221 | return PTR_ERR(trans); |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4222 | if (have_pinned_space >= 0 || |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 4223 | test_bit(BTRFS_TRANS_HAVE_FREE_BGS, |
| 4224 | &trans->transaction->flags) || |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4225 | need_commit > 0) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4226 | ret = btrfs_commit_transaction(trans); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4227 | if (ret) |
| 4228 | return ret; |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4229 | /* |
Filipe Manana | c2d6cb1 | 2016-01-15 11:05:12 +0000 | [diff] [blame] | 4230 | * The cleaner kthread might still be doing iput |
| 4231 | * operations. Wait for it to finish so that |
| 4232 | * more space is released. |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4233 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4234 | mutex_lock(&fs_info->cleaner_delayed_iput_mutex); |
| 4235 | mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4236 | goto again; |
| 4237 | } else { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4238 | btrfs_end_transaction(trans); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4239 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4240 | } |
| 4241 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4242 | trace_btrfs_space_reservation(fs_info, |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 4243 | "space_info:enospc", |
| 4244 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4245 | return -ENOSPC; |
| 4246 | } |
| 4247 | data_sinfo->bytes_may_use += bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4248 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4249 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4250 | spin_unlock(&data_sinfo->lock); |
| 4251 | |
Dongsheng Yang | 237c0e9 | 2014-12-29 06:23:05 -0500 | [diff] [blame] | 4252 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4256 | * New check_data_free_space() with ability for precious data reservation |
| 4257 | * Will replace old btrfs_check_data_free_space(), but for patch split, |
| 4258 | * add a new function first and then replace it. |
| 4259 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4260 | 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] | 4261 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4262 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4263 | int ret; |
| 4264 | |
| 4265 | /* align the range */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4266 | len = round_up(start + len, fs_info->sectorsize) - |
| 4267 | round_down(start, fs_info->sectorsize); |
| 4268 | start = round_down(start, fs_info->sectorsize); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4269 | |
| 4270 | ret = btrfs_alloc_data_chunk_ondemand(inode, len); |
| 4271 | if (ret < 0) |
| 4272 | return ret; |
| 4273 | |
Josef Bacik | 1e5ec2e | 2016-09-15 14:57:48 -0400 | [diff] [blame] | 4274 | /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */ |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4275 | ret = btrfs_qgroup_reserve_data(inode, start, len); |
Josef Bacik | 1e5ec2e | 2016-09-15 14:57:48 -0400 | [diff] [blame] | 4276 | if (ret) |
| 4277 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4278 | return ret; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4282 | * Called if we need to clear a data reservation for this inode |
| 4283 | * Normally in a error case. |
| 4284 | * |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4285 | * This one will *NOT* use accurate qgroup reserved space API, just for case |
| 4286 | * which we can't sleep and is sure it won't affect qgroup reserved space. |
| 4287 | * Like clear_bit_hook(). |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4288 | */ |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4289 | void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, |
| 4290 | u64 len) |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4291 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4292 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4293 | struct btrfs_space_info *data_sinfo; |
| 4294 | |
| 4295 | /* Make sure the range is aligned to sectorsize */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4296 | len = round_up(start + len, fs_info->sectorsize) - |
| 4297 | round_down(start, fs_info->sectorsize); |
| 4298 | start = round_down(start, fs_info->sectorsize); |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4299 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4300 | data_sinfo = fs_info->data_sinfo; |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4301 | spin_lock(&data_sinfo->lock); |
| 4302 | if (WARN_ON(data_sinfo->bytes_may_use < len)) |
| 4303 | data_sinfo->bytes_may_use = 0; |
| 4304 | else |
| 4305 | data_sinfo->bytes_may_use -= len; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4306 | trace_btrfs_space_reservation(fs_info, "space_info", |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4307 | data_sinfo->flags, len, 0); |
| 4308 | spin_unlock(&data_sinfo->lock); |
| 4309 | } |
| 4310 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4311 | /* |
| 4312 | * Called if we need to clear a data reservation for this inode |
| 4313 | * Normally in a error case. |
| 4314 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4315 | * 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] | 4316 | * space framework. |
| 4317 | */ |
| 4318 | void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len) |
| 4319 | { |
Jeff Mahoney | 0c476a5 | 2016-11-18 21:52:40 -0500 | [diff] [blame] | 4320 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4321 | |
| 4322 | /* Make sure the range is aligned to sectorsize */ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4323 | len = round_up(start + len, root->fs_info->sectorsize) - |
| 4324 | round_down(start, root->fs_info->sectorsize); |
| 4325 | start = round_down(start, root->fs_info->sectorsize); |
Jeff Mahoney | 0c476a5 | 2016-11-18 21:52:40 -0500 | [diff] [blame] | 4326 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4327 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
| 4328 | btrfs_qgroup_free_data(inode, start, len); |
| 4329 | } |
| 4330 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4331 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 4332 | { |
| 4333 | struct list_head *head = &info->space_info; |
| 4334 | struct btrfs_space_info *found; |
| 4335 | |
| 4336 | rcu_read_lock(); |
| 4337 | list_for_each_entry_rcu(found, head, list) { |
| 4338 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4339 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4340 | } |
| 4341 | rcu_read_unlock(); |
| 4342 | } |
| 4343 | |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4344 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
| 4345 | { |
| 4346 | return (global->size << 1); |
| 4347 | } |
| 4348 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4349 | static int should_alloc_chunk(struct btrfs_fs_info *fs_info, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4350 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4351 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4352 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4353 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4354 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 4355 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4356 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4357 | if (force == CHUNK_ALLOC_FORCE) |
| 4358 | return 1; |
| 4359 | |
| 4360 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4361 | * We need to take into account the global rsv because for all intents |
| 4362 | * and purposes it's used space. Don't worry about locking the |
| 4363 | * global_rsv, it doesn't change except when the transaction commits. |
| 4364 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 4365 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4366 | num_allocated += calc_global_rsv_need_space(global_rsv); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4367 | |
| 4368 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4369 | * in limited mode, we want to have some free space up to |
| 4370 | * about 1% of the FS size. |
| 4371 | */ |
| 4372 | if (force == CHUNK_ALLOC_LIMITED) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4373 | thresh = btrfs_super_total_bytes(fs_info->super_copy); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4374 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4375 | |
| 4376 | if (num_bytes - num_allocated < thresh) |
| 4377 | return 1; |
| 4378 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4379 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4380 | if (num_allocated + SZ_2M < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 4381 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4382 | return 1; |
| 4383 | } |
| 4384 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4385 | 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] | 4386 | { |
| 4387 | u64 num_dev; |
| 4388 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4389 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
| 4390 | BTRFS_BLOCK_GROUP_RAID0 | |
| 4391 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4392 | BTRFS_BLOCK_GROUP_RAID6)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4393 | num_dev = fs_info->fs_devices->rw_devices; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4394 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 4395 | num_dev = 2; |
| 4396 | else |
| 4397 | num_dev = 1; /* DUP or single */ |
| 4398 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4399 | return num_dev; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4400 | } |
| 4401 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4402 | /* |
| 4403 | * If @is_allocation is true, reserve space in the system space info necessary |
| 4404 | * for allocating a chunk, otherwise if it's false, reserve space necessary for |
| 4405 | * removing a chunk. |
| 4406 | */ |
| 4407 | void check_system_chunk(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4408 | struct btrfs_fs_info *fs_info, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4409 | { |
| 4410 | struct btrfs_space_info *info; |
| 4411 | u64 left; |
| 4412 | u64 thresh; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4413 | int ret = 0; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4414 | u64 num_devs; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4415 | |
| 4416 | /* |
| 4417 | * Needed because we can end up allocating a system chunk and for an |
| 4418 | * atomic and race free space reservation in the chunk block reserve. |
| 4419 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4420 | ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4421 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4422 | info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4423 | spin_lock(&info->lock); |
| 4424 | left = info->total_bytes - info->bytes_used - info->bytes_pinned - |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4425 | info->bytes_reserved - info->bytes_readonly - |
| 4426 | info->bytes_may_use; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4427 | spin_unlock(&info->lock); |
| 4428 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4429 | num_devs = get_profile_num_devs(fs_info, type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* 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] | 4432 | thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) + |
| 4433 | btrfs_calc_trans_metadata_size(fs_info, 1); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4434 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4435 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
| 4436 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", |
| 4437 | left, thresh, type); |
| 4438 | dump_space_info(fs_info, info, 0, 0); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | if (left < thresh) { |
| 4442 | u64 flags; |
| 4443 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4444 | flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4445 | /* |
| 4446 | * Ignore failure to create system chunk. We might end up not |
| 4447 | * needing it, as we might not need to COW all nodes/leafs from |
| 4448 | * the paths we visit in the chunk tree (they were already COWed |
| 4449 | * or created in the current transaction for example). |
| 4450 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4451 | ret = btrfs_alloc_chunk(trans, fs_info, flags); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | if (!ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4455 | ret = btrfs_block_rsv_add(fs_info->chunk_root, |
| 4456 | &fs_info->chunk_block_rsv, |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4457 | thresh, BTRFS_RESERVE_NO_FLUSH); |
| 4458 | if (!ret) |
| 4459 | trans->chunk_bytes_reserved += thresh; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4460 | } |
| 4461 | } |
| 4462 | |
Liu Bo | 28b737f | 2016-07-29 11:09:50 -0700 | [diff] [blame] | 4463 | /* |
| 4464 | * If force is CHUNK_ALLOC_FORCE: |
| 4465 | * - return 1 if it successfully allocates a chunk, |
| 4466 | * - return errors including -ENOSPC otherwise. |
| 4467 | * If force is NOT CHUNK_ALLOC_FORCE: |
| 4468 | * - return 0 if it doesn't need to allocate a new chunk, |
| 4469 | * - return 1 if it successfully allocates a chunk, |
| 4470 | * - return errors including -ENOSPC otherwise. |
| 4471 | */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4472 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4473 | struct btrfs_fs_info *fs_info, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4474 | { |
| 4475 | struct btrfs_space_info *space_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4476 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4477 | int ret = 0; |
| 4478 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4479 | /* Don't re-enter if we're already allocating a chunk */ |
| 4480 | if (trans->allocating_chunk) |
| 4481 | return -ENOSPC; |
| 4482 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4483 | space_info = __find_space_info(fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4484 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4485 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4486 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4487 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4488 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4489 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4490 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4491 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4492 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4493 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4494 | if (space_info->full) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4495 | if (should_alloc_chunk(fs_info, space_info, force)) |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4496 | ret = -ENOSPC; |
| 4497 | else |
| 4498 | ret = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4499 | spin_unlock(&space_info->lock); |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4500 | return ret; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4501 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4502 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4503 | if (!should_alloc_chunk(fs_info, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4504 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4505 | return 0; |
| 4506 | } else if (space_info->chunk_alloc) { |
| 4507 | wait_for_alloc = 1; |
| 4508 | } else { |
| 4509 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4510 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4511 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4512 | spin_unlock(&space_info->lock); |
| 4513 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4514 | mutex_lock(&fs_info->chunk_mutex); |
| 4515 | |
| 4516 | /* |
| 4517 | * The chunk_mutex is held throughout the entirety of a chunk |
| 4518 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 4519 | * other guy is done and we need to recheck and see if we should |
| 4520 | * allocate. |
| 4521 | */ |
| 4522 | if (wait_for_alloc) { |
| 4523 | mutex_unlock(&fs_info->chunk_mutex); |
| 4524 | wait_for_alloc = 0; |
| 4525 | goto again; |
| 4526 | } |
| 4527 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4528 | trans->allocating_chunk = true; |
| 4529 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4530 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4531 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 4532 | * allocating mixed chunks instead of individual chunks. |
| 4533 | */ |
| 4534 | if (btrfs_mixed_space_info(space_info)) |
| 4535 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 4536 | |
| 4537 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4538 | * if we're doing a data chunk, go ahead and make sure that |
| 4539 | * we keep a reasonable number of metadata chunks allocated in the |
| 4540 | * FS as well. |
| 4541 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4542 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4543 | fs_info->data_chunk_allocations++; |
| 4544 | if (!(fs_info->data_chunk_allocations % |
| 4545 | fs_info->metadata_ratio)) |
| 4546 | force_metadata_allocation(fs_info); |
| 4547 | } |
| 4548 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4549 | /* |
| 4550 | * Check if we have enough space in SYSTEM chunk because we may need |
| 4551 | * to update devices. |
| 4552 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4553 | check_system_chunk(trans, fs_info, flags); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4554 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4555 | ret = btrfs_alloc_chunk(trans, fs_info, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4556 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 4557 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4558 | spin_lock(&space_info->lock); |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4559 | if (ret < 0 && ret != -ENOSPC) |
| 4560 | goto out; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4561 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4562 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4563 | else |
| 4564 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4565 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4566 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4567 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4568 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4569 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 4570 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4571 | /* |
| 4572 | * When we allocate a new chunk we reserve space in the chunk block |
| 4573 | * reserve to make sure we can COW nodes/leafs in the chunk tree or |
| 4574 | * add new nodes/leafs to it if we end up needing to do it when |
| 4575 | * inserting the chunk item and updating device items as part of the |
| 4576 | * second phase of chunk allocation, performed by |
| 4577 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a |
| 4578 | * large number of new block groups to create in our transaction |
| 4579 | * handle's new_bgs list to avoid exhausting the chunk block reserve |
| 4580 | * in extreme cases - like having a single transaction create many new |
| 4581 | * block groups when starting to write out the free space caches of all |
| 4582 | * the block groups that were made dirty during the lifetime of the |
| 4583 | * transaction. |
| 4584 | */ |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 4585 | if (trans->can_flush_pending_bgs && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4586 | trans->chunk_bytes_reserved >= (u64)SZ_2M) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4587 | btrfs_create_pending_block_groups(trans, fs_info); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4588 | btrfs_trans_release_chunk_metadata(trans); |
| 4589 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4590 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4591 | } |
| 4592 | |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4593 | static int can_overcommit(struct btrfs_root *root, |
| 4594 | struct btrfs_space_info *space_info, u64 bytes, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4595 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4596 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4597 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4598 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4599 | u64 profile; |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4600 | u64 space_size; |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4601 | u64 avail; |
| 4602 | u64 used; |
| 4603 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4604 | /* Don't overcommit when in mixed mode. */ |
| 4605 | if (space_info->flags & BTRFS_BLOCK_GROUP_DATA) |
| 4606 | return 0; |
| 4607 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4608 | profile = btrfs_get_alloc_profile(root, 0); |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4609 | used = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4610 | space_info->bytes_pinned + space_info->bytes_readonly; |
| 4611 | |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4612 | /* |
| 4613 | * We only want to allow over committing if we have lots of actual space |
| 4614 | * free, but if we don't have enough space to handle the global reserve |
| 4615 | * space then we could end up having a real enospc problem when trying |
| 4616 | * to allocate a chunk or some other such important allocation. |
| 4617 | */ |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4618 | spin_lock(&global_rsv->lock); |
| 4619 | space_size = calc_global_rsv_need_space(global_rsv); |
| 4620 | spin_unlock(&global_rsv->lock); |
| 4621 | if (used + space_size >= space_info->total_bytes) |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4622 | return 0; |
| 4623 | |
| 4624 | used += space_info->bytes_may_use; |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4625 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4626 | spin_lock(&fs_info->free_chunk_lock); |
| 4627 | avail = fs_info->free_chunk_space; |
| 4628 | spin_unlock(&fs_info->free_chunk_lock); |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4629 | |
| 4630 | /* |
| 4631 | * 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] | 4632 | * space is actually useable. For raid56, the space info used |
| 4633 | * doesn't include the parity drive, so we don't have to |
| 4634 | * change the math |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4635 | */ |
| 4636 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 4637 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4638 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4639 | avail >>= 1; |
| 4640 | |
| 4641 | /* |
Miao Xie | 561c294 | 2012-10-16 11:32:18 +0000 | [diff] [blame] | 4642 | * If we aren't flushing all things, let us overcommit up to |
| 4643 | * 1/2th of the space. If we can flush, don't let us overcommit |
| 4644 | * too much, let it overcommit up to 1/8 of the space. |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4645 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4646 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4647 | avail >>= 3; |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4648 | else |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4649 | avail >>= 1; |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4650 | |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4651 | if (used + bytes < space_info->total_bytes + avail) |
Josef Bacik | a80c8dcf | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4652 | return 1; |
| 4653 | return 0; |
| 4654 | } |
| 4655 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4656 | 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] | 4657 | unsigned long nr_pages, int nr_items) |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4658 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4659 | struct super_block *sb = fs_info->sb; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4660 | |
Josef Bacik | 925a6ef | 2013-06-20 12:31:27 -0400 | [diff] [blame] | 4661 | if (down_read_trylock(&sb->s_umount)) { |
| 4662 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); |
| 4663 | up_read(&sb->s_umount); |
| 4664 | } else { |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4665 | /* |
| 4666 | * We needn't worry the filesystem going from r/w to r/o though |
| 4667 | * we don't acquire ->s_umount mutex, because the filesystem |
| 4668 | * should guarantee the delalloc inodes list be empty after |
| 4669 | * the filesystem is readonly(all dirty pages are written to |
| 4670 | * the disk). |
| 4671 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4672 | btrfs_start_delalloc_roots(fs_info, 0, nr_items); |
Josef Bacik | 98ad69c | 2013-04-04 11:55:49 -0400 | [diff] [blame] | 4673 | if (!current->journal_info) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4674 | btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1); |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4675 | } |
| 4676 | } |
| 4677 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4678 | static inline int calc_reclaim_items_nr(struct btrfs_fs_info *fs_info, |
| 4679 | u64 to_reclaim) |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4680 | { |
| 4681 | u64 bytes; |
| 4682 | int nr; |
| 4683 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4684 | bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4685 | nr = (int)div64_u64(to_reclaim, bytes); |
| 4686 | if (!nr) |
| 4687 | nr = 1; |
| 4688 | return nr; |
| 4689 | } |
| 4690 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4691 | #define EXTENT_SIZE_PER_ITEM SZ_256K |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4692 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4693 | /* |
| 4694 | * shrink metadata reservation for delalloc |
| 4695 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4696 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 4697 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4698 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4699 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4700 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4701 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4702 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4703 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4704 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 4705 | long time_left; |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4706 | unsigned long nr_pages; |
| 4707 | int loops; |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4708 | int items; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4709 | enum btrfs_reserve_flush_enum flush; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4710 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4711 | /* Calc the number of the pages we need flush for space reservation */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4712 | items = calc_reclaim_items_nr(fs_info, to_reclaim); |
Adam Borowski | 8eb0dfd | 2016-05-08 15:08:00 +0200 | [diff] [blame] | 4713 | to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM; |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4714 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4715 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4716 | block_rsv = &fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4717 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 4718 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4719 | delalloc_bytes = percpu_counter_sum_positive( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4720 | &fs_info->delalloc_bytes); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4721 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4722 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4723 | return; |
Miao Xie | 38c135a | 2013-11-04 23:13:21 +0800 | [diff] [blame] | 4724 | if (wait_ordered) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4725 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4726 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4727 | } |
| 4728 | |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4729 | loops = 0; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4730 | while (delalloc_bytes && loops < 3) { |
| 4731 | max_reclaim = min(delalloc_bytes, to_reclaim); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4732 | nr_pages = max_reclaim >> PAGE_SHIFT; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4733 | btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items); |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4734 | /* |
| 4735 | * We need to wait for the async pages to actually start before |
| 4736 | * we do anything. |
| 4737 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4738 | max_reclaim = atomic_read(&fs_info->async_delalloc_pages); |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4739 | if (!max_reclaim) |
| 4740 | goto skip_async; |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4741 | |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4742 | if (max_reclaim <= nr_pages) |
| 4743 | max_reclaim = 0; |
| 4744 | else |
| 4745 | max_reclaim -= nr_pages; |
| 4746 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4747 | wait_event(fs_info->async_submit_wait, |
| 4748 | atomic_read(&fs_info->async_delalloc_pages) <= |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4749 | (int)max_reclaim); |
| 4750 | skip_async: |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4751 | if (!trans) |
| 4752 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 4753 | else |
| 4754 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4755 | spin_lock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4756 | if (can_overcommit(root, space_info, orig, flush)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4757 | spin_unlock(&space_info->lock); |
| 4758 | break; |
| 4759 | } |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4760 | if (list_empty(&space_info->tickets) && |
| 4761 | list_empty(&space_info->priority_tickets)) { |
| 4762 | spin_unlock(&space_info->lock); |
| 4763 | break; |
| 4764 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4765 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4766 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4767 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4768 | if (wait_ordered && !trans) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4769 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4770 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4771 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4772 | if (time_left) |
| 4773 | break; |
| 4774 | } |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4775 | delalloc_bytes = percpu_counter_sum_positive( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4776 | &fs_info->delalloc_bytes); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4777 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4778 | } |
| 4779 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4780 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4781 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 4782 | * @root - the root we're allocating for |
| 4783 | * @bytes - the number of bytes we want to reserve |
| 4784 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4785 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4786 | * This will check to make sure that committing the transaction will actually |
| 4787 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 4788 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4789 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4790 | static int may_commit_transaction(struct btrfs_root *root, |
| 4791 | struct btrfs_space_info *space_info, |
| 4792 | u64 bytes, int force) |
| 4793 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4794 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4795 | struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4796 | struct btrfs_trans_handle *trans; |
| 4797 | |
| 4798 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 4799 | if (trans) |
| 4800 | return -EAGAIN; |
| 4801 | |
| 4802 | if (force) |
| 4803 | goto commit; |
| 4804 | |
| 4805 | /* See if there is enough pinned space to make this reservation */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4806 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
Miao Xie | 0424c54 | 2014-03-06 13:54:59 +0800 | [diff] [blame] | 4807 | bytes) >= 0) |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4808 | goto commit; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4809 | |
| 4810 | /* |
| 4811 | * See if there is some space in the delayed insertion reservation for |
| 4812 | * this reservation. |
| 4813 | */ |
| 4814 | if (space_info != delayed_rsv->space_info) |
| 4815 | return -ENOSPC; |
| 4816 | |
| 4817 | spin_lock(&delayed_rsv->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4818 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4819 | bytes - delayed_rsv->size) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4820 | spin_unlock(&delayed_rsv->lock); |
| 4821 | return -ENOSPC; |
| 4822 | } |
| 4823 | spin_unlock(&delayed_rsv->lock); |
| 4824 | |
| 4825 | commit: |
| 4826 | trans = btrfs_join_transaction(root); |
| 4827 | if (IS_ERR(trans)) |
| 4828 | return -ENOSPC; |
| 4829 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4830 | return btrfs_commit_transaction(trans); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4831 | } |
| 4832 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4833 | struct reserve_ticket { |
| 4834 | u64 bytes; |
| 4835 | int error; |
| 4836 | struct list_head list; |
| 4837 | wait_queue_head_t wait; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4838 | }; |
| 4839 | |
| 4840 | static int flush_space(struct btrfs_root *root, |
| 4841 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 4842 | u64 orig_bytes, int state) |
| 4843 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4844 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4845 | struct btrfs_trans_handle *trans; |
| 4846 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4847 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4848 | |
| 4849 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4850 | case FLUSH_DELAYED_ITEMS_NR: |
| 4851 | case FLUSH_DELAYED_ITEMS: |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4852 | if (state == FLUSH_DELAYED_ITEMS_NR) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4853 | nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4854 | else |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4855 | nr = -1; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4856 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4857 | trans = btrfs_join_transaction(root); |
| 4858 | if (IS_ERR(trans)) { |
| 4859 | ret = PTR_ERR(trans); |
| 4860 | break; |
| 4861 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4862 | ret = btrfs_run_delayed_items_nr(trans, fs_info, nr); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4863 | btrfs_end_transaction(trans); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4864 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4865 | case FLUSH_DELALLOC: |
| 4866 | case FLUSH_DELALLOC_WAIT: |
Miao Xie | 24af7dd | 2014-03-06 13:55:00 +0800 | [diff] [blame] | 4867 | shrink_delalloc(root, num_bytes * 2, orig_bytes, |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4868 | state == FLUSH_DELALLOC_WAIT); |
| 4869 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4870 | case ALLOC_CHUNK: |
| 4871 | trans = btrfs_join_transaction(root); |
| 4872 | if (IS_ERR(trans)) { |
| 4873 | ret = PTR_ERR(trans); |
| 4874 | break; |
| 4875 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4876 | ret = do_chunk_alloc(trans, fs_info, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4877 | btrfs_get_alloc_profile(root, 0), |
| 4878 | CHUNK_ALLOC_NO_FORCE); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4879 | btrfs_end_transaction(trans); |
Alex Lyakas | eecba89 | 2015-12-06 12:32:31 +0200 | [diff] [blame] | 4880 | if (ret > 0 || ret == -ENOSPC) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4881 | ret = 0; |
| 4882 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4883 | case COMMIT_TRANS: |
| 4884 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
| 4885 | break; |
| 4886 | default: |
| 4887 | ret = -ENOSPC; |
| 4888 | break; |
| 4889 | } |
| 4890 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4891 | trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 4892 | orig_bytes, state, ret); |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4893 | return ret; |
| 4894 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4895 | |
| 4896 | static inline u64 |
| 4897 | btrfs_calc_reclaim_metadata_size(struct btrfs_root *root, |
| 4898 | struct btrfs_space_info *space_info) |
| 4899 | { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4900 | struct reserve_ticket *ticket; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4901 | u64 used; |
| 4902 | u64 expected; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4903 | u64 to_reclaim = 0; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4904 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4905 | list_for_each_entry(ticket, &space_info->tickets, list) |
| 4906 | to_reclaim += ticket->bytes; |
| 4907 | list_for_each_entry(ticket, &space_info->priority_tickets, list) |
| 4908 | to_reclaim += ticket->bytes; |
| 4909 | if (to_reclaim) |
| 4910 | return to_reclaim; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4911 | |
Wang Xiaoguang | e0af248 | 2016-08-31 19:46:16 +0800 | [diff] [blame] | 4912 | to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M); |
| 4913 | if (can_overcommit(root, space_info, to_reclaim, |
| 4914 | BTRFS_RESERVE_FLUSH_ALL)) |
| 4915 | return 0; |
| 4916 | |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4917 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4918 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4919 | space_info->bytes_may_use; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4920 | if (can_overcommit(root, space_info, SZ_1M, BTRFS_RESERVE_FLUSH_ALL)) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4921 | expected = div_factor_fine(space_info->total_bytes, 95); |
| 4922 | else |
| 4923 | expected = div_factor_fine(space_info->total_bytes, 90); |
| 4924 | |
| 4925 | if (used > expected) |
| 4926 | to_reclaim = used - expected; |
| 4927 | else |
| 4928 | to_reclaim = 0; |
| 4929 | to_reclaim = min(to_reclaim, space_info->bytes_may_use + |
| 4930 | space_info->bytes_reserved); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4931 | return to_reclaim; |
| 4932 | } |
| 4933 | |
| 4934 | 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] | 4935 | struct btrfs_root *root, u64 used) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4936 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4937 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4938 | u64 thresh = div_factor_fine(space_info->total_bytes, 98); |
| 4939 | |
| 4940 | /* 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] | 4941 | if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh) |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4942 | return 0; |
| 4943 | |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 4944 | if (!btrfs_calc_reclaim_metadata_size(root, space_info)) |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4945 | return 0; |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4946 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4947 | return (used >= thresh && !btrfs_fs_closing(fs_info) && |
| 4948 | !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4949 | } |
| 4950 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4951 | static void wake_all_tickets(struct list_head *head) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4952 | { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4953 | struct reserve_ticket *ticket; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4954 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4955 | while (!list_empty(head)) { |
| 4956 | ticket = list_first_entry(head, struct reserve_ticket, list); |
| 4957 | list_del_init(&ticket->list); |
| 4958 | ticket->error = -ENOSPC; |
| 4959 | wake_up(&ticket->wait); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4960 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4961 | } |
| 4962 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4963 | /* |
| 4964 | * This is for normal flushers, we can wait all goddamned day if we want to. We |
| 4965 | * will loop and continuously try to flush as long as we are making progress. |
| 4966 | * We count progress as clearing off tickets each time we have to loop. |
| 4967 | */ |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4968 | static void btrfs_async_reclaim_metadata_space(struct work_struct *work) |
| 4969 | { |
| 4970 | struct btrfs_fs_info *fs_info; |
| 4971 | struct btrfs_space_info *space_info; |
| 4972 | u64 to_reclaim; |
| 4973 | int flush_state; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4974 | int commit_cycles = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 4975 | u64 last_tickets_id; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4976 | |
| 4977 | fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); |
| 4978 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4979 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4980 | spin_lock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4981 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 4982 | space_info); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4983 | if (!to_reclaim) { |
| 4984 | space_info->flush = 0; |
| 4985 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4986 | return; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4987 | } |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 4988 | last_tickets_id = space_info->tickets_id; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4989 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4990 | |
| 4991 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 4992 | do { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4993 | struct reserve_ticket *ticket; |
| 4994 | int ret; |
| 4995 | |
| 4996 | ret = flush_space(fs_info->fs_root, space_info, to_reclaim, |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4997 | to_reclaim, flush_state); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 4998 | spin_lock(&space_info->lock); |
| 4999 | if (list_empty(&space_info->tickets)) { |
| 5000 | space_info->flush = 0; |
| 5001 | spin_unlock(&space_info->lock); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5002 | return; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5003 | } |
| 5004 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 5005 | space_info); |
| 5006 | ticket = list_first_entry(&space_info->tickets, |
| 5007 | struct reserve_ticket, list); |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5008 | if (last_tickets_id == space_info->tickets_id) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5009 | flush_state++; |
| 5010 | } else { |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5011 | last_tickets_id = space_info->tickets_id; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5012 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5013 | if (commit_cycles) |
| 5014 | commit_cycles--; |
| 5015 | } |
| 5016 | |
| 5017 | if (flush_state > COMMIT_TRANS) { |
| 5018 | commit_cycles++; |
| 5019 | if (commit_cycles > 2) { |
| 5020 | wake_all_tickets(&space_info->tickets); |
| 5021 | space_info->flush = 0; |
| 5022 | } else { |
| 5023 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5024 | } |
| 5025 | } |
| 5026 | spin_unlock(&space_info->lock); |
| 5027 | } while (flush_state <= COMMIT_TRANS); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | void btrfs_init_async_reclaim_work(struct work_struct *work) |
| 5031 | { |
| 5032 | INIT_WORK(work, btrfs_async_reclaim_metadata_space); |
| 5033 | } |
| 5034 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5035 | static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, |
| 5036 | struct btrfs_space_info *space_info, |
| 5037 | struct reserve_ticket *ticket) |
| 5038 | { |
| 5039 | u64 to_reclaim; |
| 5040 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 5041 | |
| 5042 | spin_lock(&space_info->lock); |
| 5043 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 5044 | space_info); |
| 5045 | if (!to_reclaim) { |
| 5046 | spin_unlock(&space_info->lock); |
| 5047 | return; |
| 5048 | } |
| 5049 | spin_unlock(&space_info->lock); |
| 5050 | |
| 5051 | do { |
| 5052 | flush_space(fs_info->fs_root, space_info, to_reclaim, |
| 5053 | to_reclaim, flush_state); |
| 5054 | flush_state++; |
| 5055 | spin_lock(&space_info->lock); |
| 5056 | if (ticket->bytes == 0) { |
| 5057 | spin_unlock(&space_info->lock); |
| 5058 | return; |
| 5059 | } |
| 5060 | spin_unlock(&space_info->lock); |
| 5061 | |
| 5062 | /* |
| 5063 | * Priority flushers can't wait on delalloc without |
| 5064 | * deadlocking. |
| 5065 | */ |
| 5066 | if (flush_state == FLUSH_DELALLOC || |
| 5067 | flush_state == FLUSH_DELALLOC_WAIT) |
| 5068 | flush_state = ALLOC_CHUNK; |
| 5069 | } while (flush_state < COMMIT_TRANS); |
| 5070 | } |
| 5071 | |
| 5072 | static int wait_reserve_ticket(struct btrfs_fs_info *fs_info, |
| 5073 | struct btrfs_space_info *space_info, |
| 5074 | struct reserve_ticket *ticket, u64 orig_bytes) |
| 5075 | |
| 5076 | { |
| 5077 | DEFINE_WAIT(wait); |
| 5078 | int ret = 0; |
| 5079 | |
| 5080 | spin_lock(&space_info->lock); |
| 5081 | while (ticket->bytes > 0 && ticket->error == 0) { |
| 5082 | ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE); |
| 5083 | if (ret) { |
| 5084 | ret = -EINTR; |
| 5085 | break; |
| 5086 | } |
| 5087 | spin_unlock(&space_info->lock); |
| 5088 | |
| 5089 | schedule(); |
| 5090 | |
| 5091 | finish_wait(&ticket->wait, &wait); |
| 5092 | spin_lock(&space_info->lock); |
| 5093 | } |
| 5094 | if (!ret) |
| 5095 | ret = ticket->error; |
| 5096 | if (!list_empty(&ticket->list)) |
| 5097 | list_del_init(&ticket->list); |
| 5098 | if (ticket->bytes && ticket->bytes < orig_bytes) { |
| 5099 | u64 num_bytes = orig_bytes - ticket->bytes; |
| 5100 | space_info->bytes_may_use -= num_bytes; |
| 5101 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5102 | space_info->flags, num_bytes, 0); |
| 5103 | } |
| 5104 | spin_unlock(&space_info->lock); |
| 5105 | |
| 5106 | return ret; |
| 5107 | } |
| 5108 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5109 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5110 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 5111 | * @root - the root we're allocating for |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5112 | * @space_info - the space info we want to allocate from |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5113 | * @orig_bytes - the number of bytes we want |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 5114 | * @flush - whether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5115 | * |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5116 | * 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] | 5117 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 5118 | * flush out space to make room. It will do this by flushing delalloc if |
| 5119 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 5120 | * regain reservations will be made and this will fail if there is not enough |
| 5121 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5122 | */ |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5123 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
| 5124 | struct btrfs_space_info *space_info, |
| 5125 | u64 orig_bytes, |
| 5126 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5127 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5128 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5129 | struct reserve_ticket ticket; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5130 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5131 | int ret = 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5132 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5133 | ASSERT(orig_bytes); |
Josef Bacik | 8ca17f0 | 2016-05-27 13:24:13 -0400 | [diff] [blame] | 5134 | ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL); |
| 5135 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5136 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 5137 | ret = -ENOSPC; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5138 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 5139 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 5140 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5141 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5142 | /* |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5143 | * If we have enough space then hooray, make our reservation and carry |
| 5144 | * on. If not see if we can overcommit, and if we can, hooray carry on. |
| 5145 | * If not things get more complicated. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5146 | */ |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5147 | if (used + orig_bytes <= space_info->total_bytes) { |
| 5148 | space_info->bytes_may_use += orig_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5149 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5150 | space_info->flags, orig_bytes, 1); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5151 | ret = 0; |
| 5152 | } else if (can_overcommit(root, space_info, orig_bytes, flush)) { |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 5153 | space_info->bytes_may_use += orig_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5154 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5155 | space_info->flags, orig_bytes, 1); |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 5156 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 5157 | } |
| 5158 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5159 | /* |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5160 | * If we couldn't make a reservation then setup our reservation ticket |
| 5161 | * and kick the async worker if it's not already running. |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5162 | * |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5163 | * If we are a priority flusher then we just need to add our ticket to |
| 5164 | * the list and we will do our own flushing further down. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5165 | */ |
Josef Bacik | 72bcd99 | 2012-12-18 15:16:34 -0500 | [diff] [blame] | 5166 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5167 | ticket.bytes = orig_bytes; |
| 5168 | ticket.error = 0; |
| 5169 | init_waitqueue_head(&ticket.wait); |
| 5170 | if (flush == BTRFS_RESERVE_FLUSH_ALL) { |
| 5171 | list_add_tail(&ticket.list, &space_info->tickets); |
| 5172 | if (!space_info->flush) { |
| 5173 | space_info->flush = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5174 | trace_btrfs_trigger_flush(fs_info, |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 5175 | space_info->flags, |
| 5176 | orig_bytes, flush, |
| 5177 | "enospc"); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5178 | queue_work(system_unbound_wq, |
| 5179 | &root->fs_info->async_reclaim_work); |
| 5180 | } |
| 5181 | } else { |
| 5182 | list_add_tail(&ticket.list, |
| 5183 | &space_info->priority_tickets); |
| 5184 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5185 | } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
| 5186 | used += orig_bytes; |
Josef Bacik | f6acfd5 | 2014-09-18 11:27:17 -0400 | [diff] [blame] | 5187 | /* |
| 5188 | * We will do the space reservation dance during log replay, |
| 5189 | * which means we won't have fs_info->fs_root set, so don't do |
| 5190 | * the async reclaim as we will panic. |
| 5191 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5192 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) && |
Josef Bacik | 87241c2 | 2016-04-25 09:58:18 -0400 | [diff] [blame] | 5193 | need_do_async_reclaim(space_info, root, used) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5194 | !work_busy(&fs_info->async_reclaim_work)) { |
| 5195 | trace_btrfs_trigger_flush(fs_info, space_info->flags, |
| 5196 | orig_bytes, flush, "preempt"); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 5197 | queue_work(system_unbound_wq, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5198 | &fs_info->async_reclaim_work); |
Josef Bacik | f376df2 | 2016-03-25 13:25:56 -0400 | [diff] [blame] | 5199 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5200 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5201 | spin_unlock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5202 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5203 | return ret; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5204 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5205 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5206 | return wait_reserve_ticket(fs_info, space_info, &ticket, |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5207 | orig_bytes); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5208 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5209 | ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5210 | priority_reclaim_metadata_space(fs_info, space_info, &ticket); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5211 | spin_lock(&space_info->lock); |
| 5212 | if (ticket.bytes) { |
| 5213 | if (ticket.bytes < orig_bytes) { |
| 5214 | u64 num_bytes = orig_bytes - ticket.bytes; |
| 5215 | space_info->bytes_may_use -= num_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5216 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5217 | space_info->flags, |
| 5218 | num_bytes, 0); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5219 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5220 | } |
| 5221 | list_del_init(&ticket.list); |
| 5222 | ret = -ENOSPC; |
| 5223 | } |
| 5224 | spin_unlock(&space_info->lock); |
| 5225 | ASSERT(list_empty(&ticket.list)); |
| 5226 | return ret; |
| 5227 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5228 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5229 | /** |
| 5230 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 5231 | * @root - the root we're allocating for |
| 5232 | * @block_rsv - the block_rsv we're allocating for |
| 5233 | * @orig_bytes - the number of bytes we want |
| 5234 | * @flush - whether or not we can flush to make our reservation |
| 5235 | * |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5236 | * 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] | 5237 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 5238 | * flush out space to make room. It will do this by flushing delalloc if |
| 5239 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 5240 | * regain reservations will be made and this will fail if there is not enough |
| 5241 | * space already. |
| 5242 | */ |
| 5243 | static int reserve_metadata_bytes(struct btrfs_root *root, |
| 5244 | struct btrfs_block_rsv *block_rsv, |
| 5245 | u64 orig_bytes, |
| 5246 | enum btrfs_reserve_flush_enum flush) |
| 5247 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5248 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5249 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5250 | int ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5251 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5252 | ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes, |
| 5253 | flush); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5254 | if (ret == -ENOSPC && |
| 5255 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5256 | if (block_rsv != global_rsv && |
| 5257 | !block_rsv_use_bytes(global_rsv, orig_bytes)) |
| 5258 | ret = 0; |
| 5259 | } |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 5260 | if (ret == -ENOSPC) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5261 | trace_btrfs_space_reservation(fs_info, "space_info:enospc", |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5262 | block_rsv->space_info->flags, |
| 5263 | orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5264 | return ret; |
| 5265 | } |
| 5266 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5267 | static struct btrfs_block_rsv *get_block_rsv( |
| 5268 | const struct btrfs_trans_handle *trans, |
| 5269 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5270 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5271 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5272 | struct btrfs_block_rsv *block_rsv = NULL; |
| 5273 | |
Alexandru Moise | e9cf439 | 2015-09-09 00:18:50 +0000 | [diff] [blame] | 5274 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5275 | (root == fs_info->csum_root && trans->adding_csums) || |
| 5276 | (root == fs_info->uuid_root)) |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 5277 | block_rsv = trans->block_rsv; |
| 5278 | |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5279 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5280 | block_rsv = root->block_rsv; |
| 5281 | |
| 5282 | if (!block_rsv) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5283 | block_rsv = &fs_info->empty_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5284 | |
| 5285 | return block_rsv; |
| 5286 | } |
| 5287 | |
| 5288 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 5289 | u64 num_bytes) |
| 5290 | { |
| 5291 | int ret = -ENOSPC; |
| 5292 | spin_lock(&block_rsv->lock); |
| 5293 | if (block_rsv->reserved >= num_bytes) { |
| 5294 | block_rsv->reserved -= num_bytes; |
| 5295 | if (block_rsv->reserved < block_rsv->size) |
| 5296 | block_rsv->full = 0; |
| 5297 | ret = 0; |
| 5298 | } |
| 5299 | spin_unlock(&block_rsv->lock); |
| 5300 | return ret; |
| 5301 | } |
| 5302 | |
| 5303 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 5304 | u64 num_bytes, int update_size) |
| 5305 | { |
| 5306 | spin_lock(&block_rsv->lock); |
| 5307 | block_rsv->reserved += num_bytes; |
| 5308 | if (update_size) |
| 5309 | block_rsv->size += num_bytes; |
| 5310 | else if (block_rsv->reserved >= block_rsv->size) |
| 5311 | block_rsv->full = 1; |
| 5312 | spin_unlock(&block_rsv->lock); |
| 5313 | } |
| 5314 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 5315 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
| 5316 | struct btrfs_block_rsv *dest, u64 num_bytes, |
| 5317 | int min_factor) |
| 5318 | { |
| 5319 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5320 | u64 min_bytes; |
| 5321 | |
| 5322 | if (global_rsv->space_info != dest->space_info) |
| 5323 | return -ENOSPC; |
| 5324 | |
| 5325 | spin_lock(&global_rsv->lock); |
| 5326 | min_bytes = div_factor(global_rsv->size, min_factor); |
| 5327 | if (global_rsv->reserved < min_bytes + num_bytes) { |
| 5328 | spin_unlock(&global_rsv->lock); |
| 5329 | return -ENOSPC; |
| 5330 | } |
| 5331 | global_rsv->reserved -= num_bytes; |
| 5332 | if (global_rsv->reserved < global_rsv->size) |
| 5333 | global_rsv->full = 0; |
| 5334 | spin_unlock(&global_rsv->lock); |
| 5335 | |
| 5336 | block_rsv_add_bytes(dest, num_bytes, 1); |
| 5337 | return 0; |
| 5338 | } |
| 5339 | |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5340 | /* |
| 5341 | * This is for space we already have accounted in space_info->bytes_may_use, so |
| 5342 | * basically when we're returning space from block_rsv's. |
| 5343 | */ |
| 5344 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, |
| 5345 | struct btrfs_space_info *space_info, |
| 5346 | u64 num_bytes) |
| 5347 | { |
| 5348 | struct reserve_ticket *ticket; |
| 5349 | struct list_head *head; |
| 5350 | u64 used; |
| 5351 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH; |
| 5352 | bool check_overcommit = false; |
| 5353 | |
| 5354 | spin_lock(&space_info->lock); |
| 5355 | head = &space_info->priority_tickets; |
| 5356 | |
| 5357 | /* |
| 5358 | * If we are over our limit then we need to check and see if we can |
| 5359 | * overcommit, and if we can't then we just need to free up our space |
| 5360 | * and not satisfy any requests. |
| 5361 | */ |
| 5362 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 5363 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 5364 | space_info->bytes_may_use; |
| 5365 | if (used - num_bytes >= space_info->total_bytes) |
| 5366 | check_overcommit = true; |
| 5367 | again: |
| 5368 | while (!list_empty(head) && num_bytes) { |
| 5369 | ticket = list_first_entry(head, struct reserve_ticket, |
| 5370 | list); |
| 5371 | /* |
| 5372 | * We use 0 bytes because this space is already reserved, so |
| 5373 | * adding the ticket space would be a double count. |
| 5374 | */ |
| 5375 | if (check_overcommit && |
| 5376 | !can_overcommit(fs_info->extent_root, space_info, 0, |
| 5377 | flush)) |
| 5378 | break; |
| 5379 | if (num_bytes >= ticket->bytes) { |
| 5380 | list_del_init(&ticket->list); |
| 5381 | num_bytes -= ticket->bytes; |
| 5382 | ticket->bytes = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5383 | space_info->tickets_id++; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5384 | wake_up(&ticket->wait); |
| 5385 | } else { |
| 5386 | ticket->bytes -= num_bytes; |
| 5387 | num_bytes = 0; |
| 5388 | } |
| 5389 | } |
| 5390 | |
| 5391 | if (num_bytes && head == &space_info->priority_tickets) { |
| 5392 | head = &space_info->tickets; |
| 5393 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 5394 | goto again; |
| 5395 | } |
| 5396 | space_info->bytes_may_use -= num_bytes; |
| 5397 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5398 | space_info->flags, num_bytes, 0); |
| 5399 | spin_unlock(&space_info->lock); |
| 5400 | } |
| 5401 | |
| 5402 | /* |
| 5403 | * This is for newly allocated space that isn't accounted in |
| 5404 | * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent |
| 5405 | * we use this helper. |
| 5406 | */ |
| 5407 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, |
| 5408 | struct btrfs_space_info *space_info, |
| 5409 | u64 num_bytes) |
| 5410 | { |
| 5411 | struct reserve_ticket *ticket; |
| 5412 | struct list_head *head = &space_info->priority_tickets; |
| 5413 | |
| 5414 | again: |
| 5415 | while (!list_empty(head) && num_bytes) { |
| 5416 | ticket = list_first_entry(head, struct reserve_ticket, |
| 5417 | list); |
| 5418 | if (num_bytes >= ticket->bytes) { |
| 5419 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5420 | space_info->flags, |
| 5421 | ticket->bytes, 1); |
| 5422 | list_del_init(&ticket->list); |
| 5423 | num_bytes -= ticket->bytes; |
| 5424 | space_info->bytes_may_use += ticket->bytes; |
| 5425 | ticket->bytes = 0; |
Wang Xiaoguang | ce12965 | 2016-09-02 10:58:46 +0800 | [diff] [blame] | 5426 | space_info->tickets_id++; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5427 | wake_up(&ticket->wait); |
| 5428 | } else { |
| 5429 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5430 | space_info->flags, |
| 5431 | num_bytes, 1); |
| 5432 | space_info->bytes_may_use += num_bytes; |
| 5433 | ticket->bytes -= num_bytes; |
| 5434 | num_bytes = 0; |
| 5435 | } |
| 5436 | } |
| 5437 | |
| 5438 | if (num_bytes && head == &space_info->priority_tickets) { |
| 5439 | head = &space_info->tickets; |
| 5440 | goto again; |
| 5441 | } |
| 5442 | } |
| 5443 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5444 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 5445 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 5446 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5447 | { |
| 5448 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 5449 | |
| 5450 | spin_lock(&block_rsv->lock); |
| 5451 | if (num_bytes == (u64)-1) |
| 5452 | num_bytes = block_rsv->size; |
| 5453 | block_rsv->size -= num_bytes; |
| 5454 | if (block_rsv->reserved >= block_rsv->size) { |
| 5455 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 5456 | block_rsv->reserved = block_rsv->size; |
| 5457 | block_rsv->full = 1; |
| 5458 | } else { |
| 5459 | num_bytes = 0; |
| 5460 | } |
| 5461 | spin_unlock(&block_rsv->lock); |
| 5462 | |
| 5463 | if (num_bytes > 0) { |
| 5464 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 5465 | spin_lock(&dest->lock); |
| 5466 | if (!dest->full) { |
| 5467 | u64 bytes_to_add; |
| 5468 | |
| 5469 | bytes_to_add = dest->size - dest->reserved; |
| 5470 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 5471 | dest->reserved += bytes_to_add; |
| 5472 | if (dest->reserved >= dest->size) |
| 5473 | dest->full = 1; |
| 5474 | num_bytes -= bytes_to_add; |
| 5475 | } |
| 5476 | spin_unlock(&dest->lock); |
| 5477 | } |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 5478 | if (num_bytes) |
| 5479 | space_info_add_old_bytes(fs_info, space_info, |
| 5480 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5481 | } |
| 5482 | } |
| 5483 | |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5484 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src, |
| 5485 | struct btrfs_block_rsv *dst, u64 num_bytes, |
| 5486 | int update_size) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5487 | { |
| 5488 | int ret; |
| 5489 | |
| 5490 | ret = block_rsv_use_bytes(src, num_bytes); |
| 5491 | if (ret) |
| 5492 | return ret; |
| 5493 | |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5494 | block_rsv_add_bytes(dst, num_bytes, update_size); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5495 | return 0; |
| 5496 | } |
| 5497 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5498 | 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] | 5499 | { |
| 5500 | memset(rsv, 0, sizeof(*rsv)); |
| 5501 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5502 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5503 | } |
| 5504 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5505 | 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] | 5506 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5507 | { |
| 5508 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5509 | |
| 5510 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 5511 | if (!block_rsv) |
| 5512 | return NULL; |
| 5513 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5514 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5515 | block_rsv->space_info = __find_space_info(fs_info, |
| 5516 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5517 | return block_rsv; |
| 5518 | } |
| 5519 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5520 | void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5521 | struct btrfs_block_rsv *rsv) |
| 5522 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5523 | if (!rsv) |
| 5524 | return; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5525 | btrfs_block_rsv_release(fs_info, rsv, (u64)-1); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5526 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5527 | } |
| 5528 | |
Chris Mason | cdfb080 | 2015-04-06 18:17:00 -0700 | [diff] [blame] | 5529 | void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv) |
| 5530 | { |
| 5531 | kfree(rsv); |
| 5532 | } |
| 5533 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5534 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 5535 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, |
| 5536 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5537 | { |
| 5538 | int ret; |
| 5539 | |
| 5540 | if (num_bytes == 0) |
| 5541 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5542 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 5543 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5544 | if (!ret) { |
| 5545 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 5546 | return 0; |
| 5547 | } |
| 5548 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5549 | return ret; |
| 5550 | } |
| 5551 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5552 | 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] | 5553 | { |
| 5554 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5555 | int ret = -ENOSPC; |
| 5556 | |
| 5557 | if (!block_rsv) |
| 5558 | return 0; |
| 5559 | |
| 5560 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5561 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 5562 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5563 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5564 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5565 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5566 | return ret; |
| 5567 | } |
| 5568 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5569 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 5570 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, |
| 5571 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5572 | { |
| 5573 | u64 num_bytes = 0; |
| 5574 | int ret = -ENOSPC; |
| 5575 | |
| 5576 | if (!block_rsv) |
| 5577 | return 0; |
| 5578 | |
| 5579 | spin_lock(&block_rsv->lock); |
| 5580 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5581 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5582 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5583 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5584 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5585 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5586 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5587 | if (!ret) |
| 5588 | return 0; |
| 5589 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 5590 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5591 | if (!ret) { |
| 5592 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5593 | return 0; |
| 5594 | } |
| 5595 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5596 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5597 | } |
| 5598 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5599 | void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5600 | struct btrfs_block_rsv *block_rsv, |
| 5601 | u64 num_bytes) |
| 5602 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5603 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5604 | |
Liu Bo | 1750458 | 2013-12-29 21:44:50 +0800 | [diff] [blame] | 5605 | if (global_rsv == block_rsv || |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5606 | block_rsv->space_info != global_rsv->space_info) |
| 5607 | global_rsv = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5608 | block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5609 | } |
| 5610 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5611 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5612 | { |
| 5613 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 5614 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 5615 | u64 num_bytes; |
| 5616 | |
Josef Bacik | ae2e472 | 2016-05-27 12:58:35 -0400 | [diff] [blame] | 5617 | /* |
| 5618 | * The global block rsv is based on the size of the extent tree, the |
| 5619 | * checksum tree and the root tree. If the fs is empty we want to set |
| 5620 | * it to a minimal amount for safety. |
| 5621 | */ |
| 5622 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) + |
| 5623 | btrfs_root_used(&fs_info->csum_root->root_item) + |
| 5624 | btrfs_root_used(&fs_info->tree_root->root_item); |
| 5625 | num_bytes = max_t(u64, num_bytes, SZ_16M); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5626 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5627 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5628 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5629 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 5630 | block_rsv->size = min_t(u64, num_bytes, SZ_512M); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5631 | |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5632 | if (block_rsv->reserved < block_rsv->size) { |
| 5633 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
| 5634 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 5635 | sinfo->bytes_may_use; |
| 5636 | if (sinfo->total_bytes > num_bytes) { |
| 5637 | num_bytes = sinfo->total_bytes - num_bytes; |
| 5638 | num_bytes = min(num_bytes, |
| 5639 | block_rsv->size - block_rsv->reserved); |
| 5640 | block_rsv->reserved += num_bytes; |
| 5641 | sinfo->bytes_may_use += num_bytes; |
| 5642 | trace_btrfs_space_reservation(fs_info, "space_info", |
| 5643 | sinfo->flags, num_bytes, |
| 5644 | 1); |
| 5645 | } |
| 5646 | } else if (block_rsv->reserved > block_rsv->size) { |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5647 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5648 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5649 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5650 | sinfo->flags, num_bytes, 0); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5651 | block_rsv->reserved = block_rsv->size; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5652 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 5653 | |
Josef Bacik | fb4b10e | 2016-01-11 17:28:38 -0500 | [diff] [blame] | 5654 | if (block_rsv->reserved == block_rsv->size) |
| 5655 | block_rsv->full = 1; |
| 5656 | else |
| 5657 | block_rsv->full = 0; |
| 5658 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5659 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5660 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5661 | } |
| 5662 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5663 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5664 | { |
| 5665 | struct btrfs_space_info *space_info; |
| 5666 | |
| 5667 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 5668 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5669 | |
| 5670 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5671 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5672 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5673 | fs_info->trans_block_rsv.space_info = space_info; |
| 5674 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5675 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5676 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5677 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 5678 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 5679 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 5680 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Stefan Behrens | 3a6cad9 | 2013-05-16 14:48:19 +0000 | [diff] [blame] | 5681 | if (fs_info->quota_root) |
| 5682 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5683 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5684 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5685 | update_global_block_rsv(fs_info); |
| 5686 | } |
| 5687 | |
| 5688 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5689 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5690 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 5691 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5692 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 5693 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 5694 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 5695 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 5696 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 5697 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5698 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 5699 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5700 | } |
| 5701 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5702 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5703 | struct btrfs_fs_info *fs_info) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5704 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 5705 | if (!trans->block_rsv) |
| 5706 | return; |
| 5707 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5708 | if (!trans->bytes_reserved) |
| 5709 | return; |
| 5710 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5711 | trace_btrfs_space_reservation(fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5712 | trans->transid, trans->bytes_reserved, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5713 | btrfs_block_rsv_release(fs_info, trans->block_rsv, |
| 5714 | trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5715 | trans->bytes_reserved = 0; |
| 5716 | } |
| 5717 | |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5718 | /* |
| 5719 | * To be called after all the new block groups attached to the transaction |
| 5720 | * handle have been created (btrfs_create_pending_block_groups()). |
| 5721 | */ |
| 5722 | void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) |
| 5723 | { |
Jeff Mahoney | 64b6358 | 2016-06-20 17:23:41 -0400 | [diff] [blame] | 5724 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5725 | |
| 5726 | if (!trans->chunk_bytes_reserved) |
| 5727 | return; |
| 5728 | |
| 5729 | WARN_ON_ONCE(!list_empty(&trans->new_bgs)); |
| 5730 | |
| 5731 | block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL, |
| 5732 | trans->chunk_bytes_reserved); |
| 5733 | trans->chunk_bytes_reserved = 0; |
| 5734 | } |
| 5735 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5736 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5737 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 5738 | struct inode *inode) |
| 5739 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5740 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5741 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 40acc3e | 2016-05-27 13:01:08 -0400 | [diff] [blame] | 5742 | /* |
| 5743 | * We always use trans->block_rsv here as we will have reserved space |
| 5744 | * for our orphan when starting the transaction, using get_block_rsv() |
| 5745 | * here will sometimes make us choose the wrong block rsv as we could be |
| 5746 | * doing a reloc inode for a non refcounted root. |
| 5747 | */ |
| 5748 | struct btrfs_block_rsv *src_rsv = trans->block_rsv; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5749 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 5750 | |
| 5751 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5752 | * We need to hold space in order to delete our orphan item once we've |
| 5753 | * added it, so this takes the reservation so we can release it later |
| 5754 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5755 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5756 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
| 5757 | |
| 5758 | trace_btrfs_space_reservation(fs_info, "orphan", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5759 | btrfs_ino(inode), num_bytes, 1); |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5760 | return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5761 | } |
| 5762 | |
| 5763 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 5764 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5765 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5766 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5767 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
| 5768 | |
| 5769 | trace_btrfs_space_reservation(fs_info, "orphan", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5770 | btrfs_ino(inode), num_bytes, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5771 | btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5772 | } |
| 5773 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5774 | /* |
| 5775 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation |
| 5776 | * root: the root of the parent directory |
| 5777 | * rsv: block reservation |
| 5778 | * items: the number of items that we need do reservation |
| 5779 | * qgroup_reserved: used to return the reserved size in qgroup |
| 5780 | * |
| 5781 | * This function is used to reserve the space for snapshot/subvolume |
| 5782 | * creation and deletion. Those operations are different with the |
| 5783 | * common file/directory operations, they change two fs/file trees |
| 5784 | * and root tree, the number of items that the qgroup reserves is |
| 5785 | * different with the free space reservation. So we can not use |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5786 | * the space reservation mechanism in start_transaction(). |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5787 | */ |
| 5788 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, |
| 5789 | struct btrfs_block_rsv *rsv, |
| 5790 | int items, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5791 | u64 *qgroup_reserved, |
| 5792 | bool use_global_rsv) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5793 | { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5794 | u64 num_bytes; |
| 5795 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5796 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5797 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5798 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5799 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5800 | /* One for parent inode, two for dir entries */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5801 | num_bytes = 3 * fs_info->nodesize; |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5802 | ret = btrfs_qgroup_reserve_meta(root, num_bytes); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5803 | if (ret) |
| 5804 | return ret; |
| 5805 | } else { |
| 5806 | num_bytes = 0; |
| 5807 | } |
| 5808 | |
| 5809 | *qgroup_reserved = num_bytes; |
| 5810 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5811 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, items); |
| 5812 | rsv->space_info = __find_space_info(fs_info, |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5813 | BTRFS_BLOCK_GROUP_METADATA); |
| 5814 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, |
| 5815 | BTRFS_RESERVE_FLUSH_ALL); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5816 | |
| 5817 | if (ret == -ENOSPC && use_global_rsv) |
Josef Bacik | 25d609f | 2016-03-25 13:25:48 -0400 | [diff] [blame] | 5818 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5819 | |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5820 | if (ret && *qgroup_reserved) |
| 5821 | btrfs_qgroup_free_meta(root, *qgroup_reserved); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5822 | |
| 5823 | return ret; |
| 5824 | } |
| 5825 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5826 | void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info, |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5827 | struct btrfs_block_rsv *rsv, |
| 5828 | u64 qgroup_reserved) |
| 5829 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5830 | btrfs_block_rsv_release(fs_info, rsv, (u64)-1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5831 | } |
| 5832 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5833 | /** |
| 5834 | * drop_outstanding_extent - drop an outstanding extent |
| 5835 | * @inode: the inode we're dropping the extent for |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5836 | * @num_bytes: the number of bytes we're releasing. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5837 | * |
| 5838 | * This is called when we are freeing up an outstanding extent, either called |
| 5839 | * after an error or after an extent is written. This will return the number of |
| 5840 | * reserved extents that need to be freed. This must be called with |
| 5841 | * BTRFS_I(inode)->lock held. |
| 5842 | */ |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5843 | static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5844 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5845 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5846 | unsigned dropped_extents = 0; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5847 | unsigned num_extents = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5848 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5849 | num_extents = (unsigned)div64_u64(num_bytes + |
| 5850 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 5851 | BTRFS_MAX_EXTENT_SIZE); |
| 5852 | ASSERT(num_extents); |
| 5853 | ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents); |
| 5854 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5855 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5856 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5857 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5858 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5859 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5860 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5861 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5862 | * 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] | 5863 | * reserved then we need to leave the reserved extents count alone. |
| 5864 | */ |
| 5865 | if (BTRFS_I(inode)->outstanding_extents >= |
| 5866 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5867 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5868 | |
| 5869 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 5870 | BTRFS_I(inode)->outstanding_extents; |
| 5871 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5872 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5875 | /** |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 5876 | * calc_csum_metadata_size - return the amount of metadata space that must be |
| 5877 | * reserved/freed for the given bytes. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5878 | * @inode: the inode we're manipulating |
| 5879 | * @num_bytes: the number of bytes in question |
| 5880 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 5881 | * |
| 5882 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 5883 | * correct amount of metadata that must either be reserved or freed. We |
| 5884 | * calculate how many checksums we can fit into one leaf and then divide the |
| 5885 | * number of bytes that will need to be checksumed by this value to figure out |
| 5886 | * how many checksums will be required. If we are adding bytes then the number |
| 5887 | * may go up and we will return the number of additional bytes that must be |
| 5888 | * reserved. If it is going down we will return the number of bytes that must |
| 5889 | * be freed. |
| 5890 | * |
| 5891 | * This must be called with BTRFS_I(inode)->lock held. |
| 5892 | */ |
| 5893 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 5894 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5895 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5896 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 5897 | u64 old_csums, num_csums; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5898 | |
| 5899 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 5900 | BTRFS_I(inode)->csum_bytes == 0) |
| 5901 | return 0; |
| 5902 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5903 | old_csums = btrfs_csum_bytes_to_leaves(fs_info, |
| 5904 | BTRFS_I(inode)->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5905 | if (reserve) |
| 5906 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 5907 | else |
| 5908 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5909 | num_csums = btrfs_csum_bytes_to_leaves(fs_info, |
| 5910 | BTRFS_I(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 | |
| 5923 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 5924 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5925 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5926 | struct btrfs_root *root = BTRFS_I(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; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5930 | unsigned nr_extents = 0; |
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 | */ |
| 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) |
| 5958 | mutex_lock(&BTRFS_I(inode)->delalloc_mutex); |
| 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 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5962 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 6a41dd0 | 2015-03-13 15:12:23 -0400 | [diff] [blame] | 5963 | nr_extents = (unsigned)div64_u64(num_bytes + |
| 5964 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 5965 | BTRFS_MAX_EXTENT_SIZE); |
| 5966 | BTRFS_I(inode)->outstanding_extents += nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5967 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5968 | nr_extents = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5969 | if (BTRFS_I(inode)->outstanding_extents > |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5970 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5971 | nr_extents += BTRFS_I(inode)->outstanding_extents - |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5972 | BTRFS_I(inode)->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5973 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5974 | /* We always want to reserve a slot for updating the inode. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5975 | to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5976 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5977 | csum_bytes = BTRFS_I(inode)->csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5978 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5979 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5980 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5981 | ret = btrfs_qgroup_reserve_meta(root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5982 | nr_extents * fs_info->nodesize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5983 | if (ret) |
| 5984 | goto out_fail; |
Wang Shilong | a9870c0 | 2013-03-01 11:33:01 +0000 | [diff] [blame] | 5985 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5986 | |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5987 | ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5988 | if (unlikely(ret)) { |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 5989 | btrfs_qgroup_free_meta(root, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5990 | nr_extents * fs_info->nodesize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5991 | goto out_fail; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5992 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5993 | |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5994 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5995 | if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
Josef Bacik | f485c9e | 2016-03-25 13:25:55 -0400 | [diff] [blame] | 5996 | &BTRFS_I(inode)->runtime_flags)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5997 | to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 5998 | release_extra = true; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5999 | } |
| 6000 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 6001 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 6002 | |
| 6003 | if (delalloc_lock) |
| 6004 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 6005 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6006 | if (to_reserve) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6007 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6008 | btrfs_ino(inode), to_reserve, 1); |
Josef Bacik | 48c3d48 | 2016-03-25 13:25:49 -0400 | [diff] [blame] | 6009 | if (release_extra) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6010 | btrfs_block_rsv_release(fs_info, block_rsv, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6011 | btrfs_calc_trans_metadata_size(fs_info, 1)); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6012 | return 0; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6013 | |
| 6014 | out_fail: |
| 6015 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 6016 | dropped = drop_outstanding_extent(inode, num_bytes); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6017 | /* |
| 6018 | * If the inodes csum_bytes is the same as the original |
| 6019 | * csum_bytes then we know we haven't raced with any free()ers |
| 6020 | * so we can just reduce our inodes csum bytes and carry on. |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6021 | */ |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6022 | if (BTRFS_I(inode)->csum_bytes == csum_bytes) { |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6023 | calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6024 | } else { |
| 6025 | u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes; |
| 6026 | u64 bytes; |
| 6027 | |
| 6028 | /* |
| 6029 | * 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] | 6030 | * freed from any free-ers that occurred during this |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 6031 | * reservation, so we reset ->csum_bytes to the csum_bytes |
| 6032 | * before we dropped our lock, and then call the free for the |
| 6033 | * number of bytes that were freed while we were trying our |
| 6034 | * reservation. |
| 6035 | */ |
| 6036 | bytes = csum_bytes - BTRFS_I(inode)->csum_bytes; |
| 6037 | BTRFS_I(inode)->csum_bytes = csum_bytes; |
| 6038 | to_free = calc_csum_metadata_size(inode, bytes, 0); |
| 6039 | |
| 6040 | |
| 6041 | /* |
| 6042 | * Now we need to see how much we would have freed had we not |
| 6043 | * been making this reservation and our ->csum_bytes were not |
| 6044 | * artificially inflated. |
| 6045 | */ |
| 6046 | BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes; |
| 6047 | bytes = csum_bytes - orig_csum_bytes; |
| 6048 | bytes = calc_csum_metadata_size(inode, bytes, 0); |
| 6049 | |
| 6050 | /* |
| 6051 | * 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] | 6052 | * 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] | 6053 | * not had an artificially high ->csum_bytes, so we need to free |
| 6054 | * the remainder. If bytes is the same or less then we don't |
| 6055 | * need to do anything, the other free-ers did the correct |
| 6056 | * thing. |
| 6057 | */ |
| 6058 | BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes; |
| 6059 | if (bytes > to_free) |
| 6060 | to_free = bytes - to_free; |
| 6061 | else |
| 6062 | to_free = 0; |
| 6063 | } |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6064 | spin_unlock(&BTRFS_I(inode)->lock); |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 6065 | if (dropped) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6066 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6067 | |
| 6068 | if (to_free) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6069 | btrfs_block_rsv_release(fs_info, block_rsv, to_free); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6070 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 6071 | btrfs_ino(inode), to_free, 0); |
| 6072 | } |
| 6073 | if (delalloc_lock) |
| 6074 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
| 6075 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6076 | } |
| 6077 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6078 | /** |
| 6079 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 6080 | * @inode: the inode to release the reservation for |
| 6081 | * @num_bytes: the number of bytes we're releasing |
| 6082 | * |
| 6083 | * This will release the metadata reservation for an inode. This can be called |
| 6084 | * once we complete IO for a given set of bytes to release their metadata |
| 6085 | * reservations. |
| 6086 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6087 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 6088 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6089 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6090 | u64 to_free = 0; |
| 6091 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6092 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6093 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6094 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 6095 | dropped = drop_outstanding_extent(inode, num_bytes); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6096 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 6097 | if (num_bytes) |
| 6098 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6099 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 6100 | if (dropped > 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6101 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6102 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6103 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 6104 | return; |
| 6105 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6106 | trace_btrfs_space_reservation(fs_info, "delalloc", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6107 | btrfs_ino(inode), to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 6108 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6109 | 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] | 6110 | } |
| 6111 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 6112 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6113 | * btrfs_delalloc_reserve_space - reserve data and metadata space for |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6114 | * delalloc |
| 6115 | * @inode: inode we're writing to |
| 6116 | * @start: start range we are writing to |
| 6117 | * @len: how long the range we are writing to |
| 6118 | * |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6119 | * This will do the following things |
| 6120 | * |
| 6121 | * o reserve space in data space info for num bytes |
| 6122 | * and reserve precious corresponding qgroup space |
| 6123 | * (Done in check_data_free_space) |
| 6124 | * |
| 6125 | * o reserve space for metadata space, based on the number of outstanding |
| 6126 | * extents and how much csums will be needed |
| 6127 | * also reserve metadata space in a per root over-reserve method. |
| 6128 | * o add to the inodes->delalloc_bytes |
| 6129 | * o add it to the fs_info's delalloc inodes list. |
| 6130 | * (Above 3 all done in delalloc_reserve_metadata) |
| 6131 | * |
| 6132 | * Return 0 for success |
| 6133 | * Return <0 for error(-ENOSPC or -EQUOT) |
| 6134 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6135 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6136 | { |
| 6137 | int ret; |
| 6138 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6139 | ret = btrfs_check_data_free_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6140 | if (ret < 0) |
| 6141 | return ret; |
| 6142 | ret = btrfs_delalloc_reserve_metadata(inode, len); |
| 6143 | if (ret < 0) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6144 | btrfs_free_reserved_data_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6145 | return ret; |
| 6146 | } |
| 6147 | |
| 6148 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6149 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6150 | * @inode: inode we're releasing space for |
| 6151 | * @start: start position of the space already reserved |
| 6152 | * @len: the len of the space already reserved |
| 6153 | * |
| 6154 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 6155 | * called in the case that we don't need the metadata AND data reservations |
| 6156 | * anymore. So if there is an error or we insert an inline extent. |
| 6157 | * |
| 6158 | * This function will release the metadata space that was not used and will |
| 6159 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 6160 | * list if there are no delalloc bytes left. |
| 6161 | * Also it will handle the qgroup reserved space. |
| 6162 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6163 | void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 6164 | { |
| 6165 | btrfs_delalloc_release_metadata(inode, len); |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 6166 | btrfs_free_reserved_data_space(inode, start, len); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 6167 | } |
| 6168 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6169 | static int update_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 6170 | struct btrfs_fs_info *info, u64 bytenr, |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6171 | u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6172 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6173 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6174 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6175 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6176 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6177 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 6178 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6179 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 6180 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6181 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6182 | if (alloc) |
| 6183 | old_val += num_bytes; |
| 6184 | else |
| 6185 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6186 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 6187 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6188 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6189 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6190 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6191 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6192 | return -ENOENT; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6193 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 6194 | BTRFS_BLOCK_GROUP_RAID1 | |
| 6195 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6196 | factor = 2; |
| 6197 | else |
| 6198 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 6199 | /* |
| 6200 | * If this block group has free space cache written out, we |
| 6201 | * need to make sure to load it if we are removing space. This |
| 6202 | * is because we need the unpinning stage to actually add the |
| 6203 | * space back to the block group, otherwise we will leak space. |
| 6204 | */ |
| 6205 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6206 | cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6207 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6208 | byte_in_group = bytenr - cache->key.objectid; |
| 6209 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6210 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6211 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6212 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6213 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 6214 | if (btrfs_test_opt(info, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6215 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 6216 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 6217 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6218 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6219 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6220 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6221 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6222 | btrfs_set_block_group_used(&cache->item, old_val); |
| 6223 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6224 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6225 | cache->space_info->bytes_used += num_bytes; |
| 6226 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6227 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6228 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6229 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6230 | old_val -= num_bytes; |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 6231 | btrfs_set_block_group_used(&cache->item, old_val); |
| 6232 | cache->pinned += num_bytes; |
| 6233 | cache->space_info->bytes_pinned += num_bytes; |
| 6234 | cache->space_info->bytes_used -= num_bytes; |
| 6235 | cache->space_info->disk_used -= num_bytes * factor; |
| 6236 | spin_unlock(&cache->lock); |
| 6237 | spin_unlock(&cache->space_info->lock); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 6238 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6239 | trace_btrfs_space_reservation(info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6240 | cache->space_info->flags, |
| 6241 | num_bytes, 1); |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 6242 | set_extent_dirty(info->pinned_extents, |
| 6243 | bytenr, bytenr + num_bytes - 1, |
| 6244 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 6245 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 6246 | |
| 6247 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 6248 | if (list_empty(&cache->dirty_list)) { |
| 6249 | list_add_tail(&cache->dirty_list, |
| 6250 | &trans->transaction->dirty_bgs); |
| 6251 | trans->transaction->num_dirty_bgs++; |
| 6252 | btrfs_get_block_group(cache); |
| 6253 | } |
| 6254 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 6255 | |
Filipe Manana | 036a934 | 2015-11-23 15:25:16 +0000 | [diff] [blame] | 6256 | /* |
| 6257 | * No longer have used bytes in this block group, queue it for |
| 6258 | * deletion. We do this after adding the block group to the |
| 6259 | * dirty list to avoid races between cleaner kthread and space |
| 6260 | * cache writeout. |
| 6261 | */ |
| 6262 | if (!alloc && old_val == 0) { |
| 6263 | spin_lock(&info->unused_bgs_lock); |
| 6264 | if (list_empty(&cache->bg_list)) { |
| 6265 | btrfs_get_block_group(cache); |
| 6266 | list_add_tail(&cache->bg_list, |
| 6267 | &info->unused_bgs); |
| 6268 | } |
| 6269 | spin_unlock(&info->unused_bgs_lock); |
| 6270 | } |
| 6271 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6272 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6273 | total -= num_bytes; |
| 6274 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6275 | } |
| 6276 | return 0; |
| 6277 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6278 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6279 | 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] | 6280 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6281 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6282 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6283 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6284 | spin_lock(&fs_info->block_group_cache_lock); |
| 6285 | bytenr = fs_info->first_logical_byte; |
| 6286 | spin_unlock(&fs_info->block_group_cache_lock); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 6287 | |
| 6288 | if (bytenr < (u64)-1) |
| 6289 | return bytenr; |
| 6290 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6291 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6292 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6293 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6294 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6295 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6296 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6297 | |
| 6298 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6299 | } |
| 6300 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6301 | static int pin_down_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6302 | struct btrfs_block_group_cache *cache, |
| 6303 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6304 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6305 | spin_lock(&cache->space_info->lock); |
| 6306 | spin_lock(&cache->lock); |
| 6307 | cache->pinned += num_bytes; |
| 6308 | cache->space_info->bytes_pinned += num_bytes; |
| 6309 | if (reserved) { |
| 6310 | cache->reserved -= num_bytes; |
| 6311 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6312 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6313 | spin_unlock(&cache->lock); |
| 6314 | spin_unlock(&cache->space_info->lock); |
| 6315 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6316 | trace_btrfs_space_reservation(fs_info, "pinned", |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6317 | cache->space_info->flags, num_bytes, 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6318 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6319 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6320 | return 0; |
| 6321 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6322 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6323 | /* |
| 6324 | * this function must be called within transaction |
| 6325 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6326 | int btrfs_pin_extent(struct btrfs_fs_info *fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6327 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6328 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6329 | struct btrfs_block_group_cache *cache; |
| 6330 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6331 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6332 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6333 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6334 | pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6335 | |
| 6336 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6339 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6340 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6341 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6342 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6343 | 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] | 6344 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6345 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6346 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6347 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6348 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6349 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6350 | if (!cache) |
| 6351 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6352 | |
| 6353 | /* |
| 6354 | * pull in the free space cache (if any) so that our pin |
| 6355 | * removes the free space from the cache. We have load_only set |
| 6356 | * to one because the slow code to read in the free extents does check |
| 6357 | * the pinned extents. |
| 6358 | */ |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6359 | cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6360 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6361 | pin_down_extent(fs_info, cache, bytenr, num_bytes, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6362 | |
| 6363 | /* 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] | 6364 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6365 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6366 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6367 | } |
| 6368 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6369 | static int __exclude_logged_extent(struct btrfs_fs_info *fs_info, |
| 6370 | u64 start, u64 num_bytes) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6371 | { |
| 6372 | int ret; |
| 6373 | struct btrfs_block_group_cache *block_group; |
| 6374 | struct btrfs_caching_control *caching_ctl; |
| 6375 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6376 | block_group = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6377 | if (!block_group) |
| 6378 | return -EINVAL; |
| 6379 | |
| 6380 | cache_block_group(block_group, 0); |
| 6381 | caching_ctl = get_caching_control(block_group); |
| 6382 | |
| 6383 | if (!caching_ctl) { |
| 6384 | /* Logic error */ |
| 6385 | BUG_ON(!block_group_cache_done(block_group)); |
| 6386 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 6387 | } else { |
| 6388 | mutex_lock(&caching_ctl->mutex); |
| 6389 | |
| 6390 | if (start >= caching_ctl->progress) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6391 | ret = add_excluded_extent(fs_info, start, num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6392 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 6393 | ret = btrfs_remove_free_space(block_group, |
| 6394 | start, num_bytes); |
| 6395 | } else { |
| 6396 | num_bytes = caching_ctl->progress - start; |
| 6397 | ret = btrfs_remove_free_space(block_group, |
| 6398 | start, num_bytes); |
| 6399 | if (ret) |
| 6400 | goto out_lock; |
| 6401 | |
| 6402 | num_bytes = (start + num_bytes) - |
| 6403 | caching_ctl->progress; |
| 6404 | start = caching_ctl->progress; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6405 | ret = add_excluded_extent(fs_info, start, num_bytes); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6406 | } |
| 6407 | out_lock: |
| 6408 | mutex_unlock(&caching_ctl->mutex); |
| 6409 | put_caching_control(caching_ctl); |
| 6410 | } |
| 6411 | btrfs_put_block_group(block_group); |
| 6412 | return ret; |
| 6413 | } |
| 6414 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6415 | int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info, |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6416 | struct extent_buffer *eb) |
| 6417 | { |
| 6418 | struct btrfs_file_extent_item *item; |
| 6419 | struct btrfs_key key; |
| 6420 | int found_type; |
| 6421 | int i; |
| 6422 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6423 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6424 | return 0; |
| 6425 | |
| 6426 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 6427 | btrfs_item_key_to_cpu(eb, &key, i); |
| 6428 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 6429 | continue; |
| 6430 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 6431 | found_type = btrfs_file_extent_type(eb, item); |
| 6432 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 6433 | continue; |
| 6434 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 6435 | continue; |
| 6436 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 6437 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6438 | __exclude_logged_extent(fs_info, key.objectid, key.offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | return 0; |
| 6442 | } |
| 6443 | |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 6444 | static void |
| 6445 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 6446 | { |
| 6447 | atomic_inc(&bg->reservations); |
| 6448 | } |
| 6449 | |
| 6450 | void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, |
| 6451 | const u64 start) |
| 6452 | { |
| 6453 | struct btrfs_block_group_cache *bg; |
| 6454 | |
| 6455 | bg = btrfs_lookup_block_group(fs_info, start); |
| 6456 | ASSERT(bg); |
| 6457 | if (atomic_dec_and_test(&bg->reservations)) |
| 6458 | wake_up_atomic_t(&bg->reservations); |
| 6459 | btrfs_put_block_group(bg); |
| 6460 | } |
| 6461 | |
| 6462 | static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a) |
| 6463 | { |
| 6464 | schedule(); |
| 6465 | return 0; |
| 6466 | } |
| 6467 | |
| 6468 | void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg) |
| 6469 | { |
| 6470 | struct btrfs_space_info *space_info = bg->space_info; |
| 6471 | |
| 6472 | ASSERT(bg->ro); |
| 6473 | |
| 6474 | if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) |
| 6475 | return; |
| 6476 | |
| 6477 | /* |
| 6478 | * Our block group is read only but before we set it to read only, |
| 6479 | * some task might have had allocated an extent from it already, but it |
| 6480 | * has not yet created a respective ordered extent (and added it to a |
| 6481 | * root's list of ordered extents). |
| 6482 | * Therefore wait for any task currently allocating extents, since the |
| 6483 | * block group's reservations counter is incremented while a read lock |
| 6484 | * on the groups' semaphore is held and decremented after releasing |
| 6485 | * the read access on that semaphore and creating the ordered extent. |
| 6486 | */ |
| 6487 | down_write(&space_info->groups_sem); |
| 6488 | up_write(&space_info->groups_sem); |
| 6489 | |
| 6490 | wait_on_atomic_t(&bg->reservations, |
| 6491 | btrfs_wait_bg_reservations_atomic_t, |
| 6492 | TASK_UNINTERRUPTIBLE); |
| 6493 | } |
| 6494 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6495 | /** |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6496 | * btrfs_add_reserved_bytes - update the block_group and space info counters |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6497 | * @cache: The cache we are manipulating |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6498 | * @ram_bytes: The number of bytes of file content, and will be same to |
| 6499 | * @num_bytes except for the compress path. |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6500 | * @num_bytes: The number of bytes in question |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6501 | * @delalloc: The blocks are allocated for the delalloc write |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6502 | * |
Xiaoguang Wang | 745699e | 2016-09-23 12:38:50 +0800 | [diff] [blame] | 6503 | * This is called by the allocator when it reserves space. If this is a |
| 6504 | * reservation and the block group has become read only we cannot make the |
| 6505 | * reservation and return -EAGAIN, otherwise this function always succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6506 | */ |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6507 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6508 | u64 ram_bytes, u64 num_bytes, int delalloc) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6509 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6510 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6511 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6512 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6513 | spin_lock(&space_info->lock); |
| 6514 | spin_lock(&cache->lock); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6515 | if (cache->ro) { |
| 6516 | ret = -EAGAIN; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6517 | } else { |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6518 | cache->reserved += num_bytes; |
| 6519 | space_info->bytes_reserved += num_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6520 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 6521 | trace_btrfs_space_reservation(cache->fs_info, |
| 6522 | "space_info", space_info->flags, |
| 6523 | ram_bytes, 0); |
| 6524 | space_info->bytes_may_use -= ram_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6525 | if (delalloc) |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6526 | cache->delalloc_bytes += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6527 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6528 | spin_unlock(&cache->lock); |
| 6529 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6530 | return ret; |
| 6531 | } |
| 6532 | |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 6533 | /** |
| 6534 | * btrfs_free_reserved_bytes - update the block_group and space info counters |
| 6535 | * @cache: The cache we are manipulating |
| 6536 | * @num_bytes: The number of bytes in question |
| 6537 | * @delalloc: The blocks are allocated for the delalloc write |
| 6538 | * |
| 6539 | * This is called by somebody who is freeing space that was never actually used |
| 6540 | * on disk. For example if you reserve some space for a new leaf in transaction |
| 6541 | * A and before transaction A commits you free that leaf, you call this with |
| 6542 | * reserve set to 0 in order to clear the reservation. |
| 6543 | */ |
| 6544 | |
| 6545 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 6546 | u64 num_bytes, int delalloc) |
| 6547 | { |
| 6548 | struct btrfs_space_info *space_info = cache->space_info; |
| 6549 | int ret = 0; |
| 6550 | |
| 6551 | spin_lock(&space_info->lock); |
| 6552 | spin_lock(&cache->lock); |
| 6553 | if (cache->ro) |
| 6554 | space_info->bytes_readonly += num_bytes; |
| 6555 | cache->reserved -= num_bytes; |
| 6556 | space_info->bytes_reserved -= num_bytes; |
| 6557 | |
| 6558 | if (delalloc) |
| 6559 | cache->delalloc_bytes -= num_bytes; |
| 6560 | spin_unlock(&cache->lock); |
| 6561 | spin_unlock(&space_info->lock); |
| 6562 | return ret; |
| 6563 | } |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6564 | void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6565 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6566 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6567 | struct btrfs_caching_control *next; |
| 6568 | struct btrfs_caching_control *caching_ctl; |
| 6569 | struct btrfs_block_group_cache *cache; |
| 6570 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6571 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6572 | |
| 6573 | list_for_each_entry_safe(caching_ctl, next, |
| 6574 | &fs_info->caching_block_groups, list) { |
| 6575 | cache = caching_ctl->block_group; |
| 6576 | if (block_group_cache_done(cache)) { |
| 6577 | cache->last_byte_to_unpin = (u64)-1; |
| 6578 | list_del_init(&caching_ctl->list); |
| 6579 | put_caching_control(caching_ctl); |
| 6580 | } else { |
| 6581 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 6582 | } |
| 6583 | } |
| 6584 | |
| 6585 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6586 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 6587 | else |
| 6588 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 6589 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6590 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6591 | |
| 6592 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6593 | } |
| 6594 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6595 | /* |
| 6596 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 6597 | * what it should be based on the mount options. |
| 6598 | */ |
| 6599 | static struct btrfs_free_cluster * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6600 | fetch_cluster_info(struct btrfs_fs_info *fs_info, |
| 6601 | struct btrfs_space_info *space_info, u64 *empty_cluster) |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6602 | { |
| 6603 | struct btrfs_free_cluster *ret = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6604 | bool ssd = btrfs_test_opt(fs_info, SSD); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6605 | |
| 6606 | *empty_cluster = 0; |
| 6607 | if (btrfs_mixed_space_info(space_info)) |
| 6608 | return ret; |
| 6609 | |
| 6610 | if (ssd) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 6611 | *empty_cluster = SZ_2M; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6612 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6613 | ret = &fs_info->meta_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6614 | if (!ssd) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 6615 | *empty_cluster = SZ_64K; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6616 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6617 | ret = &fs_info->data_alloc_cluster; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6618 | } |
| 6619 | |
| 6620 | return ret; |
| 6621 | } |
| 6622 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6623 | static int unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 6624 | u64 start, u64 end, |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6625 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6626 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6627 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6628 | struct btrfs_space_info *space_info; |
| 6629 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6630 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6631 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6632 | u64 total_unpinned = 0; |
| 6633 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6634 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6635 | |
| 6636 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6637 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6638 | if (!cache || |
| 6639 | start >= cache->key.objectid + cache->key.offset) { |
| 6640 | if (cache) |
| 6641 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6642 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6643 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6644 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6645 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6646 | cluster = fetch_cluster_info(fs_info, |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6647 | cache->space_info, |
| 6648 | &empty_cluster); |
| 6649 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | len = cache->key.objectid + cache->key.offset - start; |
| 6653 | len = min(len, end + 1 - start); |
| 6654 | |
| 6655 | if (start < cache->last_byte_to_unpin) { |
| 6656 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6657 | if (return_free_space) |
| 6658 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6659 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6660 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6661 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6662 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6663 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6664 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6665 | /* |
| 6666 | * If this space cluster has been marked as fragmented and we've |
| 6667 | * unpinned enough in this block group to potentially allow a |
| 6668 | * cluster to be created inside of it go ahead and clear the |
| 6669 | * fragmented check. |
| 6670 | */ |
| 6671 | if (cluster && cluster->fragmented && |
| 6672 | total_unpinned > empty_cluster) { |
| 6673 | spin_lock(&cluster->lock); |
| 6674 | cluster->fragmented = 0; |
| 6675 | spin_unlock(&cluster->lock); |
| 6676 | } |
| 6677 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6678 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6679 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6680 | cache->pinned -= len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6681 | space_info->bytes_pinned -= len; |
Josef Bacik | c51e7bb | 2016-03-25 13:25:54 -0400 | [diff] [blame] | 6682 | |
| 6683 | trace_btrfs_space_reservation(fs_info, "pinned", |
| 6684 | space_info->flags, len, 0); |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 6685 | space_info->max_extent_size = 0; |
Liu Bo | d288db5 | 2014-07-02 16:58:01 +0800 | [diff] [blame] | 6686 | percpu_counter_add(&space_info->total_bytes_pinned, -len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6687 | if (cache->ro) { |
| 6688 | space_info->bytes_readonly += len; |
| 6689 | readonly = true; |
| 6690 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6691 | spin_unlock(&cache->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6692 | if (!readonly && return_free_space && |
| 6693 | global_rsv->space_info == space_info) { |
| 6694 | u64 to_add = len; |
| 6695 | WARN_ON(!return_free_space); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6696 | spin_lock(&global_rsv->lock); |
| 6697 | if (!global_rsv->full) { |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6698 | to_add = min(len, global_rsv->size - |
| 6699 | global_rsv->reserved); |
| 6700 | global_rsv->reserved += to_add; |
| 6701 | space_info->bytes_may_use += to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6702 | if (global_rsv->reserved >= global_rsv->size) |
| 6703 | global_rsv->full = 1; |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6704 | trace_btrfs_space_reservation(fs_info, |
| 6705 | "space_info", |
| 6706 | space_info->flags, |
| 6707 | to_add, 1); |
| 6708 | len -= to_add; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6709 | } |
| 6710 | spin_unlock(&global_rsv->lock); |
Josef Bacik | 957780e | 2016-05-17 13:30:55 -0400 | [diff] [blame] | 6711 | /* Add to any tickets we may have */ |
| 6712 | if (len) |
| 6713 | space_info_add_new_bytes(fs_info, space_info, |
| 6714 | len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6715 | } |
| 6716 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6720 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 6721 | return 0; |
| 6722 | } |
| 6723 | |
| 6724 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6725 | struct btrfs_fs_info *fs_info) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6726 | { |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6727 | struct btrfs_block_group_cache *block_group, *tmp; |
| 6728 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6729 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6730 | u64 start; |
| 6731 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6732 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6733 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6734 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6735 | unpin = &fs_info->freed_extents[1]; |
| 6736 | else |
| 6737 | unpin = &fs_info->freed_extents[0]; |
| 6738 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6739 | while (!trans->aborted) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6740 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6741 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 6742 | EXTENT_DIRTY, NULL); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6743 | if (ret) { |
| 6744 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6745 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6746 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6747 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6748 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6749 | ret = btrfs_discard_extent(fs_info, start, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 6750 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6751 | |
David Sterba | af6f8f6 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 6752 | clear_extent_dirty(unpin, start, end); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6753 | unpin_extent_range(fs_info, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6754 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6755 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6756 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6757 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6758 | /* |
| 6759 | * Transaction is finished. We don't need the lock anymore. We |
| 6760 | * do need to clean up the block groups in case of a transaction |
| 6761 | * abort. |
| 6762 | */ |
| 6763 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 6764 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 6765 | u64 trimmed = 0; |
| 6766 | |
| 6767 | ret = -EROFS; |
| 6768 | if (!trans->aborted) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6769 | ret = btrfs_discard_extent(fs_info, |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6770 | block_group->key.objectid, |
| 6771 | block_group->key.offset, |
| 6772 | &trimmed); |
| 6773 | |
| 6774 | list_del_init(&block_group->bg_list); |
| 6775 | btrfs_put_block_group_trimming(block_group); |
| 6776 | btrfs_put_block_group(block_group); |
| 6777 | |
| 6778 | if (ret) { |
| 6779 | const char *errstr = btrfs_decode_error(ret); |
| 6780 | btrfs_warn(fs_info, |
| 6781 | "Discard failed while removing blockgroup: errno=%d %s\n", |
| 6782 | ret, errstr); |
| 6783 | } |
| 6784 | } |
| 6785 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 6786 | return 0; |
| 6787 | } |
| 6788 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6789 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
| 6790 | u64 owner, u64 root_objectid) |
| 6791 | { |
| 6792 | struct btrfs_space_info *space_info; |
| 6793 | u64 flags; |
| 6794 | |
| 6795 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 6796 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) |
| 6797 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 6798 | else |
| 6799 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 6800 | } else { |
| 6801 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 6802 | } |
| 6803 | |
| 6804 | space_info = __find_space_info(fs_info, flags); |
| 6805 | BUG_ON(!space_info); /* Logic bug */ |
| 6806 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); |
| 6807 | } |
| 6808 | |
| 6809 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6810 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6811 | struct btrfs_fs_info *info, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6812 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6813 | u64 root_objectid, u64 owner_objectid, |
| 6814 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6815 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6816 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6817 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6818 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 6819 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6820 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6821 | struct btrfs_extent_item *ei; |
| 6822 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6823 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6824 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6825 | int extent_slot = 0; |
| 6826 | int found_extent = 0; |
| 6827 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6828 | u32 item_size; |
| 6829 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6830 | u64 bytenr = node->bytenr; |
| 6831 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6832 | int last_ref = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6833 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 6834 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6835 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6836 | if (!path) |
| 6837 | return -ENOMEM; |
| 6838 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 6839 | path->reada = READA_FORWARD; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6840 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6841 | |
| 6842 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 6843 | BUG_ON(!is_data && refs_to_drop != 1); |
| 6844 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6845 | if (is_data) |
| 6846 | skinny_metadata = 0; |
| 6847 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6848 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 6849 | bytenr, num_bytes, parent, |
| 6850 | root_objectid, owner_objectid, |
| 6851 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6852 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6853 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6854 | while (extent_slot >= 0) { |
| 6855 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6856 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6857 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6858 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6859 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6860 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6861 | found_extent = 1; |
| 6862 | break; |
| 6863 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6864 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 6865 | key.offset == owner_objectid) { |
| 6866 | found_extent = 1; |
| 6867 | break; |
| 6868 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6869 | if (path->slots[0] - extent_slot > 5) |
| 6870 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6871 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6872 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6873 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6874 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 6875 | if (found_extent && item_size < sizeof(*ei)) |
| 6876 | found_extent = 0; |
| 6877 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6878 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6879 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6880 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6881 | NULL, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6882 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6883 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6884 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6885 | goto out; |
| 6886 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6887 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6888 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6889 | |
| 6890 | key.objectid = bytenr; |
| 6891 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6892 | key.offset = num_bytes; |
| 6893 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6894 | if (!is_data && skinny_metadata) { |
| 6895 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 6896 | key.offset = owner_objectid; |
| 6897 | } |
| 6898 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6899 | ret = btrfs_search_slot(trans, extent_root, |
| 6900 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6901 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 6902 | /* |
| 6903 | * Couldn't find our skinny metadata item, |
| 6904 | * see if we have ye olde extent item. |
| 6905 | */ |
| 6906 | path->slots[0]--; |
| 6907 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 6908 | path->slots[0]); |
| 6909 | if (key.objectid == bytenr && |
| 6910 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6911 | key.offset == num_bytes) |
| 6912 | ret = 0; |
| 6913 | } |
| 6914 | |
| 6915 | if (ret > 0 && skinny_metadata) { |
| 6916 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 6917 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6918 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6919 | key.offset = num_bytes; |
| 6920 | btrfs_release_path(path); |
| 6921 | ret = btrfs_search_slot(trans, extent_root, |
| 6922 | &key, path, -1, 1); |
| 6923 | } |
| 6924 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6925 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6926 | btrfs_err(info, |
| 6927 | "umm, got %d back from search, was looking for %llu", |
| 6928 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 6929 | if (ret > 0) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6930 | btrfs_print_leaf(info, path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6931 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6932 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6933 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6934 | goto out; |
| 6935 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6936 | extent_slot = path->slots[0]; |
| 6937 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6938 | } else if (WARN_ON(ret == -ENOENT)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6939 | btrfs_print_leaf(info, path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6940 | btrfs_err(info, |
| 6941 | "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] | 6942 | bytenr, parent, root_objectid, owner_objectid, |
| 6943 | owner_offset); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6944 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 6945 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6946 | } else { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6947 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6948 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6949 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6950 | |
| 6951 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6952 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6953 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6954 | if (item_size < sizeof(*ei)) { |
| 6955 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 6956 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 6957 | owner_objectid, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6958 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6959 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6960 | goto out; |
| 6961 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6962 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6963 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6964 | path->leave_spinning = 1; |
| 6965 | |
| 6966 | key.objectid = bytenr; |
| 6967 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6968 | key.offset = num_bytes; |
| 6969 | |
| 6970 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 6971 | -1, 1); |
| 6972 | if (ret) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6973 | btrfs_err(info, |
| 6974 | "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6975 | ret, bytenr); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6976 | btrfs_print_leaf(info, path->nodes[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6977 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6978 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 6979 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6980 | goto out; |
| 6981 | } |
| 6982 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6983 | extent_slot = path->slots[0]; |
| 6984 | leaf = path->nodes[0]; |
| 6985 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6986 | } |
| 6987 | #endif |
| 6988 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6989 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 6990 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6991 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 6992 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6993 | struct btrfs_tree_block_info *bi; |
| 6994 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 6995 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 6996 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6997 | } |
| 6998 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6999 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7000 | if (refs < refs_to_drop) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7001 | btrfs_err(info, |
| 7002 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", |
| 7003 | refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7004 | ret = -EINVAL; |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7005 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 7006 | goto out; |
| 7007 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7008 | refs -= refs_to_drop; |
| 7009 | |
| 7010 | if (refs > 0) { |
| 7011 | if (extent_op) |
| 7012 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 7013 | /* |
| 7014 | * In the case of inline back ref, reference count will |
| 7015 | * be updated by remove_extent_backref |
| 7016 | */ |
| 7017 | if (iref) { |
| 7018 | BUG_ON(!found_extent); |
| 7019 | } else { |
| 7020 | btrfs_set_extent_refs(leaf, ei, refs); |
| 7021 | btrfs_mark_buffer_dirty(leaf); |
| 7022 | } |
| 7023 | if (found_extent) { |
| 7024 | ret = remove_extent_backref(trans, extent_root, path, |
| 7025 | iref, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7026 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7027 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7028 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7029 | goto out; |
| 7030 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7031 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7032 | add_pinned_bytes(info, -num_bytes, owner_objectid, |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7033 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7034 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7035 | if (found_extent) { |
| 7036 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 7037 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7038 | if (iref) { |
| 7039 | BUG_ON(path->slots[0] != extent_slot); |
| 7040 | } else { |
| 7041 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 7042 | path->slots[0] = extent_slot; |
| 7043 | num_to_del = 2; |
| 7044 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 7045 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7046 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7047 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 7048 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 7049 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7050 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7051 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7052 | goto out; |
| 7053 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7054 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 7055 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7056 | if (is_data) { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7057 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7058 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7059 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7060 | goto out; |
| 7061 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 7062 | } |
| 7063 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7064 | ret = add_to_free_space_tree(trans, info, bytenr, num_bytes); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7065 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7066 | btrfs_abort_transaction(trans, ret); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 7067 | goto out; |
| 7068 | } |
| 7069 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7070 | ret = update_block_group(trans, info, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7071 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7072 | btrfs_abort_transaction(trans, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 7073 | goto out; |
| 7074 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7075 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7076 | btrfs_release_path(path); |
| 7077 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7078 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7079 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7080 | return ret; |
| 7081 | } |
| 7082 | |
| 7083 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7084 | * 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] | 7085 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 7086 | * a given extent, and if there are no other delayed refs to be processed, it |
| 7087 | * removes it from the tree. |
| 7088 | */ |
| 7089 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7090 | u64 bytenr) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7091 | { |
| 7092 | struct btrfs_delayed_ref_head *head; |
| 7093 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7094 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7095 | |
| 7096 | delayed_refs = &trans->transaction->delayed_refs; |
| 7097 | spin_lock(&delayed_refs->lock); |
| 7098 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 7099 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 7100 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7101 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7102 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 7103 | if (!list_empty(&head->ref_list)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7104 | goto out; |
| 7105 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7106 | if (head->extent_op) { |
| 7107 | if (!head->must_insert_reserved) |
| 7108 | goto out; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 7109 | btrfs_free_delayed_extent_op(head->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7110 | head->extent_op = NULL; |
| 7111 | } |
| 7112 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7113 | /* |
| 7114 | * waiting for the lock here would deadlock. If someone else has it |
| 7115 | * locked they are already in the process of dropping it anyway |
| 7116 | */ |
| 7117 | if (!mutex_trylock(&head->mutex)) |
| 7118 | goto out; |
| 7119 | |
| 7120 | /* |
| 7121 | * at this point we have a head with no other entries. Go |
| 7122 | * ahead and process it. |
| 7123 | */ |
| 7124 | head->node.in_tree = 0; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 7125 | rb_erase(&head->href_node, &delayed_refs->href_root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7126 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7127 | atomic_dec(&delayed_refs->num_entries); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7128 | |
| 7129 | /* |
| 7130 | * we don't take a ref on the node because we're removing it from the |
| 7131 | * tree, so we just steal the ref the tree was holding. |
| 7132 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7133 | delayed_refs->num_heads--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7134 | if (head->processing == 0) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7135 | delayed_refs->num_heads_ready--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7136 | head->processing = 0; |
| 7137 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7138 | spin_unlock(&delayed_refs->lock); |
| 7139 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7140 | BUG_ON(head->extent_op); |
| 7141 | if (head->must_insert_reserved) |
| 7142 | ret = 1; |
| 7143 | |
| 7144 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7145 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7146 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7147 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 7148 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 7149 | |
| 7150 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 7151 | spin_unlock(&delayed_refs->lock); |
| 7152 | return 0; |
| 7153 | } |
| 7154 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7155 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 7156 | struct btrfs_root *root, |
| 7157 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7158 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7159 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7160 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7161 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7162 | int ret; |
| 7163 | |
| 7164 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7165 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
| 7166 | buf->start, buf->len, |
| 7167 | parent, |
| 7168 | root->root_key.objectid, |
| 7169 | btrfs_header_level(buf), |
| 7170 | BTRFS_DROP_DELAYED_REF, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7171 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | if (!last_ref) |
| 7175 | return; |
| 7176 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7177 | if (btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7178 | struct btrfs_block_group_cache *cache; |
| 7179 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7180 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7181 | ret = check_ref_cleanup(trans, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7182 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7183 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7184 | } |
| 7185 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7186 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7187 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7188 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7189 | pin_down_extent(fs_info, cache, buf->start, |
| 7190 | buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7191 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7192 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7193 | } |
| 7194 | |
| 7195 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 7196 | |
| 7197 | btrfs_add_free_space(cache, buf->start, buf->len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 7198 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 7199 | btrfs_put_block_group(cache); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7200 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7201 | pin = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7202 | } |
| 7203 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7204 | if (pin) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7205 | add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf), |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7206 | root->root_key.objectid); |
| 7207 | |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 7208 | /* |
| 7209 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 7210 | * anymore. |
| 7211 | */ |
| 7212 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7213 | } |
| 7214 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7215 | /* Can return -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7216 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 7217 | struct btrfs_fs_info *fs_info, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7218 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7219 | u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7220 | { |
| 7221 | int ret; |
| 7222 | |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 7223 | if (btrfs_is_testing(fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7224 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 7225 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7226 | add_pinned_bytes(fs_info, num_bytes, owner, root_objectid); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 7227 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7228 | /* |
| 7229 | * tree log blocks never actually go into the extent allocation |
| 7230 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7231 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7232 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 7233 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7234 | /* unlocks the pinned mutex */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7235 | btrfs_pin_extent(fs_info, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7236 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7237 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7238 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 7239 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7240 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7241 | BTRFS_DROP_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7242 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7243 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 7244 | num_bytes, |
| 7245 | parent, root_objectid, owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 7246 | offset, 0, |
| 7247 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7248 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7249 | return ret; |
| 7250 | } |
| 7251 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7252 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7253 | * when we wait for progress in the block group caching, its because |
| 7254 | * our allocation attempt failed at least once. So, we must sleep |
| 7255 | * and let some progress happen before we try again. |
| 7256 | * |
| 7257 | * This function will sleep at least once waiting for new free space to |
| 7258 | * show up, and then it will check the block group free space numbers |
| 7259 | * for our min num_bytes. Another option is to have it go ahead |
| 7260 | * and look in the rbtree for a free extent of a given size, but this |
| 7261 | * is a good start. |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7262 | * |
| 7263 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 7264 | * any of the information in this block group. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7265 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7266 | static noinline void |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7267 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 7268 | u64 num_bytes) |
| 7269 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7270 | struct btrfs_caching_control *caching_ctl; |
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 | caching_ctl = get_caching_control(cache); |
| 7273 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7274 | return; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7275 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7276 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7277 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7278 | |
| 7279 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7280 | } |
| 7281 | |
| 7282 | static noinline int |
| 7283 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 7284 | { |
| 7285 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7286 | int ret = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7287 | |
| 7288 | caching_ctl = get_caching_control(cache); |
| 7289 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7290 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7291 | |
| 7292 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7293 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 7294 | ret = -EIO; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7295 | put_caching_control(caching_ctl); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7296 | return ret; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7297 | } |
| 7298 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 7299 | int __get_raid_index(u64 flags) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7300 | { |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7301 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7302 | return BTRFS_RAID_RAID10; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7303 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7304 | return BTRFS_RAID_RAID1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7305 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7306 | return BTRFS_RAID_DUP; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7307 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 7308 | return BTRFS_RAID_RAID0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7309 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7310 | return BTRFS_RAID_RAID5; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7311 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7312 | return BTRFS_RAID_RAID6; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7313 | |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 7314 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7315 | } |
| 7316 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7317 | int get_block_group_index(struct btrfs_block_group_cache *cache) |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7318 | { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 7319 | return __get_raid_index(cache->flags); |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7320 | } |
| 7321 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7322 | static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = { |
| 7323 | [BTRFS_RAID_RAID10] = "raid10", |
| 7324 | [BTRFS_RAID_RAID1] = "raid1", |
| 7325 | [BTRFS_RAID_DUP] = "dup", |
| 7326 | [BTRFS_RAID_RAID0] = "raid0", |
| 7327 | [BTRFS_RAID_SINGLE] = "single", |
| 7328 | [BTRFS_RAID_RAID5] = "raid5", |
| 7329 | [BTRFS_RAID_RAID6] = "raid6", |
| 7330 | }; |
| 7331 | |
Jeff Mahoney | 1b8e5df | 2013-11-20 16:50:23 -0500 | [diff] [blame] | 7332 | static const char *get_raid_name(enum btrfs_raid_types type) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 7333 | { |
| 7334 | if (type >= BTRFS_NR_RAID_TYPES) |
| 7335 | return NULL; |
| 7336 | |
| 7337 | return btrfs_raid_type_names[type]; |
| 7338 | } |
| 7339 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7340 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7341 | LOOP_CACHING_NOWAIT = 0, |
| 7342 | LOOP_CACHING_WAIT = 1, |
| 7343 | LOOP_ALLOC_CHUNK = 2, |
| 7344 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7345 | }; |
| 7346 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7347 | static inline void |
| 7348 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 7349 | int delalloc) |
| 7350 | { |
| 7351 | if (delalloc) |
| 7352 | down_read(&cache->data_rwsem); |
| 7353 | } |
| 7354 | |
| 7355 | static inline void |
| 7356 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 7357 | int delalloc) |
| 7358 | { |
| 7359 | btrfs_get_block_group(cache); |
| 7360 | if (delalloc) |
| 7361 | down_read(&cache->data_rwsem); |
| 7362 | } |
| 7363 | |
| 7364 | static struct btrfs_block_group_cache * |
| 7365 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 7366 | struct btrfs_free_cluster *cluster, |
| 7367 | int delalloc) |
| 7368 | { |
Sudip Mukherjee | 89771cc | 2016-02-16 13:32:47 +0530 | [diff] [blame] | 7369 | struct btrfs_block_group_cache *used_bg = NULL; |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7370 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7371 | spin_lock(&cluster->refill_lock); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7372 | while (1) { |
| 7373 | used_bg = cluster->block_group; |
| 7374 | if (!used_bg) |
| 7375 | return NULL; |
| 7376 | |
| 7377 | if (used_bg == block_group) |
| 7378 | return used_bg; |
| 7379 | |
| 7380 | btrfs_get_block_group(used_bg); |
| 7381 | |
| 7382 | if (!delalloc) |
| 7383 | return used_bg; |
| 7384 | |
| 7385 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 7386 | return used_bg; |
| 7387 | |
| 7388 | spin_unlock(&cluster->refill_lock); |
| 7389 | |
Liu Bo | e321f8a | 2016-11-30 16:11:04 -0800 | [diff] [blame] | 7390 | /* We should only have one-level nested. */ |
| 7391 | down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING); |
Geert Uytterhoeven | 6719afd | 2014-06-22 14:30:09 +0200 | [diff] [blame] | 7392 | |
| 7393 | spin_lock(&cluster->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7394 | if (used_bg == cluster->block_group) |
| 7395 | return used_bg; |
| 7396 | |
| 7397 | up_read(&used_bg->data_rwsem); |
| 7398 | btrfs_put_block_group(used_bg); |
| 7399 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7400 | } |
| 7401 | |
| 7402 | static inline void |
| 7403 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 7404 | int delalloc) |
| 7405 | { |
| 7406 | if (delalloc) |
| 7407 | up_read(&cache->data_rwsem); |
| 7408 | btrfs_put_block_group(cache); |
| 7409 | } |
| 7410 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7411 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7412 | * walks the btree of allocated extents and find a hole of a given size. |
| 7413 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7414 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 7415 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7416 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7417 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7418 | * |
| 7419 | * If there is no suitable free space, we will record the max size of |
| 7420 | * the free space extent currently. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7421 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7422 | static noinline int find_free_extent(struct btrfs_root *orig_root, |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7423 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
| 7424 | u64 hint_byte, struct btrfs_key *ins, |
| 7425 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7426 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7427 | struct btrfs_fs_info *fs_info = orig_root->fs_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7428 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7429 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7430 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7431 | struct btrfs_block_group_cache *block_group = NULL; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7432 | u64 search_start = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7433 | u64 max_extent_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7434 | u64 empty_cluster = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7435 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7436 | int loop = 0; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7437 | int index = __get_raid_index(flags); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7438 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7439 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7440 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7441 | bool have_caching_bg = false; |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7442 | bool orig_have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7443 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7444 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7445 | WARN_ON(num_bytes < fs_info->sectorsize); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7446 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7447 | ins->objectid = 0; |
| 7448 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 7449 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7450 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7451 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7452 | space_info = __find_space_info(fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7453 | if (!space_info) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7454 | btrfs_err(fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7455 | return -ENOSPC; |
| 7456 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7457 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7458 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7459 | * If our free space is heavily fragmented we may not be able to make |
| 7460 | * big contiguous allocations, so instead of doing the expensive search |
| 7461 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 7462 | * can go ahead and search for a more manageable chunk. |
| 7463 | * |
| 7464 | * If our max_extent_size is large enough for our allocation simply |
| 7465 | * disable clustering since we will likely not be able to find enough |
| 7466 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7467 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7468 | if (unlikely(space_info->max_extent_size)) { |
| 7469 | spin_lock(&space_info->lock); |
| 7470 | if (space_info->max_extent_size && |
| 7471 | num_bytes > space_info->max_extent_size) { |
| 7472 | ins->offset = space_info->max_extent_size; |
| 7473 | spin_unlock(&space_info->lock); |
| 7474 | return -ENOSPC; |
| 7475 | } else if (space_info->max_extent_size) { |
| 7476 | use_cluster = false; |
| 7477 | } |
| 7478 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7479 | } |
| 7480 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7481 | last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7482 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7483 | spin_lock(&last_ptr->lock); |
| 7484 | if (last_ptr->block_group) |
| 7485 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7486 | if (last_ptr->fragmented) { |
| 7487 | /* |
| 7488 | * We still set window_start so we can keep track of the |
| 7489 | * last place we found an allocation to try and save |
| 7490 | * some time. |
| 7491 | */ |
| 7492 | hint_byte = last_ptr->window_start; |
| 7493 | use_cluster = false; |
| 7494 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7495 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7496 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7497 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7498 | search_start = max(search_start, first_logical_byte(fs_info, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7499 | search_start = max(search_start, hint_byte); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7500 | if (search_start == hint_byte) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7501 | block_group = btrfs_lookup_block_group(fs_info, search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7502 | /* |
| 7503 | * we don't want to use the block group if it doesn't match our |
| 7504 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7505 | * |
| 7506 | * However if we are re-searching with an ideal block group |
| 7507 | * 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] | 7508 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7509 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7510 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7511 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7512 | if (list_empty(&block_group->list) || |
| 7513 | block_group->ro) { |
| 7514 | /* |
| 7515 | * someone is removing this block group, |
| 7516 | * we can't jump into the have_block_group |
| 7517 | * target because our list pointers are not |
| 7518 | * valid |
| 7519 | */ |
| 7520 | btrfs_put_block_group(block_group); |
| 7521 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7522 | } else { |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7523 | index = get_block_group_index(block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7524 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7525 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7526 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7527 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7528 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7529 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7530 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7531 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7532 | have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7533 | if (index == 0 || index == __get_raid_index(flags)) |
| 7534 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7535 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7536 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 7537 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7538 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7539 | int cached; |
Chris Mason | 8a1413a2 | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 7540 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7541 | btrfs_grab_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7542 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7543 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7544 | /* |
| 7545 | * this can happen if we end up cycling through all the |
| 7546 | * raid types, but we want to make sure we only allocate |
| 7547 | * for the proper type. |
| 7548 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7549 | if (!block_group_bits(block_group, flags)) { |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7550 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 7551 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7552 | BTRFS_BLOCK_GROUP_RAID5 | |
| 7553 | BTRFS_BLOCK_GROUP_RAID6 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7554 | BTRFS_BLOCK_GROUP_RAID10; |
| 7555 | |
| 7556 | /* |
| 7557 | * if they asked for extra copies and this block group |
| 7558 | * doesn't provide them, bail. This does allow us to |
| 7559 | * fill raid0 from raid1. |
| 7560 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7561 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7562 | goto loop; |
| 7563 | } |
| 7564 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7565 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 7566 | cached = block_group_cache_done(block_group); |
| 7567 | if (unlikely(!cached)) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7568 | have_caching_bg = true; |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 7569 | ret = cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 7570 | BUG_ON(ret < 0); |
| 7571 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7572 | } |
| 7573 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7574 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 7575 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7576 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7577 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7578 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7579 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7580 | * Ok we want to try and use the cluster allocator, so |
| 7581 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7582 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7583 | if (last_ptr && use_cluster) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7584 | struct btrfs_block_group_cache *used_block_group; |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7585 | unsigned long aligned_cluster; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7586 | /* |
| 7587 | * the refill lock keeps out other |
| 7588 | * people trying to start a new cluster |
| 7589 | */ |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7590 | used_block_group = btrfs_lock_cluster(block_group, |
| 7591 | last_ptr, |
| 7592 | delalloc); |
| 7593 | if (!used_block_group) |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7594 | goto refill_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7595 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7596 | if (used_block_group != block_group && |
| 7597 | (used_block_group->ro || |
| 7598 | !block_group_bits(used_block_group, flags))) |
| 7599 | goto release_cluster; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7600 | |
| 7601 | offset = btrfs_alloc_from_cluster(used_block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7602 | last_ptr, |
| 7603 | num_bytes, |
| 7604 | used_block_group->key.objectid, |
| 7605 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7606 | if (offset) { |
| 7607 | /* we have a block, we're done */ |
| 7608 | spin_unlock(&last_ptr->refill_lock); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7609 | trace_btrfs_reserve_extent_cluster(fs_info, |
Miao Xie | 89d4346 | 2014-01-15 20:00:57 +0800 | [diff] [blame] | 7610 | used_block_group, |
| 7611 | search_start, num_bytes); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7612 | if (used_block_group != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7613 | btrfs_release_block_group(block_group, |
| 7614 | delalloc); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7615 | block_group = used_block_group; |
| 7616 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7617 | goto checks; |
| 7618 | } |
| 7619 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7620 | WARN_ON(last_ptr->block_group != used_block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7621 | release_cluster: |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7622 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 7623 | * set up a new clusters, so lets just skip it |
| 7624 | * and let the allocator find whatever block |
| 7625 | * it can find. If we reach this point, we |
| 7626 | * will have tried the cluster allocator |
| 7627 | * plenty of times and not have found |
| 7628 | * anything, so we are likely way too |
| 7629 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7630 | * anything. |
| 7631 | * |
| 7632 | * However, if the cluster is taken from the |
| 7633 | * current block group, release the cluster |
| 7634 | * first, so that we stand a better chance of |
| 7635 | * succeeding in the unclustered |
| 7636 | * allocation. */ |
| 7637 | if (loop >= LOOP_NO_EMPTY_SIZE && |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7638 | used_block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7639 | spin_unlock(&last_ptr->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7640 | btrfs_release_block_group(used_block_group, |
| 7641 | delalloc); |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7642 | goto unclustered_alloc; |
| 7643 | } |
| 7644 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7645 | /* |
| 7646 | * this cluster didn't work out, free it and |
| 7647 | * start over |
| 7648 | */ |
| 7649 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 7650 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7651 | if (used_block_group != block_group) |
| 7652 | btrfs_release_block_group(used_block_group, |
| 7653 | delalloc); |
| 7654 | refill_cluster: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7655 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 7656 | spin_unlock(&last_ptr->refill_lock); |
| 7657 | goto unclustered_alloc; |
| 7658 | } |
| 7659 | |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7660 | aligned_cluster = max_t(unsigned long, |
| 7661 | empty_cluster + empty_size, |
| 7662 | block_group->full_stripe_len); |
| 7663 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7664 | /* allocate a cluster in this block group */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7665 | ret = btrfs_find_space_cluster(fs_info, block_group, |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7666 | last_ptr, search_start, |
| 7667 | num_bytes, |
| 7668 | aligned_cluster); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7669 | if (ret == 0) { |
| 7670 | /* |
| 7671 | * now pull our allocation out of this |
| 7672 | * cluster |
| 7673 | */ |
| 7674 | offset = btrfs_alloc_from_cluster(block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7675 | last_ptr, |
| 7676 | num_bytes, |
| 7677 | search_start, |
| 7678 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7679 | if (offset) { |
| 7680 | /* we found one, proceed */ |
| 7681 | spin_unlock(&last_ptr->refill_lock); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7682 | trace_btrfs_reserve_extent_cluster(fs_info, |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7683 | block_group, search_start, |
| 7684 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7685 | goto checks; |
| 7686 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7687 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 7688 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7689 | spin_unlock(&last_ptr->refill_lock); |
| 7690 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7691 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7692 | wait_block_group_cache_progress(block_group, |
| 7693 | num_bytes + empty_cluster + empty_size); |
| 7694 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7695 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7696 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7697 | /* |
| 7698 | * at this point we either didn't find a cluster |
| 7699 | * or we weren't able to allocate a block from our |
| 7700 | * cluster. Free the cluster we've been trying |
| 7701 | * to use, and go to the next block group |
| 7702 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7703 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7704 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7705 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7706 | } |
| 7707 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7708 | unclustered_alloc: |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7709 | /* |
| 7710 | * We are doing an unclustered alloc, set the fragmented flag so |
| 7711 | * we don't bother trying to setup a cluster again until we get |
| 7712 | * more space. |
| 7713 | */ |
| 7714 | if (unlikely(last_ptr)) { |
| 7715 | spin_lock(&last_ptr->lock); |
| 7716 | last_ptr->fragmented = 1; |
| 7717 | spin_unlock(&last_ptr->lock); |
| 7718 | } |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7719 | spin_lock(&block_group->free_space_ctl->tree_lock); |
| 7720 | if (cached && |
| 7721 | block_group->free_space_ctl->free_space < |
| 7722 | num_bytes + empty_cluster + empty_size) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7723 | if (block_group->free_space_ctl->free_space > |
| 7724 | max_extent_size) |
| 7725 | max_extent_size = |
| 7726 | block_group->free_space_ctl->free_space; |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7727 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 7728 | goto loop; |
| 7729 | } |
| 7730 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 7731 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7732 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7733 | num_bytes, empty_size, |
| 7734 | &max_extent_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7735 | /* |
| 7736 | * If we didn't find a chunk, and we haven't failed on this |
| 7737 | * block group before, and this block group is in the middle of |
| 7738 | * caching and we are ok with waiting, then go ahead and wait |
| 7739 | * for progress to be made, and set failed_alloc to true. |
| 7740 | * |
| 7741 | * If failed_alloc is true then we've already waited on this |
| 7742 | * block group once and should move on to the next block group. |
| 7743 | */ |
| 7744 | if (!offset && !failed_alloc && !cached && |
| 7745 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7746 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7747 | num_bytes + empty_size); |
| 7748 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7749 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7750 | } else if (!offset) { |
| 7751 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7752 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7753 | checks: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7754 | search_start = ALIGN(offset, fs_info->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 7755 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7756 | /* move on to the next group */ |
| 7757 | if (search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7758 | block_group->key.objectid + block_group->key.offset) { |
| 7759 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7760 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7761 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7762 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7763 | if (offset < search_start) |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7764 | btrfs_add_free_space(block_group, offset, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7765 | search_start - offset); |
| 7766 | BUG_ON(offset > search_start); |
| 7767 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7768 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
| 7769 | num_bytes, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7770 | if (ret == -EAGAIN) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7771 | btrfs_add_free_space(block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7772 | goto loop; |
| 7773 | } |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7774 | btrfs_inc_block_group_reservations(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7775 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7776 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7777 | ins->objectid = search_start; |
| 7778 | ins->offset = num_bytes; |
| 7779 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 7780 | trace_btrfs_reserve_extent(fs_info, block_group, |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7781 | search_start, num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7782 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7783 | break; |
| 7784 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7785 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7786 | failed_alloc = false; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7787 | BUG_ON(index != get_block_group_index(block_group)); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7788 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7789 | } |
| 7790 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 7791 | |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7792 | if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg |
| 7793 | && !orig_have_caching_bg) |
| 7794 | orig_have_caching_bg = true; |
| 7795 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7796 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 7797 | goto search; |
| 7798 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7799 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 7800 | goto search; |
| 7801 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7802 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7803 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 7804 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7805 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 7806 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 7807 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 7808 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7809 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7810 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7811 | index = 0; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7812 | if (loop == LOOP_CACHING_NOWAIT) { |
| 7813 | /* |
| 7814 | * We want to skip the LOOP_CACHING_WAIT step if we |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 7815 | * don't have any uncached bgs and we've already done a |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7816 | * full search through. |
| 7817 | */ |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7818 | if (orig_have_caching_bg || !full_search) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7819 | loop = LOOP_CACHING_WAIT; |
| 7820 | else |
| 7821 | loop = LOOP_ALLOC_CHUNK; |
| 7822 | } else { |
| 7823 | loop++; |
| 7824 | } |
| 7825 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7826 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7827 | struct btrfs_trans_handle *trans; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7828 | int exist = 0; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7829 | |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7830 | trans = current->journal_info; |
| 7831 | if (trans) |
| 7832 | exist = 1; |
| 7833 | else |
| 7834 | trans = btrfs_join_transaction(root); |
| 7835 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7836 | if (IS_ERR(trans)) { |
| 7837 | ret = PTR_ERR(trans); |
| 7838 | goto out; |
| 7839 | } |
| 7840 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7841 | ret = do_chunk_alloc(trans, fs_info, flags, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7842 | CHUNK_ALLOC_FORCE); |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7843 | |
| 7844 | /* |
| 7845 | * If we can't allocate a new chunk we've already looped |
| 7846 | * through at least once, move on to the NO_EMPTY_SIZE |
| 7847 | * case. |
| 7848 | */ |
| 7849 | if (ret == -ENOSPC) |
| 7850 | loop = LOOP_NO_EMPTY_SIZE; |
| 7851 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7852 | /* |
| 7853 | * Do not bail out on ENOSPC since we |
| 7854 | * can do more things. |
| 7855 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7856 | if (ret < 0 && ret != -ENOSPC) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 7857 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7858 | else |
| 7859 | ret = 0; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7860 | if (!exist) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 7861 | btrfs_end_transaction(trans); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7862 | if (ret) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7863 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7864 | } |
| 7865 | |
| 7866 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7867 | /* |
| 7868 | * Don't loop again if we already have no empty_size and |
| 7869 | * no empty_cluster. |
| 7870 | */ |
| 7871 | if (empty_size == 0 && |
| 7872 | empty_cluster == 0) { |
| 7873 | ret = -ENOSPC; |
| 7874 | goto out; |
| 7875 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7876 | empty_size = 0; |
| 7877 | empty_cluster = 0; |
| 7878 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7879 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7880 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7881 | } else if (!ins->objectid) { |
| 7882 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7883 | } else if (ins->objectid) { |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7884 | if (!use_cluster && last_ptr) { |
| 7885 | spin_lock(&last_ptr->lock); |
| 7886 | last_ptr->window_start = ins->objectid; |
| 7887 | spin_unlock(&last_ptr->lock); |
| 7888 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7889 | ret = 0; |
| 7890 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7891 | out: |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7892 | if (ret == -ENOSPC) { |
| 7893 | spin_lock(&space_info->lock); |
| 7894 | space_info->max_extent_size = max_extent_size; |
| 7895 | spin_unlock(&space_info->lock); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7896 | ins->offset = max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7897 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 7898 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7899 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7900 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7901 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
| 7902 | struct btrfs_space_info *info, u64 bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7903 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7904 | { |
| 7905 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7906 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7907 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7908 | spin_lock(&info->lock); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7909 | btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull", |
| 7910 | info->flags, |
| 7911 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 7912 | info->bytes_reserved - info->bytes_readonly - |
| 7913 | info->bytes_may_use, (info->full) ? "" : "not "); |
| 7914 | btrfs_info(fs_info, |
| 7915 | "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu", |
| 7916 | info->total_bytes, info->bytes_used, info->bytes_pinned, |
| 7917 | info->bytes_reserved, info->bytes_may_use, |
| 7918 | info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7919 | spin_unlock(&info->lock); |
| 7920 | |
| 7921 | if (!dump_block_groups) |
| 7922 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7923 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7924 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7925 | again: |
| 7926 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7927 | spin_lock(&cache->lock); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7928 | btrfs_info(fs_info, |
| 7929 | "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s", |
| 7930 | cache->key.objectid, cache->key.offset, |
| 7931 | btrfs_block_group_used(&cache->item), cache->pinned, |
| 7932 | cache->reserved, cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7933 | btrfs_dump_free_space(cache, bytes); |
| 7934 | spin_unlock(&cache->lock); |
| 7935 | } |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7936 | if (++index < BTRFS_NR_RAID_TYPES) |
| 7937 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7938 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7939 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 7940 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7941 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7942 | u64 num_bytes, u64 min_alloc_size, |
| 7943 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7944 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7945 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7946 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 7947 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7948 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7949 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7950 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7951 | flags = btrfs_get_alloc_profile(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 7952 | again: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7953 | WARN_ON(num_bytes < fs_info->sectorsize); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7954 | ret = find_free_extent(root, ram_bytes, num_bytes, empty_size, |
| 7955 | hint_byte, ins, flags, delalloc); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7956 | if (!ret && !is_data) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7957 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
Filipe Manana | 9cfa3e3 | 2016-04-26 15:39:32 +0100 | [diff] [blame] | 7958 | } else if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7959 | if (!final_tried && ins->offset) { |
| 7960 | num_bytes = min(num_bytes >> 1, ins->offset); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 7961 | num_bytes = round_down(num_bytes, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7962 | fs_info->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7963 | num_bytes = max(num_bytes, min_alloc_size); |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 7964 | ram_bytes = num_bytes; |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7965 | if (num_bytes == min_alloc_size) |
| 7966 | final_tried = true; |
| 7967 | goto again; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7968 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7969 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7970 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7971 | sinfo = __find_space_info(fs_info, flags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7972 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 7973 | "allocation failed flags %llu, wanted %llu", |
| 7974 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 7975 | if (sinfo) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 7976 | dump_space_info(fs_info, sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7977 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7978 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7979 | |
| 7980 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7981 | } |
| 7982 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7983 | static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7984 | u64 start, u64 len, |
| 7985 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7986 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7987 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7988 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7989 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7990 | cache = btrfs_lookup_block_group(fs_info, start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7991 | if (!cache) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7992 | btrfs_err(fs_info, "Unable to find block group for %llu", |
| 7993 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7994 | return -ENOSPC; |
| 7995 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7996 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7997 | if (pin) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7998 | pin_down_extent(fs_info, cache, start, len, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7999 | else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8000 | if (btrfs_test_opt(fs_info, DISCARD)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8001 | ret = btrfs_discard_extent(fs_info, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8002 | btrfs_add_free_space(cache, start, len); |
Wang Xiaoguang | 4824f1f | 2016-07-25 15:51:39 +0800 | [diff] [blame] | 8003 | btrfs_free_reserved_bytes(cache, len, delalloc); |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8004 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8005 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 8006 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8007 | btrfs_put_block_group(cache); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8008 | return ret; |
| 8009 | } |
| 8010 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8011 | int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8012 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8013 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8014 | return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8015 | } |
| 8016 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8017 | 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] | 8018 | u64 start, u64 len) |
| 8019 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8020 | return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 8021 | } |
| 8022 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8023 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8024 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8025 | u64 parent, u64 root_objectid, |
| 8026 | u64 flags, u64 owner, u64 offset, |
| 8027 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8028 | { |
| 8029 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8030 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8031 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8032 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8033 | struct extent_buffer *leaf; |
| 8034 | int type; |
| 8035 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 8036 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8037 | if (parent > 0) |
| 8038 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 8039 | else |
| 8040 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 8041 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8042 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 8043 | |
| 8044 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 8045 | if (!path) |
| 8046 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8047 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 8048 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8049 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 8050 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8051 | if (ret) { |
| 8052 | btrfs_free_path(path); |
| 8053 | return ret; |
| 8054 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8055 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8056 | leaf = path->nodes[0]; |
| 8057 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8058 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8059 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 8060 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 8061 | btrfs_set_extent_flags(leaf, extent_item, |
| 8062 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8063 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8064 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 8065 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 8066 | if (parent > 0) { |
| 8067 | struct btrfs_shared_data_ref *ref; |
| 8068 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 8069 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 8070 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 8071 | } else { |
| 8072 | struct btrfs_extent_data_ref *ref; |
| 8073 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 8074 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 8075 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 8076 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 8077 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 8078 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 8079 | |
| 8080 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 8081 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 8082 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8083 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
| 8084 | ins->offset); |
| 8085 | if (ret) |
| 8086 | return ret; |
| 8087 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 8088 | ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8089 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8090 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8091 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 8092 | BUG(); |
| 8093 | } |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8094 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8095 | return ret; |
| 8096 | } |
| 8097 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8098 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8099 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8100 | u64 parent, u64 root_objectid, |
| 8101 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8102 | int level, struct btrfs_key *ins) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8103 | { |
| 8104 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8105 | struct btrfs_extent_item *extent_item; |
| 8106 | struct btrfs_tree_block_info *block_info; |
| 8107 | struct btrfs_extent_inline_ref *iref; |
| 8108 | struct btrfs_path *path; |
| 8109 | struct extent_buffer *leaf; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8110 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 8111 | u64 num_bytes = ins->offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8112 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8113 | |
| 8114 | if (!skinny_metadata) |
| 8115 | size += sizeof(*block_info); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8116 | |
| 8117 | path = btrfs_alloc_path(); |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8118 | if (!path) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8119 | btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8120 | fs_info->nodesize); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8121 | return -ENOMEM; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 8122 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8123 | |
| 8124 | path->leave_spinning = 1; |
| 8125 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 8126 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8127 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 8128 | btrfs_free_path(path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8129 | btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8130 | fs_info->nodesize); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8131 | return ret; |
| 8132 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8133 | |
| 8134 | leaf = path->nodes[0]; |
| 8135 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 8136 | struct btrfs_extent_item); |
| 8137 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 8138 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 8139 | btrfs_set_extent_flags(leaf, extent_item, |
| 8140 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8141 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8142 | if (skinny_metadata) { |
| 8143 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8144 | num_bytes = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8145 | } else { |
| 8146 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 8147 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 8148 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 8149 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 8150 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8151 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8152 | if (parent > 0) { |
| 8153 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 8154 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 8155 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 8156 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 8157 | } else { |
| 8158 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 8159 | BTRFS_TREE_BLOCK_REF_KEY); |
| 8160 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 8161 | } |
| 8162 | |
| 8163 | btrfs_mark_buffer_dirty(leaf); |
| 8164 | btrfs_free_path(path); |
| 8165 | |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 8166 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
| 8167 | num_bytes); |
| 8168 | if (ret) |
| 8169 | return ret; |
| 8170 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 8171 | ret = update_block_group(trans, fs_info, ins->objectid, |
| 8172 | fs_info->nodesize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8173 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8174 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 8175 | ins->objectid, ins->offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8176 | BUG(); |
| 8177 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 8178 | |
Jeff Mahoney | 71ff643 | 2016-09-06 16:00:42 -0400 | [diff] [blame] | 8179 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8180 | fs_info->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8181 | return ret; |
| 8182 | } |
| 8183 | |
| 8184 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8185 | u64 root_objectid, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 8186 | u64 offset, u64 ram_bytes, |
| 8187 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8188 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8189 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8190 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 8191 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8192 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 8193 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8194 | ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8195 | ins->offset, 0, |
| 8196 | root_objectid, owner, offset, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 8197 | ram_bytes, BTRFS_ADD_DELAYED_EXTENT, |
| 8198 | NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 8199 | return ret; |
| 8200 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8201 | |
| 8202 | /* |
| 8203 | * this is used by the tree logging recovery code. It records that |
| 8204 | * an extent has been allocated and makes sure to clear the free |
| 8205 | * space cache bits as well |
| 8206 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8207 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8208 | struct btrfs_fs_info *fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8209 | u64 root_objectid, u64 owner, u64 offset, |
| 8210 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8211 | { |
| 8212 | int ret; |
| 8213 | struct btrfs_block_group_cache *block_group; |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 8214 | struct btrfs_space_info *space_info; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8215 | |
| 8216 | /* |
| 8217 | * 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] | 8218 | * need to do the exclude dance if this fs isn't mixed. |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8219 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8220 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8221 | ret = __exclude_logged_extent(fs_info, ins->objectid, |
| 8222 | ins->offset); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8223 | if (ret) |
| 8224 | return ret; |
| 8225 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8226 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8227 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 8228 | if (!block_group) |
| 8229 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8230 | |
Wang Xiaoguang | ed7a694 | 2016-08-26 11:33:14 +0800 | [diff] [blame] | 8231 | space_info = block_group->space_info; |
| 8232 | spin_lock(&space_info->lock); |
| 8233 | spin_lock(&block_group->lock); |
| 8234 | space_info->bytes_reserved += ins->offset; |
| 8235 | block_group->reserved += ins->offset; |
| 8236 | spin_unlock(&block_group->lock); |
| 8237 | spin_unlock(&space_info->lock); |
| 8238 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8239 | ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8240 | 0, owner, offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 8241 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8242 | return ret; |
| 8243 | } |
| 8244 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 8245 | static struct extent_buffer * |
| 8246 | 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] | 8247 | u64 bytenr, int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8248 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8249 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8250 | struct extent_buffer *buf; |
| 8251 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8252 | buf = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 8253 | if (IS_ERR(buf)) |
| 8254 | return buf; |
| 8255 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8256 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 8257 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8258 | btrfs_tree_lock(buf); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8259 | clean_tree_block(trans, fs_info, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 8260 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8261 | |
| 8262 | btrfs_set_lock_blocking(buf); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 8263 | set_extent_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8264 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8265 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8266 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 8267 | /* |
| 8268 | * we allow two log transactions at a time, use different |
| 8269 | * EXENT bit to differentiate dirty pages. |
| 8270 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8271 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 8272 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 8273 | buf->start + buf->len - 1, GFP_NOFS); |
| 8274 | else |
| 8275 | set_extent_new(&root->dirty_log_pages, buf->start, |
David Sterba | 3744dbe | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 8276 | buf->start + buf->len - 1); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8277 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 8278 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 8279 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 8280 | buf->start + buf->len - 1, GFP_NOFS); |
| 8281 | } |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 8282 | trans->dirty = true; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 8283 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 8284 | return buf; |
| 8285 | } |
| 8286 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8287 | static struct btrfs_block_rsv * |
| 8288 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 8289 | struct btrfs_root *root, u32 blocksize) |
| 8290 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8291 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8292 | struct btrfs_block_rsv *block_rsv; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8293 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8294 | int ret; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8295 | bool global_updated = false; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8296 | |
| 8297 | block_rsv = get_block_rsv(trans, root); |
| 8298 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8299 | if (unlikely(block_rsv->size == 0)) |
| 8300 | goto try_reserve; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8301 | again: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8302 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 8303 | if (!ret) |
| 8304 | return block_rsv; |
| 8305 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8306 | if (block_rsv->failfast) |
| 8307 | return ERR_PTR(ret); |
| 8308 | |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8309 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
| 8310 | global_updated = true; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8311 | update_global_block_rsv(fs_info); |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 8312 | goto again; |
| 8313 | } |
| 8314 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8315 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8316 | static DEFINE_RATELIMIT_STATE(_rs, |
| 8317 | DEFAULT_RATELIMIT_INTERVAL * 10, |
| 8318 | /*DEFAULT_RATELIMIT_BURST*/ 1); |
| 8319 | if (__ratelimit(&_rs)) |
| 8320 | WARN(1, KERN_DEBUG |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 8321 | "BTRFS: block rsv returned %d\n", ret); |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8322 | } |
| 8323 | try_reserve: |
| 8324 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, |
| 8325 | BTRFS_RESERVE_NO_FLUSH); |
| 8326 | if (!ret) |
| 8327 | return block_rsv; |
| 8328 | /* |
| 8329 | * If we couldn't reserve metadata bytes try and use some from |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 8330 | * the global reserve if its space type is the same as the global |
| 8331 | * reservation. |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8332 | */ |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 8333 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
| 8334 | block_rsv->space_info == global_rsv->space_info) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 8335 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 8336 | if (!ret) |
| 8337 | return global_rsv; |
| 8338 | } |
| 8339 | return ERR_PTR(ret); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8340 | } |
| 8341 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 8342 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 8343 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8344 | { |
| 8345 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 8346 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8347 | } |
| 8348 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8349 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8350 | * 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] | 8351 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8352 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 8353 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
| 8354 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8355 | u64 parent, u64 root_objectid, |
| 8356 | struct btrfs_disk_key *key, int level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 8357 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8358 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8359 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 8360 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8361 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8362 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8363 | struct btrfs_delayed_extent_op *extent_op; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8364 | u64 flags = 0; |
| 8365 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8366 | u32 blocksize = fs_info->nodesize; |
| 8367 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8368 | |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 8369 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8370 | if (btrfs_is_testing(fs_info)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 8371 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8372 | level); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 8373 | if (!IS_ERR(buf)) |
| 8374 | root->alloc_bytenr += blocksize; |
| 8375 | return buf; |
| 8376 | } |
David Sterba | 05653ef | 2016-07-15 15:23:37 +0200 | [diff] [blame] | 8377 | #endif |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 8378 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8379 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 8380 | if (IS_ERR(block_rsv)) |
| 8381 | return ERR_CAST(block_rsv); |
| 8382 | |
Wang Xiaoguang | 1851309 | 2016-07-25 15:51:40 +0800 | [diff] [blame] | 8383 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 8384 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8385 | if (ret) |
| 8386 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 8387 | |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 8388 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8389 | if (IS_ERR(buf)) { |
| 8390 | ret = PTR_ERR(buf); |
| 8391 | goto out_free_reserved; |
| 8392 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8393 | |
| 8394 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 8395 | if (parent == 0) |
| 8396 | parent = ins.objectid; |
| 8397 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8398 | } else |
| 8399 | BUG_ON(parent > 0); |
| 8400 | |
| 8401 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 8402 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8403 | if (!extent_op) { |
| 8404 | ret = -ENOMEM; |
| 8405 | goto out_free_buf; |
| 8406 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8407 | if (key) |
| 8408 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 8409 | else |
| 8410 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 8411 | extent_op->flags_to_set = flags; |
David Sterba | 35b3ad5 | 2015-11-30 16:51:29 +0100 | [diff] [blame] | 8412 | extent_op->update_key = skinny_metadata ? false : true; |
| 8413 | extent_op->update_flags = true; |
| 8414 | extent_op->is_data = false; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8415 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8416 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8417 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8418 | ins.objectid, ins.offset, |
| 8419 | parent, root_objectid, level, |
| 8420 | BTRFS_ADD_DELAYED_EXTENT, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8421 | extent_op); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8422 | if (ret) |
| 8423 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8424 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8425 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8426 | |
| 8427 | out_free_delayed: |
| 8428 | btrfs_free_delayed_extent_op(extent_op); |
| 8429 | out_free_buf: |
| 8430 | free_extent_buffer(buf); |
| 8431 | out_free_reserved: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8432 | btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8433 | out_unuse: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8434 | unuse_block_rsv(fs_info, block_rsv, blocksize); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8435 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8436 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 8437 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8438 | struct walk_control { |
| 8439 | u64 refs[BTRFS_MAX_LEVEL]; |
| 8440 | u64 flags[BTRFS_MAX_LEVEL]; |
| 8441 | struct btrfs_key update_progress; |
| 8442 | int stage; |
| 8443 | int level; |
| 8444 | int shared_level; |
| 8445 | int update_ref; |
| 8446 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8447 | int reada_slot; |
| 8448 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8449 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8450 | }; |
| 8451 | |
| 8452 | #define DROP_REFERENCE 1 |
| 8453 | #define UPDATE_BACKREF 2 |
| 8454 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8455 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 8456 | struct btrfs_root *root, |
| 8457 | struct walk_control *wc, |
| 8458 | struct btrfs_path *path) |
| 8459 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8460 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8461 | u64 bytenr; |
| 8462 | u64 generation; |
| 8463 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8464 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8465 | u32 nritems; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8466 | struct btrfs_key key; |
| 8467 | struct extent_buffer *eb; |
| 8468 | int ret; |
| 8469 | int slot; |
| 8470 | int nread = 0; |
| 8471 | |
| 8472 | if (path->slots[wc->level] < wc->reada_slot) { |
| 8473 | wc->reada_count = wc->reada_count * 2 / 3; |
| 8474 | wc->reada_count = max(wc->reada_count, 2); |
| 8475 | } else { |
| 8476 | wc->reada_count = wc->reada_count * 3 / 2; |
| 8477 | wc->reada_count = min_t(int, wc->reada_count, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8478 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8479 | } |
| 8480 | |
| 8481 | eb = path->nodes[wc->level]; |
| 8482 | nritems = btrfs_header_nritems(eb); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8483 | |
| 8484 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 8485 | if (nread >= wc->reada_count) |
| 8486 | break; |
| 8487 | |
| 8488 | cond_resched(); |
| 8489 | bytenr = btrfs_node_blockptr(eb, slot); |
| 8490 | generation = btrfs_node_ptr_generation(eb, slot); |
| 8491 | |
| 8492 | if (slot == path->slots[wc->level]) |
| 8493 | goto reada; |
| 8494 | |
| 8495 | if (wc->stage == UPDATE_BACKREF && |
| 8496 | generation <= root->root_key.offset) |
| 8497 | continue; |
| 8498 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8499 | /* 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] | 8500 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8501 | wc->level - 1, 1, &refs, |
| 8502 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8503 | /* We don't care about errors in readahead. */ |
| 8504 | if (ret < 0) |
| 8505 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8506 | BUG_ON(refs == 0); |
| 8507 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8508 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8509 | if (refs == 1) |
| 8510 | goto reada; |
| 8511 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8512 | if (wc->level == 1 && |
| 8513 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8514 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8515 | if (!wc->update_ref || |
| 8516 | generation <= root->root_key.offset) |
| 8517 | continue; |
| 8518 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 8519 | ret = btrfs_comp_cpu_keys(&key, |
| 8520 | &wc->update_progress); |
| 8521 | if (ret < 0) |
| 8522 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8523 | } else { |
| 8524 | if (wc->level == 1 && |
| 8525 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8526 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8527 | } |
| 8528 | reada: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8529 | readahead_tree_block(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8530 | nread++; |
| 8531 | } |
| 8532 | wc->reada_slot = slot; |
| 8533 | } |
| 8534 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8535 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8536 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8537 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8538 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 8539 | * back refs for pointers in the block. |
| 8540 | * |
| 8541 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8542 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8543 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 8544 | struct btrfs_root *root, |
| 8545 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8546 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8547 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8548 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8549 | int level = wc->level; |
| 8550 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8551 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8552 | int ret; |
| 8553 | |
| 8554 | if (wc->stage == UPDATE_BACKREF && |
| 8555 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 8556 | return 1; |
| 8557 | |
| 8558 | /* |
| 8559 | * when reference count of tree block is 1, it won't increase |
| 8560 | * again. once full backref flag is set, we never clear it. |
| 8561 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8562 | if (lookup_info && |
| 8563 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 8564 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8565 | BUG_ON(!path->locks[level]); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8566 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8567 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8568 | &wc->refs[level], |
| 8569 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8570 | BUG_ON(ret == -ENOMEM); |
| 8571 | if (ret) |
| 8572 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8573 | BUG_ON(wc->refs[level] == 0); |
| 8574 | } |
| 8575 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8576 | if (wc->stage == DROP_REFERENCE) { |
| 8577 | if (wc->refs[level] > 1) |
| 8578 | return 1; |
| 8579 | |
| 8580 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8581 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8582 | path->locks[level] = 0; |
| 8583 | } |
| 8584 | return 0; |
| 8585 | } |
| 8586 | |
| 8587 | /* wc->stage == UPDATE_BACKREF */ |
| 8588 | if (!(wc->flags[level] & flag)) { |
| 8589 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8590 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8591 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8592 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8593 | BUG_ON(ret); /* -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8594 | ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8595 | eb->len, flag, |
| 8596 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8597 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8598 | wc->flags[level] |= flag; |
| 8599 | } |
| 8600 | |
| 8601 | /* |
| 8602 | * the block is shared by multiple trees, so it's not good to |
| 8603 | * keep the tree lock |
| 8604 | */ |
| 8605 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8606 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8607 | path->locks[level] = 0; |
| 8608 | } |
| 8609 | return 0; |
| 8610 | } |
| 8611 | |
| 8612 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8613 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8614 | * |
| 8615 | * when wc->stage == DROP_REFERENCE, this function checks |
| 8616 | * reference count of the block pointed to. if the block |
| 8617 | * is shared and we need update back refs for the subtree |
| 8618 | * rooted at the block, this function changes wc->stage to |
| 8619 | * UPDATE_BACKREF. if the block is shared and there is no |
| 8620 | * need to update back, this function drops the reference |
| 8621 | * to the block. |
| 8622 | * |
| 8623 | * NOTE: return value 1 means we should stop walking down. |
| 8624 | */ |
| 8625 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 8626 | struct btrfs_root *root, |
| 8627 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8628 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8629 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8630 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8631 | u64 bytenr; |
| 8632 | u64 generation; |
| 8633 | u64 parent; |
| 8634 | u32 blocksize; |
| 8635 | struct btrfs_key key; |
| 8636 | struct extent_buffer *next; |
| 8637 | int level = wc->level; |
| 8638 | int reada = 0; |
| 8639 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8640 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8641 | |
| 8642 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 8643 | path->slots[level]); |
| 8644 | /* |
| 8645 | * if the lower level block was created before the snapshot |
| 8646 | * was created, we know there is no need to update back refs |
| 8647 | * for the subtree |
| 8648 | */ |
| 8649 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8650 | generation <= root->root_key.offset) { |
| 8651 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8652 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8653 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8654 | |
| 8655 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8656 | blocksize = fs_info->nodesize; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8657 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8658 | next = find_extent_buffer(fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8659 | if (!next) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8660 | next = btrfs_find_create_tree_block(fs_info, bytenr); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 8661 | if (IS_ERR(next)) |
| 8662 | return PTR_ERR(next); |
| 8663 | |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 8664 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 8665 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8666 | reada = 1; |
| 8667 | } |
| 8668 | btrfs_tree_lock(next); |
| 8669 | btrfs_set_lock_blocking(next); |
| 8670 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8671 | ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8672 | &wc->refs[level - 1], |
| 8673 | &wc->flags[level - 1]); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8674 | if (ret < 0) |
| 8675 | goto out_unlock; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8676 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8677 | if (unlikely(wc->refs[level - 1] == 0)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8678 | btrfs_err(fs_info, "Missing references."); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8679 | ret = -EIO; |
| 8680 | goto out_unlock; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8681 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8682 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8683 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8684 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8685 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8686 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8687 | if (level == 1 && |
| 8688 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8689 | goto skip; |
| 8690 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8691 | if (!wc->update_ref || |
| 8692 | generation <= root->root_key.offset) |
| 8693 | goto skip; |
| 8694 | |
| 8695 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 8696 | path->slots[level]); |
| 8697 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 8698 | if (ret < 0) |
| 8699 | goto skip; |
| 8700 | |
| 8701 | wc->stage = UPDATE_BACKREF; |
| 8702 | wc->shared_level = level - 1; |
| 8703 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8704 | } else { |
| 8705 | if (level == 1 && |
| 8706 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8707 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8708 | } |
| 8709 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 8710 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8711 | btrfs_tree_unlock(next); |
| 8712 | free_extent_buffer(next); |
| 8713 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8714 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8715 | } |
| 8716 | |
| 8717 | if (!next) { |
| 8718 | if (reada && level == 1) |
| 8719 | reada_walk_down(trans, root, wc, path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8720 | next = read_tree_block(fs_info, bytenr, generation); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 8721 | if (IS_ERR(next)) { |
| 8722 | return PTR_ERR(next); |
| 8723 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8724 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 8725 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8726 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8727 | btrfs_tree_lock(next); |
| 8728 | btrfs_set_lock_blocking(next); |
| 8729 | } |
| 8730 | |
| 8731 | level--; |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8732 | ASSERT(level == btrfs_header_level(next)); |
| 8733 | if (level != btrfs_header_level(next)) { |
| 8734 | btrfs_err(root->fs_info, "mismatched level"); |
| 8735 | ret = -EIO; |
| 8736 | goto out_unlock; |
| 8737 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8738 | path->nodes[level] = next; |
| 8739 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8740 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8741 | wc->level = level; |
| 8742 | if (wc->level == 1) |
| 8743 | wc->reada_slot = 0; |
| 8744 | return 0; |
| 8745 | skip: |
| 8746 | wc->refs[level - 1] = 0; |
| 8747 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8748 | if (wc->stage == DROP_REFERENCE) { |
| 8749 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 8750 | parent = path->nodes[level]->start; |
| 8751 | } else { |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8752 | ASSERT(root->root_key.objectid == |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8753 | btrfs_header_owner(path->nodes[level])); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8754 | if (root->root_key.objectid != |
| 8755 | btrfs_header_owner(path->nodes[level])) { |
| 8756 | btrfs_err(root->fs_info, |
| 8757 | "mismatched block owner"); |
| 8758 | ret = -EIO; |
| 8759 | goto out_unlock; |
| 8760 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8761 | parent = 0; |
| 8762 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8763 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8764 | if (need_account) { |
Qu Wenruo | 33d1f05 | 2016-10-18 09:31:28 +0800 | [diff] [blame] | 8765 | ret = btrfs_qgroup_trace_subtree(trans, root, next, |
| 8766 | generation, level - 1); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8767 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8768 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8769 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
| 8770 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8771 | } |
| 8772 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8773 | ret = btrfs_free_extent(trans, fs_info, bytenr, blocksize, |
| 8774 | parent, root->root_key.objectid, |
| 8775 | level - 1, 0); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8776 | if (ret) |
| 8777 | goto out_unlock; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8778 | } |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8779 | |
| 8780 | *lookup_info = 1; |
| 8781 | ret = 1; |
| 8782 | |
| 8783 | out_unlock: |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8784 | btrfs_tree_unlock(next); |
| 8785 | free_extent_buffer(next); |
Josef Bacik | 4867268 | 2016-09-23 13:23:28 +0200 | [diff] [blame] | 8786 | |
| 8787 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8791 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8792 | * |
| 8793 | * when wc->stage == DROP_REFERENCE, this function drops |
| 8794 | * reference count on the block. |
| 8795 | * |
| 8796 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 8797 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 8798 | * to UPDATE_BACKREF previously while processing the block. |
| 8799 | * |
| 8800 | * NOTE: return value 1 means we should stop walking up. |
| 8801 | */ |
| 8802 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 8803 | struct btrfs_root *root, |
| 8804 | struct btrfs_path *path, |
| 8805 | struct walk_control *wc) |
| 8806 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8807 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8808 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8809 | int level = wc->level; |
| 8810 | struct extent_buffer *eb = path->nodes[level]; |
| 8811 | u64 parent = 0; |
| 8812 | |
| 8813 | if (wc->stage == UPDATE_BACKREF) { |
| 8814 | BUG_ON(wc->shared_level < level); |
| 8815 | if (level < wc->shared_level) |
| 8816 | goto out; |
| 8817 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8818 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 8819 | if (ret > 0) |
| 8820 | wc->update_ref = 0; |
| 8821 | |
| 8822 | wc->stage = DROP_REFERENCE; |
| 8823 | wc->shared_level = -1; |
| 8824 | path->slots[level] = 0; |
| 8825 | |
| 8826 | /* |
| 8827 | * check reference count again if the block isn't locked. |
| 8828 | * we should start walking down the tree again if reference |
| 8829 | * count is one. |
| 8830 | */ |
| 8831 | if (!path->locks[level]) { |
| 8832 | BUG_ON(level == 0); |
| 8833 | btrfs_tree_lock(eb); |
| 8834 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8835 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8836 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8837 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8838 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8839 | &wc->refs[level], |
| 8840 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8841 | if (ret < 0) { |
| 8842 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8843 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8844 | return ret; |
| 8845 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8846 | BUG_ON(wc->refs[level] == 0); |
| 8847 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8848 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8849 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8850 | return 1; |
| 8851 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8852 | } |
| 8853 | } |
| 8854 | |
| 8855 | /* wc->stage == DROP_REFERENCE */ |
| 8856 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 8857 | |
| 8858 | if (wc->refs[level] == 1) { |
| 8859 | if (level == 0) { |
| 8860 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8861 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8862 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8863 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8864 | BUG_ON(ret); /* -ENOMEM */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 8865 | ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8866 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8867 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 8868 | "error %d accounting leaf items. Quota is out of sync, rescan required.", |
| 8869 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8870 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8871 | } |
| 8872 | /* make block locked assertion in clean_tree_block happy */ |
| 8873 | if (!path->locks[level] && |
| 8874 | btrfs_header_generation(eb) == trans->transid) { |
| 8875 | btrfs_tree_lock(eb); |
| 8876 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8877 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8878 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 8879 | clean_tree_block(trans, fs_info, eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8880 | } |
| 8881 | |
| 8882 | if (eb == root->node) { |
| 8883 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8884 | parent = eb->start; |
| 8885 | else |
| 8886 | BUG_ON(root->root_key.objectid != |
| 8887 | btrfs_header_owner(eb)); |
| 8888 | } else { |
| 8889 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8890 | parent = path->nodes[level + 1]->start; |
| 8891 | else |
| 8892 | BUG_ON(root->root_key.objectid != |
| 8893 | btrfs_header_owner(path->nodes[level + 1])); |
| 8894 | } |
| 8895 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 8896 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8897 | out: |
| 8898 | wc->refs[level] = 0; |
| 8899 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8900 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8901 | } |
| 8902 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8903 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 8904 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8905 | struct btrfs_path *path, |
| 8906 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8907 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8908 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8909 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8910 | int ret; |
| 8911 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8912 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8913 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8914 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8915 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8916 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8917 | if (level == 0) |
| 8918 | break; |
| 8919 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 8920 | if (path->slots[level] >= |
| 8921 | btrfs_header_nritems(path->nodes[level])) |
| 8922 | break; |
| 8923 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8924 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8925 | if (ret > 0) { |
| 8926 | path->slots[level]++; |
| 8927 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 8928 | } else if (ret < 0) |
| 8929 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8930 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8931 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8932 | return 0; |
| 8933 | } |
| 8934 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8935 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 8936 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8937 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8938 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8939 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8940 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8941 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8942 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8943 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 8944 | while (level < max_level && path->nodes[level]) { |
| 8945 | wc->level = level; |
| 8946 | if (path->slots[level] + 1 < |
| 8947 | btrfs_header_nritems(path->nodes[level])) { |
| 8948 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8949 | return 0; |
| 8950 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8951 | ret = walk_up_proc(trans, root, path, wc); |
| 8952 | if (ret > 0) |
| 8953 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 8954 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8955 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8956 | btrfs_tree_unlock_rw(path->nodes[level], |
| 8957 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8958 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8959 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8960 | free_extent_buffer(path->nodes[level]); |
| 8961 | path->nodes[level] = NULL; |
| 8962 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8963 | } |
| 8964 | } |
| 8965 | return 1; |
| 8966 | } |
| 8967 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8968 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8969 | * drop a subvolume tree. |
| 8970 | * |
| 8971 | * this function traverses the tree freeing any blocks that only |
| 8972 | * referenced by the tree. |
| 8973 | * |
| 8974 | * when a shared tree block is found. this function decreases its |
| 8975 | * reference count by one. if update_ref is true, this function |
| 8976 | * also make sure backrefs for the shared block and all lower level |
| 8977 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 8978 | * |
| 8979 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8980 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 8981 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8982 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 8983 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8984 | { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8985 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8986 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8987 | struct btrfs_trans_handle *trans; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8988 | struct btrfs_root *tree_root = fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8989 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8990 | struct walk_control *wc; |
| 8991 | struct btrfs_key key; |
| 8992 | int err = 0; |
| 8993 | int ret; |
| 8994 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8995 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8996 | |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 8997 | btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8998 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8999 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9000 | if (!path) { |
| 9001 | err = -ENOMEM; |
| 9002 | goto out; |
| 9003 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9004 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9005 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 9006 | if (!wc) { |
| 9007 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9008 | err = -ENOMEM; |
| 9009 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 9010 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9011 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9012 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9013 | if (IS_ERR(trans)) { |
| 9014 | err = PTR_ERR(trans); |
| 9015 | goto out_free; |
| 9016 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 9017 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9018 | if (block_rsv) |
| 9019 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9020 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9021 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9022 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9023 | path->nodes[level] = btrfs_lock_root_node(root); |
| 9024 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9025 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9026 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9027 | memset(&wc->update_progress, 0, |
| 9028 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9029 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9030 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9031 | memcpy(&wc->update_progress, &key, |
| 9032 | sizeof(wc->update_progress)); |
| 9033 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 9034 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9035 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 9036 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9037 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 9038 | path->lowest_level = 0; |
| 9039 | if (ret < 0) { |
| 9040 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9041 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 9042 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 9043 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9044 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9045 | /* |
| 9046 | * unlock our path, this is safe because only this |
| 9047 | * function is allowed to delete this snapshot |
| 9048 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9049 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 9050 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9051 | level = btrfs_header_level(root->node); |
| 9052 | while (1) { |
| 9053 | btrfs_tree_lock(path->nodes[level]); |
| 9054 | btrfs_set_lock_blocking(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 9055 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9056 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9057 | ret = btrfs_lookup_extent_info(trans, fs_info, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9058 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 9059 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9060 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9061 | if (ret < 0) { |
| 9062 | err = ret; |
| 9063 | goto out_end_trans; |
| 9064 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9065 | BUG_ON(wc->refs[level] == 0); |
| 9066 | |
| 9067 | if (level == root_item->drop_level) |
| 9068 | break; |
| 9069 | |
| 9070 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 9071 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9072 | WARN_ON(wc->refs[level] != 1); |
| 9073 | level--; |
| 9074 | } |
| 9075 | } |
| 9076 | |
| 9077 | wc->level = level; |
| 9078 | wc->shared_level = -1; |
| 9079 | wc->stage = DROP_REFERENCE; |
| 9080 | wc->update_ref = update_ref; |
| 9081 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9082 | wc->for_reloc = for_reloc; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9083 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9084 | |
| 9085 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 9086 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9087 | ret = walk_down_tree(trans, root, path, wc); |
| 9088 | if (ret < 0) { |
| 9089 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 9090 | break; |
| 9091 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9092 | |
| 9093 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 9094 | if (ret < 0) { |
| 9095 | err = ret; |
| 9096 | break; |
| 9097 | } |
| 9098 | |
| 9099 | if (ret > 0) { |
| 9100 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 9101 | break; |
| 9102 | } |
| 9103 | |
| 9104 | if (wc->stage == DROP_REFERENCE) { |
| 9105 | level = wc->level; |
| 9106 | btrfs_node_key(path->nodes[level], |
| 9107 | &root_item->drop_progress, |
| 9108 | path->slots[level]); |
| 9109 | root_item->drop_level = level; |
| 9110 | } |
| 9111 | |
| 9112 | BUG_ON(wc->level == 0); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9113 | if (btrfs_should_end_transaction(trans) || |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9114 | (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9115 | ret = btrfs_update_root(trans, tree_root, |
| 9116 | &root->root_key, |
| 9117 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9118 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9119 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9120 | err = ret; |
| 9121 | goto out_end_trans; |
| 9122 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9123 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9124 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9125 | if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9126 | btrfs_debug(fs_info, |
| 9127 | "drop snapshot early exit"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 9128 | err = -EAGAIN; |
| 9129 | goto out_free; |
| 9130 | } |
| 9131 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 9132 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9133 | if (IS_ERR(trans)) { |
| 9134 | err = PTR_ERR(trans); |
| 9135 | goto out_free; |
| 9136 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 9137 | if (block_rsv) |
| 9138 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 9139 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9140 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9141 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9142 | if (err) |
| 9143 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9144 | |
| 9145 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9146 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9147 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9148 | goto out_end_trans; |
| 9149 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9150 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9151 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 9152 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 9153 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9154 | if (ret < 0) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 9155 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9156 | err = ret; |
| 9157 | goto out_end_trans; |
| 9158 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 9159 | /* if we fail to delete the orphan item this time |
| 9160 | * around, it'll get picked up the next time. |
| 9161 | * |
| 9162 | * The most common failure here is just -ENOENT. |
| 9163 | */ |
| 9164 | btrfs_del_orphan_item(trans, tree_root, |
| 9165 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9166 | } |
| 9167 | } |
| 9168 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 9169 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 9170 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9171 | } else { |
| 9172 | free_extent_buffer(root->node); |
| 9173 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 9174 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 9175 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9176 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9177 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9178 | btrfs_end_transaction_throttle(trans); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9179 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9180 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 9181 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 9182 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9183 | /* |
| 9184 | * So if we need to stop dropping the snapshot for whatever reason we |
| 9185 | * need to make sure to add it back to the dead root list so that we |
| 9186 | * keep trying to do the work later. This also cleans up roots if we |
| 9187 | * don't have it in the radix (like when we recover after a power fail |
| 9188 | * or unmount) so we don't leak memory. |
| 9189 | */ |
Josef Bacik | b37b39c | 2013-07-23 16:57:15 -0400 | [diff] [blame] | 9190 | if (!for_reloc && root_dropped == false) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9191 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 9192 | if (err && err != -EAGAIN) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9193 | btrfs_handle_fs_error(fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 9194 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9195 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9196 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9197 | /* |
| 9198 | * drop subtree rooted at tree block 'node'. |
| 9199 | * |
| 9200 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9201 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9202 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9203 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 9204 | struct btrfs_root *root, |
| 9205 | struct extent_buffer *node, |
| 9206 | struct extent_buffer *parent) |
| 9207 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9208 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9209 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9210 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9211 | int level; |
| 9212 | int parent_level; |
| 9213 | int ret = 0; |
| 9214 | int wret; |
| 9215 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9216 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 9217 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9218 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9219 | if (!path) |
| 9220 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9221 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9222 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9223 | if (!wc) { |
| 9224 | btrfs_free_path(path); |
| 9225 | return -ENOMEM; |
| 9226 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9227 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9228 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9229 | parent_level = btrfs_header_level(parent); |
| 9230 | extent_buffer_get(parent); |
| 9231 | path->nodes[parent_level] = parent; |
| 9232 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 9233 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9234 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9235 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9236 | path->nodes[level] = node; |
| 9237 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9238 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9239 | |
| 9240 | wc->refs[parent_level] = 1; |
| 9241 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 9242 | wc->level = level; |
| 9243 | wc->shared_level = -1; |
| 9244 | wc->stage = DROP_REFERENCE; |
| 9245 | wc->update_ref = 0; |
| 9246 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9247 | wc->for_reloc = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9248 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9249 | |
| 9250 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9251 | wret = walk_down_tree(trans, root, path, wc); |
| 9252 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9253 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9254 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9255 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9256 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9257 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9258 | if (wret < 0) |
| 9259 | ret = wret; |
| 9260 | if (wret != 0) |
| 9261 | break; |
| 9262 | } |
| 9263 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9264 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9265 | btrfs_free_path(path); |
| 9266 | return ret; |
| 9267 | } |
| 9268 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9269 | 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] | 9270 | { |
| 9271 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9272 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9273 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9274 | /* |
| 9275 | * if restripe for this chunk_type is on pick target profile and |
| 9276 | * return, otherwise do the usual balance |
| 9277 | */ |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9278 | stripped = get_restripe_target(fs_info, flags); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9279 | if (stripped) |
| 9280 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 9281 | |
Jeff Mahoney | 6202df6 | 2016-06-22 18:54:22 -0400 | [diff] [blame] | 9282 | num_devices = fs_info->fs_devices->rw_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 9283 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9284 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 9285 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9286 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 9287 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9288 | if (num_devices == 1) { |
| 9289 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9290 | stripped = flags & ~stripped; |
| 9291 | |
| 9292 | /* turn raid0 into single device chunks */ |
| 9293 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 9294 | return stripped; |
| 9295 | |
| 9296 | /* turn mirroring into duplication */ |
| 9297 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9298 | BTRFS_BLOCK_GROUP_RAID10)) |
| 9299 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9300 | } else { |
| 9301 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9302 | if (flags & stripped) |
| 9303 | return flags; |
| 9304 | |
| 9305 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9306 | stripped = flags & ~stripped; |
| 9307 | |
| 9308 | /* switch duplicated blocks with raid1 */ |
| 9309 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 9310 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 9311 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9312 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9313 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9314 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9315 | return flags; |
| 9316 | } |
| 9317 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9318 | 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] | 9319 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9320 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9321 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9322 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9323 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9324 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9325 | /* |
| 9326 | * We need some metadata space and system metadata space for |
| 9327 | * allocating chunks in some corner cases until we force to set |
| 9328 | * it to be readonly. |
| 9329 | */ |
| 9330 | if ((sinfo->flags & |
| 9331 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 9332 | !force) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 9333 | min_allocable_bytes = SZ_1M; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9334 | else |
| 9335 | min_allocable_bytes = 0; |
| 9336 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9337 | spin_lock(&sinfo->lock); |
| 9338 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9339 | |
| 9340 | if (cache->ro) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9341 | cache->ro++; |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9342 | ret = 0; |
| 9343 | goto out; |
| 9344 | } |
| 9345 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9346 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 9347 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9348 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9349 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 9350 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 9351 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9352 | sinfo->bytes_readonly += num_bytes; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9353 | cache->ro++; |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9354 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9355 | ret = 0; |
| 9356 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9357 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9358 | spin_unlock(&cache->lock); |
| 9359 | spin_unlock(&sinfo->lock); |
| 9360 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9361 | } |
| 9362 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9363 | int btrfs_inc_block_group_ro(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9364 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9365 | |
| 9366 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9367 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9368 | struct btrfs_trans_handle *trans; |
| 9369 | u64 alloc_flags; |
| 9370 | int ret; |
| 9371 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9372 | again: |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 9373 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9374 | if (IS_ERR(trans)) |
| 9375 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9376 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9377 | /* |
| 9378 | * we're not allowed to set block groups readonly after the dirty |
| 9379 | * block groups cache has started writing. If it already started, |
| 9380 | * back off and let this transaction commit |
| 9381 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9382 | mutex_lock(&fs_info->ro_block_group_mutex); |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 9383 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9384 | u64 transid = trans->transid; |
| 9385 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9386 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9387 | btrfs_end_transaction(trans); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9388 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9389 | ret = btrfs_wait_for_commit(fs_info, transid); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9390 | if (ret) |
| 9391 | return ret; |
| 9392 | goto again; |
| 9393 | } |
| 9394 | |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9395 | /* |
| 9396 | * if we are changing raid levels, try to allocate a corresponding |
| 9397 | * block group with the new raid level. |
| 9398 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9399 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9400 | if (alloc_flags != cache->flags) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9401 | ret = do_chunk_alloc(trans, fs_info, alloc_flags, |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9402 | CHUNK_ALLOC_FORCE); |
| 9403 | /* |
| 9404 | * ENOSPC is allowed here, we may have enough space |
| 9405 | * already allocated at the new raid level to |
| 9406 | * carry on |
| 9407 | */ |
| 9408 | if (ret == -ENOSPC) |
| 9409 | ret = 0; |
| 9410 | if (ret < 0) |
| 9411 | goto out; |
| 9412 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9413 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9414 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9415 | if (!ret) |
| 9416 | goto out; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9417 | alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags); |
| 9418 | ret = do_chunk_alloc(trans, fs_info, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 9419 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9420 | if (ret < 0) |
| 9421 | goto out; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9422 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9423 | out: |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9424 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9425 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 9426 | mutex_lock(&fs_info->chunk_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9427 | check_system_chunk(trans, fs_info, alloc_flags); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 9428 | mutex_unlock(&fs_info->chunk_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9429 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9430 | mutex_unlock(&fs_info->ro_block_group_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9431 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9432 | btrfs_end_transaction(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9433 | return ret; |
| 9434 | } |
| 9435 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9436 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9437 | struct btrfs_fs_info *fs_info, u64 type) |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9438 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9439 | u64 alloc_flags = get_alloc_profile(fs_info, type); |
| 9440 | |
| 9441 | return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9442 | } |
| 9443 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9444 | /* |
| 9445 | * 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] | 9446 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9447 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9448 | 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] | 9449 | { |
| 9450 | struct btrfs_block_group_cache *block_group; |
| 9451 | u64 free_bytes = 0; |
| 9452 | int factor; |
| 9453 | |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 9454 | /* It's df, we don't care if it's racy */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9455 | if (list_empty(&sinfo->ro_bgs)) |
| 9456 | return 0; |
| 9457 | |
| 9458 | spin_lock(&sinfo->lock); |
| 9459 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9460 | spin_lock(&block_group->lock); |
| 9461 | |
| 9462 | if (!block_group->ro) { |
| 9463 | spin_unlock(&block_group->lock); |
| 9464 | continue; |
| 9465 | } |
| 9466 | |
| 9467 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9468 | BTRFS_BLOCK_GROUP_RAID10 | |
| 9469 | BTRFS_BLOCK_GROUP_DUP)) |
| 9470 | factor = 2; |
| 9471 | else |
| 9472 | factor = 1; |
| 9473 | |
| 9474 | free_bytes += (block_group->key.offset - |
| 9475 | btrfs_block_group_used(&block_group->item)) * |
| 9476 | factor; |
| 9477 | |
| 9478 | spin_unlock(&block_group->lock); |
| 9479 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9480 | spin_unlock(&sinfo->lock); |
| 9481 | |
| 9482 | return free_bytes; |
| 9483 | } |
| 9484 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9485 | void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9486 | { |
| 9487 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9488 | u64 num_bytes; |
| 9489 | |
| 9490 | BUG_ON(!cache->ro); |
| 9491 | |
| 9492 | spin_lock(&sinfo->lock); |
| 9493 | spin_lock(&cache->lock); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9494 | if (!--cache->ro) { |
| 9495 | num_bytes = cache->key.offset - cache->reserved - |
| 9496 | cache->pinned - cache->bytes_super - |
| 9497 | btrfs_block_group_used(&cache->item); |
| 9498 | sinfo->bytes_readonly -= num_bytes; |
| 9499 | list_del_init(&cache->ro_list); |
| 9500 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9501 | spin_unlock(&cache->lock); |
| 9502 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9503 | } |
| 9504 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9505 | /* |
| 9506 | * checks to see if its even possible to relocate this block group. |
| 9507 | * |
| 9508 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 9509 | * ok to go ahead and try. |
| 9510 | */ |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9511 | int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9512 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9513 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9514 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9515 | struct btrfs_space_info *space_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9516 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9517 | struct btrfs_device *device; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9518 | struct btrfs_trans_handle *trans; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9519 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9520 | u64 dev_min = 1; |
| 9521 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9522 | u64 target; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9523 | int debug; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9524 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9525 | int full = 0; |
| 9526 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9527 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9528 | debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG); |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9529 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9530 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9531 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9532 | /* odd, couldn't find the block group, leave it alone */ |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9533 | if (!block_group) { |
| 9534 | if (debug) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9535 | btrfs_warn(fs_info, |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9536 | "can't find block group for bytenr %llu", |
| 9537 | bytenr); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9538 | return -1; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9539 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9540 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9541 | min_free = btrfs_block_group_used(&block_group->item); |
| 9542 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9543 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9544 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9545 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9546 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9547 | space_info = block_group->space_info; |
| 9548 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9549 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9550 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9551 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9552 | /* |
| 9553 | * 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] | 9554 | * relocate it unless we're able to allocate a new chunk below. |
| 9555 | * |
| 9556 | * Otherwise, we need to make sure we have room in the space to handle |
| 9557 | * 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] | 9558 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 9559 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9560 | (space_info->bytes_used + space_info->bytes_reserved + |
| 9561 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 9562 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9563 | spin_unlock(&space_info->lock); |
| 9564 | goto out; |
| 9565 | } |
| 9566 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9567 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9568 | /* |
| 9569 | * ok we don't have enough space, but maybe we have free space on our |
| 9570 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9571 | * alloc devices and guess if we have enough space. if this block |
| 9572 | * group is going to be restriped, run checks against the target |
| 9573 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9574 | */ |
| 9575 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 9576 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9577 | /* |
| 9578 | * index: |
| 9579 | * 0: raid10 |
| 9580 | * 1: raid1 |
| 9581 | * 2: dup |
| 9582 | * 3: raid0 |
| 9583 | * 4: single |
| 9584 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9585 | target = get_restripe_target(fs_info, block_group->flags); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9586 | if (target) { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 9587 | index = __get_raid_index(extended_to_chunk(target)); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9588 | } else { |
| 9589 | /* |
| 9590 | * this is just a balance, so if we were marked as full |
| 9591 | * we know there is no space for a new chunk |
| 9592 | */ |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9593 | if (full) { |
| 9594 | if (debug) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9595 | btrfs_warn(fs_info, |
| 9596 | "no space to alloc new chunk for block group %llu", |
| 9597 | block_group->key.objectid); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9598 | goto out; |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9599 | } |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9600 | |
| 9601 | index = get_block_group_index(block_group); |
| 9602 | } |
| 9603 | |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9604 | if (index == BTRFS_RAID_RAID10) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9605 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9606 | /* Divide by 2 */ |
| 9607 | min_free >>= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9608 | } else if (index == BTRFS_RAID_RAID1) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9609 | dev_min = 2; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9610 | } else if (index == BTRFS_RAID_DUP) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9611 | /* Multiply by 2 */ |
| 9612 | min_free <<= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9613 | } else if (index == BTRFS_RAID_RAID0) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9614 | dev_min = fs_devices->rw_devices; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 9615 | min_free = div64_u64(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9616 | } |
| 9617 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9618 | /* We need to do this so that we can look at pending chunks */ |
| 9619 | trans = btrfs_join_transaction(root); |
| 9620 | if (IS_ERR(trans)) { |
| 9621 | ret = PTR_ERR(trans); |
| 9622 | goto out; |
| 9623 | } |
| 9624 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9625 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9626 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9627 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9628 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9629 | /* |
| 9630 | * check to make sure we can actually find a chunk with enough |
| 9631 | * space to fit our block group in. |
| 9632 | */ |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 9633 | if (device->total_bytes > device->bytes_used + min_free && |
| 9634 | !device->is_tgtdev_for_dev_replace) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9635 | ret = find_free_dev_extent(trans, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9636 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9637 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9638 | dev_nr++; |
| 9639 | |
| 9640 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9641 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9642 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9643 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9644 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9645 | } |
Qu Wenruo | 0305bc2 | 2016-03-23 11:38:17 +0800 | [diff] [blame] | 9646 | if (debug && ret == -1) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9647 | btrfs_warn(fs_info, |
| 9648 | "no space to allocate a new chunk for block group %llu", |
| 9649 | block_group->key.objectid); |
| 9650 | mutex_unlock(&fs_info->chunk_mutex); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 9651 | btrfs_end_transaction(trans); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9652 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9653 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9654 | return ret; |
| 9655 | } |
| 9656 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9657 | static int find_first_block_group(struct btrfs_fs_info *fs_info, |
| 9658 | struct btrfs_path *path, |
| 9659 | struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9660 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9661 | struct btrfs_root *root = fs_info->extent_root; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9662 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9663 | struct btrfs_key found_key; |
| 9664 | struct extent_buffer *leaf; |
| 9665 | int slot; |
| 9666 | |
| 9667 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 9668 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9669 | goto out; |
| 9670 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9671 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9672 | slot = path->slots[0]; |
| 9673 | leaf = path->nodes[0]; |
| 9674 | if (slot >= btrfs_header_nritems(leaf)) { |
| 9675 | ret = btrfs_next_leaf(root, path); |
| 9676 | if (ret == 0) |
| 9677 | continue; |
| 9678 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9679 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9680 | break; |
| 9681 | } |
| 9682 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 9683 | |
| 9684 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9685 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 9686 | struct extent_map_tree *em_tree; |
| 9687 | struct extent_map *em; |
| 9688 | |
| 9689 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 9690 | read_lock(&em_tree->lock); |
| 9691 | em = lookup_extent_mapping(em_tree, found_key.objectid, |
| 9692 | found_key.offset); |
| 9693 | read_unlock(&em_tree->lock); |
| 9694 | if (!em) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9695 | btrfs_err(fs_info, |
Liu Bo | 6fb37b7 | 2016-06-22 18:31:27 -0700 | [diff] [blame] | 9696 | "logical %llu len %llu found bg but no related chunk", |
| 9697 | found_key.objectid, found_key.offset); |
| 9698 | ret = -ENOENT; |
| 9699 | } else { |
| 9700 | ret = 0; |
| 9701 | } |
Josef Bacik | 187ee58 | 2016-08-18 15:30:06 -0400 | [diff] [blame] | 9702 | free_extent_map(em); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9703 | goto out; |
| 9704 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9705 | path->slots[0]++; |
| 9706 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9707 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9708 | return ret; |
| 9709 | } |
| 9710 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9711 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 9712 | { |
| 9713 | struct btrfs_block_group_cache *block_group; |
| 9714 | u64 last = 0; |
| 9715 | |
| 9716 | while (1) { |
| 9717 | struct inode *inode; |
| 9718 | |
| 9719 | block_group = btrfs_lookup_first_block_group(info, last); |
| 9720 | while (block_group) { |
| 9721 | spin_lock(&block_group->lock); |
| 9722 | if (block_group->iref) |
| 9723 | break; |
| 9724 | spin_unlock(&block_group->lock); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9725 | block_group = next_block_group(info, block_group); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9726 | } |
| 9727 | if (!block_group) { |
| 9728 | if (last == 0) |
| 9729 | break; |
| 9730 | last = 0; |
| 9731 | continue; |
| 9732 | } |
| 9733 | |
| 9734 | inode = block_group->inode; |
| 9735 | block_group->iref = 0; |
| 9736 | block_group->inode = NULL; |
| 9737 | spin_unlock(&block_group->lock); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 9738 | ASSERT(block_group->io_ctl.inode == NULL); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9739 | iput(inode); |
| 9740 | last = block_group->key.objectid + block_group->key.offset; |
| 9741 | btrfs_put_block_group(block_group); |
| 9742 | } |
| 9743 | } |
| 9744 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9745 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 9746 | { |
| 9747 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9748 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9749 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9750 | struct rb_node *n; |
| 9751 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9752 | down_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9753 | while (!list_empty(&info->caching_block_groups)) { |
| 9754 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 9755 | struct btrfs_caching_control, list); |
| 9756 | list_del(&caching_ctl->list); |
| 9757 | put_caching_control(caching_ctl); |
| 9758 | } |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9759 | up_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9760 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9761 | spin_lock(&info->unused_bgs_lock); |
| 9762 | while (!list_empty(&info->unused_bgs)) { |
| 9763 | block_group = list_first_entry(&info->unused_bgs, |
| 9764 | struct btrfs_block_group_cache, |
| 9765 | bg_list); |
| 9766 | list_del_init(&block_group->bg_list); |
| 9767 | btrfs_put_block_group(block_group); |
| 9768 | } |
| 9769 | spin_unlock(&info->unused_bgs_lock); |
| 9770 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9771 | spin_lock(&info->block_group_cache_lock); |
| 9772 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 9773 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 9774 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9775 | rb_erase(&block_group->cache_node, |
| 9776 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 9777 | RB_CLEAR_NODE(&block_group->cache_node); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9778 | spin_unlock(&info->block_group_cache_lock); |
| 9779 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9780 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9781 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9782 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9783 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9784 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9785 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9786 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9787 | /* |
| 9788 | * We haven't cached this block group, which means we could |
| 9789 | * possibly have excluded extents on this block group. |
| 9790 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 9791 | if (block_group->cached == BTRFS_CACHE_NO || |
| 9792 | block_group->cached == BTRFS_CACHE_ERROR) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9793 | free_excluded_extents(info, block_group); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9794 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9795 | btrfs_remove_free_space_cache(block_group); |
Liu Bo | f3bca80 | 2016-07-20 17:33:44 -0700 | [diff] [blame] | 9796 | ASSERT(list_empty(&block_group->dirty_list)); |
| 9797 | ASSERT(list_empty(&block_group->io_list)); |
| 9798 | ASSERT(list_empty(&block_group->bg_list)); |
| 9799 | ASSERT(atomic_read(&block_group->count) == 1); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 9800 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9801 | |
| 9802 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9803 | } |
| 9804 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9805 | |
| 9806 | /* now that all the block groups are freed, go through and |
| 9807 | * free all the space_info structs. This is only called during |
| 9808 | * the final stages of unmount, and so we know nobody is |
| 9809 | * using them. We call synchronize_rcu() once before we start, |
| 9810 | * just to be on the safe side. |
| 9811 | */ |
| 9812 | synchronize_rcu(); |
| 9813 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 9814 | release_global_block_rsv(info); |
| 9815 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9816 | while (!list_empty(&info->space_info)) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9817 | int i; |
| 9818 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9819 | space_info = list_entry(info->space_info.next, |
| 9820 | struct btrfs_space_info, |
| 9821 | list); |
Josef Bacik | d555b6c3 | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 9822 | |
| 9823 | /* |
| 9824 | * Do not hide this behind enospc_debug, this is actually |
| 9825 | * important and indicates a real bug if this happens. |
| 9826 | */ |
| 9827 | if (WARN_ON(space_info->bytes_pinned > 0 || |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 9828 | space_info->bytes_reserved > 0 || |
Josef Bacik | d555b6c3 | 2016-03-25 13:25:51 -0400 | [diff] [blame] | 9829 | space_info->bytes_may_use > 0)) |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9830 | dump_space_info(info, space_info, 0, 0); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9831 | list_del(&space_info->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9832 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 9833 | struct kobject *kobj; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9834 | kobj = space_info->block_group_kobjs[i]; |
| 9835 | space_info->block_group_kobjs[i] = NULL; |
| 9836 | if (kobj) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9837 | kobject_del(kobj); |
| 9838 | kobject_put(kobj); |
| 9839 | } |
| 9840 | } |
| 9841 | kobject_del(&space_info->kobj); |
| 9842 | kobject_put(&space_info->kobj); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9843 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9844 | return 0; |
| 9845 | } |
| 9846 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9847 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 9848 | struct btrfs_block_group_cache *cache) |
| 9849 | { |
| 9850 | int index = get_block_group_index(cache); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9851 | bool first = false; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9852 | |
| 9853 | down_write(&space_info->groups_sem); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9854 | if (list_empty(&space_info->block_groups[index])) |
| 9855 | first = true; |
| 9856 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 9857 | up_write(&space_info->groups_sem); |
| 9858 | |
| 9859 | if (first) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9860 | struct raid_kobject *rkobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9861 | int ret; |
| 9862 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9863 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 9864 | if (!rkobj) |
| 9865 | goto out_err; |
| 9866 | rkobj->raid_type = index; |
| 9867 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 9868 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, |
| 9869 | "%s", get_raid_name(index)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9870 | if (ret) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9871 | kobject_put(&rkobj->kobj); |
| 9872 | goto out_err; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9873 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9874 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9875 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9876 | |
| 9877 | return; |
| 9878 | out_err: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 9879 | btrfs_warn(cache->fs_info, |
| 9880 | "failed to add kobject for block cache, ignoring"); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9881 | } |
| 9882 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9883 | static struct btrfs_block_group_cache * |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9884 | btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info, |
| 9885 | u64 start, u64 size) |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9886 | { |
| 9887 | struct btrfs_block_group_cache *cache; |
| 9888 | |
| 9889 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 9890 | if (!cache) |
| 9891 | return NULL; |
| 9892 | |
| 9893 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 9894 | GFP_NOFS); |
| 9895 | if (!cache->free_space_ctl) { |
| 9896 | kfree(cache); |
| 9897 | return NULL; |
| 9898 | } |
| 9899 | |
| 9900 | cache->key.objectid = start; |
| 9901 | cache->key.offset = size; |
| 9902 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 9903 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9904 | cache->sectorsize = fs_info->sectorsize; |
| 9905 | cache->fs_info = fs_info; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9906 | cache->full_stripe_len = btrfs_full_stripe_len(fs_info, |
| 9907 | &fs_info->mapping_tree, |
| 9908 | start); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 9909 | set_free_space_tree_thresholds(cache); |
| 9910 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9911 | atomic_set(&cache->count, 1); |
| 9912 | spin_lock_init(&cache->lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9913 | init_rwsem(&cache->data_rwsem); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9914 | INIT_LIST_HEAD(&cache->list); |
| 9915 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9916 | INIT_LIST_HEAD(&cache->bg_list); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9917 | INIT_LIST_HEAD(&cache->ro_list); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9918 | INIT_LIST_HEAD(&cache->dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 9919 | INIT_LIST_HEAD(&cache->io_list); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9920 | btrfs_init_free_space_ctl(cache); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 9921 | atomic_set(&cache->trimming, 0); |
Omar Sandoval | a5ed918 | 2015-09-29 20:50:35 -0700 | [diff] [blame] | 9922 | mutex_init(&cache->free_space_lock); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9923 | |
| 9924 | return cache; |
| 9925 | } |
| 9926 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 9927 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9928 | { |
| 9929 | struct btrfs_path *path; |
| 9930 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9931 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9932 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9933 | struct btrfs_key key; |
| 9934 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9935 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9936 | int need_clear = 0; |
| 9937 | u64 cache_gen; |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 9938 | u64 feature; |
| 9939 | int mixed; |
| 9940 | |
| 9941 | feature = btrfs_super_incompat_flags(info->super_copy); |
| 9942 | mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 9943 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9944 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9945 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9946 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9947 | path = btrfs_alloc_path(); |
| 9948 | if (!path) |
| 9949 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 9950 | path->reada = READA_FORWARD; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9951 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9952 | cache_gen = btrfs_super_cache_generation(info->super_copy); |
| 9953 | if (btrfs_test_opt(info, SPACE_CACHE) && |
| 9954 | btrfs_super_generation(info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9955 | need_clear = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9956 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 9957 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9958 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9959 | while (1) { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 9960 | ret = find_first_block_group(info, path, &key); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9961 | if (ret > 0) |
| 9962 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9963 | if (ret != 0) |
| 9964 | goto error; |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9965 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9966 | leaf = path->nodes[0]; |
| 9967 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9968 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 9969 | cache = btrfs_create_block_group_cache(info, found_key.objectid, |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9970 | found_key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9971 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9972 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9973 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9974 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9975 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9976 | if (need_clear) { |
| 9977 | /* |
| 9978 | * When we mount with old space cache, we need to |
| 9979 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 9980 | * |
| 9981 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 9982 | * truncate the old free space cache inode and |
| 9983 | * setup a new one. |
| 9984 | * b) Setting 'dirty flag' makes sure that we flush |
| 9985 | * the new space cache info onto disk. |
| 9986 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 9987 | if (btrfs_test_opt(info, SPACE_CACHE)) |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9988 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9989 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9990 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9991 | read_extent_buffer(leaf, &cache->item, |
| 9992 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 9993 | sizeof(cache->item)); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9994 | cache->flags = btrfs_block_group_flags(&cache->item); |
Liu Bo | 4930338 | 2016-08-25 18:08:27 -0700 | [diff] [blame] | 9995 | if (!mixed && |
| 9996 | ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && |
| 9997 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { |
| 9998 | btrfs_err(info, |
| 9999 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", |
| 10000 | cache->key.objectid); |
| 10001 | ret = -EINVAL; |
| 10002 | goto error; |
| 10003 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10004 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10005 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10006 | btrfs_release_path(path); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 10007 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10008 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 10009 | * We need to exclude the super stripes now so that the space |
| 10010 | * info has super bytes accounted for, otherwise we'll think |
| 10011 | * we have more space than we actually do. |
| 10012 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10013 | ret = exclude_super_stripes(info, cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10014 | if (ret) { |
| 10015 | /* |
| 10016 | * We may have excluded something, so call this just in |
| 10017 | * case. |
| 10018 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10019 | free_excluded_extents(info, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10020 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10021 | goto error; |
| 10022 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 10023 | |
| 10024 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10025 | * check for two cases, either we are full, and therefore |
| 10026 | * don't need to bother with the caching work since we won't |
| 10027 | * find any space, or we are empty, and we can just add all |
| 10028 | * the space in and be done with it. This saves us _alot_ of |
| 10029 | * time, particularly in the full case. |
| 10030 | */ |
| 10031 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10032 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10033 | cache->cached = BTRFS_CACHE_FINISHED; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10034 | free_excluded_extents(info, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10035 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10036 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10037 | cache->cached = BTRFS_CACHE_FINISHED; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10038 | add_new_free_space(cache, info, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10039 | found_key.objectid, |
| 10040 | found_key.objectid + |
| 10041 | found_key.offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10042 | free_excluded_extents(info, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10043 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 10044 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10045 | ret = btrfs_add_block_group_cache(info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10046 | if (ret) { |
| 10047 | btrfs_remove_free_space_cache(cache); |
| 10048 | btrfs_put_block_group(cache); |
| 10049 | goto error; |
| 10050 | } |
| 10051 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10052 | trace_btrfs_add_block_group(info, cache, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10053 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 10054 | btrfs_block_group_used(&cache->item), |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10055 | cache->bytes_super, &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10056 | if (ret) { |
| 10057 | btrfs_remove_free_space_cache(cache); |
| 10058 | spin_lock(&info->block_group_cache_lock); |
| 10059 | rb_erase(&cache->cache_node, |
| 10060 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 10061 | RB_CLEAR_NODE(&cache->cache_node); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10062 | spin_unlock(&info->block_group_cache_lock); |
| 10063 | btrfs_put_block_group(cache); |
| 10064 | goto error; |
| 10065 | } |
| 10066 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10067 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 10068 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10069 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10070 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10071 | set_avail_alloc_bits(info, cache->flags); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10072 | if (btrfs_chunk_readonly(info, cache->key.objectid)) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10073 | inc_block_group_ro(cache, 1); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10074 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
| 10075 | spin_lock(&info->unused_bgs_lock); |
| 10076 | /* Should always be true but just in case. */ |
| 10077 | if (list_empty(&cache->bg_list)) { |
| 10078 | btrfs_get_block_group(cache); |
| 10079 | list_add_tail(&cache->bg_list, |
| 10080 | &info->unused_bgs); |
| 10081 | } |
| 10082 | spin_unlock(&info->unused_bgs_lock); |
| 10083 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10084 | } |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10085 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10086 | list_for_each_entry_rcu(space_info, &info->space_info, list) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10087 | if (!(get_alloc_profile(info, space_info->flags) & |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10088 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 10089 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 10090 | BTRFS_BLOCK_GROUP_RAID5 | |
| 10091 | BTRFS_BLOCK_GROUP_RAID6 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10092 | BTRFS_BLOCK_GROUP_DUP))) |
| 10093 | continue; |
| 10094 | /* |
| 10095 | * avoid allocating from un-mirrored block group if there are |
| 10096 | * mirrored block groups. |
| 10097 | */ |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 10098 | list_for_each_entry(cache, |
| 10099 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 10100 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10101 | inc_block_group_ro(cache, 1); |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 10102 | list_for_each_entry(cache, |
| 10103 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 10104 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10105 | inc_block_group_ro(cache, 1); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10106 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 10107 | |
| 10108 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10109 | ret = 0; |
| 10110 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10111 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 10112 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 10113 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10114 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10115 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10116 | struct btrfs_fs_info *fs_info) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10117 | { |
| 10118 | struct btrfs_block_group_cache *block_group, *tmp; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10119 | struct btrfs_root *extent_root = fs_info->extent_root; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10120 | struct btrfs_block_group_item item; |
| 10121 | struct btrfs_key key; |
| 10122 | int ret = 0; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10123 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10124 | |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10125 | trans->can_flush_pending_bgs = false; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10126 | 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] | 10127 | if (ret) |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 10128 | goto next; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10129 | |
| 10130 | spin_lock(&block_group->lock); |
| 10131 | memcpy(&item, &block_group->item, sizeof(item)); |
| 10132 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10133 | spin_unlock(&block_group->lock); |
| 10134 | |
| 10135 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 10136 | sizeof(item)); |
| 10137 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10138 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10139 | ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid, |
| 10140 | key.offset); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 10141 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 10142 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10143 | add_block_group_free_space(trans, fs_info, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10144 | /* already aborted the transaction if it failed. */ |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 10145 | next: |
| 10146 | list_del_init(&block_group->bg_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10147 | } |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 10148 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 10149 | } |
| 10150 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10151 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10152 | struct btrfs_fs_info *fs_info, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 10153 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10154 | u64 size) |
| 10155 | { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10156 | struct btrfs_block_group_cache *cache; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10157 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10158 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10159 | btrfs_set_log_full_commit(fs_info, trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 10160 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10161 | cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 10162 | if (!cache) |
| 10163 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 10164 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10165 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10166 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10167 | btrfs_set_block_group_flags(&cache->item, type); |
| 10168 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10169 | cache->flags = type; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10170 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10171 | cache->cached = BTRFS_CACHE_FINISHED; |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10172 | cache->needs_free_space = 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10173 | ret = exclude_super_stripes(fs_info, cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10174 | if (ret) { |
| 10175 | /* |
| 10176 | * We may have excluded something, so call this just in |
| 10177 | * case. |
| 10178 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10179 | free_excluded_extents(fs_info, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 10180 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 10181 | return ret; |
| 10182 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10183 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10184 | add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10185 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10186 | free_excluded_extents(fs_info, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10187 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10188 | #ifdef CONFIG_BTRFS_DEBUG |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10189 | if (btrfs_should_fragment_free_space(cache)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10190 | u64 new_bytes_used = size - bytes_used; |
| 10191 | |
| 10192 | bytes_used += new_bytes_used >> 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10193 | fragment_free_space(cache); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 10194 | } |
| 10195 | #endif |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10196 | /* |
| 10197 | * Call to ensure the corresponding space_info object is created and |
| 10198 | * assigned to our block group, but don't update its counters just yet. |
| 10199 | * We want our bg to be added to the rbtree with its ->space_info set. |
| 10200 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10201 | ret = update_space_info(fs_info, cache->flags, 0, 0, 0, |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10202 | &cache->space_info); |
| 10203 | if (ret) { |
| 10204 | btrfs_remove_free_space_cache(cache); |
| 10205 | btrfs_put_block_group(cache); |
| 10206 | return ret; |
| 10207 | } |
| 10208 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10209 | ret = btrfs_add_block_group_cache(fs_info, cache); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10210 | if (ret) { |
| 10211 | btrfs_remove_free_space_cache(cache); |
| 10212 | btrfs_put_block_group(cache); |
| 10213 | return ret; |
| 10214 | } |
| 10215 | |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 10216 | /* |
| 10217 | * Now that our block group has its ->space_info set and is inserted in |
| 10218 | * the rbtree, update the space info's counters. |
| 10219 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10220 | trace_btrfs_add_block_group(fs_info, cache, 1); |
| 10221 | ret = update_space_info(fs_info, cache->flags, size, bytes_used, |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10222 | cache->bytes_super, &cache->space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10223 | if (ret) { |
| 10224 | btrfs_remove_free_space_cache(cache); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10225 | spin_lock(&fs_info->block_group_cache_lock); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10226 | rb_erase(&cache->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10227 | &fs_info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 10228 | RB_CLEAR_NODE(&cache->cache_node); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10229 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 10230 | btrfs_put_block_group(cache); |
| 10231 | return ret; |
| 10232 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10233 | update_global_block_rsv(fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 10234 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 10235 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10236 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10237 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10238 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10239 | set_avail_alloc_bits(fs_info, type); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 10240 | return 0; |
| 10241 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10242 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10243 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 10244 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 10245 | u64 extra_flags = chunk_to_extended(flags) & |
| 10246 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10247 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10248 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10249 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 10250 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 10251 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 10252 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 10253 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 10254 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10255 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10256 | } |
| 10257 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10258 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10259 | struct btrfs_fs_info *fs_info, u64 group_start, |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10260 | struct extent_map *em) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10261 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10262 | struct btrfs_root *root = fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10263 | struct btrfs_path *path; |
| 10264 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10265 | struct btrfs_free_cluster *cluster; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10266 | struct btrfs_root *tree_root = fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10267 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10268 | struct inode *inode; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10269 | struct kobject *kobj = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10270 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10271 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10272 | int factor; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10273 | struct btrfs_caching_control *caching_ctl = NULL; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10274 | bool remove_em; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10275 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 10276 | block_group = btrfs_lookup_block_group(fs_info, group_start); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10277 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10278 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10279 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10280 | /* |
| 10281 | * Free the reserved super bytes from this block group before |
| 10282 | * remove it. |
| 10283 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10284 | free_excluded_extents(fs_info, block_group); |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10285 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10286 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10287 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10288 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 10289 | BTRFS_BLOCK_GROUP_RAID1 | |
| 10290 | BTRFS_BLOCK_GROUP_RAID10)) |
| 10291 | factor = 2; |
| 10292 | else |
| 10293 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10294 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10295 | /* make sure this block group isn't part of an allocation cluster */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10296 | cluster = &fs_info->data_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10297 | spin_lock(&cluster->refill_lock); |
| 10298 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10299 | spin_unlock(&cluster->refill_lock); |
| 10300 | |
| 10301 | /* |
| 10302 | * make sure this block group isn't part of a metadata |
| 10303 | * allocation cluster |
| 10304 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10305 | cluster = &fs_info->meta_alloc_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10306 | spin_lock(&cluster->refill_lock); |
| 10307 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10308 | spin_unlock(&cluster->refill_lock); |
| 10309 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10310 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10311 | if (!path) { |
| 10312 | ret = -ENOMEM; |
| 10313 | goto out; |
| 10314 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10315 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10316 | /* |
| 10317 | * get the inode first so any iput calls done for the io_list |
| 10318 | * aren't the final iput (no unlinks allowed now) |
| 10319 | */ |
Ilya Dryomov | 10b2f34 | 2011-10-02 13:56:53 +0300 | [diff] [blame] | 10320 | inode = lookup_free_space_inode(tree_root, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10321 | |
| 10322 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 10323 | /* |
| 10324 | * make sure our free spache cache IO is done before remove the |
| 10325 | * free space inode |
| 10326 | */ |
| 10327 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10328 | if (!list_empty(&block_group->io_list)) { |
| 10329 | list_del_init(&block_group->io_list); |
| 10330 | |
| 10331 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); |
| 10332 | |
| 10333 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Jeff Mahoney | afdb571 | 2016-09-09 12:09:35 -0400 | [diff] [blame] | 10334 | btrfs_wait_cache_io(trans, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10335 | btrfs_put_block_group(block_group); |
| 10336 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10337 | } |
| 10338 | |
| 10339 | if (!list_empty(&block_group->dirty_list)) { |
| 10340 | list_del_init(&block_group->dirty_list); |
| 10341 | btrfs_put_block_group(block_group); |
| 10342 | } |
| 10343 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 10344 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 10345 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10346 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 10347 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10348 | if (ret) { |
| 10349 | btrfs_add_delayed_iput(inode); |
| 10350 | goto out; |
| 10351 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10352 | clear_nlink(inode); |
| 10353 | /* One for the block groups ref */ |
| 10354 | spin_lock(&block_group->lock); |
| 10355 | if (block_group->iref) { |
| 10356 | block_group->iref = 0; |
| 10357 | block_group->inode = NULL; |
| 10358 | spin_unlock(&block_group->lock); |
| 10359 | iput(inode); |
| 10360 | } else { |
| 10361 | spin_unlock(&block_group->lock); |
| 10362 | } |
| 10363 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 10364 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10365 | } |
| 10366 | |
| 10367 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 10368 | key.offset = block_group->key.objectid; |
| 10369 | key.type = 0; |
| 10370 | |
| 10371 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 10372 | if (ret < 0) |
| 10373 | goto out; |
| 10374 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10375 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10376 | if (ret == 0) { |
| 10377 | ret = btrfs_del_item(trans, tree_root, path); |
| 10378 | if (ret) |
| 10379 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10380 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10381 | } |
| 10382 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10383 | spin_lock(&fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10384 | rb_erase(&block_group->cache_node, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10385 | &fs_info->block_group_cache_tree); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 10386 | RB_CLEAR_NODE(&block_group->cache_node); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 10387 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10388 | if (fs_info->first_logical_byte == block_group->key.objectid) |
| 10389 | fs_info->first_logical_byte = (u64)-1; |
| 10390 | spin_unlock(&fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10391 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10392 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10393 | /* |
| 10394 | * we must use list_del_init so people can check to see if they |
| 10395 | * are still on the list after taking the semaphore |
| 10396 | */ |
| 10397 | list_del_init(&block_group->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10398 | if (list_empty(&block_group->space_info->block_groups[index])) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10399 | kobj = block_group->space_info->block_group_kobjs[index]; |
| 10400 | block_group->space_info->block_group_kobjs[index] = NULL; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10401 | clear_avail_alloc_bits(fs_info, block_group->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10402 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10403 | up_write(&block_group->space_info->groups_sem); |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10404 | if (kobj) { |
| 10405 | kobject_del(kobj); |
| 10406 | kobject_put(kobj); |
| 10407 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10408 | |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10409 | if (block_group->has_caching_ctl) |
| 10410 | caching_ctl = get_caching_control(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10411 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10412 | wait_block_group_cache_done(block_group); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10413 | if (block_group->has_caching_ctl) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10414 | down_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10415 | if (!caching_ctl) { |
| 10416 | struct btrfs_caching_control *ctl; |
| 10417 | |
| 10418 | list_for_each_entry(ctl, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10419 | &fs_info->caching_block_groups, list) |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10420 | if (ctl->block_group == block_group) { |
| 10421 | caching_ctl = ctl; |
| 10422 | atomic_inc(&caching_ctl->count); |
| 10423 | break; |
| 10424 | } |
| 10425 | } |
| 10426 | if (caching_ctl) |
| 10427 | list_del_init(&caching_ctl->list); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10428 | up_write(&fs_info->commit_root_sem); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10429 | if (caching_ctl) { |
| 10430 | /* Once for the caching bgs list and once for us. */ |
| 10431 | put_caching_control(caching_ctl); |
| 10432 | put_caching_control(caching_ctl); |
| 10433 | } |
| 10434 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10435 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10436 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10437 | if (!list_empty(&block_group->dirty_list)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10438 | WARN_ON(1); |
| 10439 | } |
| 10440 | if (!list_empty(&block_group->io_list)) { |
| 10441 | WARN_ON(1); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10442 | } |
| 10443 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10444 | btrfs_remove_free_space_cache(block_group); |
| 10445 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10446 | spin_lock(&block_group->space_info->lock); |
Filipe Manana | 75c68e9 | 2015-01-16 13:24:40 +0000 | [diff] [blame] | 10447 | list_del_init(&block_group->ro_list); |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10448 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10449 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10450 | WARN_ON(block_group->space_info->total_bytes |
| 10451 | < block_group->key.offset); |
| 10452 | WARN_ON(block_group->space_info->bytes_readonly |
| 10453 | < block_group->key.offset); |
| 10454 | WARN_ON(block_group->space_info->disk_total |
| 10455 | < block_group->key.offset * factor); |
| 10456 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10457 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 10458 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10459 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10460 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10461 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 10462 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10463 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10464 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 10465 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 10466 | if (!list_empty(&em->list)) { |
| 10467 | /* We're in the transaction->pending_chunks list. */ |
| 10468 | free_extent_map(em); |
| 10469 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10470 | spin_lock(&block_group->lock); |
| 10471 | block_group->removed = 1; |
| 10472 | /* |
| 10473 | * At this point trimming can't start on this block group, because we |
| 10474 | * removed the block group from the tree fs_info->block_group_cache_tree |
| 10475 | * so no one can't find it anymore and even if someone already got this |
| 10476 | * block group before we removed it from the rbtree, they have already |
| 10477 | * incremented block_group->trimming - if they didn't, they won't find |
| 10478 | * any free space entries because we already removed them all when we |
| 10479 | * called btrfs_remove_free_space_cache(). |
| 10480 | * |
| 10481 | * And we must not remove the extent map from the fs_info->mapping_tree |
| 10482 | * to prevent the same logical address range and physical device space |
| 10483 | * ranges from being reused for a new block group. This is because our |
| 10484 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is |
| 10485 | * completely transactionless, so while it is trimming a range the |
| 10486 | * currently running transaction might finish and a new one start, |
| 10487 | * allowing for new block groups to be created that can reuse the same |
| 10488 | * physical device locations unless we take this special care. |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10489 | * |
| 10490 | * There may also be an implicit trim operation if the file system |
| 10491 | * is mounted with -odiscard. The same protections must remain |
| 10492 | * in place until the extents have been discarded completely when |
| 10493 | * the transaction commit has completed. |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10494 | */ |
| 10495 | remove_em = (atomic_read(&block_group->trimming) == 0); |
| 10496 | /* |
| 10497 | * Make sure a trimmer task always sees the em in the pinned_chunks list |
| 10498 | * if it sees block_group->removed == 1 (needs to lock block_group->lock |
| 10499 | * before checking block_group->removed). |
| 10500 | */ |
| 10501 | if (!remove_em) { |
| 10502 | /* |
| 10503 | * Our em might be in trans->transaction->pending_chunks which |
| 10504 | * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks), |
| 10505 | * and so is the fs_info->pinned_chunks list. |
| 10506 | * |
| 10507 | * So at this point we must be holding the chunk_mutex to avoid |
| 10508 | * any races with chunk allocation (more specifically at |
| 10509 | * volumes.c:contains_pending_extent()), to ensure it always |
| 10510 | * sees the em, either in the pending_chunks list or in the |
| 10511 | * pinned_chunks list. |
| 10512 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10513 | list_move_tail(&em->list, &fs_info->pinned_chunks); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10514 | } |
| 10515 | spin_unlock(&block_group->lock); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10516 | |
| 10517 | if (remove_em) { |
| 10518 | struct extent_map_tree *em_tree; |
| 10519 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10520 | em_tree = &fs_info->mapping_tree.map_tree; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10521 | write_lock(&em_tree->lock); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10522 | /* |
| 10523 | * The em might be in the pending_chunks list, so make sure the |
| 10524 | * chunk mutex is locked, since remove_extent_mapping() will |
| 10525 | * delete us from that list. |
| 10526 | */ |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10527 | remove_extent_mapping(em_tree, em); |
| 10528 | write_unlock(&em_tree->lock); |
| 10529 | /* once for the tree */ |
| 10530 | free_extent_map(em); |
| 10531 | } |
| 10532 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 10533 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10534 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10535 | ret = remove_block_group_free_space(trans, fs_info, block_group); |
Omar Sandoval | 1e144fb | 2015-09-29 20:50:37 -0700 | [diff] [blame] | 10536 | if (ret) |
| 10537 | goto out; |
| 10538 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 10539 | btrfs_put_block_group(block_group); |
| 10540 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10541 | |
| 10542 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 10543 | if (ret > 0) |
| 10544 | ret = -EIO; |
| 10545 | if (ret < 0) |
| 10546 | goto out; |
| 10547 | |
| 10548 | ret = btrfs_del_item(trans, root, path); |
| 10549 | out: |
| 10550 | btrfs_free_path(path); |
| 10551 | return ret; |
| 10552 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10553 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10554 | struct btrfs_trans_handle * |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10555 | btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info, |
| 10556 | const u64 chunk_offset) |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10557 | { |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10558 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 10559 | struct extent_map *em; |
| 10560 | struct map_lookup *map; |
| 10561 | unsigned int num_items; |
| 10562 | |
| 10563 | read_lock(&em_tree->lock); |
| 10564 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 10565 | read_unlock(&em_tree->lock); |
| 10566 | ASSERT(em && em->start == chunk_offset); |
| 10567 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10568 | /* |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10569 | * We need to reserve 3 + N units from the metadata space info in order |
| 10570 | * to remove a block group (done at btrfs_remove_chunk() and at |
| 10571 | * btrfs_remove_block_group()), which are used for: |
| 10572 | * |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10573 | * 1 unit for adding the free space inode's orphan (located in the tree |
| 10574 | * of tree roots). |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10575 | * 1 unit for deleting the block group item (located in the extent |
| 10576 | * tree). |
| 10577 | * 1 unit for deleting the free space item (located in tree of tree |
| 10578 | * roots). |
| 10579 | * N units for deleting N device extent items corresponding to each |
| 10580 | * stripe (located in the device tree). |
| 10581 | * |
| 10582 | * In order to remove a block group we also need to reserve units in the |
| 10583 | * system space info in order to update the chunk tree (update one or |
| 10584 | * more device items and remove one chunk item), but this is done at |
| 10585 | * btrfs_remove_chunk() through a call to check_system_chunk(). |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10586 | */ |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 10587 | map = em->map_lookup; |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10588 | num_items = 3 + map->num_stripes; |
| 10589 | free_extent_map(em); |
| 10590 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10591 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10592 | num_items, 1); |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10593 | } |
| 10594 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10595 | /* |
| 10596 | * Process the unused_bgs list and remove any that don't have any allocated |
| 10597 | * space inside of them. |
| 10598 | */ |
| 10599 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) |
| 10600 | { |
| 10601 | struct btrfs_block_group_cache *block_group; |
| 10602 | struct btrfs_space_info *space_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10603 | struct btrfs_trans_handle *trans; |
| 10604 | int ret = 0; |
| 10605 | |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 10606 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10607 | return; |
| 10608 | |
| 10609 | spin_lock(&fs_info->unused_bgs_lock); |
| 10610 | while (!list_empty(&fs_info->unused_bgs)) { |
| 10611 | u64 start, end; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10612 | int trimming; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10613 | |
| 10614 | block_group = list_first_entry(&fs_info->unused_bgs, |
| 10615 | struct btrfs_block_group_cache, |
| 10616 | bg_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10617 | list_del_init(&block_group->bg_list); |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10618 | |
| 10619 | space_info = block_group->space_info; |
| 10620 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10621 | if (ret || btrfs_mixed_space_info(space_info)) { |
| 10622 | btrfs_put_block_group(block_group); |
| 10623 | continue; |
| 10624 | } |
| 10625 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10626 | |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10627 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 10628 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10629 | /* Don't want to race with allocators so take the groups_sem */ |
| 10630 | down_write(&space_info->groups_sem); |
| 10631 | spin_lock(&block_group->lock); |
| 10632 | if (block_group->reserved || |
| 10633 | btrfs_block_group_used(&block_group->item) || |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 10634 | block_group->ro || |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10635 | list_is_singular(&block_group->list)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10636 | /* |
| 10637 | * We want to bail if we made new allocations or have |
| 10638 | * outstanding allocations in this block group. We do |
| 10639 | * the ro check in case balance is currently acting on |
| 10640 | * this block group. |
| 10641 | */ |
| 10642 | spin_unlock(&block_group->lock); |
| 10643 | up_write(&space_info->groups_sem); |
| 10644 | goto next; |
| 10645 | } |
| 10646 | spin_unlock(&block_group->lock); |
| 10647 | |
| 10648 | /* 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] | 10649 | ret = inc_block_group_ro(block_group, 0); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10650 | up_write(&space_info->groups_sem); |
| 10651 | if (ret < 0) { |
| 10652 | ret = 0; |
| 10653 | goto next; |
| 10654 | } |
| 10655 | |
| 10656 | /* |
| 10657 | * Want to do this before we do anything else so we can recover |
| 10658 | * properly if we fail to join the transaction. |
| 10659 | */ |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10660 | trans = btrfs_start_trans_remove_block_group(fs_info, |
| 10661 | block_group->key.objectid); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10662 | if (IS_ERR(trans)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10663 | btrfs_dec_block_group_ro(block_group); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10664 | ret = PTR_ERR(trans); |
| 10665 | goto next; |
| 10666 | } |
| 10667 | |
| 10668 | /* |
| 10669 | * We could have pending pinned extents for this block group, |
| 10670 | * just delete them, we don't care about them anymore. |
| 10671 | */ |
| 10672 | start = block_group->key.objectid; |
| 10673 | end = start + block_group->key.offset - 1; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10674 | /* |
| 10675 | * Hold the unused_bg_unpin_mutex lock to avoid racing with |
| 10676 | * btrfs_finish_extent_commit(). If we are at transaction N, |
| 10677 | * another task might be running finish_extent_commit() for the |
| 10678 | * previous transaction N - 1, and have seen a range belonging |
| 10679 | * to the block group in freed_extents[] before we were able to |
| 10680 | * clear the whole block group range from freed_extents[]. This |
| 10681 | * means that task can lookup for the block group after we |
| 10682 | * unpinned it from freed_extents[] and removed it, leading to |
| 10683 | * a BUG_ON() at btrfs_unpin_extent_range(). |
| 10684 | */ |
| 10685 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10686 | ret = clear_extent_bits(&fs_info->freed_extents[0], 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 | } |
| 10693 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 10694 | EXTENT_DIRTY); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10695 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10696 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10697 | btrfs_dec_block_group_ro(block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10698 | goto end_trans; |
| 10699 | } |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10700 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10701 | |
| 10702 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10703 | spin_lock(&space_info->lock); |
| 10704 | spin_lock(&block_group->lock); |
| 10705 | |
| 10706 | space_info->bytes_pinned -= block_group->pinned; |
| 10707 | space_info->bytes_readonly += block_group->pinned; |
| 10708 | percpu_counter_add(&space_info->total_bytes_pinned, |
| 10709 | -block_group->pinned); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10710 | block_group->pinned = 0; |
| 10711 | |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10712 | spin_unlock(&block_group->lock); |
| 10713 | spin_unlock(&space_info->lock); |
| 10714 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10715 | /* DISCARD can flip during remount */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10716 | trimming = btrfs_test_opt(fs_info, DISCARD); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10717 | |
| 10718 | /* Implicit trim during transaction commit. */ |
| 10719 | if (trimming) |
| 10720 | btrfs_get_block_group_trimming(block_group); |
| 10721 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10722 | /* |
| 10723 | * Btrfs_remove_chunk will abort the transaction if things go |
| 10724 | * horribly wrong. |
| 10725 | */ |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 10726 | ret = btrfs_remove_chunk(trans, fs_info, |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10727 | block_group->key.objectid); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10728 | |
| 10729 | if (ret) { |
| 10730 | if (trimming) |
| 10731 | btrfs_put_block_group_trimming(block_group); |
| 10732 | goto end_trans; |
| 10733 | } |
| 10734 | |
| 10735 | /* |
| 10736 | * If we're not mounted with -odiscard, we can just forget |
| 10737 | * about this block group. Otherwise we'll need to wait |
| 10738 | * until transaction commit to do the actual discard. |
| 10739 | */ |
| 10740 | if (trimming) { |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10741 | spin_lock(&fs_info->unused_bgs_lock); |
| 10742 | /* |
| 10743 | * A concurrent scrub might have added us to the list |
| 10744 | * fs_info->unused_bgs, so use a list_move operation |
| 10745 | * to add the block group to the deleted_bgs list. |
| 10746 | */ |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10747 | list_move(&block_group->bg_list, |
| 10748 | &trans->transaction->deleted_bgs); |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10749 | spin_unlock(&fs_info->unused_bgs_lock); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10750 | btrfs_get_block_group(block_group); |
| 10751 | } |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10752 | end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 10753 | btrfs_end_transaction(trans); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10754 | next: |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10755 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10756 | btrfs_put_block_group(block_group); |
| 10757 | spin_lock(&fs_info->unused_bgs_lock); |
| 10758 | } |
| 10759 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10760 | } |
| 10761 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10762 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 10763 | { |
| 10764 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10765 | struct btrfs_super_block *disk_super; |
| 10766 | u64 features; |
| 10767 | u64 flags; |
| 10768 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10769 | int ret; |
| 10770 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 10771 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10772 | if (!btrfs_super_root(disk_super)) |
Dan Carpenter | 0dc924c5 | 2016-01-13 15:21:17 +0300 | [diff] [blame] | 10773 | return -EINVAL; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10774 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10775 | features = btrfs_super_incompat_flags(disk_super); |
| 10776 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 10777 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10778 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10779 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10780 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10781 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10782 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10783 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10784 | if (mixed) { |
| 10785 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10786 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10787 | } else { |
| 10788 | flags = BTRFS_BLOCK_GROUP_METADATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10789 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10790 | if (ret) |
| 10791 | goto out; |
| 10792 | |
| 10793 | flags = BTRFS_BLOCK_GROUP_DATA; |
Josef Bacik | e40edf2 | 2016-03-25 13:25:47 -0400 | [diff] [blame] | 10794 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10795 | } |
| 10796 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10797 | return ret; |
| 10798 | } |
| 10799 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10800 | int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, |
| 10801 | u64 start, u64 end) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10802 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10803 | return unpin_extent_range(fs_info, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10804 | } |
| 10805 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10806 | /* |
| 10807 | * It used to be that old block groups would be left around forever. |
| 10808 | * Iterating over them would be enough to trim unused space. Since we |
| 10809 | * now automatically remove them, we also need to iterate over unallocated |
| 10810 | * space. |
| 10811 | * |
| 10812 | * We don't want a transaction for this since the discard may take a |
| 10813 | * substantial amount of time. We don't require that a transaction be |
| 10814 | * running, but we do need to take a running transaction into account |
| 10815 | * to ensure that we're not discarding chunks that were released in |
| 10816 | * the current transaction. |
| 10817 | * |
| 10818 | * Holding the chunks lock will prevent other threads from allocating |
| 10819 | * or releasing chunks, but it won't prevent a running transaction |
| 10820 | * from committing and releasing the memory that the pending chunks |
| 10821 | * list head uses. For that, we need to take a reference to the |
| 10822 | * transaction. |
| 10823 | */ |
| 10824 | static int btrfs_trim_free_extents(struct btrfs_device *device, |
| 10825 | u64 minlen, u64 *trimmed) |
| 10826 | { |
| 10827 | u64 start = 0, len = 0; |
| 10828 | int ret; |
| 10829 | |
| 10830 | *trimmed = 0; |
| 10831 | |
| 10832 | /* Not writeable = nothing to do. */ |
| 10833 | if (!device->writeable) |
| 10834 | return 0; |
| 10835 | |
| 10836 | /* No free space = nothing to do. */ |
| 10837 | if (device->total_bytes <= device->bytes_used) |
| 10838 | return 0; |
| 10839 | |
| 10840 | ret = 0; |
| 10841 | |
| 10842 | while (1) { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 10843 | struct btrfs_fs_info *fs_info = device->fs_info; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10844 | struct btrfs_transaction *trans; |
| 10845 | u64 bytes; |
| 10846 | |
| 10847 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 10848 | if (ret) |
| 10849 | return ret; |
| 10850 | |
| 10851 | down_read(&fs_info->commit_root_sem); |
| 10852 | |
| 10853 | spin_lock(&fs_info->trans_lock); |
| 10854 | trans = fs_info->running_transaction; |
| 10855 | if (trans) |
| 10856 | atomic_inc(&trans->use_count); |
| 10857 | spin_unlock(&fs_info->trans_lock); |
| 10858 | |
| 10859 | ret = find_free_dev_extent_start(trans, device, minlen, start, |
| 10860 | &start, &len); |
| 10861 | if (trans) |
| 10862 | btrfs_put_transaction(trans); |
| 10863 | |
| 10864 | if (ret) { |
| 10865 | up_read(&fs_info->commit_root_sem); |
| 10866 | mutex_unlock(&fs_info->chunk_mutex); |
| 10867 | if (ret == -ENOSPC) |
| 10868 | ret = 0; |
| 10869 | break; |
| 10870 | } |
| 10871 | |
| 10872 | ret = btrfs_issue_discard(device->bdev, start, len, &bytes); |
| 10873 | up_read(&fs_info->commit_root_sem); |
| 10874 | mutex_unlock(&fs_info->chunk_mutex); |
| 10875 | |
| 10876 | if (ret) |
| 10877 | break; |
| 10878 | |
| 10879 | start += len; |
| 10880 | *trimmed += bytes; |
| 10881 | |
| 10882 | if (fatal_signal_pending(current)) { |
| 10883 | ret = -ERESTARTSYS; |
| 10884 | break; |
| 10885 | } |
| 10886 | |
| 10887 | cond_resched(); |
| 10888 | } |
| 10889 | |
| 10890 | return ret; |
| 10891 | } |
| 10892 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10893 | 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] | 10894 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10895 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10896 | struct btrfs_device *device; |
| 10897 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10898 | u64 group_trimmed; |
| 10899 | u64 start; |
| 10900 | u64 end; |
| 10901 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10902 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10903 | int ret = 0; |
| 10904 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10905 | /* |
| 10906 | * try to trim all FS space, our block group may start from non-zero. |
| 10907 | */ |
| 10908 | if (range->len == total_bytes) |
| 10909 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 10910 | else |
| 10911 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10912 | |
| 10913 | while (cache) { |
| 10914 | if (cache->key.objectid >= (range->start + range->len)) { |
| 10915 | btrfs_put_block_group(cache); |
| 10916 | break; |
| 10917 | } |
| 10918 | |
| 10919 | start = max(range->start, cache->key.objectid); |
| 10920 | end = min(range->start + range->len, |
| 10921 | cache->key.objectid + cache->key.offset); |
| 10922 | |
| 10923 | if (end - start >= range->minlen) { |
| 10924 | if (!block_group_cache_done(cache)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 10925 | ret = cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 10926 | if (ret) { |
| 10927 | btrfs_put_block_group(cache); |
| 10928 | break; |
| 10929 | } |
| 10930 | ret = wait_block_group_cache_done(cache); |
| 10931 | if (ret) { |
| 10932 | btrfs_put_block_group(cache); |
| 10933 | break; |
| 10934 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10935 | } |
| 10936 | ret = btrfs_trim_block_group(cache, |
| 10937 | &group_trimmed, |
| 10938 | start, |
| 10939 | end, |
| 10940 | range->minlen); |
| 10941 | |
| 10942 | trimmed += group_trimmed; |
| 10943 | if (ret) { |
| 10944 | btrfs_put_block_group(cache); |
| 10945 | break; |
| 10946 | } |
| 10947 | } |
| 10948 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 10949 | cache = next_block_group(fs_info, cache); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10950 | } |
| 10951 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10952 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 10953 | devices = &fs_info->fs_devices->alloc_list; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10954 | list_for_each_entry(device, devices, dev_alloc_list) { |
| 10955 | ret = btrfs_trim_free_extents(device, range->minlen, |
| 10956 | &group_trimmed); |
| 10957 | if (ret) |
| 10958 | break; |
| 10959 | |
| 10960 | trimmed += group_trimmed; |
| 10961 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 10962 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10963 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10964 | range->len = trimmed; |
| 10965 | return ret; |
| 10966 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10967 | |
| 10968 | /* |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10969 | * btrfs_{start,end}_write_no_snapshoting() are similar to |
| 10970 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 10971 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 10972 | * but flush the data into disk after the snapshot creation, or to prevent |
| 10973 | * operations while snapshoting is ongoing and that cause the snapshot to be |
| 10974 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10975 | */ |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10976 | void btrfs_end_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10977 | { |
| 10978 | percpu_counter_dec(&root->subv_writers->counter); |
| 10979 | /* |
David Sterba | a83342a | 2015-02-16 19:36:47 +0100 | [diff] [blame] | 10980 | * Make sure counter is updated before we wake up waiters. |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10981 | */ |
| 10982 | smp_mb(); |
| 10983 | if (waitqueue_active(&root->subv_writers->wait)) |
| 10984 | wake_up(&root->subv_writers->wait); |
| 10985 | } |
| 10986 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10987 | int btrfs_start_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10988 | { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10989 | if (atomic_read(&root->will_be_snapshoted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10990 | return 0; |
| 10991 | |
| 10992 | percpu_counter_inc(&root->subv_writers->counter); |
| 10993 | /* |
| 10994 | * Make sure counter is updated before we check for snapshot creation. |
| 10995 | */ |
| 10996 | smp_mb(); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10997 | if (atomic_read(&root->will_be_snapshoted)) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10998 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10999 | return 0; |
| 11000 | } |
| 11001 | return 1; |
| 11002 | } |
Zhao Lei | 0bc19f90 | 2016-01-06 18:56:36 +0800 | [diff] [blame] | 11003 | |
| 11004 | static int wait_snapshoting_atomic_t(atomic_t *a) |
| 11005 | { |
| 11006 | schedule(); |
| 11007 | return 0; |
| 11008 | } |
| 11009 | |
| 11010 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) |
| 11011 | { |
| 11012 | while (true) { |
| 11013 | int ret; |
| 11014 | |
| 11015 | ret = btrfs_start_write_no_snapshoting(root); |
| 11016 | if (ret) |
| 11017 | break; |
| 11018 | wait_on_atomic_t(&root->will_be_snapshoted, |
| 11019 | wait_snapshoting_atomic_t, |
| 11020 | TASK_UNINTERRUPTIBLE); |
| 11021 | } |
| 11022 | } |