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" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 36 | #include "math.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 37 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 38 | #include "qgroup.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 39 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 40 | #undef SCRAMBLE_DELAYED_REFS |
| 41 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 42 | /* |
| 43 | * control flags for do_chunk_alloc's force field |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 44 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 45 | * if we really need one. |
| 46 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 47 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 48 | * if we have very few chunks already allocated. This is |
| 49 | * used as part of the clustering code to help make sure |
| 50 | * we have a good pool of storage to cluster in, without |
| 51 | * filling the FS with empty chunks |
| 52 | * |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 53 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 54 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 55 | */ |
| 56 | enum { |
| 57 | CHUNK_ALLOC_NO_FORCE = 0, |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 58 | CHUNK_ALLOC_LIMITED = 1, |
| 59 | CHUNK_ALLOC_FORCE = 2, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 60 | }; |
| 61 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 62 | /* |
| 63 | * Control how reservations are dealt with. |
| 64 | * |
| 65 | * RESERVE_FREE - freeing a reservation. |
| 66 | * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for |
| 67 | * ENOSPC accounting |
| 68 | * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update |
| 69 | * bytes_may_use as the ENOSPC accounting is done elsewhere |
| 70 | */ |
| 71 | enum { |
| 72 | RESERVE_FREE = 0, |
| 73 | RESERVE_ALLOC = 1, |
| 74 | RESERVE_ALLOC_NO_ACCOUNT = 2, |
| 75 | }; |
| 76 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 77 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 78 | struct btrfs_root *root, u64 bytenr, |
| 79 | u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 80 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 81 | struct btrfs_root *root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 82 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 83 | u64 root_objectid, u64 owner_objectid, |
| 84 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 85 | struct btrfs_delayed_extent_op *extra_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 86 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 87 | struct extent_buffer *leaf, |
| 88 | struct btrfs_extent_item *ei); |
| 89 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 90 | struct btrfs_root *root, |
| 91 | u64 parent, u64 root_objectid, |
| 92 | u64 flags, u64 owner, u64 offset, |
| 93 | struct btrfs_key *ins, int ref_mod); |
| 94 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 95 | struct btrfs_root *root, |
| 96 | u64 parent, u64 root_objectid, |
| 97 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 98 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 99 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 100 | struct btrfs_root *extent_root, u64 flags, |
| 101 | int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 102 | static int find_next_key(struct btrfs_path *path, int level, |
| 103 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 104 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 105 | int dump_block_groups); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 106 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 107 | u64 num_bytes, int reserve, |
| 108 | int delalloc); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 109 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 110 | u64 num_bytes); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 111 | int btrfs_pin_extent(struct btrfs_root *root, |
| 112 | u64 bytenr, u64 num_bytes, int reserved); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 113 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 114 | static noinline int |
| 115 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 116 | { |
| 117 | smp_mb(); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 118 | return cache->cached == BTRFS_CACHE_FINISHED || |
| 119 | cache->cached == BTRFS_CACHE_ERROR; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 122 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 123 | { |
| 124 | return (cache->flags & bits) == bits; |
| 125 | } |
| 126 | |
Filipe Manana | 758f2df | 2015-11-19 11:45:48 +0000 | [diff] [blame] | 127 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 128 | { |
| 129 | atomic_inc(&cache->count); |
| 130 | } |
| 131 | |
| 132 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 133 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 134 | if (atomic_dec_and_test(&cache->count)) { |
| 135 | WARN_ON(cache->pinned > 0); |
| 136 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 137 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 138 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 139 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 142 | /* |
| 143 | * this adds the block group to the fs_info rb tree for the block group |
| 144 | * cache |
| 145 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 146 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 147 | struct btrfs_block_group_cache *block_group) |
| 148 | { |
| 149 | struct rb_node **p; |
| 150 | struct rb_node *parent = NULL; |
| 151 | struct btrfs_block_group_cache *cache; |
| 152 | |
| 153 | spin_lock(&info->block_group_cache_lock); |
| 154 | p = &info->block_group_cache_tree.rb_node; |
| 155 | |
| 156 | while (*p) { |
| 157 | parent = *p; |
| 158 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 159 | cache_node); |
| 160 | if (block_group->key.objectid < cache->key.objectid) { |
| 161 | p = &(*p)->rb_left; |
| 162 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 163 | p = &(*p)->rb_right; |
| 164 | } else { |
| 165 | spin_unlock(&info->block_group_cache_lock); |
| 166 | return -EEXIST; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | rb_link_node(&block_group->cache_node, parent, p); |
| 171 | rb_insert_color(&block_group->cache_node, |
| 172 | &info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 173 | |
| 174 | if (info->first_logical_byte > block_group->key.objectid) |
| 175 | info->first_logical_byte = block_group->key.objectid; |
| 176 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 177 | spin_unlock(&info->block_group_cache_lock); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * This will return the block group at or after bytenr if contains is 0, else |
| 184 | * it will return the block group that contains the bytenr |
| 185 | */ |
| 186 | static struct btrfs_block_group_cache * |
| 187 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 188 | int contains) |
| 189 | { |
| 190 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 191 | struct rb_node *n; |
| 192 | u64 end, start; |
| 193 | |
| 194 | spin_lock(&info->block_group_cache_lock); |
| 195 | n = info->block_group_cache_tree.rb_node; |
| 196 | |
| 197 | while (n) { |
| 198 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 199 | cache_node); |
| 200 | end = cache->key.objectid + cache->key.offset - 1; |
| 201 | start = cache->key.objectid; |
| 202 | |
| 203 | if (bytenr < start) { |
| 204 | if (!contains && (!ret || start < ret->key.objectid)) |
| 205 | ret = cache; |
| 206 | n = n->rb_left; |
| 207 | } else if (bytenr > start) { |
| 208 | if (contains && bytenr <= end) { |
| 209 | ret = cache; |
| 210 | break; |
| 211 | } |
| 212 | n = n->rb_right; |
| 213 | } else { |
| 214 | ret = cache; |
| 215 | break; |
| 216 | } |
| 217 | } |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 218 | if (ret) { |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 219 | btrfs_get_block_group(ret); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 220 | if (bytenr == 0 && info->first_logical_byte > ret->key.objectid) |
| 221 | info->first_logical_byte = ret->key.objectid; |
| 222 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 223 | spin_unlock(&info->block_group_cache_lock); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | static int add_excluded_extent(struct btrfs_root *root, |
| 229 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 230 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 231 | u64 end = start + num_bytes - 1; |
| 232 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 233 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 234 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 235 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 236 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 239 | static void free_excluded_extents(struct btrfs_root *root, |
| 240 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 241 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 242 | u64 start, end; |
| 243 | |
| 244 | start = cache->key.objectid; |
| 245 | end = start + cache->key.offset - 1; |
| 246 | |
| 247 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 248 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 249 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 250 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 251 | } |
| 252 | |
| 253 | static int exclude_super_stripes(struct btrfs_root *root, |
| 254 | struct btrfs_block_group_cache *cache) |
| 255 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 256 | u64 bytenr; |
| 257 | u64 *logical; |
| 258 | int stripe_len; |
| 259 | int i, nr, ret; |
| 260 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 261 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 262 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 263 | cache->bytes_super += stripe_len; |
| 264 | ret = add_excluded_extent(root, cache->key.objectid, |
| 265 | stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 266 | if (ret) |
| 267 | return ret; |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 270 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 271 | bytenr = btrfs_sb_offset(i); |
| 272 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 273 | cache->key.objectid, bytenr, |
| 274 | 0, &logical, &nr, &stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 275 | if (ret) |
| 276 | return ret; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 277 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 278 | while (nr--) { |
Josef Bacik | 51bf5f0 | 2013-04-23 12:55:21 -0400 | [diff] [blame] | 279 | u64 start, len; |
| 280 | |
| 281 | if (logical[nr] > cache->key.objectid + |
| 282 | cache->key.offset) |
| 283 | continue; |
| 284 | |
| 285 | if (logical[nr] + stripe_len <= cache->key.objectid) |
| 286 | continue; |
| 287 | |
| 288 | start = logical[nr]; |
| 289 | if (start < cache->key.objectid) { |
| 290 | start = cache->key.objectid; |
| 291 | len = (logical[nr] + stripe_len) - start; |
| 292 | } else { |
| 293 | len = min_t(u64, stripe_len, |
| 294 | cache->key.objectid + |
| 295 | cache->key.offset - start); |
| 296 | } |
| 297 | |
| 298 | cache->bytes_super += len; |
| 299 | ret = add_excluded_extent(root, start, len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 300 | if (ret) { |
| 301 | kfree(logical); |
| 302 | return ret; |
| 303 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 304 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 305 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 306 | kfree(logical); |
| 307 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 311 | static struct btrfs_caching_control * |
| 312 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 313 | { |
| 314 | struct btrfs_caching_control *ctl; |
| 315 | |
| 316 | spin_lock(&cache->lock); |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 317 | if (!cache->caching_ctl) { |
| 318 | spin_unlock(&cache->lock); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 322 | ctl = cache->caching_ctl; |
| 323 | atomic_inc(&ctl->count); |
| 324 | spin_unlock(&cache->lock); |
| 325 | return ctl; |
| 326 | } |
| 327 | |
| 328 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 329 | { |
| 330 | if (atomic_dec_and_test(&ctl->count)) |
| 331 | kfree(ctl); |
| 332 | } |
| 333 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 334 | #ifdef CONFIG_BTRFS_DEBUG |
| 335 | static void fragment_free_space(struct btrfs_root *root, |
| 336 | struct btrfs_block_group_cache *block_group) |
| 337 | { |
| 338 | u64 start = block_group->key.objectid; |
| 339 | u64 len = block_group->key.offset; |
| 340 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? |
| 341 | root->nodesize : root->sectorsize; |
| 342 | u64 step = chunk << 1; |
| 343 | |
| 344 | while (len > chunk) { |
| 345 | btrfs_remove_free_space(block_group, start, chunk); |
| 346 | start += step; |
| 347 | if (len < step) |
| 348 | len = 0; |
| 349 | else |
| 350 | len -= step; |
| 351 | } |
| 352 | } |
| 353 | #endif |
| 354 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 355 | /* |
| 356 | * this is only called by cache_block_group, since we could have freed extents |
| 357 | * we need to check the pinned_extents for any extents that can't be used yet |
| 358 | * since their free space will be released as soon as the transaction commits. |
| 359 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 360 | static u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 361 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 362 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 363 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 364 | int ret; |
| 365 | |
| 366 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 367 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 368 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 369 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 370 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 371 | if (ret) |
| 372 | break; |
| 373 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 374 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 375 | start = extent_end + 1; |
| 376 | } else if (extent_start > start && extent_start < end) { |
| 377 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 378 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 379 | ret = btrfs_add_free_space(block_group, start, |
| 380 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 381 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 382 | start = extent_end + 1; |
| 383 | } else { |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (start < end) { |
| 389 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 390 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 391 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 392 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 395 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 398 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 399 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 400 | struct btrfs_block_group_cache *block_group; |
| 401 | struct btrfs_fs_info *fs_info; |
| 402 | struct btrfs_caching_control *caching_ctl; |
| 403 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 404 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 405 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 406 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 407 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 408 | u64 last = 0; |
| 409 | u32 nritems; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 410 | int ret = -ENOMEM; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 411 | bool wakeup = true; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 412 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 413 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 414 | block_group = caching_ctl->block_group; |
| 415 | fs_info = block_group->fs_info; |
| 416 | extent_root = fs_info->extent_root; |
| 417 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 418 | path = btrfs_alloc_path(); |
| 419 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 420 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 421 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 422 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 423 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 424 | #ifdef CONFIG_BTRFS_DEBUG |
| 425 | /* |
| 426 | * If we're fragmenting we don't want to make anybody think we can |
| 427 | * allocate from this block group until we've had a chance to fragment |
| 428 | * the free space. |
| 429 | */ |
| 430 | if (btrfs_should_fragment_free_space(extent_root, block_group)) |
| 431 | wakeup = false; |
| 432 | #endif |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 433 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 434 | * We don't want to deadlock with somebody trying to allocate a new |
| 435 | * extent for the extent root while also trying to search the extent |
| 436 | * root to add free space. So we skip locking and search the commit |
| 437 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 438 | */ |
| 439 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 440 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 441 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 442 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 443 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 444 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 445 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 446 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 447 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 448 | /* need to make sure the commit_root doesn't disappear */ |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 449 | down_read(&fs_info->commit_root_sem); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 450 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 451 | next: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 452 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 453 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 454 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 455 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 456 | leaf = path->nodes[0]; |
| 457 | nritems = btrfs_header_nritems(leaf); |
| 458 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 459 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 460 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 461 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 462 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 463 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 464 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 465 | if (path->slots[0] < nritems) { |
| 466 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 467 | } else { |
| 468 | ret = find_next_key(path, 0, &key); |
| 469 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 470 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 471 | |
Josef Bacik | c9ea7b2 | 2013-09-19 10:02:11 -0400 | [diff] [blame] | 472 | if (need_resched() || |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 473 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 474 | if (wakeup) |
| 475 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 476 | btrfs_release_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 477 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 478 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 479 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 480 | goto again; |
| 481 | } |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 482 | |
| 483 | ret = btrfs_next_leaf(extent_root, path); |
| 484 | if (ret < 0) |
| 485 | goto err; |
| 486 | if (ret) |
| 487 | break; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 488 | leaf = path->nodes[0]; |
| 489 | nritems = btrfs_header_nritems(leaf); |
| 490 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 491 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 492 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 493 | if (key.objectid < last) { |
| 494 | key.objectid = last; |
| 495 | key.offset = 0; |
| 496 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 497 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 498 | if (wakeup) |
| 499 | caching_ctl->progress = last; |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 500 | btrfs_release_path(path); |
| 501 | goto next; |
| 502 | } |
| 503 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 504 | if (key.objectid < block_group->key.objectid) { |
| 505 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 506 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 507 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 508 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 509 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 510 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 511 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 512 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 513 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 514 | key.type == BTRFS_METADATA_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 515 | total_found += add_new_free_space(block_group, |
| 516 | fs_info, last, |
| 517 | key.objectid); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 518 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 519 | last = key.objectid + |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 520 | fs_info->tree_root->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 521 | else |
| 522 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 523 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 524 | if (total_found > (1024 * 1024 * 2)) { |
| 525 | total_found = 0; |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 526 | if (wakeup) |
| 527 | wake_up(&caching_ctl->wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 528 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 529 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 530 | path->slots[0]++; |
| 531 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 532 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 533 | |
| 534 | total_found += add_new_free_space(block_group, fs_info, last, |
| 535 | block_group->key.objectid + |
| 536 | block_group->key.offset); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 537 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 538 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 539 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 540 | spin_unlock(&block_group->lock); |
| 541 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 542 | #ifdef CONFIG_BTRFS_DEBUG |
| 543 | if (btrfs_should_fragment_free_space(extent_root, block_group)) { |
| 544 | u64 bytes_used; |
| 545 | |
| 546 | spin_lock(&block_group->space_info->lock); |
| 547 | spin_lock(&block_group->lock); |
| 548 | bytes_used = block_group->key.offset - |
| 549 | btrfs_block_group_used(&block_group->item); |
| 550 | block_group->space_info->bytes_used += bytes_used >> 1; |
| 551 | spin_unlock(&block_group->lock); |
| 552 | spin_unlock(&block_group->space_info->lock); |
| 553 | fragment_free_space(extent_root, block_group); |
| 554 | } |
| 555 | #endif |
| 556 | |
| 557 | caching_ctl->progress = (u64)-1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 558 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 559 | btrfs_free_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 560 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 561 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 562 | free_excluded_extents(extent_root, block_group); |
| 563 | |
| 564 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 565 | out: |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 566 | if (ret) { |
| 567 | spin_lock(&block_group->lock); |
| 568 | block_group->caching_ctl = NULL; |
| 569 | block_group->cached = BTRFS_CACHE_ERROR; |
| 570 | spin_unlock(&block_group->lock); |
| 571 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 572 | wake_up(&caching_ctl->wait); |
| 573 | |
| 574 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 575 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 578 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 579 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 580 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 581 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 582 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 583 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 584 | int ret = 0; |
| 585 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 586 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 587 | if (!caching_ctl) |
| 588 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 589 | |
| 590 | INIT_LIST_HEAD(&caching_ctl->list); |
| 591 | mutex_init(&caching_ctl->mutex); |
| 592 | init_waitqueue_head(&caching_ctl->wait); |
| 593 | caching_ctl->block_group = cache; |
| 594 | caching_ctl->progress = cache->key.objectid; |
| 595 | atomic_set(&caching_ctl->count, 1); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 596 | btrfs_init_work(&caching_ctl->work, btrfs_cache_helper, |
| 597 | caching_thread, NULL, NULL); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 598 | |
| 599 | spin_lock(&cache->lock); |
| 600 | /* |
| 601 | * This should be a rare occasion, but this could happen I think in the |
| 602 | * case where one thread starts to load the space cache info, and then |
| 603 | * some other thread starts a transaction commit which tries to do an |
| 604 | * allocation while the other thread is still loading the space cache |
| 605 | * info. The previous loop should have kept us from choosing this block |
| 606 | * group, but if we've moved to the state where we will wait on caching |
| 607 | * block groups we need to first check if we're doing a fast load here, |
| 608 | * so we can wait for it to finish, otherwise we could end up allocating |
| 609 | * from a block group who's cache gets evicted for one reason or |
| 610 | * another. |
| 611 | */ |
| 612 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 613 | struct btrfs_caching_control *ctl; |
| 614 | |
| 615 | ctl = cache->caching_ctl; |
| 616 | atomic_inc(&ctl->count); |
| 617 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 618 | spin_unlock(&cache->lock); |
| 619 | |
| 620 | schedule(); |
| 621 | |
| 622 | finish_wait(&ctl->wait, &wait); |
| 623 | put_caching_control(ctl); |
| 624 | spin_lock(&cache->lock); |
| 625 | } |
| 626 | |
| 627 | if (cache->cached != BTRFS_CACHE_NO) { |
| 628 | spin_unlock(&cache->lock); |
| 629 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 630 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 631 | } |
| 632 | WARN_ON(cache->caching_ctl); |
| 633 | cache->caching_ctl = caching_ctl; |
| 634 | cache->cached = BTRFS_CACHE_FAST; |
| 635 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 636 | |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 637 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 638 | mutex_lock(&caching_ctl->mutex); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 639 | ret = load_free_space_cache(fs_info, cache); |
| 640 | |
| 641 | spin_lock(&cache->lock); |
| 642 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 643 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 644 | cache->cached = BTRFS_CACHE_FINISHED; |
| 645 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 646 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 647 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 648 | if (load_cache_only) { |
| 649 | cache->caching_ctl = NULL; |
| 650 | cache->cached = BTRFS_CACHE_NO; |
| 651 | } else { |
| 652 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 653 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 654 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 655 | } |
| 656 | spin_unlock(&cache->lock); |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 657 | #ifdef CONFIG_BTRFS_DEBUG |
| 658 | if (ret == 1 && |
| 659 | btrfs_should_fragment_free_space(fs_info->extent_root, |
| 660 | cache)) { |
| 661 | u64 bytes_used; |
| 662 | |
| 663 | spin_lock(&cache->space_info->lock); |
| 664 | spin_lock(&cache->lock); |
| 665 | bytes_used = cache->key.offset - |
| 666 | btrfs_block_group_used(&cache->item); |
| 667 | cache->space_info->bytes_used += bytes_used >> 1; |
| 668 | spin_unlock(&cache->lock); |
| 669 | spin_unlock(&cache->space_info->lock); |
| 670 | fragment_free_space(fs_info->extent_root, cache); |
| 671 | } |
| 672 | #endif |
Josef Bacik | cb83b7b | 2014-11-26 11:52:54 -0500 | [diff] [blame] | 673 | mutex_unlock(&caching_ctl->mutex); |
| 674 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 675 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 676 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 677 | put_caching_control(caching_ctl); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 678 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 679 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 680 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 681 | } else { |
| 682 | /* |
| 683 | * We are not going to do the fast caching, set cached to the |
| 684 | * appropriate value and wakeup any waiters. |
| 685 | */ |
| 686 | spin_lock(&cache->lock); |
| 687 | if (load_cache_only) { |
| 688 | cache->caching_ctl = NULL; |
| 689 | cache->cached = BTRFS_CACHE_NO; |
| 690 | } else { |
| 691 | cache->cached = BTRFS_CACHE_STARTED; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 692 | cache->has_caching_ctl = 1; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 693 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 694 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 695 | wake_up(&caching_ctl->wait); |
| 696 | } |
| 697 | |
| 698 | if (load_cache_only) { |
| 699 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 700 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 701 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 702 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 703 | down_write(&fs_info->commit_root_sem); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 704 | atomic_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 705 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 706 | up_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 707 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 708 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 709 | |
Qu Wenruo | e66f0bb | 2014-02-28 10:46:12 +0800 | [diff] [blame] | 710 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 711 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 712 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 715 | /* |
| 716 | * return the block group that starts at or after bytenr |
| 717 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 718 | static struct btrfs_block_group_cache * |
| 719 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 720 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 721 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 722 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 723 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 724 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 725 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 728 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 729 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 730 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 731 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 732 | struct btrfs_fs_info *info, |
| 733 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 734 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 735 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 736 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 737 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 738 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 739 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 740 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 741 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 742 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 743 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 744 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 745 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 746 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 747 | |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 748 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 749 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 750 | rcu_read_lock(); |
| 751 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 752 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 753 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 754 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 755 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 756 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 757 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 758 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 759 | } |
| 760 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 761 | /* |
| 762 | * after adding space to the filesystem, we need to clear the full flags |
| 763 | * on all the space infos. |
| 764 | */ |
| 765 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 766 | { |
| 767 | struct list_head *head = &info->space_info; |
| 768 | struct btrfs_space_info *found; |
| 769 | |
| 770 | rcu_read_lock(); |
| 771 | list_for_each_entry_rcu(found, head, list) |
| 772 | found->full = 0; |
| 773 | rcu_read_unlock(); |
| 774 | } |
| 775 | |
Filipe Manana | 1a4ed8f | 2014-10-27 10:44:24 +0000 | [diff] [blame] | 776 | /* simple helper to search for an existing data extent at a given offset */ |
| 777 | int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 778 | { |
| 779 | int ret; |
| 780 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 781 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 782 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 783 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 784 | if (!path) |
| 785 | return -ENOMEM; |
| 786 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 787 | key.objectid = start; |
| 788 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 789 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 790 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 791 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 792 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 793 | return ret; |
| 794 | } |
| 795 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 796 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 797 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 798 | * |
| 799 | * the head node for delayed ref is used to store the sum of all the |
| 800 | * reference count modifications queued up in the rbtree. the head |
| 801 | * node may also store the extent flags to set. This way you can check |
| 802 | * to see what the reference count and extent flags would be if all of |
| 803 | * the delayed refs are not processed. |
| 804 | */ |
| 805 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 806 | struct btrfs_root *root, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 807 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 808 | { |
| 809 | struct btrfs_delayed_ref_head *head; |
| 810 | struct btrfs_delayed_ref_root *delayed_refs; |
| 811 | struct btrfs_path *path; |
| 812 | struct btrfs_extent_item *ei; |
| 813 | struct extent_buffer *leaf; |
| 814 | struct btrfs_key key; |
| 815 | u32 item_size; |
| 816 | u64 num_refs; |
| 817 | u64 extent_flags; |
| 818 | int ret; |
| 819 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 820 | /* |
| 821 | * If we don't have skinny metadata, don't bother doing anything |
| 822 | * different |
| 823 | */ |
| 824 | if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) { |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 825 | offset = root->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 826 | metadata = 0; |
| 827 | } |
| 828 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 829 | path = btrfs_alloc_path(); |
| 830 | if (!path) |
| 831 | return -ENOMEM; |
| 832 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 833 | if (!trans) { |
| 834 | path->skip_locking = 1; |
| 835 | path->search_commit_root = 1; |
| 836 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 837 | |
| 838 | search_again: |
| 839 | key.objectid = bytenr; |
| 840 | key.offset = offset; |
| 841 | if (metadata) |
| 842 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 843 | else |
| 844 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 845 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 846 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 847 | &key, path, 0, 0); |
| 848 | if (ret < 0) |
| 849 | goto out_free; |
| 850 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 851 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 852 | if (path->slots[0]) { |
| 853 | path->slots[0]--; |
| 854 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 855 | path->slots[0]); |
| 856 | if (key.objectid == bytenr && |
| 857 | key.type == BTRFS_EXTENT_ITEM_KEY && |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 858 | key.offset == root->nodesize) |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 859 | ret = 0; |
| 860 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 861 | } |
| 862 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 863 | if (ret == 0) { |
| 864 | leaf = path->nodes[0]; |
| 865 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 866 | if (item_size >= sizeof(*ei)) { |
| 867 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 868 | struct btrfs_extent_item); |
| 869 | num_refs = btrfs_extent_refs(leaf, ei); |
| 870 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 871 | } else { |
| 872 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 873 | struct btrfs_extent_item_v0 *ei0; |
| 874 | BUG_ON(item_size != sizeof(*ei0)); |
| 875 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 876 | struct btrfs_extent_item_v0); |
| 877 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 878 | /* FIXME: this isn't correct for data */ |
| 879 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 880 | #else |
| 881 | BUG(); |
| 882 | #endif |
| 883 | } |
| 884 | BUG_ON(num_refs == 0); |
| 885 | } else { |
| 886 | num_refs = 0; |
| 887 | extent_flags = 0; |
| 888 | ret = 0; |
| 889 | } |
| 890 | |
| 891 | if (!trans) |
| 892 | goto out; |
| 893 | |
| 894 | delayed_refs = &trans->transaction->delayed_refs; |
| 895 | spin_lock(&delayed_refs->lock); |
| 896 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 897 | if (head) { |
| 898 | if (!mutex_trylock(&head->mutex)) { |
| 899 | atomic_inc(&head->node.refs); |
| 900 | spin_unlock(&delayed_refs->lock); |
| 901 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 902 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 903 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 904 | /* |
| 905 | * Mutex was contended, block until it's released and try |
| 906 | * again |
| 907 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 908 | mutex_lock(&head->mutex); |
| 909 | mutex_unlock(&head->mutex); |
| 910 | btrfs_put_delayed_ref(&head->node); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 911 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 912 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 913 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 914 | if (head->extent_op && head->extent_op->update_flags) |
| 915 | extent_flags |= head->extent_op->flags_to_set; |
| 916 | else |
| 917 | BUG_ON(num_refs == 0); |
| 918 | |
| 919 | num_refs += head->node.ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 920 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 921 | mutex_unlock(&head->mutex); |
| 922 | } |
| 923 | spin_unlock(&delayed_refs->lock); |
| 924 | out: |
| 925 | WARN_ON(num_refs == 0); |
| 926 | if (refs) |
| 927 | *refs = num_refs; |
| 928 | if (flags) |
| 929 | *flags = extent_flags; |
| 930 | out_free: |
| 931 | btrfs_free_path(path); |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 936 | * Back reference rules. Back refs have three main goals: |
| 937 | * |
| 938 | * 1) differentiate between all holders of references to an extent so that |
| 939 | * when a reference is dropped we can make sure it was a valid reference |
| 940 | * before freeing the extent. |
| 941 | * |
| 942 | * 2) Provide enough information to quickly find the holders of an extent |
| 943 | * if we notice a given block is corrupted or bad. |
| 944 | * |
| 945 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 946 | * maintenance. This is actually the same as #2, but with a slightly |
| 947 | * different use case. |
| 948 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 949 | * There are two kinds of back refs. The implicit back refs is optimized |
| 950 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 951 | * back refs of this kind provide information about the block's owner tree |
| 952 | * and the pointer's key. These information allow us to find the block by |
| 953 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 954 | * referenced by their owner trees. The location of tree block is recorded |
| 955 | * in the back refs. Actually the full back refs is generic, and can be |
| 956 | * used in all cases the implicit back refs is used. The major shortcoming |
| 957 | * of the full back refs is its overhead. Every time a tree block gets |
| 958 | * COWed, we have to update back refs entry for all pointers in it. |
| 959 | * |
| 960 | * For a newly allocated tree block, we use implicit back refs for |
| 961 | * pointers in it. This means most tree related operations only involve |
| 962 | * implicit back refs. For a tree block created in old transaction, the |
| 963 | * only way to drop a reference to it is COW it. So we can detect the |
| 964 | * event that tree block loses its owner tree's reference and do the |
| 965 | * back refs conversion. |
| 966 | * |
| 967 | * When a tree block is COW'd through a tree, there are four cases: |
| 968 | * |
| 969 | * The reference count of the block is one and the tree is the block's |
| 970 | * owner tree. Nothing to do in this case. |
| 971 | * |
| 972 | * The reference count of the block is one and the tree is not the |
| 973 | * block's owner tree. In this case, full back refs is used for pointers |
| 974 | * in the block. Remove these full back refs, add implicit back refs for |
| 975 | * every pointers in the new block. |
| 976 | * |
| 977 | * The reference count of the block is greater than one and the tree is |
| 978 | * the block's owner tree. In this case, implicit back refs is used for |
| 979 | * pointers in the block. Add full back refs for every pointers in the |
| 980 | * block, increase lower level extents' reference counts. The original |
| 981 | * implicit back refs are entailed to the new block. |
| 982 | * |
| 983 | * The reference count of the block is greater than one and the tree is |
| 984 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 985 | * the new block, increase lower level extents' reference count. |
| 986 | * |
| 987 | * Back Reference Key composing: |
| 988 | * |
| 989 | * The key objectid corresponds to the first byte in the extent, |
| 990 | * The key type is used to differentiate between types of back refs. |
| 991 | * There are different meanings of the key offset for different types |
| 992 | * of back refs. |
| 993 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 994 | * File extents can be referenced by: |
| 995 | * |
| 996 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 997 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 998 | * - different offsets inside a file (bookend extents in file.c) |
| 999 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1000 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1001 | * |
| 1002 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1003 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1004 | * - original offset in the file |
| 1005 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1006 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1007 | * The key offset for the implicit back refs is hash of the first |
| 1008 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1009 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1010 | * The extent ref structure for the full back refs has field for: |
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 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1013 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1014 | * The key offset for the implicit back refs is the first byte of |
| 1015 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1016 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1017 | * When a file extent is allocated, The implicit back refs is used. |
| 1018 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1019 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1020 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 1021 | * |
| 1022 | * When a file extent is removed file truncation, we find the |
| 1023 | * corresponding implicit back refs and check the following fields: |
| 1024 | * |
| 1025 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1026 | * |
| 1027 | * Btree extents can be referenced by: |
| 1028 | * |
| 1029 | * - Different subvolumes |
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 | * Both the implicit back refs and the full back refs for tree blocks |
| 1032 | * only consist of key. The key offset for the implicit back refs is |
| 1033 | * objectid of block's owner tree. The key offset for the full back refs |
| 1034 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1035 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1036 | * When implicit back refs is used, information about the lowest key and |
| 1037 | * level of the tree block are required. These information are stored in |
| 1038 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1039 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1040 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1041 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1042 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 1043 | struct btrfs_root *root, |
| 1044 | struct btrfs_path *path, |
| 1045 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1046 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1047 | struct btrfs_extent_item *item; |
| 1048 | struct btrfs_extent_item_v0 *ei0; |
| 1049 | struct btrfs_extent_ref_v0 *ref0; |
| 1050 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1051 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1052 | struct btrfs_key key; |
| 1053 | struct btrfs_key found_key; |
| 1054 | u32 new_size = sizeof(*item); |
| 1055 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1056 | int ret; |
| 1057 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1058 | leaf = path->nodes[0]; |
| 1059 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1060 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1061 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1062 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1063 | struct btrfs_extent_item_v0); |
| 1064 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 1065 | |
| 1066 | if (owner == (u64)-1) { |
| 1067 | while (1) { |
| 1068 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1069 | ret = btrfs_next_leaf(root, path); |
| 1070 | if (ret < 0) |
| 1071 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1072 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1073 | leaf = path->nodes[0]; |
| 1074 | } |
| 1075 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1076 | path->slots[0]); |
| 1077 | BUG_ON(key.objectid != found_key.objectid); |
| 1078 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 1079 | path->slots[0]++; |
| 1080 | continue; |
| 1081 | } |
| 1082 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1083 | struct btrfs_extent_ref_v0); |
| 1084 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 1085 | break; |
| 1086 | } |
| 1087 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1088 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1089 | |
| 1090 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 1091 | new_size += sizeof(*bi); |
| 1092 | |
| 1093 | new_size -= sizeof(*ei0); |
| 1094 | ret = btrfs_search_slot(trans, root, &key, path, |
| 1095 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1096 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1097 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1098 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1099 | |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 1100 | btrfs_extend_item(root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1101 | |
| 1102 | leaf = path->nodes[0]; |
| 1103 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1104 | btrfs_set_extent_refs(leaf, item, refs); |
| 1105 | /* FIXME: get real generation */ |
| 1106 | btrfs_set_extent_generation(leaf, item, 0); |
| 1107 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1108 | btrfs_set_extent_flags(leaf, item, |
| 1109 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1110 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1111 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1112 | /* FIXME: get first key of the block */ |
| 1113 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 1114 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1115 | } else { |
| 1116 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1117 | } |
| 1118 | btrfs_mark_buffer_dirty(leaf); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | #endif |
| 1122 | |
| 1123 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1124 | { |
| 1125 | u32 high_crc = ~(u32)0; |
| 1126 | u32 low_crc = ~(u32)0; |
| 1127 | __le64 lenum; |
| 1128 | |
| 1129 | lenum = cpu_to_le64(root_objectid); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1130 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1131 | lenum = cpu_to_le64(owner); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1132 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1133 | lenum = cpu_to_le64(offset); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1134 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1135 | |
| 1136 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1137 | } |
| 1138 | |
| 1139 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1140 | struct btrfs_extent_data_ref *ref) |
| 1141 | { |
| 1142 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1143 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1144 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1145 | } |
| 1146 | |
| 1147 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1148 | struct btrfs_extent_data_ref *ref, |
| 1149 | u64 root_objectid, u64 owner, u64 offset) |
| 1150 | { |
| 1151 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1152 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1153 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1154 | return 0; |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
| 1158 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1159 | struct btrfs_root *root, |
| 1160 | struct btrfs_path *path, |
| 1161 | u64 bytenr, u64 parent, |
| 1162 | u64 root_objectid, |
| 1163 | u64 owner, u64 offset) |
| 1164 | { |
| 1165 | struct btrfs_key key; |
| 1166 | struct btrfs_extent_data_ref *ref; |
| 1167 | struct extent_buffer *leaf; |
| 1168 | u32 nritems; |
| 1169 | int ret; |
| 1170 | int recow; |
| 1171 | int err = -ENOENT; |
| 1172 | |
| 1173 | key.objectid = bytenr; |
| 1174 | if (parent) { |
| 1175 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1176 | key.offset = parent; |
| 1177 | } else { |
| 1178 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1179 | key.offset = hash_extent_data_ref(root_objectid, |
| 1180 | owner, offset); |
| 1181 | } |
| 1182 | again: |
| 1183 | recow = 0; |
| 1184 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1185 | if (ret < 0) { |
| 1186 | err = ret; |
| 1187 | goto fail; |
| 1188 | } |
| 1189 | |
| 1190 | if (parent) { |
| 1191 | if (!ret) |
| 1192 | return 0; |
| 1193 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1194 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1195 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1196 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1197 | if (ret < 0) { |
| 1198 | err = ret; |
| 1199 | goto fail; |
| 1200 | } |
| 1201 | if (!ret) |
| 1202 | return 0; |
| 1203 | #endif |
| 1204 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1208 | nritems = btrfs_header_nritems(leaf); |
| 1209 | while (1) { |
| 1210 | if (path->slots[0] >= nritems) { |
| 1211 | ret = btrfs_next_leaf(root, path); |
| 1212 | if (ret < 0) |
| 1213 | err = ret; |
| 1214 | if (ret) |
| 1215 | goto fail; |
| 1216 | |
| 1217 | leaf = path->nodes[0]; |
| 1218 | nritems = btrfs_header_nritems(leaf); |
| 1219 | recow = 1; |
| 1220 | } |
| 1221 | |
| 1222 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1223 | if (key.objectid != bytenr || |
| 1224 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1225 | goto fail; |
| 1226 | |
| 1227 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1228 | struct btrfs_extent_data_ref); |
| 1229 | |
| 1230 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1231 | owner, offset)) { |
| 1232 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1233 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1234 | goto again; |
| 1235 | } |
| 1236 | err = 0; |
| 1237 | break; |
| 1238 | } |
| 1239 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1240 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1241 | fail: |
| 1242 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1243 | } |
| 1244 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1245 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1246 | struct btrfs_root *root, |
| 1247 | struct btrfs_path *path, |
| 1248 | u64 bytenr, u64 parent, |
| 1249 | u64 root_objectid, u64 owner, |
| 1250 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1251 | { |
| 1252 | struct btrfs_key key; |
| 1253 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1254 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1255 | u32 num_refs; |
| 1256 | int ret; |
| 1257 | |
| 1258 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1259 | if (parent) { |
| 1260 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1261 | key.offset = parent; |
| 1262 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1263 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1264 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1265 | key.offset = hash_extent_data_ref(root_objectid, |
| 1266 | owner, offset); |
| 1267 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1268 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1269 | |
| 1270 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1271 | if (ret && ret != -EEXIST) |
| 1272 | goto fail; |
| 1273 | |
| 1274 | leaf = path->nodes[0]; |
| 1275 | if (parent) { |
| 1276 | struct btrfs_shared_data_ref *ref; |
| 1277 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1278 | struct btrfs_shared_data_ref); |
| 1279 | if (ret == 0) { |
| 1280 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1281 | } else { |
| 1282 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1283 | num_refs += refs_to_add; |
| 1284 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1285 | } |
| 1286 | } else { |
| 1287 | struct btrfs_extent_data_ref *ref; |
| 1288 | while (ret == -EEXIST) { |
| 1289 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1290 | struct btrfs_extent_data_ref); |
| 1291 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1292 | owner, offset)) |
| 1293 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1294 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1295 | key.offset++; |
| 1296 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1297 | size); |
| 1298 | if (ret && ret != -EEXIST) |
| 1299 | goto fail; |
| 1300 | |
| 1301 | leaf = path->nodes[0]; |
| 1302 | } |
| 1303 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1304 | struct btrfs_extent_data_ref); |
| 1305 | if (ret == 0) { |
| 1306 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1307 | root_objectid); |
| 1308 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1309 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1310 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1311 | } else { |
| 1312 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1313 | num_refs += refs_to_add; |
| 1314 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1315 | } |
| 1316 | } |
| 1317 | btrfs_mark_buffer_dirty(leaf); |
| 1318 | ret = 0; |
| 1319 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1320 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1321 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1322 | } |
| 1323 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1324 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1325 | struct btrfs_root *root, |
| 1326 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1327 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1328 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1329 | struct btrfs_key key; |
| 1330 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1331 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1332 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1333 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1334 | int ret = 0; |
| 1335 | |
| 1336 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1337 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1338 | |
| 1339 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1340 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1341 | struct btrfs_extent_data_ref); |
| 1342 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1343 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1344 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1345 | struct btrfs_shared_data_ref); |
| 1346 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1347 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1348 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1349 | struct btrfs_extent_ref_v0 *ref0; |
| 1350 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1351 | struct btrfs_extent_ref_v0); |
| 1352 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1353 | #endif |
| 1354 | } else { |
| 1355 | BUG(); |
| 1356 | } |
| 1357 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1358 | BUG_ON(num_refs < refs_to_drop); |
| 1359 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1360 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1361 | if (num_refs == 0) { |
| 1362 | ret = btrfs_del_item(trans, root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1363 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1364 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1365 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1366 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1367 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1368 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1369 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1370 | else { |
| 1371 | struct btrfs_extent_ref_v0 *ref0; |
| 1372 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1373 | struct btrfs_extent_ref_v0); |
| 1374 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1375 | } |
| 1376 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1377 | btrfs_mark_buffer_dirty(leaf); |
| 1378 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1379 | return ret; |
| 1380 | } |
| 1381 | |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 1382 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1383 | struct btrfs_extent_inline_ref *iref) |
| 1384 | { |
| 1385 | struct btrfs_key key; |
| 1386 | struct extent_buffer *leaf; |
| 1387 | struct btrfs_extent_data_ref *ref1; |
| 1388 | struct btrfs_shared_data_ref *ref2; |
| 1389 | u32 num_refs = 0; |
| 1390 | |
| 1391 | leaf = path->nodes[0]; |
| 1392 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1393 | if (iref) { |
| 1394 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1395 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1396 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1397 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1398 | } else { |
| 1399 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1400 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1401 | } |
| 1402 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1403 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1404 | struct btrfs_extent_data_ref); |
| 1405 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1406 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1407 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1408 | struct btrfs_shared_data_ref); |
| 1409 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1410 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1411 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1412 | struct btrfs_extent_ref_v0 *ref0; |
| 1413 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1414 | struct btrfs_extent_ref_v0); |
| 1415 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1416 | #endif |
| 1417 | } else { |
| 1418 | WARN_ON(1); |
| 1419 | } |
| 1420 | return num_refs; |
| 1421 | } |
| 1422 | |
| 1423 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1424 | struct btrfs_root *root, |
| 1425 | struct btrfs_path *path, |
| 1426 | u64 bytenr, u64 parent, |
| 1427 | u64 root_objectid) |
| 1428 | { |
| 1429 | struct btrfs_key key; |
| 1430 | int ret; |
| 1431 | |
| 1432 | key.objectid = bytenr; |
| 1433 | if (parent) { |
| 1434 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1435 | key.offset = parent; |
| 1436 | } else { |
| 1437 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1438 | key.offset = root_objectid; |
| 1439 | } |
| 1440 | |
| 1441 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1442 | if (ret > 0) |
| 1443 | ret = -ENOENT; |
| 1444 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1445 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1446 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1447 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1448 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1449 | if (ret > 0) |
| 1450 | ret = -ENOENT; |
| 1451 | } |
| 1452 | #endif |
| 1453 | return ret; |
| 1454 | } |
| 1455 | |
| 1456 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1457 | struct btrfs_root *root, |
| 1458 | struct btrfs_path *path, |
| 1459 | u64 bytenr, u64 parent, |
| 1460 | u64 root_objectid) |
| 1461 | { |
| 1462 | struct btrfs_key key; |
| 1463 | int ret; |
| 1464 | |
| 1465 | key.objectid = bytenr; |
| 1466 | if (parent) { |
| 1467 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1468 | key.offset = parent; |
| 1469 | } else { |
| 1470 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1471 | key.offset = root_objectid; |
| 1472 | } |
| 1473 | |
| 1474 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1475 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1476 | return ret; |
| 1477 | } |
| 1478 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1479 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1480 | { |
| 1481 | int type; |
| 1482 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1483 | if (parent > 0) |
| 1484 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1485 | else |
| 1486 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1487 | } else { |
| 1488 | if (parent > 0) |
| 1489 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1490 | else |
| 1491 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1492 | } |
| 1493 | return type; |
| 1494 | } |
| 1495 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1496 | static int find_next_key(struct btrfs_path *path, int level, |
| 1497 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1498 | |
| 1499 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1500 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1501 | if (!path->nodes[level]) |
| 1502 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1503 | if (path->slots[level] + 1 >= |
| 1504 | btrfs_header_nritems(path->nodes[level])) |
| 1505 | continue; |
| 1506 | if (level == 0) |
| 1507 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1508 | path->slots[level] + 1); |
| 1509 | else |
| 1510 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1511 | path->slots[level] + 1); |
| 1512 | return 0; |
| 1513 | } |
| 1514 | return 1; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1519 | * to the address of inline back ref, and 0 is returned. |
| 1520 | * |
| 1521 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1522 | * should be inserted, and -ENOENT is returned. |
| 1523 | * |
| 1524 | * if insert is true and there are too many inline back refs, the path |
| 1525 | * points to the extent item, and -EAGAIN is returned. |
| 1526 | * |
| 1527 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1528 | * items in the tree are ordered. |
| 1529 | */ |
| 1530 | static noinline_for_stack |
| 1531 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1532 | struct btrfs_root *root, |
| 1533 | struct btrfs_path *path, |
| 1534 | struct btrfs_extent_inline_ref **ref_ret, |
| 1535 | u64 bytenr, u64 num_bytes, |
| 1536 | u64 parent, u64 root_objectid, |
| 1537 | u64 owner, u64 offset, int insert) |
| 1538 | { |
| 1539 | struct btrfs_key key; |
| 1540 | struct extent_buffer *leaf; |
| 1541 | struct btrfs_extent_item *ei; |
| 1542 | struct btrfs_extent_inline_ref *iref; |
| 1543 | u64 flags; |
| 1544 | u64 item_size; |
| 1545 | unsigned long ptr; |
| 1546 | unsigned long end; |
| 1547 | int extra_size; |
| 1548 | int type; |
| 1549 | int want; |
| 1550 | int ret; |
| 1551 | int err = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1552 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 1553 | SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1554 | |
| 1555 | key.objectid = bytenr; |
| 1556 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1557 | key.offset = num_bytes; |
| 1558 | |
| 1559 | want = extent_ref_type(parent, owner); |
| 1560 | if (insert) { |
| 1561 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1562 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1563 | } else |
| 1564 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * Owner is our parent level, so we can just add one to get the level |
| 1568 | * for the block we are interested in. |
| 1569 | */ |
| 1570 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1571 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1572 | key.offset = owner; |
| 1573 | } |
| 1574 | |
| 1575 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1576 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1577 | if (ret < 0) { |
| 1578 | err = ret; |
| 1579 | goto out; |
| 1580 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1581 | |
| 1582 | /* |
| 1583 | * We may be a newly converted file system which still has the old fat |
| 1584 | * extent entries for metadata, so try and see if we have one of those. |
| 1585 | */ |
| 1586 | if (ret > 0 && skinny_metadata) { |
| 1587 | skinny_metadata = false; |
| 1588 | if (path->slots[0]) { |
| 1589 | path->slots[0]--; |
| 1590 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1591 | path->slots[0]); |
| 1592 | if (key.objectid == bytenr && |
| 1593 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1594 | key.offset == num_bytes) |
| 1595 | ret = 0; |
| 1596 | } |
| 1597 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 1598 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1599 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1600 | key.offset = num_bytes; |
| 1601 | btrfs_release_path(path); |
| 1602 | goto again; |
| 1603 | } |
| 1604 | } |
| 1605 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1606 | if (ret && !insert) { |
| 1607 | err = -ENOENT; |
| 1608 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1609 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1610 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1611 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1612 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1613 | |
| 1614 | leaf = path->nodes[0]; |
| 1615 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1616 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1617 | if (item_size < sizeof(*ei)) { |
| 1618 | if (!insert) { |
| 1619 | err = -ENOENT; |
| 1620 | goto out; |
| 1621 | } |
| 1622 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1623 | extra_size); |
| 1624 | if (ret < 0) { |
| 1625 | err = ret; |
| 1626 | goto out; |
| 1627 | } |
| 1628 | leaf = path->nodes[0]; |
| 1629 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1630 | } |
| 1631 | #endif |
| 1632 | BUG_ON(item_size < sizeof(*ei)); |
| 1633 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1634 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1635 | flags = btrfs_extent_flags(leaf, ei); |
| 1636 | |
| 1637 | ptr = (unsigned long)(ei + 1); |
| 1638 | end = (unsigned long)ei + item_size; |
| 1639 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1640 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1641 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1642 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | err = -ENOENT; |
| 1646 | while (1) { |
| 1647 | if (ptr >= end) { |
| 1648 | WARN_ON(ptr > end); |
| 1649 | break; |
| 1650 | } |
| 1651 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1652 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1653 | if (want < type) |
| 1654 | break; |
| 1655 | if (want > type) { |
| 1656 | ptr += btrfs_extent_inline_ref_size(type); |
| 1657 | continue; |
| 1658 | } |
| 1659 | |
| 1660 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1661 | struct btrfs_extent_data_ref *dref; |
| 1662 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1663 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1664 | owner, offset)) { |
| 1665 | err = 0; |
| 1666 | break; |
| 1667 | } |
| 1668 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1669 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1670 | break; |
| 1671 | } else { |
| 1672 | u64 ref_offset; |
| 1673 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1674 | if (parent > 0) { |
| 1675 | if (parent == ref_offset) { |
| 1676 | err = 0; |
| 1677 | break; |
| 1678 | } |
| 1679 | if (ref_offset < parent) |
| 1680 | break; |
| 1681 | } else { |
| 1682 | if (root_objectid == ref_offset) { |
| 1683 | err = 0; |
| 1684 | break; |
| 1685 | } |
| 1686 | if (ref_offset < root_objectid) |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | ptr += btrfs_extent_inline_ref_size(type); |
| 1691 | } |
| 1692 | if (err == -ENOENT && insert) { |
| 1693 | if (item_size + extra_size >= |
| 1694 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1695 | err = -EAGAIN; |
| 1696 | goto out; |
| 1697 | } |
| 1698 | /* |
| 1699 | * To add new inline back ref, we have to make sure |
| 1700 | * there is no corresponding back ref item. |
| 1701 | * For simplicity, we just do not add new inline back |
| 1702 | * ref if there is any kind of item for this block |
| 1703 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1704 | if (find_next_key(path, 0, &key) == 0 && |
| 1705 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1706 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1707 | err = -EAGAIN; |
| 1708 | goto out; |
| 1709 | } |
| 1710 | } |
| 1711 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1712 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1713 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1714 | path->keep_locks = 0; |
| 1715 | btrfs_unlock_up_safe(path, 1); |
| 1716 | } |
| 1717 | return err; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
| 1721 | * helper to add new inline back ref |
| 1722 | */ |
| 1723 | static noinline_for_stack |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1724 | void setup_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1725 | struct btrfs_path *path, |
| 1726 | struct btrfs_extent_inline_ref *iref, |
| 1727 | u64 parent, u64 root_objectid, |
| 1728 | u64 owner, u64 offset, int refs_to_add, |
| 1729 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1730 | { |
| 1731 | struct extent_buffer *leaf; |
| 1732 | struct btrfs_extent_item *ei; |
| 1733 | unsigned long ptr; |
| 1734 | unsigned long end; |
| 1735 | unsigned long item_offset; |
| 1736 | u64 refs; |
| 1737 | int size; |
| 1738 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1739 | |
| 1740 | leaf = path->nodes[0]; |
| 1741 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1742 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1743 | |
| 1744 | type = extent_ref_type(parent, owner); |
| 1745 | size = btrfs_extent_inline_ref_size(type); |
| 1746 | |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 1747 | btrfs_extend_item(root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1748 | |
| 1749 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1750 | refs = btrfs_extent_refs(leaf, ei); |
| 1751 | refs += refs_to_add; |
| 1752 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1753 | if (extent_op) |
| 1754 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1755 | |
| 1756 | ptr = (unsigned long)ei + item_offset; |
| 1757 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1758 | if (ptr < end - size) |
| 1759 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1760 | end - size - ptr); |
| 1761 | |
| 1762 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1763 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1764 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1765 | struct btrfs_extent_data_ref *dref; |
| 1766 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1767 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1768 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1769 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1770 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1771 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1772 | struct btrfs_shared_data_ref *sref; |
| 1773 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1774 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1775 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1776 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1777 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1778 | } else { |
| 1779 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1780 | } |
| 1781 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1785 | struct btrfs_root *root, |
| 1786 | struct btrfs_path *path, |
| 1787 | struct btrfs_extent_inline_ref **ref_ret, |
| 1788 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1789 | u64 root_objectid, u64 owner, u64 offset) |
| 1790 | { |
| 1791 | int ret; |
| 1792 | |
| 1793 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1794 | bytenr, num_bytes, parent, |
| 1795 | root_objectid, owner, offset, 0); |
| 1796 | if (ret != -ENOENT) |
| 1797 | return ret; |
| 1798 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1799 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1800 | *ref_ret = NULL; |
| 1801 | |
| 1802 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1803 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1804 | root_objectid); |
| 1805 | } else { |
| 1806 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1807 | root_objectid, owner, offset); |
| 1808 | } |
| 1809 | return ret; |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * helper to update/remove inline back ref |
| 1814 | */ |
| 1815 | static noinline_for_stack |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1816 | void update_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1817 | struct btrfs_path *path, |
| 1818 | struct btrfs_extent_inline_ref *iref, |
| 1819 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1820 | struct btrfs_delayed_extent_op *extent_op, |
| 1821 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1822 | { |
| 1823 | struct extent_buffer *leaf; |
| 1824 | struct btrfs_extent_item *ei; |
| 1825 | struct btrfs_extent_data_ref *dref = NULL; |
| 1826 | struct btrfs_shared_data_ref *sref = NULL; |
| 1827 | unsigned long ptr; |
| 1828 | unsigned long end; |
| 1829 | u32 item_size; |
| 1830 | int size; |
| 1831 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1832 | u64 refs; |
| 1833 | |
| 1834 | leaf = path->nodes[0]; |
| 1835 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1836 | refs = btrfs_extent_refs(leaf, ei); |
| 1837 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1838 | refs += refs_to_mod; |
| 1839 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1840 | if (extent_op) |
| 1841 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1842 | |
| 1843 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1844 | |
| 1845 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1846 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1847 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1848 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1849 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1850 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1851 | } else { |
| 1852 | refs = 1; |
| 1853 | BUG_ON(refs_to_mod != -1); |
| 1854 | } |
| 1855 | |
| 1856 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1857 | refs += refs_to_mod; |
| 1858 | |
| 1859 | if (refs > 0) { |
| 1860 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1861 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1862 | else |
| 1863 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1864 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1865 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1866 | size = btrfs_extent_inline_ref_size(type); |
| 1867 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1868 | ptr = (unsigned long)iref; |
| 1869 | end = (unsigned long)ei + item_size; |
| 1870 | if (ptr + size < end) |
| 1871 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1872 | end - ptr - size); |
| 1873 | item_size -= size; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1874 | btrfs_truncate_item(root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1875 | } |
| 1876 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | static noinline_for_stack |
| 1880 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1881 | struct btrfs_root *root, |
| 1882 | struct btrfs_path *path, |
| 1883 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1884 | u64 root_objectid, u64 owner, |
| 1885 | u64 offset, int refs_to_add, |
| 1886 | struct btrfs_delayed_extent_op *extent_op) |
| 1887 | { |
| 1888 | struct btrfs_extent_inline_ref *iref; |
| 1889 | int ret; |
| 1890 | |
| 1891 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1892 | bytenr, num_bytes, parent, |
| 1893 | root_objectid, owner, offset, 1); |
| 1894 | if (ret == 0) { |
| 1895 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1896 | update_inline_extent_backref(root, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1897 | refs_to_add, extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1898 | } else if (ret == -ENOENT) { |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1899 | setup_inline_extent_backref(root, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1900 | root_objectid, owner, offset, |
| 1901 | refs_to_add, extent_op); |
| 1902 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1903 | } |
| 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1908 | struct btrfs_root *root, |
| 1909 | struct btrfs_path *path, |
| 1910 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1911 | u64 owner, u64 offset, int refs_to_add) |
| 1912 | { |
| 1913 | int ret; |
| 1914 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1915 | BUG_ON(refs_to_add != 1); |
| 1916 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1917 | parent, root_objectid); |
| 1918 | } else { |
| 1919 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1920 | parent, root_objectid, |
| 1921 | owner, offset, refs_to_add); |
| 1922 | } |
| 1923 | return ret; |
| 1924 | } |
| 1925 | |
| 1926 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1927 | struct btrfs_root *root, |
| 1928 | struct btrfs_path *path, |
| 1929 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1930 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1931 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1932 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1933 | |
| 1934 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1935 | if (iref) { |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1936 | update_inline_extent_backref(root, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1937 | -refs_to_drop, NULL, last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1938 | } else if (is_data) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1939 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop, |
| 1940 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1941 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1942 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1943 | ret = btrfs_del_item(trans, root, path); |
| 1944 | } |
| 1945 | return ret; |
| 1946 | } |
| 1947 | |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1948 | #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len)) |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1949 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
| 1950 | u64 *discarded_bytes) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1951 | { |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1952 | int j, ret = 0; |
| 1953 | u64 bytes_left, end; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 1954 | u64 aligned_start = ALIGN(start, 1 << 9); |
| 1955 | |
| 1956 | if (WARN_ON(start != aligned_start)) { |
| 1957 | len -= aligned_start - start; |
| 1958 | len = round_down(len, 1 << 9); |
| 1959 | start = aligned_start; |
| 1960 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 1961 | |
| 1962 | *discarded_bytes = 0; |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 1963 | |
| 1964 | if (!len) |
| 1965 | return 0; |
| 1966 | |
| 1967 | end = start + len; |
| 1968 | bytes_left = len; |
| 1969 | |
| 1970 | /* Skip any superblocks on this device. */ |
| 1971 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { |
| 1972 | u64 sb_start = btrfs_sb_offset(j); |
| 1973 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; |
| 1974 | u64 size = sb_start - start; |
| 1975 | |
| 1976 | if (!in_range(sb_start, start, bytes_left) && |
| 1977 | !in_range(sb_end, start, bytes_left) && |
| 1978 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) |
| 1979 | continue; |
| 1980 | |
| 1981 | /* |
| 1982 | * Superblock spans beginning of range. Adjust start and |
| 1983 | * try again. |
| 1984 | */ |
| 1985 | if (sb_start <= start) { |
| 1986 | start += sb_end - start; |
| 1987 | if (start > end) { |
| 1988 | bytes_left = 0; |
| 1989 | break; |
| 1990 | } |
| 1991 | bytes_left = end - start; |
| 1992 | continue; |
| 1993 | } |
| 1994 | |
| 1995 | if (size) { |
| 1996 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, |
| 1997 | GFP_NOFS, 0); |
| 1998 | if (!ret) |
| 1999 | *discarded_bytes += size; |
| 2000 | else if (ret != -EOPNOTSUPP) |
| 2001 | return ret; |
| 2002 | } |
| 2003 | |
| 2004 | start = sb_end; |
| 2005 | if (start > end) { |
| 2006 | bytes_left = 0; |
| 2007 | break; |
| 2008 | } |
| 2009 | bytes_left = end - start; |
| 2010 | } |
| 2011 | |
| 2012 | if (bytes_left) { |
| 2013 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2014 | GFP_NOFS, 0); |
| 2015 | if (!ret) |
Jeff Mahoney | 8655786 | 2015-06-15 09:41:16 -0400 | [diff] [blame] | 2016 | *discarded_bytes += bytes_left; |
Jeff Mahoney | 4d89d37 | 2015-06-15 09:41:15 -0400 | [diff] [blame] | 2017 | } |
Jeff Mahoney | d04c6b8 | 2015-06-15 09:41:14 -0400 | [diff] [blame] | 2018 | return ret; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2019 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2020 | |
Filipe Manana | 1edb647b | 2014-12-08 14:01:12 +0000 | [diff] [blame] | 2021 | int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 2022 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2023 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2024 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2025 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2026 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2027 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 2028 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2029 | /* Tell the block device(s) that the sectors can be discarded */ |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 2030 | ret = btrfs_map_block(root->fs_info, REQ_DISCARD, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 2031 | bytenr, &num_bytes, &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 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 2061 | |
| 2062 | if (actual_bytes) |
| 2063 | *actual_bytes = discarded_bytes; |
| 2064 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2065 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2066 | if (ret == -EOPNOTSUPP) |
| 2067 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2068 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2069 | } |
| 2070 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2071 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2072 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 2073 | struct btrfs_root *root, |
| 2074 | u64 bytenr, u64 num_bytes, u64 parent, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2075 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2076 | { |
| 2077 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2078 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 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, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2098 | struct btrfs_root *root, |
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 | { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2104 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2105 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2106 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2107 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2108 | struct btrfs_key key; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2109 | u64 bytenr = node->bytenr; |
| 2110 | u64 num_bytes = node->num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2111 | u64 refs; |
| 2112 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2113 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2114 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2115 | if (!path) |
| 2116 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2117 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2118 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2119 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2120 | /* 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] | 2121 | ret = insert_inline_extent_backref(trans, fs_info->extent_root, path, |
| 2122 | bytenr, num_bytes, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2123 | root_objectid, owner, offset, |
| 2124 | refs_to_add, extent_op); |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 2125 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2126 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2127 | |
| 2128 | /* |
| 2129 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 2130 | * inline extent ref, so just update the reference count and add a |
| 2131 | * normal backref. |
| 2132 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2133 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2134 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2135 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2136 | refs = btrfs_extent_refs(leaf, item); |
| 2137 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 2138 | if (extent_op) |
| 2139 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2140 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2141 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2142 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2143 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2144 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2145 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2146 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2147 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2148 | path, bytenr, parent, root_objectid, |
| 2149 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2150 | if (ret) |
| 2151 | btrfs_abort_transaction(trans, root, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2152 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2153 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 2154 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2155 | } |
| 2156 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2157 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 2158 | struct btrfs_root *root, |
| 2159 | struct btrfs_delayed_ref_node *node, |
| 2160 | struct btrfs_delayed_extent_op *extent_op, |
| 2161 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2162 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2163 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2164 | struct btrfs_delayed_data_ref *ref; |
| 2165 | struct btrfs_key ins; |
| 2166 | u64 parent = 0; |
| 2167 | u64 ref_root = 0; |
| 2168 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2169 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2170 | ins.objectid = node->bytenr; |
| 2171 | ins.offset = node->num_bytes; |
| 2172 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2173 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2174 | ref = btrfs_delayed_node_to_data_ref(node); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2175 | trace_run_delayed_data_ref(node, ref, node->action); |
| 2176 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2177 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2178 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2179 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2180 | |
| 2181 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2182 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2183 | flags |= extent_op->flags_to_set; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2184 | ret = alloc_reserved_file_extent(trans, root, |
| 2185 | parent, ref_root, flags, |
| 2186 | ref->objectid, ref->offset, |
| 2187 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2188 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2189 | ret = __btrfs_inc_extent_ref(trans, root, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2190 | ref_root, ref->objectid, |
| 2191 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2192 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2193 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2194 | ret = __btrfs_free_extent(trans, root, node, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2195 | ref_root, ref->objectid, |
| 2196 | ref->offset, node->ref_mod, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2197 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2198 | } else { |
| 2199 | BUG(); |
| 2200 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2201 | return ret; |
| 2202 | } |
| 2203 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2204 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2205 | struct extent_buffer *leaf, |
| 2206 | struct btrfs_extent_item *ei) |
| 2207 | { |
| 2208 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2209 | if (extent_op->update_flags) { |
| 2210 | flags |= extent_op->flags_to_set; |
| 2211 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2212 | } |
| 2213 | |
| 2214 | if (extent_op->update_key) { |
| 2215 | struct btrfs_tree_block_info *bi; |
| 2216 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2217 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2218 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 2223 | struct btrfs_root *root, |
| 2224 | struct btrfs_delayed_ref_node *node, |
| 2225 | struct btrfs_delayed_extent_op *extent_op) |
| 2226 | { |
| 2227 | struct btrfs_key key; |
| 2228 | struct btrfs_path *path; |
| 2229 | struct btrfs_extent_item *ei; |
| 2230 | struct extent_buffer *leaf; |
| 2231 | u32 item_size; |
| 2232 | int ret; |
| 2233 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2234 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2235 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2236 | if (trans->aborted) |
| 2237 | return 0; |
| 2238 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2239 | if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) |
| 2240 | metadata = 0; |
| 2241 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2242 | path = btrfs_alloc_path(); |
| 2243 | if (!path) |
| 2244 | return -ENOMEM; |
| 2245 | |
| 2246 | key.objectid = node->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2247 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2248 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2249 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2250 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2251 | } else { |
| 2252 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2253 | key.offset = node->num_bytes; |
| 2254 | } |
| 2255 | |
| 2256 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2257 | path->reada = 1; |
| 2258 | path->leave_spinning = 1; |
| 2259 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 2260 | path, 0, 1); |
| 2261 | if (ret < 0) { |
| 2262 | err = ret; |
| 2263 | goto out; |
| 2264 | } |
| 2265 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2266 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2267 | if (path->slots[0] > 0) { |
| 2268 | path->slots[0]--; |
| 2269 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 2270 | path->slots[0]); |
| 2271 | if (key.objectid == node->bytenr && |
| 2272 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2273 | key.offset == node->num_bytes) |
| 2274 | ret = 0; |
| 2275 | } |
| 2276 | if (ret > 0) { |
| 2277 | btrfs_release_path(path); |
| 2278 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2279 | |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2280 | key.objectid = node->bytenr; |
| 2281 | key.offset = node->num_bytes; |
| 2282 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2283 | goto again; |
| 2284 | } |
| 2285 | } else { |
| 2286 | err = -EIO; |
| 2287 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2288 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | leaf = path->nodes[0]; |
| 2292 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2293 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2294 | if (item_size < sizeof(*ei)) { |
| 2295 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 2296 | path, (u64)-1, 0); |
| 2297 | if (ret < 0) { |
| 2298 | err = ret; |
| 2299 | goto out; |
| 2300 | } |
| 2301 | leaf = path->nodes[0]; |
| 2302 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2303 | } |
| 2304 | #endif |
| 2305 | BUG_ON(item_size < sizeof(*ei)); |
| 2306 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2307 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2308 | |
| 2309 | btrfs_mark_buffer_dirty(leaf); |
| 2310 | out: |
| 2311 | btrfs_free_path(path); |
| 2312 | return err; |
| 2313 | } |
| 2314 | |
| 2315 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2316 | struct btrfs_root *root, |
| 2317 | struct btrfs_delayed_ref_node *node, |
| 2318 | struct btrfs_delayed_extent_op *extent_op, |
| 2319 | int insert_reserved) |
| 2320 | { |
| 2321 | int ret = 0; |
| 2322 | struct btrfs_delayed_tree_ref *ref; |
| 2323 | struct btrfs_key ins; |
| 2324 | u64 parent = 0; |
| 2325 | u64 ref_root = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2326 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 2327 | SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2328 | |
| 2329 | ref = btrfs_delayed_node_to_tree_ref(node); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2330 | trace_run_delayed_tree_ref(node, ref, node->action); |
| 2331 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2332 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2333 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2334 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2335 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2336 | ins.objectid = node->bytenr; |
| 2337 | if (skinny_metadata) { |
| 2338 | ins.offset = ref->level; |
| 2339 | ins.type = BTRFS_METADATA_ITEM_KEY; |
| 2340 | } else { |
| 2341 | ins.offset = node->num_bytes; |
| 2342 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2343 | } |
| 2344 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2345 | BUG_ON(node->ref_mod != 1); |
| 2346 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2347 | BUG_ON(!extent_op || !extent_op->update_flags); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2348 | ret = alloc_reserved_tree_block(trans, root, |
| 2349 | parent, ref_root, |
| 2350 | extent_op->flags_to_set, |
| 2351 | &extent_op->key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 2352 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2353 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2354 | ret = __btrfs_inc_extent_ref(trans, root, node, |
| 2355 | parent, ref_root, |
| 2356 | ref->level, 0, 1, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2357 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2358 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 2359 | ret = __btrfs_free_extent(trans, root, node, |
| 2360 | parent, ref_root, |
| 2361 | ref->level, 0, 1, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2362 | } else { |
| 2363 | BUG(); |
| 2364 | } |
| 2365 | return ret; |
| 2366 | } |
| 2367 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2368 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2369 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2370 | struct btrfs_root *root, |
| 2371 | struct btrfs_delayed_ref_node *node, |
| 2372 | struct btrfs_delayed_extent_op *extent_op, |
| 2373 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2374 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2375 | int ret = 0; |
| 2376 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2377 | if (trans->aborted) { |
| 2378 | if (insert_reserved) |
| 2379 | btrfs_pin_extent(root, node->bytenr, |
| 2380 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2381 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2382 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2383 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2384 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2385 | struct btrfs_delayed_ref_head *head; |
| 2386 | /* |
| 2387 | * we've hit the end of the chain and we were supposed |
| 2388 | * to insert this extent into the tree. But, it got |
| 2389 | * deleted before we ever needed to insert it, so all |
| 2390 | * we have to do is clean up the accounting |
| 2391 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2392 | BUG_ON(extent_op); |
| 2393 | head = btrfs_delayed_node_to_head(node); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2394 | trace_run_delayed_ref_head(node, head, node->action); |
| 2395 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2396 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2397 | btrfs_pin_extent(root, node->bytenr, |
| 2398 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2399 | if (head->is_data) { |
| 2400 | ret = btrfs_del_csums(trans, root, |
| 2401 | node->bytenr, |
| 2402 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2403 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2404 | } |
Qu Wenruo | 297d750 | 2015-09-08 17:08:37 +0800 | [diff] [blame] | 2405 | |
| 2406 | /* Also free its reserved qgroup space */ |
| 2407 | btrfs_qgroup_free_delayed_ref(root->fs_info, |
| 2408 | head->qgroup_ref_root, |
| 2409 | head->qgroup_reserved); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2410 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2411 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2412 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2413 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2414 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2415 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2416 | insert_reserved); |
| 2417 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2418 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2419 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2420 | insert_reserved); |
| 2421 | else |
| 2422 | BUG(); |
| 2423 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2424 | } |
| 2425 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2426 | static inline struct btrfs_delayed_ref_node * |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2427 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2428 | { |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2429 | struct btrfs_delayed_ref_node *ref; |
| 2430 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2431 | if (list_empty(&head->ref_list)) |
| 2432 | return NULL; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2433 | |
Filipe Manana | cffc337 | 2015-07-09 13:13:44 +0100 | [diff] [blame] | 2434 | /* |
| 2435 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2436 | * This is to prevent a ref count from going down to zero, which deletes |
| 2437 | * the extent item from the extent tree, when there still are references |
| 2438 | * to add, which would fail because they would not find the extent item. |
| 2439 | */ |
| 2440 | list_for_each_entry(ref, &head->ref_list, list) { |
| 2441 | if (ref->action == BTRFS_ADD_DELAYED_REF) |
| 2442 | return ref; |
| 2443 | } |
| 2444 | |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2445 | return list_entry(head->ref_list.next, struct btrfs_delayed_ref_node, |
| 2446 | list); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2447 | } |
| 2448 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2449 | /* |
| 2450 | * Returns 0 on success or if called with an already aborted transaction. |
| 2451 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2452 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2453 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2454 | struct btrfs_root *root, |
| 2455 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2456 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2457 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2458 | struct btrfs_delayed_ref_node *ref; |
| 2459 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2460 | struct btrfs_delayed_extent_op *extent_op; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2461 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2462 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2463 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2464 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2465 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2466 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2467 | |
| 2468 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2469 | while (1) { |
| 2470 | if (!locked_ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2471 | if (count >= nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2472 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2473 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2474 | spin_lock(&delayed_refs->lock); |
| 2475 | locked_ref = btrfs_select_ref_head(trans); |
| 2476 | if (!locked_ref) { |
| 2477 | spin_unlock(&delayed_refs->lock); |
| 2478 | break; |
| 2479 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2480 | |
| 2481 | /* grab the lock that says we are going to process |
| 2482 | * all the refs for this head */ |
| 2483 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2484 | spin_unlock(&delayed_refs->lock); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2485 | /* |
| 2486 | * we may have dropped the spin lock to get the head |
| 2487 | * mutex lock, and that might have given someone else |
| 2488 | * time to free the head. If that's true, it has been |
| 2489 | * removed from our list and we can move on. |
| 2490 | */ |
| 2491 | if (ret == -EAGAIN) { |
| 2492 | locked_ref = NULL; |
| 2493 | count++; |
| 2494 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2498 | /* |
| 2499 | * We need to try and merge add/drops of the same ref since we |
| 2500 | * can run into issues with relocate dropping the implicit ref |
| 2501 | * and then it being added back again before the drop can |
| 2502 | * finish. If we merged anything we need to re-loop so we can |
| 2503 | * get a good ref. |
| 2504 | * Or we can get node references of the same type that weren't |
| 2505 | * merged when created due to bumps in the tree mod seq, and |
| 2506 | * we need to merge them to prevent adding an inline extent |
| 2507 | * backref before dropping it (triggering a BUG_ON at |
| 2508 | * insert_inline_extent_backref()). |
| 2509 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2510 | spin_lock(&locked_ref->lock); |
Filipe Manana | 2c3cf7d | 2015-10-22 09:47:34 +0100 | [diff] [blame] | 2511 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2512 | locked_ref); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2513 | |
| 2514 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2515 | * locked_ref is the head node, so we have to go one |
| 2516 | * node back for any delayed ref updates |
| 2517 | */ |
| 2518 | ref = select_delayed_ref(locked_ref); |
| 2519 | |
| 2520 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2521 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2522 | spin_unlock(&locked_ref->lock); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2523 | btrfs_delayed_ref_unlock(locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2524 | spin_lock(&delayed_refs->lock); |
| 2525 | locked_ref->processing = 0; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2526 | delayed_refs->num_heads_ready++; |
| 2527 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2528 | locked_ref = NULL; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2529 | cond_resched(); |
Josef Bacik | 27a377d | 2014-02-07 13:57:59 -0500 | [diff] [blame] | 2530 | count++; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2531 | continue; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2535 | * record the must insert reserved flag before we |
| 2536 | * drop the spin lock. |
| 2537 | */ |
| 2538 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2539 | locked_ref->must_insert_reserved = 0; |
| 2540 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2541 | extent_op = locked_ref->extent_op; |
| 2542 | locked_ref->extent_op = NULL; |
| 2543 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2544 | if (!ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2545 | |
| 2546 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2547 | /* All delayed refs have been processed, Go ahead |
| 2548 | * and send the head node to run_one_delayed_ref, |
| 2549 | * so that any accounting fixes can happen |
| 2550 | */ |
| 2551 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2552 | |
| 2553 | if (extent_op && must_insert_reserved) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2554 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2555 | extent_op = NULL; |
| 2556 | } |
| 2557 | |
| 2558 | if (extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2559 | spin_unlock(&locked_ref->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2560 | ret = run_delayed_extent_op(trans, root, |
| 2561 | ref, extent_op); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2562 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2563 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2564 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2565 | /* |
| 2566 | * Need to reset must_insert_reserved if |
| 2567 | * there was an error so the abort stuff |
| 2568 | * can cleanup the reserved space |
| 2569 | * properly. |
| 2570 | */ |
| 2571 | if (must_insert_reserved) |
| 2572 | locked_ref->must_insert_reserved = 1; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2573 | locked_ref->processing = 0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2574 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2575 | btrfs_delayed_ref_unlock(locked_ref); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2576 | return ret; |
| 2577 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2578 | continue; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2579 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2580 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2581 | /* |
| 2582 | * Need to drop our head ref lock and re-aqcuire the |
| 2583 | * delayed ref lock and then re-check to make sure |
| 2584 | * nobody got added. |
| 2585 | */ |
| 2586 | spin_unlock(&locked_ref->lock); |
| 2587 | spin_lock(&delayed_refs->lock); |
| 2588 | spin_lock(&locked_ref->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2589 | if (!list_empty(&locked_ref->ref_list) || |
Josef Bacik | 573a075 | 2014-03-27 19:41:34 -0400 | [diff] [blame] | 2590 | locked_ref->extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2591 | spin_unlock(&locked_ref->lock); |
| 2592 | spin_unlock(&delayed_refs->lock); |
| 2593 | continue; |
| 2594 | } |
| 2595 | ref->in_tree = 0; |
| 2596 | delayed_refs->num_heads--; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2597 | rb_erase(&locked_ref->href_node, |
| 2598 | &delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2599 | spin_unlock(&delayed_refs->lock); |
| 2600 | } else { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2601 | actual_count++; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2602 | ref->in_tree = 0; |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 2603 | list_del(&ref->list); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2604 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2605 | atomic_dec(&delayed_refs->num_entries); |
| 2606 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2607 | if (!btrfs_delayed_ref_is_head(ref)) { |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2608 | /* |
| 2609 | * when we play the delayed ref, also correct the |
| 2610 | * ref_mod on head |
| 2611 | */ |
| 2612 | switch (ref->action) { |
| 2613 | case BTRFS_ADD_DELAYED_REF: |
| 2614 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2615 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2616 | break; |
| 2617 | case BTRFS_DROP_DELAYED_REF: |
| 2618 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2619 | break; |
| 2620 | default: |
| 2621 | WARN_ON(1); |
| 2622 | } |
| 2623 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2624 | spin_unlock(&locked_ref->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2625 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2626 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2627 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2628 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2629 | btrfs_free_delayed_extent_op(extent_op); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2630 | if (ret) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2631 | locked_ref->processing = 0; |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2632 | btrfs_delayed_ref_unlock(locked_ref); |
| 2633 | btrfs_put_delayed_ref(ref); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2634 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2635 | return ret; |
| 2636 | } |
| 2637 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2638 | /* |
| 2639 | * If this node is a head, that means all the refs in this head |
| 2640 | * have been dealt with, and we will pick the next head to deal |
| 2641 | * with, so we must unlock the head and drop it from the cluster |
| 2642 | * list before we release it. |
| 2643 | */ |
| 2644 | if (btrfs_delayed_ref_is_head(ref)) { |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2645 | if (locked_ref->is_data && |
| 2646 | locked_ref->total_ref_mod < 0) { |
| 2647 | spin_lock(&delayed_refs->lock); |
| 2648 | delayed_refs->pending_csums -= ref->num_bytes; |
| 2649 | spin_unlock(&delayed_refs->lock); |
| 2650 | } |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2651 | btrfs_delayed_ref_unlock(locked_ref); |
| 2652 | locked_ref = NULL; |
| 2653 | } |
| 2654 | btrfs_put_delayed_ref(ref); |
| 2655 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2656 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2657 | } |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2658 | |
| 2659 | /* |
| 2660 | * We don't want to include ref heads since we can have empty ref heads |
| 2661 | * and those will drastically skew our runtime down since we just do |
| 2662 | * accounting, no actual extent tree updates. |
| 2663 | */ |
| 2664 | if (actual_count > 0) { |
| 2665 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2666 | u64 avg; |
| 2667 | |
| 2668 | /* |
| 2669 | * We weigh the current average higher than our current runtime |
| 2670 | * to avoid large swings in the average. |
| 2671 | */ |
| 2672 | spin_lock(&delayed_refs->lock); |
| 2673 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2674 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2675 | spin_unlock(&delayed_refs->lock); |
| 2676 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2677 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2678 | } |
| 2679 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2680 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2681 | /* |
| 2682 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2683 | * correlates in most cases to the order added. To expose dependencies on this |
| 2684 | * order, we start to process the tree in the middle instead of the beginning |
| 2685 | */ |
| 2686 | static u64 find_middle(struct rb_root *root) |
| 2687 | { |
| 2688 | struct rb_node *n = root->rb_node; |
| 2689 | struct btrfs_delayed_ref_node *entry; |
| 2690 | int alt = 1; |
| 2691 | u64 middle; |
| 2692 | u64 first = 0, last = 0; |
| 2693 | |
| 2694 | n = rb_first(root); |
| 2695 | if (n) { |
| 2696 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2697 | first = entry->bytenr; |
| 2698 | } |
| 2699 | n = rb_last(root); |
| 2700 | if (n) { |
| 2701 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2702 | last = entry->bytenr; |
| 2703 | } |
| 2704 | n = root->rb_node; |
| 2705 | |
| 2706 | while (n) { |
| 2707 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2708 | WARN_ON(!entry->in_tree); |
| 2709 | |
| 2710 | middle = entry->bytenr; |
| 2711 | |
| 2712 | if (alt) |
| 2713 | n = n->rb_left; |
| 2714 | else |
| 2715 | n = n->rb_right; |
| 2716 | |
| 2717 | alt = 1 - alt; |
| 2718 | } |
| 2719 | return middle; |
| 2720 | } |
| 2721 | #endif |
| 2722 | |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2723 | static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads) |
| 2724 | { |
| 2725 | u64 num_bytes; |
| 2726 | |
| 2727 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2728 | sizeof(struct btrfs_extent_inline_ref)); |
| 2729 | if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) |
| 2730 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2731 | |
| 2732 | /* |
| 2733 | * We don't ever fill up leaves all the way so multiply by 2 just to be |
| 2734 | * closer to what we're really going to want to ouse. |
| 2735 | */ |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 2736 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root)); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2737 | } |
| 2738 | |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2739 | /* |
| 2740 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it |
| 2741 | * would require to store the csums for that many bytes. |
| 2742 | */ |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 2743 | u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes) |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2744 | { |
| 2745 | u64 csum_size; |
| 2746 | u64 num_csums_per_leaf; |
| 2747 | u64 num_csums; |
| 2748 | |
| 2749 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); |
| 2750 | num_csums_per_leaf = div64_u64(csum_size, |
| 2751 | (u64)btrfs_super_csum_size(root->fs_info->super_copy)); |
| 2752 | num_csums = div64_u64(csum_bytes, root->sectorsize); |
| 2753 | num_csums += num_csums_per_leaf - 1; |
| 2754 | num_csums = div64_u64(num_csums, num_csums_per_leaf); |
| 2755 | return num_csums; |
| 2756 | } |
| 2757 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2758 | int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2759 | struct btrfs_root *root) |
| 2760 | { |
| 2761 | struct btrfs_block_rsv *global_rsv; |
| 2762 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 2763 | u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2764 | u64 num_dirty_bgs = trans->transaction->num_dirty_bgs; |
| 2765 | u64 num_bytes, num_dirty_bgs_bytes; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2766 | int ret = 0; |
| 2767 | |
| 2768 | num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 2769 | num_heads = heads_to_leaves(root, num_heads); |
| 2770 | if (num_heads > 1) |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 2771 | num_bytes += (num_heads - 1) * root->nodesize; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2772 | num_bytes <<= 1; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 2773 | num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * root->nodesize; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2774 | num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(root, |
| 2775 | num_dirty_bgs); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2776 | global_rsv = &root->fs_info->global_block_rsv; |
| 2777 | |
| 2778 | /* |
| 2779 | * If we can't allocate any more chunks lets make sure we have _lots_ of |
| 2780 | * wiggle room since running delayed refs can create more delayed refs. |
| 2781 | */ |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2782 | if (global_rsv->space_info->full) { |
| 2783 | num_dirty_bgs_bytes <<= 1; |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2784 | num_bytes <<= 1; |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2785 | } |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2786 | |
| 2787 | spin_lock(&global_rsv->lock); |
Josef Bacik | cb723e4 | 2015-02-18 08:06:57 -0800 | [diff] [blame] | 2788 | if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes) |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2789 | ret = 1; |
| 2790 | spin_unlock(&global_rsv->lock); |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2794 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
| 2795 | struct btrfs_root *root) |
| 2796 | { |
| 2797 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2798 | u64 num_entries = |
| 2799 | atomic_read(&trans->transaction->delayed_refs.num_entries); |
| 2800 | u64 avg_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2801 | u64 val; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2802 | |
| 2803 | smp_mb(); |
| 2804 | avg_runtime = fs_info->avg_delayed_ref_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2805 | val = num_entries * avg_runtime; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2806 | if (num_entries * avg_runtime >= NSEC_PER_SEC) |
| 2807 | return 1; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2808 | if (val >= NSEC_PER_SEC / 2) |
| 2809 | return 2; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2810 | |
| 2811 | return btrfs_check_space_for_delayed_refs(trans, root); |
| 2812 | } |
| 2813 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2814 | struct async_delayed_refs { |
| 2815 | struct btrfs_root *root; |
| 2816 | int count; |
| 2817 | int error; |
| 2818 | int sync; |
| 2819 | struct completion wait; |
| 2820 | struct btrfs_work work; |
| 2821 | }; |
| 2822 | |
| 2823 | static void delayed_ref_async_start(struct btrfs_work *work) |
| 2824 | { |
| 2825 | struct async_delayed_refs *async; |
| 2826 | struct btrfs_trans_handle *trans; |
| 2827 | int ret; |
| 2828 | |
| 2829 | async = container_of(work, struct async_delayed_refs, work); |
| 2830 | |
| 2831 | trans = btrfs_join_transaction(async->root); |
| 2832 | if (IS_ERR(trans)) { |
| 2833 | async->error = PTR_ERR(trans); |
| 2834 | goto done; |
| 2835 | } |
| 2836 | |
| 2837 | /* |
| 2838 | * trans->sync means that when we call end_transaciton, we won't |
| 2839 | * wait on delayed refs |
| 2840 | */ |
| 2841 | trans->sync = true; |
| 2842 | ret = btrfs_run_delayed_refs(trans, async->root, async->count); |
| 2843 | if (ret) |
| 2844 | async->error = ret; |
| 2845 | |
| 2846 | ret = btrfs_end_transaction(trans, async->root); |
| 2847 | if (ret && !async->error) |
| 2848 | async->error = ret; |
| 2849 | done: |
| 2850 | if (async->sync) |
| 2851 | complete(&async->wait); |
| 2852 | else |
| 2853 | kfree(async); |
| 2854 | } |
| 2855 | |
| 2856 | int btrfs_async_run_delayed_refs(struct btrfs_root *root, |
| 2857 | unsigned long count, int wait) |
| 2858 | { |
| 2859 | struct async_delayed_refs *async; |
| 2860 | int ret; |
| 2861 | |
| 2862 | async = kmalloc(sizeof(*async), GFP_NOFS); |
| 2863 | if (!async) |
| 2864 | return -ENOMEM; |
| 2865 | |
| 2866 | async->root = root->fs_info->tree_root; |
| 2867 | async->count = count; |
| 2868 | async->error = 0; |
| 2869 | if (wait) |
| 2870 | async->sync = 1; |
| 2871 | else |
| 2872 | async->sync = 0; |
| 2873 | init_completion(&async->wait); |
| 2874 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 2875 | btrfs_init_work(&async->work, btrfs_extent_refs_helper, |
| 2876 | delayed_ref_async_start, NULL, NULL); |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame] | 2877 | |
| 2878 | btrfs_queue_work(root->fs_info->extent_workers, &async->work); |
| 2879 | |
| 2880 | if (wait) { |
| 2881 | wait_for_completion(&async->wait); |
| 2882 | ret = async->error; |
| 2883 | kfree(async); |
| 2884 | return ret; |
| 2885 | } |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2889 | /* |
| 2890 | * this starts processing the delayed reference count updates and |
| 2891 | * extent insertions we have queued up so far. count can be |
| 2892 | * 0, which means to process everything in the tree at the start |
| 2893 | * of the run (but not newly added entries), or it can be some target |
| 2894 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2895 | * |
| 2896 | * Returns 0 on success or if called with an aborted transaction |
| 2897 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2898 | */ |
| 2899 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2900 | struct btrfs_root *root, unsigned long count) |
| 2901 | { |
| 2902 | struct rb_node *node; |
| 2903 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2904 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2905 | int ret; |
| 2906 | int run_all = count == (unsigned long)-1; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2907 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2908 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2909 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2910 | if (trans->aborted) |
| 2911 | return 0; |
| 2912 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2913 | if (root == root->fs_info->extent_root) |
| 2914 | root = root->fs_info->tree_root; |
| 2915 | |
| 2916 | delayed_refs = &trans->transaction->delayed_refs; |
Liu Bo | 26455d3 | 2014-12-17 16:14:09 +0800 | [diff] [blame] | 2917 | if (count == 0) |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2918 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2919 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2920 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2921 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2922 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2923 | #endif |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2924 | trans->can_flush_pending_bgs = false; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2925 | ret = __btrfs_run_delayed_refs(trans, root, count); |
| 2926 | if (ret < 0) { |
| 2927 | btrfs_abort_transaction(trans, root, ret); |
| 2928 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2929 | } |
| 2930 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2931 | if (run_all) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2932 | if (!list_empty(&trans->new_bgs)) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2933 | btrfs_create_pending_block_groups(trans, root); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2934 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2935 | spin_lock(&delayed_refs->lock); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2936 | node = rb_first(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2937 | if (!node) { |
| 2938 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2939 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2940 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2941 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2942 | |
| 2943 | while (node) { |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2944 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2945 | href_node); |
| 2946 | if (btrfs_delayed_ref_is_head(&head->node)) { |
| 2947 | struct btrfs_delayed_ref_node *ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2948 | |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2949 | ref = &head->node; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2950 | atomic_inc(&ref->refs); |
| 2951 | |
| 2952 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2953 | /* |
| 2954 | * Mutex was contended, block until it's |
| 2955 | * released and try again |
| 2956 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2957 | mutex_lock(&head->mutex); |
| 2958 | mutex_unlock(&head->mutex); |
| 2959 | |
| 2960 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2961 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2962 | goto again; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2963 | } else { |
| 2964 | WARN_ON(1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2965 | } |
| 2966 | node = rb_next(node); |
| 2967 | } |
| 2968 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2969 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2970 | goto again; |
| 2971 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2972 | out: |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 2973 | assert_qgroups_uptodate(trans); |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 2974 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2975 | return 0; |
| 2976 | } |
| 2977 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2978 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2979 | struct btrfs_root *root, |
| 2980 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2981 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2982 | { |
| 2983 | struct btrfs_delayed_extent_op *extent_op; |
| 2984 | int ret; |
| 2985 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2986 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2987 | if (!extent_op) |
| 2988 | return -ENOMEM; |
| 2989 | |
| 2990 | extent_op->flags_to_set = flags; |
| 2991 | extent_op->update_flags = 1; |
| 2992 | extent_op->update_key = 0; |
| 2993 | extent_op->is_data = is_data ? 1 : 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2994 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2995 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2996 | ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr, |
| 2997 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2998 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2999 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3000 | return ret; |
| 3001 | } |
| 3002 | |
| 3003 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 3004 | struct btrfs_root *root, |
| 3005 | struct btrfs_path *path, |
| 3006 | u64 objectid, u64 offset, u64 bytenr) |
| 3007 | { |
| 3008 | struct btrfs_delayed_ref_head *head; |
| 3009 | struct btrfs_delayed_ref_node *ref; |
| 3010 | struct btrfs_delayed_data_ref *data_ref; |
| 3011 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3012 | int ret = 0; |
| 3013 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3014 | delayed_refs = &trans->transaction->delayed_refs; |
| 3015 | spin_lock(&delayed_refs->lock); |
| 3016 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3017 | if (!head) { |
| 3018 | spin_unlock(&delayed_refs->lock); |
| 3019 | return 0; |
| 3020 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3021 | |
| 3022 | if (!mutex_trylock(&head->mutex)) { |
| 3023 | atomic_inc(&head->node.refs); |
| 3024 | spin_unlock(&delayed_refs->lock); |
| 3025 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3026 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3027 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 3028 | /* |
| 3029 | * Mutex was contended, block until it's released and let |
| 3030 | * caller try again |
| 3031 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3032 | mutex_lock(&head->mutex); |
| 3033 | mutex_unlock(&head->mutex); |
| 3034 | btrfs_put_delayed_ref(&head->node); |
| 3035 | return -EAGAIN; |
| 3036 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3037 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3038 | |
| 3039 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 3040 | list_for_each_entry(ref, &head->ref_list, list) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 3041 | /* If it's a shared ref we know a cross reference exists */ |
| 3042 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 3043 | ret = 1; |
| 3044 | break; |
| 3045 | } |
| 3046 | |
| 3047 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 3048 | |
| 3049 | /* |
| 3050 | * If our ref doesn't match the one we're currently looking at |
| 3051 | * then we have a cross reference. |
| 3052 | */ |
| 3053 | if (data_ref->root != root->root_key.objectid || |
| 3054 | data_ref->objectid != objectid || |
| 3055 | data_ref->offset != offset) { |
| 3056 | ret = 1; |
| 3057 | break; |
| 3058 | } |
| 3059 | } |
| 3060 | spin_unlock(&head->lock); |
| 3061 | mutex_unlock(&head->mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3062 | return ret; |
| 3063 | } |
| 3064 | |
| 3065 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 3066 | struct btrfs_root *root, |
| 3067 | struct btrfs_path *path, |
| 3068 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3069 | { |
| 3070 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3071 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3072 | struct btrfs_extent_data_ref *ref; |
| 3073 | struct btrfs_extent_inline_ref *iref; |
| 3074 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3075 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3076 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3077 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3078 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3079 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3080 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3081 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3082 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3083 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 3084 | if (ret < 0) |
| 3085 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3086 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 3087 | |
| 3088 | ret = -ENOENT; |
| 3089 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3090 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3091 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3092 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3093 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3094 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3095 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3096 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3097 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3098 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3099 | ret = 1; |
| 3100 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 3101 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3102 | if (item_size < sizeof(*ei)) { |
| 3103 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 3104 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3105 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3106 | #endif |
| 3107 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 3108 | |
| 3109 | if (item_size != sizeof(*ei) + |
| 3110 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 3111 | goto out; |
| 3112 | |
| 3113 | if (btrfs_extent_generation(leaf, ei) <= |
| 3114 | btrfs_root_last_snapshot(&root->root_item)) |
| 3115 | goto out; |
| 3116 | |
| 3117 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 3118 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 3119 | BTRFS_EXTENT_DATA_REF_KEY) |
| 3120 | goto out; |
| 3121 | |
| 3122 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3123 | if (btrfs_extent_refs(leaf, ei) != |
| 3124 | btrfs_extent_data_ref_count(leaf, ref) || |
| 3125 | btrfs_extent_data_ref_root(leaf, ref) != |
| 3126 | root->root_key.objectid || |
| 3127 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 3128 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 3129 | goto out; |
| 3130 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3131 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3132 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3133 | return ret; |
| 3134 | } |
| 3135 | |
| 3136 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 3137 | struct btrfs_root *root, |
| 3138 | u64 objectid, u64 offset, u64 bytenr) |
| 3139 | { |
| 3140 | struct btrfs_path *path; |
| 3141 | int ret; |
| 3142 | int ret2; |
| 3143 | |
| 3144 | path = btrfs_alloc_path(); |
| 3145 | if (!path) |
| 3146 | return -ENOENT; |
| 3147 | |
| 3148 | do { |
| 3149 | ret = check_committed_ref(trans, root, path, objectid, |
| 3150 | offset, bytenr); |
| 3151 | if (ret && ret != -ENOENT) |
| 3152 | goto out; |
| 3153 | |
| 3154 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 3155 | offset, bytenr); |
| 3156 | } while (ret2 == -EAGAIN); |
| 3157 | |
| 3158 | if (ret2 && ret2 != -ENOENT) { |
| 3159 | ret = ret2; |
| 3160 | goto out; |
| 3161 | } |
| 3162 | |
| 3163 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 3164 | ret = 0; |
| 3165 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3166 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3167 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 3168 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3169 | return ret; |
| 3170 | } |
| 3171 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3172 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3173 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3174 | struct extent_buffer *buf, |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3175 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3176 | { |
| 3177 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3178 | u64 num_bytes; |
| 3179 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3180 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3181 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3182 | struct btrfs_key key; |
| 3183 | struct btrfs_file_extent_item *fi; |
| 3184 | int i; |
| 3185 | int level; |
| 3186 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3187 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3188 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3189 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3190 | |
| 3191 | if (btrfs_test_is_dummy_root(root)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3192 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 3193 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3194 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3195 | nritems = btrfs_header_nritems(buf); |
| 3196 | level = btrfs_header_level(buf); |
| 3197 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3198 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3199 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3200 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3201 | if (inc) |
| 3202 | process_func = btrfs_inc_extent_ref; |
| 3203 | else |
| 3204 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3205 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3206 | if (full_backref) |
| 3207 | parent = buf->start; |
| 3208 | else |
| 3209 | parent = 0; |
| 3210 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3211 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3212 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3213 | btrfs_item_key_to_cpu(buf, &key, i); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3214 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3215 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3216 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3217 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3218 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3219 | BTRFS_FILE_EXTENT_INLINE) |
| 3220 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3221 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 3222 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3223 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3224 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3225 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 3226 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 3227 | ret = process_func(trans, root, bytenr, num_bytes, |
| 3228 | parent, ref_root, key.objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3229 | key.offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3230 | if (ret) |
| 3231 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3232 | } else { |
| 3233 | bytenr = btrfs_node_blockptr(buf, i); |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 3234 | num_bytes = root->nodesize; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3235 | ret = process_func(trans, root, bytenr, num_bytes, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 3236 | parent, ref_root, level - 1, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3237 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3238 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3239 | } |
| 3240 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3241 | return 0; |
| 3242 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3243 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3244 | } |
| 3245 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3246 | 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] | 3247 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3248 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3249 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3250 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3251 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3252 | 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] | 3253 | struct extent_buffer *buf, int full_backref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3254 | { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 3255 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3256 | } |
| 3257 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3258 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 3259 | struct btrfs_root *root, |
| 3260 | struct btrfs_path *path, |
| 3261 | struct btrfs_block_group_cache *cache) |
| 3262 | { |
| 3263 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3264 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3265 | unsigned long bi; |
| 3266 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3267 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3268 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3269 | if (ret) { |
| 3270 | if (ret > 0) |
| 3271 | ret = -ENOENT; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3272 | goto fail; |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3273 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3274 | |
| 3275 | leaf = path->nodes[0]; |
| 3276 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3277 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3278 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3279 | fail: |
Filipe Manana | 24b89d0 | 2015-04-25 18:31:05 +0100 | [diff] [blame] | 3280 | btrfs_release_path(path); |
Josef Bacik | df95e7f | 2014-12-12 16:02:20 -0500 | [diff] [blame] | 3281 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3282 | |
| 3283 | } |
| 3284 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3285 | static struct btrfs_block_group_cache * |
| 3286 | next_block_group(struct btrfs_root *root, |
| 3287 | struct btrfs_block_group_cache *cache) |
| 3288 | { |
| 3289 | struct rb_node *node; |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3290 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3291 | spin_lock(&root->fs_info->block_group_cache_lock); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 3292 | |
| 3293 | /* If our block group was removed, we need a full search. */ |
| 3294 | if (RB_EMPTY_NODE(&cache->cache_node)) { |
| 3295 | const u64 next_bytenr = cache->key.objectid + cache->key.offset; |
| 3296 | |
| 3297 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 3298 | btrfs_put_block_group(cache); |
| 3299 | cache = btrfs_lookup_first_block_group(root->fs_info, |
| 3300 | next_bytenr); |
| 3301 | return cache; |
| 3302 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3303 | node = rb_next(&cache->cache_node); |
| 3304 | btrfs_put_block_group(cache); |
| 3305 | if (node) { |
| 3306 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 3307 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 3308 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3309 | } else |
| 3310 | cache = NULL; |
| 3311 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 3312 | return cache; |
| 3313 | } |
| 3314 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3315 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3316 | struct btrfs_trans_handle *trans, |
| 3317 | struct btrfs_path *path) |
| 3318 | { |
| 3319 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 3320 | struct inode *inode = NULL; |
| 3321 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3322 | int dcs = BTRFS_DC_ERROR; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 3323 | u64 num_pages = 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3324 | int retries = 0; |
| 3325 | int ret = 0; |
| 3326 | |
| 3327 | /* |
| 3328 | * If this block group is smaller than 100 megs don't bother caching the |
| 3329 | * block group. |
| 3330 | */ |
| 3331 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 3332 | spin_lock(&block_group->lock); |
| 3333 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3334 | spin_unlock(&block_group->lock); |
| 3335 | return 0; |
| 3336 | } |
| 3337 | |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3338 | if (trans->aborted) |
| 3339 | return 0; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3340 | again: |
| 3341 | inode = lookup_free_space_inode(root, block_group, path); |
| 3342 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3343 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3344 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3345 | goto out; |
| 3346 | } |
| 3347 | |
| 3348 | if (IS_ERR(inode)) { |
| 3349 | BUG_ON(retries); |
| 3350 | retries++; |
| 3351 | |
| 3352 | if (block_group->ro) |
| 3353 | goto out_free; |
| 3354 | |
| 3355 | ret = create_free_space_inode(root, trans, block_group, path); |
| 3356 | if (ret) |
| 3357 | goto out_free; |
| 3358 | goto again; |
| 3359 | } |
| 3360 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3361 | /* We've already setup this transaction, go ahead and exit */ |
| 3362 | if (block_group->cache_generation == trans->transid && |
| 3363 | i_size_read(inode)) { |
| 3364 | dcs = BTRFS_DC_SETUP; |
| 3365 | goto out_put; |
| 3366 | } |
| 3367 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3368 | /* |
| 3369 | * We want to set the generation to 0, that way if anything goes wrong |
| 3370 | * from here on out we know not to trust this cache when we load up next |
| 3371 | * time. |
| 3372 | */ |
| 3373 | BTRFS_I(inode)->generation = 0; |
| 3374 | ret = btrfs_update_inode(trans, root, inode); |
Josef Bacik | 0c0ef4b | 2015-02-12 09:43:51 -0500 | [diff] [blame] | 3375 | if (ret) { |
| 3376 | /* |
| 3377 | * So theoretically we could recover from this, simply set the |
| 3378 | * super cache generation to 0 so we know to invalidate the |
| 3379 | * cache, but then we'd have to keep track of the block groups |
| 3380 | * that fail this way so we know we _have_ to reset this cache |
| 3381 | * before the next commit or risk reading stale cache. So to |
| 3382 | * limit our exposure to horrible edge cases lets just abort the |
| 3383 | * transaction, this only happens in really bad situations |
| 3384 | * anyway. |
| 3385 | */ |
| 3386 | btrfs_abort_transaction(trans, root, ret); |
| 3387 | goto out_put; |
| 3388 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3389 | WARN_ON(ret); |
| 3390 | |
| 3391 | if (i_size_read(inode) > 0) { |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3392 | ret = btrfs_check_trunc_cache_free_space(root, |
| 3393 | &root->fs_info->global_block_rsv); |
| 3394 | if (ret) |
| 3395 | goto out_put; |
| 3396 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3397 | ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3398 | if (ret) |
| 3399 | goto out_put; |
| 3400 | } |
| 3401 | |
| 3402 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3403 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
Chris Mason | e4c88f0 | 2015-04-18 05:22:48 -0700 | [diff] [blame] | 3404 | !btrfs_test_opt(root, SPACE_CACHE)) { |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3405 | /* |
| 3406 | * don't bother trying to write stuff out _if_ |
| 3407 | * a) we're not cached, |
| 3408 | * b) we're with nospace_cache mount option. |
| 3409 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3410 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3411 | spin_unlock(&block_group->lock); |
| 3412 | goto out_put; |
| 3413 | } |
| 3414 | spin_unlock(&block_group->lock); |
| 3415 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3416 | /* |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3417 | * We hit an ENOSPC when setting up the cache in this transaction, just |
| 3418 | * skip doing the setup, we've already cleared the cache so we're safe. |
| 3419 | */ |
| 3420 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { |
| 3421 | ret = -ENOSPC; |
| 3422 | goto out_put; |
| 3423 | } |
| 3424 | |
| 3425 | /* |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3426 | * Try to preallocate enough space based on how big the block group is. |
| 3427 | * Keep in mind this has to include any pinned space which could end up |
| 3428 | * taking up quite a bit since it's not folded into the other space |
| 3429 | * cache. |
| 3430 | */ |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 3431 | num_pages = div_u64(block_group->key.offset, 256 * 1024 * 1024); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3432 | if (!num_pages) |
| 3433 | num_pages = 1; |
| 3434 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3435 | num_pages *= 16; |
| 3436 | num_pages *= PAGE_CACHE_SIZE; |
| 3437 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 3438 | ret = btrfs_check_data_free_space(inode, 0, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3439 | if (ret) |
| 3440 | goto out_put; |
| 3441 | |
| 3442 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3443 | num_pages, num_pages, |
| 3444 | &alloc_hint); |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3445 | /* |
| 3446 | * Our cache requires contiguous chunks so that we don't modify a bunch |
| 3447 | * of metadata or split extents when writing the cache out, which means |
| 3448 | * we can enospc if we are heavily fragmented in addition to just normal |
| 3449 | * out of space conditions. So if we hit this just skip setting up any |
| 3450 | * other block groups for this transaction, maybe we'll unpin enough |
| 3451 | * space the next time around. |
| 3452 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3453 | if (!ret) |
| 3454 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 2968b1f | 2015-10-01 12:55:18 -0400 | [diff] [blame] | 3455 | else if (ret == -ENOSPC) |
| 3456 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 3457 | btrfs_free_reserved_data_space(inode, 0, num_pages); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3458 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3459 | out_put: |
| 3460 | iput(inode); |
| 3461 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3462 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3463 | out: |
| 3464 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3465 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3466 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3467 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3468 | spin_unlock(&block_group->lock); |
| 3469 | |
| 3470 | return ret; |
| 3471 | } |
| 3472 | |
Josef Bacik | dcdf7f6 | 2015-03-02 16:37:31 -0500 | [diff] [blame] | 3473 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, |
| 3474 | struct btrfs_root *root) |
| 3475 | { |
| 3476 | struct btrfs_block_group_cache *cache, *tmp; |
| 3477 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3478 | struct btrfs_path *path; |
| 3479 | |
| 3480 | if (list_empty(&cur_trans->dirty_bgs) || |
| 3481 | !btrfs_test_opt(root, SPACE_CACHE)) |
| 3482 | return 0; |
| 3483 | |
| 3484 | path = btrfs_alloc_path(); |
| 3485 | if (!path) |
| 3486 | return -ENOMEM; |
| 3487 | |
| 3488 | /* Could add new block groups, use _safe just in case */ |
| 3489 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, |
| 3490 | dirty_list) { |
| 3491 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3492 | cache_save_setup(cache, trans, path); |
| 3493 | } |
| 3494 | |
| 3495 | btrfs_free_path(path); |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3499 | /* |
| 3500 | * transaction commit does final block group cache writeback during a |
| 3501 | * critical section where nothing is allowed to change the FS. This is |
| 3502 | * required in order for the cache to actually match the block group, |
| 3503 | * but can introduce a lot of latency into the commit. |
| 3504 | * |
| 3505 | * So, btrfs_start_dirty_block_groups is here to kick off block group |
| 3506 | * cache IO. There's a chance we'll have to redo some of it if the |
| 3507 | * block group changes again during the commit, but it greatly reduces |
| 3508 | * the commit latency by getting rid of the easy block groups while |
| 3509 | * we're still allowing others to join the commit. |
| 3510 | */ |
| 3511 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 3512 | struct btrfs_root *root) |
| 3513 | { |
| 3514 | struct btrfs_block_group_cache *cache; |
| 3515 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3516 | int ret = 0; |
| 3517 | int should_put; |
| 3518 | struct btrfs_path *path = NULL; |
| 3519 | LIST_HEAD(dirty); |
| 3520 | struct list_head *io = &cur_trans->io_bgs; |
| 3521 | int num_started = 0; |
| 3522 | int loops = 0; |
| 3523 | |
| 3524 | spin_lock(&cur_trans->dirty_bgs_lock); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3525 | if (list_empty(&cur_trans->dirty_bgs)) { |
| 3526 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3527 | return 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3528 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3529 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3530 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3531 | |
| 3532 | again: |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3533 | /* |
| 3534 | * make sure all the block groups on our dirty list actually |
| 3535 | * exist |
| 3536 | */ |
| 3537 | btrfs_create_pending_block_groups(trans, root); |
| 3538 | |
| 3539 | if (!path) { |
| 3540 | path = btrfs_alloc_path(); |
| 3541 | if (!path) |
| 3542 | return -ENOMEM; |
| 3543 | } |
| 3544 | |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3545 | /* |
| 3546 | * cache_write_mutex is here only to save us from balance or automatic |
| 3547 | * removal of empty block groups deleting this block group while we are |
| 3548 | * writing out the cache |
| 3549 | */ |
| 3550 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3551 | while (!list_empty(&dirty)) { |
| 3552 | cache = list_first_entry(&dirty, |
| 3553 | struct btrfs_block_group_cache, |
| 3554 | dirty_list); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3555 | /* |
| 3556 | * this can happen if something re-dirties a block |
| 3557 | * group that is already under IO. Just wait for it to |
| 3558 | * finish and then do it all again |
| 3559 | */ |
| 3560 | if (!list_empty(&cache->io_list)) { |
| 3561 | list_del_init(&cache->io_list); |
| 3562 | btrfs_wait_cache_io(root, trans, cache, |
| 3563 | &cache->io_ctl, path, |
| 3564 | cache->key.objectid); |
| 3565 | btrfs_put_block_group(cache); |
| 3566 | } |
| 3567 | |
| 3568 | |
| 3569 | /* |
| 3570 | * btrfs_wait_cache_io uses the cache->dirty_list to decide |
| 3571 | * if it should update the cache_state. Don't delete |
| 3572 | * until after we wait. |
| 3573 | * |
| 3574 | * Since we're not running in the commit critical section |
| 3575 | * we need the dirty_bgs_lock to protect from update_block_group |
| 3576 | */ |
| 3577 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3578 | list_del_init(&cache->dirty_list); |
| 3579 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3580 | |
| 3581 | should_put = 1; |
| 3582 | |
| 3583 | cache_save_setup(cache, trans, path); |
| 3584 | |
| 3585 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3586 | cache->io_ctl.inode = NULL; |
| 3587 | ret = btrfs_write_out_cache(root, trans, cache, path); |
| 3588 | if (ret == 0 && cache->io_ctl.inode) { |
| 3589 | num_started++; |
| 3590 | should_put = 0; |
| 3591 | |
| 3592 | /* |
| 3593 | * the cache_write_mutex is protecting |
| 3594 | * the io_list |
| 3595 | */ |
| 3596 | list_add_tail(&cache->io_list, io); |
| 3597 | } else { |
| 3598 | /* |
| 3599 | * if we failed to write the cache, the |
| 3600 | * generation will be bad and life goes on |
| 3601 | */ |
| 3602 | ret = 0; |
| 3603 | } |
| 3604 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3605 | if (!ret) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3606 | ret = write_one_cache_group(trans, root, path, cache); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3607 | /* |
| 3608 | * Our block group might still be attached to the list |
| 3609 | * of new block groups in the transaction handle of some |
| 3610 | * other task (struct btrfs_trans_handle->new_bgs). This |
| 3611 | * means its block group item isn't yet in the extent |
| 3612 | * tree. If this happens ignore the error, as we will |
| 3613 | * try again later in the critical section of the |
| 3614 | * transaction commit. |
| 3615 | */ |
| 3616 | if (ret == -ENOENT) { |
| 3617 | ret = 0; |
| 3618 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3619 | if (list_empty(&cache->dirty_list)) { |
| 3620 | list_add_tail(&cache->dirty_list, |
| 3621 | &cur_trans->dirty_bgs); |
| 3622 | btrfs_get_block_group(cache); |
| 3623 | } |
| 3624 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3625 | } else if (ret) { |
| 3626 | btrfs_abort_transaction(trans, root, ret); |
| 3627 | } |
| 3628 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3629 | |
| 3630 | /* if its not on the io list, we need to put the block group */ |
| 3631 | if (should_put) |
| 3632 | btrfs_put_block_group(cache); |
| 3633 | |
| 3634 | if (ret) |
| 3635 | break; |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3636 | |
| 3637 | /* |
| 3638 | * Avoid blocking other tasks for too long. It might even save |
| 3639 | * us from writing caches for block groups that are going to be |
| 3640 | * removed. |
| 3641 | */ |
| 3642 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 3643 | mutex_lock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3644 | } |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3645 | mutex_unlock(&trans->transaction->cache_write_mutex); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3646 | |
| 3647 | /* |
| 3648 | * go through delayed refs for all the stuff we've just kicked off |
| 3649 | * and then loop back (just once) |
| 3650 | */ |
| 3651 | ret = btrfs_run_delayed_refs(trans, root, 0); |
| 3652 | if (!ret && loops == 0) { |
| 3653 | loops++; |
| 3654 | spin_lock(&cur_trans->dirty_bgs_lock); |
| 3655 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
Filipe Manana | b58d1a9 | 2015-04-25 18:29:16 +0100 | [diff] [blame] | 3656 | /* |
| 3657 | * dirty_bgs_lock protects us from concurrent block group |
| 3658 | * deletes too (not just cache_write_mutex). |
| 3659 | */ |
| 3660 | if (!list_empty(&dirty)) { |
| 3661 | spin_unlock(&cur_trans->dirty_bgs_lock); |
| 3662 | goto again; |
| 3663 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3664 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | btrfs_free_path(path); |
| 3668 | return ret; |
| 3669 | } |
| 3670 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3671 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 3672 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3673 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3674 | struct btrfs_block_group_cache *cache; |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3675 | struct btrfs_transaction *cur_trans = trans->transaction; |
| 3676 | int ret = 0; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3677 | int should_put; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3678 | struct btrfs_path *path; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3679 | struct list_head *io = &cur_trans->io_bgs; |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3680 | int num_started = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3681 | |
| 3682 | path = btrfs_alloc_path(); |
| 3683 | if (!path) |
| 3684 | return -ENOMEM; |
| 3685 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3686 | /* |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3687 | * Even though we are in the critical section of the transaction commit, |
| 3688 | * we can still have concurrent tasks adding elements to this |
| 3689 | * transaction's list of dirty block groups. These tasks correspond to |
| 3690 | * endio free space workers started when writeback finishes for a |
| 3691 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can |
| 3692 | * allocate new block groups as a result of COWing nodes of the root |
| 3693 | * tree when updating the free space inode. The writeback for the space |
| 3694 | * caches is triggered by an earlier call to |
| 3695 | * btrfs_start_dirty_block_groups() and iterations of the following |
| 3696 | * loop. |
| 3697 | * 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] | 3698 | * delayed refs to make sure we have the best chance at doing this all |
| 3699 | * in one shot. |
| 3700 | */ |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3701 | spin_lock(&cur_trans->dirty_bgs_lock); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3702 | while (!list_empty(&cur_trans->dirty_bgs)) { |
| 3703 | cache = list_first_entry(&cur_trans->dirty_bgs, |
| 3704 | struct btrfs_block_group_cache, |
| 3705 | dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3706 | |
| 3707 | /* |
| 3708 | * this can happen if cache_save_setup re-dirties a block |
| 3709 | * group that is already under IO. Just wait for it to |
| 3710 | * finish and then do it all again |
| 3711 | */ |
| 3712 | if (!list_empty(&cache->io_list)) { |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3713 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3714 | list_del_init(&cache->io_list); |
| 3715 | btrfs_wait_cache_io(root, trans, cache, |
| 3716 | &cache->io_ctl, path, |
| 3717 | cache->key.objectid); |
| 3718 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3719 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3722 | /* |
| 3723 | * don't remove from the dirty list until after we've waited |
| 3724 | * on any pending IO |
| 3725 | */ |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3726 | list_del_init(&cache->dirty_list); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3727 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3728 | should_put = 1; |
| 3729 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3730 | cache_save_setup(cache, trans, path); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3731 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3732 | if (!ret) |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3733 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1); |
| 3734 | |
| 3735 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { |
| 3736 | cache->io_ctl.inode = NULL; |
| 3737 | ret = btrfs_write_out_cache(root, trans, cache, path); |
| 3738 | if (ret == 0 && cache->io_ctl.inode) { |
| 3739 | num_started++; |
| 3740 | should_put = 0; |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3741 | list_add_tail(&cache->io_list, io); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3742 | } else { |
| 3743 | /* |
| 3744 | * if we failed to write the cache, the |
| 3745 | * generation will be bad and life goes on |
| 3746 | */ |
| 3747 | ret = 0; |
| 3748 | } |
| 3749 | } |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3750 | if (!ret) { |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3751 | ret = write_one_cache_group(trans, root, path, cache); |
Filipe Manana | ff1f825 | 2015-05-06 16:15:09 +0100 | [diff] [blame] | 3752 | if (ret) |
| 3753 | btrfs_abort_transaction(trans, root, ret); |
| 3754 | } |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3755 | |
| 3756 | /* if its not on the io list, we need to put the block group */ |
| 3757 | if (should_put) |
| 3758 | btrfs_put_block_group(cache); |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3759 | spin_lock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3760 | } |
Filipe Manana | e44081e | 2015-12-18 03:02:48 +0000 | [diff] [blame^] | 3761 | spin_unlock(&cur_trans->dirty_bgs_lock); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3762 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 3763 | while (!list_empty(io)) { |
| 3764 | cache = list_first_entry(io, struct btrfs_block_group_cache, |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3765 | io_list); |
| 3766 | list_del_init(&cache->io_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 3767 | btrfs_wait_cache_io(root, trans, cache, |
| 3768 | &cache->io_ctl, path, cache->key.objectid); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3769 | btrfs_put_block_group(cache); |
| 3770 | } |
| 3771 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3772 | btrfs_free_path(path); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 3773 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3774 | } |
| 3775 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3776 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 3777 | { |
| 3778 | struct btrfs_block_group_cache *block_group; |
| 3779 | int readonly = 0; |
| 3780 | |
| 3781 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 3782 | if (!block_group || block_group->ro) |
| 3783 | readonly = 1; |
| 3784 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3785 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3786 | return readonly; |
| 3787 | } |
| 3788 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3789 | static const char *alloc_name(u64 flags) |
| 3790 | { |
| 3791 | switch (flags) { |
| 3792 | case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA: |
| 3793 | return "mixed"; |
| 3794 | case BTRFS_BLOCK_GROUP_METADATA: |
| 3795 | return "metadata"; |
| 3796 | case BTRFS_BLOCK_GROUP_DATA: |
| 3797 | return "data"; |
| 3798 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 3799 | return "system"; |
| 3800 | default: |
| 3801 | WARN_ON(1); |
| 3802 | return "invalid-combination"; |
| 3803 | }; |
| 3804 | } |
| 3805 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3806 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3807 | u64 total_bytes, u64 bytes_used, |
| 3808 | struct btrfs_space_info **space_info) |
| 3809 | { |
| 3810 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3811 | int i; |
| 3812 | int factor; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3813 | int ret; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3814 | |
| 3815 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3816 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3817 | factor = 2; |
| 3818 | else |
| 3819 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3820 | |
| 3821 | found = __find_space_info(info, flags); |
| 3822 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3823 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3824 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3825 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3826 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3827 | found->disk_used += bytes_used * factor; |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 3828 | if (total_bytes > 0) |
| 3829 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3830 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3831 | *space_info = found; |
| 3832 | return 0; |
| 3833 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3834 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3835 | if (!found) |
| 3836 | return -ENOMEM; |
| 3837 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3838 | ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3839 | if (ret) { |
| 3840 | kfree(found); |
| 3841 | return ret; |
| 3842 | } |
| 3843 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 3844 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3845 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3846 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3847 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3848 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3849 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3850 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3851 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3852 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3853 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3854 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3855 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3856 | found->bytes_may_use = 0; |
Filipe Manana | 6af3e3a | 2015-09-07 10:41:12 +0100 | [diff] [blame] | 3857 | found->full = 0; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 3858 | found->max_extent_size = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3859 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3860 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3861 | found->flush = 0; |
| 3862 | init_waitqueue_head(&found->wait); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 3863 | INIT_LIST_HEAD(&found->ro_bgs); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3864 | |
| 3865 | ret = kobject_init_and_add(&found->kobj, &space_info_ktype, |
| 3866 | info->space_info_kobj, "%s", |
| 3867 | alloc_name(found->flags)); |
| 3868 | if (ret) { |
| 3869 | kfree(found); |
| 3870 | return ret; |
| 3871 | } |
| 3872 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3873 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3874 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3875 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3876 | info->data_sinfo = found; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3877 | |
| 3878 | return ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3879 | } |
| 3880 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3881 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3882 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3883 | u64 extra_flags = chunk_to_extended(flags) & |
| 3884 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3885 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3886 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3887 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3888 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3889 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3890 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3891 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3892 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3893 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3894 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3895 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3896 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3897 | * returns target flags in extended format or 0 if restripe for this |
| 3898 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3899 | * |
| 3900 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3901 | */ |
| 3902 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 3903 | { |
| 3904 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3905 | u64 target = 0; |
| 3906 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3907 | if (!bctl) |
| 3908 | return 0; |
| 3909 | |
| 3910 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 3911 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3912 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 3913 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 3914 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3915 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 3916 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 3917 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3918 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 3919 | } |
| 3920 | |
| 3921 | return target; |
| 3922 | } |
| 3923 | |
| 3924 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3925 | * @flags: available profiles in extended format (see ctree.h) |
| 3926 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3927 | * Returns reduced profile in chunk format. If profile changing is in |
| 3928 | * progress (either running or paused) picks the target profile (if it's |
| 3929 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3930 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3931 | static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3932 | { |
Miao Xie | 9566997 | 2014-07-24 11:37:14 +0800 | [diff] [blame] | 3933 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3934 | u64 target; |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3935 | u64 raid_type; |
| 3936 | u64 allowed = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3937 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3938 | /* |
| 3939 | * see if restripe for this chunk_type is in progress, if so |
| 3940 | * try to reduce to the target profile |
| 3941 | */ |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3942 | spin_lock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3943 | target = get_restripe_target(root->fs_info, flags); |
| 3944 | if (target) { |
| 3945 | /* pick target profile only if it's already available */ |
| 3946 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3947 | spin_unlock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3948 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3949 | } |
| 3950 | } |
| 3951 | spin_unlock(&root->fs_info->balance_lock); |
| 3952 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3953 | /* First, mask out the RAID levels which aren't possible */ |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3954 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
| 3955 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) |
| 3956 | allowed |= btrfs_raid_group[raid_type]; |
| 3957 | } |
| 3958 | allowed &= flags; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3959 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3960 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) |
| 3961 | allowed = BTRFS_BLOCK_GROUP_RAID6; |
| 3962 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) |
| 3963 | allowed = BTRFS_BLOCK_GROUP_RAID5; |
| 3964 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) |
| 3965 | allowed = BTRFS_BLOCK_GROUP_RAID10; |
| 3966 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) |
| 3967 | allowed = BTRFS_BLOCK_GROUP_RAID1; |
| 3968 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) |
| 3969 | allowed = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3970 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3971 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3972 | |
Zhao Lei | 9c170b2 | 2015-09-15 21:08:08 +0800 | [diff] [blame] | 3973 | return extended_to_chunk(flags | allowed); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3974 | } |
| 3975 | |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3976 | static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3977 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3978 | unsigned seq; |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3979 | u64 flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3980 | |
| 3981 | do { |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3982 | flags = orig_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3983 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 3984 | |
| 3985 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3986 | flags |= root->fs_info->avail_data_alloc_bits; |
| 3987 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3988 | flags |= root->fs_info->avail_system_alloc_bits; |
| 3989 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3990 | flags |= root->fs_info->avail_metadata_alloc_bits; |
| 3991 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3992 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3993 | return btrfs_reduce_alloc_profile(root, flags); |
| 3994 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3995 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3996 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3997 | { |
| 3998 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3999 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4000 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4001 | if (data) |
| 4002 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 4003 | else if (root == root->fs_info->chunk_root) |
| 4004 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 4005 | else |
| 4006 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 4007 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4008 | ret = get_alloc_profile(root, flags); |
| 4009 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4010 | } |
| 4011 | |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4012 | int btrfs_alloc_data_chunk_ondemand(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4013 | { |
| 4014 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4015 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4016 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4017 | u64 used; |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4018 | int ret = 0; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4019 | int need_commit = 2; |
| 4020 | int have_pinned_space; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4021 | |
| 4022 | /* make sure bytes are sectorsize aligned */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4023 | bytes = ALIGN(bytes, root->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4024 | |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4025 | if (btrfs_is_free_space_inode(inode)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4026 | need_commit = 0; |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4027 | ASSERT(current->journal_info); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4028 | } |
| 4029 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4030 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4031 | if (!data_sinfo) |
| 4032 | goto alloc; |
| 4033 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4034 | again: |
| 4035 | /* make sure we have enough space to handle the data first */ |
| 4036 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4037 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 4038 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 4039 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 4040 | |
| 4041 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4042 | struct btrfs_trans_handle *trans; |
| 4043 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4044 | /* |
| 4045 | * if we don't have enough free bytes in this space then we need |
| 4046 | * to alloc a new chunk. |
| 4047 | */ |
Zhao Lei | b9fd47c | 2015-02-09 14:40:20 +0800 | [diff] [blame] | 4048 | if (!data_sinfo->full) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4049 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4050 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4051 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4052 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4053 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4054 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 4055 | /* |
| 4056 | * It is ugly that we don't call nolock join |
| 4057 | * transaction for the free space inode case here. |
| 4058 | * But it is safe because we only do the data space |
| 4059 | * reservation for the free space cache in the |
| 4060 | * transaction context, the common join transaction |
| 4061 | * just increase the counter of the current transaction |
| 4062 | * handler, doesn't try to acquire the trans_lock of |
| 4063 | * the fs. |
| 4064 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4065 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4066 | if (IS_ERR(trans)) |
| 4067 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4068 | |
| 4069 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4070 | alloc_target, |
| 4071 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4072 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4073 | if (ret < 0) { |
| 4074 | if (ret != -ENOSPC) |
| 4075 | return ret; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4076 | else { |
| 4077 | have_pinned_space = 1; |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4078 | goto commit_trans; |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4079 | } |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4080 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 4081 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 4082 | if (!data_sinfo) |
| 4083 | data_sinfo = fs_info->data_sinfo; |
| 4084 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4085 | goto again; |
| 4086 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4087 | |
| 4088 | /* |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4089 | * If we don't have enough pinned space to deal with this |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4090 | * allocation, and no removed chunk in current transaction, |
| 4091 | * don't bother committing the transaction. |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 4092 | */ |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4093 | have_pinned_space = percpu_counter_compare( |
| 4094 | &data_sinfo->total_bytes_pinned, |
| 4095 | used + bytes - data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4096 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4097 | |
| 4098 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 4099 | commit_trans: |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4100 | if (need_commit && |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 4101 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4102 | need_commit--; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4103 | |
Zhao Lei | 9a4e727 | 2015-04-09 12:34:43 +0800 | [diff] [blame] | 4104 | if (need_commit > 0) |
| 4105 | btrfs_wait_ordered_roots(fs_info, -1); |
| 4106 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 4107 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4108 | if (IS_ERR(trans)) |
| 4109 | return PTR_ERR(trans); |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4110 | if (have_pinned_space >= 0 || |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 4111 | test_bit(BTRFS_TRANS_HAVE_FREE_BGS, |
| 4112 | &trans->transaction->flags) || |
Zhao Lei | c99f1b0 | 2015-03-02 19:32:20 +0800 | [diff] [blame] | 4113 | need_commit > 0) { |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4114 | ret = btrfs_commit_transaction(trans, root); |
| 4115 | if (ret) |
| 4116 | return ret; |
Zhao Lei | d7c1517 | 2015-02-26 10:49:20 +0800 | [diff] [blame] | 4117 | /* |
| 4118 | * make sure that all running delayed iput are |
| 4119 | * done |
| 4120 | */ |
| 4121 | down_write(&root->fs_info->delayed_iput_sem); |
| 4122 | up_write(&root->fs_info->delayed_iput_sem); |
Zhao Lei | 94b947b | 2015-02-14 13:23:45 +0800 | [diff] [blame] | 4123 | goto again; |
| 4124 | } else { |
| 4125 | btrfs_end_transaction(trans, root); |
| 4126 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 4127 | } |
| 4128 | |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 4129 | trace_btrfs_space_reservation(root->fs_info, |
| 4130 | "space_info:enospc", |
| 4131 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4132 | return -ENOSPC; |
| 4133 | } |
| 4134 | data_sinfo->bytes_may_use += bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4135 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4136 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4137 | spin_unlock(&data_sinfo->lock); |
| 4138 | |
Dongsheng Yang | 237c0e9 | 2014-12-29 06:23:05 -0500 | [diff] [blame] | 4139 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4143 | * New check_data_free_space() with ability for precious data reservation |
| 4144 | * Will replace old btrfs_check_data_free_space(), but for patch split, |
| 4145 | * add a new function first and then replace it. |
| 4146 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 4147 | 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] | 4148 | { |
| 4149 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4150 | int ret; |
| 4151 | |
| 4152 | /* align the range */ |
| 4153 | len = round_up(start + len, root->sectorsize) - |
| 4154 | round_down(start, root->sectorsize); |
| 4155 | start = round_down(start, root->sectorsize); |
| 4156 | |
| 4157 | ret = btrfs_alloc_data_chunk_ondemand(inode, len); |
| 4158 | if (ret < 0) |
| 4159 | return ret; |
| 4160 | |
Qu Wenruo | 94ed938 | 2015-09-08 17:25:56 +0800 | [diff] [blame] | 4161 | /* |
| 4162 | * Use new btrfs_qgroup_reserve_data to reserve precious data space |
| 4163 | * |
| 4164 | * TODO: Find a good method to avoid reserve data space for NOCOW |
| 4165 | * range, but don't impact performance on quota disable case. |
| 4166 | */ |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4167 | ret = btrfs_qgroup_reserve_data(inode, start, len); |
| 4168 | return ret; |
| 4169 | } |
| 4170 | |
| 4171 | /* |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4172 | * Called if we need to clear a data reservation for this inode |
| 4173 | * Normally in a error case. |
| 4174 | * |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4175 | * This one will *NOT* use accurate qgroup reserved space API, just for case |
| 4176 | * which we can't sleep and is sure it won't affect qgroup reserved space. |
| 4177 | * Like clear_bit_hook(). |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4178 | */ |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4179 | void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, |
| 4180 | u64 len) |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4181 | { |
| 4182 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4183 | struct btrfs_space_info *data_sinfo; |
| 4184 | |
| 4185 | /* Make sure the range is aligned to sectorsize */ |
| 4186 | len = round_up(start + len, root->sectorsize) - |
| 4187 | round_down(start, root->sectorsize); |
| 4188 | start = round_down(start, root->sectorsize); |
| 4189 | |
Qu Wenruo | 4ceff07 | 2015-09-08 17:22:42 +0800 | [diff] [blame] | 4190 | data_sinfo = root->fs_info->data_sinfo; |
| 4191 | spin_lock(&data_sinfo->lock); |
| 4192 | if (WARN_ON(data_sinfo->bytes_may_use < len)) |
| 4193 | data_sinfo->bytes_may_use = 0; |
| 4194 | else |
| 4195 | data_sinfo->bytes_may_use -= len; |
| 4196 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
| 4197 | data_sinfo->flags, len, 0); |
| 4198 | spin_unlock(&data_sinfo->lock); |
| 4199 | } |
| 4200 | |
Qu Wenruo | 51773be | 2015-10-08 18:19:37 +0800 | [diff] [blame] | 4201 | /* |
| 4202 | * Called if we need to clear a data reservation for this inode |
| 4203 | * Normally in a error case. |
| 4204 | * |
| 4205 | * This one will handle the per-indoe data rsv map for accurate reserved |
| 4206 | * space framework. |
| 4207 | */ |
| 4208 | void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len) |
| 4209 | { |
| 4210 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
| 4211 | btrfs_qgroup_free_data(inode, start, len); |
| 4212 | } |
| 4213 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4214 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 4215 | { |
| 4216 | struct list_head *head = &info->space_info; |
| 4217 | struct btrfs_space_info *found; |
| 4218 | |
| 4219 | rcu_read_lock(); |
| 4220 | list_for_each_entry_rcu(found, head, list) { |
| 4221 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4222 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4223 | } |
| 4224 | rcu_read_unlock(); |
| 4225 | } |
| 4226 | |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4227 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
| 4228 | { |
| 4229 | return (global->size << 1); |
| 4230 | } |
| 4231 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 4232 | static int should_alloc_chunk(struct btrfs_root *root, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4233 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4234 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4235 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4236 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4237 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 4238 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4239 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4240 | if (force == CHUNK_ALLOC_FORCE) |
| 4241 | return 1; |
| 4242 | |
| 4243 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4244 | * We need to take into account the global rsv because for all intents |
| 4245 | * and purposes it's used space. Don't worry about locking the |
| 4246 | * global_rsv, it doesn't change except when the transaction commits. |
| 4247 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 4248 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4249 | num_allocated += calc_global_rsv_need_space(global_rsv); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4250 | |
| 4251 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4252 | * in limited mode, we want to have some free space up to |
| 4253 | * about 1% of the FS size. |
| 4254 | */ |
| 4255 | if (force == CHUNK_ALLOC_LIMITED) { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4256 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4257 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 4258 | div_factor_fine(thresh, 1)); |
| 4259 | |
| 4260 | if (num_bytes - num_allocated < thresh) |
| 4261 | return 1; |
| 4262 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4263 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4264 | if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 4265 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4266 | return 1; |
| 4267 | } |
| 4268 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4269 | static u64 get_profile_num_devs(struct btrfs_root *root, u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4270 | { |
| 4271 | u64 num_dev; |
| 4272 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4273 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
| 4274 | BTRFS_BLOCK_GROUP_RAID0 | |
| 4275 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4276 | BTRFS_BLOCK_GROUP_RAID6)) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4277 | num_dev = root->fs_info->fs_devices->rw_devices; |
| 4278 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 4279 | num_dev = 2; |
| 4280 | else |
| 4281 | num_dev = 1; /* DUP or single */ |
| 4282 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4283 | return num_dev; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4284 | } |
| 4285 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4286 | /* |
| 4287 | * If @is_allocation is true, reserve space in the system space info necessary |
| 4288 | * for allocating a chunk, otherwise if it's false, reserve space necessary for |
| 4289 | * removing a chunk. |
| 4290 | */ |
| 4291 | void check_system_chunk(struct btrfs_trans_handle *trans, |
| 4292 | struct btrfs_root *root, |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 4293 | u64 type) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4294 | { |
| 4295 | struct btrfs_space_info *info; |
| 4296 | u64 left; |
| 4297 | u64 thresh; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4298 | int ret = 0; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4299 | u64 num_devs; |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4300 | |
| 4301 | /* |
| 4302 | * Needed because we can end up allocating a system chunk and for an |
| 4303 | * atomic and race free space reservation in the chunk block reserve. |
| 4304 | */ |
| 4305 | ASSERT(mutex_is_locked(&root->fs_info->chunk_mutex)); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4306 | |
| 4307 | info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 4308 | spin_lock(&info->lock); |
| 4309 | left = info->total_bytes - info->bytes_used - info->bytes_pinned - |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4310 | info->bytes_reserved - info->bytes_readonly - |
| 4311 | info->bytes_may_use; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4312 | spin_unlock(&info->lock); |
| 4313 | |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4314 | num_devs = get_profile_num_devs(root, type); |
| 4315 | |
| 4316 | /* num_devs device items to update and 1 chunk item to add or remove */ |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 4317 | thresh = btrfs_calc_trunc_metadata_size(root, num_devs) + |
| 4318 | btrfs_calc_trans_metadata_size(root, 1); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 4319 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4320 | if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4321 | btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu", |
| 4322 | left, thresh, type); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4323 | dump_space_info(info, 0, 0); |
| 4324 | } |
| 4325 | |
| 4326 | if (left < thresh) { |
| 4327 | u64 flags; |
| 4328 | |
| 4329 | flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0); |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 4330 | /* |
| 4331 | * Ignore failure to create system chunk. We might end up not |
| 4332 | * needing it, as we might not need to COW all nodes/leafs from |
| 4333 | * the paths we visit in the chunk tree (they were already COWed |
| 4334 | * or created in the current transaction for example). |
| 4335 | */ |
| 4336 | ret = btrfs_alloc_chunk(trans, root, flags); |
| 4337 | } |
| 4338 | |
| 4339 | if (!ret) { |
| 4340 | ret = btrfs_block_rsv_add(root->fs_info->chunk_root, |
| 4341 | &root->fs_info->chunk_block_rsv, |
| 4342 | thresh, BTRFS_RESERVE_NO_FLUSH); |
| 4343 | if (!ret) |
| 4344 | trans->chunk_bytes_reserved += thresh; |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4348 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4349 | struct btrfs_root *extent_root, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4350 | { |
| 4351 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4352 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4353 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 4354 | int ret = 0; |
| 4355 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4356 | /* Don't re-enter if we're already allocating a chunk */ |
| 4357 | if (trans->allocating_chunk) |
| 4358 | return -ENOSPC; |
| 4359 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4360 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4361 | if (!space_info) { |
| 4362 | ret = update_space_info(extent_root->fs_info, flags, |
| 4363 | 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4364 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4365 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4366 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4367 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4368 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4369 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 4370 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4371 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4372 | if (space_info->full) { |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4373 | if (should_alloc_chunk(extent_root, space_info, force)) |
| 4374 | ret = -ENOSPC; |
| 4375 | else |
| 4376 | ret = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4377 | spin_unlock(&space_info->lock); |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 4378 | return ret; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4379 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4380 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 4381 | if (!should_alloc_chunk(extent_root, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4382 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4383 | return 0; |
| 4384 | } else if (space_info->chunk_alloc) { |
| 4385 | wait_for_alloc = 1; |
| 4386 | } else { |
| 4387 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4388 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4389 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4390 | spin_unlock(&space_info->lock); |
| 4391 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4392 | mutex_lock(&fs_info->chunk_mutex); |
| 4393 | |
| 4394 | /* |
| 4395 | * The chunk_mutex is held throughout the entirety of a chunk |
| 4396 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 4397 | * other guy is done and we need to recheck and see if we should |
| 4398 | * allocate. |
| 4399 | */ |
| 4400 | if (wait_for_alloc) { |
| 4401 | mutex_unlock(&fs_info->chunk_mutex); |
| 4402 | wait_for_alloc = 0; |
| 4403 | goto again; |
| 4404 | } |
| 4405 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4406 | trans->allocating_chunk = true; |
| 4407 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4408 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4409 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 4410 | * allocating mixed chunks instead of individual chunks. |
| 4411 | */ |
| 4412 | if (btrfs_mixed_space_info(space_info)) |
| 4413 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 4414 | |
| 4415 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4416 | * if we're doing a data chunk, go ahead and make sure that |
| 4417 | * we keep a reasonable number of metadata chunks allocated in the |
| 4418 | * FS as well. |
| 4419 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4420 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 4421 | fs_info->data_chunk_allocations++; |
| 4422 | if (!(fs_info->data_chunk_allocations % |
| 4423 | fs_info->metadata_ratio)) |
| 4424 | force_metadata_allocation(fs_info); |
| 4425 | } |
| 4426 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4427 | /* |
| 4428 | * Check if we have enough space in SYSTEM chunk because we may need |
| 4429 | * to update devices. |
| 4430 | */ |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 4431 | check_system_chunk(trans, extent_root, flags); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4432 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4433 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 4434 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 4435 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4436 | spin_lock(&space_info->lock); |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4437 | if (ret < 0 && ret != -ENOSPC) |
| 4438 | goto out; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4439 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4440 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4441 | else |
| 4442 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4443 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4444 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4445 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4446 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4447 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 4448 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4449 | /* |
| 4450 | * When we allocate a new chunk we reserve space in the chunk block |
| 4451 | * reserve to make sure we can COW nodes/leafs in the chunk tree or |
| 4452 | * add new nodes/leafs to it if we end up needing to do it when |
| 4453 | * inserting the chunk item and updating device items as part of the |
| 4454 | * second phase of chunk allocation, performed by |
| 4455 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a |
| 4456 | * large number of new block groups to create in our transaction |
| 4457 | * handle's new_bgs list to avoid exhausting the chunk block reserve |
| 4458 | * in extreme cases - like having a single transaction create many new |
| 4459 | * block groups when starting to write out the free space caches of all |
| 4460 | * the block groups that were made dirty during the lifetime of the |
| 4461 | * transaction. |
| 4462 | */ |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 4463 | if (trans->can_flush_pending_bgs && |
| 4464 | trans->chunk_bytes_reserved >= (2 * 1024 * 1024ull)) { |
Filipe Manana | 00d80e3 | 2015-07-20 14:56:20 +0100 | [diff] [blame] | 4465 | btrfs_create_pending_block_groups(trans, trans->root); |
| 4466 | btrfs_trans_release_chunk_metadata(trans); |
| 4467 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4468 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4469 | } |
| 4470 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4471 | static int can_overcommit(struct btrfs_root *root, |
| 4472 | struct btrfs_space_info *space_info, u64 bytes, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4473 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4474 | { |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4475 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4476 | u64 profile = btrfs_get_alloc_profile(root, 0); |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4477 | u64 space_size; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4478 | u64 avail; |
| 4479 | u64 used; |
| 4480 | |
| 4481 | used = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4482 | space_info->bytes_pinned + space_info->bytes_readonly; |
| 4483 | |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4484 | /* |
| 4485 | * We only want to allow over committing if we have lots of actual space |
| 4486 | * free, but if we don't have enough space to handle the global reserve |
| 4487 | * space then we could end up having a real enospc problem when trying |
| 4488 | * to allocate a chunk or some other such important allocation. |
| 4489 | */ |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4490 | spin_lock(&global_rsv->lock); |
| 4491 | space_size = calc_global_rsv_need_space(global_rsv); |
| 4492 | spin_unlock(&global_rsv->lock); |
| 4493 | if (used + space_size >= space_info->total_bytes) |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4494 | return 0; |
| 4495 | |
| 4496 | used += space_info->bytes_may_use; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4497 | |
| 4498 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4499 | avail = root->fs_info->free_chunk_space; |
| 4500 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4501 | |
| 4502 | /* |
| 4503 | * 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] | 4504 | * space is actually useable. For raid56, the space info used |
| 4505 | * doesn't include the parity drive, so we don't have to |
| 4506 | * change the math |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4507 | */ |
| 4508 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 4509 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4510 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4511 | avail >>= 1; |
| 4512 | |
| 4513 | /* |
Miao Xie | 561c294 | 2012-10-16 11:32:18 +0000 | [diff] [blame] | 4514 | * If we aren't flushing all things, let us overcommit up to |
| 4515 | * 1/2th of the space. If we can flush, don't let us overcommit |
| 4516 | * too much, let it overcommit up to 1/8 of the space. |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4517 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4518 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4519 | avail >>= 3; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4520 | else |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4521 | avail >>= 1; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4522 | |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4523 | if (used + bytes < space_info->total_bytes + avail) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4524 | return 1; |
| 4525 | return 0; |
| 4526 | } |
| 4527 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4528 | static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4529 | unsigned long nr_pages, int nr_items) |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4530 | { |
| 4531 | struct super_block *sb = root->fs_info->sb; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4532 | |
Josef Bacik | 925a6ef | 2013-06-20 12:31:27 -0400 | [diff] [blame] | 4533 | if (down_read_trylock(&sb->s_umount)) { |
| 4534 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); |
| 4535 | up_read(&sb->s_umount); |
| 4536 | } else { |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4537 | /* |
| 4538 | * We needn't worry the filesystem going from r/w to r/o though |
| 4539 | * we don't acquire ->s_umount mutex, because the filesystem |
| 4540 | * should guarantee the delalloc inodes list be empty after |
| 4541 | * the filesystem is readonly(all dirty pages are written to |
| 4542 | * the disk). |
| 4543 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4544 | btrfs_start_delalloc_roots(root->fs_info, 0, nr_items); |
Josef Bacik | 98ad69c | 2013-04-04 11:55:49 -0400 | [diff] [blame] | 4545 | if (!current->journal_info) |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4546 | btrfs_wait_ordered_roots(root->fs_info, nr_items); |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4547 | } |
| 4548 | } |
| 4549 | |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4550 | static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim) |
| 4551 | { |
| 4552 | u64 bytes; |
| 4553 | int nr; |
| 4554 | |
| 4555 | bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 4556 | nr = (int)div64_u64(to_reclaim, bytes); |
| 4557 | if (!nr) |
| 4558 | nr = 1; |
| 4559 | return nr; |
| 4560 | } |
| 4561 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4562 | #define EXTENT_SIZE_PER_ITEM (256 * 1024) |
| 4563 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4564 | /* |
| 4565 | * shrink metadata reservation for delalloc |
| 4566 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4567 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 4568 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4569 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4570 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4571 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4572 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4573 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4574 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 4575 | long time_left; |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4576 | unsigned long nr_pages; |
| 4577 | int loops; |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4578 | int items; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4579 | enum btrfs_reserve_flush_enum flush; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4580 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4581 | /* Calc the number of the pages we need flush for space reservation */ |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4582 | items = calc_reclaim_items_nr(root, to_reclaim); |
| 4583 | to_reclaim = items * EXTENT_SIZE_PER_ITEM; |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4584 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4585 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4586 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4587 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 4588 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4589 | delalloc_bytes = percpu_counter_sum_positive( |
| 4590 | &root->fs_info->delalloc_bytes); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4591 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4592 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4593 | return; |
Miao Xie | 38c135a | 2013-11-04 23:13:21 +0800 | [diff] [blame] | 4594 | if (wait_ordered) |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4595 | btrfs_wait_ordered_roots(root->fs_info, items); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4596 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4597 | } |
| 4598 | |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4599 | loops = 0; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4600 | while (delalloc_bytes && loops < 3) { |
| 4601 | max_reclaim = min(delalloc_bytes, to_reclaim); |
| 4602 | nr_pages = max_reclaim >> PAGE_CACHE_SHIFT; |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4603 | btrfs_writeback_inodes_sb_nr(root, nr_pages, items); |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4604 | /* |
| 4605 | * We need to wait for the async pages to actually start before |
| 4606 | * we do anything. |
| 4607 | */ |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4608 | max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages); |
| 4609 | if (!max_reclaim) |
| 4610 | goto skip_async; |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4611 | |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4612 | if (max_reclaim <= nr_pages) |
| 4613 | max_reclaim = 0; |
| 4614 | else |
| 4615 | max_reclaim -= nr_pages; |
| 4616 | |
| 4617 | wait_event(root->fs_info->async_submit_wait, |
| 4618 | atomic_read(&root->fs_info->async_delalloc_pages) <= |
| 4619 | (int)max_reclaim); |
| 4620 | skip_async: |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4621 | if (!trans) |
| 4622 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 4623 | else |
| 4624 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4625 | spin_lock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4626 | if (can_overcommit(root, space_info, orig, flush)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4627 | spin_unlock(&space_info->lock); |
| 4628 | break; |
| 4629 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4630 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4631 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4632 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4633 | if (wait_ordered && !trans) { |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4634 | btrfs_wait_ordered_roots(root->fs_info, items); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4635 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4636 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4637 | if (time_left) |
| 4638 | break; |
| 4639 | } |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4640 | delalloc_bytes = percpu_counter_sum_positive( |
| 4641 | &root->fs_info->delalloc_bytes); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4642 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4643 | } |
| 4644 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4645 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4646 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 4647 | * @root - the root we're allocating for |
| 4648 | * @bytes - the number of bytes we want to reserve |
| 4649 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4650 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4651 | * This will check to make sure that committing the transaction will actually |
| 4652 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 4653 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4654 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4655 | static int may_commit_transaction(struct btrfs_root *root, |
| 4656 | struct btrfs_space_info *space_info, |
| 4657 | u64 bytes, int force) |
| 4658 | { |
| 4659 | struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; |
| 4660 | struct btrfs_trans_handle *trans; |
| 4661 | |
| 4662 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 4663 | if (trans) |
| 4664 | return -EAGAIN; |
| 4665 | |
| 4666 | if (force) |
| 4667 | goto commit; |
| 4668 | |
| 4669 | /* See if there is enough pinned space to make this reservation */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4670 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
Miao Xie | 0424c54 | 2014-03-06 13:54:59 +0800 | [diff] [blame] | 4671 | bytes) >= 0) |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4672 | goto commit; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4673 | |
| 4674 | /* |
| 4675 | * See if there is some space in the delayed insertion reservation for |
| 4676 | * this reservation. |
| 4677 | */ |
| 4678 | if (space_info != delayed_rsv->space_info) |
| 4679 | return -ENOSPC; |
| 4680 | |
| 4681 | spin_lock(&delayed_rsv->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4682 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4683 | bytes - delayed_rsv->size) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4684 | spin_unlock(&delayed_rsv->lock); |
| 4685 | return -ENOSPC; |
| 4686 | } |
| 4687 | spin_unlock(&delayed_rsv->lock); |
| 4688 | |
| 4689 | commit: |
| 4690 | trans = btrfs_join_transaction(root); |
| 4691 | if (IS_ERR(trans)) |
| 4692 | return -ENOSPC; |
| 4693 | |
| 4694 | return btrfs_commit_transaction(trans, root); |
| 4695 | } |
| 4696 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4697 | enum flush_state { |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4698 | FLUSH_DELAYED_ITEMS_NR = 1, |
| 4699 | FLUSH_DELAYED_ITEMS = 2, |
| 4700 | FLUSH_DELALLOC = 3, |
| 4701 | FLUSH_DELALLOC_WAIT = 4, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4702 | ALLOC_CHUNK = 5, |
| 4703 | COMMIT_TRANS = 6, |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4704 | }; |
| 4705 | |
| 4706 | static int flush_space(struct btrfs_root *root, |
| 4707 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 4708 | u64 orig_bytes, int state) |
| 4709 | { |
| 4710 | struct btrfs_trans_handle *trans; |
| 4711 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4712 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4713 | |
| 4714 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4715 | case FLUSH_DELAYED_ITEMS_NR: |
| 4716 | case FLUSH_DELAYED_ITEMS: |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4717 | if (state == FLUSH_DELAYED_ITEMS_NR) |
| 4718 | nr = calc_reclaim_items_nr(root, num_bytes) * 2; |
| 4719 | else |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4720 | nr = -1; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4721 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4722 | trans = btrfs_join_transaction(root); |
| 4723 | if (IS_ERR(trans)) { |
| 4724 | ret = PTR_ERR(trans); |
| 4725 | break; |
| 4726 | } |
| 4727 | ret = btrfs_run_delayed_items_nr(trans, root, nr); |
| 4728 | btrfs_end_transaction(trans, root); |
| 4729 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4730 | case FLUSH_DELALLOC: |
| 4731 | case FLUSH_DELALLOC_WAIT: |
Miao Xie | 24af7dd | 2014-03-06 13:55:00 +0800 | [diff] [blame] | 4732 | shrink_delalloc(root, num_bytes * 2, orig_bytes, |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4733 | state == FLUSH_DELALLOC_WAIT); |
| 4734 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4735 | case ALLOC_CHUNK: |
| 4736 | trans = btrfs_join_transaction(root); |
| 4737 | if (IS_ERR(trans)) { |
| 4738 | ret = PTR_ERR(trans); |
| 4739 | break; |
| 4740 | } |
| 4741 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4742 | btrfs_get_alloc_profile(root, 0), |
| 4743 | CHUNK_ALLOC_NO_FORCE); |
| 4744 | btrfs_end_transaction(trans, root); |
| 4745 | if (ret == -ENOSPC) |
| 4746 | ret = 0; |
| 4747 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4748 | case COMMIT_TRANS: |
| 4749 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
| 4750 | break; |
| 4751 | default: |
| 4752 | ret = -ENOSPC; |
| 4753 | break; |
| 4754 | } |
| 4755 | |
| 4756 | return ret; |
| 4757 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4758 | |
| 4759 | static inline u64 |
| 4760 | btrfs_calc_reclaim_metadata_size(struct btrfs_root *root, |
| 4761 | struct btrfs_space_info *space_info) |
| 4762 | { |
| 4763 | u64 used; |
| 4764 | u64 expected; |
| 4765 | u64 to_reclaim; |
| 4766 | |
| 4767 | to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024, |
| 4768 | 16 * 1024 * 1024); |
| 4769 | spin_lock(&space_info->lock); |
| 4770 | if (can_overcommit(root, space_info, to_reclaim, |
| 4771 | BTRFS_RESERVE_FLUSH_ALL)) { |
| 4772 | to_reclaim = 0; |
| 4773 | goto out; |
| 4774 | } |
| 4775 | |
| 4776 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4777 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4778 | space_info->bytes_may_use; |
| 4779 | if (can_overcommit(root, space_info, 1024 * 1024, |
| 4780 | BTRFS_RESERVE_FLUSH_ALL)) |
| 4781 | expected = div_factor_fine(space_info->total_bytes, 95); |
| 4782 | else |
| 4783 | expected = div_factor_fine(space_info->total_bytes, 90); |
| 4784 | |
| 4785 | if (used > expected) |
| 4786 | to_reclaim = used - expected; |
| 4787 | else |
| 4788 | to_reclaim = 0; |
| 4789 | to_reclaim = min(to_reclaim, space_info->bytes_may_use + |
| 4790 | space_info->bytes_reserved); |
| 4791 | out: |
| 4792 | spin_unlock(&space_info->lock); |
| 4793 | |
| 4794 | return to_reclaim; |
| 4795 | } |
| 4796 | |
| 4797 | static inline int need_do_async_reclaim(struct btrfs_space_info *space_info, |
| 4798 | struct btrfs_fs_info *fs_info, u64 used) |
| 4799 | { |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4800 | u64 thresh = div_factor_fine(space_info->total_bytes, 98); |
| 4801 | |
| 4802 | /* If we're just plain full then async reclaim just slows us down. */ |
| 4803 | if (space_info->bytes_used >= thresh) |
| 4804 | return 0; |
| 4805 | |
| 4806 | return (used >= thresh && !btrfs_fs_closing(fs_info) && |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4807 | !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)); |
| 4808 | } |
| 4809 | |
| 4810 | static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info, |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4811 | struct btrfs_fs_info *fs_info, |
| 4812 | int flush_state) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4813 | { |
| 4814 | u64 used; |
| 4815 | |
| 4816 | spin_lock(&space_info->lock); |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4817 | /* |
| 4818 | * We run out of space and have not got any free space via flush_space, |
| 4819 | * so don't bother doing async reclaim. |
| 4820 | */ |
| 4821 | if (flush_state > COMMIT_TRANS && space_info->full) { |
| 4822 | spin_unlock(&space_info->lock); |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4826 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4827 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4828 | space_info->bytes_may_use; |
| 4829 | if (need_do_async_reclaim(space_info, fs_info, used)) { |
| 4830 | spin_unlock(&space_info->lock); |
| 4831 | return 1; |
| 4832 | } |
| 4833 | spin_unlock(&space_info->lock); |
| 4834 | |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
| 4838 | static void btrfs_async_reclaim_metadata_space(struct work_struct *work) |
| 4839 | { |
| 4840 | struct btrfs_fs_info *fs_info; |
| 4841 | struct btrfs_space_info *space_info; |
| 4842 | u64 to_reclaim; |
| 4843 | int flush_state; |
| 4844 | |
| 4845 | fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); |
| 4846 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4847 | |
| 4848 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 4849 | space_info); |
| 4850 | if (!to_reclaim) |
| 4851 | return; |
| 4852 | |
| 4853 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 4854 | do { |
| 4855 | flush_space(fs_info->fs_root, space_info, to_reclaim, |
| 4856 | to_reclaim, flush_state); |
| 4857 | flush_state++; |
Liu Bo | 25ce459 | 2014-09-10 12:58:50 +0800 | [diff] [blame] | 4858 | if (!btrfs_need_do_async_reclaim(space_info, fs_info, |
| 4859 | flush_state)) |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4860 | return; |
Josef Bacik | 365c531 | 2015-02-18 13:58:15 -0800 | [diff] [blame] | 4861 | } while (flush_state < COMMIT_TRANS); |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | void btrfs_init_async_reclaim_work(struct work_struct *work) |
| 4865 | { |
| 4866 | INIT_WORK(work, btrfs_async_reclaim_metadata_space); |
| 4867 | } |
| 4868 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4869 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4870 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 4871 | * @root - the root we're allocating for |
| 4872 | * @block_rsv - the block_rsv we're allocating for |
| 4873 | * @orig_bytes - the number of bytes we want |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 4874 | * @flush - whether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4875 | * |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4876 | * This will reserve orgi_bytes number of bytes from the space info associated |
| 4877 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 4878 | * flush out space to make room. It will do this by flushing delalloc if |
| 4879 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 4880 | * regain reservations will be made and this will fail if there is not enough |
| 4881 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4882 | */ |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4883 | static int reserve_metadata_bytes(struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4884 | struct btrfs_block_rsv *block_rsv, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4885 | u64 orig_bytes, |
| 4886 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4887 | { |
| 4888 | struct btrfs_space_info *space_info = block_rsv->space_info; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4889 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4890 | u64 num_bytes = orig_bytes; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4891 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4892 | int ret = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4893 | bool flushing = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4894 | |
| 4895 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4896 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4897 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4898 | /* |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4899 | * We only want to wait if somebody other than us is flushing and we |
| 4900 | * are actually allowed to flush all things. |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4901 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4902 | while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing && |
| 4903 | space_info->flush) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4904 | spin_unlock(&space_info->lock); |
| 4905 | /* |
| 4906 | * If we have a trans handle we can't wait because the flusher |
| 4907 | * may have to commit the transaction, which would mean we would |
| 4908 | * deadlock since we are waiting for the flusher to finish, but |
| 4909 | * hold the current transaction open. |
| 4910 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4911 | if (current->journal_info) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4912 | return -EAGAIN; |
Arne Jansen | b9688bb | 2012-04-18 10:27:16 +0200 | [diff] [blame] | 4913 | ret = wait_event_killable(space_info->wait, !space_info->flush); |
| 4914 | /* Must have been killed, return */ |
| 4915 | if (ret) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4916 | return -EINTR; |
| 4917 | |
| 4918 | spin_lock(&space_info->lock); |
| 4919 | } |
| 4920 | |
| 4921 | ret = -ENOSPC; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4922 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4923 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4924 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4925 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4926 | /* |
| 4927 | * The idea here is that we've not already over-reserved the block group |
| 4928 | * then we can go ahead and save our reservation first and then start |
| 4929 | * flushing if we need to. Otherwise if we've already overcommitted |
| 4930 | * lets start flushing stuff first and then come back and try to make |
| 4931 | * our reservation. |
| 4932 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4933 | if (used <= space_info->total_bytes) { |
| 4934 | if (used + orig_bytes <= space_info->total_bytes) { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4935 | space_info->bytes_may_use += orig_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4936 | trace_btrfs_space_reservation(root->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4937 | "space_info", space_info->flags, orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4938 | ret = 0; |
| 4939 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4940 | /* |
| 4941 | * Ok set num_bytes to orig_bytes since we aren't |
| 4942 | * overocmmitted, this way we only try and reclaim what |
| 4943 | * we need. |
| 4944 | */ |
| 4945 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4946 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4947 | } else { |
| 4948 | /* |
| 4949 | * Ok we're over committed, set num_bytes to the overcommitted |
| 4950 | * amount plus the amount of bytes that we need for this |
| 4951 | * reservation. |
| 4952 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4953 | num_bytes = used - space_info->total_bytes + |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4954 | (orig_bytes * 2); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4955 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4956 | |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 4957 | if (ret && can_overcommit(root, space_info, orig_bytes, flush)) { |
| 4958 | space_info->bytes_may_use += orig_bytes; |
| 4959 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
| 4960 | space_info->flags, orig_bytes, |
| 4961 | 1); |
| 4962 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4963 | } |
| 4964 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4965 | /* |
| 4966 | * Couldn't make our reservation, save our place so while we're trying |
| 4967 | * to reclaim space we can actually use it instead of somebody else |
| 4968 | * stealing it from us. |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4969 | * |
| 4970 | * We make the other tasks wait for the flush only when we can flush |
| 4971 | * all things. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4972 | */ |
Josef Bacik | 72bcd99 | 2012-12-18 15:16:34 -0500 | [diff] [blame] | 4973 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4974 | flushing = true; |
| 4975 | space_info->flush = 1; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4976 | } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
| 4977 | used += orig_bytes; |
Josef Bacik | f6acfd5 | 2014-09-18 11:27:17 -0400 | [diff] [blame] | 4978 | /* |
| 4979 | * We will do the space reservation dance during log replay, |
| 4980 | * which means we won't have fs_info->fs_root set, so don't do |
| 4981 | * the async reclaim as we will panic. |
| 4982 | */ |
| 4983 | if (!root->fs_info->log_root_recovering && |
| 4984 | need_do_async_reclaim(space_info, root->fs_info, used) && |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4985 | !work_busy(&root->fs_info->async_reclaim_work)) |
| 4986 | queue_work(system_unbound_wq, |
| 4987 | &root->fs_info->async_reclaim_work); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4988 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4989 | spin_unlock(&space_info->lock); |
| 4990 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4991 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4992 | goto out; |
| 4993 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4994 | ret = flush_space(root, space_info, num_bytes, orig_bytes, |
| 4995 | flush_state); |
| 4996 | flush_state++; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4997 | |
| 4998 | /* |
| 4999 | * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock |
| 5000 | * would happen. So skip delalloc flush. |
| 5001 | */ |
| 5002 | if (flush == BTRFS_RESERVE_FLUSH_LIMIT && |
| 5003 | (flush_state == FLUSH_DELALLOC || |
| 5004 | flush_state == FLUSH_DELALLOC_WAIT)) |
| 5005 | flush_state = ALLOC_CHUNK; |
| 5006 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 5007 | if (!ret) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5008 | goto again; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5009 | else if (flush == BTRFS_RESERVE_FLUSH_LIMIT && |
| 5010 | flush_state < COMMIT_TRANS) |
| 5011 | goto again; |
| 5012 | else if (flush == BTRFS_RESERVE_FLUSH_ALL && |
| 5013 | flush_state <= COMMIT_TRANS) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5014 | goto again; |
| 5015 | |
| 5016 | out: |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 5017 | if (ret == -ENOSPC && |
| 5018 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { |
| 5019 | struct btrfs_block_rsv *global_rsv = |
| 5020 | &root->fs_info->global_block_rsv; |
| 5021 | |
| 5022 | if (block_rsv != global_rsv && |
| 5023 | !block_rsv_use_bytes(global_rsv, orig_bytes)) |
| 5024 | ret = 0; |
| 5025 | } |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 5026 | if (ret == -ENOSPC) |
| 5027 | trace_btrfs_space_reservation(root->fs_info, |
| 5028 | "space_info:enospc", |
| 5029 | space_info->flags, orig_bytes, 1); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 5030 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5031 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 5032 | space_info->flush = 0; |
| 5033 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5034 | spin_unlock(&space_info->lock); |
| 5035 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5036 | return ret; |
| 5037 | } |
| 5038 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5039 | static struct btrfs_block_rsv *get_block_rsv( |
| 5040 | const struct btrfs_trans_handle *trans, |
| 5041 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5042 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5043 | struct btrfs_block_rsv *block_rsv = NULL; |
| 5044 | |
Alexandru Moise | e9cf439 | 2015-09-09 00:18:50 +0000 | [diff] [blame] | 5045 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
| 5046 | (root == root->fs_info->csum_root && trans->adding_csums) || |
| 5047 | (root == root->fs_info->uuid_root)) |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 5048 | block_rsv = trans->block_rsv; |
| 5049 | |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 5050 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5051 | block_rsv = root->block_rsv; |
| 5052 | |
| 5053 | if (!block_rsv) |
| 5054 | block_rsv = &root->fs_info->empty_block_rsv; |
| 5055 | |
| 5056 | return block_rsv; |
| 5057 | } |
| 5058 | |
| 5059 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 5060 | u64 num_bytes) |
| 5061 | { |
| 5062 | int ret = -ENOSPC; |
| 5063 | spin_lock(&block_rsv->lock); |
| 5064 | if (block_rsv->reserved >= num_bytes) { |
| 5065 | block_rsv->reserved -= num_bytes; |
| 5066 | if (block_rsv->reserved < block_rsv->size) |
| 5067 | block_rsv->full = 0; |
| 5068 | ret = 0; |
| 5069 | } |
| 5070 | spin_unlock(&block_rsv->lock); |
| 5071 | return ret; |
| 5072 | } |
| 5073 | |
| 5074 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 5075 | u64 num_bytes, int update_size) |
| 5076 | { |
| 5077 | spin_lock(&block_rsv->lock); |
| 5078 | block_rsv->reserved += num_bytes; |
| 5079 | if (update_size) |
| 5080 | block_rsv->size += num_bytes; |
| 5081 | else if (block_rsv->reserved >= block_rsv->size) |
| 5082 | block_rsv->full = 1; |
| 5083 | spin_unlock(&block_rsv->lock); |
| 5084 | } |
| 5085 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 5086 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
| 5087 | struct btrfs_block_rsv *dest, u64 num_bytes, |
| 5088 | int min_factor) |
| 5089 | { |
| 5090 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 5091 | u64 min_bytes; |
| 5092 | |
| 5093 | if (global_rsv->space_info != dest->space_info) |
| 5094 | return -ENOSPC; |
| 5095 | |
| 5096 | spin_lock(&global_rsv->lock); |
| 5097 | min_bytes = div_factor(global_rsv->size, min_factor); |
| 5098 | if (global_rsv->reserved < min_bytes + num_bytes) { |
| 5099 | spin_unlock(&global_rsv->lock); |
| 5100 | return -ENOSPC; |
| 5101 | } |
| 5102 | global_rsv->reserved -= num_bytes; |
| 5103 | if (global_rsv->reserved < global_rsv->size) |
| 5104 | global_rsv->full = 0; |
| 5105 | spin_unlock(&global_rsv->lock); |
| 5106 | |
| 5107 | block_rsv_add_bytes(dest, num_bytes, 1); |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5111 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 5112 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 5113 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5114 | { |
| 5115 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 5116 | |
| 5117 | spin_lock(&block_rsv->lock); |
| 5118 | if (num_bytes == (u64)-1) |
| 5119 | num_bytes = block_rsv->size; |
| 5120 | block_rsv->size -= num_bytes; |
| 5121 | if (block_rsv->reserved >= block_rsv->size) { |
| 5122 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 5123 | block_rsv->reserved = block_rsv->size; |
| 5124 | block_rsv->full = 1; |
| 5125 | } else { |
| 5126 | num_bytes = 0; |
| 5127 | } |
| 5128 | spin_unlock(&block_rsv->lock); |
| 5129 | |
| 5130 | if (num_bytes > 0) { |
| 5131 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 5132 | spin_lock(&dest->lock); |
| 5133 | if (!dest->full) { |
| 5134 | u64 bytes_to_add; |
| 5135 | |
| 5136 | bytes_to_add = dest->size - dest->reserved; |
| 5137 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 5138 | dest->reserved += bytes_to_add; |
| 5139 | if (dest->reserved >= dest->size) |
| 5140 | dest->full = 1; |
| 5141 | num_bytes -= bytes_to_add; |
| 5142 | } |
| 5143 | spin_unlock(&dest->lock); |
| 5144 | } |
| 5145 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5146 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5147 | space_info->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5148 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5149 | space_info->flags, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5150 | spin_unlock(&space_info->lock); |
| 5151 | } |
| 5152 | } |
| 5153 | } |
| 5154 | |
| 5155 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 5156 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 5157 | { |
| 5158 | int ret; |
| 5159 | |
| 5160 | ret = block_rsv_use_bytes(src, num_bytes); |
| 5161 | if (ret) |
| 5162 | return ret; |
| 5163 | |
| 5164 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 5165 | return 0; |
| 5166 | } |
| 5167 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5168 | 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] | 5169 | { |
| 5170 | memset(rsv, 0, sizeof(*rsv)); |
| 5171 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5172 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5173 | } |
| 5174 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5175 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, |
| 5176 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5177 | { |
| 5178 | struct btrfs_block_rsv *block_rsv; |
| 5179 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5180 | |
| 5181 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 5182 | if (!block_rsv) |
| 5183 | return NULL; |
| 5184 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 5185 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5186 | block_rsv->space_info = __find_space_info(fs_info, |
| 5187 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5188 | return block_rsv; |
| 5189 | } |
| 5190 | |
| 5191 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 5192 | struct btrfs_block_rsv *rsv) |
| 5193 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 5194 | if (!rsv) |
| 5195 | return; |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5196 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 5197 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5198 | } |
| 5199 | |
Chris Mason | cdfb080 | 2015-04-06 18:17:00 -0700 | [diff] [blame] | 5200 | void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv) |
| 5201 | { |
| 5202 | kfree(rsv); |
| 5203 | } |
| 5204 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5205 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 5206 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, |
| 5207 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5208 | { |
| 5209 | int ret; |
| 5210 | |
| 5211 | if (num_bytes == 0) |
| 5212 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5213 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 5214 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5215 | if (!ret) { |
| 5216 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 5217 | return 0; |
| 5218 | } |
| 5219 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5220 | return ret; |
| 5221 | } |
| 5222 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 5223 | int btrfs_block_rsv_check(struct btrfs_root *root, |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5224 | struct btrfs_block_rsv *block_rsv, int min_factor) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5225 | { |
| 5226 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5227 | int ret = -ENOSPC; |
| 5228 | |
| 5229 | if (!block_rsv) |
| 5230 | return 0; |
| 5231 | |
| 5232 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5233 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 5234 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5235 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5236 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5237 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5238 | return ret; |
| 5239 | } |
| 5240 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5241 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 5242 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, |
| 5243 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5244 | { |
| 5245 | u64 num_bytes = 0; |
| 5246 | int ret = -ENOSPC; |
| 5247 | |
| 5248 | if (!block_rsv) |
| 5249 | return 0; |
| 5250 | |
| 5251 | spin_lock(&block_rsv->lock); |
| 5252 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5253 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5254 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5255 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5256 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5257 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5258 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5259 | if (!ret) |
| 5260 | return 0; |
| 5261 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 5262 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 5263 | if (!ret) { |
| 5264 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5265 | return 0; |
| 5266 | } |
| 5267 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 5268 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5269 | } |
| 5270 | |
| 5271 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 5272 | struct btrfs_block_rsv *dst_rsv, |
| 5273 | u64 num_bytes) |
| 5274 | { |
| 5275 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 5276 | } |
| 5277 | |
| 5278 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 5279 | struct btrfs_block_rsv *block_rsv, |
| 5280 | u64 num_bytes) |
| 5281 | { |
| 5282 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Liu Bo | 1750458 | 2013-12-29 21:44:50 +0800 | [diff] [blame] | 5283 | if (global_rsv == block_rsv || |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5284 | block_rsv->space_info != global_rsv->space_info) |
| 5285 | global_rsv = NULL; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5286 | block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv, |
| 5287 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5288 | } |
| 5289 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5290 | /* |
| 5291 | * helper to calculate size of global block reservation. |
| 5292 | * the desired value is sum of space used by extent tree, |
| 5293 | * checksum tree and root tree |
| 5294 | */ |
| 5295 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 5296 | { |
| 5297 | struct btrfs_space_info *sinfo; |
| 5298 | u64 num_bytes; |
| 5299 | u64 meta_used; |
| 5300 | u64 data_used; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5301 | int csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5302 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5303 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 5304 | spin_lock(&sinfo->lock); |
| 5305 | data_used = sinfo->bytes_used; |
| 5306 | spin_unlock(&sinfo->lock); |
| 5307 | |
| 5308 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 5309 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 5310 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 5311 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5312 | meta_used = sinfo->bytes_used; |
| 5313 | spin_unlock(&sinfo->lock); |
| 5314 | |
| 5315 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 5316 | csum_size * 2; |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 5317 | num_bytes += div_u64(data_used + meta_used, 50); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5318 | |
| 5319 | if (num_bytes * 3 > meta_used) |
David Sterba | f8c269d | 2015-01-16 17:21:12 +0100 | [diff] [blame] | 5320 | num_bytes = div_u64(meta_used, 3); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5321 | |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 5322 | return ALIGN(num_bytes, fs_info->extent_root->nodesize << 10); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5326 | { |
| 5327 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 5328 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 5329 | u64 num_bytes; |
| 5330 | |
| 5331 | num_bytes = calc_global_metadata_size(fs_info); |
| 5332 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5333 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5334 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5335 | |
Josef Bacik | fdf30d1 | 2013-03-26 15:31:45 -0400 | [diff] [blame] | 5336 | block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5337 | |
| 5338 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 5339 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 5340 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5341 | |
| 5342 | if (sinfo->total_bytes > num_bytes) { |
| 5343 | num_bytes = sinfo->total_bytes - num_bytes; |
| 5344 | block_rsv->reserved += num_bytes; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5345 | sinfo->bytes_may_use += num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5346 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5347 | sinfo->flags, num_bytes, 1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | if (block_rsv->reserved >= block_rsv->size) { |
| 5351 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5352 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5353 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5354 | sinfo->flags, num_bytes, 0); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5355 | block_rsv->reserved = block_rsv->size; |
| 5356 | block_rsv->full = 1; |
| 5357 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 5358 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5359 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 5360 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5361 | } |
| 5362 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5363 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5364 | { |
| 5365 | struct btrfs_space_info *space_info; |
| 5366 | |
| 5367 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 5368 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5369 | |
| 5370 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5371 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5372 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5373 | fs_info->trans_block_rsv.space_info = space_info; |
| 5374 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5375 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5376 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5377 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 5378 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 5379 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 5380 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Stefan Behrens | 3a6cad9 | 2013-05-16 14:48:19 +0000 | [diff] [blame] | 5381 | if (fs_info->quota_root) |
| 5382 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5383 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5384 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5385 | update_global_block_rsv(fs_info); |
| 5386 | } |
| 5387 | |
| 5388 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 5389 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5390 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 5391 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5392 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 5393 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 5394 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 5395 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 5396 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 5397 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 5398 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 5399 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5400 | } |
| 5401 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5402 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 5403 | struct btrfs_root *root) |
| 5404 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 5405 | if (!trans->block_rsv) |
| 5406 | return; |
| 5407 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5408 | if (!trans->bytes_reserved) |
| 5409 | return; |
| 5410 | |
Chris Mason | e77266e | 2012-02-24 10:39:05 -0500 | [diff] [blame] | 5411 | trace_btrfs_space_reservation(root->fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5412 | trans->transid, trans->bytes_reserved, 0); |
Josef Bacik | b24e03d | 2011-10-14 14:40:17 -0400 | [diff] [blame] | 5413 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5414 | trans->bytes_reserved = 0; |
| 5415 | } |
| 5416 | |
Filipe Manana | 4fbcdf6 | 2015-05-20 14:01:54 +0100 | [diff] [blame] | 5417 | /* |
| 5418 | * To be called after all the new block groups attached to the transaction |
| 5419 | * handle have been created (btrfs_create_pending_block_groups()). |
| 5420 | */ |
| 5421 | void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) |
| 5422 | { |
| 5423 | struct btrfs_fs_info *fs_info = trans->root->fs_info; |
| 5424 | |
| 5425 | if (!trans->chunk_bytes_reserved) |
| 5426 | return; |
| 5427 | |
| 5428 | WARN_ON_ONCE(!list_empty(&trans->new_bgs)); |
| 5429 | |
| 5430 | block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL, |
| 5431 | trans->chunk_bytes_reserved); |
| 5432 | trans->chunk_bytes_reserved = 0; |
| 5433 | } |
| 5434 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5435 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5436 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 5437 | struct inode *inode) |
| 5438 | { |
| 5439 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5440 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 5441 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 5442 | |
| 5443 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 5444 | * We need to hold space in order to delete our orphan item once we've |
| 5445 | * added it, so this takes the reservation so we can release it later |
| 5446 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5447 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5448 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5449 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 5450 | btrfs_ino(inode), num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5451 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 5452 | } |
| 5453 | |
| 5454 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 5455 | { |
| 5456 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5457 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5458 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 5459 | btrfs_ino(inode), num_bytes, 0); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5460 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 5461 | } |
| 5462 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5463 | /* |
| 5464 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation |
| 5465 | * root: the root of the parent directory |
| 5466 | * rsv: block reservation |
| 5467 | * items: the number of items that we need do reservation |
| 5468 | * qgroup_reserved: used to return the reserved size in qgroup |
| 5469 | * |
| 5470 | * This function is used to reserve the space for snapshot/subvolume |
| 5471 | * creation and deletion. Those operations are different with the |
| 5472 | * common file/directory operations, they change two fs/file trees |
| 5473 | * and root tree, the number of items that the qgroup reserves is |
| 5474 | * different with the free space reservation. So we can not use |
| 5475 | * the space reseravtion mechanism in start_transaction(). |
| 5476 | */ |
| 5477 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, |
| 5478 | struct btrfs_block_rsv *rsv, |
| 5479 | int items, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5480 | u64 *qgroup_reserved, |
| 5481 | bool use_global_rsv) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5482 | { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5483 | u64 num_bytes; |
| 5484 | int ret; |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5485 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5486 | |
| 5487 | if (root->fs_info->quota_enabled) { |
| 5488 | /* One for parent inode, two for dir entries */ |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 5489 | num_bytes = 3 * root->nodesize; |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5490 | ret = btrfs_qgroup_reserve_meta(root, num_bytes); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5491 | if (ret) |
| 5492 | return ret; |
| 5493 | } else { |
| 5494 | num_bytes = 0; |
| 5495 | } |
| 5496 | |
| 5497 | *qgroup_reserved = num_bytes; |
| 5498 | |
| 5499 | num_bytes = btrfs_calc_trans_metadata_size(root, items); |
| 5500 | rsv->space_info = __find_space_info(root->fs_info, |
| 5501 | BTRFS_BLOCK_GROUP_METADATA); |
| 5502 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, |
| 5503 | BTRFS_RESERVE_FLUSH_ALL); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5504 | |
| 5505 | if (ret == -ENOSPC && use_global_rsv) |
| 5506 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes); |
| 5507 | |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5508 | if (ret && *qgroup_reserved) |
| 5509 | btrfs_qgroup_free_meta(root, *qgroup_reserved); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5510 | |
| 5511 | return ret; |
| 5512 | } |
| 5513 | |
| 5514 | void btrfs_subvolume_release_metadata(struct btrfs_root *root, |
| 5515 | struct btrfs_block_rsv *rsv, |
| 5516 | u64 qgroup_reserved) |
| 5517 | { |
| 5518 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5519 | } |
| 5520 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5521 | /** |
| 5522 | * drop_outstanding_extent - drop an outstanding extent |
| 5523 | * @inode: the inode we're dropping the extent for |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5524 | * @num_bytes: the number of bytes we're relaseing. |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5525 | * |
| 5526 | * This is called when we are freeing up an outstanding extent, either called |
| 5527 | * after an error or after an extent is written. This will return the number of |
| 5528 | * reserved extents that need to be freed. This must be called with |
| 5529 | * BTRFS_I(inode)->lock held. |
| 5530 | */ |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5531 | static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5532 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5533 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5534 | unsigned dropped_extents = 0; |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5535 | unsigned num_extents = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5536 | |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5537 | num_extents = (unsigned)div64_u64(num_bytes + |
| 5538 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 5539 | BTRFS_MAX_EXTENT_SIZE); |
| 5540 | ASSERT(num_extents); |
| 5541 | ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents); |
| 5542 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5543 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5544 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5545 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5546 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5547 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5548 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5549 | /* |
| 5550 | * If we have more or the same amount of outsanding extents than we have |
| 5551 | * reserved then we need to leave the reserved extents count alone. |
| 5552 | */ |
| 5553 | if (BTRFS_I(inode)->outstanding_extents >= |
| 5554 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5555 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5556 | |
| 5557 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 5558 | BTRFS_I(inode)->outstanding_extents; |
| 5559 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5560 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5563 | /** |
| 5564 | * calc_csum_metadata_size - return the amount of metada space that must be |
| 5565 | * reserved/free'd for the given bytes. |
| 5566 | * @inode: the inode we're manipulating |
| 5567 | * @num_bytes: the number of bytes in question |
| 5568 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 5569 | * |
| 5570 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 5571 | * correct amount of metadata that must either be reserved or freed. We |
| 5572 | * calculate how many checksums we can fit into one leaf and then divide the |
| 5573 | * number of bytes that will need to be checksumed by this value to figure out |
| 5574 | * how many checksums will be required. If we are adding bytes then the number |
| 5575 | * may go up and we will return the number of additional bytes that must be |
| 5576 | * reserved. If it is going down we will return the number of bytes that must |
| 5577 | * be freed. |
| 5578 | * |
| 5579 | * This must be called with BTRFS_I(inode)->lock held. |
| 5580 | */ |
| 5581 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 5582 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5583 | { |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5584 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 1262133 | 2015-02-03 07:50:16 -0800 | [diff] [blame] | 5585 | u64 old_csums, num_csums; |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5586 | |
| 5587 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 5588 | BTRFS_I(inode)->csum_bytes == 0) |
| 5589 | return 0; |
| 5590 | |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 5591 | old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5592 | if (reserve) |
| 5593 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 5594 | else |
| 5595 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
Chris Mason | 28f75a0 | 2015-02-04 06:59:29 -0800 | [diff] [blame] | 5596 | num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5597 | |
| 5598 | /* No change, no need to reserve more */ |
| 5599 | if (old_csums == num_csums) |
| 5600 | return 0; |
| 5601 | |
| 5602 | if (reserve) |
| 5603 | return btrfs_calc_trans_metadata_size(root, |
| 5604 | num_csums - old_csums); |
| 5605 | |
| 5606 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 5610 | { |
| 5611 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5612 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5613 | u64 to_reserve = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5614 | u64 csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5615 | unsigned nr_extents = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5616 | int extra_reserve = 0; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5617 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; |
Jan Schmidt | eb6b88d | 2013-01-27 23:26:00 -0700 | [diff] [blame] | 5618 | int ret = 0; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5619 | bool delalloc_lock = true; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5620 | u64 to_free = 0; |
| 5621 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5622 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5623 | /* If we are a free space inode we need to not flush since we will be in |
| 5624 | * the middle of a transaction commit. We also don't need the delalloc |
| 5625 | * mutex since we won't race with anybody. We need this mostly to make |
| 5626 | * lockdep shut its filthy mouth. |
| 5627 | */ |
| 5628 | if (btrfs_is_free_space_inode(inode)) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5629 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5630 | delalloc_lock = false; |
| 5631 | } |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 5632 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5633 | if (flush != BTRFS_RESERVE_NO_FLUSH && |
| 5634 | btrfs_transaction_in_commit(root->fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5635 | schedule_timeout(1); |
| 5636 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5637 | if (delalloc_lock) |
| 5638 | mutex_lock(&BTRFS_I(inode)->delalloc_mutex); |
| 5639 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5640 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5641 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5642 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 6a41dd0 | 2015-03-13 15:12:23 -0400 | [diff] [blame] | 5643 | nr_extents = (unsigned)div64_u64(num_bytes + |
| 5644 | BTRFS_MAX_EXTENT_SIZE - 1, |
| 5645 | BTRFS_MAX_EXTENT_SIZE); |
| 5646 | BTRFS_I(inode)->outstanding_extents += nr_extents; |
| 5647 | nr_extents = 0; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5648 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5649 | if (BTRFS_I(inode)->outstanding_extents > |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5650 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5651 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 5652 | BTRFS_I(inode)->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5653 | |
| 5654 | /* |
| 5655 | * Add an item to reserve for updating the inode when we complete the |
| 5656 | * delalloc io. |
| 5657 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5658 | if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5659 | &BTRFS_I(inode)->runtime_flags)) { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5660 | nr_extents++; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5661 | extra_reserve = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5665 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5666 | csum_bytes = BTRFS_I(inode)->csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5667 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5668 | |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5669 | if (root->fs_info->quota_enabled) { |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5670 | ret = btrfs_qgroup_reserve_meta(root, |
| 5671 | nr_extents * root->nodesize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5672 | if (ret) |
| 5673 | goto out_fail; |
Wang Shilong | a9870c0 | 2013-03-01 11:33:01 +0000 | [diff] [blame] | 5674 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5675 | |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5676 | ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); |
| 5677 | if (unlikely(ret)) { |
Qu Wenruo | 7174109 | 2015-09-08 17:22:41 +0800 | [diff] [blame] | 5678 | btrfs_qgroup_free_meta(root, nr_extents * root->nodesize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5679 | goto out_fail; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5680 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5681 | |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5682 | spin_lock(&BTRFS_I(inode)->lock); |
| 5683 | if (extra_reserve) { |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5684 | set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5685 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5686 | nr_extents--; |
| 5687 | } |
| 5688 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 5689 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5690 | |
| 5691 | if (delalloc_lock) |
| 5692 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5693 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5694 | if (to_reserve) |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 5695 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5696 | btrfs_ino(inode), to_reserve, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5697 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 5698 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5699 | return 0; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5700 | |
| 5701 | out_fail: |
| 5702 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5703 | dropped = drop_outstanding_extent(inode, num_bytes); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5704 | /* |
| 5705 | * If the inodes csum_bytes is the same as the original |
| 5706 | * csum_bytes then we know we haven't raced with any free()ers |
| 5707 | * so we can just reduce our inodes csum bytes and carry on. |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5708 | */ |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 5709 | if (BTRFS_I(inode)->csum_bytes == csum_bytes) { |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5710 | calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 5711 | } else { |
| 5712 | u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes; |
| 5713 | u64 bytes; |
| 5714 | |
| 5715 | /* |
| 5716 | * This is tricky, but first we need to figure out how much we |
| 5717 | * free'd from any free-ers that occured during this |
| 5718 | * reservation, so we reset ->csum_bytes to the csum_bytes |
| 5719 | * before we dropped our lock, and then call the free for the |
| 5720 | * number of bytes that were freed while we were trying our |
| 5721 | * reservation. |
| 5722 | */ |
| 5723 | bytes = csum_bytes - BTRFS_I(inode)->csum_bytes; |
| 5724 | BTRFS_I(inode)->csum_bytes = csum_bytes; |
| 5725 | to_free = calc_csum_metadata_size(inode, bytes, 0); |
| 5726 | |
| 5727 | |
| 5728 | /* |
| 5729 | * Now we need to see how much we would have freed had we not |
| 5730 | * been making this reservation and our ->csum_bytes were not |
| 5731 | * artificially inflated. |
| 5732 | */ |
| 5733 | BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes; |
| 5734 | bytes = csum_bytes - orig_csum_bytes; |
| 5735 | bytes = calc_csum_metadata_size(inode, bytes, 0); |
| 5736 | |
| 5737 | /* |
| 5738 | * Now reset ->csum_bytes to what it should be. If bytes is |
| 5739 | * more than to_free then we would have free'd more space had we |
| 5740 | * not had an artificially high ->csum_bytes, so we need to free |
| 5741 | * the remainder. If bytes is the same or less then we don't |
| 5742 | * need to do anything, the other free-ers did the correct |
| 5743 | * thing. |
| 5744 | */ |
| 5745 | BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes; |
| 5746 | if (bytes > to_free) |
| 5747 | to_free = bytes - to_free; |
| 5748 | else |
| 5749 | to_free = 0; |
| 5750 | } |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5751 | spin_unlock(&BTRFS_I(inode)->lock); |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 5752 | if (dropped) |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5753 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
| 5754 | |
| 5755 | if (to_free) { |
| 5756 | btrfs_block_rsv_release(root, block_rsv, to_free); |
| 5757 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 5758 | btrfs_ino(inode), to_free, 0); |
| 5759 | } |
| 5760 | if (delalloc_lock) |
| 5761 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
| 5762 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5763 | } |
| 5764 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5765 | /** |
| 5766 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 5767 | * @inode: the inode to release the reservation for |
| 5768 | * @num_bytes: the number of bytes we're releasing |
| 5769 | * |
| 5770 | * This will release the metadata reservation for an inode. This can be called |
| 5771 | * once we complete IO for a given set of bytes to release their metadata |
| 5772 | * reservations. |
| 5773 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5774 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 5775 | { |
| 5776 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5777 | u64 to_free = 0; |
| 5778 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5779 | |
| 5780 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5781 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | dcab6a3 | 2015-02-11 15:08:59 -0500 | [diff] [blame] | 5782 | dropped = drop_outstanding_extent(inode, num_bytes); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5783 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 5784 | if (num_bytes) |
| 5785 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5786 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5787 | if (dropped > 0) |
| 5788 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5789 | |
Josef Bacik | 6a3891c | 2015-03-16 17:38:52 -0400 | [diff] [blame] | 5790 | if (btrfs_test_is_dummy_root(root)) |
| 5791 | return; |
| 5792 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5793 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 5794 | btrfs_ino(inode), to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5795 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5796 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 5797 | to_free); |
| 5798 | } |
| 5799 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5800 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5801 | * btrfs_delalloc_reserve_space - reserve data and metadata space for |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 5802 | * delalloc |
| 5803 | * @inode: inode we're writing to |
| 5804 | * @start: start range we are writing to |
| 5805 | * @len: how long the range we are writing to |
| 5806 | * |
| 5807 | * TODO: This function will finally replace old btrfs_delalloc_reserve_space() |
| 5808 | * |
| 5809 | * This will do the following things |
| 5810 | * |
| 5811 | * o reserve space in data space info for num bytes |
| 5812 | * and reserve precious corresponding qgroup space |
| 5813 | * (Done in check_data_free_space) |
| 5814 | * |
| 5815 | * o reserve space for metadata space, based on the number of outstanding |
| 5816 | * extents and how much csums will be needed |
| 5817 | * also reserve metadata space in a per root over-reserve method. |
| 5818 | * o add to the inodes->delalloc_bytes |
| 5819 | * o add it to the fs_info's delalloc inodes list. |
| 5820 | * (Above 3 all done in delalloc_reserve_metadata) |
| 5821 | * |
| 5822 | * Return 0 for success |
| 5823 | * Return <0 for error(-ENOSPC or -EQUOT) |
| 5824 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5825 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 5826 | { |
| 5827 | int ret; |
| 5828 | |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5829 | ret = btrfs_check_data_free_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 5830 | if (ret < 0) |
| 5831 | return ret; |
| 5832 | ret = btrfs_delalloc_reserve_metadata(inode, len); |
| 5833 | if (ret < 0) |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5834 | btrfs_free_reserved_data_space(inode, start, len); |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 5835 | return ret; |
| 5836 | } |
| 5837 | |
| 5838 | /** |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5839 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 5840 | * @inode: inode we're releasing space for |
| 5841 | * @start: start position of the space already reserved |
| 5842 | * @len: the len of the space already reserved |
| 5843 | * |
| 5844 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 5845 | * called in the case that we don't need the metadata AND data reservations |
| 5846 | * anymore. So if there is an error or we insert an inline extent. |
| 5847 | * |
| 5848 | * This function will release the metadata space that was not used and will |
| 5849 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 5850 | * list if there are no delalloc bytes left. |
| 5851 | * Also it will handle the qgroup reserved space. |
| 5852 | */ |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5853 | void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len) |
Qu Wenruo | 1ada3a6 | 2015-09-08 17:25:53 +0800 | [diff] [blame] | 5854 | { |
| 5855 | btrfs_delalloc_release_metadata(inode, len); |
Qu Wenruo | 7cf5b97 | 2015-09-08 17:25:55 +0800 | [diff] [blame] | 5856 | btrfs_free_reserved_data_space(inode, start, len); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5857 | } |
| 5858 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 5859 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 5860 | struct btrfs_root *root, u64 bytenr, |
| 5861 | u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5862 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5863 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5864 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5865 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5866 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5867 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5868 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5869 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5870 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 5871 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5872 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5873 | if (alloc) |
| 5874 | old_val += num_bytes; |
| 5875 | else |
| 5876 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5877 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 5878 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5879 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5880 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5881 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5882 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5883 | return -ENOENT; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5884 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 5885 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5886 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5887 | factor = 2; |
| 5888 | else |
| 5889 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5890 | /* |
| 5891 | * If this block group has free space cache written out, we |
| 5892 | * need to make sure to load it if we are removing space. This |
| 5893 | * is because we need the unpinning stage to actually add the |
| 5894 | * space back to the block group, otherwise we will leak space. |
| 5895 | */ |
| 5896 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 5897 | cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5898 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5899 | byte_in_group = bytenr - cache->key.objectid; |
| 5900 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5901 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5902 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5903 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5904 | |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 5905 | if (btrfs_test_opt(root, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5906 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 5907 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 5908 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5909 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5910 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5911 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5912 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5913 | btrfs_set_block_group_used(&cache->item, old_val); |
| 5914 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5915 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5916 | cache->space_info->bytes_used += num_bytes; |
| 5917 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5918 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5919 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5920 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5921 | old_val -= num_bytes; |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 5922 | btrfs_set_block_group_used(&cache->item, old_val); |
| 5923 | cache->pinned += num_bytes; |
| 5924 | cache->space_info->bytes_pinned += num_bytes; |
| 5925 | cache->space_info->bytes_used -= num_bytes; |
| 5926 | cache->space_info->disk_used -= num_bytes * factor; |
| 5927 | spin_unlock(&cache->lock); |
| 5928 | spin_unlock(&cache->space_info->lock); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 5929 | |
Filipe Manana | ae0ab00 | 2014-11-26 15:28:52 +0000 | [diff] [blame] | 5930 | set_extent_dirty(info->pinned_extents, |
| 5931 | bytenr, bytenr + num_bytes - 1, |
| 5932 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5933 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 5934 | |
| 5935 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 5936 | if (list_empty(&cache->dirty_list)) { |
| 5937 | list_add_tail(&cache->dirty_list, |
| 5938 | &trans->transaction->dirty_bgs); |
| 5939 | trans->transaction->num_dirty_bgs++; |
| 5940 | btrfs_get_block_group(cache); |
| 5941 | } |
| 5942 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 5943 | |
Filipe Manana | 036a934 | 2015-11-23 15:25:16 +0000 | [diff] [blame] | 5944 | /* |
| 5945 | * No longer have used bytes in this block group, queue it for |
| 5946 | * deletion. We do this after adding the block group to the |
| 5947 | * dirty list to avoid races between cleaner kthread and space |
| 5948 | * cache writeout. |
| 5949 | */ |
| 5950 | if (!alloc && old_val == 0) { |
| 5951 | spin_lock(&info->unused_bgs_lock); |
| 5952 | if (list_empty(&cache->bg_list)) { |
| 5953 | btrfs_get_block_group(cache); |
| 5954 | list_add_tail(&cache->bg_list, |
| 5955 | &info->unused_bgs); |
| 5956 | } |
| 5957 | spin_unlock(&info->unused_bgs_lock); |
| 5958 | } |
| 5959 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5960 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5961 | total -= num_bytes; |
| 5962 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5963 | } |
| 5964 | return 0; |
| 5965 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5966 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5967 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 5968 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5969 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5970 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5971 | |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 5972 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 5973 | bytenr = root->fs_info->first_logical_byte; |
| 5974 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 5975 | |
| 5976 | if (bytenr < (u64)-1) |
| 5977 | return bytenr; |
| 5978 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5979 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 5980 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5981 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5982 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5983 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5984 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5985 | |
| 5986 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5987 | } |
| 5988 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5989 | static int pin_down_extent(struct btrfs_root *root, |
| 5990 | struct btrfs_block_group_cache *cache, |
| 5991 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5992 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5993 | spin_lock(&cache->space_info->lock); |
| 5994 | spin_lock(&cache->lock); |
| 5995 | cache->pinned += num_bytes; |
| 5996 | cache->space_info->bytes_pinned += num_bytes; |
| 5997 | if (reserved) { |
| 5998 | cache->reserved -= num_bytes; |
| 5999 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6000 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6001 | spin_unlock(&cache->lock); |
| 6002 | spin_unlock(&cache->space_info->lock); |
| 6003 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6004 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 6005 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Dongsheng Yang | e2d1f92 | 2015-02-06 10:26:52 -0500 | [diff] [blame] | 6006 | if (reserved) |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 6007 | trace_btrfs_reserved_extent_free(root, bytenr, num_bytes); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 6008 | return 0; |
| 6009 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6010 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6011 | /* |
| 6012 | * this function must be called within transaction |
| 6013 | */ |
| 6014 | int btrfs_pin_extent(struct btrfs_root *root, |
| 6015 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6016 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6017 | struct btrfs_block_group_cache *cache; |
| 6018 | |
| 6019 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6020 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6021 | |
| 6022 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 6023 | |
| 6024 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6025 | return 0; |
| 6026 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6027 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6028 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6029 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6030 | */ |
Liu Bo | dcfac41 | 2012-12-27 09:01:20 +0000 | [diff] [blame] | 6031 | int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6032 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6033 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6034 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6035 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6036 | |
| 6037 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6038 | if (!cache) |
| 6039 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6040 | |
| 6041 | /* |
| 6042 | * pull in the free space cache (if any) so that our pin |
| 6043 | * removes the free space from the cache. We have load_only set |
| 6044 | * to one because the slow code to read in the free extents does check |
| 6045 | * the pinned extents. |
| 6046 | */ |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6047 | cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6048 | |
| 6049 | pin_down_extent(root, cache, bytenr, num_bytes, 0); |
| 6050 | |
| 6051 | /* 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] | 6052 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6053 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6054 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6055 | } |
| 6056 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6057 | static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes) |
| 6058 | { |
| 6059 | int ret; |
| 6060 | struct btrfs_block_group_cache *block_group; |
| 6061 | struct btrfs_caching_control *caching_ctl; |
| 6062 | |
| 6063 | block_group = btrfs_lookup_block_group(root->fs_info, start); |
| 6064 | if (!block_group) |
| 6065 | return -EINVAL; |
| 6066 | |
| 6067 | cache_block_group(block_group, 0); |
| 6068 | caching_ctl = get_caching_control(block_group); |
| 6069 | |
| 6070 | if (!caching_ctl) { |
| 6071 | /* Logic error */ |
| 6072 | BUG_ON(!block_group_cache_done(block_group)); |
| 6073 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 6074 | } else { |
| 6075 | mutex_lock(&caching_ctl->mutex); |
| 6076 | |
| 6077 | if (start >= caching_ctl->progress) { |
| 6078 | ret = add_excluded_extent(root, start, num_bytes); |
| 6079 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 6080 | ret = btrfs_remove_free_space(block_group, |
| 6081 | start, num_bytes); |
| 6082 | } else { |
| 6083 | num_bytes = caching_ctl->progress - start; |
| 6084 | ret = btrfs_remove_free_space(block_group, |
| 6085 | start, num_bytes); |
| 6086 | if (ret) |
| 6087 | goto out_lock; |
| 6088 | |
| 6089 | num_bytes = (start + num_bytes) - |
| 6090 | caching_ctl->progress; |
| 6091 | start = caching_ctl->progress; |
| 6092 | ret = add_excluded_extent(root, start, num_bytes); |
| 6093 | } |
| 6094 | out_lock: |
| 6095 | mutex_unlock(&caching_ctl->mutex); |
| 6096 | put_caching_control(caching_ctl); |
| 6097 | } |
| 6098 | btrfs_put_block_group(block_group); |
| 6099 | return ret; |
| 6100 | } |
| 6101 | |
| 6102 | int btrfs_exclude_logged_extents(struct btrfs_root *log, |
| 6103 | struct extent_buffer *eb) |
| 6104 | { |
| 6105 | struct btrfs_file_extent_item *item; |
| 6106 | struct btrfs_key key; |
| 6107 | int found_type; |
| 6108 | int i; |
| 6109 | |
| 6110 | if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) |
| 6111 | return 0; |
| 6112 | |
| 6113 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 6114 | btrfs_item_key_to_cpu(eb, &key, i); |
| 6115 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 6116 | continue; |
| 6117 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 6118 | found_type = btrfs_file_extent_type(eb, item); |
| 6119 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 6120 | continue; |
| 6121 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 6122 | continue; |
| 6123 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 6124 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 6125 | __exclude_logged_extent(log, key.objectid, key.offset); |
| 6126 | } |
| 6127 | |
| 6128 | return 0; |
| 6129 | } |
| 6130 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6131 | /** |
| 6132 | * btrfs_update_reserved_bytes - update the block_group and space info counters |
| 6133 | * @cache: The cache we are manipulating |
| 6134 | * @num_bytes: The number of bytes in question |
| 6135 | * @reserve: One of the reservation enums |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6136 | * @delalloc: The blocks are allocated for the delalloc write |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6137 | * |
| 6138 | * This is called by the allocator when it reserves space, or by somebody who is |
| 6139 | * freeing space that was never actually used on disk. For example if you |
| 6140 | * reserve some space for a new leaf in transaction A and before transaction A |
| 6141 | * commits you free that leaf, you call this with reserve set to 0 in order to |
| 6142 | * clear the reservation. |
| 6143 | * |
| 6144 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper |
| 6145 | * ENOSPC accounting. For data we handle the reservation through clearing the |
| 6146 | * delalloc bits in the io_tree. We have to do this since we could end up |
| 6147 | * allocating less disk space for the amount of data we have reserved in the |
| 6148 | * case of compression. |
| 6149 | * |
| 6150 | * If this is a reservation and the block group has become read only we cannot |
| 6151 | * make the reservation and return -EAGAIN, otherwise this function always |
| 6152 | * succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6153 | */ |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6154 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6155 | u64 num_bytes, int reserve, int delalloc) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6156 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6157 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6158 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6159 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6160 | spin_lock(&space_info->lock); |
| 6161 | spin_lock(&cache->lock); |
| 6162 | if (reserve != RESERVE_FREE) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6163 | if (cache->ro) { |
| 6164 | ret = -EAGAIN; |
| 6165 | } else { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6166 | cache->reserved += num_bytes; |
| 6167 | space_info->bytes_reserved += num_bytes; |
| 6168 | if (reserve == RESERVE_ALLOC) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6169 | trace_btrfs_space_reservation(cache->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 6170 | "space_info", space_info->flags, |
| 6171 | num_bytes, 0); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6172 | space_info->bytes_may_use -= num_bytes; |
| 6173 | } |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6174 | |
| 6175 | if (delalloc) |
| 6176 | cache->delalloc_bytes += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6177 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6178 | } else { |
| 6179 | if (cache->ro) |
| 6180 | space_info->bytes_readonly += num_bytes; |
| 6181 | cache->reserved -= num_bytes; |
| 6182 | space_info->bytes_reserved -= num_bytes; |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6183 | |
| 6184 | if (delalloc) |
| 6185 | cache->delalloc_bytes -= num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6186 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6187 | spin_unlock(&cache->lock); |
| 6188 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6189 | return ret; |
| 6190 | } |
| 6191 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6192 | void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6193 | struct btrfs_root *root) |
| 6194 | { |
| 6195 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 6196 | struct btrfs_caching_control *next; |
| 6197 | struct btrfs_caching_control *caching_ctl; |
| 6198 | struct btrfs_block_group_cache *cache; |
| 6199 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6200 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6201 | |
| 6202 | list_for_each_entry_safe(caching_ctl, next, |
| 6203 | &fs_info->caching_block_groups, list) { |
| 6204 | cache = caching_ctl->block_group; |
| 6205 | if (block_group_cache_done(cache)) { |
| 6206 | cache->last_byte_to_unpin = (u64)-1; |
| 6207 | list_del_init(&caching_ctl->list); |
| 6208 | put_caching_control(caching_ctl); |
| 6209 | } else { |
| 6210 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 6211 | } |
| 6212 | } |
| 6213 | |
| 6214 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6215 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 6216 | else |
| 6217 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 6218 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 6219 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6220 | |
| 6221 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6222 | } |
| 6223 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6224 | /* |
| 6225 | * Returns the free cluster for the given space info and sets empty_cluster to |
| 6226 | * what it should be based on the mount options. |
| 6227 | */ |
| 6228 | static struct btrfs_free_cluster * |
| 6229 | fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info, |
| 6230 | u64 *empty_cluster) |
| 6231 | { |
| 6232 | struct btrfs_free_cluster *ret = NULL; |
| 6233 | bool ssd = btrfs_test_opt(root, SSD); |
| 6234 | |
| 6235 | *empty_cluster = 0; |
| 6236 | if (btrfs_mixed_space_info(space_info)) |
| 6237 | return ret; |
| 6238 | |
| 6239 | if (ssd) |
| 6240 | *empty_cluster = 2 * 1024 * 1024; |
| 6241 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
| 6242 | ret = &root->fs_info->meta_alloc_cluster; |
| 6243 | if (!ssd) |
| 6244 | *empty_cluster = 64 * 1024; |
| 6245 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) { |
| 6246 | ret = &root->fs_info->data_alloc_cluster; |
| 6247 | } |
| 6248 | |
| 6249 | return ret; |
| 6250 | } |
| 6251 | |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6252 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end, |
| 6253 | const bool return_free_space) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6254 | { |
| 6255 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 6256 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6257 | struct btrfs_space_info *space_info; |
| 6258 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6259 | struct btrfs_free_cluster *cluster = NULL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6260 | u64 len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6261 | u64 total_unpinned = 0; |
| 6262 | u64 empty_cluster = 0; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6263 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6264 | |
| 6265 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6266 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6267 | if (!cache || |
| 6268 | start >= cache->key.objectid + cache->key.offset) { |
| 6269 | if (cache) |
| 6270 | btrfs_put_block_group(cache); |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6271 | total_unpinned = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6272 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6273 | BUG_ON(!cache); /* Logic error */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6274 | |
| 6275 | cluster = fetch_cluster_info(root, |
| 6276 | cache->space_info, |
| 6277 | &empty_cluster); |
| 6278 | empty_cluster <<= 1; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | len = cache->key.objectid + cache->key.offset - start; |
| 6282 | len = min(len, end + 1 - start); |
| 6283 | |
| 6284 | if (start < cache->last_byte_to_unpin) { |
| 6285 | len = min(len, cache->last_byte_to_unpin - start); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6286 | if (return_free_space) |
| 6287 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6288 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6289 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6290 | start += len; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6291 | total_unpinned += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6292 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6293 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 6294 | /* |
| 6295 | * If this space cluster has been marked as fragmented and we've |
| 6296 | * unpinned enough in this block group to potentially allow a |
| 6297 | * cluster to be created inside of it go ahead and clear the |
| 6298 | * fragmented check. |
| 6299 | */ |
| 6300 | if (cluster && cluster->fragmented && |
| 6301 | total_unpinned > empty_cluster) { |
| 6302 | spin_lock(&cluster->lock); |
| 6303 | cluster->fragmented = 0; |
| 6304 | spin_unlock(&cluster->lock); |
| 6305 | } |
| 6306 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6307 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6308 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6309 | cache->pinned -= len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6310 | space_info->bytes_pinned -= len; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 6311 | space_info->max_extent_size = 0; |
Liu Bo | d288db5 | 2014-07-02 16:58:01 +0800 | [diff] [blame] | 6312 | percpu_counter_add(&space_info->total_bytes_pinned, -len); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6313 | if (cache->ro) { |
| 6314 | space_info->bytes_readonly += len; |
| 6315 | readonly = true; |
| 6316 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6317 | spin_unlock(&cache->lock); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 6318 | if (!readonly && global_rsv->space_info == space_info) { |
| 6319 | spin_lock(&global_rsv->lock); |
| 6320 | if (!global_rsv->full) { |
| 6321 | len = min(len, global_rsv->size - |
| 6322 | global_rsv->reserved); |
| 6323 | global_rsv->reserved += len; |
| 6324 | space_info->bytes_may_use += len; |
| 6325 | if (global_rsv->reserved >= global_rsv->size) |
| 6326 | global_rsv->full = 1; |
| 6327 | } |
| 6328 | spin_unlock(&global_rsv->lock); |
| 6329 | } |
| 6330 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6334 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 6335 | return 0; |
| 6336 | } |
| 6337 | |
| 6338 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6339 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6340 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6341 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6342 | struct btrfs_block_group_cache *block_group, *tmp; |
| 6343 | struct list_head *deleted_bgs; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6344 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6345 | u64 start; |
| 6346 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6347 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6348 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6349 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 6350 | unpin = &fs_info->freed_extents[1]; |
| 6351 | else |
| 6352 | unpin = &fs_info->freed_extents[0]; |
| 6353 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6354 | while (!trans->aborted) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6355 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6356 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 6357 | EXTENT_DIRTY, NULL); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6358 | if (ret) { |
| 6359 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6360 | break; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6361 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6362 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 6363 | if (btrfs_test_opt(root, DISCARD)) |
| 6364 | ret = btrfs_discard_extent(root, start, |
| 6365 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6366 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 6367 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 6368 | unpin_extent_range(root, start, end, true); |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 6369 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6370 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6371 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6372 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 6373 | /* |
| 6374 | * Transaction is finished. We don't need the lock anymore. We |
| 6375 | * do need to clean up the block groups in case of a transaction |
| 6376 | * abort. |
| 6377 | */ |
| 6378 | deleted_bgs = &trans->transaction->deleted_bgs; |
| 6379 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { |
| 6380 | u64 trimmed = 0; |
| 6381 | |
| 6382 | ret = -EROFS; |
| 6383 | if (!trans->aborted) |
| 6384 | ret = btrfs_discard_extent(root, |
| 6385 | block_group->key.objectid, |
| 6386 | block_group->key.offset, |
| 6387 | &trimmed); |
| 6388 | |
| 6389 | list_del_init(&block_group->bg_list); |
| 6390 | btrfs_put_block_group_trimming(block_group); |
| 6391 | btrfs_put_block_group(block_group); |
| 6392 | |
| 6393 | if (ret) { |
| 6394 | const char *errstr = btrfs_decode_error(ret); |
| 6395 | btrfs_warn(fs_info, |
| 6396 | "Discard failed while removing blockgroup: errno=%d %s\n", |
| 6397 | ret, errstr); |
| 6398 | } |
| 6399 | } |
| 6400 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 6401 | return 0; |
| 6402 | } |
| 6403 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6404 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
| 6405 | u64 owner, u64 root_objectid) |
| 6406 | { |
| 6407 | struct btrfs_space_info *space_info; |
| 6408 | u64 flags; |
| 6409 | |
| 6410 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 6411 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) |
| 6412 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 6413 | else |
| 6414 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 6415 | } else { |
| 6416 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 6417 | } |
| 6418 | |
| 6419 | space_info = __find_space_info(fs_info, flags); |
| 6420 | BUG_ON(!space_info); /* Logic bug */ |
| 6421 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); |
| 6422 | } |
| 6423 | |
| 6424 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6425 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 6426 | struct btrfs_root *root, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6427 | struct btrfs_delayed_ref_node *node, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6428 | u64 root_objectid, u64 owner_objectid, |
| 6429 | u64 owner_offset, int refs_to_drop, |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6430 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6431 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6432 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6433 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 6434 | struct btrfs_fs_info *info = root->fs_info; |
| 6435 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6436 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6437 | struct btrfs_extent_item *ei; |
| 6438 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6439 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6440 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6441 | int extent_slot = 0; |
| 6442 | int found_extent = 0; |
| 6443 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6444 | u32 item_size; |
| 6445 | u64 refs; |
Qu Wenruo | c682f9b | 2015-03-17 16:59:47 +0800 | [diff] [blame] | 6446 | u64 bytenr = node->bytenr; |
| 6447 | u64 num_bytes = node->num_bytes; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6448 | int last_ref = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6449 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 6450 | SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 6451 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6452 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6453 | if (!path) |
| 6454 | return -ENOMEM; |
| 6455 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 6456 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6457 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6458 | |
| 6459 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 6460 | BUG_ON(!is_data && refs_to_drop != 1); |
| 6461 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6462 | if (is_data) |
| 6463 | skinny_metadata = 0; |
| 6464 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6465 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 6466 | bytenr, num_bytes, parent, |
| 6467 | root_objectid, owner_objectid, |
| 6468 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6469 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6470 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6471 | while (extent_slot >= 0) { |
| 6472 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6473 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6474 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6475 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6476 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6477 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6478 | found_extent = 1; |
| 6479 | break; |
| 6480 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6481 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 6482 | key.offset == owner_objectid) { |
| 6483 | found_extent = 1; |
| 6484 | break; |
| 6485 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6486 | if (path->slots[0] - extent_slot > 5) |
| 6487 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6488 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6489 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6490 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6491 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 6492 | if (found_extent && item_size < sizeof(*ei)) |
| 6493 | found_extent = 0; |
| 6494 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6495 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6496 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6497 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6498 | NULL, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6499 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6500 | if (ret) { |
| 6501 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6502 | goto out; |
| 6503 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6504 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6505 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6506 | |
| 6507 | key.objectid = bytenr; |
| 6508 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6509 | key.offset = num_bytes; |
| 6510 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6511 | if (!is_data && skinny_metadata) { |
| 6512 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 6513 | key.offset = owner_objectid; |
| 6514 | } |
| 6515 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6516 | ret = btrfs_search_slot(trans, extent_root, |
| 6517 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6518 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 6519 | /* |
| 6520 | * Couldn't find our skinny metadata item, |
| 6521 | * see if we have ye olde extent item. |
| 6522 | */ |
| 6523 | path->slots[0]--; |
| 6524 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 6525 | path->slots[0]); |
| 6526 | if (key.objectid == bytenr && |
| 6527 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 6528 | key.offset == num_bytes) |
| 6529 | ret = 0; |
| 6530 | } |
| 6531 | |
| 6532 | if (ret > 0 && skinny_metadata) { |
| 6533 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 6534 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6535 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6536 | key.offset = num_bytes; |
| 6537 | btrfs_release_path(path); |
| 6538 | ret = btrfs_search_slot(trans, extent_root, |
| 6539 | &key, path, -1, 1); |
| 6540 | } |
| 6541 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6542 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6543 | btrfs_err(info, "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6544 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 6545 | if (ret > 0) |
| 6546 | btrfs_print_leaf(extent_root, |
| 6547 | path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 6548 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6549 | if (ret < 0) { |
| 6550 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6551 | goto out; |
| 6552 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6553 | extent_slot = path->slots[0]; |
| 6554 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6555 | } else if (WARN_ON(ret == -ENOENT)) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6556 | btrfs_print_leaf(extent_root, path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6557 | btrfs_err(info, |
| 6558 | "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] | 6559 | bytenr, parent, root_objectid, owner_objectid, |
| 6560 | owner_offset); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 6561 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6562 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6563 | } else { |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6564 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6565 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6566 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6567 | |
| 6568 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6569 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6570 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 6571 | if (item_size < sizeof(*ei)) { |
| 6572 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 6573 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 6574 | owner_objectid, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6575 | if (ret < 0) { |
| 6576 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6577 | goto out; |
| 6578 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6579 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6580 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6581 | path->leave_spinning = 1; |
| 6582 | |
| 6583 | key.objectid = bytenr; |
| 6584 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6585 | key.offset = num_bytes; |
| 6586 | |
| 6587 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 6588 | -1, 1); |
| 6589 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6590 | btrfs_err(info, "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6591 | ret, bytenr); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6592 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 6593 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6594 | if (ret < 0) { |
| 6595 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6596 | goto out; |
| 6597 | } |
| 6598 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6599 | extent_slot = path->slots[0]; |
| 6600 | leaf = path->nodes[0]; |
| 6601 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 6602 | } |
| 6603 | #endif |
| 6604 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6605 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 6606 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6607 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 6608 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6609 | struct btrfs_tree_block_info *bi; |
| 6610 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 6611 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 6612 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6613 | } |
| 6614 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6615 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 6616 | if (refs < refs_to_drop) { |
| 6617 | btrfs_err(info, "trying to drop %d refs but we only have %Lu " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 6618 | "for bytenr %Lu", refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 6619 | ret = -EINVAL; |
| 6620 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6621 | goto out; |
| 6622 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6623 | refs -= refs_to_drop; |
| 6624 | |
| 6625 | if (refs > 0) { |
| 6626 | if (extent_op) |
| 6627 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 6628 | /* |
| 6629 | * In the case of inline back ref, reference count will |
| 6630 | * be updated by remove_extent_backref |
| 6631 | */ |
| 6632 | if (iref) { |
| 6633 | BUG_ON(!found_extent); |
| 6634 | } else { |
| 6635 | btrfs_set_extent_refs(leaf, ei, refs); |
| 6636 | btrfs_mark_buffer_dirty(leaf); |
| 6637 | } |
| 6638 | if (found_extent) { |
| 6639 | ret = remove_extent_backref(trans, extent_root, path, |
| 6640 | iref, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6641 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6642 | if (ret) { |
| 6643 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6644 | goto out; |
| 6645 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6646 | } |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6647 | add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid, |
| 6648 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6649 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6650 | if (found_extent) { |
| 6651 | BUG_ON(is_data && refs_to_drop != |
Zhaolei | 9ed0dea | 2015-08-06 22:16:24 +0800 | [diff] [blame] | 6652 | extent_data_ref_count(path, iref)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6653 | if (iref) { |
| 6654 | BUG_ON(path->slots[0] != extent_slot); |
| 6655 | } else { |
| 6656 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 6657 | path->slots[0] = extent_slot; |
| 6658 | num_to_del = 2; |
| 6659 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 6660 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6661 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6662 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6663 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 6664 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6665 | if (ret) { |
| 6666 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6667 | goto out; |
| 6668 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6669 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 6670 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6671 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 6672 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6673 | if (ret) { |
| 6674 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6675 | goto out; |
| 6676 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 6677 | } |
| 6678 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 6679 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6680 | if (ret) { |
| 6681 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6682 | goto out; |
| 6683 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6684 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6685 | btrfs_release_path(path); |
| 6686 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6687 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6688 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6689 | return ret; |
| 6690 | } |
| 6691 | |
| 6692 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6693 | * 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] | 6694 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 6695 | * a given extent, and if there are no other delayed refs to be processed, it |
| 6696 | * removes it from the tree. |
| 6697 | */ |
| 6698 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 6699 | struct btrfs_root *root, u64 bytenr) |
| 6700 | { |
| 6701 | struct btrfs_delayed_ref_head *head; |
| 6702 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6703 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6704 | |
| 6705 | delayed_refs = &trans->transaction->delayed_refs; |
| 6706 | spin_lock(&delayed_refs->lock); |
| 6707 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 6708 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 6709 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6710 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6711 | spin_lock(&head->lock); |
Qu Wenruo | c6fc245 | 2015-03-30 17:03:00 +0800 | [diff] [blame] | 6712 | if (!list_empty(&head->ref_list)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6713 | goto out; |
| 6714 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6715 | if (head->extent_op) { |
| 6716 | if (!head->must_insert_reserved) |
| 6717 | goto out; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 6718 | btrfs_free_delayed_extent_op(head->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6719 | head->extent_op = NULL; |
| 6720 | } |
| 6721 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6722 | /* |
| 6723 | * waiting for the lock here would deadlock. If someone else has it |
| 6724 | * locked they are already in the process of dropping it anyway |
| 6725 | */ |
| 6726 | if (!mutex_trylock(&head->mutex)) |
| 6727 | goto out; |
| 6728 | |
| 6729 | /* |
| 6730 | * at this point we have a head with no other entries. Go |
| 6731 | * ahead and process it. |
| 6732 | */ |
| 6733 | head->node.in_tree = 0; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 6734 | rb_erase(&head->href_node, &delayed_refs->href_root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6735 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6736 | atomic_dec(&delayed_refs->num_entries); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6737 | |
| 6738 | /* |
| 6739 | * we don't take a ref on the node because we're removing it from the |
| 6740 | * tree, so we just steal the ref the tree was holding. |
| 6741 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6742 | delayed_refs->num_heads--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6743 | if (head->processing == 0) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6744 | delayed_refs->num_heads_ready--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6745 | head->processing = 0; |
| 6746 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6747 | spin_unlock(&delayed_refs->lock); |
| 6748 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6749 | BUG_ON(head->extent_op); |
| 6750 | if (head->must_insert_reserved) |
| 6751 | ret = 1; |
| 6752 | |
| 6753 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6754 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6755 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6756 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6757 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 6758 | |
| 6759 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6760 | spin_unlock(&delayed_refs->lock); |
| 6761 | return 0; |
| 6762 | } |
| 6763 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6764 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 6765 | struct btrfs_root *root, |
| 6766 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6767 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6768 | { |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6769 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6770 | int ret; |
| 6771 | |
| 6772 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6773 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 6774 | buf->start, buf->len, |
| 6775 | parent, root->root_key.objectid, |
| 6776 | btrfs_header_level(buf), |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 6777 | BTRFS_DROP_DELAYED_REF, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6778 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6779 | } |
| 6780 | |
| 6781 | if (!last_ref) |
| 6782 | return; |
| 6783 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6784 | if (btrfs_header_generation(buf) == trans->transid) { |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 6785 | struct btrfs_block_group_cache *cache; |
| 6786 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6787 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 6788 | ret = check_ref_cleanup(trans, root, buf->start); |
| 6789 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 6790 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6791 | } |
| 6792 | |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 6793 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
| 6794 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6795 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 6796 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 6797 | btrfs_put_block_group(cache); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 6798 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 6802 | |
| 6803 | btrfs_add_free_space(cache, buf->start, buf->len); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6804 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0); |
Filipe Manana | 6219872 | 2015-01-06 20:18:45 +0000 | [diff] [blame] | 6805 | btrfs_put_block_group(cache); |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 6806 | trace_btrfs_reserved_extent_free(root, buf->start, buf->len); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6807 | pin = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6808 | } |
| 6809 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6810 | if (pin) |
| 6811 | add_pinned_bytes(root->fs_info, buf->len, |
| 6812 | btrfs_header_level(buf), |
| 6813 | root->root_key.objectid); |
| 6814 | |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 6815 | /* |
| 6816 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 6817 | * anymore. |
| 6818 | */ |
| 6819 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6820 | } |
| 6821 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6822 | /* Can return -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6823 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 6824 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 6825 | u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6826 | { |
| 6827 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6828 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6829 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 6830 | if (btrfs_test_is_dummy_root(root)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6831 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 6832 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6833 | add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid); |
| 6834 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6835 | /* |
| 6836 | * tree log blocks never actually go into the extent allocation |
| 6837 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6838 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6839 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 6840 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6841 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6842 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6843 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6844 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6845 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 6846 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6847 | parent, root_objectid, (int)owner, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 6848 | BTRFS_DROP_DELAYED_REF, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6849 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6850 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 6851 | num_bytes, |
| 6852 | parent, root_objectid, owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 6853 | offset, 0, |
| 6854 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6855 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6856 | return ret; |
| 6857 | } |
| 6858 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6859 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6860 | * when we wait for progress in the block group caching, its because |
| 6861 | * our allocation attempt failed at least once. So, we must sleep |
| 6862 | * and let some progress happen before we try again. |
| 6863 | * |
| 6864 | * This function will sleep at least once waiting for new free space to |
| 6865 | * show up, and then it will check the block group free space numbers |
| 6866 | * for our min num_bytes. Another option is to have it go ahead |
| 6867 | * and look in the rbtree for a free extent of a given size, but this |
| 6868 | * is a good start. |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6869 | * |
| 6870 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 6871 | * any of the information in this block group. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6872 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6873 | static noinline void |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6874 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 6875 | u64 num_bytes) |
| 6876 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6877 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6878 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6879 | caching_ctl = get_caching_control(cache); |
| 6880 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6881 | return; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6882 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6883 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6884 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6885 | |
| 6886 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6887 | } |
| 6888 | |
| 6889 | static noinline int |
| 6890 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 6891 | { |
| 6892 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6893 | int ret = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6894 | |
| 6895 | caching_ctl = get_caching_control(cache); |
| 6896 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6897 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6898 | |
| 6899 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6900 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 6901 | ret = -EIO; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6902 | put_caching_control(caching_ctl); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6903 | return ret; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6904 | } |
| 6905 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 6906 | int __get_raid_index(u64 flags) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6907 | { |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6908 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6909 | return BTRFS_RAID_RAID10; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6910 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6911 | return BTRFS_RAID_RAID1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6912 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6913 | return BTRFS_RAID_DUP; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6914 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6915 | return BTRFS_RAID_RAID0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6916 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6917 | return BTRFS_RAID_RAID5; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6918 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6919 | return BTRFS_RAID_RAID6; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6920 | |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6921 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6922 | } |
| 6923 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 6924 | int get_block_group_index(struct btrfs_block_group_cache *cache) |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6925 | { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 6926 | return __get_raid_index(cache->flags); |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6927 | } |
| 6928 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 6929 | static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = { |
| 6930 | [BTRFS_RAID_RAID10] = "raid10", |
| 6931 | [BTRFS_RAID_RAID1] = "raid1", |
| 6932 | [BTRFS_RAID_DUP] = "dup", |
| 6933 | [BTRFS_RAID_RAID0] = "raid0", |
| 6934 | [BTRFS_RAID_SINGLE] = "single", |
| 6935 | [BTRFS_RAID_RAID5] = "raid5", |
| 6936 | [BTRFS_RAID_RAID6] = "raid6", |
| 6937 | }; |
| 6938 | |
Jeff Mahoney | 1b8e5df | 2013-11-20 16:50:23 -0500 | [diff] [blame] | 6939 | static const char *get_raid_name(enum btrfs_raid_types type) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 6940 | { |
| 6941 | if (type >= BTRFS_NR_RAID_TYPES) |
| 6942 | return NULL; |
| 6943 | |
| 6944 | return btrfs_raid_type_names[type]; |
| 6945 | } |
| 6946 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6947 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6948 | LOOP_CACHING_NOWAIT = 0, |
| 6949 | LOOP_CACHING_WAIT = 1, |
| 6950 | LOOP_ALLOC_CHUNK = 2, |
| 6951 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6952 | }; |
| 6953 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 6954 | static inline void |
| 6955 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, |
| 6956 | int delalloc) |
| 6957 | { |
| 6958 | if (delalloc) |
| 6959 | down_read(&cache->data_rwsem); |
| 6960 | } |
| 6961 | |
| 6962 | static inline void |
| 6963 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, |
| 6964 | int delalloc) |
| 6965 | { |
| 6966 | btrfs_get_block_group(cache); |
| 6967 | if (delalloc) |
| 6968 | down_read(&cache->data_rwsem); |
| 6969 | } |
| 6970 | |
| 6971 | static struct btrfs_block_group_cache * |
| 6972 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, |
| 6973 | struct btrfs_free_cluster *cluster, |
| 6974 | int delalloc) |
| 6975 | { |
| 6976 | struct btrfs_block_group_cache *used_bg; |
| 6977 | bool locked = false; |
| 6978 | again: |
| 6979 | spin_lock(&cluster->refill_lock); |
| 6980 | if (locked) { |
| 6981 | if (used_bg == cluster->block_group) |
| 6982 | return used_bg; |
| 6983 | |
| 6984 | up_read(&used_bg->data_rwsem); |
| 6985 | btrfs_put_block_group(used_bg); |
| 6986 | } |
| 6987 | |
| 6988 | used_bg = cluster->block_group; |
| 6989 | if (!used_bg) |
| 6990 | return NULL; |
| 6991 | |
| 6992 | if (used_bg == block_group) |
| 6993 | return used_bg; |
| 6994 | |
| 6995 | btrfs_get_block_group(used_bg); |
| 6996 | |
| 6997 | if (!delalloc) |
| 6998 | return used_bg; |
| 6999 | |
| 7000 | if (down_read_trylock(&used_bg->data_rwsem)) |
| 7001 | return used_bg; |
| 7002 | |
| 7003 | spin_unlock(&cluster->refill_lock); |
| 7004 | down_read(&used_bg->data_rwsem); |
| 7005 | locked = true; |
| 7006 | goto again; |
| 7007 | } |
| 7008 | |
| 7009 | static inline void |
| 7010 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, |
| 7011 | int delalloc) |
| 7012 | { |
| 7013 | if (delalloc) |
| 7014 | up_read(&cache->data_rwsem); |
| 7015 | btrfs_put_block_group(cache); |
| 7016 | } |
| 7017 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7018 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7019 | * walks the btree of allocated extents and find a hole of a given size. |
| 7020 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7021 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 7022 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7023 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7024 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7025 | * |
| 7026 | * If there is no suitable free space, we will record the max size of |
| 7027 | * the free space extent currently. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7028 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7029 | static noinline int find_free_extent(struct btrfs_root *orig_root, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 7030 | u64 num_bytes, u64 empty_size, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 7031 | u64 hint_byte, struct btrfs_key *ins, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7032 | u64 flags, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7033 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7034 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7035 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7036 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7037 | struct btrfs_block_group_cache *block_group = NULL; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7038 | u64 search_start = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7039 | u64 max_extent_size = 0; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7040 | u64 empty_cluster = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7041 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7042 | int loop = 0; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7043 | int index = __get_raid_index(flags); |
| 7044 | int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ? |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 7045 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7046 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7047 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7048 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7049 | bool have_caching_bg = false; |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7050 | bool orig_have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7051 | bool full_search = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7052 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 7053 | WARN_ON(num_bytes < root->sectorsize); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 7054 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7055 | ins->objectid = 0; |
| 7056 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 7057 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7058 | trace_find_free_extent(orig_root, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7059 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7060 | space_info = __find_space_info(root->fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7061 | if (!space_info) { |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7062 | btrfs_err(root->fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 7063 | return -ENOSPC; |
| 7064 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7065 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7066 | /* |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7067 | * If our free space is heavily fragmented we may not be able to make |
| 7068 | * big contiguous allocations, so instead of doing the expensive search |
| 7069 | * for free space, simply return ENOSPC with our max_extent_size so we |
| 7070 | * can go ahead and search for a more manageable chunk. |
| 7071 | * |
| 7072 | * If our max_extent_size is large enough for our allocation simply |
| 7073 | * disable clustering since we will likely not be able to find enough |
| 7074 | * space to create a cluster and induce latency trying. |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 7075 | */ |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7076 | if (unlikely(space_info->max_extent_size)) { |
| 7077 | spin_lock(&space_info->lock); |
| 7078 | if (space_info->max_extent_size && |
| 7079 | num_bytes > space_info->max_extent_size) { |
| 7080 | ins->offset = space_info->max_extent_size; |
| 7081 | spin_unlock(&space_info->lock); |
| 7082 | return -ENOSPC; |
| 7083 | } else if (space_info->max_extent_size) { |
| 7084 | use_cluster = false; |
| 7085 | } |
| 7086 | spin_unlock(&space_info->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7087 | } |
| 7088 | |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7089 | last_ptr = fetch_cluster_info(orig_root, space_info, &empty_cluster); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7090 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7091 | spin_lock(&last_ptr->lock); |
| 7092 | if (last_ptr->block_group) |
| 7093 | hint_byte = last_ptr->window_start; |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7094 | if (last_ptr->fragmented) { |
| 7095 | /* |
| 7096 | * We still set window_start so we can keep track of the |
| 7097 | * last place we found an allocation to try and save |
| 7098 | * some time. |
| 7099 | */ |
| 7100 | hint_byte = last_ptr->window_start; |
| 7101 | use_cluster = false; |
| 7102 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7103 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7104 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7105 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7106 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 7107 | search_start = max(search_start, hint_byte); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7108 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7109 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 7110 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7111 | /* |
| 7112 | * we don't want to use the block group if it doesn't match our |
| 7113 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7114 | * |
| 7115 | * However if we are re-searching with an ideal block group |
| 7116 | * 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] | 7117 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7118 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7119 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7120 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7121 | if (list_empty(&block_group->list) || |
| 7122 | block_group->ro) { |
| 7123 | /* |
| 7124 | * someone is removing this block group, |
| 7125 | * we can't jump into the have_block_group |
| 7126 | * target because our list pointers are not |
| 7127 | * valid |
| 7128 | */ |
| 7129 | btrfs_put_block_group(block_group); |
| 7130 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7131 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7132 | index = get_block_group_index(block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7133 | btrfs_lock_block_group(block_group, delalloc); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7134 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7135 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7136 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7137 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7138 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7139 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7140 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7141 | have_caching_bg = false; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7142 | if (index == 0 || index == __get_raid_index(flags)) |
| 7143 | full_search = true; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7144 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7145 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 7146 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7147 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7148 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 7149 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7150 | btrfs_grab_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7151 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7152 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7153 | /* |
| 7154 | * this can happen if we end up cycling through all the |
| 7155 | * raid types, but we want to make sure we only allocate |
| 7156 | * for the proper type. |
| 7157 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7158 | if (!block_group_bits(block_group, flags)) { |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7159 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 7160 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7161 | BTRFS_BLOCK_GROUP_RAID5 | |
| 7162 | BTRFS_BLOCK_GROUP_RAID6 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7163 | BTRFS_BLOCK_GROUP_RAID10; |
| 7164 | |
| 7165 | /* |
| 7166 | * if they asked for extra copies and this block group |
| 7167 | * doesn't provide them, bail. This does allow us to |
| 7168 | * fill raid0 from raid1. |
| 7169 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7170 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 7171 | goto loop; |
| 7172 | } |
| 7173 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7174 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 7175 | cached = block_group_cache_done(block_group); |
| 7176 | if (unlikely(!cached)) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7177 | have_caching_bg = true; |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 7178 | ret = cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 7179 | BUG_ON(ret < 0); |
| 7180 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7181 | } |
| 7182 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 7183 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 7184 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7185 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7186 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7187 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7188 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7189 | * Ok we want to try and use the cluster allocator, so |
| 7190 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7191 | */ |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7192 | if (last_ptr && use_cluster) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7193 | struct btrfs_block_group_cache *used_block_group; |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7194 | unsigned long aligned_cluster; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7195 | /* |
| 7196 | * the refill lock keeps out other |
| 7197 | * people trying to start a new cluster |
| 7198 | */ |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7199 | used_block_group = btrfs_lock_cluster(block_group, |
| 7200 | last_ptr, |
| 7201 | delalloc); |
| 7202 | if (!used_block_group) |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7203 | goto refill_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7204 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7205 | if (used_block_group != block_group && |
| 7206 | (used_block_group->ro || |
| 7207 | !block_group_bits(used_block_group, flags))) |
| 7208 | goto release_cluster; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7209 | |
| 7210 | offset = btrfs_alloc_from_cluster(used_block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7211 | last_ptr, |
| 7212 | num_bytes, |
| 7213 | used_block_group->key.objectid, |
| 7214 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7215 | if (offset) { |
| 7216 | /* we have a block, we're done */ |
| 7217 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7218 | trace_btrfs_reserve_extent_cluster(root, |
Miao Xie | 89d4346 | 2014-01-15 20:00:57 +0800 | [diff] [blame] | 7219 | used_block_group, |
| 7220 | search_start, num_bytes); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7221 | if (used_block_group != block_group) { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7222 | btrfs_release_block_group(block_group, |
| 7223 | delalloc); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7224 | block_group = used_block_group; |
| 7225 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7226 | goto checks; |
| 7227 | } |
| 7228 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 7229 | WARN_ON(last_ptr->block_group != used_block_group); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7230 | release_cluster: |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7231 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 7232 | * set up a new clusters, so lets just skip it |
| 7233 | * and let the allocator find whatever block |
| 7234 | * it can find. If we reach this point, we |
| 7235 | * will have tried the cluster allocator |
| 7236 | * plenty of times and not have found |
| 7237 | * anything, so we are likely way too |
| 7238 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7239 | * anything. |
| 7240 | * |
| 7241 | * However, if the cluster is taken from the |
| 7242 | * current block group, release the cluster |
| 7243 | * first, so that we stand a better chance of |
| 7244 | * succeeding in the unclustered |
| 7245 | * allocation. */ |
| 7246 | if (loop >= LOOP_NO_EMPTY_SIZE && |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7247 | used_block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7248 | spin_unlock(&last_ptr->refill_lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7249 | btrfs_release_block_group(used_block_group, |
| 7250 | delalloc); |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7251 | goto unclustered_alloc; |
| 7252 | } |
| 7253 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7254 | /* |
| 7255 | * this cluster didn't work out, free it and |
| 7256 | * start over |
| 7257 | */ |
| 7258 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 7259 | |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7260 | if (used_block_group != block_group) |
| 7261 | btrfs_release_block_group(used_block_group, |
| 7262 | delalloc); |
| 7263 | refill_cluster: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7264 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 7265 | spin_unlock(&last_ptr->refill_lock); |
| 7266 | goto unclustered_alloc; |
| 7267 | } |
| 7268 | |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 7269 | aligned_cluster = max_t(unsigned long, |
| 7270 | empty_cluster + empty_size, |
| 7271 | block_group->full_stripe_len); |
| 7272 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7273 | /* allocate a cluster in this block group */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7274 | ret = btrfs_find_space_cluster(root, block_group, |
| 7275 | last_ptr, search_start, |
| 7276 | num_bytes, |
| 7277 | aligned_cluster); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7278 | if (ret == 0) { |
| 7279 | /* |
| 7280 | * now pull our allocation out of this |
| 7281 | * cluster |
| 7282 | */ |
| 7283 | offset = btrfs_alloc_from_cluster(block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7284 | last_ptr, |
| 7285 | num_bytes, |
| 7286 | search_start, |
| 7287 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7288 | if (offset) { |
| 7289 | /* we found one, proceed */ |
| 7290 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7291 | trace_btrfs_reserve_extent_cluster(root, |
| 7292 | block_group, search_start, |
| 7293 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7294 | goto checks; |
| 7295 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7296 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 7297 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7298 | spin_unlock(&last_ptr->refill_lock); |
| 7299 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7300 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7301 | wait_block_group_cache_progress(block_group, |
| 7302 | num_bytes + empty_cluster + empty_size); |
| 7303 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7304 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7305 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7306 | /* |
| 7307 | * at this point we either didn't find a cluster |
| 7308 | * or we weren't able to allocate a block from our |
| 7309 | * cluster. Free the cluster we've been trying |
| 7310 | * to use, and go to the next block group |
| 7311 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7312 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7313 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7314 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7315 | } |
| 7316 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 7317 | unclustered_alloc: |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7318 | /* |
| 7319 | * We are doing an unclustered alloc, set the fragmented flag so |
| 7320 | * we don't bother trying to setup a cluster again until we get |
| 7321 | * more space. |
| 7322 | */ |
| 7323 | if (unlikely(last_ptr)) { |
| 7324 | spin_lock(&last_ptr->lock); |
| 7325 | last_ptr->fragmented = 1; |
| 7326 | spin_unlock(&last_ptr->lock); |
| 7327 | } |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7328 | spin_lock(&block_group->free_space_ctl->tree_lock); |
| 7329 | if (cached && |
| 7330 | block_group->free_space_ctl->free_space < |
| 7331 | num_bytes + empty_cluster + empty_size) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7332 | if (block_group->free_space_ctl->free_space > |
| 7333 | max_extent_size) |
| 7334 | max_extent_size = |
| 7335 | block_group->free_space_ctl->free_space; |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 7336 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 7337 | goto loop; |
| 7338 | } |
| 7339 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 7340 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7341 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7342 | num_bytes, empty_size, |
| 7343 | &max_extent_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7344 | /* |
| 7345 | * If we didn't find a chunk, and we haven't failed on this |
| 7346 | * block group before, and this block group is in the middle of |
| 7347 | * caching and we are ok with waiting, then go ahead and wait |
| 7348 | * for progress to be made, and set failed_alloc to true. |
| 7349 | * |
| 7350 | * If failed_alloc is true then we've already waited on this |
| 7351 | * block group once and should move on to the next block group. |
| 7352 | */ |
| 7353 | if (!offset && !failed_alloc && !cached && |
| 7354 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7355 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7356 | num_bytes + empty_size); |
| 7357 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7358 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7359 | } else if (!offset) { |
| 7360 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7361 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7362 | checks: |
David Sterba | 4e54b17 | 2014-06-05 01:39:19 +0200 | [diff] [blame] | 7363 | search_start = ALIGN(offset, root->stripesize); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 7364 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7365 | /* move on to the next group */ |
| 7366 | if (search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7367 | block_group->key.objectid + block_group->key.offset) { |
| 7368 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7369 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7370 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7371 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7372 | if (offset < search_start) |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7373 | btrfs_add_free_space(block_group, offset, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7374 | search_start - offset); |
| 7375 | BUG_ON(offset > search_start); |
| 7376 | |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7377 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7378 | alloc_type, delalloc); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7379 | if (ret == -EAGAIN) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 7380 | btrfs_add_free_space(block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7381 | goto loop; |
| 7382 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7383 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7384 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7385 | ins->objectid = search_start; |
| 7386 | ins->offset = num_bytes; |
| 7387 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 7388 | trace_btrfs_reserve_extent(orig_root, block_group, |
| 7389 | search_start, num_bytes); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7390 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7391 | break; |
| 7392 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7393 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 7394 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7395 | BUG_ON(index != get_block_group_index(block_group)); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7396 | btrfs_release_block_group(block_group, delalloc); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7397 | } |
| 7398 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 7399 | |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7400 | if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg |
| 7401 | && !orig_have_caching_bg) |
| 7402 | orig_have_caching_bg = true; |
| 7403 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 7404 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 7405 | goto search; |
| 7406 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7407 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 7408 | goto search; |
| 7409 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 7410 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 7411 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 7412 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7413 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 7414 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 7415 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 7416 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7417 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7418 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7419 | index = 0; |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7420 | if (loop == LOOP_CACHING_NOWAIT) { |
| 7421 | /* |
| 7422 | * We want to skip the LOOP_CACHING_WAIT step if we |
| 7423 | * don't have any unached bgs and we've alrelady done a |
| 7424 | * full search through. |
| 7425 | */ |
chandan | 13a0db5 | 2015-11-02 13:59:46 +0530 | [diff] [blame] | 7426 | if (orig_have_caching_bg || !full_search) |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7427 | loop = LOOP_CACHING_WAIT; |
| 7428 | else |
| 7429 | loop = LOOP_ALLOC_CHUNK; |
| 7430 | } else { |
| 7431 | loop++; |
| 7432 | } |
| 7433 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7434 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7435 | struct btrfs_trans_handle *trans; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7436 | int exist = 0; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7437 | |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7438 | trans = current->journal_info; |
| 7439 | if (trans) |
| 7440 | exist = 1; |
| 7441 | else |
| 7442 | trans = btrfs_join_transaction(root); |
| 7443 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7444 | if (IS_ERR(trans)) { |
| 7445 | ret = PTR_ERR(trans); |
| 7446 | goto out; |
| 7447 | } |
| 7448 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7449 | ret = do_chunk_alloc(trans, root, flags, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7450 | CHUNK_ALLOC_FORCE); |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7451 | |
| 7452 | /* |
| 7453 | * If we can't allocate a new chunk we've already looped |
| 7454 | * through at least once, move on to the NO_EMPTY_SIZE |
| 7455 | * case. |
| 7456 | */ |
| 7457 | if (ret == -ENOSPC) |
| 7458 | loop = LOOP_NO_EMPTY_SIZE; |
| 7459 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7460 | /* |
| 7461 | * Do not bail out on ENOSPC since we |
| 7462 | * can do more things. |
| 7463 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7464 | if (ret < 0 && ret != -ENOSPC) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7465 | btrfs_abort_transaction(trans, |
| 7466 | root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7467 | else |
| 7468 | ret = 0; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 7469 | if (!exist) |
| 7470 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7471 | if (ret) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7472 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7473 | } |
| 7474 | |
| 7475 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Josef Bacik | a5e681d | 2015-10-01 14:54:10 -0400 | [diff] [blame] | 7476 | /* |
| 7477 | * Don't loop again if we already have no empty_size and |
| 7478 | * no empty_cluster. |
| 7479 | */ |
| 7480 | if (empty_size == 0 && |
| 7481 | empty_cluster == 0) { |
| 7482 | ret = -ENOSPC; |
| 7483 | goto out; |
| 7484 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7485 | empty_size = 0; |
| 7486 | empty_cluster = 0; |
| 7487 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 7488 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 7489 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7490 | } else if (!ins->objectid) { |
| 7491 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 7492 | } else if (ins->objectid) { |
Josef Bacik | c759c4e | 2015-10-02 15:25:10 -0400 | [diff] [blame] | 7493 | if (!use_cluster && last_ptr) { |
| 7494 | spin_lock(&last_ptr->lock); |
| 7495 | last_ptr->window_start = ins->objectid; |
| 7496 | spin_unlock(&last_ptr->lock); |
| 7497 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 7498 | ret = 0; |
| 7499 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7500 | out: |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7501 | if (ret == -ENOSPC) { |
| 7502 | spin_lock(&space_info->lock); |
| 7503 | space_info->max_extent_size = max_extent_size; |
| 7504 | spin_unlock(&space_info->lock); |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7505 | ins->offset = max_extent_size; |
Josef Bacik | 4f4db21 | 2015-09-29 11:40:47 -0400 | [diff] [blame] | 7506 | } |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 7507 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7508 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7509 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7510 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 7511 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7512 | { |
| 7513 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7514 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7515 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7516 | spin_lock(&info->lock); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7517 | printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7518 | info->flags, |
| 7519 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 7520 | info->bytes_reserved - info->bytes_readonly, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7521 | (info->full) ? "" : "not "); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7522 | printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, " |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7523 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7524 | info->total_bytes, info->bytes_used, info->bytes_pinned, |
| 7525 | info->bytes_reserved, info->bytes_may_use, |
| 7526 | info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 7527 | spin_unlock(&info->lock); |
| 7528 | |
| 7529 | if (!dump_block_groups) |
| 7530 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7531 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7532 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7533 | again: |
| 7534 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7535 | spin_lock(&cache->lock); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7536 | printk(KERN_INFO "BTRFS: " |
| 7537 | "block group %llu has %llu bytes, " |
| 7538 | "%llu used %llu pinned %llu reserved %s\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7539 | cache->key.objectid, cache->key.offset, |
| 7540 | btrfs_block_group_used(&cache->item), cache->pinned, |
| 7541 | cache->reserved, cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7542 | btrfs_dump_free_space(cache, bytes); |
| 7543 | spin_unlock(&cache->lock); |
| 7544 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7545 | if (++index < BTRFS_NR_RAID_TYPES) |
| 7546 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7547 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7548 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 7549 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7550 | int btrfs_reserve_extent(struct btrfs_root *root, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7551 | u64 num_bytes, u64 min_alloc_size, |
| 7552 | u64 empty_size, u64 hint_byte, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7553 | struct btrfs_key *ins, int is_data, int delalloc) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7554 | { |
Josef Bacik | 36af4e0 | 2015-09-25 16:13:11 -0400 | [diff] [blame] | 7555 | bool final_tried = num_bytes == min_alloc_size; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7556 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7557 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7558 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7559 | flags = btrfs_get_alloc_profile(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 7560 | again: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 7561 | WARN_ON(num_bytes < root->sectorsize); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7562 | ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7563 | flags, delalloc); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 7564 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7565 | if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 7566 | if (!final_tried && ins->offset) { |
| 7567 | num_bytes = min(num_bytes >> 1, ins->offset); |
Zach Brown | 24542bf | 2012-11-16 00:04:43 +0000 | [diff] [blame] | 7568 | num_bytes = round_down(num_bytes, root->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7569 | num_bytes = max(num_bytes, min_alloc_size); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7570 | if (num_bytes == min_alloc_size) |
| 7571 | final_tried = true; |
| 7572 | goto again; |
| 7573 | } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 7574 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7575 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 7576 | sinfo = __find_space_info(root->fs_info, flags); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7577 | btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7578 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 7579 | if (sinfo) |
| 7580 | dump_space_info(sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 7581 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7582 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7583 | |
| 7584 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7585 | } |
| 7586 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7587 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7588 | u64 start, u64 len, |
| 7589 | int pin, int delalloc) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7590 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7591 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7592 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7593 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7594 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 7595 | if (!cache) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7596 | btrfs_err(root->fs_info, "Unable to find block group for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7597 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7598 | return -ENOSPC; |
| 7599 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 7600 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7601 | if (pin) |
| 7602 | pin_down_extent(root, cache, start, len, 1); |
| 7603 | else { |
Filipe Manana | dcc82f4 | 2015-03-23 14:07:40 +0000 | [diff] [blame] | 7604 | if (btrfs_test_opt(root, DISCARD)) |
| 7605 | ret = btrfs_discard_extent(root, start, len, NULL); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7606 | btrfs_add_free_space(cache, start, len); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7607 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7608 | } |
Dongsheng Yang | 3119321 | 2014-12-12 16:44:35 +0800 | [diff] [blame] | 7609 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7610 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7611 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 7612 | trace_btrfs_reserved_extent_free(root, start, len); |
| 7613 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7614 | return ret; |
| 7615 | } |
| 7616 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7617 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7618 | u64 start, u64 len, int delalloc) |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7619 | { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7620 | return __btrfs_free_reserved_extent(root, start, len, 0, delalloc); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7621 | } |
| 7622 | |
| 7623 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, |
| 7624 | u64 start, u64 len) |
| 7625 | { |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7626 | return __btrfs_free_reserved_extent(root, start, len, 1, 0); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 7627 | } |
| 7628 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7629 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 7630 | struct btrfs_root *root, |
| 7631 | u64 parent, u64 root_objectid, |
| 7632 | u64 flags, u64 owner, u64 offset, |
| 7633 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7634 | { |
| 7635 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7636 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7637 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7638 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7639 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7640 | struct extent_buffer *leaf; |
| 7641 | int type; |
| 7642 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 7643 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7644 | if (parent > 0) |
| 7645 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 7646 | else |
| 7647 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 7648 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7649 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 7650 | |
| 7651 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7652 | if (!path) |
| 7653 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 7654 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7655 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7656 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 7657 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7658 | if (ret) { |
| 7659 | btrfs_free_path(path); |
| 7660 | return ret; |
| 7661 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7662 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7663 | leaf = path->nodes[0]; |
| 7664 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 7665 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7666 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 7667 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 7668 | btrfs_set_extent_flags(leaf, extent_item, |
| 7669 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 7670 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7671 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 7672 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 7673 | if (parent > 0) { |
| 7674 | struct btrfs_shared_data_ref *ref; |
| 7675 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 7676 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 7677 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 7678 | } else { |
| 7679 | struct btrfs_extent_data_ref *ref; |
| 7680 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 7681 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 7682 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 7683 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 7684 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 7685 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 7686 | |
| 7687 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 7688 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 7689 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 7690 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7691 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7692 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7693 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 7694 | BUG(); |
| 7695 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 7696 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7697 | return ret; |
| 7698 | } |
| 7699 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7700 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 7701 | struct btrfs_root *root, |
| 7702 | u64 parent, u64 root_objectid, |
| 7703 | u64 flags, struct btrfs_disk_key *key, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 7704 | int level, struct btrfs_key *ins) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7705 | { |
| 7706 | int ret; |
| 7707 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 7708 | struct btrfs_extent_item *extent_item; |
| 7709 | struct btrfs_tree_block_info *block_info; |
| 7710 | struct btrfs_extent_inline_ref *iref; |
| 7711 | struct btrfs_path *path; |
| 7712 | struct extent_buffer *leaf; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7713 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7714 | u64 num_bytes = ins->offset; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7715 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 7716 | SKINNY_METADATA); |
| 7717 | |
| 7718 | if (!skinny_metadata) |
| 7719 | size += sizeof(*block_info); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7720 | |
| 7721 | path = btrfs_alloc_path(); |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 7722 | if (!path) { |
| 7723 | btrfs_free_and_pin_reserved_extent(root, ins->objectid, |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 7724 | root->nodesize); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7725 | return -ENOMEM; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 7726 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7727 | |
| 7728 | path->leave_spinning = 1; |
| 7729 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 7730 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7731 | if (ret) { |
Chris Mason | dd82525 | 2015-04-01 08:36:05 -0700 | [diff] [blame] | 7732 | btrfs_free_path(path); |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 7733 | btrfs_free_and_pin_reserved_extent(root, ins->objectid, |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 7734 | root->nodesize); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7735 | return ret; |
| 7736 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7737 | |
| 7738 | leaf = path->nodes[0]; |
| 7739 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 7740 | struct btrfs_extent_item); |
| 7741 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 7742 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 7743 | btrfs_set_extent_flags(leaf, extent_item, |
| 7744 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7745 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7746 | if (skinny_metadata) { |
| 7747 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 7748 | num_bytes = root->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7749 | } else { |
| 7750 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 7751 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 7752 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 7753 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 7754 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7755 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7756 | if (parent > 0) { |
| 7757 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 7758 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 7759 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 7760 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 7761 | } else { |
| 7762 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 7763 | BTRFS_TREE_BLOCK_REF_KEY); |
| 7764 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 7765 | } |
| 7766 | |
| 7767 | btrfs_mark_buffer_dirty(leaf); |
| 7768 | btrfs_free_path(path); |
| 7769 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 7770 | ret = update_block_group(trans, root, ins->objectid, root->nodesize, |
| 7771 | 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7772 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7773 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7774 | ins->objectid, ins->offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7775 | BUG(); |
| 7776 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 7777 | |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 7778 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->nodesize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7779 | return ret; |
| 7780 | } |
| 7781 | |
| 7782 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 7783 | struct btrfs_root *root, |
| 7784 | u64 root_objectid, u64 owner, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 7785 | u64 offset, u64 ram_bytes, |
| 7786 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7787 | { |
| 7788 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 7789 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7790 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7791 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7792 | ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, |
| 7793 | ins->offset, 0, |
| 7794 | root_objectid, owner, offset, |
Qu Wenruo | 5846a3c | 2015-10-26 14:11:18 +0800 | [diff] [blame] | 7795 | ram_bytes, BTRFS_ADD_DELAYED_EXTENT, |
| 7796 | NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7797 | return ret; |
| 7798 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7799 | |
| 7800 | /* |
| 7801 | * this is used by the tree logging recovery code. It records that |
| 7802 | * an extent has been allocated and makes sure to clear the free |
| 7803 | * space cache bits as well |
| 7804 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7805 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 7806 | struct btrfs_root *root, |
| 7807 | u64 root_objectid, u64 owner, u64 offset, |
| 7808 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7809 | { |
| 7810 | int ret; |
| 7811 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 7812 | |
| 7813 | /* |
| 7814 | * Mixed block groups will exclude before processing the log so we only |
| 7815 | * need to do the exlude dance if this fs isn't mixed. |
| 7816 | */ |
| 7817 | if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) { |
| 7818 | ret = __exclude_logged_extent(root, ins->objectid, ins->offset); |
| 7819 | if (ret) |
| 7820 | return ret; |
| 7821 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7822 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7823 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 7824 | if (!block_group) |
| 7825 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7826 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 7827 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7828 | RESERVE_ALLOC_NO_ACCOUNT, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7829 | BUG_ON(ret); /* logic error */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7830 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 7831 | 0, owner, offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 7832 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7833 | return ret; |
| 7834 | } |
| 7835 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7836 | static struct extent_buffer * |
| 7837 | 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] | 7838 | u64 bytenr, int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7839 | { |
| 7840 | struct extent_buffer *buf; |
| 7841 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 7842 | buf = btrfs_find_create_tree_block(root, bytenr); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7843 | if (!buf) |
| 7844 | return ERR_PTR(-ENOMEM); |
| 7845 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 7846 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7847 | btrfs_tree_lock(buf); |
Daniel Dressler | 01d5847 | 2014-11-21 17:15:07 +0900 | [diff] [blame] | 7848 | clean_tree_block(trans, root->fs_info, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7849 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 7850 | |
| 7851 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7852 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 7853 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 7854 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 7855 | buf->log_index = root->log_transid % 2; |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 7856 | /* |
| 7857 | * we allow two log transactions at a time, use different |
| 7858 | * EXENT bit to differentiate dirty pages. |
| 7859 | */ |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 7860 | if (buf->log_index == 0) |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 7861 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 7862 | buf->start + buf->len - 1, GFP_NOFS); |
| 7863 | else |
| 7864 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 7865 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 7866 | } else { |
Filipe Manana | 656f30d | 2014-09-26 12:25:56 +0100 | [diff] [blame] | 7867 | buf->log_index = -1; |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 7868 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 7869 | buf->start + buf->len - 1, GFP_NOFS); |
| 7870 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7871 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 7872 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7873 | return buf; |
| 7874 | } |
| 7875 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7876 | static struct btrfs_block_rsv * |
| 7877 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 7878 | struct btrfs_root *root, u32 blocksize) |
| 7879 | { |
| 7880 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 7881 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7882 | int ret; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 7883 | bool global_updated = false; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7884 | |
| 7885 | block_rsv = get_block_rsv(trans, root); |
| 7886 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7887 | if (unlikely(block_rsv->size == 0)) |
| 7888 | goto try_reserve; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 7889 | again: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7890 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 7891 | if (!ret) |
| 7892 | return block_rsv; |
| 7893 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7894 | if (block_rsv->failfast) |
| 7895 | return ERR_PTR(ret); |
| 7896 | |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 7897 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
| 7898 | global_updated = true; |
| 7899 | update_global_block_rsv(root->fs_info); |
| 7900 | goto again; |
| 7901 | } |
| 7902 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7903 | if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 7904 | static DEFINE_RATELIMIT_STATE(_rs, |
| 7905 | DEFAULT_RATELIMIT_INTERVAL * 10, |
| 7906 | /*DEFAULT_RATELIMIT_BURST*/ 1); |
| 7907 | if (__ratelimit(&_rs)) |
| 7908 | WARN(1, KERN_DEBUG |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7909 | "BTRFS: block rsv returned %d\n", ret); |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7910 | } |
| 7911 | try_reserve: |
| 7912 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, |
| 7913 | BTRFS_RESERVE_NO_FLUSH); |
| 7914 | if (!ret) |
| 7915 | return block_rsv; |
| 7916 | /* |
| 7917 | * If we couldn't reserve metadata bytes try and use some from |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 7918 | * the global reserve if its space type is the same as the global |
| 7919 | * reservation. |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7920 | */ |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 7921 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
| 7922 | block_rsv->space_info == global_rsv->space_info) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7923 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 7924 | if (!ret) |
| 7925 | return global_rsv; |
| 7926 | } |
| 7927 | return ERR_PTR(ret); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7928 | } |
| 7929 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 7930 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 7931 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7932 | { |
| 7933 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 7934 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7935 | } |
| 7936 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7937 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7938 | * 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] | 7939 | * returns the tree buffer or an ERR_PTR on error. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7940 | */ |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 7941 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
| 7942 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7943 | u64 parent, u64 root_objectid, |
| 7944 | struct btrfs_disk_key *key, int level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7945 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7946 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 7947 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7948 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7949 | struct extent_buffer *buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 7950 | struct btrfs_delayed_extent_op *extent_op; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7951 | u64 flags = 0; |
| 7952 | int ret; |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 7953 | u32 blocksize = root->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7954 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 7955 | SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7956 | |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 7957 | if (btrfs_test_is_dummy_root(root)) { |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7958 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 7959 | level); |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7960 | if (!IS_ERR(buf)) |
| 7961 | root->alloc_bytenr += blocksize; |
| 7962 | return buf; |
| 7963 | } |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 7964 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7965 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 7966 | if (IS_ERR(block_rsv)) |
| 7967 | return ERR_CAST(block_rsv); |
| 7968 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7969 | ret = btrfs_reserve_extent(root, blocksize, blocksize, |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 7970 | empty_size, hint, &ins, 0, 0); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 7971 | if (ret) |
| 7972 | goto out_unuse; |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 7973 | |
David Sterba | fe86457 | 2014-06-15 02:28:42 +0200 | [diff] [blame] | 7974 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 7975 | if (IS_ERR(buf)) { |
| 7976 | ret = PTR_ERR(buf); |
| 7977 | goto out_free_reserved; |
| 7978 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7979 | |
| 7980 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 7981 | if (parent == 0) |
| 7982 | parent = ins.objectid; |
| 7983 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7984 | } else |
| 7985 | BUG_ON(parent > 0); |
| 7986 | |
| 7987 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 7988 | extent_op = btrfs_alloc_delayed_extent_op(); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 7989 | if (!extent_op) { |
| 7990 | ret = -ENOMEM; |
| 7991 | goto out_free_buf; |
| 7992 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7993 | if (key) |
| 7994 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 7995 | else |
| 7996 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 7997 | extent_op->flags_to_set = flags; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7998 | if (skinny_metadata) |
| 7999 | extent_op->update_key = 0; |
| 8000 | else |
| 8001 | extent_op->update_key = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8002 | extent_op->update_flags = 1; |
| 8003 | extent_op->is_data = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8004 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8005 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8006 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8007 | ins.objectid, ins.offset, |
| 8008 | parent, root_objectid, level, |
| 8009 | BTRFS_ADD_DELAYED_EXTENT, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8010 | extent_op); |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8011 | if (ret) |
| 8012 | goto out_free_delayed; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8013 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8014 | return buf; |
Omar Sandoval | 67b7859 | 2015-02-24 02:47:04 -0800 | [diff] [blame] | 8015 | |
| 8016 | out_free_delayed: |
| 8017 | btrfs_free_delayed_extent_op(extent_op); |
| 8018 | out_free_buf: |
| 8019 | free_extent_buffer(buf); |
| 8020 | out_free_reserved: |
| 8021 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0); |
| 8022 | out_unuse: |
| 8023 | unuse_block_rsv(root->fs_info, block_rsv, blocksize); |
| 8024 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 8025 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 8026 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8027 | struct walk_control { |
| 8028 | u64 refs[BTRFS_MAX_LEVEL]; |
| 8029 | u64 flags[BTRFS_MAX_LEVEL]; |
| 8030 | struct btrfs_key update_progress; |
| 8031 | int stage; |
| 8032 | int level; |
| 8033 | int shared_level; |
| 8034 | int update_ref; |
| 8035 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8036 | int reada_slot; |
| 8037 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8038 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8039 | }; |
| 8040 | |
| 8041 | #define DROP_REFERENCE 1 |
| 8042 | #define UPDATE_BACKREF 2 |
| 8043 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8044 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 8045 | struct btrfs_root *root, |
| 8046 | struct walk_control *wc, |
| 8047 | struct btrfs_path *path) |
| 8048 | { |
| 8049 | u64 bytenr; |
| 8050 | u64 generation; |
| 8051 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8052 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8053 | u32 nritems; |
| 8054 | u32 blocksize; |
| 8055 | struct btrfs_key key; |
| 8056 | struct extent_buffer *eb; |
| 8057 | int ret; |
| 8058 | int slot; |
| 8059 | int nread = 0; |
| 8060 | |
| 8061 | if (path->slots[wc->level] < wc->reada_slot) { |
| 8062 | wc->reada_count = wc->reada_count * 2 / 3; |
| 8063 | wc->reada_count = max(wc->reada_count, 2); |
| 8064 | } else { |
| 8065 | wc->reada_count = wc->reada_count * 3 / 2; |
| 8066 | wc->reada_count = min_t(int, wc->reada_count, |
| 8067 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 8068 | } |
| 8069 | |
| 8070 | eb = path->nodes[wc->level]; |
| 8071 | nritems = btrfs_header_nritems(eb); |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 8072 | blocksize = root->nodesize; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8073 | |
| 8074 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 8075 | if (nread >= wc->reada_count) |
| 8076 | break; |
| 8077 | |
| 8078 | cond_resched(); |
| 8079 | bytenr = btrfs_node_blockptr(eb, slot); |
| 8080 | generation = btrfs_node_ptr_generation(eb, slot); |
| 8081 | |
| 8082 | if (slot == path->slots[wc->level]) |
| 8083 | goto reada; |
| 8084 | |
| 8085 | if (wc->stage == UPDATE_BACKREF && |
| 8086 | generation <= root->root_key.offset) |
| 8087 | continue; |
| 8088 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8089 | /* We don't lock the tree block, it's OK to be racy here */ |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8090 | ret = btrfs_lookup_extent_info(trans, root, bytenr, |
| 8091 | wc->level - 1, 1, &refs, |
| 8092 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8093 | /* We don't care about errors in readahead. */ |
| 8094 | if (ret < 0) |
| 8095 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8096 | BUG_ON(refs == 0); |
| 8097 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8098 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8099 | if (refs == 1) |
| 8100 | goto reada; |
| 8101 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8102 | if (wc->level == 1 && |
| 8103 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8104 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8105 | if (!wc->update_ref || |
| 8106 | generation <= root->root_key.offset) |
| 8107 | continue; |
| 8108 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 8109 | ret = btrfs_comp_cpu_keys(&key, |
| 8110 | &wc->update_progress); |
| 8111 | if (ret < 0) |
| 8112 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8113 | } else { |
| 8114 | if (wc->level == 1 && |
| 8115 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8116 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8117 | } |
| 8118 | reada: |
David Sterba | d3e46fe | 2014-06-15 02:04:19 +0200 | [diff] [blame] | 8119 | readahead_tree_block(root, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8120 | nread++; |
| 8121 | } |
| 8122 | wc->reada_slot = slot; |
| 8123 | } |
| 8124 | |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 8125 | /* |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 8126 | * These may not be seen by the usual inc/dec ref code so we have to |
| 8127 | * add them here. |
Qu Wenruo | 0ed4792 | 2015-04-16 16:55:08 +0800 | [diff] [blame] | 8128 | */ |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 8129 | static int record_one_subtree_extent(struct btrfs_trans_handle *trans, |
| 8130 | struct btrfs_root *root, u64 bytenr, |
| 8131 | u64 num_bytes) |
| 8132 | { |
| 8133 | struct btrfs_qgroup_extent_record *qrecord; |
| 8134 | struct btrfs_delayed_ref_root *delayed_refs; |
| 8135 | |
| 8136 | qrecord = kmalloc(sizeof(*qrecord), GFP_NOFS); |
| 8137 | if (!qrecord) |
| 8138 | return -ENOMEM; |
| 8139 | |
| 8140 | qrecord->bytenr = bytenr; |
| 8141 | qrecord->num_bytes = num_bytes; |
| 8142 | qrecord->old_roots = NULL; |
| 8143 | |
| 8144 | delayed_refs = &trans->transaction->delayed_refs; |
| 8145 | spin_lock(&delayed_refs->lock); |
| 8146 | if (btrfs_qgroup_insert_dirty_extent(delayed_refs, qrecord)) |
| 8147 | kfree(qrecord); |
| 8148 | spin_unlock(&delayed_refs->lock); |
| 8149 | |
| 8150 | return 0; |
| 8151 | } |
| 8152 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8153 | static int account_leaf_items(struct btrfs_trans_handle *trans, |
| 8154 | struct btrfs_root *root, |
| 8155 | struct extent_buffer *eb) |
| 8156 | { |
| 8157 | int nr = btrfs_header_nritems(eb); |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 8158 | int i, extent_type, ret; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8159 | struct btrfs_key key; |
| 8160 | struct btrfs_file_extent_item *fi; |
| 8161 | u64 bytenr, num_bytes; |
| 8162 | |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 8163 | /* We can be called directly from walk_up_proc() */ |
| 8164 | if (!root->fs_info->quota_enabled) |
| 8165 | return 0; |
| 8166 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8167 | for (i = 0; i < nr; i++) { |
| 8168 | btrfs_item_key_to_cpu(eb, &key, i); |
| 8169 | |
| 8170 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 8171 | continue; |
| 8172 | |
| 8173 | fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 8174 | /* filter out non qgroup-accountable extents */ |
| 8175 | extent_type = btrfs_file_extent_type(eb, fi); |
| 8176 | |
| 8177 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) |
| 8178 | continue; |
| 8179 | |
| 8180 | bytenr = btrfs_file_extent_disk_bytenr(eb, fi); |
| 8181 | if (!bytenr) |
| 8182 | continue; |
| 8183 | |
| 8184 | num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi); |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 8185 | |
| 8186 | ret = record_one_subtree_extent(trans, root, bytenr, num_bytes); |
| 8187 | if (ret) |
| 8188 | return ret; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8189 | } |
| 8190 | return 0; |
| 8191 | } |
| 8192 | |
| 8193 | /* |
| 8194 | * Walk up the tree from the bottom, freeing leaves and any interior |
| 8195 | * nodes which have had all slots visited. If a node (leaf or |
| 8196 | * interior) is freed, the node above it will have it's slot |
| 8197 | * incremented. The root node will never be freed. |
| 8198 | * |
| 8199 | * At the end of this function, we should have a path which has all |
| 8200 | * slots incremented to the next position for a search. If we need to |
| 8201 | * read a new node it will be NULL and the node above it will have the |
| 8202 | * correct slot selected for a later read. |
| 8203 | * |
| 8204 | * If we increment the root nodes slot counter past the number of |
| 8205 | * elements, 1 is returned to signal completion of the search. |
| 8206 | */ |
| 8207 | static int adjust_slots_upwards(struct btrfs_root *root, |
| 8208 | struct btrfs_path *path, int root_level) |
| 8209 | { |
| 8210 | int level = 0; |
| 8211 | int nr, slot; |
| 8212 | struct extent_buffer *eb; |
| 8213 | |
| 8214 | if (root_level == 0) |
| 8215 | return 1; |
| 8216 | |
| 8217 | while (level <= root_level) { |
| 8218 | eb = path->nodes[level]; |
| 8219 | nr = btrfs_header_nritems(eb); |
| 8220 | path->slots[level]++; |
| 8221 | slot = path->slots[level]; |
| 8222 | if (slot >= nr || level == 0) { |
| 8223 | /* |
| 8224 | * Don't free the root - we will detect this |
| 8225 | * condition after our loop and return a |
| 8226 | * positive value for caller to stop walking the tree. |
| 8227 | */ |
| 8228 | if (level != root_level) { |
| 8229 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
| 8230 | path->locks[level] = 0; |
| 8231 | |
| 8232 | free_extent_buffer(eb); |
| 8233 | path->nodes[level] = NULL; |
| 8234 | path->slots[level] = 0; |
| 8235 | } |
| 8236 | } else { |
| 8237 | /* |
| 8238 | * We have a valid slot to walk back down |
| 8239 | * from. Stop here so caller can process these |
| 8240 | * new nodes. |
| 8241 | */ |
| 8242 | break; |
| 8243 | } |
| 8244 | |
| 8245 | level++; |
| 8246 | } |
| 8247 | |
| 8248 | eb = path->nodes[root_level]; |
| 8249 | if (path->slots[root_level] >= btrfs_header_nritems(eb)) |
| 8250 | return 1; |
| 8251 | |
| 8252 | return 0; |
| 8253 | } |
| 8254 | |
| 8255 | /* |
| 8256 | * root_eb is the subtree root and is locked before this function is called. |
| 8257 | */ |
| 8258 | static int account_shared_subtree(struct btrfs_trans_handle *trans, |
| 8259 | struct btrfs_root *root, |
| 8260 | struct extent_buffer *root_eb, |
| 8261 | u64 root_gen, |
| 8262 | int root_level) |
| 8263 | { |
| 8264 | int ret = 0; |
| 8265 | int level; |
| 8266 | struct extent_buffer *eb = root_eb; |
| 8267 | struct btrfs_path *path = NULL; |
| 8268 | |
| 8269 | BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL); |
| 8270 | BUG_ON(root_eb == NULL); |
| 8271 | |
| 8272 | if (!root->fs_info->quota_enabled) |
| 8273 | return 0; |
| 8274 | |
| 8275 | if (!extent_buffer_uptodate(root_eb)) { |
| 8276 | ret = btrfs_read_buffer(root_eb, root_gen); |
| 8277 | if (ret) |
| 8278 | goto out; |
| 8279 | } |
| 8280 | |
| 8281 | if (root_level == 0) { |
| 8282 | ret = account_leaf_items(trans, root, root_eb); |
| 8283 | goto out; |
| 8284 | } |
| 8285 | |
| 8286 | path = btrfs_alloc_path(); |
| 8287 | if (!path) |
| 8288 | return -ENOMEM; |
| 8289 | |
| 8290 | /* |
| 8291 | * Walk down the tree. Missing extent blocks are filled in as |
| 8292 | * we go. Metadata is accounted every time we read a new |
| 8293 | * extent block. |
| 8294 | * |
| 8295 | * When we reach a leaf, we account for file extent items in it, |
| 8296 | * walk back up the tree (adjusting slot pointers as we go) |
| 8297 | * and restart the search process. |
| 8298 | */ |
| 8299 | extent_buffer_get(root_eb); /* For path */ |
| 8300 | path->nodes[root_level] = root_eb; |
| 8301 | path->slots[root_level] = 0; |
| 8302 | path->locks[root_level] = 0; /* so release_path doesn't try to unlock */ |
| 8303 | walk_down: |
| 8304 | level = root_level; |
| 8305 | while (level >= 0) { |
| 8306 | if (path->nodes[level] == NULL) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8307 | int parent_slot; |
| 8308 | u64 child_gen; |
| 8309 | u64 child_bytenr; |
| 8310 | |
| 8311 | /* We need to get child blockptr/gen from |
| 8312 | * parent before we can read it. */ |
| 8313 | eb = path->nodes[level + 1]; |
| 8314 | parent_slot = path->slots[level + 1]; |
| 8315 | child_bytenr = btrfs_node_blockptr(eb, parent_slot); |
| 8316 | child_gen = btrfs_node_ptr_generation(eb, parent_slot); |
| 8317 | |
David Sterba | ce86cd5 | 2014-06-15 01:07:32 +0200 | [diff] [blame] | 8318 | eb = read_tree_block(root, child_bytenr, child_gen); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 8319 | if (IS_ERR(eb)) { |
| 8320 | ret = PTR_ERR(eb); |
| 8321 | goto out; |
| 8322 | } else if (!extent_buffer_uptodate(eb)) { |
Liu Bo | 8635eda | 2015-05-25 17:30:14 +0800 | [diff] [blame] | 8323 | free_extent_buffer(eb); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 8324 | ret = -EIO; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8325 | goto out; |
| 8326 | } |
| 8327 | |
| 8328 | path->nodes[level] = eb; |
| 8329 | path->slots[level] = 0; |
| 8330 | |
| 8331 | btrfs_tree_read_lock(eb); |
| 8332 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 8333 | path->locks[level] = BTRFS_READ_LOCK_BLOCKING; |
Mark Fasheh | 82bd101 | 2015-11-05 14:38:00 -0800 | [diff] [blame] | 8334 | |
| 8335 | ret = record_one_subtree_extent(trans, root, child_bytenr, |
| 8336 | root->nodesize); |
| 8337 | if (ret) |
| 8338 | goto out; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8339 | } |
| 8340 | |
| 8341 | if (level == 0) { |
| 8342 | ret = account_leaf_items(trans, root, path->nodes[level]); |
| 8343 | if (ret) |
| 8344 | goto out; |
| 8345 | |
| 8346 | /* Nonzero return here means we completed our search */ |
| 8347 | ret = adjust_slots_upwards(root, path, root_level); |
| 8348 | if (ret) |
| 8349 | break; |
| 8350 | |
| 8351 | /* Restart search with new slots */ |
| 8352 | goto walk_down; |
| 8353 | } |
| 8354 | |
| 8355 | level--; |
| 8356 | } |
| 8357 | |
| 8358 | ret = 0; |
| 8359 | out: |
| 8360 | btrfs_free_path(path); |
| 8361 | |
| 8362 | return ret; |
| 8363 | } |
| 8364 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8365 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8366 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8367 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8368 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 8369 | * back refs for pointers in the block. |
| 8370 | * |
| 8371 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8372 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8373 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 8374 | struct btrfs_root *root, |
| 8375 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8376 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8377 | { |
| 8378 | int level = wc->level; |
| 8379 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8380 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8381 | int ret; |
| 8382 | |
| 8383 | if (wc->stage == UPDATE_BACKREF && |
| 8384 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 8385 | return 1; |
| 8386 | |
| 8387 | /* |
| 8388 | * when reference count of tree block is 1, it won't increase |
| 8389 | * again. once full backref flag is set, we never clear it. |
| 8390 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8391 | if (lookup_info && |
| 8392 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 8393 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8394 | BUG_ON(!path->locks[level]); |
| 8395 | ret = btrfs_lookup_extent_info(trans, root, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8396 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8397 | &wc->refs[level], |
| 8398 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8399 | BUG_ON(ret == -ENOMEM); |
| 8400 | if (ret) |
| 8401 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8402 | BUG_ON(wc->refs[level] == 0); |
| 8403 | } |
| 8404 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8405 | if (wc->stage == DROP_REFERENCE) { |
| 8406 | if (wc->refs[level] > 1) |
| 8407 | return 1; |
| 8408 | |
| 8409 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8410 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8411 | path->locks[level] = 0; |
| 8412 | } |
| 8413 | return 0; |
| 8414 | } |
| 8415 | |
| 8416 | /* wc->stage == UPDATE_BACKREF */ |
| 8417 | if (!(wc->flags[level] & flag)) { |
| 8418 | BUG_ON(!path->locks[level]); |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8419 | ret = btrfs_inc_ref(trans, root, eb, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8420 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8421 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8422 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8423 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 8424 | eb->len, flag, |
| 8425 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8426 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8427 | wc->flags[level] |= flag; |
| 8428 | } |
| 8429 | |
| 8430 | /* |
| 8431 | * the block is shared by multiple trees, so it's not good to |
| 8432 | * keep the tree lock |
| 8433 | */ |
| 8434 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8435 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8436 | path->locks[level] = 0; |
| 8437 | } |
| 8438 | return 0; |
| 8439 | } |
| 8440 | |
| 8441 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8442 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8443 | * |
| 8444 | * when wc->stage == DROP_REFERENCE, this function checks |
| 8445 | * reference count of the block pointed to. if the block |
| 8446 | * is shared and we need update back refs for the subtree |
| 8447 | * rooted at the block, this function changes wc->stage to |
| 8448 | * UPDATE_BACKREF. if the block is shared and there is no |
| 8449 | * need to update back, this function drops the reference |
| 8450 | * to the block. |
| 8451 | * |
| 8452 | * NOTE: return value 1 means we should stop walking down. |
| 8453 | */ |
| 8454 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 8455 | struct btrfs_root *root, |
| 8456 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8457 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8458 | { |
| 8459 | u64 bytenr; |
| 8460 | u64 generation; |
| 8461 | u64 parent; |
| 8462 | u32 blocksize; |
| 8463 | struct btrfs_key key; |
| 8464 | struct extent_buffer *next; |
| 8465 | int level = wc->level; |
| 8466 | int reada = 0; |
| 8467 | int ret = 0; |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8468 | bool need_account = false; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8469 | |
| 8470 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 8471 | path->slots[level]); |
| 8472 | /* |
| 8473 | * if the lower level block was created before the snapshot |
| 8474 | * was created, we know there is no need to update back refs |
| 8475 | * for the subtree |
| 8476 | */ |
| 8477 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8478 | generation <= root->root_key.offset) { |
| 8479 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8480 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8481 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8482 | |
| 8483 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
David Sterba | 707e8a0 | 2014-06-04 19:22:26 +0200 | [diff] [blame] | 8484 | blocksize = root->nodesize; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8485 | |
Daniel Dressler | 01d5847 | 2014-11-21 17:15:07 +0900 | [diff] [blame] | 8486 | next = btrfs_find_tree_block(root->fs_info, bytenr); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8487 | if (!next) { |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 8488 | next = btrfs_find_create_tree_block(root, bytenr); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 8489 | if (!next) |
| 8490 | return -ENOMEM; |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 8491 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 8492 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8493 | reada = 1; |
| 8494 | } |
| 8495 | btrfs_tree_lock(next); |
| 8496 | btrfs_set_lock_blocking(next); |
| 8497 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8498 | ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8499 | &wc->refs[level - 1], |
| 8500 | &wc->flags[level - 1]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8501 | if (ret < 0) { |
| 8502 | btrfs_tree_unlock(next); |
| 8503 | return ret; |
| 8504 | } |
| 8505 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 8506 | if (unlikely(wc->refs[level - 1] == 0)) { |
| 8507 | btrfs_err(root->fs_info, "Missing references."); |
| 8508 | BUG(); |
| 8509 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8510 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8511 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8512 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8513 | if (wc->refs[level - 1] > 1) { |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8514 | need_account = true; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8515 | if (level == 1 && |
| 8516 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8517 | goto skip; |
| 8518 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8519 | if (!wc->update_ref || |
| 8520 | generation <= root->root_key.offset) |
| 8521 | goto skip; |
| 8522 | |
| 8523 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 8524 | path->slots[level]); |
| 8525 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 8526 | if (ret < 0) |
| 8527 | goto skip; |
| 8528 | |
| 8529 | wc->stage = UPDATE_BACKREF; |
| 8530 | wc->shared_level = level - 1; |
| 8531 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8532 | } else { |
| 8533 | if (level == 1 && |
| 8534 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 8535 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8536 | } |
| 8537 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 8538 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8539 | btrfs_tree_unlock(next); |
| 8540 | free_extent_buffer(next); |
| 8541 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8542 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | if (!next) { |
| 8546 | if (reada && level == 1) |
| 8547 | reada_walk_down(trans, root, wc, path); |
David Sterba | ce86cd5 | 2014-06-15 01:07:32 +0200 | [diff] [blame] | 8548 | next = read_tree_block(root, bytenr, generation); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 8549 | if (IS_ERR(next)) { |
| 8550 | return PTR_ERR(next); |
| 8551 | } else if (!extent_buffer_uptodate(next)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8552 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 8553 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 8554 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8555 | btrfs_tree_lock(next); |
| 8556 | btrfs_set_lock_blocking(next); |
| 8557 | } |
| 8558 | |
| 8559 | level--; |
| 8560 | BUG_ON(level != btrfs_header_level(next)); |
| 8561 | path->nodes[level] = next; |
| 8562 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8563 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8564 | wc->level = level; |
| 8565 | if (wc->level == 1) |
| 8566 | wc->reada_slot = 0; |
| 8567 | return 0; |
| 8568 | skip: |
| 8569 | wc->refs[level - 1] = 0; |
| 8570 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8571 | if (wc->stage == DROP_REFERENCE) { |
| 8572 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 8573 | parent = path->nodes[level]->start; |
| 8574 | } else { |
| 8575 | BUG_ON(root->root_key.objectid != |
| 8576 | btrfs_header_owner(path->nodes[level])); |
| 8577 | parent = 0; |
| 8578 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8579 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8580 | if (need_account) { |
| 8581 | ret = account_shared_subtree(trans, root, next, |
| 8582 | generation, level - 1); |
| 8583 | if (ret) { |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 8584 | btrfs_err_rl(root->fs_info, |
| 8585 | "Error " |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8586 | "%d accounting shared subtree. Quota " |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 8587 | "is out of sync, rescan required.", |
| 8588 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8589 | } |
| 8590 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8591 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
Filipe Manana | b06c4bf | 2015-10-23 07:52:54 +0100 | [diff] [blame] | 8592 | root->root_key.objectid, level - 1, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8593 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8594 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8595 | btrfs_tree_unlock(next); |
| 8596 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8597 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8598 | return 1; |
| 8599 | } |
| 8600 | |
| 8601 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 8602 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8603 | * |
| 8604 | * when wc->stage == DROP_REFERENCE, this function drops |
| 8605 | * reference count on the block. |
| 8606 | * |
| 8607 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 8608 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 8609 | * to UPDATE_BACKREF previously while processing the block. |
| 8610 | * |
| 8611 | * NOTE: return value 1 means we should stop walking up. |
| 8612 | */ |
| 8613 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 8614 | struct btrfs_root *root, |
| 8615 | struct btrfs_path *path, |
| 8616 | struct walk_control *wc) |
| 8617 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8618 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8619 | int level = wc->level; |
| 8620 | struct extent_buffer *eb = path->nodes[level]; |
| 8621 | u64 parent = 0; |
| 8622 | |
| 8623 | if (wc->stage == UPDATE_BACKREF) { |
| 8624 | BUG_ON(wc->shared_level < level); |
| 8625 | if (level < wc->shared_level) |
| 8626 | goto out; |
| 8627 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8628 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 8629 | if (ret > 0) |
| 8630 | wc->update_ref = 0; |
| 8631 | |
| 8632 | wc->stage = DROP_REFERENCE; |
| 8633 | wc->shared_level = -1; |
| 8634 | path->slots[level] = 0; |
| 8635 | |
| 8636 | /* |
| 8637 | * check reference count again if the block isn't locked. |
| 8638 | * we should start walking down the tree again if reference |
| 8639 | * count is one. |
| 8640 | */ |
| 8641 | if (!path->locks[level]) { |
| 8642 | BUG_ON(level == 0); |
| 8643 | btrfs_tree_lock(eb); |
| 8644 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8645 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8646 | |
| 8647 | ret = btrfs_lookup_extent_info(trans, root, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8648 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8649 | &wc->refs[level], |
| 8650 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8651 | if (ret < 0) { |
| 8652 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8653 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8654 | return ret; |
| 8655 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8656 | BUG_ON(wc->refs[level] == 0); |
| 8657 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8658 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 8659 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8660 | return 1; |
| 8661 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8662 | } |
| 8663 | } |
| 8664 | |
| 8665 | /* wc->stage == DROP_REFERENCE */ |
| 8666 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 8667 | |
| 8668 | if (wc->refs[level] == 1) { |
| 8669 | if (level == 0) { |
| 8670 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8671 | ret = btrfs_dec_ref(trans, root, eb, 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8672 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 8673 | ret = btrfs_dec_ref(trans, root, eb, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8674 | BUG_ON(ret); /* -ENOMEM */ |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8675 | ret = account_leaf_items(trans, root, eb); |
| 8676 | if (ret) { |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 8677 | btrfs_err_rl(root->fs_info, |
| 8678 | "error " |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8679 | "%d accounting leaf items. Quota " |
David Sterba | 9464732 | 2015-10-08 11:01:36 +0200 | [diff] [blame] | 8680 | "is out of sync, rescan required.", |
| 8681 | ret); |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8682 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8683 | } |
| 8684 | /* make block locked assertion in clean_tree_block happy */ |
| 8685 | if (!path->locks[level] && |
| 8686 | btrfs_header_generation(eb) == trans->transid) { |
| 8687 | btrfs_tree_lock(eb); |
| 8688 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8689 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8690 | } |
Daniel Dressler | 01d5847 | 2014-11-21 17:15:07 +0900 | [diff] [blame] | 8691 | clean_tree_block(trans, root->fs_info, eb); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8692 | } |
| 8693 | |
| 8694 | if (eb == root->node) { |
| 8695 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8696 | parent = eb->start; |
| 8697 | else |
| 8698 | BUG_ON(root->root_key.objectid != |
| 8699 | btrfs_header_owner(eb)); |
| 8700 | } else { |
| 8701 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 8702 | parent = path->nodes[level + 1]->start; |
| 8703 | else |
| 8704 | BUG_ON(root->root_key.objectid != |
| 8705 | btrfs_header_owner(path->nodes[level + 1])); |
| 8706 | } |
| 8707 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 8708 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8709 | out: |
| 8710 | wc->refs[level] = 0; |
| 8711 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8712 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8713 | } |
| 8714 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8715 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 8716 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8717 | struct btrfs_path *path, |
| 8718 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8719 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8720 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8721 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8722 | int ret; |
| 8723 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8724 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8725 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8726 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8727 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8728 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8729 | if (level == 0) |
| 8730 | break; |
| 8731 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 8732 | if (path->slots[level] >= |
| 8733 | btrfs_header_nritems(path->nodes[level])) |
| 8734 | break; |
| 8735 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 8736 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8737 | if (ret > 0) { |
| 8738 | path->slots[level]++; |
| 8739 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 8740 | } else if (ret < 0) |
| 8741 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8742 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8743 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8744 | return 0; |
| 8745 | } |
| 8746 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8747 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 8748 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8749 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8750 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8751 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8752 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8753 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8754 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8755 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 8756 | while (level < max_level && path->nodes[level]) { |
| 8757 | wc->level = level; |
| 8758 | if (path->slots[level] + 1 < |
| 8759 | btrfs_header_nritems(path->nodes[level])) { |
| 8760 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8761 | return 0; |
| 8762 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8763 | ret = walk_up_proc(trans, root, path, wc); |
| 8764 | if (ret > 0) |
| 8765 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 8766 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8767 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8768 | btrfs_tree_unlock_rw(path->nodes[level], |
| 8769 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8770 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8771 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8772 | free_extent_buffer(path->nodes[level]); |
| 8773 | path->nodes[level] = NULL; |
| 8774 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8775 | } |
| 8776 | } |
| 8777 | return 1; |
| 8778 | } |
| 8779 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8780 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8781 | * drop a subvolume tree. |
| 8782 | * |
| 8783 | * this function traverses the tree freeing any blocks that only |
| 8784 | * referenced by the tree. |
| 8785 | * |
| 8786 | * when a shared tree block is found. this function decreases its |
| 8787 | * reference count by one. if update_ref is true, this function |
| 8788 | * also make sure backrefs for the shared block and all lower level |
| 8789 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 8790 | * |
| 8791 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8792 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 8793 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8794 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 8795 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8796 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8797 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8798 | struct btrfs_trans_handle *trans; |
| 8799 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8800 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8801 | struct walk_control *wc; |
| 8802 | struct btrfs_key key; |
| 8803 | int err = 0; |
| 8804 | int ret; |
| 8805 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8806 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8807 | |
Mark Fasheh | 1152651 | 2014-07-17 12:39:01 -0700 | [diff] [blame] | 8808 | btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid); |
| 8809 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8810 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8811 | if (!path) { |
| 8812 | err = -ENOMEM; |
| 8813 | goto out; |
| 8814 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8815 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8816 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 8817 | if (!wc) { |
| 8818 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8819 | err = -ENOMEM; |
| 8820 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 8821 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8822 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8823 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8824 | if (IS_ERR(trans)) { |
| 8825 | err = PTR_ERR(trans); |
| 8826 | goto out_free; |
| 8827 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 8828 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8829 | if (block_rsv) |
| 8830 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8831 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8832 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8833 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8834 | path->nodes[level] = btrfs_lock_root_node(root); |
| 8835 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8836 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8837 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8838 | memset(&wc->update_progress, 0, |
| 8839 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8840 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8841 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8842 | memcpy(&wc->update_progress, &key, |
| 8843 | sizeof(wc->update_progress)); |
| 8844 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 8845 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8846 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 8847 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8848 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 8849 | path->lowest_level = 0; |
| 8850 | if (ret < 0) { |
| 8851 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8852 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 8853 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8854 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8855 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 8856 | /* |
| 8857 | * unlock our path, this is safe because only this |
| 8858 | * function is allowed to delete this snapshot |
| 8859 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8860 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 8861 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8862 | level = btrfs_header_level(root->node); |
| 8863 | while (1) { |
| 8864 | btrfs_tree_lock(path->nodes[level]); |
| 8865 | btrfs_set_lock_blocking(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 8866 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8867 | |
| 8868 | ret = btrfs_lookup_extent_info(trans, root, |
| 8869 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 8870 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8871 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8872 | if (ret < 0) { |
| 8873 | err = ret; |
| 8874 | goto out_end_trans; |
| 8875 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8876 | BUG_ON(wc->refs[level] == 0); |
| 8877 | |
| 8878 | if (level == root_item->drop_level) |
| 8879 | break; |
| 8880 | |
| 8881 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 8882 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8883 | WARN_ON(wc->refs[level] != 1); |
| 8884 | level--; |
| 8885 | } |
| 8886 | } |
| 8887 | |
| 8888 | wc->level = level; |
| 8889 | wc->shared_level = -1; |
| 8890 | wc->stage = DROP_REFERENCE; |
| 8891 | wc->update_ref = update_ref; |
| 8892 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8893 | wc->for_reloc = for_reloc; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8894 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8895 | |
| 8896 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 8897 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8898 | ret = walk_down_tree(trans, root, path, wc); |
| 8899 | if (ret < 0) { |
| 8900 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 8901 | break; |
| 8902 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8903 | |
| 8904 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 8905 | if (ret < 0) { |
| 8906 | err = ret; |
| 8907 | break; |
| 8908 | } |
| 8909 | |
| 8910 | if (ret > 0) { |
| 8911 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 8912 | break; |
| 8913 | } |
| 8914 | |
| 8915 | if (wc->stage == DROP_REFERENCE) { |
| 8916 | level = wc->level; |
| 8917 | btrfs_node_key(path->nodes[level], |
| 8918 | &root_item->drop_progress, |
| 8919 | path->slots[level]); |
| 8920 | root_item->drop_level = level; |
| 8921 | } |
| 8922 | |
| 8923 | BUG_ON(wc->level == 0); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 8924 | if (btrfs_should_end_transaction(trans, tree_root) || |
| 8925 | (!for_reloc && btrfs_need_cleaner_sleep(root))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8926 | ret = btrfs_update_root(trans, tree_root, |
| 8927 | &root->root_key, |
| 8928 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8929 | if (ret) { |
| 8930 | btrfs_abort_transaction(trans, tree_root, ret); |
| 8931 | err = ret; |
| 8932 | goto out_end_trans; |
| 8933 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8934 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8935 | btrfs_end_transaction_throttle(trans, tree_root); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 8936 | if (!for_reloc && btrfs_need_cleaner_sleep(root)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 8937 | pr_debug("BTRFS: drop snapshot early exit\n"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 8938 | err = -EAGAIN; |
| 8939 | goto out_free; |
| 8940 | } |
| 8941 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 8942 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8943 | if (IS_ERR(trans)) { |
| 8944 | err = PTR_ERR(trans); |
| 8945 | goto out_free; |
| 8946 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8947 | if (block_rsv) |
| 8948 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 8949 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8950 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8951 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8952 | if (err) |
| 8953 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8954 | |
| 8955 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8956 | if (ret) { |
| 8957 | btrfs_abort_transaction(trans, tree_root, ret); |
| 8958 | goto out_end_trans; |
| 8959 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8960 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8961 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 8962 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 8963 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8964 | if (ret < 0) { |
| 8965 | btrfs_abort_transaction(trans, tree_root, ret); |
| 8966 | err = ret; |
| 8967 | goto out_end_trans; |
| 8968 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 8969 | /* if we fail to delete the orphan item this time |
| 8970 | * around, it'll get picked up the next time. |
| 8971 | * |
| 8972 | * The most common failure here is just -ENOENT. |
| 8973 | */ |
| 8974 | btrfs_del_orphan_item(trans, tree_root, |
| 8975 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8976 | } |
| 8977 | } |
| 8978 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 8979 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Josef Bacik | 2b9dbef | 2015-09-15 10:07:04 -0400 | [diff] [blame] | 8980 | btrfs_add_dropped_root(trans, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8981 | } else { |
| 8982 | free_extent_buffer(root->node); |
| 8983 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 8984 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8985 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8986 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8987 | out_end_trans: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8988 | btrfs_end_transaction_throttle(trans, tree_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8989 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8990 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8991 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8992 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8993 | /* |
| 8994 | * So if we need to stop dropping the snapshot for whatever reason we |
| 8995 | * need to make sure to add it back to the dead root list so that we |
| 8996 | * keep trying to do the work later. This also cleans up roots if we |
| 8997 | * don't have it in the radix (like when we recover after a power fail |
| 8998 | * or unmount) so we don't leak memory. |
| 8999 | */ |
Josef Bacik | b37b39c | 2013-07-23 16:57:15 -0400 | [diff] [blame] | 9000 | if (!for_reloc && root_dropped == false) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 9001 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 9002 | if (err && err != -EAGAIN) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 9003 | btrfs_std_error(root->fs_info, err, NULL); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 9004 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 9005 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9006 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9007 | /* |
| 9008 | * drop subtree rooted at tree block 'node'. |
| 9009 | * |
| 9010 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9011 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9012 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9013 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 9014 | struct btrfs_root *root, |
| 9015 | struct extent_buffer *node, |
| 9016 | struct extent_buffer *parent) |
| 9017 | { |
| 9018 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9019 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9020 | int level; |
| 9021 | int parent_level; |
| 9022 | int ret = 0; |
| 9023 | int wret; |
| 9024 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9025 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 9026 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9027 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9028 | if (!path) |
| 9029 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9030 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9031 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 9032 | if (!wc) { |
| 9033 | btrfs_free_path(path); |
| 9034 | return -ENOMEM; |
| 9035 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9036 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9037 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9038 | parent_level = btrfs_header_level(parent); |
| 9039 | extent_buffer_get(parent); |
| 9040 | path->nodes[parent_level] = parent; |
| 9041 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 9042 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 9043 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9044 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9045 | path->nodes[level] = node; |
| 9046 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 9047 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9048 | |
| 9049 | wc->refs[parent_level] = 1; |
| 9050 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 9051 | wc->level = level; |
| 9052 | wc->shared_level = -1; |
| 9053 | wc->stage = DROP_REFERENCE; |
| 9054 | wc->update_ref = 0; |
| 9055 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 9056 | wc->for_reloc = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 9057 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9058 | |
| 9059 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9060 | wret = walk_down_tree(trans, root, path, wc); |
| 9061 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9062 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9063 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9064 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9065 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9066 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9067 | if (wret < 0) |
| 9068 | ret = wret; |
| 9069 | if (wret != 0) |
| 9070 | break; |
| 9071 | } |
| 9072 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 9073 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9074 | btrfs_free_path(path); |
| 9075 | return ret; |
| 9076 | } |
| 9077 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9078 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 9079 | { |
| 9080 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9081 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9082 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9083 | /* |
| 9084 | * if restripe for this chunk_type is on pick target profile and |
| 9085 | * return, otherwise do the usual balance |
| 9086 | */ |
| 9087 | stripped = get_restripe_target(root->fs_info, flags); |
| 9088 | if (stripped) |
| 9089 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 9090 | |
Miao Xie | 9566997 | 2014-07-24 11:37:14 +0800 | [diff] [blame] | 9091 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 9092 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9093 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 9094 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9095 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 9096 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9097 | if (num_devices == 1) { |
| 9098 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9099 | stripped = flags & ~stripped; |
| 9100 | |
| 9101 | /* turn raid0 into single device chunks */ |
| 9102 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 9103 | return stripped; |
| 9104 | |
| 9105 | /* turn mirroring into duplication */ |
| 9106 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9107 | BTRFS_BLOCK_GROUP_RAID10)) |
| 9108 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9109 | } else { |
| 9110 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9111 | if (flags & stripped) |
| 9112 | return flags; |
| 9113 | |
| 9114 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 9115 | stripped = flags & ~stripped; |
| 9116 | |
| 9117 | /* switch duplicated blocks with raid1 */ |
| 9118 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 9119 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 9120 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9121 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9122 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 9123 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 9124 | return flags; |
| 9125 | } |
| 9126 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9127 | 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] | 9128 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9129 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9130 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9131 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9132 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9133 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 9134 | /* |
| 9135 | * We need some metadata space and system metadata space for |
| 9136 | * allocating chunks in some corner cases until we force to set |
| 9137 | * it to be readonly. |
| 9138 | */ |
| 9139 | if ((sinfo->flags & |
| 9140 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 9141 | !force) |
| 9142 | min_allocable_bytes = 1 * 1024 * 1024; |
| 9143 | else |
| 9144 | min_allocable_bytes = 0; |
| 9145 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9146 | spin_lock(&sinfo->lock); |
| 9147 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9148 | |
| 9149 | if (cache->ro) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9150 | cache->ro++; |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9151 | ret = 0; |
| 9152 | goto out; |
| 9153 | } |
| 9154 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9155 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 9156 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 9157 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9158 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 9159 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 9160 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9161 | sinfo->bytes_readonly += num_bytes; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9162 | cache->ro++; |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9163 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9164 | ret = 0; |
| 9165 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 9166 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9167 | spin_unlock(&cache->lock); |
| 9168 | spin_unlock(&sinfo->lock); |
| 9169 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 9170 | } |
| 9171 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9172 | int btrfs_inc_block_group_ro(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9173 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9174 | |
| 9175 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9176 | struct btrfs_trans_handle *trans; |
| 9177 | u64 alloc_flags; |
| 9178 | int ret; |
| 9179 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9180 | again: |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 9181 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9182 | if (IS_ERR(trans)) |
| 9183 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9184 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9185 | /* |
| 9186 | * we're not allowed to set block groups readonly after the dirty |
| 9187 | * block groups cache has started writing. If it already started, |
| 9188 | * back off and let this transaction commit |
| 9189 | */ |
| 9190 | mutex_lock(&root->fs_info->ro_block_group_mutex); |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 9191 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9192 | u64 transid = trans->transid; |
| 9193 | |
| 9194 | mutex_unlock(&root->fs_info->ro_block_group_mutex); |
| 9195 | btrfs_end_transaction(trans, root); |
| 9196 | |
| 9197 | ret = btrfs_wait_for_commit(root, transid); |
| 9198 | if (ret) |
| 9199 | return ret; |
| 9200 | goto again; |
| 9201 | } |
| 9202 | |
Chris Mason | 153c35b | 2015-05-19 18:54:41 -0700 | [diff] [blame] | 9203 | /* |
| 9204 | * if we are changing raid levels, try to allocate a corresponding |
| 9205 | * block group with the new raid level. |
| 9206 | */ |
| 9207 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 9208 | if (alloc_flags != cache->flags) { |
| 9209 | ret = do_chunk_alloc(trans, root, alloc_flags, |
| 9210 | CHUNK_ALLOC_FORCE); |
| 9211 | /* |
| 9212 | * ENOSPC is allowed here, we may have enough space |
| 9213 | * already allocated at the new raid level to |
| 9214 | * carry on |
| 9215 | */ |
| 9216 | if (ret == -ENOSPC) |
| 9217 | ret = 0; |
| 9218 | if (ret < 0) |
| 9219 | goto out; |
| 9220 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9221 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9222 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9223 | if (!ret) |
| 9224 | goto out; |
| 9225 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 9226 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 9227 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9228 | if (ret < 0) |
| 9229 | goto out; |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9230 | ret = inc_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9231 | out: |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9232 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 9233 | alloc_flags = update_block_group_flags(root, cache->flags); |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 9234 | lock_chunks(root->fs_info->chunk_root); |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 9235 | check_system_chunk(trans, root, alloc_flags); |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 9236 | unlock_chunks(root->fs_info->chunk_root); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9237 | } |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 9238 | mutex_unlock(&root->fs_info->ro_block_group_mutex); |
Shaohua Li | 2f08108 | 2015-01-09 10:40:15 -0800 | [diff] [blame] | 9239 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9240 | btrfs_end_transaction(trans, root); |
| 9241 | return ret; |
| 9242 | } |
| 9243 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9244 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 9245 | struct btrfs_root *root, u64 type) |
| 9246 | { |
| 9247 | u64 alloc_flags = get_alloc_profile(root, type); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 9248 | return do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 9249 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 9250 | } |
| 9251 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9252 | /* |
| 9253 | * 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] | 9254 | * space_info. takes mirrors into account. |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9255 | */ |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9256 | 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] | 9257 | { |
| 9258 | struct btrfs_block_group_cache *block_group; |
| 9259 | u64 free_bytes = 0; |
| 9260 | int factor; |
| 9261 | |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9262 | /* It's df, we don't care if it's racey */ |
| 9263 | if (list_empty(&sinfo->ro_bgs)) |
| 9264 | return 0; |
| 9265 | |
| 9266 | spin_lock(&sinfo->lock); |
| 9267 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9268 | spin_lock(&block_group->lock); |
| 9269 | |
| 9270 | if (!block_group->ro) { |
| 9271 | spin_unlock(&block_group->lock); |
| 9272 | continue; |
| 9273 | } |
| 9274 | |
| 9275 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 9276 | BTRFS_BLOCK_GROUP_RAID10 | |
| 9277 | BTRFS_BLOCK_GROUP_DUP)) |
| 9278 | factor = 2; |
| 9279 | else |
| 9280 | factor = 1; |
| 9281 | |
| 9282 | free_bytes += (block_group->key.offset - |
| 9283 | btrfs_block_group_used(&block_group->item)) * |
| 9284 | factor; |
| 9285 | |
| 9286 | spin_unlock(&block_group->lock); |
| 9287 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 9288 | spin_unlock(&sinfo->lock); |
| 9289 | |
| 9290 | return free_bytes; |
| 9291 | } |
| 9292 | |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9293 | void btrfs_dec_block_group_ro(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9294 | struct btrfs_block_group_cache *cache) |
| 9295 | { |
| 9296 | struct btrfs_space_info *sinfo = cache->space_info; |
| 9297 | u64 num_bytes; |
| 9298 | |
| 9299 | BUG_ON(!cache->ro); |
| 9300 | |
| 9301 | spin_lock(&sinfo->lock); |
| 9302 | spin_lock(&cache->lock); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9303 | if (!--cache->ro) { |
| 9304 | num_bytes = cache->key.offset - cache->reserved - |
| 9305 | cache->pinned - cache->bytes_super - |
| 9306 | btrfs_block_group_used(&cache->item); |
| 9307 | sinfo->bytes_readonly -= num_bytes; |
| 9308 | list_del_init(&cache->ro_list); |
| 9309 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9310 | spin_unlock(&cache->lock); |
| 9311 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 9312 | } |
| 9313 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9314 | /* |
| 9315 | * checks to see if its even possible to relocate this block group. |
| 9316 | * |
| 9317 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 9318 | * ok to go ahead and try. |
| 9319 | */ |
| 9320 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9321 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9322 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9323 | struct btrfs_space_info *space_info; |
| 9324 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 9325 | struct btrfs_device *device; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9326 | struct btrfs_trans_handle *trans; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9327 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9328 | u64 dev_min = 1; |
| 9329 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9330 | u64 target; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9331 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9332 | int full = 0; |
| 9333 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9334 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9335 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9336 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9337 | /* odd, couldn't find the block group, leave it alone */ |
| 9338 | if (!block_group) |
| 9339 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9340 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9341 | min_free = btrfs_block_group_used(&block_group->item); |
| 9342 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9343 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9344 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9345 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9346 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9347 | space_info = block_group->space_info; |
| 9348 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 9349 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9350 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9351 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9352 | /* |
| 9353 | * 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] | 9354 | * relocate it unless we're able to allocate a new chunk below. |
| 9355 | * |
| 9356 | * Otherwise, we need to make sure we have room in the space to handle |
| 9357 | * 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] | 9358 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 9359 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9360 | (space_info->bytes_used + space_info->bytes_reserved + |
| 9361 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 9362 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9363 | spin_unlock(&space_info->lock); |
| 9364 | goto out; |
| 9365 | } |
| 9366 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9367 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9368 | /* |
| 9369 | * ok we don't have enough space, but maybe we have free space on our |
| 9370 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9371 | * alloc devices and guess if we have enough space. if this block |
| 9372 | * group is going to be restriped, run checks against the target |
| 9373 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9374 | */ |
| 9375 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 9376 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9377 | /* |
| 9378 | * index: |
| 9379 | * 0: raid10 |
| 9380 | * 1: raid1 |
| 9381 | * 2: dup |
| 9382 | * 3: raid0 |
| 9383 | * 4: single |
| 9384 | */ |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9385 | target = get_restripe_target(root->fs_info, block_group->flags); |
| 9386 | if (target) { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 9387 | index = __get_raid_index(extended_to_chunk(target)); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 9388 | } else { |
| 9389 | /* |
| 9390 | * this is just a balance, so if we were marked as full |
| 9391 | * we know there is no space for a new chunk |
| 9392 | */ |
| 9393 | if (full) |
| 9394 | goto out; |
| 9395 | |
| 9396 | index = get_block_group_index(block_group); |
| 9397 | } |
| 9398 | |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9399 | if (index == BTRFS_RAID_RAID10) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9400 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9401 | /* Divide by 2 */ |
| 9402 | min_free >>= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9403 | } else if (index == BTRFS_RAID_RAID1) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9404 | dev_min = 2; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9405 | } else if (index == BTRFS_RAID_DUP) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 9406 | /* Multiply by 2 */ |
| 9407 | min_free <<= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 9408 | } else if (index == BTRFS_RAID_RAID0) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9409 | dev_min = fs_devices->rw_devices; |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 9410 | min_free = div64_u64(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9411 | } |
| 9412 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9413 | /* We need to do this so that we can look at pending chunks */ |
| 9414 | trans = btrfs_join_transaction(root); |
| 9415 | if (IS_ERR(trans)) { |
| 9416 | ret = PTR_ERR(trans); |
| 9417 | goto out; |
| 9418 | } |
| 9419 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9420 | mutex_lock(&root->fs_info->chunk_mutex); |
| 9421 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9422 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 9423 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9424 | /* |
| 9425 | * check to make sure we can actually find a chunk with enough |
| 9426 | * space to fit our block group in. |
| 9427 | */ |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 9428 | if (device->total_bytes > device->bytes_used + min_free && |
| 9429 | !device->is_tgtdev_for_dev_replace) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9430 | ret = find_free_dev_extent(trans, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 9431 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9432 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9433 | dev_nr++; |
| 9434 | |
| 9435 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9436 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 9437 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9438 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 9439 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9440 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9441 | mutex_unlock(&root->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9442 | btrfs_end_transaction(trans, root); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9443 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9444 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 9445 | return ret; |
| 9446 | } |
| 9447 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 9448 | static int find_first_block_group(struct btrfs_root *root, |
| 9449 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9450 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9451 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9452 | struct btrfs_key found_key; |
| 9453 | struct extent_buffer *leaf; |
| 9454 | int slot; |
| 9455 | |
| 9456 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 9457 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9458 | goto out; |
| 9459 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9460 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9461 | slot = path->slots[0]; |
| 9462 | leaf = path->nodes[0]; |
| 9463 | if (slot >= btrfs_header_nritems(leaf)) { |
| 9464 | ret = btrfs_next_leaf(root, path); |
| 9465 | if (ret == 0) |
| 9466 | continue; |
| 9467 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9468 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9469 | break; |
| 9470 | } |
| 9471 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 9472 | |
| 9473 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9474 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 9475 | ret = 0; |
| 9476 | goto out; |
| 9477 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9478 | path->slots[0]++; |
| 9479 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9480 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9481 | return ret; |
| 9482 | } |
| 9483 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9484 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 9485 | { |
| 9486 | struct btrfs_block_group_cache *block_group; |
| 9487 | u64 last = 0; |
| 9488 | |
| 9489 | while (1) { |
| 9490 | struct inode *inode; |
| 9491 | |
| 9492 | block_group = btrfs_lookup_first_block_group(info, last); |
| 9493 | while (block_group) { |
| 9494 | spin_lock(&block_group->lock); |
| 9495 | if (block_group->iref) |
| 9496 | break; |
| 9497 | spin_unlock(&block_group->lock); |
| 9498 | block_group = next_block_group(info->tree_root, |
| 9499 | block_group); |
| 9500 | } |
| 9501 | if (!block_group) { |
| 9502 | if (last == 0) |
| 9503 | break; |
| 9504 | last = 0; |
| 9505 | continue; |
| 9506 | } |
| 9507 | |
| 9508 | inode = block_group->inode; |
| 9509 | block_group->iref = 0; |
| 9510 | block_group->inode = NULL; |
| 9511 | spin_unlock(&block_group->lock); |
| 9512 | iput(inode); |
| 9513 | last = block_group->key.objectid + block_group->key.offset; |
| 9514 | btrfs_put_block_group(block_group); |
| 9515 | } |
| 9516 | } |
| 9517 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9518 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 9519 | { |
| 9520 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9521 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9522 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9523 | struct rb_node *n; |
| 9524 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9525 | down_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9526 | while (!list_empty(&info->caching_block_groups)) { |
| 9527 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 9528 | struct btrfs_caching_control, list); |
| 9529 | list_del(&caching_ctl->list); |
| 9530 | put_caching_control(caching_ctl); |
| 9531 | } |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 9532 | up_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9533 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9534 | spin_lock(&info->unused_bgs_lock); |
| 9535 | while (!list_empty(&info->unused_bgs)) { |
| 9536 | block_group = list_first_entry(&info->unused_bgs, |
| 9537 | struct btrfs_block_group_cache, |
| 9538 | bg_list); |
| 9539 | list_del_init(&block_group->bg_list); |
| 9540 | btrfs_put_block_group(block_group); |
| 9541 | } |
| 9542 | spin_unlock(&info->unused_bgs_lock); |
| 9543 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9544 | spin_lock(&info->block_group_cache_lock); |
| 9545 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 9546 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 9547 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9548 | rb_erase(&block_group->cache_node, |
| 9549 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 9550 | RB_CLEAR_NODE(&block_group->cache_node); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9551 | spin_unlock(&info->block_group_cache_lock); |
| 9552 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9553 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9554 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9555 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 9556 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9557 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9558 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9559 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9560 | /* |
| 9561 | * We haven't cached this block group, which means we could |
| 9562 | * possibly have excluded extents on this block group. |
| 9563 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 9564 | if (block_group->cached == BTRFS_CACHE_NO || |
| 9565 | block_group->cached == BTRFS_CACHE_ERROR) |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9566 | free_excluded_extents(info->extent_root, block_group); |
| 9567 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9568 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 9569 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 9570 | |
| 9571 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9572 | } |
| 9573 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9574 | |
| 9575 | /* now that all the block groups are freed, go through and |
| 9576 | * free all the space_info structs. This is only called during |
| 9577 | * the final stages of unmount, and so we know nobody is |
| 9578 | * using them. We call synchronize_rcu() once before we start, |
| 9579 | * just to be on the safe side. |
| 9580 | */ |
| 9581 | synchronize_rcu(); |
| 9582 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 9583 | release_global_block_rsv(info); |
| 9584 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 9585 | while (!list_empty(&info->space_info)) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9586 | int i; |
| 9587 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9588 | space_info = list_entry(info->space_info.next, |
| 9589 | struct btrfs_space_info, |
| 9590 | list); |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 9591 | if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9592 | if (WARN_ON(space_info->bytes_pinned > 0 || |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 9593 | space_info->bytes_reserved > 0 || |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 9594 | space_info->bytes_may_use > 0)) { |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 9595 | dump_space_info(space_info, 0, 0); |
| 9596 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9597 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9598 | list_del(&space_info->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9599 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 9600 | struct kobject *kobj; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9601 | kobj = space_info->block_group_kobjs[i]; |
| 9602 | space_info->block_group_kobjs[i] = NULL; |
| 9603 | if (kobj) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9604 | kobject_del(kobj); |
| 9605 | kobject_put(kobj); |
| 9606 | } |
| 9607 | } |
| 9608 | kobject_del(&space_info->kobj); |
| 9609 | kobject_put(&space_info->kobj); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 9610 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9611 | return 0; |
| 9612 | } |
| 9613 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9614 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 9615 | struct btrfs_block_group_cache *cache) |
| 9616 | { |
| 9617 | int index = get_block_group_index(cache); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9618 | bool first = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9619 | |
| 9620 | down_write(&space_info->groups_sem); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 9621 | if (list_empty(&space_info->block_groups[index])) |
| 9622 | first = true; |
| 9623 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 9624 | up_write(&space_info->groups_sem); |
| 9625 | |
| 9626 | if (first) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9627 | struct raid_kobject *rkobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9628 | int ret; |
| 9629 | |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9630 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 9631 | if (!rkobj) |
| 9632 | goto out_err; |
| 9633 | rkobj->raid_type = index; |
| 9634 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 9635 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, |
| 9636 | "%s", get_raid_name(index)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9637 | if (ret) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9638 | kobject_put(&rkobj->kobj); |
| 9639 | goto out_err; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9640 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9641 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9642 | } |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 9643 | |
| 9644 | return; |
| 9645 | out_err: |
| 9646 | pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n"); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9647 | } |
| 9648 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9649 | static struct btrfs_block_group_cache * |
| 9650 | btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) |
| 9651 | { |
| 9652 | struct btrfs_block_group_cache *cache; |
| 9653 | |
| 9654 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 9655 | if (!cache) |
| 9656 | return NULL; |
| 9657 | |
| 9658 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 9659 | GFP_NOFS); |
| 9660 | if (!cache->free_space_ctl) { |
| 9661 | kfree(cache); |
| 9662 | return NULL; |
| 9663 | } |
| 9664 | |
| 9665 | cache->key.objectid = start; |
| 9666 | cache->key.offset = size; |
| 9667 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 9668 | |
| 9669 | cache->sectorsize = root->sectorsize; |
| 9670 | cache->fs_info = root->fs_info; |
| 9671 | cache->full_stripe_len = btrfs_full_stripe_len(root, |
| 9672 | &root->fs_info->mapping_tree, |
| 9673 | start); |
| 9674 | atomic_set(&cache->count, 1); |
| 9675 | spin_lock_init(&cache->lock); |
Miao Xie | e570fd2 | 2014-06-19 10:42:50 +0800 | [diff] [blame] | 9676 | init_rwsem(&cache->data_rwsem); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9677 | INIT_LIST_HEAD(&cache->list); |
| 9678 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9679 | INIT_LIST_HEAD(&cache->bg_list); |
Josef Bacik | 633c0aa | 2014-10-31 09:49:34 -0400 | [diff] [blame] | 9680 | INIT_LIST_HEAD(&cache->ro_list); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9681 | INIT_LIST_HEAD(&cache->dirty_list); |
Chris Mason | c9dc4c6 | 2015-04-04 17:14:42 -0700 | [diff] [blame] | 9682 | INIT_LIST_HEAD(&cache->io_list); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9683 | btrfs_init_free_space_ctl(cache); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 9684 | atomic_set(&cache->trimming, 0); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9685 | |
| 9686 | return cache; |
| 9687 | } |
| 9688 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9689 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 9690 | { |
| 9691 | struct btrfs_path *path; |
| 9692 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9693 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 9694 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9695 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9696 | struct btrfs_key key; |
| 9697 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9698 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9699 | int need_clear = 0; |
| 9700 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 9701 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 9702 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9703 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9704 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 9705 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9706 | path = btrfs_alloc_path(); |
| 9707 | if (!path) |
| 9708 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 9709 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9710 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 9711 | cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 9712 | if (btrfs_test_opt(root, SPACE_CACHE) && |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 9713 | btrfs_super_generation(root->fs_info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9714 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 9715 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 9716 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9717 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 9718 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9719 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9720 | if (ret > 0) |
| 9721 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9722 | if (ret != 0) |
| 9723 | goto error; |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9724 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9725 | leaf = path->nodes[0]; |
| 9726 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9727 | |
| 9728 | cache = btrfs_create_block_group_cache(root, found_key.objectid, |
| 9729 | found_key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9730 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9731 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9732 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9733 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9734 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9735 | if (need_clear) { |
| 9736 | /* |
| 9737 | * When we mount with old space cache, we need to |
| 9738 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 9739 | * |
| 9740 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 9741 | * truncate the old free space cache inode and |
| 9742 | * setup a new one. |
| 9743 | * b) Setting 'dirty flag' makes sure that we flush |
| 9744 | * the new space cache info onto disk. |
| 9745 | */ |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9746 | if (btrfs_test_opt(root, SPACE_CACHE)) |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 9747 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 9748 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9749 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 9750 | read_extent_buffer(leaf, &cache->item, |
| 9751 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 9752 | sizeof(cache->item)); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9753 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9754 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9755 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9756 | btrfs_release_path(path); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 9757 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9758 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9759 | * We need to exclude the super stripes now so that the space |
| 9760 | * info has super bytes accounted for, otherwise we'll think |
| 9761 | * we have more space than we actually do. |
| 9762 | */ |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 9763 | ret = exclude_super_stripes(root, cache); |
| 9764 | if (ret) { |
| 9765 | /* |
| 9766 | * We may have excluded something, so call this just in |
| 9767 | * case. |
| 9768 | */ |
| 9769 | free_excluded_extents(root, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9770 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 9771 | goto error; |
| 9772 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 9773 | |
| 9774 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9775 | * check for two cases, either we are full, and therefore |
| 9776 | * don't need to bother with the caching work since we won't |
| 9777 | * find any space, or we are empty, and we can just add all |
| 9778 | * the space in and be done with it. This saves us _alot_ of |
| 9779 | * time, particularly in the full case. |
| 9780 | */ |
| 9781 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9782 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9783 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 9784 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9785 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9786 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9787 | cache->cached = BTRFS_CACHE_FINISHED; |
| 9788 | add_new_free_space(cache, root->fs_info, |
| 9789 | found_key.objectid, |
| 9790 | found_key.objectid + |
| 9791 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9792 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9793 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 9794 | |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 9795 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 9796 | if (ret) { |
| 9797 | btrfs_remove_free_space_cache(cache); |
| 9798 | btrfs_put_block_group(cache); |
| 9799 | goto error; |
| 9800 | } |
| 9801 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9802 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 9803 | btrfs_block_group_used(&cache->item), |
| 9804 | &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 9805 | if (ret) { |
| 9806 | btrfs_remove_free_space_cache(cache); |
| 9807 | spin_lock(&info->block_group_cache_lock); |
| 9808 | rb_erase(&cache->cache_node, |
| 9809 | &info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 9810 | RB_CLEAR_NODE(&cache->cache_node); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 9811 | spin_unlock(&info->block_group_cache_lock); |
| 9812 | btrfs_put_block_group(cache); |
| 9813 | goto error; |
| 9814 | } |
| 9815 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9816 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 9817 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9818 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 9819 | spin_unlock(&cache->space_info->lock); |
| 9820 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9821 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9822 | |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 9823 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9824 | if (btrfs_chunk_readonly(root, cache->key.objectid)) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9825 | inc_block_group_ro(cache, 1); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9826 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
| 9827 | spin_lock(&info->unused_bgs_lock); |
| 9828 | /* Should always be true but just in case. */ |
| 9829 | if (list_empty(&cache->bg_list)) { |
| 9830 | btrfs_get_block_group(cache); |
| 9831 | list_add_tail(&cache->bg_list, |
| 9832 | &info->unused_bgs); |
| 9833 | } |
| 9834 | spin_unlock(&info->unused_bgs_lock); |
| 9835 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9836 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9837 | |
| 9838 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 9839 | if (!(get_alloc_profile(root, space_info->flags) & |
| 9840 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 9841 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 9842 | BTRFS_BLOCK_GROUP_RAID5 | |
| 9843 | BTRFS_BLOCK_GROUP_RAID6 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9844 | BTRFS_BLOCK_GROUP_DUP))) |
| 9845 | continue; |
| 9846 | /* |
| 9847 | * avoid allocating from un-mirrored block group if there are |
| 9848 | * mirrored block groups. |
| 9849 | */ |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 9850 | list_for_each_entry(cache, |
| 9851 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 9852 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9853 | inc_block_group_ro(cache, 1); |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 9854 | list_for_each_entry(cache, |
| 9855 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 9856 | list) |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 9857 | inc_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9858 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9859 | |
| 9860 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9861 | ret = 0; |
| 9862 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9863 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 9864 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 9865 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9866 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 9867 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
| 9868 | struct btrfs_root *root) |
| 9869 | { |
| 9870 | struct btrfs_block_group_cache *block_group, *tmp; |
| 9871 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 9872 | struct btrfs_block_group_item item; |
| 9873 | struct btrfs_key key; |
| 9874 | int ret = 0; |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 9875 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 9876 | |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 9877 | trans->can_flush_pending_bgs = false; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9878 | 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] | 9879 | if (ret) |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 9880 | goto next; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 9881 | |
| 9882 | spin_lock(&block_group->lock); |
| 9883 | memcpy(&item, &block_group->item, sizeof(item)); |
| 9884 | memcpy(&key, &block_group->key, sizeof(key)); |
| 9885 | spin_unlock(&block_group->lock); |
| 9886 | |
| 9887 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 9888 | sizeof(item)); |
| 9889 | if (ret) |
| 9890 | btrfs_abort_transaction(trans, extent_root, ret); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 9891 | ret = btrfs_finish_chunk_alloc(trans, extent_root, |
| 9892 | key.objectid, key.offset); |
| 9893 | if (ret) |
| 9894 | btrfs_abort_transaction(trans, extent_root, ret); |
Filipe Manana | c92f6be | 2014-11-26 15:28:55 +0000 | [diff] [blame] | 9895 | next: |
| 9896 | list_del_init(&block_group->bg_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 9897 | } |
Filipe Manana | d9a0540 | 2015-10-03 13:13:13 +0100 | [diff] [blame] | 9898 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 9899 | } |
| 9900 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9901 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 9902 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 9903 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9904 | u64 size) |
| 9905 | { |
| 9906 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9907 | struct btrfs_root *extent_root; |
| 9908 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9909 | |
| 9910 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9911 | |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 9912 | btrfs_set_log_full_commit(root->fs_info, trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 9913 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9914 | cache = btrfs_create_block_group_cache(root, chunk_offset, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 9915 | if (!cache) |
| 9916 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 9917 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9918 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9919 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9920 | btrfs_set_block_group_flags(&cache->item, type); |
| 9921 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9922 | cache->flags = type; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9923 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9924 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 9925 | ret = exclude_super_stripes(root, cache); |
| 9926 | if (ret) { |
| 9927 | /* |
| 9928 | * We may have excluded something, so call this just in |
| 9929 | * case. |
| 9930 | */ |
| 9931 | free_excluded_extents(root, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 9932 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 9933 | return ret; |
| 9934 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9935 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9936 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 9937 | chunk_offset + size); |
| 9938 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9939 | free_excluded_extents(root, cache); |
| 9940 | |
Josef Bacik | d0bd456 | 2015-09-23 14:54:14 -0400 | [diff] [blame] | 9941 | #ifdef CONFIG_BTRFS_DEBUG |
| 9942 | if (btrfs_should_fragment_free_space(root, cache)) { |
| 9943 | u64 new_bytes_used = size - bytes_used; |
| 9944 | |
| 9945 | bytes_used += new_bytes_used >> 1; |
| 9946 | fragment_free_space(root, cache); |
| 9947 | } |
| 9948 | #endif |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 9949 | /* |
| 9950 | * Call to ensure the corresponding space_info object is created and |
| 9951 | * assigned to our block group, but don't update its counters just yet. |
| 9952 | * We want our bg to be added to the rbtree with its ->space_info set. |
| 9953 | */ |
| 9954 | ret = update_space_info(root->fs_info, cache->flags, 0, 0, |
| 9955 | &cache->space_info); |
| 9956 | if (ret) { |
| 9957 | btrfs_remove_free_space_cache(cache); |
| 9958 | btrfs_put_block_group(cache); |
| 9959 | return ret; |
| 9960 | } |
| 9961 | |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 9962 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 9963 | if (ret) { |
| 9964 | btrfs_remove_free_space_cache(cache); |
| 9965 | btrfs_put_block_group(cache); |
| 9966 | return ret; |
| 9967 | } |
| 9968 | |
Filipe Manana | 2e6e518 | 2015-05-12 00:28:11 +0100 | [diff] [blame] | 9969 | /* |
| 9970 | * Now that our block group has its ->space_info set and is inserted in |
| 9971 | * the rbtree, update the space info's counters. |
| 9972 | */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9973 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 9974 | &cache->space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 9975 | if (ret) { |
| 9976 | btrfs_remove_free_space_cache(cache); |
| 9977 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 9978 | rb_erase(&cache->cache_node, |
| 9979 | &root->fs_info->block_group_cache_tree); |
Filipe Manana | 01eacb2 | 2014-12-04 18:38:30 +0000 | [diff] [blame] | 9980 | RB_CLEAR_NODE(&cache->cache_node); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 9981 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 9982 | btrfs_put_block_group(cache); |
| 9983 | return ret; |
| 9984 | } |
Li Zefan | c7c144d | 2011-12-07 10:39:22 +0800 | [diff] [blame] | 9985 | update_global_block_rsv(root->fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 9986 | |
| 9987 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 9988 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 9989 | spin_unlock(&cache->space_info->lock); |
| 9990 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 9991 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9992 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 9993 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9994 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 9995 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 9996 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 9997 | return 0; |
| 9998 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9999 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10000 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 10001 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 10002 | u64 extra_flags = chunk_to_extended(flags) & |
| 10003 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10004 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10005 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10006 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 10007 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 10008 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 10009 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 10010 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 10011 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 10012 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10013 | } |
| 10014 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10015 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10016 | struct btrfs_root *root, u64 group_start, |
| 10017 | struct extent_map *em) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10018 | { |
| 10019 | struct btrfs_path *path; |
| 10020 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10021 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10022 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10023 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10024 | struct inode *inode; |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10025 | struct kobject *kobj = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10026 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10027 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10028 | int factor; |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10029 | struct btrfs_caching_control *caching_ctl = NULL; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10030 | bool remove_em; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10031 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10032 | root = root->fs_info->extent_root; |
| 10033 | |
| 10034 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 10035 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10036 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10037 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 10038 | /* |
| 10039 | * Free the reserved super bytes from this block group before |
| 10040 | * remove it. |
| 10041 | */ |
| 10042 | free_excluded_extents(root, block_group); |
| 10043 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10044 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10045 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10046 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 10047 | BTRFS_BLOCK_GROUP_RAID1 | |
| 10048 | BTRFS_BLOCK_GROUP_RAID10)) |
| 10049 | factor = 2; |
| 10050 | else |
| 10051 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10052 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10053 | /* make sure this block group isn't part of an allocation cluster */ |
| 10054 | cluster = &root->fs_info->data_alloc_cluster; |
| 10055 | spin_lock(&cluster->refill_lock); |
| 10056 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10057 | spin_unlock(&cluster->refill_lock); |
| 10058 | |
| 10059 | /* |
| 10060 | * make sure this block group isn't part of a metadata |
| 10061 | * allocation cluster |
| 10062 | */ |
| 10063 | cluster = &root->fs_info->meta_alloc_cluster; |
| 10064 | spin_lock(&cluster->refill_lock); |
| 10065 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 10066 | spin_unlock(&cluster->refill_lock); |
| 10067 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10068 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 10069 | if (!path) { |
| 10070 | ret = -ENOMEM; |
| 10071 | goto out; |
| 10072 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10073 | |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10074 | /* |
| 10075 | * get the inode first so any iput calls done for the io_list |
| 10076 | * aren't the final iput (no unlinks allowed now) |
| 10077 | */ |
Ilya Dryomov | 10b2f34 | 2011-10-02 13:56:53 +0300 | [diff] [blame] | 10078 | inode = lookup_free_space_inode(tree_root, block_group, path); |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10079 | |
| 10080 | mutex_lock(&trans->transaction->cache_write_mutex); |
| 10081 | /* |
| 10082 | * make sure our free spache cache IO is done before remove the |
| 10083 | * free space inode |
| 10084 | */ |
| 10085 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10086 | if (!list_empty(&block_group->io_list)) { |
| 10087 | list_del_init(&block_group->io_list); |
| 10088 | |
| 10089 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); |
| 10090 | |
| 10091 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 10092 | btrfs_wait_cache_io(root, trans, block_group, |
| 10093 | &block_group->io_ctl, path, |
| 10094 | block_group->key.objectid); |
| 10095 | btrfs_put_block_group(block_group); |
| 10096 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10097 | } |
| 10098 | |
| 10099 | if (!list_empty(&block_group->dirty_list)) { |
| 10100 | list_del_init(&block_group->dirty_list); |
| 10101 | btrfs_put_block_group(block_group); |
| 10102 | } |
| 10103 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
| 10104 | mutex_unlock(&trans->transaction->cache_write_mutex); |
| 10105 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10106 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 10107 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 10108 | if (ret) { |
| 10109 | btrfs_add_delayed_iput(inode); |
| 10110 | goto out; |
| 10111 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10112 | clear_nlink(inode); |
| 10113 | /* One for the block groups ref */ |
| 10114 | spin_lock(&block_group->lock); |
| 10115 | if (block_group->iref) { |
| 10116 | block_group->iref = 0; |
| 10117 | block_group->inode = NULL; |
| 10118 | spin_unlock(&block_group->lock); |
| 10119 | iput(inode); |
| 10120 | } else { |
| 10121 | spin_unlock(&block_group->lock); |
| 10122 | } |
| 10123 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 10124 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10125 | } |
| 10126 | |
| 10127 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 10128 | key.offset = block_group->key.objectid; |
| 10129 | key.type = 0; |
| 10130 | |
| 10131 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 10132 | if (ret < 0) |
| 10133 | goto out; |
| 10134 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10135 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10136 | if (ret == 0) { |
| 10137 | ret = btrfs_del_item(trans, tree_root, path); |
| 10138 | if (ret) |
| 10139 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 10140 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10141 | } |
| 10142 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 10143 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10144 | rb_erase(&block_group->cache_node, |
| 10145 | &root->fs_info->block_group_cache_tree); |
Filipe Manana | 292cbd5 | 2014-11-26 15:28:50 +0000 | [diff] [blame] | 10146 | RB_CLEAR_NODE(&block_group->cache_node); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 10147 | |
| 10148 | if (root->fs_info->first_logical_byte == block_group->key.objectid) |
| 10149 | root->fs_info->first_logical_byte = (u64)-1; |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 10150 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10151 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10152 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 10153 | /* |
| 10154 | * we must use list_del_init so people can check to see if they |
| 10155 | * are still on the list after taking the semaphore |
| 10156 | */ |
| 10157 | list_del_init(&block_group->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10158 | if (list_empty(&block_group->space_info->block_groups[index])) { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10159 | kobj = block_group->space_info->block_group_kobjs[index]; |
| 10160 | block_group->space_info->block_group_kobjs[index] = NULL; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 10161 | clear_avail_alloc_bits(root->fs_info, block_group->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 10162 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 10163 | up_write(&block_group->space_info->groups_sem); |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 10164 | if (kobj) { |
| 10165 | kobject_del(kobj); |
| 10166 | kobject_put(kobj); |
| 10167 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10168 | |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10169 | if (block_group->has_caching_ctl) |
| 10170 | caching_ctl = get_caching_control(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10171 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 10172 | wait_block_group_cache_done(block_group); |
Filipe Manana | 4f69cb9 | 2014-11-26 15:28:51 +0000 | [diff] [blame] | 10173 | if (block_group->has_caching_ctl) { |
| 10174 | down_write(&root->fs_info->commit_root_sem); |
| 10175 | if (!caching_ctl) { |
| 10176 | struct btrfs_caching_control *ctl; |
| 10177 | |
| 10178 | list_for_each_entry(ctl, |
| 10179 | &root->fs_info->caching_block_groups, list) |
| 10180 | if (ctl->block_group == block_group) { |
| 10181 | caching_ctl = ctl; |
| 10182 | atomic_inc(&caching_ctl->count); |
| 10183 | break; |
| 10184 | } |
| 10185 | } |
| 10186 | if (caching_ctl) |
| 10187 | list_del_init(&caching_ctl->list); |
| 10188 | up_write(&root->fs_info->commit_root_sem); |
| 10189 | if (caching_ctl) { |
| 10190 | /* Once for the caching bgs list and once for us. */ |
| 10191 | put_caching_control(caching_ctl); |
| 10192 | put_caching_control(caching_ctl); |
| 10193 | } |
| 10194 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10195 | |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10196 | spin_lock(&trans->transaction->dirty_bgs_lock); |
| 10197 | if (!list_empty(&block_group->dirty_list)) { |
Chris Mason | 1bbc621 | 2015-04-06 12:46:08 -0700 | [diff] [blame] | 10198 | WARN_ON(1); |
| 10199 | } |
| 10200 | if (!list_empty(&block_group->io_list)) { |
| 10201 | WARN_ON(1); |
Josef Bacik | ce93ec5 | 2014-11-17 15:45:48 -0500 | [diff] [blame] | 10202 | } |
| 10203 | spin_unlock(&trans->transaction->dirty_bgs_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 10204 | btrfs_remove_free_space_cache(block_group); |
| 10205 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10206 | spin_lock(&block_group->space_info->lock); |
Filipe Manana | 75c68e9 | 2015-01-16 13:24:40 +0000 | [diff] [blame] | 10207 | list_del_init(&block_group->ro_list); |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10208 | |
| 10209 | if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 10210 | WARN_ON(block_group->space_info->total_bytes |
| 10211 | < block_group->key.offset); |
| 10212 | WARN_ON(block_group->space_info->bytes_readonly |
| 10213 | < block_group->key.offset); |
| 10214 | WARN_ON(block_group->space_info->disk_total |
| 10215 | < block_group->key.offset * factor); |
| 10216 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10217 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 10218 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 10219 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Zhao Lei | 18d018a | 2015-02-24 20:07:44 +0800 | [diff] [blame] | 10220 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 10221 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 10222 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 10223 | memcpy(&key, &block_group->key, sizeof(key)); |
| 10224 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10225 | lock_chunks(root); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 10226 | if (!list_empty(&em->list)) { |
| 10227 | /* We're in the transaction->pending_chunks list. */ |
| 10228 | free_extent_map(em); |
| 10229 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10230 | spin_lock(&block_group->lock); |
| 10231 | block_group->removed = 1; |
| 10232 | /* |
| 10233 | * At this point trimming can't start on this block group, because we |
| 10234 | * removed the block group from the tree fs_info->block_group_cache_tree |
| 10235 | * so no one can't find it anymore and even if someone already got this |
| 10236 | * block group before we removed it from the rbtree, they have already |
| 10237 | * incremented block_group->trimming - if they didn't, they won't find |
| 10238 | * any free space entries because we already removed them all when we |
| 10239 | * called btrfs_remove_free_space_cache(). |
| 10240 | * |
| 10241 | * And we must not remove the extent map from the fs_info->mapping_tree |
| 10242 | * to prevent the same logical address range and physical device space |
| 10243 | * ranges from being reused for a new block group. This is because our |
| 10244 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is |
| 10245 | * completely transactionless, so while it is trimming a range the |
| 10246 | * currently running transaction might finish and a new one start, |
| 10247 | * allowing for new block groups to be created that can reuse the same |
| 10248 | * physical device locations unless we take this special care. |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10249 | * |
| 10250 | * There may also be an implicit trim operation if the file system |
| 10251 | * is mounted with -odiscard. The same protections must remain |
| 10252 | * in place until the extents have been discarded completely when |
| 10253 | * the transaction commit has completed. |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10254 | */ |
| 10255 | remove_em = (atomic_read(&block_group->trimming) == 0); |
| 10256 | /* |
| 10257 | * Make sure a trimmer task always sees the em in the pinned_chunks list |
| 10258 | * if it sees block_group->removed == 1 (needs to lock block_group->lock |
| 10259 | * before checking block_group->removed). |
| 10260 | */ |
| 10261 | if (!remove_em) { |
| 10262 | /* |
| 10263 | * Our em might be in trans->transaction->pending_chunks which |
| 10264 | * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks), |
| 10265 | * and so is the fs_info->pinned_chunks list. |
| 10266 | * |
| 10267 | * So at this point we must be holding the chunk_mutex to avoid |
| 10268 | * any races with chunk allocation (more specifically at |
| 10269 | * volumes.c:contains_pending_extent()), to ensure it always |
| 10270 | * sees the em, either in the pending_chunks list or in the |
| 10271 | * pinned_chunks list. |
| 10272 | */ |
| 10273 | list_move_tail(&em->list, &root->fs_info->pinned_chunks); |
| 10274 | } |
| 10275 | spin_unlock(&block_group->lock); |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10276 | |
| 10277 | if (remove_em) { |
| 10278 | struct extent_map_tree *em_tree; |
| 10279 | |
| 10280 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 10281 | write_lock(&em_tree->lock); |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10282 | /* |
| 10283 | * The em might be in the pending_chunks list, so make sure the |
| 10284 | * chunk mutex is locked, since remove_extent_mapping() will |
| 10285 | * delete us from that list. |
| 10286 | */ |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 10287 | remove_extent_mapping(em_tree, em); |
| 10288 | write_unlock(&em_tree->lock); |
| 10289 | /* once for the tree */ |
| 10290 | free_extent_map(em); |
| 10291 | } |
| 10292 | |
Filipe Manana | 8dbcd10 | 2014-12-02 18:07:49 +0000 | [diff] [blame] | 10293 | unlock_chunks(root); |
| 10294 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 10295 | btrfs_put_block_group(block_group); |
| 10296 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 10297 | |
| 10298 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 10299 | if (ret > 0) |
| 10300 | ret = -EIO; |
| 10301 | if (ret < 0) |
| 10302 | goto out; |
| 10303 | |
| 10304 | ret = btrfs_del_item(trans, root, path); |
| 10305 | out: |
| 10306 | btrfs_free_path(path); |
| 10307 | return ret; |
| 10308 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10309 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10310 | struct btrfs_trans_handle * |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10311 | btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info, |
| 10312 | const u64 chunk_offset) |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10313 | { |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10314 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 10315 | struct extent_map *em; |
| 10316 | struct map_lookup *map; |
| 10317 | unsigned int num_items; |
| 10318 | |
| 10319 | read_lock(&em_tree->lock); |
| 10320 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 10321 | read_unlock(&em_tree->lock); |
| 10322 | ASSERT(em && em->start == chunk_offset); |
| 10323 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10324 | /* |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10325 | * We need to reserve 3 + N units from the metadata space info in order |
| 10326 | * to remove a block group (done at btrfs_remove_chunk() and at |
| 10327 | * btrfs_remove_block_group()), which are used for: |
| 10328 | * |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10329 | * 1 unit for adding the free space inode's orphan (located in the tree |
| 10330 | * of tree roots). |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10331 | * 1 unit for deleting the block group item (located in the extent |
| 10332 | * tree). |
| 10333 | * 1 unit for deleting the free space item (located in tree of tree |
| 10334 | * roots). |
| 10335 | * N units for deleting N device extent items corresponding to each |
| 10336 | * stripe (located in the device tree). |
| 10337 | * |
| 10338 | * In order to remove a block group we also need to reserve units in the |
| 10339 | * system space info in order to update the chunk tree (update one or |
| 10340 | * more device items and remove one chunk item), but this is done at |
| 10341 | * btrfs_remove_chunk() through a call to check_system_chunk(). |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10342 | */ |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10343 | map = (struct map_lookup *)em->bdev; |
| 10344 | num_items = 3 + map->num_stripes; |
| 10345 | free_extent_map(em); |
| 10346 | |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10347 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10348 | num_items, 1); |
Filipe Manana | 8eab77f | 2015-11-13 23:57:16 +0000 | [diff] [blame] | 10349 | } |
| 10350 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10351 | /* |
| 10352 | * Process the unused_bgs list and remove any that don't have any allocated |
| 10353 | * space inside of them. |
| 10354 | */ |
| 10355 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) |
| 10356 | { |
| 10357 | struct btrfs_block_group_cache *block_group; |
| 10358 | struct btrfs_space_info *space_info; |
| 10359 | struct btrfs_root *root = fs_info->extent_root; |
| 10360 | struct btrfs_trans_handle *trans; |
| 10361 | int ret = 0; |
| 10362 | |
| 10363 | if (!fs_info->open) |
| 10364 | return; |
| 10365 | |
| 10366 | spin_lock(&fs_info->unused_bgs_lock); |
| 10367 | while (!list_empty(&fs_info->unused_bgs)) { |
| 10368 | u64 start, end; |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10369 | int trimming; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10370 | |
| 10371 | block_group = list_first_entry(&fs_info->unused_bgs, |
| 10372 | struct btrfs_block_group_cache, |
| 10373 | bg_list); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10374 | list_del_init(&block_group->bg_list); |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10375 | |
| 10376 | space_info = block_group->space_info; |
| 10377 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10378 | if (ret || btrfs_mixed_space_info(space_info)) { |
| 10379 | btrfs_put_block_group(block_group); |
| 10380 | continue; |
| 10381 | } |
| 10382 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10383 | |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10384 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 10385 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10386 | /* Don't want to race with allocators so take the groups_sem */ |
| 10387 | down_write(&space_info->groups_sem); |
| 10388 | spin_lock(&block_group->lock); |
| 10389 | if (block_group->reserved || |
| 10390 | btrfs_block_group_used(&block_group->item) || |
Zhao Lei | aefbe9a | 2015-09-29 21:03:54 +0800 | [diff] [blame] | 10391 | block_group->ro || |
| 10392 | list_is_singular(&block_group->list)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10393 | /* |
| 10394 | * We want to bail if we made new allocations or have |
| 10395 | * outstanding allocations in this block group. We do |
| 10396 | * the ro check in case balance is currently acting on |
| 10397 | * this block group. |
| 10398 | */ |
| 10399 | spin_unlock(&block_group->lock); |
| 10400 | up_write(&space_info->groups_sem); |
| 10401 | goto next; |
| 10402 | } |
| 10403 | spin_unlock(&block_group->lock); |
| 10404 | |
| 10405 | /* 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] | 10406 | ret = inc_block_group_ro(block_group, 0); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10407 | up_write(&space_info->groups_sem); |
| 10408 | if (ret < 0) { |
| 10409 | ret = 0; |
| 10410 | goto next; |
| 10411 | } |
| 10412 | |
| 10413 | /* |
| 10414 | * Want to do this before we do anything else so we can recover |
| 10415 | * properly if we fail to join the transaction. |
| 10416 | */ |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 10417 | trans = btrfs_start_trans_remove_block_group(fs_info, |
| 10418 | block_group->key.objectid); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10419 | if (IS_ERR(trans)) { |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10420 | btrfs_dec_block_group_ro(root, block_group); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10421 | ret = PTR_ERR(trans); |
| 10422 | goto next; |
| 10423 | } |
| 10424 | |
| 10425 | /* |
| 10426 | * We could have pending pinned extents for this block group, |
| 10427 | * just delete them, we don't care about them anymore. |
| 10428 | */ |
| 10429 | start = block_group->key.objectid; |
| 10430 | end = start + block_group->key.offset - 1; |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10431 | /* |
| 10432 | * Hold the unused_bg_unpin_mutex lock to avoid racing with |
| 10433 | * btrfs_finish_extent_commit(). If we are at transaction N, |
| 10434 | * another task might be running finish_extent_commit() for the |
| 10435 | * previous transaction N - 1, and have seen a range belonging |
| 10436 | * to the block group in freed_extents[] before we were able to |
| 10437 | * clear the whole block group range from freed_extents[]. This |
| 10438 | * means that task can lookup for the block group after we |
| 10439 | * unpinned it from freed_extents[] and removed it, leading to |
| 10440 | * a BUG_ON() at btrfs_unpin_extent_range(). |
| 10441 | */ |
| 10442 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10443 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10444 | EXTENT_DIRTY, GFP_NOFS); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10445 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10446 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10447 | btrfs_dec_block_group_ro(root, block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10448 | goto end_trans; |
| 10449 | } |
| 10450 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10451 | EXTENT_DIRTY, GFP_NOFS); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10452 | if (ret) { |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10453 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Zhaolei | 868f401 | 2015-08-05 16:43:27 +0800 | [diff] [blame] | 10454 | btrfs_dec_block_group_ro(root, block_group); |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10455 | goto end_trans; |
| 10456 | } |
Filipe Manana | d4b450c | 2015-01-29 19:18:25 +0000 | [diff] [blame] | 10457 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10458 | |
| 10459 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10460 | spin_lock(&space_info->lock); |
| 10461 | spin_lock(&block_group->lock); |
| 10462 | |
| 10463 | space_info->bytes_pinned -= block_group->pinned; |
| 10464 | space_info->bytes_readonly += block_group->pinned; |
| 10465 | percpu_counter_add(&space_info->total_bytes_pinned, |
| 10466 | -block_group->pinned); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10467 | block_group->pinned = 0; |
| 10468 | |
Zhao Lei | c30666d | 2015-02-25 14:17:20 +0800 | [diff] [blame] | 10469 | spin_unlock(&block_group->lock); |
| 10470 | spin_unlock(&space_info->lock); |
| 10471 | |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10472 | /* DISCARD can flip during remount */ |
| 10473 | trimming = btrfs_test_opt(root, DISCARD); |
| 10474 | |
| 10475 | /* Implicit trim during transaction commit. */ |
| 10476 | if (trimming) |
| 10477 | btrfs_get_block_group_trimming(block_group); |
| 10478 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10479 | /* |
| 10480 | * Btrfs_remove_chunk will abort the transaction if things go |
| 10481 | * horribly wrong. |
| 10482 | */ |
| 10483 | ret = btrfs_remove_chunk(trans, root, |
| 10484 | block_group->key.objectid); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10485 | |
| 10486 | if (ret) { |
| 10487 | if (trimming) |
| 10488 | btrfs_put_block_group_trimming(block_group); |
| 10489 | goto end_trans; |
| 10490 | } |
| 10491 | |
| 10492 | /* |
| 10493 | * If we're not mounted with -odiscard, we can just forget |
| 10494 | * about this block group. Otherwise we'll need to wait |
| 10495 | * until transaction commit to do the actual discard. |
| 10496 | */ |
| 10497 | if (trimming) { |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10498 | spin_lock(&fs_info->unused_bgs_lock); |
| 10499 | /* |
| 10500 | * A concurrent scrub might have added us to the list |
| 10501 | * fs_info->unused_bgs, so use a list_move operation |
| 10502 | * to add the block group to the deleted_bgs list. |
| 10503 | */ |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10504 | list_move(&block_group->bg_list, |
| 10505 | &trans->transaction->deleted_bgs); |
Filipe Manana | 348a001 | 2015-11-27 12:16:16 +0000 | [diff] [blame] | 10506 | spin_unlock(&fs_info->unused_bgs_lock); |
Jeff Mahoney | e33e17e | 2015-06-15 09:41:19 -0400 | [diff] [blame] | 10507 | btrfs_get_block_group(block_group); |
| 10508 | } |
Filipe Manana | 758eb51 | 2014-11-03 14:08:39 +0000 | [diff] [blame] | 10509 | end_trans: |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10510 | btrfs_end_transaction(trans, root); |
| 10511 | next: |
Zhao Lei | d5f2e33 | 2015-10-08 18:46:44 +0800 | [diff] [blame] | 10512 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 10513 | btrfs_put_block_group(block_group); |
| 10514 | spin_lock(&fs_info->unused_bgs_lock); |
| 10515 | } |
| 10516 | spin_unlock(&fs_info->unused_bgs_lock); |
| 10517 | } |
| 10518 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10519 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 10520 | { |
| 10521 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10522 | struct btrfs_super_block *disk_super; |
| 10523 | u64 features; |
| 10524 | u64 flags; |
| 10525 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10526 | int ret; |
| 10527 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 10528 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10529 | if (!btrfs_super_root(disk_super)) |
| 10530 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10531 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10532 | features = btrfs_super_incompat_flags(disk_super); |
| 10533 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 10534 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10535 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10536 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 10537 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10538 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10539 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10540 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 10541 | if (mixed) { |
| 10542 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 10543 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 10544 | } else { |
| 10545 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 10546 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 10547 | if (ret) |
| 10548 | goto out; |
| 10549 | |
| 10550 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 10551 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 10552 | } |
| 10553 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 10554 | return ret; |
| 10555 | } |
| 10556 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10557 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 10558 | { |
Filipe Manana | 678886b | 2014-12-07 21:31:47 +0000 | [diff] [blame] | 10559 | return unpin_extent_range(root, start, end, false); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 10560 | } |
| 10561 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10562 | /* |
| 10563 | * It used to be that old block groups would be left around forever. |
| 10564 | * Iterating over them would be enough to trim unused space. Since we |
| 10565 | * now automatically remove them, we also need to iterate over unallocated |
| 10566 | * space. |
| 10567 | * |
| 10568 | * We don't want a transaction for this since the discard may take a |
| 10569 | * substantial amount of time. We don't require that a transaction be |
| 10570 | * running, but we do need to take a running transaction into account |
| 10571 | * to ensure that we're not discarding chunks that were released in |
| 10572 | * the current transaction. |
| 10573 | * |
| 10574 | * Holding the chunks lock will prevent other threads from allocating |
| 10575 | * or releasing chunks, but it won't prevent a running transaction |
| 10576 | * from committing and releasing the memory that the pending chunks |
| 10577 | * list head uses. For that, we need to take a reference to the |
| 10578 | * transaction. |
| 10579 | */ |
| 10580 | static int btrfs_trim_free_extents(struct btrfs_device *device, |
| 10581 | u64 minlen, u64 *trimmed) |
| 10582 | { |
| 10583 | u64 start = 0, len = 0; |
| 10584 | int ret; |
| 10585 | |
| 10586 | *trimmed = 0; |
| 10587 | |
| 10588 | /* Not writeable = nothing to do. */ |
| 10589 | if (!device->writeable) |
| 10590 | return 0; |
| 10591 | |
| 10592 | /* No free space = nothing to do. */ |
| 10593 | if (device->total_bytes <= device->bytes_used) |
| 10594 | return 0; |
| 10595 | |
| 10596 | ret = 0; |
| 10597 | |
| 10598 | while (1) { |
| 10599 | struct btrfs_fs_info *fs_info = device->dev_root->fs_info; |
| 10600 | struct btrfs_transaction *trans; |
| 10601 | u64 bytes; |
| 10602 | |
| 10603 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); |
| 10604 | if (ret) |
| 10605 | return ret; |
| 10606 | |
| 10607 | down_read(&fs_info->commit_root_sem); |
| 10608 | |
| 10609 | spin_lock(&fs_info->trans_lock); |
| 10610 | trans = fs_info->running_transaction; |
| 10611 | if (trans) |
| 10612 | atomic_inc(&trans->use_count); |
| 10613 | spin_unlock(&fs_info->trans_lock); |
| 10614 | |
| 10615 | ret = find_free_dev_extent_start(trans, device, minlen, start, |
| 10616 | &start, &len); |
| 10617 | if (trans) |
| 10618 | btrfs_put_transaction(trans); |
| 10619 | |
| 10620 | if (ret) { |
| 10621 | up_read(&fs_info->commit_root_sem); |
| 10622 | mutex_unlock(&fs_info->chunk_mutex); |
| 10623 | if (ret == -ENOSPC) |
| 10624 | ret = 0; |
| 10625 | break; |
| 10626 | } |
| 10627 | |
| 10628 | ret = btrfs_issue_discard(device->bdev, start, len, &bytes); |
| 10629 | up_read(&fs_info->commit_root_sem); |
| 10630 | mutex_unlock(&fs_info->chunk_mutex); |
| 10631 | |
| 10632 | if (ret) |
| 10633 | break; |
| 10634 | |
| 10635 | start += len; |
| 10636 | *trimmed += bytes; |
| 10637 | |
| 10638 | if (fatal_signal_pending(current)) { |
| 10639 | ret = -ERESTARTSYS; |
| 10640 | break; |
| 10641 | } |
| 10642 | |
| 10643 | cond_resched(); |
| 10644 | } |
| 10645 | |
| 10646 | return ret; |
| 10647 | } |
| 10648 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10649 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 10650 | { |
| 10651 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 10652 | struct btrfs_block_group_cache *cache = NULL; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10653 | struct btrfs_device *device; |
| 10654 | struct list_head *devices; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10655 | u64 group_trimmed; |
| 10656 | u64 start; |
| 10657 | u64 end; |
| 10658 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10659 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10660 | int ret = 0; |
| 10661 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 10662 | /* |
| 10663 | * try to trim all FS space, our block group may start from non-zero. |
| 10664 | */ |
| 10665 | if (range->len == total_bytes) |
| 10666 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 10667 | else |
| 10668 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10669 | |
| 10670 | while (cache) { |
| 10671 | if (cache->key.objectid >= (range->start + range->len)) { |
| 10672 | btrfs_put_block_group(cache); |
| 10673 | break; |
| 10674 | } |
| 10675 | |
| 10676 | start = max(range->start, cache->key.objectid); |
| 10677 | end = min(range->start + range->len, |
| 10678 | cache->key.objectid + cache->key.offset); |
| 10679 | |
| 10680 | if (end - start >= range->minlen) { |
| 10681 | if (!block_group_cache_done(cache)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 10682 | ret = cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 10683 | if (ret) { |
| 10684 | btrfs_put_block_group(cache); |
| 10685 | break; |
| 10686 | } |
| 10687 | ret = wait_block_group_cache_done(cache); |
| 10688 | if (ret) { |
| 10689 | btrfs_put_block_group(cache); |
| 10690 | break; |
| 10691 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10692 | } |
| 10693 | ret = btrfs_trim_block_group(cache, |
| 10694 | &group_trimmed, |
| 10695 | start, |
| 10696 | end, |
| 10697 | range->minlen); |
| 10698 | |
| 10699 | trimmed += group_trimmed; |
| 10700 | if (ret) { |
| 10701 | btrfs_put_block_group(cache); |
| 10702 | break; |
| 10703 | } |
| 10704 | } |
| 10705 | |
| 10706 | cache = next_block_group(fs_info->tree_root, cache); |
| 10707 | } |
| 10708 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 10709 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 10710 | devices = &root->fs_info->fs_devices->alloc_list; |
| 10711 | list_for_each_entry(device, devices, dev_alloc_list) { |
| 10712 | ret = btrfs_trim_free_extents(device, range->minlen, |
| 10713 | &group_trimmed); |
| 10714 | if (ret) |
| 10715 | break; |
| 10716 | |
| 10717 | trimmed += group_trimmed; |
| 10718 | } |
| 10719 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 10720 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 10721 | range->len = trimmed; |
| 10722 | return ret; |
| 10723 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10724 | |
| 10725 | /* |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10726 | * btrfs_{start,end}_write_no_snapshoting() are similar to |
| 10727 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing |
| 10728 | * data into the page cache through nocow before the subvolume is snapshoted, |
| 10729 | * but flush the data into disk after the snapshot creation, or to prevent |
| 10730 | * operations while snapshoting is ongoing and that cause the snapshot to be |
| 10731 | * inconsistent (writes followed by expanding truncates for example). |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10732 | */ |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10733 | void btrfs_end_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10734 | { |
| 10735 | percpu_counter_dec(&root->subv_writers->counter); |
| 10736 | /* |
David Sterba | a83342a | 2015-02-16 19:36:47 +0100 | [diff] [blame] | 10737 | * Make sure counter is updated before we wake up waiters. |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10738 | */ |
| 10739 | smp_mb(); |
| 10740 | if (waitqueue_active(&root->subv_writers->wait)) |
| 10741 | wake_up(&root->subv_writers->wait); |
| 10742 | } |
| 10743 | |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10744 | int btrfs_start_write_no_snapshoting(struct btrfs_root *root) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10745 | { |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10746 | if (atomic_read(&root->will_be_snapshoted)) |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10747 | return 0; |
| 10748 | |
| 10749 | percpu_counter_inc(&root->subv_writers->counter); |
| 10750 | /* |
| 10751 | * Make sure counter is updated before we check for snapshot creation. |
| 10752 | */ |
| 10753 | smp_mb(); |
David Sterba | ee39b43 | 2014-09-30 01:33:33 +0200 | [diff] [blame] | 10754 | if (atomic_read(&root->will_be_snapshoted)) { |
Filipe Manana | 9ea24bb | 2014-10-29 11:57:59 +0000 | [diff] [blame] | 10755 | btrfs_end_write_no_snapshoting(root); |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 10756 | return 0; |
| 10757 | } |
| 10758 | return 1; |
| 10759 | } |