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 | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 28 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 29 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 30 | #include "ctree.h" |
| 31 | #include "disk-io.h" |
| 32 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 33 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 34 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 35 | #include "raid56.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 36 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 37 | #include "free-space-cache.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 38 | #include "math.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 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 77 | static int update_block_group(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 78 | u64 bytenr, u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 79 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 80 | struct btrfs_root *root, |
| 81 | u64 bytenr, u64 num_bytes, u64 parent, |
| 82 | u64 root_objectid, u64 owner_objectid, |
| 83 | u64 owner_offset, int refs_to_drop, |
| 84 | struct btrfs_delayed_extent_op *extra_op); |
| 85 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 86 | struct extent_buffer *leaf, |
| 87 | struct btrfs_extent_item *ei); |
| 88 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 89 | struct btrfs_root *root, |
| 90 | u64 parent, u64 root_objectid, |
| 91 | u64 flags, u64 owner, u64 offset, |
| 92 | struct btrfs_key *ins, int ref_mod); |
| 93 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 94 | struct btrfs_root *root, |
| 95 | u64 parent, u64 root_objectid, |
| 96 | u64 flags, struct btrfs_disk_key *key, |
| 97 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 98 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 99 | struct btrfs_root *extent_root, u64 flags, |
| 100 | int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 101 | static int find_next_key(struct btrfs_path *path, int level, |
| 102 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 103 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 104 | int dump_block_groups); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 105 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 106 | u64 num_bytes, int reserve); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 107 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 108 | u64 num_bytes); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 109 | int btrfs_pin_extent(struct btrfs_root *root, |
| 110 | u64 bytenr, u64 num_bytes, int reserved); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 111 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 112 | static noinline int |
| 113 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 114 | { |
| 115 | smp_mb(); |
| 116 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 117 | } |
| 118 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 119 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 120 | { |
| 121 | return (cache->flags & bits) == bits; |
| 122 | } |
| 123 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 124 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 125 | { |
| 126 | atomic_inc(&cache->count); |
| 127 | } |
| 128 | |
| 129 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 130 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 131 | if (atomic_dec_and_test(&cache->count)) { |
| 132 | WARN_ON(cache->pinned > 0); |
| 133 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 134 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 135 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 136 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 139 | /* |
| 140 | * this adds the block group to the fs_info rb tree for the block group |
| 141 | * cache |
| 142 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 143 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 144 | struct btrfs_block_group_cache *block_group) |
| 145 | { |
| 146 | struct rb_node **p; |
| 147 | struct rb_node *parent = NULL; |
| 148 | struct btrfs_block_group_cache *cache; |
| 149 | |
| 150 | spin_lock(&info->block_group_cache_lock); |
| 151 | p = &info->block_group_cache_tree.rb_node; |
| 152 | |
| 153 | while (*p) { |
| 154 | parent = *p; |
| 155 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 156 | cache_node); |
| 157 | if (block_group->key.objectid < cache->key.objectid) { |
| 158 | p = &(*p)->rb_left; |
| 159 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 160 | p = &(*p)->rb_right; |
| 161 | } else { |
| 162 | spin_unlock(&info->block_group_cache_lock); |
| 163 | return -EEXIST; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | rb_link_node(&block_group->cache_node, parent, p); |
| 168 | rb_insert_color(&block_group->cache_node, |
| 169 | &info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 170 | |
| 171 | if (info->first_logical_byte > block_group->key.objectid) |
| 172 | info->first_logical_byte = block_group->key.objectid; |
| 173 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 174 | spin_unlock(&info->block_group_cache_lock); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * This will return the block group at or after bytenr if contains is 0, else |
| 181 | * it will return the block group that contains the bytenr |
| 182 | */ |
| 183 | static struct btrfs_block_group_cache * |
| 184 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 185 | int contains) |
| 186 | { |
| 187 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 188 | struct rb_node *n; |
| 189 | u64 end, start; |
| 190 | |
| 191 | spin_lock(&info->block_group_cache_lock); |
| 192 | n = info->block_group_cache_tree.rb_node; |
| 193 | |
| 194 | while (n) { |
| 195 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 196 | cache_node); |
| 197 | end = cache->key.objectid + cache->key.offset - 1; |
| 198 | start = cache->key.objectid; |
| 199 | |
| 200 | if (bytenr < start) { |
| 201 | if (!contains && (!ret || start < ret->key.objectid)) |
| 202 | ret = cache; |
| 203 | n = n->rb_left; |
| 204 | } else if (bytenr > start) { |
| 205 | if (contains && bytenr <= end) { |
| 206 | ret = cache; |
| 207 | break; |
| 208 | } |
| 209 | n = n->rb_right; |
| 210 | } else { |
| 211 | ret = cache; |
| 212 | break; |
| 213 | } |
| 214 | } |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 215 | if (ret) { |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 216 | btrfs_get_block_group(ret); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 217 | if (bytenr == 0 && info->first_logical_byte > ret->key.objectid) |
| 218 | info->first_logical_byte = ret->key.objectid; |
| 219 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 220 | spin_unlock(&info->block_group_cache_lock); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 225 | static int add_excluded_extent(struct btrfs_root *root, |
| 226 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 227 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | u64 end = start + num_bytes - 1; |
| 229 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 230 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 231 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 232 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 233 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 236 | static void free_excluded_extents(struct btrfs_root *root, |
| 237 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 238 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 239 | u64 start, end; |
| 240 | |
| 241 | start = cache->key.objectid; |
| 242 | end = start + cache->key.offset - 1; |
| 243 | |
| 244 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 245 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 246 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 247 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 248 | } |
| 249 | |
| 250 | static int exclude_super_stripes(struct btrfs_root *root, |
| 251 | struct btrfs_block_group_cache *cache) |
| 252 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 253 | u64 bytenr; |
| 254 | u64 *logical; |
| 255 | int stripe_len; |
| 256 | int i, nr, ret; |
| 257 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 258 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 259 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 260 | cache->bytes_super += stripe_len; |
| 261 | ret = add_excluded_extent(root, cache->key.objectid, |
| 262 | stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 267 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 268 | bytenr = btrfs_sb_offset(i); |
| 269 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 270 | cache->key.objectid, bytenr, |
| 271 | 0, &logical, &nr, &stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 272 | if (ret) |
| 273 | return ret; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 274 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 275 | while (nr--) { |
Josef Bacik | 51bf5f0 | 2013-04-23 12:55:21 -0400 | [diff] [blame] | 276 | u64 start, len; |
| 277 | |
| 278 | if (logical[nr] > cache->key.objectid + |
| 279 | cache->key.offset) |
| 280 | continue; |
| 281 | |
| 282 | if (logical[nr] + stripe_len <= cache->key.objectid) |
| 283 | continue; |
| 284 | |
| 285 | start = logical[nr]; |
| 286 | if (start < cache->key.objectid) { |
| 287 | start = cache->key.objectid; |
| 288 | len = (logical[nr] + stripe_len) - start; |
| 289 | } else { |
| 290 | len = min_t(u64, stripe_len, |
| 291 | cache->key.objectid + |
| 292 | cache->key.offset - start); |
| 293 | } |
| 294 | |
| 295 | cache->bytes_super += len; |
| 296 | ret = add_excluded_extent(root, start, len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 297 | if (ret) { |
| 298 | kfree(logical); |
| 299 | return ret; |
| 300 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 301 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 302 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 303 | kfree(logical); |
| 304 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 308 | static struct btrfs_caching_control * |
| 309 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 310 | { |
| 311 | struct btrfs_caching_control *ctl; |
| 312 | |
| 313 | spin_lock(&cache->lock); |
| 314 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 315 | spin_unlock(&cache->lock); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 319 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 320 | if (!cache->caching_ctl) { |
| 321 | spin_unlock(&cache->lock); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 325 | ctl = cache->caching_ctl; |
| 326 | atomic_inc(&ctl->count); |
| 327 | spin_unlock(&cache->lock); |
| 328 | return ctl; |
| 329 | } |
| 330 | |
| 331 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 332 | { |
| 333 | if (atomic_dec_and_test(&ctl->count)) |
| 334 | kfree(ctl); |
| 335 | } |
| 336 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 337 | /* |
| 338 | * this is only called by cache_block_group, since we could have freed extents |
| 339 | * we need to check the pinned_extents for any extents that can't be used yet |
| 340 | * since their free space will be released as soon as the transaction commits. |
| 341 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 342 | 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] | 343 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 344 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 345 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 346 | int ret; |
| 347 | |
| 348 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 349 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 350 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 351 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 352 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 353 | if (ret) |
| 354 | break; |
| 355 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 356 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 357 | start = extent_end + 1; |
| 358 | } else if (extent_start > start && extent_start < end) { |
| 359 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 360 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 361 | ret = btrfs_add_free_space(block_group, start, |
| 362 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 363 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 364 | start = extent_end + 1; |
| 365 | } else { |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (start < end) { |
| 371 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 372 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 373 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 374 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 375 | } |
| 376 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 377 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 380 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 381 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 382 | struct btrfs_block_group_cache *block_group; |
| 383 | struct btrfs_fs_info *fs_info; |
| 384 | struct btrfs_caching_control *caching_ctl; |
| 385 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 386 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 387 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 388 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 389 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 390 | u64 last = 0; |
| 391 | u32 nritems; |
| 392 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 393 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 394 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 395 | block_group = caching_ctl->block_group; |
| 396 | fs_info = block_group->fs_info; |
| 397 | extent_root = fs_info->extent_root; |
| 398 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 399 | path = btrfs_alloc_path(); |
| 400 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 401 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 402 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 403 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 404 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 405 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 406 | * We don't want to deadlock with somebody trying to allocate a new |
| 407 | * extent for the extent root while also trying to search the extent |
| 408 | * root to add free space. So we skip locking and search the commit |
| 409 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 410 | */ |
| 411 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 412 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 413 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 414 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 415 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 416 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 417 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 418 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 419 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 420 | /* need to make sure the commit_root doesn't disappear */ |
| 421 | down_read(&fs_info->extent_commit_sem); |
| 422 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 423 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 424 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 425 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 426 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 427 | leaf = path->nodes[0]; |
| 428 | nritems = btrfs_header_nritems(leaf); |
| 429 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 430 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 431 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 432 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 433 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 434 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 435 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 436 | if (path->slots[0] < nritems) { |
| 437 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 438 | } else { |
| 439 | ret = find_next_key(path, 0, &key); |
| 440 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 441 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 442 | |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 443 | if (need_resched()) { |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 444 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 445 | btrfs_release_path(path); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 446 | up_read(&fs_info->extent_commit_sem); |
| 447 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 448 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 449 | goto again; |
| 450 | } |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 451 | |
| 452 | ret = btrfs_next_leaf(extent_root, path); |
| 453 | if (ret < 0) |
| 454 | goto err; |
| 455 | if (ret) |
| 456 | break; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 457 | leaf = path->nodes[0]; |
| 458 | nritems = btrfs_header_nritems(leaf); |
| 459 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 460 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 461 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 462 | if (key.objectid < block_group->key.objectid) { |
| 463 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 464 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 465 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 466 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 467 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 468 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 469 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 470 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 471 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 472 | key.type == BTRFS_METADATA_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 473 | total_found += add_new_free_space(block_group, |
| 474 | fs_info, last, |
| 475 | key.objectid); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 476 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 477 | last = key.objectid + |
| 478 | fs_info->tree_root->leafsize; |
| 479 | else |
| 480 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 481 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 482 | if (total_found > (1024 * 1024 * 2)) { |
| 483 | total_found = 0; |
| 484 | wake_up(&caching_ctl->wait); |
| 485 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 486 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 487 | path->slots[0]++; |
| 488 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 489 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 490 | |
| 491 | total_found += add_new_free_space(block_group, fs_info, last, |
| 492 | block_group->key.objectid + |
| 493 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 494 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 495 | |
| 496 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 497 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 498 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 499 | spin_unlock(&block_group->lock); |
| 500 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 501 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 502 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 503 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 504 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 505 | free_excluded_extents(extent_root, block_group); |
| 506 | |
| 507 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 508 | out: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 509 | wake_up(&caching_ctl->wait); |
| 510 | |
| 511 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 512 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 515 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 516 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 517 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 518 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 519 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 520 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 521 | int ret = 0; |
| 522 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 523 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 524 | if (!caching_ctl) |
| 525 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 526 | |
| 527 | INIT_LIST_HEAD(&caching_ctl->list); |
| 528 | mutex_init(&caching_ctl->mutex); |
| 529 | init_waitqueue_head(&caching_ctl->wait); |
| 530 | caching_ctl->block_group = cache; |
| 531 | caching_ctl->progress = cache->key.objectid; |
| 532 | atomic_set(&caching_ctl->count, 1); |
| 533 | caching_ctl->work.func = caching_thread; |
| 534 | |
| 535 | spin_lock(&cache->lock); |
| 536 | /* |
| 537 | * This should be a rare occasion, but this could happen I think in the |
| 538 | * case where one thread starts to load the space cache info, and then |
| 539 | * some other thread starts a transaction commit which tries to do an |
| 540 | * allocation while the other thread is still loading the space cache |
| 541 | * info. The previous loop should have kept us from choosing this block |
| 542 | * group, but if we've moved to the state where we will wait on caching |
| 543 | * block groups we need to first check if we're doing a fast load here, |
| 544 | * so we can wait for it to finish, otherwise we could end up allocating |
| 545 | * from a block group who's cache gets evicted for one reason or |
| 546 | * another. |
| 547 | */ |
| 548 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 549 | struct btrfs_caching_control *ctl; |
| 550 | |
| 551 | ctl = cache->caching_ctl; |
| 552 | atomic_inc(&ctl->count); |
| 553 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 554 | spin_unlock(&cache->lock); |
| 555 | |
| 556 | schedule(); |
| 557 | |
| 558 | finish_wait(&ctl->wait, &wait); |
| 559 | put_caching_control(ctl); |
| 560 | spin_lock(&cache->lock); |
| 561 | } |
| 562 | |
| 563 | if (cache->cached != BTRFS_CACHE_NO) { |
| 564 | spin_unlock(&cache->lock); |
| 565 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 566 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 567 | } |
| 568 | WARN_ON(cache->caching_ctl); |
| 569 | cache->caching_ctl = caching_ctl; |
| 570 | cache->cached = BTRFS_CACHE_FAST; |
| 571 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 572 | |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 573 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 574 | ret = load_free_space_cache(fs_info, cache); |
| 575 | |
| 576 | spin_lock(&cache->lock); |
| 577 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 578 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 579 | cache->cached = BTRFS_CACHE_FINISHED; |
| 580 | cache->last_byte_to_unpin = (u64)-1; |
| 581 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 582 | if (load_cache_only) { |
| 583 | cache->caching_ctl = NULL; |
| 584 | cache->cached = BTRFS_CACHE_NO; |
| 585 | } else { |
| 586 | cache->cached = BTRFS_CACHE_STARTED; |
| 587 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 588 | } |
| 589 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 590 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 591 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 592 | put_caching_control(caching_ctl); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 593 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 594 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 595 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 596 | } else { |
| 597 | /* |
| 598 | * We are not going to do the fast caching, set cached to the |
| 599 | * appropriate value and wakeup any waiters. |
| 600 | */ |
| 601 | spin_lock(&cache->lock); |
| 602 | if (load_cache_only) { |
| 603 | cache->caching_ctl = NULL; |
| 604 | cache->cached = BTRFS_CACHE_NO; |
| 605 | } else { |
| 606 | cache->cached = BTRFS_CACHE_STARTED; |
| 607 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 608 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 609 | wake_up(&caching_ctl->wait); |
| 610 | } |
| 611 | |
| 612 | if (load_cache_only) { |
| 613 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 614 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 615 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 616 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 617 | down_write(&fs_info->extent_commit_sem); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 618 | atomic_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 619 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 620 | up_write(&fs_info->extent_commit_sem); |
| 621 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 622 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 623 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 624 | btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 625 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 626 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 629 | /* |
| 630 | * return the block group that starts at or after bytenr |
| 631 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 632 | static struct btrfs_block_group_cache * |
| 633 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 634 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 635 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 636 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 637 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 638 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 639 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 642 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 643 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 644 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 645 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 646 | struct btrfs_fs_info *info, |
| 647 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 648 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 649 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 650 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 651 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 652 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 653 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 654 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 655 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 656 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 657 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 658 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 659 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 660 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 661 | |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 662 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 663 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 664 | rcu_read_lock(); |
| 665 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 666 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 667 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 668 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 669 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 670 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 671 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 672 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 673 | } |
| 674 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 675 | /* |
| 676 | * after adding space to the filesystem, we need to clear the full flags |
| 677 | * on all the space infos. |
| 678 | */ |
| 679 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 680 | { |
| 681 | struct list_head *head = &info->space_info; |
| 682 | struct btrfs_space_info *found; |
| 683 | |
| 684 | rcu_read_lock(); |
| 685 | list_for_each_entry_rcu(found, head, list) |
| 686 | found->full = 0; |
| 687 | rcu_read_unlock(); |
| 688 | } |
| 689 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 690 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 691 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 692 | { |
| 693 | int ret; |
| 694 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 695 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 696 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 697 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 698 | if (!path) |
| 699 | return -ENOMEM; |
| 700 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 701 | key.objectid = start; |
| 702 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 703 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 704 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 705 | 0, 0); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 706 | if (ret > 0) { |
| 707 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 708 | if (key.objectid == start && |
| 709 | key.type == BTRFS_METADATA_ITEM_KEY) |
| 710 | ret = 0; |
| 711 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 712 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 713 | return ret; |
| 714 | } |
| 715 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 716 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 717 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 718 | * |
| 719 | * the head node for delayed ref is used to store the sum of all the |
| 720 | * reference count modifications queued up in the rbtree. the head |
| 721 | * node may also store the extent flags to set. This way you can check |
| 722 | * to see what the reference count and extent flags would be if all of |
| 723 | * the delayed refs are not processed. |
| 724 | */ |
| 725 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 726 | struct btrfs_root *root, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 727 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 728 | { |
| 729 | struct btrfs_delayed_ref_head *head; |
| 730 | struct btrfs_delayed_ref_root *delayed_refs; |
| 731 | struct btrfs_path *path; |
| 732 | struct btrfs_extent_item *ei; |
| 733 | struct extent_buffer *leaf; |
| 734 | struct btrfs_key key; |
| 735 | u32 item_size; |
| 736 | u64 num_refs; |
| 737 | u64 extent_flags; |
| 738 | int ret; |
| 739 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 740 | /* |
| 741 | * If we don't have skinny metadata, don't bother doing anything |
| 742 | * different |
| 743 | */ |
| 744 | if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) { |
| 745 | offset = root->leafsize; |
| 746 | metadata = 0; |
| 747 | } |
| 748 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 749 | path = btrfs_alloc_path(); |
| 750 | if (!path) |
| 751 | return -ENOMEM; |
| 752 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 753 | if (metadata) { |
| 754 | key.objectid = bytenr; |
| 755 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 756 | key.offset = offset; |
| 757 | } else { |
| 758 | key.objectid = bytenr; |
| 759 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 760 | key.offset = offset; |
| 761 | } |
| 762 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 763 | if (!trans) { |
| 764 | path->skip_locking = 1; |
| 765 | path->search_commit_root = 1; |
| 766 | } |
| 767 | again: |
| 768 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 769 | &key, path, 0, 0); |
| 770 | if (ret < 0) |
| 771 | goto out_free; |
| 772 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 773 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
| 774 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 775 | key.offset = root->leafsize; |
| 776 | btrfs_release_path(path); |
| 777 | goto again; |
| 778 | } |
| 779 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 780 | if (ret == 0) { |
| 781 | leaf = path->nodes[0]; |
| 782 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 783 | if (item_size >= sizeof(*ei)) { |
| 784 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 785 | struct btrfs_extent_item); |
| 786 | num_refs = btrfs_extent_refs(leaf, ei); |
| 787 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 788 | } else { |
| 789 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 790 | struct btrfs_extent_item_v0 *ei0; |
| 791 | BUG_ON(item_size != sizeof(*ei0)); |
| 792 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 793 | struct btrfs_extent_item_v0); |
| 794 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 795 | /* FIXME: this isn't correct for data */ |
| 796 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 797 | #else |
| 798 | BUG(); |
| 799 | #endif |
| 800 | } |
| 801 | BUG_ON(num_refs == 0); |
| 802 | } else { |
| 803 | num_refs = 0; |
| 804 | extent_flags = 0; |
| 805 | ret = 0; |
| 806 | } |
| 807 | |
| 808 | if (!trans) |
| 809 | goto out; |
| 810 | |
| 811 | delayed_refs = &trans->transaction->delayed_refs; |
| 812 | spin_lock(&delayed_refs->lock); |
| 813 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 814 | if (head) { |
| 815 | if (!mutex_trylock(&head->mutex)) { |
| 816 | atomic_inc(&head->node.refs); |
| 817 | spin_unlock(&delayed_refs->lock); |
| 818 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 819 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 820 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 821 | /* |
| 822 | * Mutex was contended, block until it's released and try |
| 823 | * again |
| 824 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 825 | mutex_lock(&head->mutex); |
| 826 | mutex_unlock(&head->mutex); |
| 827 | btrfs_put_delayed_ref(&head->node); |
| 828 | goto again; |
| 829 | } |
| 830 | if (head->extent_op && head->extent_op->update_flags) |
| 831 | extent_flags |= head->extent_op->flags_to_set; |
| 832 | else |
| 833 | BUG_ON(num_refs == 0); |
| 834 | |
| 835 | num_refs += head->node.ref_mod; |
| 836 | mutex_unlock(&head->mutex); |
| 837 | } |
| 838 | spin_unlock(&delayed_refs->lock); |
| 839 | out: |
| 840 | WARN_ON(num_refs == 0); |
| 841 | if (refs) |
| 842 | *refs = num_refs; |
| 843 | if (flags) |
| 844 | *flags = extent_flags; |
| 845 | out_free: |
| 846 | btrfs_free_path(path); |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 851 | * Back reference rules. Back refs have three main goals: |
| 852 | * |
| 853 | * 1) differentiate between all holders of references to an extent so that |
| 854 | * when a reference is dropped we can make sure it was a valid reference |
| 855 | * before freeing the extent. |
| 856 | * |
| 857 | * 2) Provide enough information to quickly find the holders of an extent |
| 858 | * if we notice a given block is corrupted or bad. |
| 859 | * |
| 860 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 861 | * maintenance. This is actually the same as #2, but with a slightly |
| 862 | * different use case. |
| 863 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 864 | * There are two kinds of back refs. The implicit back refs is optimized |
| 865 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 866 | * back refs of this kind provide information about the block's owner tree |
| 867 | * and the pointer's key. These information allow us to find the block by |
| 868 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 869 | * referenced by their owner trees. The location of tree block is recorded |
| 870 | * in the back refs. Actually the full back refs is generic, and can be |
| 871 | * used in all cases the implicit back refs is used. The major shortcoming |
| 872 | * of the full back refs is its overhead. Every time a tree block gets |
| 873 | * COWed, we have to update back refs entry for all pointers in it. |
| 874 | * |
| 875 | * For a newly allocated tree block, we use implicit back refs for |
| 876 | * pointers in it. This means most tree related operations only involve |
| 877 | * implicit back refs. For a tree block created in old transaction, the |
| 878 | * only way to drop a reference to it is COW it. So we can detect the |
| 879 | * event that tree block loses its owner tree's reference and do the |
| 880 | * back refs conversion. |
| 881 | * |
| 882 | * When a tree block is COW'd through a tree, there are four cases: |
| 883 | * |
| 884 | * The reference count of the block is one and the tree is the block's |
| 885 | * owner tree. Nothing to do in this case. |
| 886 | * |
| 887 | * The reference count of the block is one and the tree is not the |
| 888 | * block's owner tree. In this case, full back refs is used for pointers |
| 889 | * in the block. Remove these full back refs, add implicit back refs for |
| 890 | * every pointers in the new block. |
| 891 | * |
| 892 | * The reference count of the block is greater than one and the tree is |
| 893 | * the block's owner tree. In this case, implicit back refs is used for |
| 894 | * pointers in the block. Add full back refs for every pointers in the |
| 895 | * block, increase lower level extents' reference counts. The original |
| 896 | * implicit back refs are entailed to the new block. |
| 897 | * |
| 898 | * The reference count of the block is greater than one and the tree is |
| 899 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 900 | * the new block, increase lower level extents' reference count. |
| 901 | * |
| 902 | * Back Reference Key composing: |
| 903 | * |
| 904 | * The key objectid corresponds to the first byte in the extent, |
| 905 | * The key type is used to differentiate between types of back refs. |
| 906 | * There are different meanings of the key offset for different types |
| 907 | * of back refs. |
| 908 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 909 | * File extents can be referenced by: |
| 910 | * |
| 911 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 912 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 913 | * - different offsets inside a file (bookend extents in file.c) |
| 914 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 915 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 916 | * |
| 917 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 918 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 919 | * - original offset in the file |
| 920 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 921 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 922 | * The key offset for the implicit back refs is hash of the first |
| 923 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 924 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 925 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 926 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 927 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 928 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 929 | * The key offset for the implicit back refs is the first byte of |
| 930 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 931 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 932 | * When a file extent is allocated, The implicit back refs is used. |
| 933 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 934 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 935 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 936 | * |
| 937 | * When a file extent is removed file truncation, we find the |
| 938 | * corresponding implicit back refs and check the following fields: |
| 939 | * |
| 940 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 941 | * |
| 942 | * Btree extents can be referenced by: |
| 943 | * |
| 944 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 945 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 946 | * Both the implicit back refs and the full back refs for tree blocks |
| 947 | * only consist of key. The key offset for the implicit back refs is |
| 948 | * objectid of block's owner tree. The key offset for the full back refs |
| 949 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 950 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 951 | * When implicit back refs is used, information about the lowest key and |
| 952 | * level of the tree block are required. These information are stored in |
| 953 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 954 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 955 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 956 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 957 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 958 | struct btrfs_root *root, |
| 959 | struct btrfs_path *path, |
| 960 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 961 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 962 | struct btrfs_extent_item *item; |
| 963 | struct btrfs_extent_item_v0 *ei0; |
| 964 | struct btrfs_extent_ref_v0 *ref0; |
| 965 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 966 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 967 | struct btrfs_key key; |
| 968 | struct btrfs_key found_key; |
| 969 | u32 new_size = sizeof(*item); |
| 970 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 971 | int ret; |
| 972 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 973 | leaf = path->nodes[0]; |
| 974 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 975 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 976 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 977 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 978 | struct btrfs_extent_item_v0); |
| 979 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 980 | |
| 981 | if (owner == (u64)-1) { |
| 982 | while (1) { |
| 983 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 984 | ret = btrfs_next_leaf(root, path); |
| 985 | if (ret < 0) |
| 986 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 987 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 988 | leaf = path->nodes[0]; |
| 989 | } |
| 990 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 991 | path->slots[0]); |
| 992 | BUG_ON(key.objectid != found_key.objectid); |
| 993 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 994 | path->slots[0]++; |
| 995 | continue; |
| 996 | } |
| 997 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 998 | struct btrfs_extent_ref_v0); |
| 999 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1003 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1004 | |
| 1005 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 1006 | new_size += sizeof(*bi); |
| 1007 | |
| 1008 | new_size -= sizeof(*ei0); |
| 1009 | ret = btrfs_search_slot(trans, root, &key, path, |
| 1010 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1011 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1012 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1013 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1014 | |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 1015 | btrfs_extend_item(root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1016 | |
| 1017 | leaf = path->nodes[0]; |
| 1018 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1019 | btrfs_set_extent_refs(leaf, item, refs); |
| 1020 | /* FIXME: get real generation */ |
| 1021 | btrfs_set_extent_generation(leaf, item, 0); |
| 1022 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1023 | btrfs_set_extent_flags(leaf, item, |
| 1024 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1025 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1026 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1027 | /* FIXME: get first key of the block */ |
| 1028 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 1029 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1030 | } else { |
| 1031 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1032 | } |
| 1033 | btrfs_mark_buffer_dirty(leaf); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | #endif |
| 1037 | |
| 1038 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1039 | { |
| 1040 | u32 high_crc = ~(u32)0; |
| 1041 | u32 low_crc = ~(u32)0; |
| 1042 | __le64 lenum; |
| 1043 | |
| 1044 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1045 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1046 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1047 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1048 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1049 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1050 | |
| 1051 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1052 | } |
| 1053 | |
| 1054 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1055 | struct btrfs_extent_data_ref *ref) |
| 1056 | { |
| 1057 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1058 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1059 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1060 | } |
| 1061 | |
| 1062 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1063 | struct btrfs_extent_data_ref *ref, |
| 1064 | u64 root_objectid, u64 owner, u64 offset) |
| 1065 | { |
| 1066 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1067 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1068 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1069 | return 0; |
| 1070 | return 1; |
| 1071 | } |
| 1072 | |
| 1073 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1074 | struct btrfs_root *root, |
| 1075 | struct btrfs_path *path, |
| 1076 | u64 bytenr, u64 parent, |
| 1077 | u64 root_objectid, |
| 1078 | u64 owner, u64 offset) |
| 1079 | { |
| 1080 | struct btrfs_key key; |
| 1081 | struct btrfs_extent_data_ref *ref; |
| 1082 | struct extent_buffer *leaf; |
| 1083 | u32 nritems; |
| 1084 | int ret; |
| 1085 | int recow; |
| 1086 | int err = -ENOENT; |
| 1087 | |
| 1088 | key.objectid = bytenr; |
| 1089 | if (parent) { |
| 1090 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1091 | key.offset = parent; |
| 1092 | } else { |
| 1093 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1094 | key.offset = hash_extent_data_ref(root_objectid, |
| 1095 | owner, offset); |
| 1096 | } |
| 1097 | again: |
| 1098 | recow = 0; |
| 1099 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1100 | if (ret < 0) { |
| 1101 | err = ret; |
| 1102 | goto fail; |
| 1103 | } |
| 1104 | |
| 1105 | if (parent) { |
| 1106 | if (!ret) |
| 1107 | return 0; |
| 1108 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1109 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1110 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1111 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1112 | if (ret < 0) { |
| 1113 | err = ret; |
| 1114 | goto fail; |
| 1115 | } |
| 1116 | if (!ret) |
| 1117 | return 0; |
| 1118 | #endif |
| 1119 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1123 | nritems = btrfs_header_nritems(leaf); |
| 1124 | while (1) { |
| 1125 | if (path->slots[0] >= nritems) { |
| 1126 | ret = btrfs_next_leaf(root, path); |
| 1127 | if (ret < 0) |
| 1128 | err = ret; |
| 1129 | if (ret) |
| 1130 | goto fail; |
| 1131 | |
| 1132 | leaf = path->nodes[0]; |
| 1133 | nritems = btrfs_header_nritems(leaf); |
| 1134 | recow = 1; |
| 1135 | } |
| 1136 | |
| 1137 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1138 | if (key.objectid != bytenr || |
| 1139 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1140 | goto fail; |
| 1141 | |
| 1142 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1143 | struct btrfs_extent_data_ref); |
| 1144 | |
| 1145 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1146 | owner, offset)) { |
| 1147 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1148 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1149 | goto again; |
| 1150 | } |
| 1151 | err = 0; |
| 1152 | break; |
| 1153 | } |
| 1154 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1155 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1156 | fail: |
| 1157 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1158 | } |
| 1159 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1160 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1161 | struct btrfs_root *root, |
| 1162 | struct btrfs_path *path, |
| 1163 | u64 bytenr, u64 parent, |
| 1164 | u64 root_objectid, u64 owner, |
| 1165 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1166 | { |
| 1167 | struct btrfs_key key; |
| 1168 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1169 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1170 | u32 num_refs; |
| 1171 | int ret; |
| 1172 | |
| 1173 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1174 | if (parent) { |
| 1175 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1176 | key.offset = parent; |
| 1177 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1178 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1179 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1180 | key.offset = hash_extent_data_ref(root_objectid, |
| 1181 | owner, offset); |
| 1182 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1183 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1184 | |
| 1185 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1186 | if (ret && ret != -EEXIST) |
| 1187 | goto fail; |
| 1188 | |
| 1189 | leaf = path->nodes[0]; |
| 1190 | if (parent) { |
| 1191 | struct btrfs_shared_data_ref *ref; |
| 1192 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1193 | struct btrfs_shared_data_ref); |
| 1194 | if (ret == 0) { |
| 1195 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1196 | } else { |
| 1197 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1198 | num_refs += refs_to_add; |
| 1199 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1200 | } |
| 1201 | } else { |
| 1202 | struct btrfs_extent_data_ref *ref; |
| 1203 | while (ret == -EEXIST) { |
| 1204 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1205 | struct btrfs_extent_data_ref); |
| 1206 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1207 | owner, offset)) |
| 1208 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1209 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1210 | key.offset++; |
| 1211 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1212 | size); |
| 1213 | if (ret && ret != -EEXIST) |
| 1214 | goto fail; |
| 1215 | |
| 1216 | leaf = path->nodes[0]; |
| 1217 | } |
| 1218 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1219 | struct btrfs_extent_data_ref); |
| 1220 | if (ret == 0) { |
| 1221 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1222 | root_objectid); |
| 1223 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1224 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1225 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1226 | } else { |
| 1227 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1228 | num_refs += refs_to_add; |
| 1229 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1230 | } |
| 1231 | } |
| 1232 | btrfs_mark_buffer_dirty(leaf); |
| 1233 | ret = 0; |
| 1234 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1235 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1236 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1237 | } |
| 1238 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1239 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1240 | struct btrfs_root *root, |
| 1241 | struct btrfs_path *path, |
| 1242 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1243 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1244 | struct btrfs_key key; |
| 1245 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1246 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1247 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1248 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1249 | int ret = 0; |
| 1250 | |
| 1251 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1252 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1253 | |
| 1254 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1255 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1256 | struct btrfs_extent_data_ref); |
| 1257 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1258 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1259 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1260 | struct btrfs_shared_data_ref); |
| 1261 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1262 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1263 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1264 | struct btrfs_extent_ref_v0 *ref0; |
| 1265 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1266 | struct btrfs_extent_ref_v0); |
| 1267 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1268 | #endif |
| 1269 | } else { |
| 1270 | BUG(); |
| 1271 | } |
| 1272 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1273 | BUG_ON(num_refs < refs_to_drop); |
| 1274 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1275 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1276 | if (num_refs == 0) { |
| 1277 | ret = btrfs_del_item(trans, root, path); |
| 1278 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1279 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1280 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1281 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1282 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1283 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1284 | else { |
| 1285 | struct btrfs_extent_ref_v0 *ref0; |
| 1286 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1287 | struct btrfs_extent_ref_v0); |
| 1288 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1289 | } |
| 1290 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1291 | btrfs_mark_buffer_dirty(leaf); |
| 1292 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1293 | return ret; |
| 1294 | } |
| 1295 | |
| 1296 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1297 | struct btrfs_path *path, |
| 1298 | struct btrfs_extent_inline_ref *iref) |
| 1299 | { |
| 1300 | struct btrfs_key key; |
| 1301 | struct extent_buffer *leaf; |
| 1302 | struct btrfs_extent_data_ref *ref1; |
| 1303 | struct btrfs_shared_data_ref *ref2; |
| 1304 | u32 num_refs = 0; |
| 1305 | |
| 1306 | leaf = path->nodes[0]; |
| 1307 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1308 | if (iref) { |
| 1309 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1310 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1311 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1312 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1313 | } else { |
| 1314 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1315 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1316 | } |
| 1317 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1318 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1319 | struct btrfs_extent_data_ref); |
| 1320 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1321 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1322 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1323 | struct btrfs_shared_data_ref); |
| 1324 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1325 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1326 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1327 | struct btrfs_extent_ref_v0 *ref0; |
| 1328 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1329 | struct btrfs_extent_ref_v0); |
| 1330 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1331 | #endif |
| 1332 | } else { |
| 1333 | WARN_ON(1); |
| 1334 | } |
| 1335 | return num_refs; |
| 1336 | } |
| 1337 | |
| 1338 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1339 | struct btrfs_root *root, |
| 1340 | struct btrfs_path *path, |
| 1341 | u64 bytenr, u64 parent, |
| 1342 | u64 root_objectid) |
| 1343 | { |
| 1344 | struct btrfs_key key; |
| 1345 | int ret; |
| 1346 | |
| 1347 | key.objectid = bytenr; |
| 1348 | if (parent) { |
| 1349 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1350 | key.offset = parent; |
| 1351 | } else { |
| 1352 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1353 | key.offset = root_objectid; |
| 1354 | } |
| 1355 | |
| 1356 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1357 | if (ret > 0) |
| 1358 | ret = -ENOENT; |
| 1359 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1360 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1361 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1362 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1363 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1364 | if (ret > 0) |
| 1365 | ret = -ENOENT; |
| 1366 | } |
| 1367 | #endif |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
| 1371 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1372 | struct btrfs_root *root, |
| 1373 | struct btrfs_path *path, |
| 1374 | u64 bytenr, u64 parent, |
| 1375 | u64 root_objectid) |
| 1376 | { |
| 1377 | struct btrfs_key key; |
| 1378 | int ret; |
| 1379 | |
| 1380 | key.objectid = bytenr; |
| 1381 | if (parent) { |
| 1382 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1383 | key.offset = parent; |
| 1384 | } else { |
| 1385 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1386 | key.offset = root_objectid; |
| 1387 | } |
| 1388 | |
| 1389 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1390 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1391 | return ret; |
| 1392 | } |
| 1393 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1394 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1395 | { |
| 1396 | int type; |
| 1397 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1398 | if (parent > 0) |
| 1399 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1400 | else |
| 1401 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1402 | } else { |
| 1403 | if (parent > 0) |
| 1404 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1405 | else |
| 1406 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1407 | } |
| 1408 | return type; |
| 1409 | } |
| 1410 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1411 | static int find_next_key(struct btrfs_path *path, int level, |
| 1412 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1413 | |
| 1414 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1415 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1416 | if (!path->nodes[level]) |
| 1417 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1418 | if (path->slots[level] + 1 >= |
| 1419 | btrfs_header_nritems(path->nodes[level])) |
| 1420 | continue; |
| 1421 | if (level == 0) |
| 1422 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1423 | path->slots[level] + 1); |
| 1424 | else |
| 1425 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1426 | path->slots[level] + 1); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1434 | * to the address of inline back ref, and 0 is returned. |
| 1435 | * |
| 1436 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1437 | * should be inserted, and -ENOENT is returned. |
| 1438 | * |
| 1439 | * if insert is true and there are too many inline back refs, the path |
| 1440 | * points to the extent item, and -EAGAIN is returned. |
| 1441 | * |
| 1442 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1443 | * items in the tree are ordered. |
| 1444 | */ |
| 1445 | static noinline_for_stack |
| 1446 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1447 | struct btrfs_root *root, |
| 1448 | struct btrfs_path *path, |
| 1449 | struct btrfs_extent_inline_ref **ref_ret, |
| 1450 | u64 bytenr, u64 num_bytes, |
| 1451 | u64 parent, u64 root_objectid, |
| 1452 | u64 owner, u64 offset, int insert) |
| 1453 | { |
| 1454 | struct btrfs_key key; |
| 1455 | struct extent_buffer *leaf; |
| 1456 | struct btrfs_extent_item *ei; |
| 1457 | struct btrfs_extent_inline_ref *iref; |
| 1458 | u64 flags; |
| 1459 | u64 item_size; |
| 1460 | unsigned long ptr; |
| 1461 | unsigned long end; |
| 1462 | int extra_size; |
| 1463 | int type; |
| 1464 | int want; |
| 1465 | int ret; |
| 1466 | int err = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1467 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 1468 | SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1469 | |
| 1470 | key.objectid = bytenr; |
| 1471 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1472 | key.offset = num_bytes; |
| 1473 | |
| 1474 | want = extent_ref_type(parent, owner); |
| 1475 | if (insert) { |
| 1476 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1477 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1478 | } else |
| 1479 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1480 | |
| 1481 | /* |
| 1482 | * Owner is our parent level, so we can just add one to get the level |
| 1483 | * for the block we are interested in. |
| 1484 | */ |
| 1485 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1486 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1487 | key.offset = owner; |
| 1488 | } |
| 1489 | |
| 1490 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1491 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1492 | if (ret < 0) { |
| 1493 | err = ret; |
| 1494 | goto out; |
| 1495 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1496 | |
| 1497 | /* |
| 1498 | * We may be a newly converted file system which still has the old fat |
| 1499 | * extent entries for metadata, so try and see if we have one of those. |
| 1500 | */ |
| 1501 | if (ret > 0 && skinny_metadata) { |
| 1502 | skinny_metadata = false; |
| 1503 | if (path->slots[0]) { |
| 1504 | path->slots[0]--; |
| 1505 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1506 | path->slots[0]); |
| 1507 | if (key.objectid == bytenr && |
| 1508 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1509 | key.offset == num_bytes) |
| 1510 | ret = 0; |
| 1511 | } |
| 1512 | if (ret) { |
| 1513 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1514 | key.offset = num_bytes; |
| 1515 | btrfs_release_path(path); |
| 1516 | goto again; |
| 1517 | } |
| 1518 | } |
| 1519 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1520 | if (ret && !insert) { |
| 1521 | err = -ENOENT; |
| 1522 | goto out; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1523 | } else if (ret) { |
| 1524 | err = -EIO; |
| 1525 | WARN_ON(1); |
| 1526 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1527 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1528 | |
| 1529 | leaf = path->nodes[0]; |
| 1530 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1531 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1532 | if (item_size < sizeof(*ei)) { |
| 1533 | if (!insert) { |
| 1534 | err = -ENOENT; |
| 1535 | goto out; |
| 1536 | } |
| 1537 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1538 | extra_size); |
| 1539 | if (ret < 0) { |
| 1540 | err = ret; |
| 1541 | goto out; |
| 1542 | } |
| 1543 | leaf = path->nodes[0]; |
| 1544 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1545 | } |
| 1546 | #endif |
| 1547 | BUG_ON(item_size < sizeof(*ei)); |
| 1548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1549 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1550 | flags = btrfs_extent_flags(leaf, ei); |
| 1551 | |
| 1552 | ptr = (unsigned long)(ei + 1); |
| 1553 | end = (unsigned long)ei + item_size; |
| 1554 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1555 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1556 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1557 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | err = -ENOENT; |
| 1561 | while (1) { |
| 1562 | if (ptr >= end) { |
| 1563 | WARN_ON(ptr > end); |
| 1564 | break; |
| 1565 | } |
| 1566 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1567 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1568 | if (want < type) |
| 1569 | break; |
| 1570 | if (want > type) { |
| 1571 | ptr += btrfs_extent_inline_ref_size(type); |
| 1572 | continue; |
| 1573 | } |
| 1574 | |
| 1575 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1576 | struct btrfs_extent_data_ref *dref; |
| 1577 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1578 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1579 | owner, offset)) { |
| 1580 | err = 0; |
| 1581 | break; |
| 1582 | } |
| 1583 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1584 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1585 | break; |
| 1586 | } else { |
| 1587 | u64 ref_offset; |
| 1588 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1589 | if (parent > 0) { |
| 1590 | if (parent == ref_offset) { |
| 1591 | err = 0; |
| 1592 | break; |
| 1593 | } |
| 1594 | if (ref_offset < parent) |
| 1595 | break; |
| 1596 | } else { |
| 1597 | if (root_objectid == ref_offset) { |
| 1598 | err = 0; |
| 1599 | break; |
| 1600 | } |
| 1601 | if (ref_offset < root_objectid) |
| 1602 | break; |
| 1603 | } |
| 1604 | } |
| 1605 | ptr += btrfs_extent_inline_ref_size(type); |
| 1606 | } |
| 1607 | if (err == -ENOENT && insert) { |
| 1608 | if (item_size + extra_size >= |
| 1609 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1610 | err = -EAGAIN; |
| 1611 | goto out; |
| 1612 | } |
| 1613 | /* |
| 1614 | * To add new inline back ref, we have to make sure |
| 1615 | * there is no corresponding back ref item. |
| 1616 | * For simplicity, we just do not add new inline back |
| 1617 | * ref if there is any kind of item for this block |
| 1618 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1619 | if (find_next_key(path, 0, &key) == 0 && |
| 1620 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1621 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1622 | err = -EAGAIN; |
| 1623 | goto out; |
| 1624 | } |
| 1625 | } |
| 1626 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1627 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1628 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1629 | path->keep_locks = 0; |
| 1630 | btrfs_unlock_up_safe(path, 1); |
| 1631 | } |
| 1632 | return err; |
| 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | * helper to add new inline back ref |
| 1637 | */ |
| 1638 | static noinline_for_stack |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1639 | void setup_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1640 | struct btrfs_path *path, |
| 1641 | struct btrfs_extent_inline_ref *iref, |
| 1642 | u64 parent, u64 root_objectid, |
| 1643 | u64 owner, u64 offset, int refs_to_add, |
| 1644 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1645 | { |
| 1646 | struct extent_buffer *leaf; |
| 1647 | struct btrfs_extent_item *ei; |
| 1648 | unsigned long ptr; |
| 1649 | unsigned long end; |
| 1650 | unsigned long item_offset; |
| 1651 | u64 refs; |
| 1652 | int size; |
| 1653 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1654 | |
| 1655 | leaf = path->nodes[0]; |
| 1656 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1657 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1658 | |
| 1659 | type = extent_ref_type(parent, owner); |
| 1660 | size = btrfs_extent_inline_ref_size(type); |
| 1661 | |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 1662 | btrfs_extend_item(root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1663 | |
| 1664 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1665 | refs = btrfs_extent_refs(leaf, ei); |
| 1666 | refs += refs_to_add; |
| 1667 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1668 | if (extent_op) |
| 1669 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1670 | |
| 1671 | ptr = (unsigned long)ei + item_offset; |
| 1672 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1673 | if (ptr < end - size) |
| 1674 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1675 | end - size - ptr); |
| 1676 | |
| 1677 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1678 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1679 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1680 | struct btrfs_extent_data_ref *dref; |
| 1681 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1682 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1683 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1684 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1685 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1686 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1687 | struct btrfs_shared_data_ref *sref; |
| 1688 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1689 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1690 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1691 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1692 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1693 | } else { |
| 1694 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1695 | } |
| 1696 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1700 | struct btrfs_root *root, |
| 1701 | struct btrfs_path *path, |
| 1702 | struct btrfs_extent_inline_ref **ref_ret, |
| 1703 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1704 | u64 root_objectid, u64 owner, u64 offset) |
| 1705 | { |
| 1706 | int ret; |
| 1707 | |
| 1708 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1709 | bytenr, num_bytes, parent, |
| 1710 | root_objectid, owner, offset, 0); |
| 1711 | if (ret != -ENOENT) |
| 1712 | return ret; |
| 1713 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1714 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1715 | *ref_ret = NULL; |
| 1716 | |
| 1717 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1718 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1719 | root_objectid); |
| 1720 | } else { |
| 1721 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1722 | root_objectid, owner, offset); |
| 1723 | } |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * helper to update/remove inline back ref |
| 1729 | */ |
| 1730 | static noinline_for_stack |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1731 | void update_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1732 | struct btrfs_path *path, |
| 1733 | struct btrfs_extent_inline_ref *iref, |
| 1734 | int refs_to_mod, |
| 1735 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1736 | { |
| 1737 | struct extent_buffer *leaf; |
| 1738 | struct btrfs_extent_item *ei; |
| 1739 | struct btrfs_extent_data_ref *dref = NULL; |
| 1740 | struct btrfs_shared_data_ref *sref = NULL; |
| 1741 | unsigned long ptr; |
| 1742 | unsigned long end; |
| 1743 | u32 item_size; |
| 1744 | int size; |
| 1745 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1746 | u64 refs; |
| 1747 | |
| 1748 | leaf = path->nodes[0]; |
| 1749 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1750 | refs = btrfs_extent_refs(leaf, ei); |
| 1751 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1752 | refs += refs_to_mod; |
| 1753 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1754 | if (extent_op) |
| 1755 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1756 | |
| 1757 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1758 | |
| 1759 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1760 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1761 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1762 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1763 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1764 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1765 | } else { |
| 1766 | refs = 1; |
| 1767 | BUG_ON(refs_to_mod != -1); |
| 1768 | } |
| 1769 | |
| 1770 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1771 | refs += refs_to_mod; |
| 1772 | |
| 1773 | if (refs > 0) { |
| 1774 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1775 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1776 | else |
| 1777 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1778 | } else { |
| 1779 | size = btrfs_extent_inline_ref_size(type); |
| 1780 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1781 | ptr = (unsigned long)iref; |
| 1782 | end = (unsigned long)ei + item_size; |
| 1783 | if (ptr + size < end) |
| 1784 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1785 | end - ptr - size); |
| 1786 | item_size -= size; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1787 | btrfs_truncate_item(root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1788 | } |
| 1789 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static noinline_for_stack |
| 1793 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1794 | struct btrfs_root *root, |
| 1795 | struct btrfs_path *path, |
| 1796 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1797 | u64 root_objectid, u64 owner, |
| 1798 | u64 offset, int refs_to_add, |
| 1799 | struct btrfs_delayed_extent_op *extent_op) |
| 1800 | { |
| 1801 | struct btrfs_extent_inline_ref *iref; |
| 1802 | int ret; |
| 1803 | |
| 1804 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1805 | bytenr, num_bytes, parent, |
| 1806 | root_objectid, owner, offset, 1); |
| 1807 | if (ret == 0) { |
| 1808 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1809 | update_inline_extent_backref(root, path, iref, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1810 | refs_to_add, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1811 | } else if (ret == -ENOENT) { |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1812 | setup_inline_extent_backref(root, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1813 | root_objectid, owner, offset, |
| 1814 | refs_to_add, extent_op); |
| 1815 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1816 | } |
| 1817 | return ret; |
| 1818 | } |
| 1819 | |
| 1820 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1821 | struct btrfs_root *root, |
| 1822 | struct btrfs_path *path, |
| 1823 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1824 | u64 owner, u64 offset, int refs_to_add) |
| 1825 | { |
| 1826 | int ret; |
| 1827 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1828 | BUG_ON(refs_to_add != 1); |
| 1829 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1830 | parent, root_objectid); |
| 1831 | } else { |
| 1832 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1833 | parent, root_objectid, |
| 1834 | owner, offset, refs_to_add); |
| 1835 | } |
| 1836 | return ret; |
| 1837 | } |
| 1838 | |
| 1839 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1840 | struct btrfs_root *root, |
| 1841 | struct btrfs_path *path, |
| 1842 | struct btrfs_extent_inline_ref *iref, |
| 1843 | int refs_to_drop, int is_data) |
| 1844 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1845 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1846 | |
| 1847 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1848 | if (iref) { |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1849 | update_inline_extent_backref(root, path, iref, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1850 | -refs_to_drop, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1851 | } else if (is_data) { |
| 1852 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1853 | } else { |
| 1854 | ret = btrfs_del_item(trans, root, path); |
| 1855 | } |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1859 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1860 | u64 start, u64 len) |
| 1861 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1862 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1863 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1864 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1865 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1866 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1867 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1868 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1869 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1870 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1871 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1872 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1873 | /* Tell the block device(s) that the sectors can be discarded */ |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1874 | ret = btrfs_map_block(root->fs_info, REQ_DISCARD, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1875 | bytenr, &num_bytes, &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1876 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1877 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1878 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1879 | int i; |
| 1880 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1881 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1882 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1883 | if (!stripe->dev->can_discard) |
| 1884 | continue; |
| 1885 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1886 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1887 | stripe->physical, |
| 1888 | stripe->length); |
| 1889 | if (!ret) |
| 1890 | discarded_bytes += stripe->length; |
| 1891 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1892 | 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] | 1893 | |
| 1894 | /* |
| 1895 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1896 | * just ignore the return value so we don't screw up |
| 1897 | * people calling discard_extent. |
| 1898 | */ |
| 1899 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1900 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1901 | kfree(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1902 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1903 | |
| 1904 | if (actual_bytes) |
| 1905 | *actual_bytes = discarded_bytes; |
| 1906 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1907 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1908 | if (ret == -EOPNOTSUPP) |
| 1909 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1910 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1911 | } |
| 1912 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1913 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1914 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1915 | struct btrfs_root *root, |
| 1916 | u64 bytenr, u64 num_bytes, u64 parent, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1917 | u64 root_objectid, u64 owner, u64 offset, int for_cow) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1918 | { |
| 1919 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1920 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1921 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1922 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1923 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1924 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1925 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1926 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 1927 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1928 | parent, root_objectid, (int)owner, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1929 | BTRFS_ADD_DELAYED_REF, NULL, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1930 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1931 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 1932 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1933 | parent, root_objectid, owner, offset, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1934 | BTRFS_ADD_DELAYED_REF, NULL, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1935 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1936 | return ret; |
| 1937 | } |
| 1938 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1939 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1940 | struct btrfs_root *root, |
| 1941 | u64 bytenr, u64 num_bytes, |
| 1942 | u64 parent, u64 root_objectid, |
| 1943 | u64 owner, u64 offset, int refs_to_add, |
| 1944 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1945 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1946 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1947 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1948 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1949 | u64 refs; |
| 1950 | int ret; |
| 1951 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1952 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1953 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1954 | if (!path) |
| 1955 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1956 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1957 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1958 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1959 | /* this will setup the path even if it fails to insert the back ref */ |
| 1960 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1961 | path, bytenr, num_bytes, parent, |
| 1962 | root_objectid, owner, offset, |
| 1963 | refs_to_add, extent_op); |
| 1964 | if (ret == 0) |
| 1965 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1966 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1967 | if (ret != -EAGAIN) { |
| 1968 | err = ret; |
| 1969 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1970 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1971 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1972 | leaf = path->nodes[0]; |
| 1973 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1974 | refs = btrfs_extent_refs(leaf, item); |
| 1975 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1976 | if (extent_op) |
| 1977 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1978 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1979 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1980 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1981 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1982 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1983 | path->leave_spinning = 1; |
| 1984 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1985 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1986 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1987 | path, bytenr, parent, root_objectid, |
| 1988 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1989 | if (ret) |
| 1990 | btrfs_abort_transaction(trans, root, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1991 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1992 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1993 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1994 | } |
| 1995 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1996 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1997 | struct btrfs_root *root, |
| 1998 | struct btrfs_delayed_ref_node *node, |
| 1999 | struct btrfs_delayed_extent_op *extent_op, |
| 2000 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2001 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2002 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2003 | struct btrfs_delayed_data_ref *ref; |
| 2004 | struct btrfs_key ins; |
| 2005 | u64 parent = 0; |
| 2006 | u64 ref_root = 0; |
| 2007 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2008 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2009 | ins.objectid = node->bytenr; |
| 2010 | ins.offset = node->num_bytes; |
| 2011 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2012 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2013 | ref = btrfs_delayed_node_to_data_ref(node); |
| 2014 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2015 | parent = ref->parent; |
| 2016 | else |
| 2017 | ref_root = ref->root; |
| 2018 | |
| 2019 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2020 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2021 | flags |= extent_op->flags_to_set; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2022 | ret = alloc_reserved_file_extent(trans, root, |
| 2023 | parent, ref_root, flags, |
| 2024 | ref->objectid, ref->offset, |
| 2025 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2026 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2027 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2028 | node->num_bytes, parent, |
| 2029 | ref_root, ref->objectid, |
| 2030 | ref->offset, node->ref_mod, |
| 2031 | extent_op); |
| 2032 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2033 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2034 | node->num_bytes, parent, |
| 2035 | ref_root, ref->objectid, |
| 2036 | ref->offset, node->ref_mod, |
| 2037 | extent_op); |
| 2038 | } else { |
| 2039 | BUG(); |
| 2040 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2041 | return ret; |
| 2042 | } |
| 2043 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2044 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2045 | struct extent_buffer *leaf, |
| 2046 | struct btrfs_extent_item *ei) |
| 2047 | { |
| 2048 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2049 | if (extent_op->update_flags) { |
| 2050 | flags |= extent_op->flags_to_set; |
| 2051 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2052 | } |
| 2053 | |
| 2054 | if (extent_op->update_key) { |
| 2055 | struct btrfs_tree_block_info *bi; |
| 2056 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2057 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2058 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 2063 | struct btrfs_root *root, |
| 2064 | struct btrfs_delayed_ref_node *node, |
| 2065 | struct btrfs_delayed_extent_op *extent_op) |
| 2066 | { |
| 2067 | struct btrfs_key key; |
| 2068 | struct btrfs_path *path; |
| 2069 | struct btrfs_extent_item *ei; |
| 2070 | struct extent_buffer *leaf; |
| 2071 | u32 item_size; |
| 2072 | int ret; |
| 2073 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2074 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2075 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2076 | if (trans->aborted) |
| 2077 | return 0; |
| 2078 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2079 | if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) |
| 2080 | metadata = 0; |
| 2081 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2082 | path = btrfs_alloc_path(); |
| 2083 | if (!path) |
| 2084 | return -ENOMEM; |
| 2085 | |
| 2086 | key.objectid = node->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2087 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2088 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2089 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2090 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2091 | } else { |
| 2092 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2093 | key.offset = node->num_bytes; |
| 2094 | } |
| 2095 | |
| 2096 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2097 | path->reada = 1; |
| 2098 | path->leave_spinning = 1; |
| 2099 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 2100 | path, 0, 1); |
| 2101 | if (ret < 0) { |
| 2102 | err = ret; |
| 2103 | goto out; |
| 2104 | } |
| 2105 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2106 | if (metadata) { |
| 2107 | btrfs_release_path(path); |
| 2108 | metadata = 0; |
| 2109 | |
| 2110 | key.offset = node->num_bytes; |
| 2111 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2112 | goto again; |
| 2113 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2114 | err = -EIO; |
| 2115 | goto out; |
| 2116 | } |
| 2117 | |
| 2118 | leaf = path->nodes[0]; |
| 2119 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2120 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2121 | if (item_size < sizeof(*ei)) { |
| 2122 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 2123 | path, (u64)-1, 0); |
| 2124 | if (ret < 0) { |
| 2125 | err = ret; |
| 2126 | goto out; |
| 2127 | } |
| 2128 | leaf = path->nodes[0]; |
| 2129 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2130 | } |
| 2131 | #endif |
| 2132 | BUG_ON(item_size < sizeof(*ei)); |
| 2133 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2134 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2135 | |
| 2136 | btrfs_mark_buffer_dirty(leaf); |
| 2137 | out: |
| 2138 | btrfs_free_path(path); |
| 2139 | return err; |
| 2140 | } |
| 2141 | |
| 2142 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2143 | struct btrfs_root *root, |
| 2144 | struct btrfs_delayed_ref_node *node, |
| 2145 | struct btrfs_delayed_extent_op *extent_op, |
| 2146 | int insert_reserved) |
| 2147 | { |
| 2148 | int ret = 0; |
| 2149 | struct btrfs_delayed_tree_ref *ref; |
| 2150 | struct btrfs_key ins; |
| 2151 | u64 parent = 0; |
| 2152 | u64 ref_root = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2153 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 2154 | SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2155 | |
| 2156 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2157 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2158 | parent = ref->parent; |
| 2159 | else |
| 2160 | ref_root = ref->root; |
| 2161 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2162 | ins.objectid = node->bytenr; |
| 2163 | if (skinny_metadata) { |
| 2164 | ins.offset = ref->level; |
| 2165 | ins.type = BTRFS_METADATA_ITEM_KEY; |
| 2166 | } else { |
| 2167 | ins.offset = node->num_bytes; |
| 2168 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2169 | } |
| 2170 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2171 | BUG_ON(node->ref_mod != 1); |
| 2172 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2173 | BUG_ON(!extent_op || !extent_op->update_flags); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2174 | ret = alloc_reserved_tree_block(trans, root, |
| 2175 | parent, ref_root, |
| 2176 | extent_op->flags_to_set, |
| 2177 | &extent_op->key, |
| 2178 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2179 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2180 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2181 | node->num_bytes, parent, ref_root, |
| 2182 | ref->level, 0, 1, extent_op); |
| 2183 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2184 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2185 | node->num_bytes, parent, ref_root, |
| 2186 | ref->level, 0, 1, extent_op); |
| 2187 | } else { |
| 2188 | BUG(); |
| 2189 | } |
| 2190 | return ret; |
| 2191 | } |
| 2192 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2193 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2194 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2195 | struct btrfs_root *root, |
| 2196 | struct btrfs_delayed_ref_node *node, |
| 2197 | struct btrfs_delayed_extent_op *extent_op, |
| 2198 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2199 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2200 | int ret = 0; |
| 2201 | |
| 2202 | if (trans->aborted) |
| 2203 | return 0; |
| 2204 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2205 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2206 | struct btrfs_delayed_ref_head *head; |
| 2207 | /* |
| 2208 | * we've hit the end of the chain and we were supposed |
| 2209 | * to insert this extent into the tree. But, it got |
| 2210 | * deleted before we ever needed to insert it, so all |
| 2211 | * we have to do is clean up the accounting |
| 2212 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2213 | BUG_ON(extent_op); |
| 2214 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2215 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2216 | btrfs_pin_extent(root, node->bytenr, |
| 2217 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2218 | if (head->is_data) { |
| 2219 | ret = btrfs_del_csums(trans, root, |
| 2220 | node->bytenr, |
| 2221 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2222 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2223 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2224 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2225 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2226 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2227 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2228 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2229 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2230 | insert_reserved); |
| 2231 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2232 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2233 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2234 | insert_reserved); |
| 2235 | else |
| 2236 | BUG(); |
| 2237 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2240 | static noinline struct btrfs_delayed_ref_node * |
| 2241 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2242 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2243 | struct rb_node *node; |
| 2244 | struct btrfs_delayed_ref_node *ref; |
| 2245 | int action = BTRFS_ADD_DELAYED_REF; |
| 2246 | again: |
| 2247 | /* |
| 2248 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2249 | * this prevents ref count from going down to zero when |
| 2250 | * there still are pending delayed ref. |
| 2251 | */ |
| 2252 | node = rb_prev(&head->node.rb_node); |
| 2253 | while (1) { |
| 2254 | if (!node) |
| 2255 | break; |
| 2256 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2257 | rb_node); |
| 2258 | if (ref->bytenr != head->node.bytenr) |
| 2259 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2260 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2261 | return ref; |
| 2262 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2263 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2264 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2265 | action = BTRFS_DROP_DELAYED_REF; |
| 2266 | goto again; |
| 2267 | } |
| 2268 | return NULL; |
| 2269 | } |
| 2270 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2271 | /* |
| 2272 | * Returns 0 on success or if called with an already aborted transaction. |
| 2273 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2274 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2275 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2276 | struct btrfs_root *root, |
| 2277 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2278 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2279 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2280 | struct btrfs_delayed_ref_node *ref; |
| 2281 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2282 | struct btrfs_delayed_extent_op *extent_op; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2283 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2284 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2285 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2286 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2287 | |
| 2288 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2289 | while (1) { |
| 2290 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2291 | /* pick a new head ref from the cluster list */ |
| 2292 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2293 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2294 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2295 | locked_ref = list_entry(cluster->next, |
| 2296 | struct btrfs_delayed_ref_head, cluster); |
| 2297 | |
| 2298 | /* grab the lock that says we are going to process |
| 2299 | * all the refs for this head */ |
| 2300 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2301 | |
| 2302 | /* |
| 2303 | * we may have dropped the spin lock to get the head |
| 2304 | * mutex lock, and that might have given someone else |
| 2305 | * time to free the head. If that's true, it has been |
| 2306 | * removed from our list and we can move on. |
| 2307 | */ |
| 2308 | if (ret == -EAGAIN) { |
| 2309 | locked_ref = NULL; |
| 2310 | count++; |
| 2311 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | /* |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2316 | * We need to try and merge add/drops of the same ref since we |
| 2317 | * can run into issues with relocate dropping the implicit ref |
| 2318 | * and then it being added back again before the drop can |
| 2319 | * finish. If we merged anything we need to re-loop so we can |
| 2320 | * get a good ref. |
| 2321 | */ |
| 2322 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2323 | locked_ref); |
| 2324 | |
| 2325 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2326 | * locked_ref is the head node, so we have to go one |
| 2327 | * node back for any delayed ref updates |
| 2328 | */ |
| 2329 | ref = select_delayed_ref(locked_ref); |
| 2330 | |
| 2331 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2332 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2333 | /* |
| 2334 | * there are still refs with lower seq numbers in the |
| 2335 | * process of being added. Don't run this ref yet. |
| 2336 | */ |
| 2337 | list_del_init(&locked_ref->cluster); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2338 | btrfs_delayed_ref_unlock(locked_ref); |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2339 | locked_ref = NULL; |
| 2340 | delayed_refs->num_heads_ready++; |
| 2341 | spin_unlock(&delayed_refs->lock); |
| 2342 | cond_resched(); |
| 2343 | spin_lock(&delayed_refs->lock); |
| 2344 | continue; |
| 2345 | } |
| 2346 | |
| 2347 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2348 | * record the must insert reserved flag before we |
| 2349 | * drop the spin lock. |
| 2350 | */ |
| 2351 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2352 | locked_ref->must_insert_reserved = 0; |
| 2353 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2354 | extent_op = locked_ref->extent_op; |
| 2355 | locked_ref->extent_op = NULL; |
| 2356 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2357 | if (!ref) { |
| 2358 | /* All delayed refs have been processed, Go ahead |
| 2359 | * and send the head node to run_one_delayed_ref, |
| 2360 | * so that any accounting fixes can happen |
| 2361 | */ |
| 2362 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2363 | |
| 2364 | if (extent_op && must_insert_reserved) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2365 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2366 | extent_op = NULL; |
| 2367 | } |
| 2368 | |
| 2369 | if (extent_op) { |
| 2370 | spin_unlock(&delayed_refs->lock); |
| 2371 | |
| 2372 | ret = run_delayed_extent_op(trans, root, |
| 2373 | ref, extent_op); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2374 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2375 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2376 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2377 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
Dan Carpenter | 253beeb | 2012-04-18 09:59:03 +0300 | [diff] [blame] | 2378 | spin_lock(&delayed_refs->lock); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2379 | btrfs_delayed_ref_unlock(locked_ref); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2380 | return ret; |
| 2381 | } |
| 2382 | |
Chris Mason | 203bf28 | 2012-01-06 15:23:57 -0500 | [diff] [blame] | 2383 | goto next; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2384 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | ref->in_tree = 0; |
| 2388 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2389 | delayed_refs->num_entries--; |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2390 | if (!btrfs_delayed_ref_is_head(ref)) { |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2391 | /* |
| 2392 | * when we play the delayed ref, also correct the |
| 2393 | * ref_mod on head |
| 2394 | */ |
| 2395 | switch (ref->action) { |
| 2396 | case BTRFS_ADD_DELAYED_REF: |
| 2397 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2398 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2399 | break; |
| 2400 | case BTRFS_DROP_DELAYED_REF: |
| 2401 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2402 | break; |
| 2403 | default: |
| 2404 | WARN_ON(1); |
| 2405 | } |
| 2406 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2407 | spin_unlock(&delayed_refs->lock); |
| 2408 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2409 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2410 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2411 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2412 | btrfs_free_delayed_extent_op(extent_op); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2413 | if (ret) { |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2414 | btrfs_delayed_ref_unlock(locked_ref); |
| 2415 | btrfs_put_delayed_ref(ref); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2416 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret); |
Dan Carpenter | 253beeb | 2012-04-18 09:59:03 +0300 | [diff] [blame] | 2417 | spin_lock(&delayed_refs->lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2418 | return ret; |
| 2419 | } |
| 2420 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2421 | /* |
| 2422 | * If this node is a head, that means all the refs in this head |
| 2423 | * have been dealt with, and we will pick the next head to deal |
| 2424 | * with, so we must unlock the head and drop it from the cluster |
| 2425 | * list before we release it. |
| 2426 | */ |
| 2427 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2428 | list_del_init(&locked_ref->cluster); |
| 2429 | btrfs_delayed_ref_unlock(locked_ref); |
| 2430 | locked_ref = NULL; |
| 2431 | } |
| 2432 | btrfs_put_delayed_ref(ref); |
| 2433 | count++; |
Chris Mason | 203bf28 | 2012-01-06 15:23:57 -0500 | [diff] [blame] | 2434 | next: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2435 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2436 | spin_lock(&delayed_refs->lock); |
| 2437 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2438 | return count; |
| 2439 | } |
| 2440 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2441 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2442 | /* |
| 2443 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2444 | * correlates in most cases to the order added. To expose dependencies on this |
| 2445 | * order, we start to process the tree in the middle instead of the beginning |
| 2446 | */ |
| 2447 | static u64 find_middle(struct rb_root *root) |
| 2448 | { |
| 2449 | struct rb_node *n = root->rb_node; |
| 2450 | struct btrfs_delayed_ref_node *entry; |
| 2451 | int alt = 1; |
| 2452 | u64 middle; |
| 2453 | u64 first = 0, last = 0; |
| 2454 | |
| 2455 | n = rb_first(root); |
| 2456 | if (n) { |
| 2457 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2458 | first = entry->bytenr; |
| 2459 | } |
| 2460 | n = rb_last(root); |
| 2461 | if (n) { |
| 2462 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2463 | last = entry->bytenr; |
| 2464 | } |
| 2465 | n = root->rb_node; |
| 2466 | |
| 2467 | while (n) { |
| 2468 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2469 | WARN_ON(!entry->in_tree); |
| 2470 | |
| 2471 | middle = entry->bytenr; |
| 2472 | |
| 2473 | if (alt) |
| 2474 | n = n->rb_left; |
| 2475 | else |
| 2476 | n = n->rb_right; |
| 2477 | |
| 2478 | alt = 1 - alt; |
| 2479 | } |
| 2480 | return middle; |
| 2481 | } |
| 2482 | #endif |
| 2483 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2484 | int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans, |
| 2485 | struct btrfs_fs_info *fs_info) |
| 2486 | { |
| 2487 | struct qgroup_update *qgroup_update; |
| 2488 | int ret = 0; |
| 2489 | |
| 2490 | if (list_empty(&trans->qgroup_ref_list) != |
| 2491 | !trans->delayed_ref_elem.seq) { |
| 2492 | /* list without seq or seq without list */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2493 | btrfs_err(fs_info, |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 2494 | "qgroup accounting update error, list is%s empty, seq is %#x.%x", |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2495 | list_empty(&trans->qgroup_ref_list) ? "" : " not", |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 2496 | (u32)(trans->delayed_ref_elem.seq >> 32), |
| 2497 | (u32)trans->delayed_ref_elem.seq); |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2498 | BUG(); |
| 2499 | } |
| 2500 | |
| 2501 | if (!trans->delayed_ref_elem.seq) |
| 2502 | return 0; |
| 2503 | |
| 2504 | while (!list_empty(&trans->qgroup_ref_list)) { |
| 2505 | qgroup_update = list_first_entry(&trans->qgroup_ref_list, |
| 2506 | struct qgroup_update, list); |
| 2507 | list_del(&qgroup_update->list); |
| 2508 | if (!ret) |
| 2509 | ret = btrfs_qgroup_account_ref( |
| 2510 | trans, fs_info, qgroup_update->node, |
| 2511 | qgroup_update->extent_op); |
| 2512 | kfree(qgroup_update); |
| 2513 | } |
| 2514 | |
| 2515 | btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem); |
| 2516 | |
| 2517 | return ret; |
| 2518 | } |
| 2519 | |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2520 | static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq, |
| 2521 | int count) |
| 2522 | { |
| 2523 | int val = atomic_read(&delayed_refs->ref_seq); |
| 2524 | |
| 2525 | if (val < seq || val >= seq + count) |
| 2526 | return 1; |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2530 | static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads) |
| 2531 | { |
| 2532 | u64 num_bytes; |
| 2533 | |
| 2534 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2535 | sizeof(struct btrfs_extent_inline_ref)); |
| 2536 | if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) |
| 2537 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2538 | |
| 2539 | /* |
| 2540 | * We don't ever fill up leaves all the way so multiply by 2 just to be |
| 2541 | * closer to what we're really going to want to ouse. |
| 2542 | */ |
| 2543 | return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root)); |
| 2544 | } |
| 2545 | |
| 2546 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
| 2547 | struct btrfs_root *root) |
| 2548 | { |
| 2549 | struct btrfs_block_rsv *global_rsv; |
| 2550 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; |
| 2551 | u64 num_bytes; |
| 2552 | int ret = 0; |
| 2553 | |
| 2554 | num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 2555 | num_heads = heads_to_leaves(root, num_heads); |
| 2556 | if (num_heads > 1) |
| 2557 | num_bytes += (num_heads - 1) * root->leafsize; |
| 2558 | num_bytes <<= 1; |
| 2559 | global_rsv = &root->fs_info->global_block_rsv; |
| 2560 | |
| 2561 | /* |
| 2562 | * If we can't allocate any more chunks lets make sure we have _lots_ of |
| 2563 | * wiggle room since running delayed refs can create more delayed refs. |
| 2564 | */ |
| 2565 | if (global_rsv->space_info->full) |
| 2566 | num_bytes <<= 1; |
| 2567 | |
| 2568 | spin_lock(&global_rsv->lock); |
| 2569 | if (global_rsv->reserved <= num_bytes) |
| 2570 | ret = 1; |
| 2571 | spin_unlock(&global_rsv->lock); |
| 2572 | return ret; |
| 2573 | } |
| 2574 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2575 | /* |
| 2576 | * this starts processing the delayed reference count updates and |
| 2577 | * extent insertions we have queued up so far. count can be |
| 2578 | * 0, which means to process everything in the tree at the start |
| 2579 | * of the run (but not newly added entries), or it can be some target |
| 2580 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2581 | * |
| 2582 | * Returns 0 on success or if called with an aborted transaction |
| 2583 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2584 | */ |
| 2585 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2586 | struct btrfs_root *root, unsigned long count) |
| 2587 | { |
| 2588 | struct rb_node *node; |
| 2589 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2590 | struct btrfs_delayed_ref_node *ref; |
| 2591 | struct list_head cluster; |
| 2592 | int ret; |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2593 | u64 delayed_start; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2594 | int run_all = count == (unsigned long)-1; |
| 2595 | int run_most = 0; |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2596 | int loops; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2597 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2598 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2599 | if (trans->aborted) |
| 2600 | return 0; |
| 2601 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2602 | if (root == root->fs_info->extent_root) |
| 2603 | root = root->fs_info->tree_root; |
| 2604 | |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 2605 | btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info); |
| 2606 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2607 | delayed_refs = &trans->transaction->delayed_refs; |
| 2608 | INIT_LIST_HEAD(&cluster); |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2609 | if (count == 0) { |
| 2610 | count = delayed_refs->num_entries * 2; |
| 2611 | run_most = 1; |
| 2612 | } |
| 2613 | |
| 2614 | if (!run_all && !run_most) { |
| 2615 | int old; |
| 2616 | int seq = atomic_read(&delayed_refs->ref_seq); |
| 2617 | |
| 2618 | progress: |
| 2619 | old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1); |
| 2620 | if (old) { |
| 2621 | DEFINE_WAIT(__wait); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2622 | if (delayed_refs->flushing || |
| 2623 | !btrfs_should_throttle_delayed_refs(trans, root)) |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2624 | return 0; |
| 2625 | |
| 2626 | prepare_to_wait(&delayed_refs->wait, &__wait, |
| 2627 | TASK_UNINTERRUPTIBLE); |
| 2628 | |
| 2629 | old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1); |
| 2630 | if (old) { |
| 2631 | schedule(); |
| 2632 | finish_wait(&delayed_refs->wait, &__wait); |
| 2633 | |
| 2634 | if (!refs_newer(delayed_refs, seq, 256)) |
| 2635 | goto progress; |
| 2636 | else |
| 2637 | return 0; |
| 2638 | } else { |
| 2639 | finish_wait(&delayed_refs->wait, &__wait); |
| 2640 | goto again; |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | } else { |
| 2645 | atomic_inc(&delayed_refs->procs_running_refs); |
| 2646 | } |
| 2647 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2648 | again: |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2649 | loops = 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2650 | spin_lock(&delayed_refs->lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2651 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2652 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2653 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2654 | #endif |
| 2655 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2656 | while (1) { |
| 2657 | if (!(run_all || run_most) && |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2658 | !btrfs_should_throttle_delayed_refs(trans, root)) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2659 | break; |
| 2660 | |
| 2661 | /* |
| 2662 | * go find something we can process in the rbtree. We start at |
| 2663 | * the beginning of the tree, and then build a cluster |
| 2664 | * of refs to process starting at the first one we are able to |
| 2665 | * lock |
| 2666 | */ |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2667 | delayed_start = delayed_refs->run_delayed_start; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2668 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2669 | delayed_refs->run_delayed_start); |
| 2670 | if (ret) |
| 2671 | break; |
| 2672 | |
| 2673 | ret = run_clustered_refs(trans, root, &cluster); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2674 | if (ret < 0) { |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2675 | btrfs_release_ref_cluster(&cluster); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2676 | spin_unlock(&delayed_refs->lock); |
| 2677 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2678 | atomic_dec(&delayed_refs->procs_running_refs); |
Josef Bacik | f971fe2 | 2013-06-10 11:52:32 -0400 | [diff] [blame] | 2679 | wake_up(&delayed_refs->wait); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2680 | return ret; |
| 2681 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2682 | |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2683 | atomic_add(ret, &delayed_refs->ref_seq); |
| 2684 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2685 | count -= min_t(unsigned long, ret, count); |
| 2686 | |
| 2687 | if (count == 0) |
| 2688 | break; |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2689 | |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2690 | if (delayed_start >= delayed_refs->run_delayed_start) { |
| 2691 | if (loops == 0) { |
| 2692 | /* |
| 2693 | * btrfs_find_ref_cluster looped. let's do one |
| 2694 | * more cycle. if we don't run any delayed ref |
| 2695 | * during that cycle (because we can't because |
| 2696 | * all of them are blocked), bail out. |
| 2697 | */ |
| 2698 | loops = 1; |
| 2699 | } else { |
| 2700 | /* |
| 2701 | * no runnable refs left, stop trying |
| 2702 | */ |
| 2703 | BUG_ON(run_all); |
| 2704 | break; |
| 2705 | } |
| 2706 | } |
| 2707 | if (ret) { |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2708 | /* refs were run, let's reset staleness detection */ |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2709 | loops = 0; |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2710 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2711 | } |
| 2712 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2713 | if (run_all) { |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2714 | if (!list_empty(&trans->new_bgs)) { |
| 2715 | spin_unlock(&delayed_refs->lock); |
| 2716 | btrfs_create_pending_block_groups(trans, root); |
| 2717 | spin_lock(&delayed_refs->lock); |
| 2718 | } |
| 2719 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2720 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2721 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2722 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2723 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2724 | |
| 2725 | while (node) { |
| 2726 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2727 | rb_node); |
| 2728 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2729 | struct btrfs_delayed_ref_head *head; |
| 2730 | |
| 2731 | head = btrfs_delayed_node_to_head(ref); |
| 2732 | atomic_inc(&ref->refs); |
| 2733 | |
| 2734 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2735 | /* |
| 2736 | * Mutex was contended, block until it's |
| 2737 | * released and try again |
| 2738 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2739 | mutex_lock(&head->mutex); |
| 2740 | mutex_unlock(&head->mutex); |
| 2741 | |
| 2742 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2743 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2744 | goto again; |
| 2745 | } |
| 2746 | node = rb_next(node); |
| 2747 | } |
| 2748 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2749 | schedule_timeout(1); |
| 2750 | goto again; |
| 2751 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2752 | out: |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2753 | atomic_dec(&delayed_refs->procs_running_refs); |
| 2754 | smp_mb(); |
| 2755 | if (waitqueue_active(&delayed_refs->wait)) |
| 2756 | wake_up(&delayed_refs->wait); |
| 2757 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2758 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 2759 | assert_qgroups_uptodate(trans); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2760 | return 0; |
| 2761 | } |
| 2762 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2763 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2764 | struct btrfs_root *root, |
| 2765 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2766 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2767 | { |
| 2768 | struct btrfs_delayed_extent_op *extent_op; |
| 2769 | int ret; |
| 2770 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2771 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2772 | if (!extent_op) |
| 2773 | return -ENOMEM; |
| 2774 | |
| 2775 | extent_op->flags_to_set = flags; |
| 2776 | extent_op->update_flags = 1; |
| 2777 | extent_op->update_key = 0; |
| 2778 | extent_op->is_data = is_data ? 1 : 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2779 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2780 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2781 | ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr, |
| 2782 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2783 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2784 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2785 | return ret; |
| 2786 | } |
| 2787 | |
| 2788 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2789 | struct btrfs_root *root, |
| 2790 | struct btrfs_path *path, |
| 2791 | u64 objectid, u64 offset, u64 bytenr) |
| 2792 | { |
| 2793 | struct btrfs_delayed_ref_head *head; |
| 2794 | struct btrfs_delayed_ref_node *ref; |
| 2795 | struct btrfs_delayed_data_ref *data_ref; |
| 2796 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2797 | struct rb_node *node; |
| 2798 | int ret = 0; |
| 2799 | |
| 2800 | ret = -ENOENT; |
| 2801 | delayed_refs = &trans->transaction->delayed_refs; |
| 2802 | spin_lock(&delayed_refs->lock); |
| 2803 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2804 | if (!head) |
| 2805 | goto out; |
| 2806 | |
| 2807 | if (!mutex_trylock(&head->mutex)) { |
| 2808 | atomic_inc(&head->node.refs); |
| 2809 | spin_unlock(&delayed_refs->lock); |
| 2810 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2811 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2812 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2813 | /* |
| 2814 | * Mutex was contended, block until it's released and let |
| 2815 | * caller try again |
| 2816 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2817 | mutex_lock(&head->mutex); |
| 2818 | mutex_unlock(&head->mutex); |
| 2819 | btrfs_put_delayed_ref(&head->node); |
| 2820 | return -EAGAIN; |
| 2821 | } |
| 2822 | |
| 2823 | node = rb_prev(&head->node.rb_node); |
| 2824 | if (!node) |
| 2825 | goto out_unlock; |
| 2826 | |
| 2827 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2828 | |
| 2829 | if (ref->bytenr != bytenr) |
| 2830 | goto out_unlock; |
| 2831 | |
| 2832 | ret = 1; |
| 2833 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2834 | goto out_unlock; |
| 2835 | |
| 2836 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2837 | |
| 2838 | node = rb_prev(node); |
| 2839 | if (node) { |
Liu Bo | df57dbe | 2012-07-23 05:50:03 -0600 | [diff] [blame] | 2840 | int seq = ref->seq; |
| 2841 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2842 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
Liu Bo | df57dbe | 2012-07-23 05:50:03 -0600 | [diff] [blame] | 2843 | if (ref->bytenr == bytenr && ref->seq == seq) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2844 | goto out_unlock; |
| 2845 | } |
| 2846 | |
| 2847 | if (data_ref->root != root->root_key.objectid || |
| 2848 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2849 | goto out_unlock; |
| 2850 | |
| 2851 | ret = 0; |
| 2852 | out_unlock: |
| 2853 | mutex_unlock(&head->mutex); |
| 2854 | out: |
| 2855 | spin_unlock(&delayed_refs->lock); |
| 2856 | return ret; |
| 2857 | } |
| 2858 | |
| 2859 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2860 | struct btrfs_root *root, |
| 2861 | struct btrfs_path *path, |
| 2862 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2863 | { |
| 2864 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2865 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2866 | struct btrfs_extent_data_ref *ref; |
| 2867 | struct btrfs_extent_inline_ref *iref; |
| 2868 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2869 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2870 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2871 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2872 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2873 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2874 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2875 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2876 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2877 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2878 | if (ret < 0) |
| 2879 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2880 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2881 | |
| 2882 | ret = -ENOENT; |
| 2883 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2884 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2885 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2886 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2887 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2888 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2889 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2890 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2891 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2892 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2893 | ret = 1; |
| 2894 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2895 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2896 | if (item_size < sizeof(*ei)) { |
| 2897 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2898 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2899 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2900 | #endif |
| 2901 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2902 | |
| 2903 | if (item_size != sizeof(*ei) + |
| 2904 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2905 | goto out; |
| 2906 | |
| 2907 | if (btrfs_extent_generation(leaf, ei) <= |
| 2908 | btrfs_root_last_snapshot(&root->root_item)) |
| 2909 | goto out; |
| 2910 | |
| 2911 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2912 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2913 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2914 | goto out; |
| 2915 | |
| 2916 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2917 | if (btrfs_extent_refs(leaf, ei) != |
| 2918 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2919 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2920 | root->root_key.objectid || |
| 2921 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2922 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2923 | goto out; |
| 2924 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2925 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2926 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2927 | return ret; |
| 2928 | } |
| 2929 | |
| 2930 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2931 | struct btrfs_root *root, |
| 2932 | u64 objectid, u64 offset, u64 bytenr) |
| 2933 | { |
| 2934 | struct btrfs_path *path; |
| 2935 | int ret; |
| 2936 | int ret2; |
| 2937 | |
| 2938 | path = btrfs_alloc_path(); |
| 2939 | if (!path) |
| 2940 | return -ENOENT; |
| 2941 | |
| 2942 | do { |
| 2943 | ret = check_committed_ref(trans, root, path, objectid, |
| 2944 | offset, bytenr); |
| 2945 | if (ret && ret != -ENOENT) |
| 2946 | goto out; |
| 2947 | |
| 2948 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2949 | offset, bytenr); |
| 2950 | } while (ret2 == -EAGAIN); |
| 2951 | |
| 2952 | if (ret2 && ret2 != -ENOENT) { |
| 2953 | ret = ret2; |
| 2954 | goto out; |
| 2955 | } |
| 2956 | |
| 2957 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2958 | ret = 0; |
| 2959 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2960 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2961 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2962 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2963 | return ret; |
| 2964 | } |
| 2965 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2966 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2967 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2968 | struct extent_buffer *buf, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2969 | int full_backref, int inc, int for_cow) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2970 | { |
| 2971 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2972 | u64 num_bytes; |
| 2973 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2974 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2975 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2976 | struct btrfs_key key; |
| 2977 | struct btrfs_file_extent_item *fi; |
| 2978 | int i; |
| 2979 | int level; |
| 2980 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2981 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2982 | u64, u64, u64, u64, u64, u64, int); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2983 | |
| 2984 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2985 | nritems = btrfs_header_nritems(buf); |
| 2986 | level = btrfs_header_level(buf); |
| 2987 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2988 | if (!root->ref_cows && level == 0) |
| 2989 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2990 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2991 | if (inc) |
| 2992 | process_func = btrfs_inc_extent_ref; |
| 2993 | else |
| 2994 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2995 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2996 | if (full_backref) |
| 2997 | parent = buf->start; |
| 2998 | else |
| 2999 | parent = 0; |
| 3000 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3001 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3002 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3003 | btrfs_item_key_to_cpu(buf, &key, i); |
| 3004 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3005 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3006 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3007 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3008 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3009 | BTRFS_FILE_EXTENT_INLINE) |
| 3010 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3011 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 3012 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3013 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3014 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3015 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 3016 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 3017 | ret = process_func(trans, root, bytenr, num_bytes, |
| 3018 | parent, ref_root, key.objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3019 | key.offset, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3020 | if (ret) |
| 3021 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3022 | } else { |
| 3023 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3024 | num_bytes = btrfs_level_size(root, level - 1); |
| 3025 | ret = process_func(trans, root, bytenr, num_bytes, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3026 | parent, ref_root, level - 1, 0, |
| 3027 | for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3028 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3029 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3030 | } |
| 3031 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3032 | return 0; |
| 3033 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3034 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3035 | } |
| 3036 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3037 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3038 | struct extent_buffer *buf, int full_backref, int for_cow) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3039 | { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3040 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3041 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3042 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3043 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3044 | struct extent_buffer *buf, int full_backref, int for_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3045 | { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3046 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3047 | } |
| 3048 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3049 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 3050 | struct btrfs_root *root, |
| 3051 | struct btrfs_path *path, |
| 3052 | struct btrfs_block_group_cache *cache) |
| 3053 | { |
| 3054 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3055 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3056 | unsigned long bi; |
| 3057 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3058 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3059 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3060 | if (ret < 0) |
| 3061 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3062 | BUG_ON(ret); /* Corruption */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3063 | |
| 3064 | leaf = path->nodes[0]; |
| 3065 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3066 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3067 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3068 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3069 | fail: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3070 | if (ret) { |
| 3071 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3072 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3073 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3074 | return 0; |
| 3075 | |
| 3076 | } |
| 3077 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3078 | static struct btrfs_block_group_cache * |
| 3079 | next_block_group(struct btrfs_root *root, |
| 3080 | struct btrfs_block_group_cache *cache) |
| 3081 | { |
| 3082 | struct rb_node *node; |
| 3083 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 3084 | node = rb_next(&cache->cache_node); |
| 3085 | btrfs_put_block_group(cache); |
| 3086 | if (node) { |
| 3087 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 3088 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 3089 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3090 | } else |
| 3091 | cache = NULL; |
| 3092 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 3093 | return cache; |
| 3094 | } |
| 3095 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3096 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3097 | struct btrfs_trans_handle *trans, |
| 3098 | struct btrfs_path *path) |
| 3099 | { |
| 3100 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 3101 | struct inode *inode = NULL; |
| 3102 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3103 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3104 | int num_pages = 0; |
| 3105 | int retries = 0; |
| 3106 | int ret = 0; |
| 3107 | |
| 3108 | /* |
| 3109 | * If this block group is smaller than 100 megs don't bother caching the |
| 3110 | * block group. |
| 3111 | */ |
| 3112 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 3113 | spin_lock(&block_group->lock); |
| 3114 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3115 | spin_unlock(&block_group->lock); |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
| 3119 | again: |
| 3120 | inode = lookup_free_space_inode(root, block_group, path); |
| 3121 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3122 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3123 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3124 | goto out; |
| 3125 | } |
| 3126 | |
| 3127 | if (IS_ERR(inode)) { |
| 3128 | BUG_ON(retries); |
| 3129 | retries++; |
| 3130 | |
| 3131 | if (block_group->ro) |
| 3132 | goto out_free; |
| 3133 | |
| 3134 | ret = create_free_space_inode(root, trans, block_group, path); |
| 3135 | if (ret) |
| 3136 | goto out_free; |
| 3137 | goto again; |
| 3138 | } |
| 3139 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3140 | /* We've already setup this transaction, go ahead and exit */ |
| 3141 | if (block_group->cache_generation == trans->transid && |
| 3142 | i_size_read(inode)) { |
| 3143 | dcs = BTRFS_DC_SETUP; |
| 3144 | goto out_put; |
| 3145 | } |
| 3146 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3147 | /* |
| 3148 | * We want to set the generation to 0, that way if anything goes wrong |
| 3149 | * from here on out we know not to trust this cache when we load up next |
| 3150 | * time. |
| 3151 | */ |
| 3152 | BTRFS_I(inode)->generation = 0; |
| 3153 | ret = btrfs_update_inode(trans, root, inode); |
| 3154 | WARN_ON(ret); |
| 3155 | |
| 3156 | if (i_size_read(inode) > 0) { |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3157 | ret = btrfs_check_trunc_cache_free_space(root, |
| 3158 | &root->fs_info->global_block_rsv); |
| 3159 | if (ret) |
| 3160 | goto out_put; |
| 3161 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3162 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 3163 | inode); |
| 3164 | if (ret) |
| 3165 | goto out_put; |
| 3166 | } |
| 3167 | |
| 3168 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3169 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
| 3170 | !btrfs_test_opt(root, SPACE_CACHE)) { |
| 3171 | /* |
| 3172 | * don't bother trying to write stuff out _if_ |
| 3173 | * a) we're not cached, |
| 3174 | * b) we're with nospace_cache mount option. |
| 3175 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3176 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3177 | spin_unlock(&block_group->lock); |
| 3178 | goto out_put; |
| 3179 | } |
| 3180 | spin_unlock(&block_group->lock); |
| 3181 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3182 | /* |
| 3183 | * Try to preallocate enough space based on how big the block group is. |
| 3184 | * Keep in mind this has to include any pinned space which could end up |
| 3185 | * taking up quite a bit since it's not folded into the other space |
| 3186 | * cache. |
| 3187 | */ |
| 3188 | num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3189 | if (!num_pages) |
| 3190 | num_pages = 1; |
| 3191 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3192 | num_pages *= 16; |
| 3193 | num_pages *= PAGE_CACHE_SIZE; |
| 3194 | |
| 3195 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 3196 | if (ret) |
| 3197 | goto out_put; |
| 3198 | |
| 3199 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3200 | num_pages, num_pages, |
| 3201 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3202 | if (!ret) |
| 3203 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3204 | btrfs_free_reserved_data_space(inode, num_pages); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3205 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3206 | out_put: |
| 3207 | iput(inode); |
| 3208 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3209 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3210 | out: |
| 3211 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3212 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3213 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3214 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3215 | spin_unlock(&block_group->lock); |
| 3216 | |
| 3217 | return ret; |
| 3218 | } |
| 3219 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3220 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 3221 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3222 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3223 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3224 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3225 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3226 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3227 | |
| 3228 | path = btrfs_alloc_path(); |
| 3229 | if (!path) |
| 3230 | return -ENOMEM; |
| 3231 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3232 | again: |
| 3233 | while (1) { |
| 3234 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3235 | while (cache) { |
| 3236 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3237 | break; |
| 3238 | cache = next_block_group(root, cache); |
| 3239 | } |
| 3240 | if (!cache) { |
| 3241 | if (last == 0) |
| 3242 | break; |
| 3243 | last = 0; |
| 3244 | continue; |
| 3245 | } |
| 3246 | err = cache_save_setup(cache, trans, path); |
| 3247 | last = cache->key.objectid + cache->key.offset; |
| 3248 | btrfs_put_block_group(cache); |
| 3249 | } |
| 3250 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3251 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3252 | if (last == 0) { |
| 3253 | err = btrfs_run_delayed_refs(trans, root, |
| 3254 | (unsigned long)-1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3255 | if (err) /* File system offline */ |
| 3256 | goto out; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3257 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3258 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3259 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3260 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3261 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 3262 | btrfs_put_block_group(cache); |
| 3263 | goto again; |
| 3264 | } |
| 3265 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3266 | if (cache->dirty) |
| 3267 | break; |
| 3268 | cache = next_block_group(root, cache); |
| 3269 | } |
| 3270 | if (!cache) { |
| 3271 | if (last == 0) |
| 3272 | break; |
| 3273 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3274 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3275 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3276 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3277 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 3278 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3279 | cache->dirty = 0; |
| 3280 | last = cache->key.objectid + cache->key.offset; |
| 3281 | |
| 3282 | err = write_one_cache_group(trans, root, path, cache); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3283 | if (err) /* File system offline */ |
| 3284 | goto out; |
| 3285 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3286 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3287 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3288 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3289 | while (1) { |
| 3290 | /* |
| 3291 | * I don't think this is needed since we're just marking our |
| 3292 | * preallocated extent as written, but just in case it can't |
| 3293 | * hurt. |
| 3294 | */ |
| 3295 | if (last == 0) { |
| 3296 | err = btrfs_run_delayed_refs(trans, root, |
| 3297 | (unsigned long)-1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3298 | if (err) /* File system offline */ |
| 3299 | goto out; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3303 | while (cache) { |
| 3304 | /* |
| 3305 | * Really this shouldn't happen, but it could if we |
| 3306 | * couldn't write the entire preallocated extent and |
| 3307 | * splitting the extent resulted in a new block. |
| 3308 | */ |
| 3309 | if (cache->dirty) { |
| 3310 | btrfs_put_block_group(cache); |
| 3311 | goto again; |
| 3312 | } |
| 3313 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 3314 | break; |
| 3315 | cache = next_block_group(root, cache); |
| 3316 | } |
| 3317 | if (!cache) { |
| 3318 | if (last == 0) |
| 3319 | break; |
| 3320 | last = 0; |
| 3321 | continue; |
| 3322 | } |
| 3323 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3324 | err = btrfs_write_out_cache(root, trans, cache, path); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3325 | |
| 3326 | /* |
| 3327 | * If we didn't have an error then the cache state is still |
| 3328 | * NEED_WRITE, so we can set it to WRITTEN. |
| 3329 | */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3330 | if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3331 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3332 | last = cache->key.objectid + cache->key.offset; |
| 3333 | btrfs_put_block_group(cache); |
| 3334 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3335 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3336 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3337 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3338 | return err; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3339 | } |
| 3340 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3341 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 3342 | { |
| 3343 | struct btrfs_block_group_cache *block_group; |
| 3344 | int readonly = 0; |
| 3345 | |
| 3346 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 3347 | if (!block_group || block_group->ro) |
| 3348 | readonly = 1; |
| 3349 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3350 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3351 | return readonly; |
| 3352 | } |
| 3353 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3354 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3355 | u64 total_bytes, u64 bytes_used, |
| 3356 | struct btrfs_space_info **space_info) |
| 3357 | { |
| 3358 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3359 | int i; |
| 3360 | int factor; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3361 | int ret; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3362 | |
| 3363 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3364 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3365 | factor = 2; |
| 3366 | else |
| 3367 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3368 | |
| 3369 | found = __find_space_info(info, flags); |
| 3370 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3371 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3372 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3373 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3374 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3375 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3376 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3377 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3378 | *space_info = found; |
| 3379 | return 0; |
| 3380 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3381 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3382 | if (!found) |
| 3383 | return -ENOMEM; |
| 3384 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3385 | ret = percpu_counter_init(&found->total_bytes_pinned, 0); |
| 3386 | if (ret) { |
| 3387 | kfree(found); |
| 3388 | return ret; |
| 3389 | } |
| 3390 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3391 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 3392 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3393 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3394 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3395 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3396 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3397 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3398 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3399 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3400 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3401 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3402 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3403 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3404 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3405 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3406 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3407 | found->flush = 0; |
| 3408 | init_waitqueue_head(&found->wait); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3409 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3410 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3411 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3412 | info->data_sinfo = found; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3413 | return 0; |
| 3414 | } |
| 3415 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3416 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3417 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3418 | u64 extra_flags = chunk_to_extended(flags) & |
| 3419 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3420 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3421 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3422 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3423 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3424 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3425 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3426 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3427 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3428 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3429 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3430 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3431 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3432 | * returns target flags in extended format or 0 if restripe for this |
| 3433 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3434 | * |
| 3435 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3436 | */ |
| 3437 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 3438 | { |
| 3439 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3440 | u64 target = 0; |
| 3441 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3442 | if (!bctl) |
| 3443 | return 0; |
| 3444 | |
| 3445 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 3446 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3447 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 3448 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 3449 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3450 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 3451 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 3452 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3453 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 3454 | } |
| 3455 | |
| 3456 | return target; |
| 3457 | } |
| 3458 | |
| 3459 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3460 | * @flags: available profiles in extended format (see ctree.h) |
| 3461 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3462 | * Returns reduced profile in chunk format. If profile changing is in |
| 3463 | * progress (either running or paused) picks the target profile (if it's |
| 3464 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3465 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3466 | static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3467 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 3468 | /* |
| 3469 | * we add in the count of missing devices because we want |
| 3470 | * to make sure that any RAID levels on a degraded FS |
| 3471 | * continue to be honored. |
| 3472 | */ |
| 3473 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 3474 | root->fs_info->fs_devices->missing_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3475 | u64 target; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3476 | u64 tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3477 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3478 | /* |
| 3479 | * see if restripe for this chunk_type is in progress, if so |
| 3480 | * try to reduce to the target profile |
| 3481 | */ |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3482 | spin_lock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3483 | target = get_restripe_target(root->fs_info, flags); |
| 3484 | if (target) { |
| 3485 | /* pick target profile only if it's already available */ |
| 3486 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3487 | spin_unlock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3488 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3489 | } |
| 3490 | } |
| 3491 | spin_unlock(&root->fs_info->balance_lock); |
| 3492 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3493 | /* First, mask out the RAID levels which aren't possible */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3494 | if (num_devices == 1) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3495 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 | |
| 3496 | BTRFS_BLOCK_GROUP_RAID5); |
| 3497 | if (num_devices < 3) |
| 3498 | flags &= ~BTRFS_BLOCK_GROUP_RAID6; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3499 | if (num_devices < 4) |
| 3500 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 3501 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3502 | tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3503 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3504 | BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10); |
| 3505 | flags &= ~tmp; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3506 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3507 | if (tmp & BTRFS_BLOCK_GROUP_RAID6) |
| 3508 | tmp = BTRFS_BLOCK_GROUP_RAID6; |
| 3509 | else if (tmp & BTRFS_BLOCK_GROUP_RAID5) |
| 3510 | tmp = BTRFS_BLOCK_GROUP_RAID5; |
| 3511 | else if (tmp & BTRFS_BLOCK_GROUP_RAID10) |
| 3512 | tmp = BTRFS_BLOCK_GROUP_RAID10; |
| 3513 | else if (tmp & BTRFS_BLOCK_GROUP_RAID1) |
| 3514 | tmp = BTRFS_BLOCK_GROUP_RAID1; |
| 3515 | else if (tmp & BTRFS_BLOCK_GROUP_RAID0) |
| 3516 | tmp = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3517 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3518 | return extended_to_chunk(flags | tmp); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3519 | } |
| 3520 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3521 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3522 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3523 | unsigned seq; |
| 3524 | |
| 3525 | do { |
| 3526 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 3527 | |
| 3528 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3529 | flags |= root->fs_info->avail_data_alloc_bits; |
| 3530 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3531 | flags |= root->fs_info->avail_system_alloc_bits; |
| 3532 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3533 | flags |= root->fs_info->avail_metadata_alloc_bits; |
| 3534 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3535 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3536 | return btrfs_reduce_alloc_profile(root, flags); |
| 3537 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3538 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3539 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3540 | { |
| 3541 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3542 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3543 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3544 | if (data) |
| 3545 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3546 | else if (root == root->fs_info->chunk_root) |
| 3547 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3548 | else |
| 3549 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3550 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3551 | ret = get_alloc_profile(root, flags); |
| 3552 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3553 | } |
| 3554 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3555 | /* |
| 3556 | * This will check the space that the inode allocates from to make sure we have |
| 3557 | * enough space for bytes. |
| 3558 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3559 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3560 | { |
| 3561 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3562 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3563 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3564 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3565 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3566 | |
| 3567 | /* make sure bytes are sectorsize aligned */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3568 | bytes = ALIGN(bytes, root->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3569 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3570 | if (root == root->fs_info->tree_root || |
| 3571 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3572 | alloc_chunk = 0; |
| 3573 | committed = 1; |
| 3574 | } |
| 3575 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3576 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3577 | if (!data_sinfo) |
| 3578 | goto alloc; |
| 3579 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3580 | again: |
| 3581 | /* make sure we have enough space to handle the data first */ |
| 3582 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3583 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3584 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3585 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3586 | |
| 3587 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3588 | struct btrfs_trans_handle *trans; |
| 3589 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3590 | /* |
| 3591 | * if we don't have enough free bytes in this space then we need |
| 3592 | * to alloc a new chunk. |
| 3593 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3594 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3595 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3596 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3597 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3598 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3599 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3600 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3601 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3602 | if (IS_ERR(trans)) |
| 3603 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3604 | |
| 3605 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3606 | alloc_target, |
| 3607 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3608 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3609 | if (ret < 0) { |
| 3610 | if (ret != -ENOSPC) |
| 3611 | return ret; |
| 3612 | else |
| 3613 | goto commit_trans; |
| 3614 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3615 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3616 | if (!data_sinfo) |
| 3617 | data_sinfo = fs_info->data_sinfo; |
| 3618 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3619 | goto again; |
| 3620 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3621 | |
| 3622 | /* |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3623 | * If we don't have enough pinned space to deal with this |
| 3624 | * allocation don't bother committing the transaction. |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3625 | */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3626 | if (percpu_counter_compare(&data_sinfo->total_bytes_pinned, |
| 3627 | bytes) < 0) |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3628 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3629 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3630 | |
| 3631 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3632 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3633 | if (!committed && |
| 3634 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3635 | committed = 1; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3636 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3637 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3638 | if (IS_ERR(trans)) |
| 3639 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3640 | ret = btrfs_commit_transaction(trans, root); |
| 3641 | if (ret) |
| 3642 | return ret; |
| 3643 | goto again; |
| 3644 | } |
| 3645 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3646 | return -ENOSPC; |
| 3647 | } |
| 3648 | data_sinfo->bytes_may_use += bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3649 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3650 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3651 | spin_unlock(&data_sinfo->lock); |
| 3652 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3653 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3657 | * Called if we need to clear a data reservation for this inode. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3658 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3659 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3660 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3661 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3662 | struct btrfs_space_info *data_sinfo; |
| 3663 | |
| 3664 | /* make sure bytes are sectorsize aligned */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3665 | bytes = ALIGN(bytes, root->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3666 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3667 | data_sinfo = root->fs_info->data_sinfo; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3668 | spin_lock(&data_sinfo->lock); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 3669 | WARN_ON(data_sinfo->bytes_may_use < bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3670 | data_sinfo->bytes_may_use -= bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3671 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3672 | data_sinfo->flags, bytes, 0); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3673 | spin_unlock(&data_sinfo->lock); |
| 3674 | } |
| 3675 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3676 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3677 | { |
| 3678 | struct list_head *head = &info->space_info; |
| 3679 | struct btrfs_space_info *found; |
| 3680 | |
| 3681 | rcu_read_lock(); |
| 3682 | list_for_each_entry_rcu(found, head, list) { |
| 3683 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3684 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3685 | } |
| 3686 | rcu_read_unlock(); |
| 3687 | } |
| 3688 | |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 3689 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
| 3690 | { |
| 3691 | return (global->size << 1); |
| 3692 | } |
| 3693 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3694 | static int should_alloc_chunk(struct btrfs_root *root, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3695 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3696 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3697 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3698 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3699 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3700 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3701 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3702 | if (force == CHUNK_ALLOC_FORCE) |
| 3703 | return 1; |
| 3704 | |
| 3705 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3706 | * We need to take into account the global rsv because for all intents |
| 3707 | * and purposes it's used space. Don't worry about locking the |
| 3708 | * global_rsv, it doesn't change except when the transaction commits. |
| 3709 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 3710 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 3711 | num_allocated += calc_global_rsv_need_space(global_rsv); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3712 | |
| 3713 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3714 | * in limited mode, we want to have some free space up to |
| 3715 | * about 1% of the FS size. |
| 3716 | */ |
| 3717 | if (force == CHUNK_ALLOC_LIMITED) { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3718 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3719 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3720 | div_factor_fine(thresh, 1)); |
| 3721 | |
| 3722 | if (num_bytes - num_allocated < thresh) |
| 3723 | return 1; |
| 3724 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3725 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3726 | if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3727 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3728 | return 1; |
| 3729 | } |
| 3730 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3731 | static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type) |
| 3732 | { |
| 3733 | u64 num_dev; |
| 3734 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3735 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
| 3736 | BTRFS_BLOCK_GROUP_RAID0 | |
| 3737 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3738 | BTRFS_BLOCK_GROUP_RAID6)) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3739 | num_dev = root->fs_info->fs_devices->rw_devices; |
| 3740 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 3741 | num_dev = 2; |
| 3742 | else |
| 3743 | num_dev = 1; /* DUP or single */ |
| 3744 | |
| 3745 | /* metadata for updaing devices and chunk tree */ |
| 3746 | return btrfs_calc_trans_metadata_size(root, num_dev + 1); |
| 3747 | } |
| 3748 | |
| 3749 | static void check_system_chunk(struct btrfs_trans_handle *trans, |
| 3750 | struct btrfs_root *root, u64 type) |
| 3751 | { |
| 3752 | struct btrfs_space_info *info; |
| 3753 | u64 left; |
| 3754 | u64 thresh; |
| 3755 | |
| 3756 | info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3757 | spin_lock(&info->lock); |
| 3758 | left = info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 3759 | info->bytes_reserved - info->bytes_readonly; |
| 3760 | spin_unlock(&info->lock); |
| 3761 | |
| 3762 | thresh = get_system_chunk_thresh(root, type); |
| 3763 | if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3764 | btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu", |
| 3765 | left, thresh, type); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3766 | dump_space_info(info, 0, 0); |
| 3767 | } |
| 3768 | |
| 3769 | if (left < thresh) { |
| 3770 | u64 flags; |
| 3771 | |
| 3772 | flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0); |
| 3773 | btrfs_alloc_chunk(trans, root, flags); |
| 3774 | } |
| 3775 | } |
| 3776 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3777 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3778 | struct btrfs_root *extent_root, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3779 | { |
| 3780 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3781 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3782 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3783 | int ret = 0; |
| 3784 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3785 | /* Don't re-enter if we're already allocating a chunk */ |
| 3786 | if (trans->allocating_chunk) |
| 3787 | return -ENOSPC; |
| 3788 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3789 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3790 | if (!space_info) { |
| 3791 | ret = update_space_info(extent_root->fs_info, flags, |
| 3792 | 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3793 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3794 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3795 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3796 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3797 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3798 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 3799 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3800 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3801 | if (space_info->full) { |
| 3802 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3803 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3804 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3805 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3806 | if (!should_alloc_chunk(extent_root, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3807 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3808 | return 0; |
| 3809 | } else if (space_info->chunk_alloc) { |
| 3810 | wait_for_alloc = 1; |
| 3811 | } else { |
| 3812 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3813 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3814 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3815 | spin_unlock(&space_info->lock); |
| 3816 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3817 | mutex_lock(&fs_info->chunk_mutex); |
| 3818 | |
| 3819 | /* |
| 3820 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3821 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3822 | * other guy is done and we need to recheck and see if we should |
| 3823 | * allocate. |
| 3824 | */ |
| 3825 | if (wait_for_alloc) { |
| 3826 | mutex_unlock(&fs_info->chunk_mutex); |
| 3827 | wait_for_alloc = 0; |
| 3828 | goto again; |
| 3829 | } |
| 3830 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3831 | trans->allocating_chunk = true; |
| 3832 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3833 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3834 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3835 | * allocating mixed chunks instead of individual chunks. |
| 3836 | */ |
| 3837 | if (btrfs_mixed_space_info(space_info)) |
| 3838 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3839 | |
| 3840 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3841 | * if we're doing a data chunk, go ahead and make sure that |
| 3842 | * we keep a reasonable number of metadata chunks allocated in the |
| 3843 | * FS as well. |
| 3844 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3845 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3846 | fs_info->data_chunk_allocations++; |
| 3847 | if (!(fs_info->data_chunk_allocations % |
| 3848 | fs_info->metadata_ratio)) |
| 3849 | force_metadata_allocation(fs_info); |
| 3850 | } |
| 3851 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3852 | /* |
| 3853 | * Check if we have enough space in SYSTEM chunk because we may need |
| 3854 | * to update devices. |
| 3855 | */ |
| 3856 | check_system_chunk(trans, extent_root, flags); |
| 3857 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3858 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3859 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3860 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3861 | spin_lock(&space_info->lock); |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 3862 | if (ret < 0 && ret != -ENOSPC) |
| 3863 | goto out; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3864 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3865 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3866 | else |
| 3867 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3868 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3869 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 3870 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3871 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3872 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 3873 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3874 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3875 | } |
| 3876 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3877 | static int can_overcommit(struct btrfs_root *root, |
| 3878 | struct btrfs_space_info *space_info, u64 bytes, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 3879 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3880 | { |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 3881 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3882 | u64 profile = btrfs_get_alloc_profile(root, 0); |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 3883 | u64 space_size; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3884 | u64 avail; |
| 3885 | u64 used; |
Josef Bacik | 70afa39 | 2013-02-06 13:53:19 -0500 | [diff] [blame] | 3886 | u64 to_add; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3887 | |
| 3888 | used = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 3889 | space_info->bytes_pinned + space_info->bytes_readonly; |
| 3890 | |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 3891 | /* |
| 3892 | * We only want to allow over committing if we have lots of actual space |
| 3893 | * free, but if we don't have enough space to handle the global reserve |
| 3894 | * space then we could end up having a real enospc problem when trying |
| 3895 | * to allocate a chunk or some other such important allocation. |
| 3896 | */ |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 3897 | spin_lock(&global_rsv->lock); |
| 3898 | space_size = calc_global_rsv_need_space(global_rsv); |
| 3899 | spin_unlock(&global_rsv->lock); |
| 3900 | if (used + space_size >= space_info->total_bytes) |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 3901 | return 0; |
| 3902 | |
| 3903 | used += space_info->bytes_may_use; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3904 | |
| 3905 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3906 | avail = root->fs_info->free_chunk_space; |
| 3907 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3908 | |
| 3909 | /* |
| 3910 | * 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] | 3911 | * space is actually useable. For raid56, the space info used |
| 3912 | * doesn't include the parity drive, so we don't have to |
| 3913 | * change the math |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3914 | */ |
| 3915 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 3916 | BTRFS_BLOCK_GROUP_RAID1 | |
| 3917 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3918 | avail >>= 1; |
| 3919 | |
Josef Bacik | 70afa39 | 2013-02-06 13:53:19 -0500 | [diff] [blame] | 3920 | to_add = space_info->total_bytes; |
| 3921 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3922 | /* |
Miao Xie | 561c294 | 2012-10-16 11:32:18 +0000 | [diff] [blame] | 3923 | * If we aren't flushing all things, let us overcommit up to |
| 3924 | * 1/2th of the space. If we can flush, don't let us overcommit |
| 3925 | * too much, let it overcommit up to 1/8 of the space. |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3926 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 3927 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Josef Bacik | 70afa39 | 2013-02-06 13:53:19 -0500 | [diff] [blame] | 3928 | to_add >>= 3; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3929 | else |
Josef Bacik | 70afa39 | 2013-02-06 13:53:19 -0500 | [diff] [blame] | 3930 | to_add >>= 1; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3931 | |
Josef Bacik | 70afa39 | 2013-02-06 13:53:19 -0500 | [diff] [blame] | 3932 | /* |
| 3933 | * Limit the overcommit to the amount of free space we could possibly |
| 3934 | * allocate for chunks. |
| 3935 | */ |
| 3936 | to_add = min(avail, to_add); |
| 3937 | |
| 3938 | if (used + bytes < space_info->total_bytes + to_add) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3939 | return 1; |
| 3940 | return 0; |
| 3941 | } |
| 3942 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3943 | static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, |
| 3944 | unsigned long nr_pages) |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 3945 | { |
| 3946 | struct super_block *sb = root->fs_info->sb; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 3947 | |
Josef Bacik | 925a6ef | 2013-06-20 12:31:27 -0400 | [diff] [blame] | 3948 | if (down_read_trylock(&sb->s_umount)) { |
| 3949 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); |
| 3950 | up_read(&sb->s_umount); |
| 3951 | } else { |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 3952 | /* |
| 3953 | * We needn't worry the filesystem going from r/w to r/o though |
| 3954 | * we don't acquire ->s_umount mutex, because the filesystem |
| 3955 | * should guarantee the delalloc inodes list be empty after |
| 3956 | * the filesystem is readonly(all dirty pages are written to |
| 3957 | * the disk). |
| 3958 | */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 3959 | btrfs_start_all_delalloc_inodes(root->fs_info, 0); |
Josef Bacik | 98ad69c | 2013-04-04 11:55:49 -0400 | [diff] [blame] | 3960 | if (!current->journal_info) |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 3961 | btrfs_wait_all_ordered_extents(root->fs_info, 0); |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 3962 | } |
| 3963 | } |
| 3964 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3965 | /* |
| 3966 | * shrink metadata reservation for delalloc |
| 3967 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3968 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 3969 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3970 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3971 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3972 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3973 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3974 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3975 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3976 | long time_left; |
Josef Bacik | 877da17 | 2011-10-14 14:02:10 -0400 | [diff] [blame] | 3977 | unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3978 | int loops = 0; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 3979 | enum btrfs_reserve_flush_enum flush; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3980 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3981 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3982 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3983 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3984 | |
| 3985 | smp_mb(); |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 3986 | delalloc_bytes = percpu_counter_sum_positive( |
| 3987 | &root->fs_info->delalloc_bytes); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3988 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3989 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3990 | return; |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 3991 | btrfs_wait_all_ordered_extents(root->fs_info, 0); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3992 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3993 | } |
| 3994 | |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3995 | while (delalloc_bytes && loops < 3) { |
| 3996 | max_reclaim = min(delalloc_bytes, to_reclaim); |
| 3997 | nr_pages = max_reclaim >> PAGE_CACHE_SHIFT; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 3998 | btrfs_writeback_inodes_sb_nr(root, nr_pages); |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 3999 | /* |
| 4000 | * We need to wait for the async pages to actually start before |
| 4001 | * we do anything. |
| 4002 | */ |
| 4003 | wait_event(root->fs_info->async_submit_wait, |
| 4004 | !atomic_read(&root->fs_info->async_delalloc_pages)); |
| 4005 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4006 | if (!trans) |
| 4007 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 4008 | else |
| 4009 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4010 | spin_lock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4011 | if (can_overcommit(root, space_info, orig, flush)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4012 | spin_unlock(&space_info->lock); |
| 4013 | break; |
| 4014 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4015 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4016 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4017 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4018 | if (wait_ordered && !trans) { |
Miao Xie | 199c2a9 | 2013-05-15 07:48:23 +0000 | [diff] [blame] | 4019 | btrfs_wait_all_ordered_extents(root->fs_info, 0); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4020 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4021 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4022 | if (time_left) |
| 4023 | break; |
| 4024 | } |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4025 | smp_mb(); |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4026 | delalloc_bytes = percpu_counter_sum_positive( |
| 4027 | &root->fs_info->delalloc_bytes); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4028 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4029 | } |
| 4030 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4031 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4032 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 4033 | * @root - the root we're allocating for |
| 4034 | * @bytes - the number of bytes we want to reserve |
| 4035 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4036 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4037 | * This will check to make sure that committing the transaction will actually |
| 4038 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 4039 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4040 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4041 | static int may_commit_transaction(struct btrfs_root *root, |
| 4042 | struct btrfs_space_info *space_info, |
| 4043 | u64 bytes, int force) |
| 4044 | { |
| 4045 | struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; |
| 4046 | struct btrfs_trans_handle *trans; |
| 4047 | |
| 4048 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 4049 | if (trans) |
| 4050 | return -EAGAIN; |
| 4051 | |
| 4052 | if (force) |
| 4053 | goto commit; |
| 4054 | |
| 4055 | /* See if there is enough pinned space to make this reservation */ |
| 4056 | spin_lock(&space_info->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4057 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4058 | bytes) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4059 | spin_unlock(&space_info->lock); |
| 4060 | goto commit; |
| 4061 | } |
| 4062 | spin_unlock(&space_info->lock); |
| 4063 | |
| 4064 | /* |
| 4065 | * See if there is some space in the delayed insertion reservation for |
| 4066 | * this reservation. |
| 4067 | */ |
| 4068 | if (space_info != delayed_rsv->space_info) |
| 4069 | return -ENOSPC; |
| 4070 | |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 4071 | spin_lock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4072 | spin_lock(&delayed_rsv->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4073 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4074 | bytes - delayed_rsv->size) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4075 | spin_unlock(&delayed_rsv->lock); |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 4076 | spin_unlock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4077 | return -ENOSPC; |
| 4078 | } |
| 4079 | spin_unlock(&delayed_rsv->lock); |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 4080 | spin_unlock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4081 | |
| 4082 | commit: |
| 4083 | trans = btrfs_join_transaction(root); |
| 4084 | if (IS_ERR(trans)) |
| 4085 | return -ENOSPC; |
| 4086 | |
| 4087 | return btrfs_commit_transaction(trans, root); |
| 4088 | } |
| 4089 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4090 | enum flush_state { |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4091 | FLUSH_DELAYED_ITEMS_NR = 1, |
| 4092 | FLUSH_DELAYED_ITEMS = 2, |
| 4093 | FLUSH_DELALLOC = 3, |
| 4094 | FLUSH_DELALLOC_WAIT = 4, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4095 | ALLOC_CHUNK = 5, |
| 4096 | COMMIT_TRANS = 6, |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4097 | }; |
| 4098 | |
| 4099 | static int flush_space(struct btrfs_root *root, |
| 4100 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 4101 | u64 orig_bytes, int state) |
| 4102 | { |
| 4103 | struct btrfs_trans_handle *trans; |
| 4104 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4105 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4106 | |
| 4107 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4108 | case FLUSH_DELAYED_ITEMS_NR: |
| 4109 | case FLUSH_DELAYED_ITEMS: |
| 4110 | if (state == FLUSH_DELAYED_ITEMS_NR) { |
| 4111 | u64 bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 4112 | |
| 4113 | nr = (int)div64_u64(num_bytes, bytes); |
| 4114 | if (!nr) |
| 4115 | nr = 1; |
| 4116 | nr *= 2; |
| 4117 | } else { |
| 4118 | nr = -1; |
| 4119 | } |
| 4120 | trans = btrfs_join_transaction(root); |
| 4121 | if (IS_ERR(trans)) { |
| 4122 | ret = PTR_ERR(trans); |
| 4123 | break; |
| 4124 | } |
| 4125 | ret = btrfs_run_delayed_items_nr(trans, root, nr); |
| 4126 | btrfs_end_transaction(trans, root); |
| 4127 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4128 | case FLUSH_DELALLOC: |
| 4129 | case FLUSH_DELALLOC_WAIT: |
| 4130 | shrink_delalloc(root, num_bytes, orig_bytes, |
| 4131 | state == FLUSH_DELALLOC_WAIT); |
| 4132 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4133 | case ALLOC_CHUNK: |
| 4134 | trans = btrfs_join_transaction(root); |
| 4135 | if (IS_ERR(trans)) { |
| 4136 | ret = PTR_ERR(trans); |
| 4137 | break; |
| 4138 | } |
| 4139 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4140 | btrfs_get_alloc_profile(root, 0), |
| 4141 | CHUNK_ALLOC_NO_FORCE); |
| 4142 | btrfs_end_transaction(trans, root); |
| 4143 | if (ret == -ENOSPC) |
| 4144 | ret = 0; |
| 4145 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4146 | case COMMIT_TRANS: |
| 4147 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
| 4148 | break; |
| 4149 | default: |
| 4150 | ret = -ENOSPC; |
| 4151 | break; |
| 4152 | } |
| 4153 | |
| 4154 | return ret; |
| 4155 | } |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4156 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4157 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 4158 | * @root - the root we're allocating for |
| 4159 | * @block_rsv - the block_rsv we're allocating for |
| 4160 | * @orig_bytes - the number of bytes we want |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 4161 | * @flush - whether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4162 | * |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4163 | * This will reserve orgi_bytes number of bytes from the space info associated |
| 4164 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 4165 | * flush out space to make room. It will do this by flushing delalloc if |
| 4166 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 4167 | * regain reservations will be made and this will fail if there is not enough |
| 4168 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4169 | */ |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4170 | static int reserve_metadata_bytes(struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4171 | struct btrfs_block_rsv *block_rsv, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4172 | u64 orig_bytes, |
| 4173 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4174 | { |
| 4175 | struct btrfs_space_info *space_info = block_rsv->space_info; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4176 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4177 | u64 num_bytes = orig_bytes; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4178 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4179 | int ret = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4180 | bool flushing = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4181 | |
| 4182 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4183 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4184 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4185 | /* |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4186 | * We only want to wait if somebody other than us is flushing and we |
| 4187 | * are actually allowed to flush all things. |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4188 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4189 | while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing && |
| 4190 | space_info->flush) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4191 | spin_unlock(&space_info->lock); |
| 4192 | /* |
| 4193 | * If we have a trans handle we can't wait because the flusher |
| 4194 | * may have to commit the transaction, which would mean we would |
| 4195 | * deadlock since we are waiting for the flusher to finish, but |
| 4196 | * hold the current transaction open. |
| 4197 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4198 | if (current->journal_info) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4199 | return -EAGAIN; |
Arne Jansen | b9688bb | 2012-04-18 10:27:16 +0200 | [diff] [blame] | 4200 | ret = wait_event_killable(space_info->wait, !space_info->flush); |
| 4201 | /* Must have been killed, return */ |
| 4202 | if (ret) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4203 | return -EINTR; |
| 4204 | |
| 4205 | spin_lock(&space_info->lock); |
| 4206 | } |
| 4207 | |
| 4208 | ret = -ENOSPC; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4209 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4210 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4211 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4212 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4213 | /* |
| 4214 | * The idea here is that we've not already over-reserved the block group |
| 4215 | * then we can go ahead and save our reservation first and then start |
| 4216 | * flushing if we need to. Otherwise if we've already overcommitted |
| 4217 | * lets start flushing stuff first and then come back and try to make |
| 4218 | * our reservation. |
| 4219 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4220 | if (used <= space_info->total_bytes) { |
| 4221 | if (used + orig_bytes <= space_info->total_bytes) { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4222 | space_info->bytes_may_use += orig_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4223 | trace_btrfs_space_reservation(root->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4224 | "space_info", space_info->flags, orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4225 | ret = 0; |
| 4226 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4227 | /* |
| 4228 | * Ok set num_bytes to orig_bytes since we aren't |
| 4229 | * overocmmitted, this way we only try and reclaim what |
| 4230 | * we need. |
| 4231 | */ |
| 4232 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4233 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4234 | } else { |
| 4235 | /* |
| 4236 | * Ok we're over committed, set num_bytes to the overcommitted |
| 4237 | * amount plus the amount of bytes that we need for this |
| 4238 | * reservation. |
| 4239 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4240 | num_bytes = used - space_info->total_bytes + |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4241 | (orig_bytes * 2); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4242 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4243 | |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 4244 | if (ret && can_overcommit(root, space_info, orig_bytes, flush)) { |
| 4245 | space_info->bytes_may_use += orig_bytes; |
| 4246 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
| 4247 | space_info->flags, orig_bytes, |
| 4248 | 1); |
| 4249 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4250 | } |
| 4251 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4252 | /* |
| 4253 | * Couldn't make our reservation, save our place so while we're trying |
| 4254 | * to reclaim space we can actually use it instead of somebody else |
| 4255 | * stealing it from us. |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4256 | * |
| 4257 | * We make the other tasks wait for the flush only when we can flush |
| 4258 | * all things. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4259 | */ |
Josef Bacik | 72bcd99 | 2012-12-18 15:16:34 -0500 | [diff] [blame] | 4260 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4261 | flushing = true; |
| 4262 | space_info->flush = 1; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4263 | } |
| 4264 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4265 | spin_unlock(&space_info->lock); |
| 4266 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4267 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4268 | goto out; |
| 4269 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4270 | ret = flush_space(root, space_info, num_bytes, orig_bytes, |
| 4271 | flush_state); |
| 4272 | flush_state++; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4273 | |
| 4274 | /* |
| 4275 | * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock |
| 4276 | * would happen. So skip delalloc flush. |
| 4277 | */ |
| 4278 | if (flush == BTRFS_RESERVE_FLUSH_LIMIT && |
| 4279 | (flush_state == FLUSH_DELALLOC || |
| 4280 | flush_state == FLUSH_DELALLOC_WAIT)) |
| 4281 | flush_state = ALLOC_CHUNK; |
| 4282 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4283 | if (!ret) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4284 | goto again; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4285 | else if (flush == BTRFS_RESERVE_FLUSH_LIMIT && |
| 4286 | flush_state < COMMIT_TRANS) |
| 4287 | goto again; |
| 4288 | else if (flush == BTRFS_RESERVE_FLUSH_ALL && |
| 4289 | flush_state <= COMMIT_TRANS) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4290 | goto again; |
| 4291 | |
| 4292 | out: |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 4293 | if (ret == -ENOSPC && |
| 4294 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { |
| 4295 | struct btrfs_block_rsv *global_rsv = |
| 4296 | &root->fs_info->global_block_rsv; |
| 4297 | |
| 4298 | if (block_rsv != global_rsv && |
| 4299 | !block_rsv_use_bytes(global_rsv, orig_bytes)) |
| 4300 | ret = 0; |
| 4301 | } |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4302 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4303 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4304 | space_info->flush = 0; |
| 4305 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4306 | spin_unlock(&space_info->lock); |
| 4307 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4308 | return ret; |
| 4309 | } |
| 4310 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4311 | static struct btrfs_block_rsv *get_block_rsv( |
| 4312 | const struct btrfs_trans_handle *trans, |
| 4313 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4314 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4315 | struct btrfs_block_rsv *block_rsv = NULL; |
| 4316 | |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 4317 | if (root->ref_cows) |
| 4318 | block_rsv = trans->block_rsv; |
| 4319 | |
| 4320 | if (root == root->fs_info->csum_root && trans->adding_csums) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4321 | block_rsv = trans->block_rsv; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4322 | |
| 4323 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4324 | block_rsv = root->block_rsv; |
| 4325 | |
| 4326 | if (!block_rsv) |
| 4327 | block_rsv = &root->fs_info->empty_block_rsv; |
| 4328 | |
| 4329 | return block_rsv; |
| 4330 | } |
| 4331 | |
| 4332 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 4333 | u64 num_bytes) |
| 4334 | { |
| 4335 | int ret = -ENOSPC; |
| 4336 | spin_lock(&block_rsv->lock); |
| 4337 | if (block_rsv->reserved >= num_bytes) { |
| 4338 | block_rsv->reserved -= num_bytes; |
| 4339 | if (block_rsv->reserved < block_rsv->size) |
| 4340 | block_rsv->full = 0; |
| 4341 | ret = 0; |
| 4342 | } |
| 4343 | spin_unlock(&block_rsv->lock); |
| 4344 | return ret; |
| 4345 | } |
| 4346 | |
| 4347 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 4348 | u64 num_bytes, int update_size) |
| 4349 | { |
| 4350 | spin_lock(&block_rsv->lock); |
| 4351 | block_rsv->reserved += num_bytes; |
| 4352 | if (update_size) |
| 4353 | block_rsv->size += num_bytes; |
| 4354 | else if (block_rsv->reserved >= block_rsv->size) |
| 4355 | block_rsv->full = 1; |
| 4356 | spin_unlock(&block_rsv->lock); |
| 4357 | } |
| 4358 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4359 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
| 4360 | struct btrfs_block_rsv *dest, u64 num_bytes, |
| 4361 | int min_factor) |
| 4362 | { |
| 4363 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 4364 | u64 min_bytes; |
| 4365 | |
| 4366 | if (global_rsv->space_info != dest->space_info) |
| 4367 | return -ENOSPC; |
| 4368 | |
| 4369 | spin_lock(&global_rsv->lock); |
| 4370 | min_bytes = div_factor(global_rsv->size, min_factor); |
| 4371 | if (global_rsv->reserved < min_bytes + num_bytes) { |
| 4372 | spin_unlock(&global_rsv->lock); |
| 4373 | return -ENOSPC; |
| 4374 | } |
| 4375 | global_rsv->reserved -= num_bytes; |
| 4376 | if (global_rsv->reserved < global_rsv->size) |
| 4377 | global_rsv->full = 0; |
| 4378 | spin_unlock(&global_rsv->lock); |
| 4379 | |
| 4380 | block_rsv_add_bytes(dest, num_bytes, 1); |
| 4381 | return 0; |
| 4382 | } |
| 4383 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4384 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 4385 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 4386 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4387 | { |
| 4388 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 4389 | |
| 4390 | spin_lock(&block_rsv->lock); |
| 4391 | if (num_bytes == (u64)-1) |
| 4392 | num_bytes = block_rsv->size; |
| 4393 | block_rsv->size -= num_bytes; |
| 4394 | if (block_rsv->reserved >= block_rsv->size) { |
| 4395 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 4396 | block_rsv->reserved = block_rsv->size; |
| 4397 | block_rsv->full = 1; |
| 4398 | } else { |
| 4399 | num_bytes = 0; |
| 4400 | } |
| 4401 | spin_unlock(&block_rsv->lock); |
| 4402 | |
| 4403 | if (num_bytes > 0) { |
| 4404 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 4405 | spin_lock(&dest->lock); |
| 4406 | if (!dest->full) { |
| 4407 | u64 bytes_to_add; |
| 4408 | |
| 4409 | bytes_to_add = dest->size - dest->reserved; |
| 4410 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 4411 | dest->reserved += bytes_to_add; |
| 4412 | if (dest->reserved >= dest->size) |
| 4413 | dest->full = 1; |
| 4414 | num_bytes -= bytes_to_add; |
| 4415 | } |
| 4416 | spin_unlock(&dest->lock); |
| 4417 | } |
| 4418 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4419 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4420 | space_info->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4421 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4422 | space_info->flags, num_bytes, 0); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4423 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4424 | spin_unlock(&space_info->lock); |
| 4425 | } |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 4430 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 4431 | { |
| 4432 | int ret; |
| 4433 | |
| 4434 | ret = block_rsv_use_bytes(src, num_bytes); |
| 4435 | if (ret) |
| 4436 | return ret; |
| 4437 | |
| 4438 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 4439 | return 0; |
| 4440 | } |
| 4441 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4442 | 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] | 4443 | { |
| 4444 | memset(rsv, 0, sizeof(*rsv)); |
| 4445 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4446 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4447 | } |
| 4448 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4449 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, |
| 4450 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4451 | { |
| 4452 | struct btrfs_block_rsv *block_rsv; |
| 4453 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4454 | |
| 4455 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 4456 | if (!block_rsv) |
| 4457 | return NULL; |
| 4458 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4459 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4460 | block_rsv->space_info = __find_space_info(fs_info, |
| 4461 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4462 | return block_rsv; |
| 4463 | } |
| 4464 | |
| 4465 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 4466 | struct btrfs_block_rsv *rsv) |
| 4467 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4468 | if (!rsv) |
| 4469 | return; |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 4470 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 4471 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4472 | } |
| 4473 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4474 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 4475 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, |
| 4476 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4477 | { |
| 4478 | int ret; |
| 4479 | |
| 4480 | if (num_bytes == 0) |
| 4481 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4482 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 4483 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4484 | if (!ret) { |
| 4485 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 4486 | return 0; |
| 4487 | } |
| 4488 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4489 | return ret; |
| 4490 | } |
| 4491 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4492 | int btrfs_block_rsv_check(struct btrfs_root *root, |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4493 | struct btrfs_block_rsv *block_rsv, int min_factor) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4494 | { |
| 4495 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4496 | int ret = -ENOSPC; |
| 4497 | |
| 4498 | if (!block_rsv) |
| 4499 | return 0; |
| 4500 | |
| 4501 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4502 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 4503 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4504 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4505 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4506 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4507 | return ret; |
| 4508 | } |
| 4509 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4510 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 4511 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, |
| 4512 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4513 | { |
| 4514 | u64 num_bytes = 0; |
| 4515 | int ret = -ENOSPC; |
| 4516 | |
| 4517 | if (!block_rsv) |
| 4518 | return 0; |
| 4519 | |
| 4520 | spin_lock(&block_rsv->lock); |
| 4521 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4522 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4523 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4524 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4525 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4526 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4527 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4528 | if (!ret) |
| 4529 | return 0; |
| 4530 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 4531 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 4532 | if (!ret) { |
| 4533 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4534 | return 0; |
| 4535 | } |
| 4536 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4537 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 4541 | struct btrfs_block_rsv *dst_rsv, |
| 4542 | u64 num_bytes) |
| 4543 | { |
| 4544 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4545 | } |
| 4546 | |
| 4547 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 4548 | struct btrfs_block_rsv *block_rsv, |
| 4549 | u64 num_bytes) |
| 4550 | { |
| 4551 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 4552 | if (global_rsv->full || global_rsv == block_rsv || |
| 4553 | block_rsv->space_info != global_rsv->space_info) |
| 4554 | global_rsv = NULL; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4555 | block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv, |
| 4556 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4557 | } |
| 4558 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4559 | /* |
| 4560 | * helper to calculate size of global block reservation. |
| 4561 | * the desired value is sum of space used by extent tree, |
| 4562 | * checksum tree and root tree |
| 4563 | */ |
| 4564 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 4565 | { |
| 4566 | struct btrfs_space_info *sinfo; |
| 4567 | u64 num_bytes; |
| 4568 | u64 meta_used; |
| 4569 | u64 data_used; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4570 | int csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4571 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4572 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 4573 | spin_lock(&sinfo->lock); |
| 4574 | data_used = sinfo->bytes_used; |
| 4575 | spin_unlock(&sinfo->lock); |
| 4576 | |
| 4577 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4578 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 4579 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 4580 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4581 | meta_used = sinfo->bytes_used; |
| 4582 | spin_unlock(&sinfo->lock); |
| 4583 | |
| 4584 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 4585 | csum_size * 2; |
| 4586 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 4587 | |
| 4588 | if (num_bytes * 3 > meta_used) |
Chris Mason | 8e62c2d | 2012-04-12 13:46:48 -0400 | [diff] [blame] | 4589 | num_bytes = div64_u64(meta_used, 3); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4590 | |
| 4591 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 4592 | } |
| 4593 | |
| 4594 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4595 | { |
| 4596 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 4597 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 4598 | u64 num_bytes; |
| 4599 | |
| 4600 | num_bytes = calc_global_metadata_size(fs_info); |
| 4601 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4602 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 4603 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4604 | |
Josef Bacik | fdf30d1 | 2013-03-26 15:31:45 -0400 | [diff] [blame] | 4605 | block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4606 | |
| 4607 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 4608 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 4609 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4610 | |
| 4611 | if (sinfo->total_bytes > num_bytes) { |
| 4612 | num_bytes = sinfo->total_bytes - num_bytes; |
| 4613 | block_rsv->reserved += num_bytes; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4614 | sinfo->bytes_may_use += num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4615 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4616 | sinfo->flags, num_bytes, 1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | if (block_rsv->reserved >= block_rsv->size) { |
| 4620 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4621 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4622 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4623 | sinfo->flags, num_bytes, 0); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4624 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4625 | block_rsv->reserved = block_rsv->size; |
| 4626 | block_rsv->full = 1; |
| 4627 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 4628 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4629 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 4630 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4631 | } |
| 4632 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4633 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4634 | { |
| 4635 | struct btrfs_space_info *space_info; |
| 4636 | |
| 4637 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 4638 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4639 | |
| 4640 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4641 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4642 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4643 | fs_info->trans_block_rsv.space_info = space_info; |
| 4644 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4645 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4646 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4647 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 4648 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 4649 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 4650 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Stefan Behrens | 3a6cad9 | 2013-05-16 14:48:19 +0000 | [diff] [blame] | 4651 | if (fs_info->quota_root) |
| 4652 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4653 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4654 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4655 | update_global_block_rsv(fs_info); |
| 4656 | } |
| 4657 | |
| 4658 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4659 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4660 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 4661 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4662 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 4663 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 4664 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 4665 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 4666 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 4667 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4668 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 4669 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4670 | } |
| 4671 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4672 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 4673 | struct btrfs_root *root) |
| 4674 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 4675 | if (!trans->block_rsv) |
| 4676 | return; |
| 4677 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4678 | if (!trans->bytes_reserved) |
| 4679 | return; |
| 4680 | |
Chris Mason | e77266e | 2012-02-24 10:39:05 -0500 | [diff] [blame] | 4681 | trace_btrfs_space_reservation(root->fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4682 | trans->transid, trans->bytes_reserved, 0); |
Josef Bacik | b24e03d | 2011-10-14 14:40:17 -0400 | [diff] [blame] | 4683 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4684 | trans->bytes_reserved = 0; |
| 4685 | } |
| 4686 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4687 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4688 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4689 | struct inode *inode) |
| 4690 | { |
| 4691 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4692 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4693 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 4694 | |
| 4695 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4696 | * We need to hold space in order to delete our orphan item once we've |
| 4697 | * added it, so this takes the reservation so we can release it later |
| 4698 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4699 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4700 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4701 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 4702 | btrfs_ino(inode), num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4703 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4704 | } |
| 4705 | |
| 4706 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 4707 | { |
| 4708 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4709 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4710 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 4711 | btrfs_ino(inode), num_bytes, 0); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4712 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 4713 | } |
| 4714 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 4715 | /* |
| 4716 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation |
| 4717 | * root: the root of the parent directory |
| 4718 | * rsv: block reservation |
| 4719 | * items: the number of items that we need do reservation |
| 4720 | * qgroup_reserved: used to return the reserved size in qgroup |
| 4721 | * |
| 4722 | * This function is used to reserve the space for snapshot/subvolume |
| 4723 | * creation and deletion. Those operations are different with the |
| 4724 | * common file/directory operations, they change two fs/file trees |
| 4725 | * and root tree, the number of items that the qgroup reserves is |
| 4726 | * different with the free space reservation. So we can not use |
| 4727 | * the space reseravtion mechanism in start_transaction(). |
| 4728 | */ |
| 4729 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, |
| 4730 | struct btrfs_block_rsv *rsv, |
| 4731 | int items, |
| 4732 | u64 *qgroup_reserved) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4733 | { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 4734 | u64 num_bytes; |
| 4735 | int ret; |
| 4736 | |
| 4737 | if (root->fs_info->quota_enabled) { |
| 4738 | /* One for parent inode, two for dir entries */ |
| 4739 | num_bytes = 3 * root->leafsize; |
| 4740 | ret = btrfs_qgroup_reserve(root, num_bytes); |
| 4741 | if (ret) |
| 4742 | return ret; |
| 4743 | } else { |
| 4744 | num_bytes = 0; |
| 4745 | } |
| 4746 | |
| 4747 | *qgroup_reserved = num_bytes; |
| 4748 | |
| 4749 | num_bytes = btrfs_calc_trans_metadata_size(root, items); |
| 4750 | rsv->space_info = __find_space_info(root->fs_info, |
| 4751 | BTRFS_BLOCK_GROUP_METADATA); |
| 4752 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, |
| 4753 | BTRFS_RESERVE_FLUSH_ALL); |
| 4754 | if (ret) { |
| 4755 | if (*qgroup_reserved) |
| 4756 | btrfs_qgroup_free(root, *qgroup_reserved); |
| 4757 | } |
| 4758 | |
| 4759 | return ret; |
| 4760 | } |
| 4761 | |
| 4762 | void btrfs_subvolume_release_metadata(struct btrfs_root *root, |
| 4763 | struct btrfs_block_rsv *rsv, |
| 4764 | u64 qgroup_reserved) |
| 4765 | { |
| 4766 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 4767 | if (qgroup_reserved) |
| 4768 | btrfs_qgroup_free(root, qgroup_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4769 | } |
| 4770 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4771 | /** |
| 4772 | * drop_outstanding_extent - drop an outstanding extent |
| 4773 | * @inode: the inode we're dropping the extent for |
| 4774 | * |
| 4775 | * This is called when we are freeing up an outstanding extent, either called |
| 4776 | * after an error or after an extent is written. This will return the number of |
| 4777 | * reserved extents that need to be freed. This must be called with |
| 4778 | * BTRFS_I(inode)->lock held. |
| 4779 | */ |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4780 | static unsigned drop_outstanding_extent(struct inode *inode) |
| 4781 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4782 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4783 | unsigned dropped_extents = 0; |
| 4784 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4785 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
| 4786 | BTRFS_I(inode)->outstanding_extents--; |
| 4787 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4788 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4789 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 4790 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4791 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4792 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4793 | /* |
| 4794 | * If we have more or the same amount of outsanding extents than we have |
| 4795 | * reserved then we need to leave the reserved extents count alone. |
| 4796 | */ |
| 4797 | if (BTRFS_I(inode)->outstanding_extents >= |
| 4798 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4799 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4800 | |
| 4801 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 4802 | BTRFS_I(inode)->outstanding_extents; |
| 4803 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4804 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4807 | /** |
| 4808 | * calc_csum_metadata_size - return the amount of metada space that must be |
| 4809 | * reserved/free'd for the given bytes. |
| 4810 | * @inode: the inode we're manipulating |
| 4811 | * @num_bytes: the number of bytes in question |
| 4812 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 4813 | * |
| 4814 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 4815 | * correct amount of metadata that must either be reserved or freed. We |
| 4816 | * calculate how many checksums we can fit into one leaf and then divide the |
| 4817 | * number of bytes that will need to be checksumed by this value to figure out |
| 4818 | * how many checksums will be required. If we are adding bytes then the number |
| 4819 | * may go up and we will return the number of additional bytes that must be |
| 4820 | * reserved. If it is going down we will return the number of bytes that must |
| 4821 | * be freed. |
| 4822 | * |
| 4823 | * This must be called with BTRFS_I(inode)->lock held. |
| 4824 | */ |
| 4825 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 4826 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4827 | { |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4828 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4829 | u64 csum_size; |
| 4830 | int num_csums_per_leaf; |
| 4831 | int num_csums; |
| 4832 | int old_csums; |
| 4833 | |
| 4834 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 4835 | BTRFS_I(inode)->csum_bytes == 0) |
| 4836 | return 0; |
| 4837 | |
| 4838 | old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 4839 | if (reserve) |
| 4840 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 4841 | else |
| 4842 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
| 4843 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); |
| 4844 | num_csums_per_leaf = (int)div64_u64(csum_size, |
| 4845 | sizeof(struct btrfs_csum_item) + |
| 4846 | sizeof(struct btrfs_disk_key)); |
| 4847 | num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 4848 | num_csums = num_csums + num_csums_per_leaf - 1; |
| 4849 | num_csums = num_csums / num_csums_per_leaf; |
| 4850 | |
| 4851 | old_csums = old_csums + num_csums_per_leaf - 1; |
| 4852 | old_csums = old_csums / num_csums_per_leaf; |
| 4853 | |
| 4854 | /* No change, no need to reserve more */ |
| 4855 | if (old_csums == num_csums) |
| 4856 | return 0; |
| 4857 | |
| 4858 | if (reserve) |
| 4859 | return btrfs_calc_trans_metadata_size(root, |
| 4860 | num_csums - old_csums); |
| 4861 | |
| 4862 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 4866 | { |
| 4867 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4868 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4869 | u64 to_reserve = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4870 | u64 csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4871 | unsigned nr_extents = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4872 | int extra_reserve = 0; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4873 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; |
Jan Schmidt | eb6b88d | 2013-01-27 23:26:00 -0700 | [diff] [blame] | 4874 | int ret = 0; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 4875 | bool delalloc_lock = true; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4876 | u64 to_free = 0; |
| 4877 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4878 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 4879 | /* If we are a free space inode we need to not flush since we will be in |
| 4880 | * the middle of a transaction commit. We also don't need the delalloc |
| 4881 | * mutex since we won't race with anybody. We need this mostly to make |
| 4882 | * lockdep shut its filthy mouth. |
| 4883 | */ |
| 4884 | if (btrfs_is_free_space_inode(inode)) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4885 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 4886 | delalloc_lock = false; |
| 4887 | } |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4888 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4889 | if (flush != BTRFS_RESERVE_NO_FLUSH && |
| 4890 | btrfs_transaction_in_commit(root->fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4891 | schedule_timeout(1); |
| 4892 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 4893 | if (delalloc_lock) |
| 4894 | mutex_lock(&BTRFS_I(inode)->delalloc_mutex); |
| 4895 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4896 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4897 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4898 | spin_lock(&BTRFS_I(inode)->lock); |
| 4899 | BTRFS_I(inode)->outstanding_extents++; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4900 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4901 | if (BTRFS_I(inode)->outstanding_extents > |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4902 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4903 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 4904 | BTRFS_I(inode)->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4905 | |
| 4906 | /* |
| 4907 | * Add an item to reserve for updating the inode when we complete the |
| 4908 | * delalloc io. |
| 4909 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4910 | if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 4911 | &BTRFS_I(inode)->runtime_flags)) { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4912 | nr_extents++; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4913 | extra_reserve = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4917 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4918 | csum_bytes = BTRFS_I(inode)->csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4919 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4920 | |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4921 | if (root->fs_info->quota_enabled) { |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 4922 | ret = btrfs_qgroup_reserve(root, num_bytes + |
| 4923 | nr_extents * root->leafsize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4924 | if (ret) |
| 4925 | goto out_fail; |
Wang Shilong | a9870c0 | 2013-03-01 11:33:01 +0000 | [diff] [blame] | 4926 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 4927 | |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4928 | ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); |
| 4929 | if (unlikely(ret)) { |
| 4930 | if (root->fs_info->quota_enabled) |
Miao Xie | 4b5829a | 2012-12-05 10:53:25 +0000 | [diff] [blame] | 4931 | btrfs_qgroup_free(root, num_bytes + |
| 4932 | nr_extents * root->leafsize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4933 | goto out_fail; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4934 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4935 | |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4936 | spin_lock(&BTRFS_I(inode)->lock); |
| 4937 | if (extra_reserve) { |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4938 | set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 4939 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4940 | nr_extents--; |
| 4941 | } |
| 4942 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 4943 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 4944 | |
| 4945 | if (delalloc_lock) |
| 4946 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4947 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4948 | if (to_reserve) |
| 4949 | trace_btrfs_space_reservation(root->fs_info,"delalloc", |
| 4950 | btrfs_ino(inode), to_reserve, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4951 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4952 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4953 | return 0; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4954 | |
| 4955 | out_fail: |
| 4956 | spin_lock(&BTRFS_I(inode)->lock); |
| 4957 | dropped = drop_outstanding_extent(inode); |
| 4958 | /* |
| 4959 | * If the inodes csum_bytes is the same as the original |
| 4960 | * csum_bytes then we know we haven't raced with any free()ers |
| 4961 | * so we can just reduce our inodes csum bytes and carry on. |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4962 | */ |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 4963 | if (BTRFS_I(inode)->csum_bytes == csum_bytes) { |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 4964 | calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 4965 | } else { |
| 4966 | u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes; |
| 4967 | u64 bytes; |
| 4968 | |
| 4969 | /* |
| 4970 | * This is tricky, but first we need to figure out how much we |
| 4971 | * free'd from any free-ers that occured during this |
| 4972 | * reservation, so we reset ->csum_bytes to the csum_bytes |
| 4973 | * before we dropped our lock, and then call the free for the |
| 4974 | * number of bytes that were freed while we were trying our |
| 4975 | * reservation. |
| 4976 | */ |
| 4977 | bytes = csum_bytes - BTRFS_I(inode)->csum_bytes; |
| 4978 | BTRFS_I(inode)->csum_bytes = csum_bytes; |
| 4979 | to_free = calc_csum_metadata_size(inode, bytes, 0); |
| 4980 | |
| 4981 | |
| 4982 | /* |
| 4983 | * Now we need to see how much we would have freed had we not |
| 4984 | * been making this reservation and our ->csum_bytes were not |
| 4985 | * artificially inflated. |
| 4986 | */ |
| 4987 | BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes; |
| 4988 | bytes = csum_bytes - orig_csum_bytes; |
| 4989 | bytes = calc_csum_metadata_size(inode, bytes, 0); |
| 4990 | |
| 4991 | /* |
| 4992 | * Now reset ->csum_bytes to what it should be. If bytes is |
| 4993 | * more than to_free then we would have free'd more space had we |
| 4994 | * not had an artificially high ->csum_bytes, so we need to free |
| 4995 | * the remainder. If bytes is the same or less then we don't |
| 4996 | * need to do anything, the other free-ers did the correct |
| 4997 | * thing. |
| 4998 | */ |
| 4999 | BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes; |
| 5000 | if (bytes > to_free) |
| 5001 | to_free = bytes - to_free; |
| 5002 | else |
| 5003 | to_free = 0; |
| 5004 | } |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5005 | spin_unlock(&BTRFS_I(inode)->lock); |
| 5006 | if (dropped) |
| 5007 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
| 5008 | |
| 5009 | if (to_free) { |
| 5010 | btrfs_block_rsv_release(root, block_rsv, to_free); |
| 5011 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 5012 | btrfs_ino(inode), to_free, 0); |
| 5013 | } |
| 5014 | if (delalloc_lock) |
| 5015 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
| 5016 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5017 | } |
| 5018 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5019 | /** |
| 5020 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 5021 | * @inode: the inode to release the reservation for |
| 5022 | * @num_bytes: the number of bytes we're releasing |
| 5023 | * |
| 5024 | * This will release the metadata reservation for an inode. This can be called |
| 5025 | * once we complete IO for a given set of bytes to release their metadata |
| 5026 | * reservations. |
| 5027 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5028 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 5029 | { |
| 5030 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5031 | u64 to_free = 0; |
| 5032 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5033 | |
| 5034 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5035 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5036 | dropped = drop_outstanding_extent(inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5037 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 5038 | if (num_bytes) |
| 5039 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5040 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5041 | if (dropped > 0) |
| 5042 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5043 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5044 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 5045 | btrfs_ino(inode), to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5046 | if (root->fs_info->quota_enabled) { |
| 5047 | btrfs_qgroup_free(root, num_bytes + |
| 5048 | dropped * root->leafsize); |
| 5049 | } |
| 5050 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5051 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 5052 | to_free); |
| 5053 | } |
| 5054 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5055 | /** |
| 5056 | * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc |
| 5057 | * @inode: inode we're writing to |
| 5058 | * @num_bytes: the number of bytes we want to allocate |
| 5059 | * |
| 5060 | * This will do the following things |
| 5061 | * |
| 5062 | * o reserve space in the data space info for num_bytes |
| 5063 | * o reserve space in the metadata space info based on number of outstanding |
| 5064 | * extents and how much csums will be needed |
| 5065 | * o add to the inodes ->delalloc_bytes |
| 5066 | * o add it to the fs_info's delalloc inodes list. |
| 5067 | * |
| 5068 | * This will return 0 for success and -ENOSPC if there is no space left. |
| 5069 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5070 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 5071 | { |
| 5072 | int ret; |
| 5073 | |
| 5074 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 5075 | if (ret) |
| 5076 | return ret; |
| 5077 | |
| 5078 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 5079 | if (ret) { |
| 5080 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 5081 | return ret; |
| 5082 | } |
| 5083 | |
| 5084 | return 0; |
| 5085 | } |
| 5086 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5087 | /** |
| 5088 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
| 5089 | * @inode: inode we're releasing space for |
| 5090 | * @num_bytes: the number of bytes we want to free up |
| 5091 | * |
| 5092 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 5093 | * called in the case that we don't need the metadata AND data reservations |
| 5094 | * anymore. So if there is an error or we insert an inline extent. |
| 5095 | * |
| 5096 | * This function will release the metadata space that was not used and will |
| 5097 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 5098 | * list if there are no delalloc bytes left. |
| 5099 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5100 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 5101 | { |
| 5102 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 5103 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 5104 | } |
| 5105 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 5106 | static int update_block_group(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5107 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5108 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5109 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5110 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5111 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5112 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5113 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5114 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5115 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5116 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 5117 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5118 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5119 | if (alloc) |
| 5120 | old_val += num_bytes; |
| 5121 | else |
| 5122 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5123 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 5124 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5125 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5126 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5127 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5128 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5129 | return -ENOENT; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5130 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 5131 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5132 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5133 | factor = 2; |
| 5134 | else |
| 5135 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5136 | /* |
| 5137 | * If this block group has free space cache written out, we |
| 5138 | * need to make sure to load it if we are removing space. This |
| 5139 | * is because we need the unpinning stage to actually add the |
| 5140 | * space back to the block group, otherwise we will leak space. |
| 5141 | */ |
| 5142 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 5143 | cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5144 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5145 | byte_in_group = bytenr - cache->key.objectid; |
| 5146 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5147 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5148 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5149 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5150 | |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 5151 | if (btrfs_test_opt(root, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5152 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 5153 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 5154 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5155 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5156 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5157 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5158 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5159 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5160 | btrfs_set_block_group_used(&cache->item, old_val); |
| 5161 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5162 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5163 | cache->space_info->bytes_used += num_bytes; |
| 5164 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5165 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5166 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5167 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5168 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5169 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5170 | cache->pinned += num_bytes; |
| 5171 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5172 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5173 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5174 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5175 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5176 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5177 | set_extent_dirty(info->pinned_extents, |
| 5178 | bytenr, bytenr + num_bytes - 1, |
| 5179 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5180 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5181 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5182 | total -= num_bytes; |
| 5183 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5184 | } |
| 5185 | return 0; |
| 5186 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5187 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5188 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 5189 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5190 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5191 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5192 | |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 5193 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 5194 | bytenr = root->fs_info->first_logical_byte; |
| 5195 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 5196 | |
| 5197 | if (bytenr < (u64)-1) |
| 5198 | return bytenr; |
| 5199 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5200 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 5201 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5202 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5203 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5204 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5205 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5206 | |
| 5207 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5208 | } |
| 5209 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5210 | static int pin_down_extent(struct btrfs_root *root, |
| 5211 | struct btrfs_block_group_cache *cache, |
| 5212 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5213 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5214 | spin_lock(&cache->space_info->lock); |
| 5215 | spin_lock(&cache->lock); |
| 5216 | cache->pinned += num_bytes; |
| 5217 | cache->space_info->bytes_pinned += num_bytes; |
| 5218 | if (reserved) { |
| 5219 | cache->reserved -= num_bytes; |
| 5220 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5221 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5222 | spin_unlock(&cache->lock); |
| 5223 | spin_unlock(&cache->space_info->lock); |
| 5224 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5225 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 5226 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5227 | return 0; |
| 5228 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5229 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5230 | /* |
| 5231 | * this function must be called within transaction |
| 5232 | */ |
| 5233 | int btrfs_pin_extent(struct btrfs_root *root, |
| 5234 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5235 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5236 | struct btrfs_block_group_cache *cache; |
| 5237 | |
| 5238 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5239 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5240 | |
| 5241 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 5242 | |
| 5243 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5244 | return 0; |
| 5245 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5246 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5247 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5248 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5249 | */ |
Liu Bo | dcfac41 | 2012-12-27 09:01:20 +0000 | [diff] [blame] | 5250 | int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5251 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5252 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5253 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5254 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5255 | |
| 5256 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5257 | if (!cache) |
| 5258 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5259 | |
| 5260 | /* |
| 5261 | * pull in the free space cache (if any) so that our pin |
| 5262 | * removes the free space from the cache. We have load_only set |
| 5263 | * to one because the slow code to read in the free extents does check |
| 5264 | * the pinned extents. |
| 5265 | */ |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 5266 | cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5267 | |
| 5268 | pin_down_extent(root, cache, bytenr, num_bytes, 0); |
| 5269 | |
| 5270 | /* 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] | 5271 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5272 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5273 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5274 | } |
| 5275 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 5276 | static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes) |
| 5277 | { |
| 5278 | int ret; |
| 5279 | struct btrfs_block_group_cache *block_group; |
| 5280 | struct btrfs_caching_control *caching_ctl; |
| 5281 | |
| 5282 | block_group = btrfs_lookup_block_group(root->fs_info, start); |
| 5283 | if (!block_group) |
| 5284 | return -EINVAL; |
| 5285 | |
| 5286 | cache_block_group(block_group, 0); |
| 5287 | caching_ctl = get_caching_control(block_group); |
| 5288 | |
| 5289 | if (!caching_ctl) { |
| 5290 | /* Logic error */ |
| 5291 | BUG_ON(!block_group_cache_done(block_group)); |
| 5292 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5293 | } else { |
| 5294 | mutex_lock(&caching_ctl->mutex); |
| 5295 | |
| 5296 | if (start >= caching_ctl->progress) { |
| 5297 | ret = add_excluded_extent(root, start, num_bytes); |
| 5298 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5299 | ret = btrfs_remove_free_space(block_group, |
| 5300 | start, num_bytes); |
| 5301 | } else { |
| 5302 | num_bytes = caching_ctl->progress - start; |
| 5303 | ret = btrfs_remove_free_space(block_group, |
| 5304 | start, num_bytes); |
| 5305 | if (ret) |
| 5306 | goto out_lock; |
| 5307 | |
| 5308 | num_bytes = (start + num_bytes) - |
| 5309 | caching_ctl->progress; |
| 5310 | start = caching_ctl->progress; |
| 5311 | ret = add_excluded_extent(root, start, num_bytes); |
| 5312 | } |
| 5313 | out_lock: |
| 5314 | mutex_unlock(&caching_ctl->mutex); |
| 5315 | put_caching_control(caching_ctl); |
| 5316 | } |
| 5317 | btrfs_put_block_group(block_group); |
| 5318 | return ret; |
| 5319 | } |
| 5320 | |
| 5321 | int btrfs_exclude_logged_extents(struct btrfs_root *log, |
| 5322 | struct extent_buffer *eb) |
| 5323 | { |
| 5324 | struct btrfs_file_extent_item *item; |
| 5325 | struct btrfs_key key; |
| 5326 | int found_type; |
| 5327 | int i; |
| 5328 | |
| 5329 | if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) |
| 5330 | return 0; |
| 5331 | |
| 5332 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 5333 | btrfs_item_key_to_cpu(eb, &key, i); |
| 5334 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 5335 | continue; |
| 5336 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 5337 | found_type = btrfs_file_extent_type(eb, item); |
| 5338 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 5339 | continue; |
| 5340 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 5341 | continue; |
| 5342 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 5343 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 5344 | __exclude_logged_extent(log, key.objectid, key.offset); |
| 5345 | } |
| 5346 | |
| 5347 | return 0; |
| 5348 | } |
| 5349 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5350 | /** |
| 5351 | * btrfs_update_reserved_bytes - update the block_group and space info counters |
| 5352 | * @cache: The cache we are manipulating |
| 5353 | * @num_bytes: The number of bytes in question |
| 5354 | * @reserve: One of the reservation enums |
| 5355 | * |
| 5356 | * This is called by the allocator when it reserves space, or by somebody who is |
| 5357 | * freeing space that was never actually used on disk. For example if you |
| 5358 | * reserve some space for a new leaf in transaction A and before transaction A |
| 5359 | * commits you free that leaf, you call this with reserve set to 0 in order to |
| 5360 | * clear the reservation. |
| 5361 | * |
| 5362 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper |
| 5363 | * ENOSPC accounting. For data we handle the reservation through clearing the |
| 5364 | * delalloc bits in the io_tree. We have to do this since we could end up |
| 5365 | * allocating less disk space for the amount of data we have reserved in the |
| 5366 | * case of compression. |
| 5367 | * |
| 5368 | * If this is a reservation and the block group has become read only we cannot |
| 5369 | * make the reservation and return -EAGAIN, otherwise this function always |
| 5370 | * succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5371 | */ |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5372 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 5373 | u64 num_bytes, int reserve) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5374 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5375 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5376 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5377 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5378 | spin_lock(&space_info->lock); |
| 5379 | spin_lock(&cache->lock); |
| 5380 | if (reserve != RESERVE_FREE) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5381 | if (cache->ro) { |
| 5382 | ret = -EAGAIN; |
| 5383 | } else { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5384 | cache->reserved += num_bytes; |
| 5385 | space_info->bytes_reserved += num_bytes; |
| 5386 | if (reserve == RESERVE_ALLOC) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5387 | trace_btrfs_space_reservation(cache->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5388 | "space_info", space_info->flags, |
| 5389 | num_bytes, 0); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5390 | space_info->bytes_may_use -= num_bytes; |
| 5391 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5392 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5393 | } else { |
| 5394 | if (cache->ro) |
| 5395 | space_info->bytes_readonly += num_bytes; |
| 5396 | cache->reserved -= num_bytes; |
| 5397 | space_info->bytes_reserved -= num_bytes; |
| 5398 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5399 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5400 | spin_unlock(&cache->lock); |
| 5401 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5402 | return ret; |
| 5403 | } |
| 5404 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5405 | void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5406 | struct btrfs_root *root) |
| 5407 | { |
| 5408 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5409 | struct btrfs_caching_control *next; |
| 5410 | struct btrfs_caching_control *caching_ctl; |
| 5411 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5412 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5413 | |
| 5414 | down_write(&fs_info->extent_commit_sem); |
| 5415 | |
| 5416 | list_for_each_entry_safe(caching_ctl, next, |
| 5417 | &fs_info->caching_block_groups, list) { |
| 5418 | cache = caching_ctl->block_group; |
| 5419 | if (block_group_cache_done(cache)) { |
| 5420 | cache->last_byte_to_unpin = (u64)-1; |
| 5421 | list_del_init(&caching_ctl->list); |
| 5422 | put_caching_control(caching_ctl); |
| 5423 | } else { |
| 5424 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 5425 | } |
| 5426 | } |
| 5427 | |
| 5428 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 5429 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 5430 | else |
| 5431 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 5432 | |
| 5433 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5434 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5435 | list_for_each_entry_rcu(space_info, &fs_info->space_info, list) |
| 5436 | percpu_counter_set(&space_info->total_bytes_pinned, 0); |
| 5437 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5438 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 5442 | { |
| 5443 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5444 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5445 | struct btrfs_space_info *space_info; |
| 5446 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5447 | u64 len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5448 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5449 | |
| 5450 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5451 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5452 | if (!cache || |
| 5453 | start >= cache->key.objectid + cache->key.offset) { |
| 5454 | if (cache) |
| 5455 | btrfs_put_block_group(cache); |
| 5456 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5457 | BUG_ON(!cache); /* Logic error */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | len = cache->key.objectid + cache->key.offset - start; |
| 5461 | len = min(len, end + 1 - start); |
| 5462 | |
| 5463 | if (start < cache->last_byte_to_unpin) { |
| 5464 | len = min(len, cache->last_byte_to_unpin - start); |
| 5465 | btrfs_add_free_space(cache, start, len); |
| 5466 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5467 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5468 | start += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5469 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5470 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5471 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5472 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5473 | cache->pinned -= len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5474 | space_info->bytes_pinned -= len; |
| 5475 | if (cache->ro) { |
| 5476 | space_info->bytes_readonly += len; |
| 5477 | readonly = true; |
| 5478 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5479 | spin_unlock(&cache->lock); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5480 | if (!readonly && global_rsv->space_info == space_info) { |
| 5481 | spin_lock(&global_rsv->lock); |
| 5482 | if (!global_rsv->full) { |
| 5483 | len = min(len, global_rsv->size - |
| 5484 | global_rsv->reserved); |
| 5485 | global_rsv->reserved += len; |
| 5486 | space_info->bytes_may_use += len; |
| 5487 | if (global_rsv->reserved >= global_rsv->size) |
| 5488 | global_rsv->full = 1; |
| 5489 | } |
| 5490 | spin_unlock(&global_rsv->lock); |
| 5491 | } |
| 5492 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5496 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5497 | return 0; |
| 5498 | } |
| 5499 | |
| 5500 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5501 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5502 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5503 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5504 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5505 | u64 start; |
| 5506 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5507 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5508 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5509 | if (trans->aborted) |
| 5510 | return 0; |
| 5511 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5512 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 5513 | unpin = &fs_info->freed_extents[1]; |
| 5514 | else |
| 5515 | unpin = &fs_info->freed_extents[0]; |
| 5516 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5517 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5518 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 5519 | EXTENT_DIRTY, NULL); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5520 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5521 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5522 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5523 | if (btrfs_test_opt(root, DISCARD)) |
| 5524 | ret = btrfs_discard_extent(root, start, |
| 5525 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5526 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5527 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5528 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5529 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5530 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5531 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 5532 | return 0; |
| 5533 | } |
| 5534 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5535 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
| 5536 | u64 owner, u64 root_objectid) |
| 5537 | { |
| 5538 | struct btrfs_space_info *space_info; |
| 5539 | u64 flags; |
| 5540 | |
| 5541 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 5542 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) |
| 5543 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 5544 | else |
| 5545 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 5546 | } else { |
| 5547 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 5548 | } |
| 5549 | |
| 5550 | space_info = __find_space_info(fs_info, flags); |
| 5551 | BUG_ON(!space_info); /* Logic bug */ |
| 5552 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); |
| 5553 | } |
| 5554 | |
| 5555 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5556 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 5557 | struct btrfs_root *root, |
| 5558 | u64 bytenr, u64 num_bytes, u64 parent, |
| 5559 | u64 root_objectid, u64 owner_objectid, |
| 5560 | u64 owner_offset, int refs_to_drop, |
| 5561 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5562 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5563 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5564 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 5565 | struct btrfs_fs_info *info = root->fs_info; |
| 5566 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5567 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5568 | struct btrfs_extent_item *ei; |
| 5569 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5570 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5571 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5572 | int extent_slot = 0; |
| 5573 | int found_extent = 0; |
| 5574 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5575 | u32 item_size; |
| 5576 | u64 refs; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5577 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 5578 | SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 5579 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5580 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5581 | if (!path) |
| 5582 | return -ENOMEM; |
| 5583 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 5584 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5585 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5586 | |
| 5587 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 5588 | BUG_ON(!is_data && refs_to_drop != 1); |
| 5589 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5590 | if (is_data) |
| 5591 | skinny_metadata = 0; |
| 5592 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5593 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 5594 | bytenr, num_bytes, parent, |
| 5595 | root_objectid, owner_objectid, |
| 5596 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5597 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5598 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5599 | while (extent_slot >= 0) { |
| 5600 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5601 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5602 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5603 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5604 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 5605 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5606 | found_extent = 1; |
| 5607 | break; |
| 5608 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5609 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 5610 | key.offset == owner_objectid) { |
| 5611 | found_extent = 1; |
| 5612 | break; |
| 5613 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5614 | if (path->slots[0] - extent_slot > 5) |
| 5615 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5616 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5617 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5618 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 5619 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 5620 | if (found_extent && item_size < sizeof(*ei)) |
| 5621 | found_extent = 0; |
| 5622 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5623 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5624 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5625 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5626 | NULL, refs_to_drop, |
| 5627 | is_data); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5628 | if (ret) { |
| 5629 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5630 | goto out; |
| 5631 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5632 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5633 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5634 | |
| 5635 | key.objectid = bytenr; |
| 5636 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5637 | key.offset = num_bytes; |
| 5638 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5639 | if (!is_data && skinny_metadata) { |
| 5640 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 5641 | key.offset = owner_objectid; |
| 5642 | } |
| 5643 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5644 | ret = btrfs_search_slot(trans, extent_root, |
| 5645 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5646 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 5647 | /* |
| 5648 | * Couldn't find our skinny metadata item, |
| 5649 | * see if we have ye olde extent item. |
| 5650 | */ |
| 5651 | path->slots[0]--; |
| 5652 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 5653 | path->slots[0]); |
| 5654 | if (key.objectid == bytenr && |
| 5655 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 5656 | key.offset == num_bytes) |
| 5657 | ret = 0; |
| 5658 | } |
| 5659 | |
| 5660 | if (ret > 0 && skinny_metadata) { |
| 5661 | skinny_metadata = false; |
| 5662 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5663 | key.offset = num_bytes; |
| 5664 | btrfs_release_path(path); |
| 5665 | ret = btrfs_search_slot(trans, extent_root, |
| 5666 | &key, path, -1, 1); |
| 5667 | } |
| 5668 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5669 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5670 | btrfs_err(info, "umm, got %d back from search, was looking for %llu", |
| 5671 | ret, (unsigned long long)bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 5672 | if (ret > 0) |
| 5673 | btrfs_print_leaf(extent_root, |
| 5674 | path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5675 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5676 | if (ret < 0) { |
| 5677 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5678 | goto out; |
| 5679 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5680 | extent_slot = path->slots[0]; |
| 5681 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5682 | } else if (ret == -ENOENT) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5683 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 5684 | WARN_ON(1); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5685 | btrfs_err(info, |
| 5686 | "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", |
| 5687 | (unsigned long long)bytenr, |
| 5688 | (unsigned long long)parent, |
| 5689 | (unsigned long long)root_objectid, |
| 5690 | (unsigned long long)owner_objectid, |
| 5691 | (unsigned long long)owner_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5692 | } else { |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5693 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5694 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5695 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5696 | |
| 5697 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5698 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 5699 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 5700 | if (item_size < sizeof(*ei)) { |
| 5701 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 5702 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 5703 | owner_objectid, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5704 | if (ret < 0) { |
| 5705 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5706 | goto out; |
| 5707 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5708 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5709 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5710 | path->leave_spinning = 1; |
| 5711 | |
| 5712 | key.objectid = bytenr; |
| 5713 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5714 | key.offset = num_bytes; |
| 5715 | |
| 5716 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 5717 | -1, 1); |
| 5718 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5719 | btrfs_err(info, "umm, got %d back from search, was looking for %llu", |
| 5720 | ret, (unsigned long long)bytenr); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5721 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 5722 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5723 | if (ret < 0) { |
| 5724 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5725 | goto out; |
| 5726 | } |
| 5727 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5728 | extent_slot = path->slots[0]; |
| 5729 | leaf = path->nodes[0]; |
| 5730 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 5731 | } |
| 5732 | #endif |
| 5733 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5734 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 5735 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5736 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 5737 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5738 | struct btrfs_tree_block_info *bi; |
| 5739 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 5740 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 5741 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5742 | } |
| 5743 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5744 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 5745 | if (refs < refs_to_drop) { |
| 5746 | btrfs_err(info, "trying to drop %d refs but we only have %Lu " |
| 5747 | "for bytenr %Lu\n", refs_to_drop, refs, bytenr); |
| 5748 | ret = -EINVAL; |
| 5749 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5750 | goto out; |
| 5751 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5752 | refs -= refs_to_drop; |
| 5753 | |
| 5754 | if (refs > 0) { |
| 5755 | if (extent_op) |
| 5756 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 5757 | /* |
| 5758 | * In the case of inline back ref, reference count will |
| 5759 | * be updated by remove_extent_backref |
| 5760 | */ |
| 5761 | if (iref) { |
| 5762 | BUG_ON(!found_extent); |
| 5763 | } else { |
| 5764 | btrfs_set_extent_refs(leaf, ei, refs); |
| 5765 | btrfs_mark_buffer_dirty(leaf); |
| 5766 | } |
| 5767 | if (found_extent) { |
| 5768 | ret = remove_extent_backref(trans, extent_root, path, |
| 5769 | iref, refs_to_drop, |
| 5770 | is_data); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5771 | if (ret) { |
| 5772 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5773 | goto out; |
| 5774 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5775 | } |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5776 | add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid, |
| 5777 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5778 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5779 | if (found_extent) { |
| 5780 | BUG_ON(is_data && refs_to_drop != |
| 5781 | extent_data_ref_count(root, path, iref)); |
| 5782 | if (iref) { |
| 5783 | BUG_ON(path->slots[0] != extent_slot); |
| 5784 | } else { |
| 5785 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 5786 | path->slots[0] = extent_slot; |
| 5787 | num_to_del = 2; |
| 5788 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 5789 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5790 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5791 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 5792 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5793 | if (ret) { |
| 5794 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5795 | goto out; |
| 5796 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5797 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 5798 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5799 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 5800 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5801 | if (ret) { |
| 5802 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5803 | goto out; |
| 5804 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 5805 | } |
| 5806 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 5807 | ret = update_block_group(root, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5808 | if (ret) { |
| 5809 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5810 | goto out; |
| 5811 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5812 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5813 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5814 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5815 | return ret; |
| 5816 | } |
| 5817 | |
| 5818 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5819 | * 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] | 5820 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 5821 | * a given extent, and if there are no other delayed refs to be processed, it |
| 5822 | * removes it from the tree. |
| 5823 | */ |
| 5824 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 5825 | struct btrfs_root *root, u64 bytenr) |
| 5826 | { |
| 5827 | struct btrfs_delayed_ref_head *head; |
| 5828 | struct btrfs_delayed_ref_root *delayed_refs; |
| 5829 | struct btrfs_delayed_ref_node *ref; |
| 5830 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5831 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5832 | |
| 5833 | delayed_refs = &trans->transaction->delayed_refs; |
| 5834 | spin_lock(&delayed_refs->lock); |
| 5835 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 5836 | if (!head) |
| 5837 | goto out; |
| 5838 | |
| 5839 | node = rb_prev(&head->node.rb_node); |
| 5840 | if (!node) |
| 5841 | goto out; |
| 5842 | |
| 5843 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 5844 | |
| 5845 | /* there are still entries for this ref, we can't drop it */ |
| 5846 | if (ref->bytenr == bytenr) |
| 5847 | goto out; |
| 5848 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5849 | if (head->extent_op) { |
| 5850 | if (!head->must_insert_reserved) |
| 5851 | goto out; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 5852 | btrfs_free_delayed_extent_op(head->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5853 | head->extent_op = NULL; |
| 5854 | } |
| 5855 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5856 | /* |
| 5857 | * waiting for the lock here would deadlock. If someone else has it |
| 5858 | * locked they are already in the process of dropping it anyway |
| 5859 | */ |
| 5860 | if (!mutex_trylock(&head->mutex)) |
| 5861 | goto out; |
| 5862 | |
| 5863 | /* |
| 5864 | * at this point we have a head with no other entries. Go |
| 5865 | * ahead and process it. |
| 5866 | */ |
| 5867 | head->node.in_tree = 0; |
| 5868 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5869 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5870 | delayed_refs->num_entries--; |
| 5871 | |
| 5872 | /* |
| 5873 | * we don't take a ref on the node because we're removing it from the |
| 5874 | * tree, so we just steal the ref the tree was holding. |
| 5875 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5876 | delayed_refs->num_heads--; |
| 5877 | if (list_empty(&head->cluster)) |
| 5878 | delayed_refs->num_heads_ready--; |
| 5879 | |
| 5880 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5881 | spin_unlock(&delayed_refs->lock); |
| 5882 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5883 | BUG_ON(head->extent_op); |
| 5884 | if (head->must_insert_reserved) |
| 5885 | ret = 1; |
| 5886 | |
| 5887 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5888 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5889 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5890 | out: |
| 5891 | spin_unlock(&delayed_refs->lock); |
| 5892 | return 0; |
| 5893 | } |
| 5894 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5895 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 5896 | struct btrfs_root *root, |
| 5897 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5898 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5899 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5900 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5901 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5902 | int ret; |
| 5903 | |
| 5904 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5905 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 5906 | buf->start, buf->len, |
| 5907 | parent, root->root_key.objectid, |
| 5908 | btrfs_header_level(buf), |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5909 | BTRFS_DROP_DELAYED_REF, NULL, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5910 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | if (!last_ref) |
| 5914 | return; |
| 5915 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5916 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5917 | |
| 5918 | if (btrfs_header_generation(buf) == trans->transid) { |
| 5919 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5920 | ret = check_ref_cleanup(trans, root, buf->start); |
| 5921 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 5922 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 5926 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 5927 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 5931 | |
| 5932 | btrfs_add_free_space(cache, buf->start, buf->len); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5933 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5934 | pin = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5935 | } |
| 5936 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5937 | if (pin) |
| 5938 | add_pinned_bytes(root->fs_info, buf->len, |
| 5939 | btrfs_header_level(buf), |
| 5940 | root->root_key.objectid); |
| 5941 | |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 5942 | /* |
| 5943 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 5944 | * anymore. |
| 5945 | */ |
| 5946 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5947 | btrfs_put_block_group(cache); |
| 5948 | } |
| 5949 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5950 | /* Can return -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5951 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 5952 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
| 5953 | u64 owner, u64 offset, int for_cow) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5954 | { |
| 5955 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5956 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5957 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5958 | add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid); |
| 5959 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5960 | /* |
| 5961 | * tree log blocks never actually go into the extent allocation |
| 5962 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5963 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5964 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 5965 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5966 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5967 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5968 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5969 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5970 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 5971 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5972 | parent, root_objectid, (int)owner, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5973 | BTRFS_DROP_DELAYED_REF, NULL, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5974 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5975 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 5976 | num_bytes, |
| 5977 | parent, root_objectid, owner, |
| 5978 | offset, BTRFS_DROP_DELAYED_REF, |
| 5979 | NULL, for_cow); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5980 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5981 | return ret; |
| 5982 | } |
| 5983 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5984 | static u64 stripe_align(struct btrfs_root *root, |
| 5985 | struct btrfs_block_group_cache *cache, |
| 5986 | u64 val, u64 num_bytes) |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 5987 | { |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5988 | u64 ret = ALIGN(val, root->stripesize); |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 5989 | return ret; |
| 5990 | } |
| 5991 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5992 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5993 | * when we wait for progress in the block group caching, its because |
| 5994 | * our allocation attempt failed at least once. So, we must sleep |
| 5995 | * and let some progress happen before we try again. |
| 5996 | * |
| 5997 | * This function will sleep at least once waiting for new free space to |
| 5998 | * show up, and then it will check the block group free space numbers |
| 5999 | * for our min num_bytes. Another option is to have it go ahead |
| 6000 | * and look in the rbtree for a free extent of a given size, but this |
| 6001 | * is a good start. |
| 6002 | */ |
| 6003 | static noinline int |
| 6004 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 6005 | u64 num_bytes) |
| 6006 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6007 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6008 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6009 | caching_ctl = get_caching_control(cache); |
| 6010 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6011 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6012 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6013 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6014 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6015 | |
| 6016 | put_caching_control(caching_ctl); |
| 6017 | return 0; |
| 6018 | } |
| 6019 | |
| 6020 | static noinline int |
| 6021 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 6022 | { |
| 6023 | struct btrfs_caching_control *caching_ctl; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6024 | |
| 6025 | caching_ctl = get_caching_control(cache); |
| 6026 | if (!caching_ctl) |
| 6027 | return 0; |
| 6028 | |
| 6029 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 6030 | |
| 6031 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6032 | return 0; |
| 6033 | } |
| 6034 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 6035 | int __get_raid_index(u64 flags) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6036 | { |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6037 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6038 | return BTRFS_RAID_RAID10; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6039 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6040 | return BTRFS_RAID_RAID1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6041 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6042 | return BTRFS_RAID_DUP; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6043 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6044 | return BTRFS_RAID_RAID0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6045 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6046 | return BTRFS_RAID_RAID5; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6047 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6048 | return BTRFS_RAID_RAID6; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6049 | |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6050 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6051 | } |
| 6052 | |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6053 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 6054 | { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 6055 | return __get_raid_index(cache->flags); |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6056 | } |
| 6057 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6058 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6059 | LOOP_CACHING_NOWAIT = 0, |
| 6060 | LOOP_CACHING_WAIT = 1, |
| 6061 | LOOP_ALLOC_CHUNK = 2, |
| 6062 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6063 | }; |
| 6064 | |
| 6065 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6066 | * walks the btree of allocated extents and find a hole of a given size. |
| 6067 | * The key ins is changed to record the hole: |
| 6068 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 6069 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6070 | * ins->offset == number of blocks |
| 6071 | * Any available blocks before search_start are skipped. |
| 6072 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6073 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6074 | struct btrfs_root *orig_root, |
| 6075 | u64 num_bytes, u64 empty_size, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6076 | u64 hint_byte, struct btrfs_key *ins, |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6077 | u64 flags) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6078 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6079 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6080 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6081 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6082 | struct btrfs_block_group_cache *block_group = NULL; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6083 | struct btrfs_block_group_cache *used_block_group; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6084 | u64 search_start = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6085 | int empty_cluster = 2 * 1024 * 1024; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6086 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6087 | int loop = 0; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6088 | int index = __get_raid_index(flags); |
| 6089 | int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ? |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6090 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6091 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6092 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6093 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 6094 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6095 | bool have_caching_bg = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6096 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6097 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 6098 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6099 | ins->objectid = 0; |
| 6100 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 6101 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6102 | trace_find_free_extent(orig_root, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6103 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6104 | space_info = __find_space_info(root->fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 6105 | if (!space_info) { |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6106 | btrfs_err(root->fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 6107 | return -ENOSPC; |
| 6108 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6109 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 6110 | /* |
| 6111 | * If the space info is for both data and metadata it means we have a |
| 6112 | * small filesystem and we can't use the clustering stuff. |
| 6113 | */ |
| 6114 | if (btrfs_mixed_space_info(space_info)) |
| 6115 | use_cluster = false; |
| 6116 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6117 | if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6118 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 6119 | if (!btrfs_test_opt(root, SSD)) |
| 6120 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6121 | } |
| 6122 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6123 | if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 6124 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6125 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 6126 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6127 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6128 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6129 | spin_lock(&last_ptr->lock); |
| 6130 | if (last_ptr->block_group) |
| 6131 | hint_byte = last_ptr->window_start; |
| 6132 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6133 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6134 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6135 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6136 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6137 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6138 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6139 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6140 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6141 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6142 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6143 | search_start); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6144 | used_block_group = block_group; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6145 | /* |
| 6146 | * we don't want to use the block group if it doesn't match our |
| 6147 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6148 | * |
| 6149 | * However if we are re-searching with an ideal block group |
| 6150 | * 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] | 6151 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6152 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6153 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6154 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6155 | if (list_empty(&block_group->list) || |
| 6156 | block_group->ro) { |
| 6157 | /* |
| 6158 | * someone is removing this block group, |
| 6159 | * we can't jump into the have_block_group |
| 6160 | * target because our list pointers are not |
| 6161 | * valid |
| 6162 | */ |
| 6163 | btrfs_put_block_group(block_group); |
| 6164 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6165 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6166 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6167 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6168 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6169 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6170 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6171 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 6172 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6173 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6174 | have_caching_bg = false; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6175 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6176 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 6177 | list) { |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6178 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6179 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 6180 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6181 | used_block_group = block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 6182 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6183 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 6184 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6185 | /* |
| 6186 | * this can happen if we end up cycling through all the |
| 6187 | * raid types, but we want to make sure we only allocate |
| 6188 | * for the proper type. |
| 6189 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6190 | if (!block_group_bits(block_group, flags)) { |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6191 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 6192 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6193 | BTRFS_BLOCK_GROUP_RAID5 | |
| 6194 | BTRFS_BLOCK_GROUP_RAID6 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6195 | BTRFS_BLOCK_GROUP_RAID10; |
| 6196 | |
| 6197 | /* |
| 6198 | * if they asked for extra copies and this block group |
| 6199 | * doesn't provide them, bail. This does allow us to |
| 6200 | * fill raid0 from raid1. |
| 6201 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6202 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6203 | goto loop; |
| 6204 | } |
| 6205 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6206 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 6207 | cached = block_group_cache_done(block_group); |
| 6208 | if (unlikely(!cached)) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 6209 | found_uncached_bg = true; |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6210 | ret = cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 6211 | BUG_ON(ret < 0); |
| 6212 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 6213 | } |
| 6214 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 6215 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6216 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6217 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6218 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6219 | * Ok we want to try and use the cluster allocator, so |
| 6220 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6221 | */ |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6222 | if (last_ptr) { |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 6223 | unsigned long aligned_cluster; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6224 | /* |
| 6225 | * the refill lock keeps out other |
| 6226 | * people trying to start a new cluster |
| 6227 | */ |
| 6228 | spin_lock(&last_ptr->refill_lock); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6229 | used_block_group = last_ptr->block_group; |
| 6230 | if (used_block_group != block_group && |
| 6231 | (!used_block_group || |
| 6232 | used_block_group->ro || |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6233 | !block_group_bits(used_block_group, flags))) { |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6234 | used_block_group = block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6235 | goto refill_cluster; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6236 | } |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6237 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6238 | if (used_block_group != block_group) |
| 6239 | btrfs_get_block_group(used_block_group); |
| 6240 | |
| 6241 | offset = btrfs_alloc_from_cluster(used_block_group, |
| 6242 | last_ptr, num_bytes, used_block_group->key.objectid); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6243 | if (offset) { |
| 6244 | /* we have a block, we're done */ |
| 6245 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6246 | trace_btrfs_reserve_extent_cluster(root, |
| 6247 | block_group, search_start, num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6248 | goto checks; |
| 6249 | } |
| 6250 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6251 | WARN_ON(last_ptr->block_group != used_block_group); |
| 6252 | if (used_block_group != block_group) { |
| 6253 | btrfs_put_block_group(used_block_group); |
| 6254 | used_block_group = block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6255 | } |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6256 | refill_cluster: |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6257 | BUG_ON(used_block_group != block_group); |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6258 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 6259 | * set up a new clusters, so lets just skip it |
| 6260 | * and let the allocator find whatever block |
| 6261 | * it can find. If we reach this point, we |
| 6262 | * will have tried the cluster allocator |
| 6263 | * plenty of times and not have found |
| 6264 | * anything, so we are likely way too |
| 6265 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6266 | * anything. |
| 6267 | * |
| 6268 | * However, if the cluster is taken from the |
| 6269 | * current block group, release the cluster |
| 6270 | * first, so that we stand a better chance of |
| 6271 | * succeeding in the unclustered |
| 6272 | * allocation. */ |
| 6273 | if (loop >= LOOP_NO_EMPTY_SIZE && |
| 6274 | last_ptr->block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6275 | spin_unlock(&last_ptr->refill_lock); |
| 6276 | goto unclustered_alloc; |
| 6277 | } |
| 6278 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6279 | /* |
| 6280 | * this cluster didn't work out, free it and |
| 6281 | * start over |
| 6282 | */ |
| 6283 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 6284 | |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6285 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 6286 | spin_unlock(&last_ptr->refill_lock); |
| 6287 | goto unclustered_alloc; |
| 6288 | } |
| 6289 | |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 6290 | aligned_cluster = max_t(unsigned long, |
| 6291 | empty_cluster + empty_size, |
| 6292 | block_group->full_stripe_len); |
| 6293 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6294 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 6295 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6296 | block_group, last_ptr, |
Alexandre Oliva | 1b22bad | 2011-11-30 13:43:00 -0500 | [diff] [blame] | 6297 | search_start, num_bytes, |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 6298 | aligned_cluster); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6299 | if (ret == 0) { |
| 6300 | /* |
| 6301 | * now pull our allocation out of this |
| 6302 | * cluster |
| 6303 | */ |
| 6304 | offset = btrfs_alloc_from_cluster(block_group, |
| 6305 | last_ptr, num_bytes, |
| 6306 | search_start); |
| 6307 | if (offset) { |
| 6308 | /* we found one, proceed */ |
| 6309 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6310 | trace_btrfs_reserve_extent_cluster(root, |
| 6311 | block_group, search_start, |
| 6312 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6313 | goto checks; |
| 6314 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6315 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 6316 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6317 | spin_unlock(&last_ptr->refill_lock); |
| 6318 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6319 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6320 | wait_block_group_cache_progress(block_group, |
| 6321 | num_bytes + empty_cluster + empty_size); |
| 6322 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6323 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6324 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6325 | /* |
| 6326 | * at this point we either didn't find a cluster |
| 6327 | * or we weren't able to allocate a block from our |
| 6328 | * cluster. Free the cluster we've been trying |
| 6329 | * to use, and go to the next block group |
| 6330 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6331 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6332 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6333 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6334 | } |
| 6335 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6336 | unclustered_alloc: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6337 | spin_lock(&block_group->free_space_ctl->tree_lock); |
| 6338 | if (cached && |
| 6339 | block_group->free_space_ctl->free_space < |
| 6340 | num_bytes + empty_cluster + empty_size) { |
| 6341 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 6342 | goto loop; |
| 6343 | } |
| 6344 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 6345 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6346 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 6347 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6348 | /* |
| 6349 | * If we didn't find a chunk, and we haven't failed on this |
| 6350 | * block group before, and this block group is in the middle of |
| 6351 | * caching and we are ok with waiting, then go ahead and wait |
| 6352 | * for progress to be made, and set failed_alloc to true. |
| 6353 | * |
| 6354 | * If failed_alloc is true then we've already waited on this |
| 6355 | * block group once and should move on to the next block group. |
| 6356 | */ |
| 6357 | if (!offset && !failed_alloc && !cached && |
| 6358 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6359 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6360 | num_bytes + empty_size); |
| 6361 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6362 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6363 | } else if (!offset) { |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6364 | if (!cached) |
| 6365 | have_caching_bg = true; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6366 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6367 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6368 | checks: |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6369 | search_start = stripe_align(root, used_block_group, |
| 6370 | offset, num_bytes); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 6371 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6372 | /* move on to the next group */ |
| 6373 | if (search_start + num_bytes > |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6374 | used_block_group->key.objectid + used_block_group->key.offset) { |
| 6375 | btrfs_add_free_space(used_block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6376 | goto loop; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6377 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6378 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6379 | if (offset < search_start) |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6380 | btrfs_add_free_space(used_block_group, offset, |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6381 | search_start - offset); |
| 6382 | BUG_ON(offset > search_start); |
| 6383 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6384 | ret = btrfs_update_reserved_bytes(used_block_group, num_bytes, |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6385 | alloc_type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6386 | if (ret == -EAGAIN) { |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6387 | btrfs_add_free_space(used_block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6388 | goto loop; |
| 6389 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6390 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6391 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6392 | ins->objectid = search_start; |
| 6393 | ins->offset = num_bytes; |
| 6394 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6395 | trace_btrfs_reserve_extent(orig_root, block_group, |
| 6396 | search_start, num_bytes); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6397 | if (used_block_group != block_group) |
| 6398 | btrfs_put_block_group(used_block_group); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6399 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6400 | break; |
| 6401 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6402 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6403 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6404 | BUG_ON(index != get_block_group_index(block_group)); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6405 | if (used_block_group != block_group) |
| 6406 | btrfs_put_block_group(used_block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6407 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6408 | } |
| 6409 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 6410 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6411 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 6412 | goto search; |
| 6413 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6414 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 6415 | goto search; |
| 6416 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6417 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6418 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 6419 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6420 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 6421 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 6422 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 6423 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6424 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6425 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6426 | index = 0; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6427 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6428 | if (loop == LOOP_ALLOC_CHUNK) { |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6429 | ret = do_chunk_alloc(trans, root, flags, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 6430 | CHUNK_ALLOC_FORCE); |
| 6431 | /* |
| 6432 | * Do not bail out on ENOSPC since we |
| 6433 | * can do more things. |
| 6434 | */ |
| 6435 | if (ret < 0 && ret != -ENOSPC) { |
| 6436 | btrfs_abort_transaction(trans, |
| 6437 | root, ret); |
| 6438 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6439 | } |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6443 | empty_size = 0; |
| 6444 | empty_cluster = 0; |
| 6445 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 6446 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6447 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6448 | } else if (!ins->objectid) { |
| 6449 | ret = -ENOSPC; |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6450 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6451 | ret = 0; |
| 6452 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6453 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6454 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 6455 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6456 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6457 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6458 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 6459 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6460 | { |
| 6461 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6462 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6463 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6464 | spin_lock(&info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6465 | printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n", |
| 6466 | (unsigned long long)info->flags, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6467 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6468 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6469 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6470 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6471 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 6472 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 6473 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6474 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6475 | (unsigned long long)info->bytes_pinned, |
| 6476 | (unsigned long long)info->bytes_reserved, |
| 6477 | (unsigned long long)info->bytes_may_use, |
| 6478 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6479 | spin_unlock(&info->lock); |
| 6480 | |
| 6481 | if (!dump_block_groups) |
| 6482 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6483 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6484 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6485 | again: |
| 6486 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6487 | spin_lock(&cache->lock); |
Liu Bo | 799ffc3 | 2012-07-06 03:31:35 -0600 | [diff] [blame] | 6488 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6489 | (unsigned long long)cache->key.objectid, |
| 6490 | (unsigned long long)cache->key.offset, |
| 6491 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 6492 | (unsigned long long)cache->pinned, |
Liu Bo | 799ffc3 | 2012-07-06 03:31:35 -0600 | [diff] [blame] | 6493 | (unsigned long long)cache->reserved, |
| 6494 | cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6495 | btrfs_dump_free_space(cache, bytes); |
| 6496 | spin_unlock(&cache->lock); |
| 6497 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6498 | if (++index < BTRFS_NR_RAID_TYPES) |
| 6499 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6500 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6501 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6502 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6503 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 6504 | struct btrfs_root *root, |
| 6505 | u64 num_bytes, u64 min_alloc_size, |
| 6506 | u64 empty_size, u64 hint_byte, |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6507 | struct btrfs_key *ins, int is_data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6508 | { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6509 | bool final_tried = false; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6510 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6511 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6512 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6513 | flags = btrfs_get_alloc_profile(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 6514 | again: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6515 | WARN_ON(num_bytes < root->sectorsize); |
| 6516 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6517 | hint_byte, ins, flags); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6518 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6519 | if (ret == -ENOSPC) { |
| 6520 | if (!final_tried) { |
| 6521 | num_bytes = num_bytes >> 1; |
Zach Brown | 24542bf | 2012-11-16 00:04:43 +0000 | [diff] [blame] | 6522 | num_bytes = round_down(num_bytes, root->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6523 | num_bytes = max(num_bytes, min_alloc_size); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6524 | if (num_bytes == min_alloc_size) |
| 6525 | final_tried = true; |
| 6526 | goto again; |
| 6527 | } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 6528 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6529 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6530 | sinfo = __find_space_info(root->fs_info, flags); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6531 | btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu", |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6532 | (unsigned long long)flags, |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6533 | (unsigned long long)num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 6534 | if (sinfo) |
| 6535 | dump_space_info(sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6536 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6537 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6538 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6539 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 6540 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6541 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6542 | } |
| 6543 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6544 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
| 6545 | u64 start, u64 len, int pin) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6546 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6547 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6548 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6549 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6550 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 6551 | if (!cache) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6552 | btrfs_err(root->fs_info, "Unable to find block group for %llu", |
| 6553 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6554 | return -ENOSPC; |
| 6555 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6556 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 6557 | if (btrfs_test_opt(root, DISCARD)) |
| 6558 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6559 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6560 | if (pin) |
| 6561 | pin_down_extent(root, cache, start, len, 1); |
| 6562 | else { |
| 6563 | btrfs_add_free_space(cache, start, len); |
| 6564 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); |
| 6565 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6566 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6567 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6568 | trace_btrfs_reserved_extent_free(root, start, len); |
| 6569 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6570 | return ret; |
| 6571 | } |
| 6572 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6573 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
| 6574 | u64 start, u64 len) |
| 6575 | { |
| 6576 | return __btrfs_free_reserved_extent(root, start, len, 0); |
| 6577 | } |
| 6578 | |
| 6579 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, |
| 6580 | u64 start, u64 len) |
| 6581 | { |
| 6582 | return __btrfs_free_reserved_extent(root, start, len, 1); |
| 6583 | } |
| 6584 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6585 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 6586 | struct btrfs_root *root, |
| 6587 | u64 parent, u64 root_objectid, |
| 6588 | u64 flags, u64 owner, u64 offset, |
| 6589 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6590 | { |
| 6591 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6592 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6593 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6594 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6595 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6596 | struct extent_buffer *leaf; |
| 6597 | int type; |
| 6598 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 6599 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6600 | if (parent > 0) |
| 6601 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 6602 | else |
| 6603 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6604 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6605 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6606 | |
| 6607 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6608 | if (!path) |
| 6609 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6610 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6611 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6612 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 6613 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6614 | if (ret) { |
| 6615 | btrfs_free_path(path); |
| 6616 | return ret; |
| 6617 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6618 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6619 | leaf = path->nodes[0]; |
| 6620 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6621 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6622 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 6623 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 6624 | btrfs_set_extent_flags(leaf, extent_item, |
| 6625 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6626 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6627 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 6628 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 6629 | if (parent > 0) { |
| 6630 | struct btrfs_shared_data_ref *ref; |
| 6631 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 6632 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 6633 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 6634 | } else { |
| 6635 | struct btrfs_extent_data_ref *ref; |
| 6636 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 6637 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 6638 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 6639 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 6640 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 6641 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6642 | |
| 6643 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6644 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 6645 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 6646 | ret = update_block_group(root, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6647 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6648 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
| 6649 | (unsigned long long)ins->objectid, |
| 6650 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 6651 | BUG(); |
| 6652 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6653 | return ret; |
| 6654 | } |
| 6655 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6656 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 6657 | struct btrfs_root *root, |
| 6658 | u64 parent, u64 root_objectid, |
| 6659 | u64 flags, struct btrfs_disk_key *key, |
| 6660 | int level, struct btrfs_key *ins) |
| 6661 | { |
| 6662 | int ret; |
| 6663 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 6664 | struct btrfs_extent_item *extent_item; |
| 6665 | struct btrfs_tree_block_info *block_info; |
| 6666 | struct btrfs_extent_inline_ref *iref; |
| 6667 | struct btrfs_path *path; |
| 6668 | struct extent_buffer *leaf; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6669 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
| 6670 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 6671 | SKINNY_METADATA); |
| 6672 | |
| 6673 | if (!skinny_metadata) |
| 6674 | size += sizeof(*block_info); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6675 | |
| 6676 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6677 | if (!path) |
| 6678 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6679 | |
| 6680 | path->leave_spinning = 1; |
| 6681 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 6682 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6683 | if (ret) { |
| 6684 | btrfs_free_path(path); |
| 6685 | return ret; |
| 6686 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6687 | |
| 6688 | leaf = path->nodes[0]; |
| 6689 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 6690 | struct btrfs_extent_item); |
| 6691 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 6692 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 6693 | btrfs_set_extent_flags(leaf, extent_item, |
| 6694 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6695 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6696 | if (skinny_metadata) { |
| 6697 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 6698 | } else { |
| 6699 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 6700 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 6701 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 6702 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 6703 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6704 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6705 | if (parent > 0) { |
| 6706 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 6707 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 6708 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 6709 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 6710 | } else { |
| 6711 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 6712 | BTRFS_TREE_BLOCK_REF_KEY); |
| 6713 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 6714 | } |
| 6715 | |
| 6716 | btrfs_mark_buffer_dirty(leaf); |
| 6717 | btrfs_free_path(path); |
| 6718 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6719 | ret = update_block_group(root, ins->objectid, root->leafsize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6720 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6721 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
| 6722 | (unsigned long long)ins->objectid, |
| 6723 | (unsigned long long)ins->offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6724 | BUG(); |
| 6725 | } |
| 6726 | return ret; |
| 6727 | } |
| 6728 | |
| 6729 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 6730 | struct btrfs_root *root, |
| 6731 | u64 root_objectid, u64 owner, |
| 6732 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6733 | { |
| 6734 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 6735 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6736 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6737 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6738 | ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, |
| 6739 | ins->offset, 0, |
| 6740 | root_objectid, owner, offset, |
| 6741 | BTRFS_ADD_DELAYED_EXTENT, NULL, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6742 | return ret; |
| 6743 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6744 | |
| 6745 | /* |
| 6746 | * this is used by the tree logging recovery code. It records that |
| 6747 | * an extent has been allocated and makes sure to clear the free |
| 6748 | * space cache bits as well |
| 6749 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6750 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 6751 | struct btrfs_root *root, |
| 6752 | u64 root_objectid, u64 owner, u64 offset, |
| 6753 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6754 | { |
| 6755 | int ret; |
| 6756 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6757 | |
| 6758 | /* |
| 6759 | * Mixed block groups will exclude before processing the log so we only |
| 6760 | * need to do the exlude dance if this fs isn't mixed. |
| 6761 | */ |
| 6762 | if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) { |
| 6763 | ret = __exclude_logged_extent(root, ins->objectid, ins->offset); |
| 6764 | if (ret) |
| 6765 | return ret; |
| 6766 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6767 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6768 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 6769 | if (!block_group) |
| 6770 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6771 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6772 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
| 6773 | RESERVE_ALLOC_NO_ACCOUNT); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6774 | BUG_ON(ret); /* logic error */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6775 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 6776 | 0, owner, offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 6777 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6778 | return ret; |
| 6779 | } |
| 6780 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6781 | static struct extent_buffer * |
| 6782 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 6783 | u64 bytenr, u32 blocksize, int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6784 | { |
| 6785 | struct extent_buffer *buf; |
| 6786 | |
| 6787 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 6788 | if (!buf) |
| 6789 | return ERR_PTR(-ENOMEM); |
| 6790 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6791 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6792 | btrfs_tree_lock(buf); |
| 6793 | clean_tree_block(trans, root, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6794 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6795 | |
| 6796 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6797 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6798 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6799 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 6800 | /* |
| 6801 | * we allow two log transactions at a time, use different |
| 6802 | * EXENT bit to differentiate dirty pages. |
| 6803 | */ |
| 6804 | if (root->log_transid % 2 == 0) |
| 6805 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 6806 | buf->start + buf->len - 1, GFP_NOFS); |
| 6807 | else |
| 6808 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 6809 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6810 | } else { |
| 6811 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 6812 | buf->start + buf->len - 1, GFP_NOFS); |
| 6813 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6814 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6815 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6816 | return buf; |
| 6817 | } |
| 6818 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6819 | static struct btrfs_block_rsv * |
| 6820 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 6821 | struct btrfs_root *root, u32 blocksize) |
| 6822 | { |
| 6823 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 6824 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6825 | int ret; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 6826 | bool global_updated = false; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6827 | |
| 6828 | block_rsv = get_block_rsv(trans, root); |
| 6829 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 6830 | if (unlikely(block_rsv->size == 0)) |
| 6831 | goto try_reserve; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 6832 | again: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6833 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 6834 | if (!ret) |
| 6835 | return block_rsv; |
| 6836 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 6837 | if (block_rsv->failfast) |
| 6838 | return ERR_PTR(ret); |
| 6839 | |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 6840 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
| 6841 | global_updated = true; |
| 6842 | update_global_block_rsv(root->fs_info); |
| 6843 | goto again; |
| 6844 | } |
| 6845 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 6846 | if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 6847 | static DEFINE_RATELIMIT_STATE(_rs, |
| 6848 | DEFAULT_RATELIMIT_INTERVAL * 10, |
| 6849 | /*DEFAULT_RATELIMIT_BURST*/ 1); |
| 6850 | if (__ratelimit(&_rs)) |
| 6851 | WARN(1, KERN_DEBUG |
| 6852 | "btrfs: block rsv returned %d\n", ret); |
| 6853 | } |
| 6854 | try_reserve: |
| 6855 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, |
| 6856 | BTRFS_RESERVE_NO_FLUSH); |
| 6857 | if (!ret) |
| 6858 | return block_rsv; |
| 6859 | /* |
| 6860 | * If we couldn't reserve metadata bytes try and use some from |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 6861 | * the global reserve if its space type is the same as the global |
| 6862 | * reservation. |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 6863 | */ |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 6864 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
| 6865 | block_rsv->space_info == global_rsv->space_info) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 6866 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 6867 | if (!ret) |
| 6868 | return global_rsv; |
| 6869 | } |
| 6870 | return ERR_PTR(ret); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6871 | } |
| 6872 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6873 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 6874 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6875 | { |
| 6876 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6877 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6878 | } |
| 6879 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6880 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6881 | * finds a free extent and does all the dirty work required for allocation |
| 6882 | * returns the key for the extent through ins, and a tree buffer for |
| 6883 | * the first block of the extent through buf. |
| 6884 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6885 | * returns the tree buffer or NULL. |
| 6886 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6887 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6888 | struct btrfs_root *root, u32 blocksize, |
| 6889 | u64 parent, u64 root_objectid, |
| 6890 | struct btrfs_disk_key *key, int level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6891 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6892 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6893 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6894 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6895 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6896 | u64 flags = 0; |
| 6897 | int ret; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6898 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 6899 | SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6900 | |
| 6901 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 6902 | if (IS_ERR(block_rsv)) |
| 6903 | return ERR_CAST(block_rsv); |
| 6904 | |
| 6905 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6906 | empty_size, hint, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6907 | if (ret) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6908 | unuse_block_rsv(root->fs_info, block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6909 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6910 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 6911 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6912 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 6913 | blocksize, level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6914 | BUG_ON(IS_ERR(buf)); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6915 | |
| 6916 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 6917 | if (parent == 0) |
| 6918 | parent = ins.objectid; |
| 6919 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6920 | } else |
| 6921 | BUG_ON(parent > 0); |
| 6922 | |
| 6923 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 6924 | struct btrfs_delayed_extent_op *extent_op; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 6925 | extent_op = btrfs_alloc_delayed_extent_op(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6926 | BUG_ON(!extent_op); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6927 | if (key) |
| 6928 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 6929 | else |
| 6930 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 6931 | extent_op->flags_to_set = flags; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6932 | if (skinny_metadata) |
| 6933 | extent_op->update_key = 0; |
| 6934 | else |
| 6935 | extent_op->update_key = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6936 | extent_op->update_flags = 1; |
| 6937 | extent_op->is_data = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 6938 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6939 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6940 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 6941 | ins.objectid, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6942 | ins.offset, parent, root_objectid, |
| 6943 | level, BTRFS_ADD_DELAYED_EXTENT, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6944 | extent_op, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6945 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6946 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6947 | return buf; |
| 6948 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6949 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6950 | struct walk_control { |
| 6951 | u64 refs[BTRFS_MAX_LEVEL]; |
| 6952 | u64 flags[BTRFS_MAX_LEVEL]; |
| 6953 | struct btrfs_key update_progress; |
| 6954 | int stage; |
| 6955 | int level; |
| 6956 | int shared_level; |
| 6957 | int update_ref; |
| 6958 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6959 | int reada_slot; |
| 6960 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6961 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6962 | }; |
| 6963 | |
| 6964 | #define DROP_REFERENCE 1 |
| 6965 | #define UPDATE_BACKREF 2 |
| 6966 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6967 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 6968 | struct btrfs_root *root, |
| 6969 | struct walk_control *wc, |
| 6970 | struct btrfs_path *path) |
| 6971 | { |
| 6972 | u64 bytenr; |
| 6973 | u64 generation; |
| 6974 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6975 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6976 | u32 nritems; |
| 6977 | u32 blocksize; |
| 6978 | struct btrfs_key key; |
| 6979 | struct extent_buffer *eb; |
| 6980 | int ret; |
| 6981 | int slot; |
| 6982 | int nread = 0; |
| 6983 | |
| 6984 | if (path->slots[wc->level] < wc->reada_slot) { |
| 6985 | wc->reada_count = wc->reada_count * 2 / 3; |
| 6986 | wc->reada_count = max(wc->reada_count, 2); |
| 6987 | } else { |
| 6988 | wc->reada_count = wc->reada_count * 3 / 2; |
| 6989 | wc->reada_count = min_t(int, wc->reada_count, |
| 6990 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 6991 | } |
| 6992 | |
| 6993 | eb = path->nodes[wc->level]; |
| 6994 | nritems = btrfs_header_nritems(eb); |
| 6995 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 6996 | |
| 6997 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 6998 | if (nread >= wc->reada_count) |
| 6999 | break; |
| 7000 | |
| 7001 | cond_resched(); |
| 7002 | bytenr = btrfs_node_blockptr(eb, slot); |
| 7003 | generation = btrfs_node_ptr_generation(eb, slot); |
| 7004 | |
| 7005 | if (slot == path->slots[wc->level]) |
| 7006 | goto reada; |
| 7007 | |
| 7008 | if (wc->stage == UPDATE_BACKREF && |
| 7009 | generation <= root->root_key.offset) |
| 7010 | continue; |
| 7011 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7012 | /* 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] | 7013 | ret = btrfs_lookup_extent_info(trans, root, bytenr, |
| 7014 | wc->level - 1, 1, &refs, |
| 7015 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7016 | /* We don't care about errors in readahead. */ |
| 7017 | if (ret < 0) |
| 7018 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7019 | BUG_ON(refs == 0); |
| 7020 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7021 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7022 | if (refs == 1) |
| 7023 | goto reada; |
| 7024 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7025 | if (wc->level == 1 && |
| 7026 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7027 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7028 | if (!wc->update_ref || |
| 7029 | generation <= root->root_key.offset) |
| 7030 | continue; |
| 7031 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 7032 | ret = btrfs_comp_cpu_keys(&key, |
| 7033 | &wc->update_progress); |
| 7034 | if (ret < 0) |
| 7035 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7036 | } else { |
| 7037 | if (wc->level == 1 && |
| 7038 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7039 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7040 | } |
| 7041 | reada: |
| 7042 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 7043 | generation); |
| 7044 | if (ret) |
| 7045 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7046 | nread++; |
| 7047 | } |
| 7048 | wc->reada_slot = slot; |
| 7049 | } |
| 7050 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7051 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 7052 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7053 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7054 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 7055 | * back refs for pointers in the block. |
| 7056 | * |
| 7057 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7058 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7059 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 7060 | struct btrfs_root *root, |
| 7061 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7062 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7063 | { |
| 7064 | int level = wc->level; |
| 7065 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7066 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7067 | int ret; |
| 7068 | |
| 7069 | if (wc->stage == UPDATE_BACKREF && |
| 7070 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 7071 | return 1; |
| 7072 | |
| 7073 | /* |
| 7074 | * when reference count of tree block is 1, it won't increase |
| 7075 | * again. once full backref flag is set, we never clear it. |
| 7076 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7077 | if (lookup_info && |
| 7078 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 7079 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7080 | BUG_ON(!path->locks[level]); |
| 7081 | ret = btrfs_lookup_extent_info(trans, root, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7082 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7083 | &wc->refs[level], |
| 7084 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7085 | BUG_ON(ret == -ENOMEM); |
| 7086 | if (ret) |
| 7087 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7088 | BUG_ON(wc->refs[level] == 0); |
| 7089 | } |
| 7090 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7091 | if (wc->stage == DROP_REFERENCE) { |
| 7092 | if (wc->refs[level] > 1) |
| 7093 | return 1; |
| 7094 | |
| 7095 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7096 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7097 | path->locks[level] = 0; |
| 7098 | } |
| 7099 | return 0; |
| 7100 | } |
| 7101 | |
| 7102 | /* wc->stage == UPDATE_BACKREF */ |
| 7103 | if (!(wc->flags[level] & flag)) { |
| 7104 | BUG_ON(!path->locks[level]); |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7105 | ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7106 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7107 | ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7108 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7109 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 7110 | eb->len, flag, |
| 7111 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7112 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7113 | wc->flags[level] |= flag; |
| 7114 | } |
| 7115 | |
| 7116 | /* |
| 7117 | * the block is shared by multiple trees, so it's not good to |
| 7118 | * keep the tree lock |
| 7119 | */ |
| 7120 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7121 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7122 | path->locks[level] = 0; |
| 7123 | } |
| 7124 | return 0; |
| 7125 | } |
| 7126 | |
| 7127 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 7128 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7129 | * |
| 7130 | * when wc->stage == DROP_REFERENCE, this function checks |
| 7131 | * reference count of the block pointed to. if the block |
| 7132 | * is shared and we need update back refs for the subtree |
| 7133 | * rooted at the block, this function changes wc->stage to |
| 7134 | * UPDATE_BACKREF. if the block is shared and there is no |
| 7135 | * need to update back, this function drops the reference |
| 7136 | * to the block. |
| 7137 | * |
| 7138 | * NOTE: return value 1 means we should stop walking down. |
| 7139 | */ |
| 7140 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 7141 | struct btrfs_root *root, |
| 7142 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7143 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7144 | { |
| 7145 | u64 bytenr; |
| 7146 | u64 generation; |
| 7147 | u64 parent; |
| 7148 | u32 blocksize; |
| 7149 | struct btrfs_key key; |
| 7150 | struct extent_buffer *next; |
| 7151 | int level = wc->level; |
| 7152 | int reada = 0; |
| 7153 | int ret = 0; |
| 7154 | |
| 7155 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 7156 | path->slots[level]); |
| 7157 | /* |
| 7158 | * if the lower level block was created before the snapshot |
| 7159 | * was created, we know there is no need to update back refs |
| 7160 | * for the subtree |
| 7161 | */ |
| 7162 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7163 | generation <= root->root_key.offset) { |
| 7164 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7165 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7166 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7167 | |
| 7168 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 7169 | blocksize = btrfs_level_size(root, level - 1); |
| 7170 | |
| 7171 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 7172 | if (!next) { |
| 7173 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 7174 | if (!next) |
| 7175 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7176 | reada = 1; |
| 7177 | } |
| 7178 | btrfs_tree_lock(next); |
| 7179 | btrfs_set_lock_blocking(next); |
| 7180 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7181 | ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7182 | &wc->refs[level - 1], |
| 7183 | &wc->flags[level - 1]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7184 | if (ret < 0) { |
| 7185 | btrfs_tree_unlock(next); |
| 7186 | return ret; |
| 7187 | } |
| 7188 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7189 | if (unlikely(wc->refs[level - 1] == 0)) { |
| 7190 | btrfs_err(root->fs_info, "Missing references."); |
| 7191 | BUG(); |
| 7192 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7193 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7194 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7195 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7196 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7197 | if (level == 1 && |
| 7198 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7199 | goto skip; |
| 7200 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7201 | if (!wc->update_ref || |
| 7202 | generation <= root->root_key.offset) |
| 7203 | goto skip; |
| 7204 | |
| 7205 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 7206 | path->slots[level]); |
| 7207 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 7208 | if (ret < 0) |
| 7209 | goto skip; |
| 7210 | |
| 7211 | wc->stage = UPDATE_BACKREF; |
| 7212 | wc->shared_level = level - 1; |
| 7213 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7214 | } else { |
| 7215 | if (level == 1 && |
| 7216 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7217 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7218 | } |
| 7219 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 7220 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7221 | btrfs_tree_unlock(next); |
| 7222 | free_extent_buffer(next); |
| 7223 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7224 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7225 | } |
| 7226 | |
| 7227 | if (!next) { |
| 7228 | if (reada && level == 1) |
| 7229 | reada_walk_down(trans, root, wc, path); |
| 7230 | next = read_tree_block(root, bytenr, blocksize, generation); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 7231 | if (!next || !extent_buffer_uptodate(next)) { |
| 7232 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 7233 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 7234 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7235 | btrfs_tree_lock(next); |
| 7236 | btrfs_set_lock_blocking(next); |
| 7237 | } |
| 7238 | |
| 7239 | level--; |
| 7240 | BUG_ON(level != btrfs_header_level(next)); |
| 7241 | path->nodes[level] = next; |
| 7242 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7243 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7244 | wc->level = level; |
| 7245 | if (wc->level == 1) |
| 7246 | wc->reada_slot = 0; |
| 7247 | return 0; |
| 7248 | skip: |
| 7249 | wc->refs[level - 1] = 0; |
| 7250 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7251 | if (wc->stage == DROP_REFERENCE) { |
| 7252 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 7253 | parent = path->nodes[level]->start; |
| 7254 | } else { |
| 7255 | BUG_ON(root->root_key.objectid != |
| 7256 | btrfs_header_owner(path->nodes[level])); |
| 7257 | parent = 0; |
| 7258 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7259 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7260 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7261 | root->root_key.objectid, level - 1, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7262 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7263 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7264 | btrfs_tree_unlock(next); |
| 7265 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7266 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7267 | return 1; |
| 7268 | } |
| 7269 | |
| 7270 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 7271 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7272 | * |
| 7273 | * when wc->stage == DROP_REFERENCE, this function drops |
| 7274 | * reference count on the block. |
| 7275 | * |
| 7276 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 7277 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 7278 | * to UPDATE_BACKREF previously while processing the block. |
| 7279 | * |
| 7280 | * NOTE: return value 1 means we should stop walking up. |
| 7281 | */ |
| 7282 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 7283 | struct btrfs_root *root, |
| 7284 | struct btrfs_path *path, |
| 7285 | struct walk_control *wc) |
| 7286 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7287 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7288 | int level = wc->level; |
| 7289 | struct extent_buffer *eb = path->nodes[level]; |
| 7290 | u64 parent = 0; |
| 7291 | |
| 7292 | if (wc->stage == UPDATE_BACKREF) { |
| 7293 | BUG_ON(wc->shared_level < level); |
| 7294 | if (level < wc->shared_level) |
| 7295 | goto out; |
| 7296 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7297 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 7298 | if (ret > 0) |
| 7299 | wc->update_ref = 0; |
| 7300 | |
| 7301 | wc->stage = DROP_REFERENCE; |
| 7302 | wc->shared_level = -1; |
| 7303 | path->slots[level] = 0; |
| 7304 | |
| 7305 | /* |
| 7306 | * check reference count again if the block isn't locked. |
| 7307 | * we should start walking down the tree again if reference |
| 7308 | * count is one. |
| 7309 | */ |
| 7310 | if (!path->locks[level]) { |
| 7311 | BUG_ON(level == 0); |
| 7312 | btrfs_tree_lock(eb); |
| 7313 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7314 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7315 | |
| 7316 | ret = btrfs_lookup_extent_info(trans, root, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7317 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7318 | &wc->refs[level], |
| 7319 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7320 | if (ret < 0) { |
| 7321 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 7322 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7323 | return ret; |
| 7324 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7325 | BUG_ON(wc->refs[level] == 0); |
| 7326 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7327 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 7328 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7329 | return 1; |
| 7330 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7331 | } |
| 7332 | } |
| 7333 | |
| 7334 | /* wc->stage == DROP_REFERENCE */ |
| 7335 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 7336 | |
| 7337 | if (wc->refs[level] == 1) { |
| 7338 | if (level == 0) { |
| 7339 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7340 | ret = btrfs_dec_ref(trans, root, eb, 1, |
| 7341 | wc->for_reloc); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7342 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7343 | ret = btrfs_dec_ref(trans, root, eb, 0, |
| 7344 | wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7345 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7346 | } |
| 7347 | /* make block locked assertion in clean_tree_block happy */ |
| 7348 | if (!path->locks[level] && |
| 7349 | btrfs_header_generation(eb) == trans->transid) { |
| 7350 | btrfs_tree_lock(eb); |
| 7351 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7352 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7353 | } |
| 7354 | clean_tree_block(trans, root, eb); |
| 7355 | } |
| 7356 | |
| 7357 | if (eb == root->node) { |
| 7358 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 7359 | parent = eb->start; |
| 7360 | else |
| 7361 | BUG_ON(root->root_key.objectid != |
| 7362 | btrfs_header_owner(eb)); |
| 7363 | } else { |
| 7364 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 7365 | parent = path->nodes[level + 1]->start; |
| 7366 | else |
| 7367 | BUG_ON(root->root_key.objectid != |
| 7368 | btrfs_header_owner(path->nodes[level + 1])); |
| 7369 | } |
| 7370 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7371 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7372 | out: |
| 7373 | wc->refs[level] = 0; |
| 7374 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7375 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7376 | } |
| 7377 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7378 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 7379 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7380 | struct btrfs_path *path, |
| 7381 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7382 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7383 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7384 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7385 | int ret; |
| 7386 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7387 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7388 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7389 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7390 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7391 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7392 | if (level == 0) |
| 7393 | break; |
| 7394 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 7395 | if (path->slots[level] >= |
| 7396 | btrfs_header_nritems(path->nodes[level])) |
| 7397 | break; |
| 7398 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7399 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7400 | if (ret > 0) { |
| 7401 | path->slots[level]++; |
| 7402 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 7403 | } else if (ret < 0) |
| 7404 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7405 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7406 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7407 | return 0; |
| 7408 | } |
| 7409 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7410 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 7411 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7412 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7413 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7414 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7415 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7416 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7417 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7418 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 7419 | while (level < max_level && path->nodes[level]) { |
| 7420 | wc->level = level; |
| 7421 | if (path->slots[level] + 1 < |
| 7422 | btrfs_header_nritems(path->nodes[level])) { |
| 7423 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7424 | return 0; |
| 7425 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7426 | ret = walk_up_proc(trans, root, path, wc); |
| 7427 | if (ret > 0) |
| 7428 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 7429 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7430 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7431 | btrfs_tree_unlock_rw(path->nodes[level], |
| 7432 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7433 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7434 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7435 | free_extent_buffer(path->nodes[level]); |
| 7436 | path->nodes[level] = NULL; |
| 7437 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7438 | } |
| 7439 | } |
| 7440 | return 1; |
| 7441 | } |
| 7442 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7443 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7444 | * drop a subvolume tree. |
| 7445 | * |
| 7446 | * this function traverses the tree freeing any blocks that only |
| 7447 | * referenced by the tree. |
| 7448 | * |
| 7449 | * when a shared tree block is found. this function decreases its |
| 7450 | * reference count by one. if update_ref is true, this function |
| 7451 | * also make sure backrefs for the shared block and all lower level |
| 7452 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 7453 | * |
| 7454 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7455 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 7456 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7457 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 7458 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7459 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7460 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7461 | struct btrfs_trans_handle *trans; |
| 7462 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7463 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7464 | struct walk_control *wc; |
| 7465 | struct btrfs_key key; |
| 7466 | int err = 0; |
| 7467 | int ret; |
| 7468 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7469 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7470 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7471 | if (!path) { |
| 7472 | err = -ENOMEM; |
| 7473 | goto out; |
| 7474 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7475 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7476 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 7477 | if (!wc) { |
| 7478 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7479 | err = -ENOMEM; |
| 7480 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 7481 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7482 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7483 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7484 | if (IS_ERR(trans)) { |
| 7485 | err = PTR_ERR(trans); |
| 7486 | goto out_free; |
| 7487 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 7488 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7489 | if (block_rsv) |
| 7490 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7491 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7492 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7493 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7494 | path->nodes[level] = btrfs_lock_root_node(root); |
| 7495 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7496 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7497 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7498 | memset(&wc->update_progress, 0, |
| 7499 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7500 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7501 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7502 | memcpy(&wc->update_progress, &key, |
| 7503 | sizeof(wc->update_progress)); |
| 7504 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 7505 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7506 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 7507 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7508 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7509 | path->lowest_level = 0; |
| 7510 | if (ret < 0) { |
| 7511 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7512 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7513 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7514 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7515 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7516 | /* |
| 7517 | * unlock our path, this is safe because only this |
| 7518 | * function is allowed to delete this snapshot |
| 7519 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7520 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7521 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7522 | level = btrfs_header_level(root->node); |
| 7523 | while (1) { |
| 7524 | btrfs_tree_lock(path->nodes[level]); |
| 7525 | btrfs_set_lock_blocking(path->nodes[level]); |
| 7526 | |
| 7527 | ret = btrfs_lookup_extent_info(trans, root, |
| 7528 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7529 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7530 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7531 | if (ret < 0) { |
| 7532 | err = ret; |
| 7533 | goto out_end_trans; |
| 7534 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7535 | BUG_ON(wc->refs[level] == 0); |
| 7536 | |
| 7537 | if (level == root_item->drop_level) |
| 7538 | break; |
| 7539 | |
| 7540 | btrfs_tree_unlock(path->nodes[level]); |
| 7541 | WARN_ON(wc->refs[level] != 1); |
| 7542 | level--; |
| 7543 | } |
| 7544 | } |
| 7545 | |
| 7546 | wc->level = level; |
| 7547 | wc->shared_level = -1; |
| 7548 | wc->stage = DROP_REFERENCE; |
| 7549 | wc->update_ref = update_ref; |
| 7550 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7551 | wc->for_reloc = for_reloc; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7552 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7553 | |
| 7554 | while (1) { |
Miao Xie | babbf170 | 2013-05-14 10:20:43 +0000 | [diff] [blame] | 7555 | if (!for_reloc && btrfs_need_cleaner_sleep(root)) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 7556 | pr_debug("btrfs: drop snapshot early exit\n"); |
| 7557 | err = -EAGAIN; |
| 7558 | goto out_end_trans; |
| 7559 | } |
| 7560 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7561 | ret = walk_down_tree(trans, root, path, wc); |
| 7562 | if (ret < 0) { |
| 7563 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 7564 | break; |
| 7565 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7566 | |
| 7567 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 7568 | if (ret < 0) { |
| 7569 | err = ret; |
| 7570 | break; |
| 7571 | } |
| 7572 | |
| 7573 | if (ret > 0) { |
| 7574 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 7575 | break; |
| 7576 | } |
| 7577 | |
| 7578 | if (wc->stage == DROP_REFERENCE) { |
| 7579 | level = wc->level; |
| 7580 | btrfs_node_key(path->nodes[level], |
| 7581 | &root_item->drop_progress, |
| 7582 | path->slots[level]); |
| 7583 | root_item->drop_level = level; |
| 7584 | } |
| 7585 | |
| 7586 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7587 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7588 | ret = btrfs_update_root(trans, tree_root, |
| 7589 | &root->root_key, |
| 7590 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7591 | if (ret) { |
| 7592 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7593 | err = ret; |
| 7594 | goto out_end_trans; |
| 7595 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7596 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7597 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7598 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7599 | if (IS_ERR(trans)) { |
| 7600 | err = PTR_ERR(trans); |
| 7601 | goto out_free; |
| 7602 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7603 | if (block_rsv) |
| 7604 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7605 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7606 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7607 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7608 | if (err) |
| 7609 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7610 | |
| 7611 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7612 | if (ret) { |
| 7613 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7614 | goto out_end_trans; |
| 7615 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7616 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7617 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7618 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 7619 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7620 | if (ret < 0) { |
| 7621 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7622 | err = ret; |
| 7623 | goto out_end_trans; |
| 7624 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 7625 | /* if we fail to delete the orphan item this time |
| 7626 | * around, it'll get picked up the next time. |
| 7627 | * |
| 7628 | * The most common failure here is just -ENOENT. |
| 7629 | */ |
| 7630 | btrfs_del_orphan_item(trans, tree_root, |
| 7631 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7632 | } |
| 7633 | } |
| 7634 | |
| 7635 | if (root->in_radix) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7636 | btrfs_drop_and_free_fs_root(tree_root->fs_info, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7637 | } else { |
| 7638 | free_extent_buffer(root->node); |
| 7639 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 7640 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7641 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7642 | out_end_trans: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7643 | btrfs_end_transaction_throttle(trans, tree_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7644 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7645 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7646 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7647 | out: |
| 7648 | if (err) |
| 7649 | btrfs_std_error(root->fs_info, err); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 7650 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7651 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7652 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7653 | /* |
| 7654 | * drop subtree rooted at tree block 'node'. |
| 7655 | * |
| 7656 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7657 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7658 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7659 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 7660 | struct btrfs_root *root, |
| 7661 | struct extent_buffer *node, |
| 7662 | struct extent_buffer *parent) |
| 7663 | { |
| 7664 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7665 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7666 | int level; |
| 7667 | int parent_level; |
| 7668 | int ret = 0; |
| 7669 | int wret; |
| 7670 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7671 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 7672 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7673 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7674 | if (!path) |
| 7675 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7676 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7677 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7678 | if (!wc) { |
| 7679 | btrfs_free_path(path); |
| 7680 | return -ENOMEM; |
| 7681 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7682 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 7683 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7684 | parent_level = btrfs_header_level(parent); |
| 7685 | extent_buffer_get(parent); |
| 7686 | path->nodes[parent_level] = parent; |
| 7687 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 7688 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 7689 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7690 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7691 | path->nodes[level] = node; |
| 7692 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7693 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7694 | |
| 7695 | wc->refs[parent_level] = 1; |
| 7696 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7697 | wc->level = level; |
| 7698 | wc->shared_level = -1; |
| 7699 | wc->stage = DROP_REFERENCE; |
| 7700 | wc->update_ref = 0; |
| 7701 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7702 | wc->for_reloc = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7703 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7704 | |
| 7705 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7706 | wret = walk_down_tree(trans, root, path, wc); |
| 7707 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7708 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7709 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7710 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7711 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7712 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7713 | if (wret < 0) |
| 7714 | ret = wret; |
| 7715 | if (wret != 0) |
| 7716 | break; |
| 7717 | } |
| 7718 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7719 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7720 | btrfs_free_path(path); |
| 7721 | return ret; |
| 7722 | } |
| 7723 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7724 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7725 | { |
| 7726 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7727 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7728 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7729 | /* |
| 7730 | * if restripe for this chunk_type is on pick target profile and |
| 7731 | * return, otherwise do the usual balance |
| 7732 | */ |
| 7733 | stripped = get_restripe_target(root->fs_info, flags); |
| 7734 | if (stripped) |
| 7735 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 7736 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 7737 | /* |
| 7738 | * we add in the count of missing devices because we want |
| 7739 | * to make sure that any RAID levels on a degraded FS |
| 7740 | * continue to be honored. |
| 7741 | */ |
| 7742 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 7743 | root->fs_info->fs_devices->missing_devices; |
| 7744 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7745 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 7746 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7747 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7748 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7749 | if (num_devices == 1) { |
| 7750 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7751 | stripped = flags & ~stripped; |
| 7752 | |
| 7753 | /* turn raid0 into single device chunks */ |
| 7754 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7755 | return stripped; |
| 7756 | |
| 7757 | /* turn mirroring into duplication */ |
| 7758 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7759 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7760 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7761 | } else { |
| 7762 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7763 | if (flags & stripped) |
| 7764 | return flags; |
| 7765 | |
| 7766 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7767 | stripped = flags & ~stripped; |
| 7768 | |
| 7769 | /* switch duplicated blocks with raid1 */ |
| 7770 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7771 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7772 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7773 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7774 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7775 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7776 | return flags; |
| 7777 | } |
| 7778 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7779 | static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7780 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7781 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7782 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7783 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7784 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7785 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7786 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7787 | /* |
| 7788 | * We need some metadata space and system metadata space for |
| 7789 | * allocating chunks in some corner cases until we force to set |
| 7790 | * it to be readonly. |
| 7791 | */ |
| 7792 | if ((sinfo->flags & |
| 7793 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 7794 | !force) |
| 7795 | min_allocable_bytes = 1 * 1024 * 1024; |
| 7796 | else |
| 7797 | min_allocable_bytes = 0; |
| 7798 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7799 | spin_lock(&sinfo->lock); |
| 7800 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7801 | |
| 7802 | if (cache->ro) { |
| 7803 | ret = 0; |
| 7804 | goto out; |
| 7805 | } |
| 7806 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7807 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7808 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7809 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7810 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7811 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 7812 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7813 | sinfo->bytes_readonly += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7814 | cache->ro = 1; |
| 7815 | ret = 0; |
| 7816 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7817 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7818 | spin_unlock(&cache->lock); |
| 7819 | spin_unlock(&sinfo->lock); |
| 7820 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7821 | } |
| 7822 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7823 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 7824 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7825 | |
| 7826 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7827 | struct btrfs_trans_handle *trans; |
| 7828 | u64 alloc_flags; |
| 7829 | int ret; |
| 7830 | |
| 7831 | BUG_ON(cache->ro); |
| 7832 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 7833 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7834 | if (IS_ERR(trans)) |
| 7835 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7836 | |
| 7837 | alloc_flags = update_block_group_flags(root, cache->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7838 | if (alloc_flags != cache->flags) { |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 7839 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7840 | CHUNK_ALLOC_FORCE); |
| 7841 | if (ret < 0) |
| 7842 | goto out; |
| 7843 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7844 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7845 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7846 | if (!ret) |
| 7847 | goto out; |
| 7848 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 7849 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 7850 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7851 | if (ret < 0) |
| 7852 | goto out; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7853 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7854 | out: |
| 7855 | btrfs_end_transaction(trans, root); |
| 7856 | return ret; |
| 7857 | } |
| 7858 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 7859 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 7860 | struct btrfs_root *root, u64 type) |
| 7861 | { |
| 7862 | u64 alloc_flags = get_alloc_profile(root, type); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 7863 | return do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 7864 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 7865 | } |
| 7866 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7867 | /* |
| 7868 | * helper to account the unused space of all the readonly block group in the |
| 7869 | * list. takes mirrors into account. |
| 7870 | */ |
| 7871 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 7872 | { |
| 7873 | struct btrfs_block_group_cache *block_group; |
| 7874 | u64 free_bytes = 0; |
| 7875 | int factor; |
| 7876 | |
| 7877 | list_for_each_entry(block_group, groups_list, list) { |
| 7878 | spin_lock(&block_group->lock); |
| 7879 | |
| 7880 | if (!block_group->ro) { |
| 7881 | spin_unlock(&block_group->lock); |
| 7882 | continue; |
| 7883 | } |
| 7884 | |
| 7885 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7886 | BTRFS_BLOCK_GROUP_RAID10 | |
| 7887 | BTRFS_BLOCK_GROUP_DUP)) |
| 7888 | factor = 2; |
| 7889 | else |
| 7890 | factor = 1; |
| 7891 | |
| 7892 | free_bytes += (block_group->key.offset - |
| 7893 | btrfs_block_group_used(&block_group->item)) * |
| 7894 | factor; |
| 7895 | |
| 7896 | spin_unlock(&block_group->lock); |
| 7897 | } |
| 7898 | |
| 7899 | return free_bytes; |
| 7900 | } |
| 7901 | |
| 7902 | /* |
| 7903 | * helper to account the unused space of all the readonly block group in the |
| 7904 | * space_info. takes mirrors into account. |
| 7905 | */ |
| 7906 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 7907 | { |
| 7908 | int i; |
| 7909 | u64 free_bytes = 0; |
| 7910 | |
| 7911 | spin_lock(&sinfo->lock); |
| 7912 | |
| 7913 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 7914 | if (!list_empty(&sinfo->block_groups[i])) |
| 7915 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 7916 | &sinfo->block_groups[i]); |
| 7917 | |
| 7918 | spin_unlock(&sinfo->lock); |
| 7919 | |
| 7920 | return free_bytes; |
| 7921 | } |
| 7922 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 7923 | void btrfs_set_block_group_rw(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7924 | struct btrfs_block_group_cache *cache) |
| 7925 | { |
| 7926 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7927 | u64 num_bytes; |
| 7928 | |
| 7929 | BUG_ON(!cache->ro); |
| 7930 | |
| 7931 | spin_lock(&sinfo->lock); |
| 7932 | spin_lock(&cache->lock); |
| 7933 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7934 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 7935 | sinfo->bytes_readonly -= num_bytes; |
| 7936 | cache->ro = 0; |
| 7937 | spin_unlock(&cache->lock); |
| 7938 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7939 | } |
| 7940 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7941 | /* |
| 7942 | * checks to see if its even possible to relocate this block group. |
| 7943 | * |
| 7944 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7945 | * ok to go ahead and try. |
| 7946 | */ |
| 7947 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7948 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7949 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7950 | struct btrfs_space_info *space_info; |
| 7951 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7952 | struct btrfs_device *device; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 7953 | struct btrfs_trans_handle *trans; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7954 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 7955 | u64 dev_min = 1; |
| 7956 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7957 | u64 target; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7958 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7959 | int full = 0; |
| 7960 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7961 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7962 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7963 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7964 | /* odd, couldn't find the block group, leave it alone */ |
| 7965 | if (!block_group) |
| 7966 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7967 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7968 | min_free = btrfs_block_group_used(&block_group->item); |
| 7969 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7970 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7971 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7972 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7973 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7974 | space_info = block_group->space_info; |
| 7975 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7976 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7977 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7978 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7979 | /* |
| 7980 | * 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] | 7981 | * relocate it unless we're able to allocate a new chunk below. |
| 7982 | * |
| 7983 | * Otherwise, we need to make sure we have room in the space to handle |
| 7984 | * 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] | 7985 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7986 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7987 | (space_info->bytes_used + space_info->bytes_reserved + |
| 7988 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7989 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7990 | spin_unlock(&space_info->lock); |
| 7991 | goto out; |
| 7992 | } |
| 7993 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7994 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7995 | /* |
| 7996 | * ok we don't have enough space, but maybe we have free space on our |
| 7997 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7998 | * alloc devices and guess if we have enough space. if this block |
| 7999 | * group is going to be restriped, run checks against the target |
| 8000 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8001 | */ |
| 8002 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 8003 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8004 | /* |
| 8005 | * index: |
| 8006 | * 0: raid10 |
| 8007 | * 1: raid1 |
| 8008 | * 2: dup |
| 8009 | * 3: raid0 |
| 8010 | * 4: single |
| 8011 | */ |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8012 | target = get_restripe_target(root->fs_info, block_group->flags); |
| 8013 | if (target) { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 8014 | index = __get_raid_index(extended_to_chunk(target)); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8015 | } else { |
| 8016 | /* |
| 8017 | * this is just a balance, so if we were marked as full |
| 8018 | * we know there is no space for a new chunk |
| 8019 | */ |
| 8020 | if (full) |
| 8021 | goto out; |
| 8022 | |
| 8023 | index = get_block_group_index(block_group); |
| 8024 | } |
| 8025 | |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8026 | if (index == BTRFS_RAID_RAID10) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8027 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8028 | /* Divide by 2 */ |
| 8029 | min_free >>= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8030 | } else if (index == BTRFS_RAID_RAID1) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8031 | dev_min = 2; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8032 | } else if (index == BTRFS_RAID_DUP) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8033 | /* Multiply by 2 */ |
| 8034 | min_free <<= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8035 | } else if (index == BTRFS_RAID_RAID0) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8036 | dev_min = fs_devices->rw_devices; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8037 | do_div(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8038 | } |
| 8039 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8040 | /* We need to do this so that we can look at pending chunks */ |
| 8041 | trans = btrfs_join_transaction(root); |
| 8042 | if (IS_ERR(trans)) { |
| 8043 | ret = PTR_ERR(trans); |
| 8044 | goto out; |
| 8045 | } |
| 8046 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8047 | mutex_lock(&root->fs_info->chunk_mutex); |
| 8048 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8049 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8050 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8051 | /* |
| 8052 | * check to make sure we can actually find a chunk with enough |
| 8053 | * space to fit our block group in. |
| 8054 | */ |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 8055 | if (device->total_bytes > device->bytes_used + min_free && |
| 8056 | !device->is_tgtdev_for_dev_replace) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8057 | ret = find_free_dev_extent(trans, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8058 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8059 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8060 | dev_nr++; |
| 8061 | |
| 8062 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8063 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8064 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8065 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8066 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8067 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8068 | mutex_unlock(&root->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8069 | btrfs_end_transaction(trans, root); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8070 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8071 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8072 | return ret; |
| 8073 | } |
| 8074 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 8075 | static int find_first_block_group(struct btrfs_root *root, |
| 8076 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8077 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8078 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8079 | struct btrfs_key found_key; |
| 8080 | struct extent_buffer *leaf; |
| 8081 | int slot; |
| 8082 | |
| 8083 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 8084 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8085 | goto out; |
| 8086 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8087 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8088 | slot = path->slots[0]; |
| 8089 | leaf = path->nodes[0]; |
| 8090 | if (slot >= btrfs_header_nritems(leaf)) { |
| 8091 | ret = btrfs_next_leaf(root, path); |
| 8092 | if (ret == 0) |
| 8093 | continue; |
| 8094 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8095 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8096 | break; |
| 8097 | } |
| 8098 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 8099 | |
| 8100 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8101 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 8102 | ret = 0; |
| 8103 | goto out; |
| 8104 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8105 | path->slots[0]++; |
| 8106 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8107 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8108 | return ret; |
| 8109 | } |
| 8110 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8111 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 8112 | { |
| 8113 | struct btrfs_block_group_cache *block_group; |
| 8114 | u64 last = 0; |
| 8115 | |
| 8116 | while (1) { |
| 8117 | struct inode *inode; |
| 8118 | |
| 8119 | block_group = btrfs_lookup_first_block_group(info, last); |
| 8120 | while (block_group) { |
| 8121 | spin_lock(&block_group->lock); |
| 8122 | if (block_group->iref) |
| 8123 | break; |
| 8124 | spin_unlock(&block_group->lock); |
| 8125 | block_group = next_block_group(info->tree_root, |
| 8126 | block_group); |
| 8127 | } |
| 8128 | if (!block_group) { |
| 8129 | if (last == 0) |
| 8130 | break; |
| 8131 | last = 0; |
| 8132 | continue; |
| 8133 | } |
| 8134 | |
| 8135 | inode = block_group->inode; |
| 8136 | block_group->iref = 0; |
| 8137 | block_group->inode = NULL; |
| 8138 | spin_unlock(&block_group->lock); |
| 8139 | iput(inode); |
| 8140 | last = block_group->key.objectid + block_group->key.offset; |
| 8141 | btrfs_put_block_group(block_group); |
| 8142 | } |
| 8143 | } |
| 8144 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8145 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 8146 | { |
| 8147 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8148 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8149 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8150 | struct rb_node *n; |
| 8151 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8152 | down_write(&info->extent_commit_sem); |
| 8153 | while (!list_empty(&info->caching_block_groups)) { |
| 8154 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 8155 | struct btrfs_caching_control, list); |
| 8156 | list_del(&caching_ctl->list); |
| 8157 | put_caching_control(caching_ctl); |
| 8158 | } |
| 8159 | up_write(&info->extent_commit_sem); |
| 8160 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8161 | spin_lock(&info->block_group_cache_lock); |
| 8162 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 8163 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 8164 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8165 | rb_erase(&block_group->cache_node, |
| 8166 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8167 | spin_unlock(&info->block_group_cache_lock); |
| 8168 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8169 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8170 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8171 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8172 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8173 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8174 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8175 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8176 | /* |
| 8177 | * We haven't cached this block group, which means we could |
| 8178 | * possibly have excluded extents on this block group. |
| 8179 | */ |
| 8180 | if (block_group->cached == BTRFS_CACHE_NO) |
| 8181 | free_excluded_extents(info->extent_root, block_group); |
| 8182 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8183 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 8184 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8185 | |
| 8186 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8187 | } |
| 8188 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8189 | |
| 8190 | /* now that all the block groups are freed, go through and |
| 8191 | * free all the space_info structs. This is only called during |
| 8192 | * the final stages of unmount, and so we know nobody is |
| 8193 | * using them. We call synchronize_rcu() once before we start, |
| 8194 | * just to be on the safe side. |
| 8195 | */ |
| 8196 | synchronize_rcu(); |
| 8197 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 8198 | release_global_block_rsv(info); |
| 8199 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8200 | while(!list_empty(&info->space_info)) { |
| 8201 | space_info = list_entry(info->space_info.next, |
| 8202 | struct btrfs_space_info, |
| 8203 | list); |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 8204 | if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) { |
| 8205 | if (space_info->bytes_pinned > 0 || |
| 8206 | space_info->bytes_reserved > 0 || |
| 8207 | space_info->bytes_may_use > 0) { |
| 8208 | WARN_ON(1); |
| 8209 | dump_space_info(space_info, 0, 0); |
| 8210 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8211 | } |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 8212 | percpu_counter_destroy(&space_info->total_bytes_pinned); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8213 | list_del(&space_info->list); |
| 8214 | kfree(space_info); |
| 8215 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8216 | return 0; |
| 8217 | } |
| 8218 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8219 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 8220 | struct btrfs_block_group_cache *cache) |
| 8221 | { |
| 8222 | int index = get_block_group_index(cache); |
| 8223 | |
| 8224 | down_write(&space_info->groups_sem); |
| 8225 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 8226 | up_write(&space_info->groups_sem); |
| 8227 | } |
| 8228 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8229 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 8230 | { |
| 8231 | struct btrfs_path *path; |
| 8232 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8233 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8234 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8235 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8236 | struct btrfs_key key; |
| 8237 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8238 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8239 | int need_clear = 0; |
| 8240 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8241 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8242 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8243 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8244 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8245 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8246 | path = btrfs_alloc_path(); |
| 8247 | if (!path) |
| 8248 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 8249 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8250 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 8251 | cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 8252 | if (btrfs_test_opt(root, SPACE_CACHE) && |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 8253 | btrfs_super_generation(root->fs_info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8254 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 8255 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 8256 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8257 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8258 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8259 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8260 | if (ret > 0) |
| 8261 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8262 | if (ret != 0) |
| 8263 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8264 | leaf = path->nodes[0]; |
| 8265 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8266 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8267 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8268 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8269 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8270 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 8271 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 8272 | GFP_NOFS); |
| 8273 | if (!cache->free_space_ctl) { |
| 8274 | kfree(cache); |
| 8275 | ret = -ENOMEM; |
| 8276 | goto error; |
| 8277 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 8278 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8279 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8280 | spin_lock_init(&cache->lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8281 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8282 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8283 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8284 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 8285 | if (need_clear) { |
| 8286 | /* |
| 8287 | * When we mount with old space cache, we need to |
| 8288 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 8289 | * |
| 8290 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 8291 | * truncate the old free space cache inode and |
| 8292 | * setup a new one. |
| 8293 | * b) Setting 'dirty flag' makes sure that we flush |
| 8294 | * the new space cache info onto disk. |
| 8295 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8296 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 8297 | if (btrfs_test_opt(root, SPACE_CACHE)) |
| 8298 | cache->dirty = 1; |
| 8299 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8300 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8301 | read_extent_buffer(leaf, &cache->item, |
| 8302 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 8303 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8304 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8305 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8306 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8307 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8308 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8309 | cache->sectorsize = root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8310 | cache->full_stripe_len = btrfs_full_stripe_len(root, |
| 8311 | &root->fs_info->mapping_tree, |
| 8312 | found_key.objectid); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 8313 | btrfs_init_free_space_ctl(cache); |
| 8314 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8315 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8316 | * We need to exclude the super stripes now so that the space |
| 8317 | * info has super bytes accounted for, otherwise we'll think |
| 8318 | * we have more space than we actually do. |
| 8319 | */ |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 8320 | ret = exclude_super_stripes(root, cache); |
| 8321 | if (ret) { |
| 8322 | /* |
| 8323 | * We may have excluded something, so call this just in |
| 8324 | * case. |
| 8325 | */ |
| 8326 | free_excluded_extents(root, cache); |
| 8327 | kfree(cache->free_space_ctl); |
| 8328 | kfree(cache); |
| 8329 | goto error; |
| 8330 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8331 | |
| 8332 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8333 | * check for two cases, either we are full, and therefore |
| 8334 | * don't need to bother with the caching work since we won't |
| 8335 | * find any space, or we are empty, and we can just add all |
| 8336 | * the space in and be done with it. This saves us _alot_ of |
| 8337 | * time, particularly in the full case. |
| 8338 | */ |
| 8339 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8340 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8341 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8342 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8343 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8344 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8345 | cache->cached = BTRFS_CACHE_FINISHED; |
| 8346 | add_new_free_space(cache, root->fs_info, |
| 8347 | found_key.objectid, |
| 8348 | found_key.objectid + |
| 8349 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8350 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8351 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8352 | |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8353 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8354 | if (ret) { |
| 8355 | btrfs_remove_free_space_cache(cache); |
| 8356 | btrfs_put_block_group(cache); |
| 8357 | goto error; |
| 8358 | } |
| 8359 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8360 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 8361 | btrfs_block_group_used(&cache->item), |
| 8362 | &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8363 | if (ret) { |
| 8364 | btrfs_remove_free_space_cache(cache); |
| 8365 | spin_lock(&info->block_group_cache_lock); |
| 8366 | rb_erase(&cache->cache_node, |
| 8367 | &info->block_group_cache_tree); |
| 8368 | spin_unlock(&info->block_group_cache_lock); |
| 8369 | btrfs_put_block_group(cache); |
| 8370 | goto error; |
| 8371 | } |
| 8372 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8373 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8374 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8375 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8376 | spin_unlock(&cache->space_info->lock); |
| 8377 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8378 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8379 | |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 8380 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 8381 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8382 | set_block_group_ro(cache, 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8383 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8384 | |
| 8385 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 8386 | if (!(get_alloc_profile(root, space_info->flags) & |
| 8387 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 8388 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8389 | BTRFS_BLOCK_GROUP_RAID5 | |
| 8390 | BTRFS_BLOCK_GROUP_RAID6 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8391 | BTRFS_BLOCK_GROUP_DUP))) |
| 8392 | continue; |
| 8393 | /* |
| 8394 | * avoid allocating from un-mirrored block group if there are |
| 8395 | * mirrored block groups. |
| 8396 | */ |
| 8397 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8398 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8399 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8400 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8401 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8402 | |
| 8403 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8404 | ret = 0; |
| 8405 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8406 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8407 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8408 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8409 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8410 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
| 8411 | struct btrfs_root *root) |
| 8412 | { |
| 8413 | struct btrfs_block_group_cache *block_group, *tmp; |
| 8414 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 8415 | struct btrfs_block_group_item item; |
| 8416 | struct btrfs_key key; |
| 8417 | int ret = 0; |
| 8418 | |
| 8419 | list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, |
| 8420 | new_bg_list) { |
| 8421 | list_del_init(&block_group->new_bg_list); |
| 8422 | |
| 8423 | if (ret) |
| 8424 | continue; |
| 8425 | |
| 8426 | spin_lock(&block_group->lock); |
| 8427 | memcpy(&item, &block_group->item, sizeof(item)); |
| 8428 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8429 | spin_unlock(&block_group->lock); |
| 8430 | |
| 8431 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 8432 | sizeof(item)); |
| 8433 | if (ret) |
| 8434 | btrfs_abort_transaction(trans, extent_root, ret); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8435 | ret = btrfs_finish_chunk_alloc(trans, extent_root, |
| 8436 | key.objectid, key.offset); |
| 8437 | if (ret) |
| 8438 | btrfs_abort_transaction(trans, extent_root, ret); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8439 | } |
| 8440 | } |
| 8441 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8442 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8443 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8444 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8445 | u64 size) |
| 8446 | { |
| 8447 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8448 | struct btrfs_root *extent_root; |
| 8449 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8450 | |
| 8451 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8452 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8453 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8454 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8455 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8456 | if (!cache) |
| 8457 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 8458 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 8459 | GFP_NOFS); |
| 8460 | if (!cache->free_space_ctl) { |
| 8461 | kfree(cache); |
| 8462 | return -ENOMEM; |
| 8463 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8464 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8465 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8466 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8467 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8468 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8469 | cache->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8470 | cache->full_stripe_len = btrfs_full_stripe_len(root, |
| 8471 | &root->fs_info->mapping_tree, |
| 8472 | chunk_offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8473 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8474 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8475 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8476 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8477 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8478 | INIT_LIST_HEAD(&cache->new_bg_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8479 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 8480 | btrfs_init_free_space_ctl(cache); |
| 8481 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8482 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8483 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 8484 | cache->flags = type; |
| 8485 | btrfs_set_block_group_flags(&cache->item, type); |
| 8486 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8487 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8488 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 8489 | ret = exclude_super_stripes(root, cache); |
| 8490 | if (ret) { |
| 8491 | /* |
| 8492 | * We may have excluded something, so call this just in |
| 8493 | * case. |
| 8494 | */ |
| 8495 | free_excluded_extents(root, cache); |
| 8496 | kfree(cache->free_space_ctl); |
| 8497 | kfree(cache); |
| 8498 | return ret; |
| 8499 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8500 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8501 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8502 | chunk_offset + size); |
| 8503 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8504 | free_excluded_extents(root, cache); |
| 8505 | |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8506 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8507 | if (ret) { |
| 8508 | btrfs_remove_free_space_cache(cache); |
| 8509 | btrfs_put_block_group(cache); |
| 8510 | return ret; |
| 8511 | } |
| 8512 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8513 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8514 | &cache->space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8515 | if (ret) { |
| 8516 | btrfs_remove_free_space_cache(cache); |
| 8517 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 8518 | rb_erase(&cache->cache_node, |
| 8519 | &root->fs_info->block_group_cache_tree); |
| 8520 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 8521 | btrfs_put_block_group(cache); |
| 8522 | return ret; |
| 8523 | } |
Li Zefan | c7c144d | 2011-12-07 10:39:22 +0800 | [diff] [blame] | 8524 | update_global_block_rsv(root->fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8525 | |
| 8526 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8527 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8528 | spin_unlock(&cache->space_info->lock); |
| 8529 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8530 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8531 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8532 | list_add_tail(&cache->new_bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8533 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8534 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8535 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8536 | return 0; |
| 8537 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8538 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8539 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 8540 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 8541 | u64 extra_flags = chunk_to_extended(flags) & |
| 8542 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8543 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 8544 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8545 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 8546 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 8547 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 8548 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 8549 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 8550 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 8551 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8552 | } |
| 8553 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8554 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8555 | struct btrfs_root *root, u64 group_start) |
| 8556 | { |
| 8557 | struct btrfs_path *path; |
| 8558 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8559 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8560 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8561 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8562 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8563 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8564 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8565 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8566 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8567 | root = root->fs_info->extent_root; |
| 8568 | |
| 8569 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8570 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8571 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8572 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 8573 | /* |
| 8574 | * Free the reserved super bytes from this block group before |
| 8575 | * remove it. |
| 8576 | */ |
| 8577 | free_excluded_extents(root, block_group); |
| 8578 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8579 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8580 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8581 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 8582 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8583 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8584 | factor = 2; |
| 8585 | else |
| 8586 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8587 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8588 | /* make sure this block group isn't part of an allocation cluster */ |
| 8589 | cluster = &root->fs_info->data_alloc_cluster; |
| 8590 | spin_lock(&cluster->refill_lock); |
| 8591 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8592 | spin_unlock(&cluster->refill_lock); |
| 8593 | |
| 8594 | /* |
| 8595 | * make sure this block group isn't part of a metadata |
| 8596 | * allocation cluster |
| 8597 | */ |
| 8598 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8599 | spin_lock(&cluster->refill_lock); |
| 8600 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8601 | spin_unlock(&cluster->refill_lock); |
| 8602 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8603 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 8604 | if (!path) { |
| 8605 | ret = -ENOMEM; |
| 8606 | goto out; |
| 8607 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8608 | |
Ilya Dryomov | 10b2f34 | 2011-10-02 13:56:53 +0300 | [diff] [blame] | 8609 | inode = lookup_free_space_inode(tree_root, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8610 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 8611 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8612 | if (ret) { |
| 8613 | btrfs_add_delayed_iput(inode); |
| 8614 | goto out; |
| 8615 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8616 | clear_nlink(inode); |
| 8617 | /* One for the block groups ref */ |
| 8618 | spin_lock(&block_group->lock); |
| 8619 | if (block_group->iref) { |
| 8620 | block_group->iref = 0; |
| 8621 | block_group->inode = NULL; |
| 8622 | spin_unlock(&block_group->lock); |
| 8623 | iput(inode); |
| 8624 | } else { |
| 8625 | spin_unlock(&block_group->lock); |
| 8626 | } |
| 8627 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 8628 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8629 | } |
| 8630 | |
| 8631 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 8632 | key.offset = block_group->key.objectid; |
| 8633 | key.type = 0; |
| 8634 | |
| 8635 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 8636 | if (ret < 0) |
| 8637 | goto out; |
| 8638 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8639 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8640 | if (ret == 0) { |
| 8641 | ret = btrfs_del_item(trans, tree_root, path); |
| 8642 | if (ret) |
| 8643 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8644 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8645 | } |
| 8646 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8647 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8648 | rb_erase(&block_group->cache_node, |
| 8649 | &root->fs_info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 8650 | |
| 8651 | if (root->fs_info->first_logical_byte == block_group->key.objectid) |
| 8652 | root->fs_info->first_logical_byte = (u64)-1; |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8653 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8654 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8655 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8656 | /* |
| 8657 | * we must use list_del_init so people can check to see if they |
| 8658 | * are still on the list after taking the semaphore |
| 8659 | */ |
| 8660 | list_del_init(&block_group->list); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8661 | if (list_empty(&block_group->space_info->block_groups[index])) |
| 8662 | clear_avail_alloc_bits(root->fs_info, block_group->flags); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8663 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8664 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8665 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8666 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8667 | |
| 8668 | btrfs_remove_free_space_cache(block_group); |
| 8669 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8670 | spin_lock(&block_group->space_info->lock); |
| 8671 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8672 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8673 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8674 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8675 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8676 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8677 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8678 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8679 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8680 | btrfs_put_block_group(block_group); |
| 8681 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8682 | |
| 8683 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8684 | if (ret > 0) |
| 8685 | ret = -EIO; |
| 8686 | if (ret < 0) |
| 8687 | goto out; |
| 8688 | |
| 8689 | ret = btrfs_del_item(trans, root, path); |
| 8690 | out: |
| 8691 | btrfs_free_path(path); |
| 8692 | return ret; |
| 8693 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8694 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8695 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 8696 | { |
| 8697 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8698 | struct btrfs_super_block *disk_super; |
| 8699 | u64 features; |
| 8700 | u64 flags; |
| 8701 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8702 | int ret; |
| 8703 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 8704 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8705 | if (!btrfs_super_root(disk_super)) |
| 8706 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8707 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8708 | features = btrfs_super_incompat_flags(disk_super); |
| 8709 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 8710 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8711 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8712 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 8713 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8714 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8715 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8716 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8717 | if (mixed) { |
| 8718 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 8719 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8720 | } else { |
| 8721 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 8722 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8723 | if (ret) |
| 8724 | goto out; |
| 8725 | |
| 8726 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 8727 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8728 | } |
| 8729 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8730 | return ret; |
| 8731 | } |
| 8732 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8733 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 8734 | { |
| 8735 | return unpin_extent_range(root, start, end); |
| 8736 | } |
| 8737 | |
| 8738 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8739 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8740 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8741 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8742 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8743 | |
| 8744 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 8745 | { |
| 8746 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 8747 | struct btrfs_block_group_cache *cache = NULL; |
| 8748 | u64 group_trimmed; |
| 8749 | u64 start; |
| 8750 | u64 end; |
| 8751 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 8752 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8753 | int ret = 0; |
| 8754 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 8755 | /* |
| 8756 | * try to trim all FS space, our block group may start from non-zero. |
| 8757 | */ |
| 8758 | if (range->len == total_bytes) |
| 8759 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 8760 | else |
| 8761 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8762 | |
| 8763 | while (cache) { |
| 8764 | if (cache->key.objectid >= (range->start + range->len)) { |
| 8765 | btrfs_put_block_group(cache); |
| 8766 | break; |
| 8767 | } |
| 8768 | |
| 8769 | start = max(range->start, cache->key.objectid); |
| 8770 | end = min(range->start + range->len, |
| 8771 | cache->key.objectid + cache->key.offset); |
| 8772 | |
| 8773 | if (end - start >= range->minlen) { |
| 8774 | if (!block_group_cache_done(cache)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 8775 | ret = cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 8776 | if (ret) { |
| 8777 | btrfs_put_block_group(cache); |
| 8778 | break; |
| 8779 | } |
| 8780 | ret = wait_block_group_cache_done(cache); |
| 8781 | if (ret) { |
| 8782 | btrfs_put_block_group(cache); |
| 8783 | break; |
| 8784 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8785 | } |
| 8786 | ret = btrfs_trim_block_group(cache, |
| 8787 | &group_trimmed, |
| 8788 | start, |
| 8789 | end, |
| 8790 | range->minlen); |
| 8791 | |
| 8792 | trimmed += group_trimmed; |
| 8793 | if (ret) { |
| 8794 | btrfs_put_block_group(cache); |
| 8795 | break; |
| 8796 | } |
| 8797 | } |
| 8798 | |
| 8799 | cache = next_block_group(fs_info->tree_root, cache); |
| 8800 | } |
| 8801 | |
| 8802 | range->len = trimmed; |
| 8803 | return ret; |
| 8804 | } |