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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 27 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 28 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 29 | #include "ctree.h" |
| 30 | #include "disk-io.h" |
| 31 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 32 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 34 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 35 | #include "free-space-cache.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame^] | 36 | #include "math.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 37 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 38 | #undef SCRAMBLE_DELAYED_REFS |
| 39 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 40 | /* |
| 41 | * control flags for do_chunk_alloc's force field |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 42 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 43 | * if we really need one. |
| 44 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 45 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 46 | * if we have very few chunks already allocated. This is |
| 47 | * used as part of the clustering code to help make sure |
| 48 | * we have a good pool of storage to cluster in, without |
| 49 | * filling the FS with empty chunks |
| 50 | * |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 51 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 52 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 53 | */ |
| 54 | enum { |
| 55 | CHUNK_ALLOC_NO_FORCE = 0, |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 56 | CHUNK_ALLOC_LIMITED = 1, |
| 57 | CHUNK_ALLOC_FORCE = 2, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 60 | /* |
| 61 | * Control how reservations are dealt with. |
| 62 | * |
| 63 | * RESERVE_FREE - freeing a reservation. |
| 64 | * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for |
| 65 | * ENOSPC accounting |
| 66 | * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update |
| 67 | * bytes_may_use as the ENOSPC accounting is done elsewhere |
| 68 | */ |
| 69 | enum { |
| 70 | RESERVE_FREE = 0, |
| 71 | RESERVE_ALLOC = 1, |
| 72 | RESERVE_ALLOC_NO_ACCOUNT = 2, |
| 73 | }; |
| 74 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 75 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 76 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 77 | u64 bytenr, u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 78 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 79 | struct btrfs_root *root, |
| 80 | u64 bytenr, u64 num_bytes, u64 parent, |
| 81 | u64 root_objectid, u64 owner_objectid, |
| 82 | u64 owner_offset, int refs_to_drop, |
| 83 | struct btrfs_delayed_extent_op *extra_op); |
| 84 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 85 | struct extent_buffer *leaf, |
| 86 | struct btrfs_extent_item *ei); |
| 87 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 88 | struct btrfs_root *root, |
| 89 | u64 parent, u64 root_objectid, |
| 90 | u64 flags, u64 owner, u64 offset, |
| 91 | struct btrfs_key *ins, int ref_mod); |
| 92 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 93 | struct btrfs_root *root, |
| 94 | u64 parent, u64 root_objectid, |
| 95 | u64 flags, struct btrfs_disk_key *key, |
| 96 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 97 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 98 | struct btrfs_root *extent_root, u64 flags, |
| 99 | int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 100 | static int find_next_key(struct btrfs_path *path, int level, |
| 101 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 102 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 103 | int dump_block_groups); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 104 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 105 | u64 num_bytes, int reserve); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 106 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 107 | static noinline int |
| 108 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 109 | { |
| 110 | smp_mb(); |
| 111 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 112 | } |
| 113 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 114 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 115 | { |
| 116 | return (cache->flags & bits) == bits; |
| 117 | } |
| 118 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 119 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 120 | { |
| 121 | atomic_inc(&cache->count); |
| 122 | } |
| 123 | |
| 124 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 125 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 126 | if (atomic_dec_and_test(&cache->count)) { |
| 127 | WARN_ON(cache->pinned > 0); |
| 128 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 129 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 130 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 131 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 134 | /* |
| 135 | * this adds the block group to the fs_info rb tree for the block group |
| 136 | * cache |
| 137 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 138 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 139 | struct btrfs_block_group_cache *block_group) |
| 140 | { |
| 141 | struct rb_node **p; |
| 142 | struct rb_node *parent = NULL; |
| 143 | struct btrfs_block_group_cache *cache; |
| 144 | |
| 145 | spin_lock(&info->block_group_cache_lock); |
| 146 | p = &info->block_group_cache_tree.rb_node; |
| 147 | |
| 148 | while (*p) { |
| 149 | parent = *p; |
| 150 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 151 | cache_node); |
| 152 | if (block_group->key.objectid < cache->key.objectid) { |
| 153 | p = &(*p)->rb_left; |
| 154 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 155 | p = &(*p)->rb_right; |
| 156 | } else { |
| 157 | spin_unlock(&info->block_group_cache_lock); |
| 158 | return -EEXIST; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | rb_link_node(&block_group->cache_node, parent, p); |
| 163 | rb_insert_color(&block_group->cache_node, |
| 164 | &info->block_group_cache_tree); |
| 165 | spin_unlock(&info->block_group_cache_lock); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * This will return the block group at or after bytenr if contains is 0, else |
| 172 | * it will return the block group that contains the bytenr |
| 173 | */ |
| 174 | static struct btrfs_block_group_cache * |
| 175 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 176 | int contains) |
| 177 | { |
| 178 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 179 | struct rb_node *n; |
| 180 | u64 end, start; |
| 181 | |
| 182 | spin_lock(&info->block_group_cache_lock); |
| 183 | n = info->block_group_cache_tree.rb_node; |
| 184 | |
| 185 | while (n) { |
| 186 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 187 | cache_node); |
| 188 | end = cache->key.objectid + cache->key.offset - 1; |
| 189 | start = cache->key.objectid; |
| 190 | |
| 191 | if (bytenr < start) { |
| 192 | if (!contains && (!ret || start < ret->key.objectid)) |
| 193 | ret = cache; |
| 194 | n = n->rb_left; |
| 195 | } else if (bytenr > start) { |
| 196 | if (contains && bytenr <= end) { |
| 197 | ret = cache; |
| 198 | break; |
| 199 | } |
| 200 | n = n->rb_right; |
| 201 | } else { |
| 202 | ret = cache; |
| 203 | break; |
| 204 | } |
| 205 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 206 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 207 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 208 | spin_unlock(&info->block_group_cache_lock); |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 213 | static int add_excluded_extent(struct btrfs_root *root, |
| 214 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 215 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 216 | u64 end = start + num_bytes - 1; |
| 217 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 218 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 219 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 220 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 221 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 224 | static void free_excluded_extents(struct btrfs_root *root, |
| 225 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 226 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 227 | u64 start, end; |
| 228 | |
| 229 | start = cache->key.objectid; |
| 230 | end = start + cache->key.offset - 1; |
| 231 | |
| 232 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 233 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 234 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 235 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 236 | } |
| 237 | |
| 238 | static int exclude_super_stripes(struct btrfs_root *root, |
| 239 | struct btrfs_block_group_cache *cache) |
| 240 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 241 | u64 bytenr; |
| 242 | u64 *logical; |
| 243 | int stripe_len; |
| 244 | int i, nr, ret; |
| 245 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 246 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 247 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 248 | cache->bytes_super += stripe_len; |
| 249 | ret = add_excluded_extent(root, cache->key.objectid, |
| 250 | stripe_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 251 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 254 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 255 | bytenr = btrfs_sb_offset(i); |
| 256 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 257 | cache->key.objectid, bytenr, |
| 258 | 0, &logical, &nr, &stripe_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 259 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 260 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 261 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 262 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 263 | ret = add_excluded_extent(root, logical[nr], |
| 264 | stripe_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 265 | BUG_ON(ret); /* -ENOMEM */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 266 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 267 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 268 | kfree(logical); |
| 269 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 273 | static struct btrfs_caching_control * |
| 274 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 275 | { |
| 276 | struct btrfs_caching_control *ctl; |
| 277 | |
| 278 | spin_lock(&cache->lock); |
| 279 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 280 | spin_unlock(&cache->lock); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 284 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 285 | if (!cache->caching_ctl) { |
| 286 | spin_unlock(&cache->lock); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 290 | ctl = cache->caching_ctl; |
| 291 | atomic_inc(&ctl->count); |
| 292 | spin_unlock(&cache->lock); |
| 293 | return ctl; |
| 294 | } |
| 295 | |
| 296 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 297 | { |
| 298 | if (atomic_dec_and_test(&ctl->count)) |
| 299 | kfree(ctl); |
| 300 | } |
| 301 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 302 | /* |
| 303 | * this is only called by cache_block_group, since we could have freed extents |
| 304 | * we need to check the pinned_extents for any extents that can't be used yet |
| 305 | * since their free space will be released as soon as the transaction commits. |
| 306 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 307 | 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] | 308 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 309 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 310 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 311 | int ret; |
| 312 | |
| 313 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 314 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 315 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 316 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 317 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 318 | if (ret) |
| 319 | break; |
| 320 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 321 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 322 | start = extent_end + 1; |
| 323 | } else if (extent_start > start && extent_start < end) { |
| 324 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 325 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 326 | ret = btrfs_add_free_space(block_group, start, |
| 327 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 328 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 329 | start = extent_end + 1; |
| 330 | } else { |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (start < end) { |
| 336 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 337 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 338 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 339 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 342 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 345 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 346 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 347 | struct btrfs_block_group_cache *block_group; |
| 348 | struct btrfs_fs_info *fs_info; |
| 349 | struct btrfs_caching_control *caching_ctl; |
| 350 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 351 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 352 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 353 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 354 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 355 | u64 last = 0; |
| 356 | u32 nritems; |
| 357 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 358 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 359 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 360 | block_group = caching_ctl->block_group; |
| 361 | fs_info = block_group->fs_info; |
| 362 | extent_root = fs_info->extent_root; |
| 363 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 364 | path = btrfs_alloc_path(); |
| 365 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 366 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 367 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 368 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 369 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 370 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 371 | * We don't want to deadlock with somebody trying to allocate a new |
| 372 | * extent for the extent root while also trying to search the extent |
| 373 | * root to add free space. So we skip locking and search the commit |
| 374 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 375 | */ |
| 376 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 377 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 378 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 379 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 380 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 381 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 382 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 383 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 384 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 385 | /* need to make sure the commit_root doesn't disappear */ |
| 386 | down_read(&fs_info->extent_commit_sem); |
| 387 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 388 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 389 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 390 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 391 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 392 | leaf = path->nodes[0]; |
| 393 | nritems = btrfs_header_nritems(leaf); |
| 394 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 395 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 396 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 397 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 398 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 399 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 400 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 401 | if (path->slots[0] < nritems) { |
| 402 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 403 | } else { |
| 404 | ret = find_next_key(path, 0, &key); |
| 405 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 406 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 407 | |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 408 | if (need_resched() || |
| 409 | btrfs_next_leaf(extent_root, path)) { |
| 410 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 411 | btrfs_release_path(path); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 412 | up_read(&fs_info->extent_commit_sem); |
| 413 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 414 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 415 | goto again; |
| 416 | } |
| 417 | leaf = path->nodes[0]; |
| 418 | nritems = btrfs_header_nritems(leaf); |
| 419 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 420 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 421 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 422 | if (key.objectid < block_group->key.objectid) { |
| 423 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 424 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 425 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 426 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 427 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 428 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 429 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 430 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 432 | total_found += add_new_free_space(block_group, |
| 433 | fs_info, last, |
| 434 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 435 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 436 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 437 | if (total_found > (1024 * 1024 * 2)) { |
| 438 | total_found = 0; |
| 439 | wake_up(&caching_ctl->wait); |
| 440 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 441 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 442 | path->slots[0]++; |
| 443 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 444 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 445 | |
| 446 | total_found += add_new_free_space(block_group, fs_info, last, |
| 447 | block_group->key.objectid + |
| 448 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 449 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 450 | |
| 451 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 452 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 453 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 454 | spin_unlock(&block_group->lock); |
| 455 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 456 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 457 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 458 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 459 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 460 | free_excluded_extents(extent_root, block_group); |
| 461 | |
| 462 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 463 | out: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 464 | wake_up(&caching_ctl->wait); |
| 465 | |
| 466 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 467 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 470 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 471 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 472 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 473 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 474 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 475 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 476 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 477 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 478 | int ret = 0; |
| 479 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 480 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 481 | if (!caching_ctl) |
| 482 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 483 | |
| 484 | INIT_LIST_HEAD(&caching_ctl->list); |
| 485 | mutex_init(&caching_ctl->mutex); |
| 486 | init_waitqueue_head(&caching_ctl->wait); |
| 487 | caching_ctl->block_group = cache; |
| 488 | caching_ctl->progress = cache->key.objectid; |
| 489 | atomic_set(&caching_ctl->count, 1); |
| 490 | caching_ctl->work.func = caching_thread; |
| 491 | |
| 492 | spin_lock(&cache->lock); |
| 493 | /* |
| 494 | * This should be a rare occasion, but this could happen I think in the |
| 495 | * case where one thread starts to load the space cache info, and then |
| 496 | * some other thread starts a transaction commit which tries to do an |
| 497 | * allocation while the other thread is still loading the space cache |
| 498 | * info. The previous loop should have kept us from choosing this block |
| 499 | * group, but if we've moved to the state where we will wait on caching |
| 500 | * block groups we need to first check if we're doing a fast load here, |
| 501 | * so we can wait for it to finish, otherwise we could end up allocating |
| 502 | * from a block group who's cache gets evicted for one reason or |
| 503 | * another. |
| 504 | */ |
| 505 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 506 | struct btrfs_caching_control *ctl; |
| 507 | |
| 508 | ctl = cache->caching_ctl; |
| 509 | atomic_inc(&ctl->count); |
| 510 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 511 | spin_unlock(&cache->lock); |
| 512 | |
| 513 | schedule(); |
| 514 | |
| 515 | finish_wait(&ctl->wait, &wait); |
| 516 | put_caching_control(ctl); |
| 517 | spin_lock(&cache->lock); |
| 518 | } |
| 519 | |
| 520 | if (cache->cached != BTRFS_CACHE_NO) { |
| 521 | spin_unlock(&cache->lock); |
| 522 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 523 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 524 | } |
| 525 | WARN_ON(cache->caching_ctl); |
| 526 | cache->caching_ctl = caching_ctl; |
| 527 | cache->cached = BTRFS_CACHE_FAST; |
| 528 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 529 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 530 | /* |
| 531 | * We can't do the read from on-disk cache during a commit since we need |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 532 | * to have the normal tree locking. Also if we are currently trying to |
| 533 | * allocate blocks for the tree root we can't do the fast caching since |
| 534 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 535 | */ |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 536 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 537 | ret = load_free_space_cache(fs_info, cache); |
| 538 | |
| 539 | spin_lock(&cache->lock); |
| 540 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 541 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 542 | cache->cached = BTRFS_CACHE_FINISHED; |
| 543 | cache->last_byte_to_unpin = (u64)-1; |
| 544 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 545 | if (load_cache_only) { |
| 546 | cache->caching_ctl = NULL; |
| 547 | cache->cached = BTRFS_CACHE_NO; |
| 548 | } else { |
| 549 | cache->cached = BTRFS_CACHE_STARTED; |
| 550 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 551 | } |
| 552 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 553 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 554 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 555 | put_caching_control(caching_ctl); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 556 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 557 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 558 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 559 | } else { |
| 560 | /* |
| 561 | * We are not going to do the fast caching, set cached to the |
| 562 | * appropriate value and wakeup any waiters. |
| 563 | */ |
| 564 | spin_lock(&cache->lock); |
| 565 | if (load_cache_only) { |
| 566 | cache->caching_ctl = NULL; |
| 567 | cache->cached = BTRFS_CACHE_NO; |
| 568 | } else { |
| 569 | cache->cached = BTRFS_CACHE_STARTED; |
| 570 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 571 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 572 | wake_up(&caching_ctl->wait); |
| 573 | } |
| 574 | |
| 575 | if (load_cache_only) { |
| 576 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 577 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 578 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 579 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 580 | down_write(&fs_info->extent_commit_sem); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 581 | atomic_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 582 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 583 | up_write(&fs_info->extent_commit_sem); |
| 584 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 585 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 586 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 587 | btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 588 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 589 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 592 | /* |
| 593 | * return the block group that starts at or after bytenr |
| 594 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 595 | static struct btrfs_block_group_cache * |
| 596 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 597 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 598 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 599 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 600 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 601 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 602 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 605 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 606 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 607 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 608 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 609 | struct btrfs_fs_info *info, |
| 610 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 611 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 612 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 613 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 614 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 615 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 616 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 617 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 618 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 619 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 620 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 621 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 622 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 623 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 624 | |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 625 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 626 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 627 | rcu_read_lock(); |
| 628 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 629 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 630 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 631 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 632 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 633 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 634 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 635 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 636 | } |
| 637 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 638 | /* |
| 639 | * after adding space to the filesystem, we need to clear the full flags |
| 640 | * on all the space infos. |
| 641 | */ |
| 642 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 643 | { |
| 644 | struct list_head *head = &info->space_info; |
| 645 | struct btrfs_space_info *found; |
| 646 | |
| 647 | rcu_read_lock(); |
| 648 | list_for_each_entry_rcu(found, head, list) |
| 649 | found->full = 0; |
| 650 | rcu_read_unlock(); |
| 651 | } |
| 652 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 653 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 654 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 655 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 656 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 657 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 658 | u64 last = max(search_hint, search_start); |
| 659 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 660 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 661 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 662 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 663 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 664 | while (1) { |
| 665 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 666 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 667 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 668 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 669 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 670 | last = cache->key.objectid + cache->key.offset; |
| 671 | used = btrfs_block_group_used(&cache->item); |
| 672 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 673 | if ((full_search || !cache->ro) && |
| 674 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 675 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 676 | div_factor(cache->key.offset, factor)) { |
| 677 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 678 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 679 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 680 | goto found; |
| 681 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 682 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 683 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 684 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 685 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 686 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 687 | if (!wrapped) { |
| 688 | last = search_start; |
| 689 | wrapped = 1; |
| 690 | goto again; |
| 691 | } |
| 692 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 693 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 694 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 695 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 696 | goto again; |
| 697 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 698 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 699 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 700 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 701 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 702 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 703 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 704 | { |
| 705 | int ret; |
| 706 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 707 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 708 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 709 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 710 | if (!path) |
| 711 | return -ENOMEM; |
| 712 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 713 | key.objectid = start; |
| 714 | key.offset = len; |
| 715 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 716 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 717 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 718 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 719 | return ret; |
| 720 | } |
| 721 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 722 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 723 | * helper function to lookup reference count and flags of extent. |
| 724 | * |
| 725 | * the head node for delayed ref is used to store the sum of all the |
| 726 | * reference count modifications queued up in the rbtree. the head |
| 727 | * node may also store the extent flags to set. This way you can check |
| 728 | * to see what the reference count and extent flags would be if all of |
| 729 | * the delayed refs are not processed. |
| 730 | */ |
| 731 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 732 | struct btrfs_root *root, u64 bytenr, |
| 733 | u64 num_bytes, u64 *refs, u64 *flags) |
| 734 | { |
| 735 | struct btrfs_delayed_ref_head *head; |
| 736 | struct btrfs_delayed_ref_root *delayed_refs; |
| 737 | struct btrfs_path *path; |
| 738 | struct btrfs_extent_item *ei; |
| 739 | struct extent_buffer *leaf; |
| 740 | struct btrfs_key key; |
| 741 | u32 item_size; |
| 742 | u64 num_refs; |
| 743 | u64 extent_flags; |
| 744 | int ret; |
| 745 | |
| 746 | path = btrfs_alloc_path(); |
| 747 | if (!path) |
| 748 | return -ENOMEM; |
| 749 | |
| 750 | key.objectid = bytenr; |
| 751 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 752 | key.offset = num_bytes; |
| 753 | if (!trans) { |
| 754 | path->skip_locking = 1; |
| 755 | path->search_commit_root = 1; |
| 756 | } |
| 757 | again: |
| 758 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 759 | &key, path, 0, 0); |
| 760 | if (ret < 0) |
| 761 | goto out_free; |
| 762 | |
| 763 | if (ret == 0) { |
| 764 | leaf = path->nodes[0]; |
| 765 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 766 | if (item_size >= sizeof(*ei)) { |
| 767 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 768 | struct btrfs_extent_item); |
| 769 | num_refs = btrfs_extent_refs(leaf, ei); |
| 770 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 771 | } else { |
| 772 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 773 | struct btrfs_extent_item_v0 *ei0; |
| 774 | BUG_ON(item_size != sizeof(*ei0)); |
| 775 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 776 | struct btrfs_extent_item_v0); |
| 777 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 778 | /* FIXME: this isn't correct for data */ |
| 779 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 780 | #else |
| 781 | BUG(); |
| 782 | #endif |
| 783 | } |
| 784 | BUG_ON(num_refs == 0); |
| 785 | } else { |
| 786 | num_refs = 0; |
| 787 | extent_flags = 0; |
| 788 | ret = 0; |
| 789 | } |
| 790 | |
| 791 | if (!trans) |
| 792 | goto out; |
| 793 | |
| 794 | delayed_refs = &trans->transaction->delayed_refs; |
| 795 | spin_lock(&delayed_refs->lock); |
| 796 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 797 | if (head) { |
| 798 | if (!mutex_trylock(&head->mutex)) { |
| 799 | atomic_inc(&head->node.refs); |
| 800 | spin_unlock(&delayed_refs->lock); |
| 801 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 802 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 803 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 804 | /* |
| 805 | * Mutex was contended, block until it's released and try |
| 806 | * again |
| 807 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 808 | mutex_lock(&head->mutex); |
| 809 | mutex_unlock(&head->mutex); |
| 810 | btrfs_put_delayed_ref(&head->node); |
| 811 | goto again; |
| 812 | } |
| 813 | if (head->extent_op && head->extent_op->update_flags) |
| 814 | extent_flags |= head->extent_op->flags_to_set; |
| 815 | else |
| 816 | BUG_ON(num_refs == 0); |
| 817 | |
| 818 | num_refs += head->node.ref_mod; |
| 819 | mutex_unlock(&head->mutex); |
| 820 | } |
| 821 | spin_unlock(&delayed_refs->lock); |
| 822 | out: |
| 823 | WARN_ON(num_refs == 0); |
| 824 | if (refs) |
| 825 | *refs = num_refs; |
| 826 | if (flags) |
| 827 | *flags = extent_flags; |
| 828 | out_free: |
| 829 | btrfs_free_path(path); |
| 830 | return ret; |
| 831 | } |
| 832 | |
| 833 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 834 | * Back reference rules. Back refs have three main goals: |
| 835 | * |
| 836 | * 1) differentiate between all holders of references to an extent so that |
| 837 | * when a reference is dropped we can make sure it was a valid reference |
| 838 | * before freeing the extent. |
| 839 | * |
| 840 | * 2) Provide enough information to quickly find the holders of an extent |
| 841 | * if we notice a given block is corrupted or bad. |
| 842 | * |
| 843 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 844 | * maintenance. This is actually the same as #2, but with a slightly |
| 845 | * different use case. |
| 846 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 847 | * There are two kinds of back refs. The implicit back refs is optimized |
| 848 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 849 | * back refs of this kind provide information about the block's owner tree |
| 850 | * and the pointer's key. These information allow us to find the block by |
| 851 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 852 | * referenced by their owner trees. The location of tree block is recorded |
| 853 | * in the back refs. Actually the full back refs is generic, and can be |
| 854 | * used in all cases the implicit back refs is used. The major shortcoming |
| 855 | * of the full back refs is its overhead. Every time a tree block gets |
| 856 | * COWed, we have to update back refs entry for all pointers in it. |
| 857 | * |
| 858 | * For a newly allocated tree block, we use implicit back refs for |
| 859 | * pointers in it. This means most tree related operations only involve |
| 860 | * implicit back refs. For a tree block created in old transaction, the |
| 861 | * only way to drop a reference to it is COW it. So we can detect the |
| 862 | * event that tree block loses its owner tree's reference and do the |
| 863 | * back refs conversion. |
| 864 | * |
| 865 | * When a tree block is COW'd through a tree, there are four cases: |
| 866 | * |
| 867 | * The reference count of the block is one and the tree is the block's |
| 868 | * owner tree. Nothing to do in this case. |
| 869 | * |
| 870 | * The reference count of the block is one and the tree is not the |
| 871 | * block's owner tree. In this case, full back refs is used for pointers |
| 872 | * in the block. Remove these full back refs, add implicit back refs for |
| 873 | * every pointers in the new block. |
| 874 | * |
| 875 | * The reference count of the block is greater than one and the tree is |
| 876 | * the block's owner tree. In this case, implicit back refs is used for |
| 877 | * pointers in the block. Add full back refs for every pointers in the |
| 878 | * block, increase lower level extents' reference counts. The original |
| 879 | * implicit back refs are entailed to the new block. |
| 880 | * |
| 881 | * The reference count of the block is greater than one and the tree is |
| 882 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 883 | * the new block, increase lower level extents' reference count. |
| 884 | * |
| 885 | * Back Reference Key composing: |
| 886 | * |
| 887 | * The key objectid corresponds to the first byte in the extent, |
| 888 | * The key type is used to differentiate between types of back refs. |
| 889 | * There are different meanings of the key offset for different types |
| 890 | * of back refs. |
| 891 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 892 | * File extents can be referenced by: |
| 893 | * |
| 894 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 895 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 896 | * - different offsets inside a file (bookend extents in file.c) |
| 897 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 898 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 899 | * |
| 900 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 901 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 902 | * - original offset in the file |
| 903 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 904 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 905 | * The key offset for the implicit back refs is hash of the first |
| 906 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 907 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 908 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 909 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 910 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 911 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 912 | * The key offset for the implicit back refs is the first byte of |
| 913 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 914 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 915 | * When a file extent is allocated, The implicit back refs is used. |
| 916 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 917 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 919 | * |
| 920 | * When a file extent is removed file truncation, we find the |
| 921 | * corresponding implicit back refs and check the following fields: |
| 922 | * |
| 923 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 924 | * |
| 925 | * Btree extents can be referenced by: |
| 926 | * |
| 927 | * - Different subvolumes |
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 | * Both the implicit back refs and the full back refs for tree blocks |
| 930 | * only consist of key. The key offset for the implicit back refs is |
| 931 | * objectid of block's owner tree. The key offset for the full back refs |
| 932 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 933 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 934 | * When implicit back refs is used, information about the lowest key and |
| 935 | * level of the tree block are required. These information are stored in |
| 936 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 937 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 938 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 939 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 940 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 941 | struct btrfs_root *root, |
| 942 | struct btrfs_path *path, |
| 943 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 944 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 945 | struct btrfs_extent_item *item; |
| 946 | struct btrfs_extent_item_v0 *ei0; |
| 947 | struct btrfs_extent_ref_v0 *ref0; |
| 948 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 949 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 950 | struct btrfs_key key; |
| 951 | struct btrfs_key found_key; |
| 952 | u32 new_size = sizeof(*item); |
| 953 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 954 | int ret; |
| 955 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 956 | leaf = path->nodes[0]; |
| 957 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 958 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 959 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 960 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 961 | struct btrfs_extent_item_v0); |
| 962 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 963 | |
| 964 | if (owner == (u64)-1) { |
| 965 | while (1) { |
| 966 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 967 | ret = btrfs_next_leaf(root, path); |
| 968 | if (ret < 0) |
| 969 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 970 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 971 | leaf = path->nodes[0]; |
| 972 | } |
| 973 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 974 | path->slots[0]); |
| 975 | BUG_ON(key.objectid != found_key.objectid); |
| 976 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 977 | path->slots[0]++; |
| 978 | continue; |
| 979 | } |
| 980 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 981 | struct btrfs_extent_ref_v0); |
| 982 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 983 | break; |
| 984 | } |
| 985 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 986 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 987 | |
| 988 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 989 | new_size += sizeof(*bi); |
| 990 | |
| 991 | new_size -= sizeof(*ei0); |
| 992 | ret = btrfs_search_slot(trans, root, &key, path, |
| 993 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 994 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 995 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 996 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 997 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 998 | btrfs_extend_item(trans, root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 999 | |
| 1000 | leaf = path->nodes[0]; |
| 1001 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1002 | btrfs_set_extent_refs(leaf, item, refs); |
| 1003 | /* FIXME: get real generation */ |
| 1004 | btrfs_set_extent_generation(leaf, item, 0); |
| 1005 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1006 | btrfs_set_extent_flags(leaf, item, |
| 1007 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1008 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1009 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1010 | /* FIXME: get first key of the block */ |
| 1011 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 1012 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1013 | } else { |
| 1014 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1015 | } |
| 1016 | btrfs_mark_buffer_dirty(leaf); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | #endif |
| 1020 | |
| 1021 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1022 | { |
| 1023 | u32 high_crc = ~(u32)0; |
| 1024 | u32 low_crc = ~(u32)0; |
| 1025 | __le64 lenum; |
| 1026 | |
| 1027 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1028 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1029 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1030 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1031 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1032 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1033 | |
| 1034 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1035 | } |
| 1036 | |
| 1037 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1038 | struct btrfs_extent_data_ref *ref) |
| 1039 | { |
| 1040 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1041 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1042 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1043 | } |
| 1044 | |
| 1045 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1046 | struct btrfs_extent_data_ref *ref, |
| 1047 | u64 root_objectid, u64 owner, u64 offset) |
| 1048 | { |
| 1049 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1050 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1051 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1052 | return 0; |
| 1053 | return 1; |
| 1054 | } |
| 1055 | |
| 1056 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1057 | struct btrfs_root *root, |
| 1058 | struct btrfs_path *path, |
| 1059 | u64 bytenr, u64 parent, |
| 1060 | u64 root_objectid, |
| 1061 | u64 owner, u64 offset) |
| 1062 | { |
| 1063 | struct btrfs_key key; |
| 1064 | struct btrfs_extent_data_ref *ref; |
| 1065 | struct extent_buffer *leaf; |
| 1066 | u32 nritems; |
| 1067 | int ret; |
| 1068 | int recow; |
| 1069 | int err = -ENOENT; |
| 1070 | |
| 1071 | key.objectid = bytenr; |
| 1072 | if (parent) { |
| 1073 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1074 | key.offset = parent; |
| 1075 | } else { |
| 1076 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1077 | key.offset = hash_extent_data_ref(root_objectid, |
| 1078 | owner, offset); |
| 1079 | } |
| 1080 | again: |
| 1081 | recow = 0; |
| 1082 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1083 | if (ret < 0) { |
| 1084 | err = ret; |
| 1085 | goto fail; |
| 1086 | } |
| 1087 | |
| 1088 | if (parent) { |
| 1089 | if (!ret) |
| 1090 | return 0; |
| 1091 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1092 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1093 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1094 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1095 | if (ret < 0) { |
| 1096 | err = ret; |
| 1097 | goto fail; |
| 1098 | } |
| 1099 | if (!ret) |
| 1100 | return 0; |
| 1101 | #endif |
| 1102 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1106 | nritems = btrfs_header_nritems(leaf); |
| 1107 | while (1) { |
| 1108 | if (path->slots[0] >= nritems) { |
| 1109 | ret = btrfs_next_leaf(root, path); |
| 1110 | if (ret < 0) |
| 1111 | err = ret; |
| 1112 | if (ret) |
| 1113 | goto fail; |
| 1114 | |
| 1115 | leaf = path->nodes[0]; |
| 1116 | nritems = btrfs_header_nritems(leaf); |
| 1117 | recow = 1; |
| 1118 | } |
| 1119 | |
| 1120 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1121 | if (key.objectid != bytenr || |
| 1122 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1123 | goto fail; |
| 1124 | |
| 1125 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1126 | struct btrfs_extent_data_ref); |
| 1127 | |
| 1128 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1129 | owner, offset)) { |
| 1130 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1131 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1132 | goto again; |
| 1133 | } |
| 1134 | err = 0; |
| 1135 | break; |
| 1136 | } |
| 1137 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1138 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1139 | fail: |
| 1140 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1143 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1144 | struct btrfs_root *root, |
| 1145 | struct btrfs_path *path, |
| 1146 | u64 bytenr, u64 parent, |
| 1147 | u64 root_objectid, u64 owner, |
| 1148 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1149 | { |
| 1150 | struct btrfs_key key; |
| 1151 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1152 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1153 | u32 num_refs; |
| 1154 | int ret; |
| 1155 | |
| 1156 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1157 | if (parent) { |
| 1158 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1159 | key.offset = parent; |
| 1160 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1161 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1162 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1163 | key.offset = hash_extent_data_ref(root_objectid, |
| 1164 | owner, offset); |
| 1165 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1166 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1167 | |
| 1168 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1169 | if (ret && ret != -EEXIST) |
| 1170 | goto fail; |
| 1171 | |
| 1172 | leaf = path->nodes[0]; |
| 1173 | if (parent) { |
| 1174 | struct btrfs_shared_data_ref *ref; |
| 1175 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1176 | struct btrfs_shared_data_ref); |
| 1177 | if (ret == 0) { |
| 1178 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1179 | } else { |
| 1180 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1181 | num_refs += refs_to_add; |
| 1182 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1183 | } |
| 1184 | } else { |
| 1185 | struct btrfs_extent_data_ref *ref; |
| 1186 | while (ret == -EEXIST) { |
| 1187 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1188 | struct btrfs_extent_data_ref); |
| 1189 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1190 | owner, offset)) |
| 1191 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1192 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1193 | key.offset++; |
| 1194 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1195 | size); |
| 1196 | if (ret && ret != -EEXIST) |
| 1197 | goto fail; |
| 1198 | |
| 1199 | leaf = path->nodes[0]; |
| 1200 | } |
| 1201 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1202 | struct btrfs_extent_data_ref); |
| 1203 | if (ret == 0) { |
| 1204 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1205 | root_objectid); |
| 1206 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1207 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1208 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1209 | } else { |
| 1210 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1211 | num_refs += refs_to_add; |
| 1212 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1213 | } |
| 1214 | } |
| 1215 | btrfs_mark_buffer_dirty(leaf); |
| 1216 | ret = 0; |
| 1217 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1218 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1219 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1220 | } |
| 1221 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1222 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1223 | struct btrfs_root *root, |
| 1224 | struct btrfs_path *path, |
| 1225 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1226 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1227 | struct btrfs_key key; |
| 1228 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1229 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1230 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1231 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1232 | int ret = 0; |
| 1233 | |
| 1234 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1235 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1236 | |
| 1237 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1238 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1239 | struct btrfs_extent_data_ref); |
| 1240 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1241 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1242 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1243 | struct btrfs_shared_data_ref); |
| 1244 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1245 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1246 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1247 | struct btrfs_extent_ref_v0 *ref0; |
| 1248 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1249 | struct btrfs_extent_ref_v0); |
| 1250 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1251 | #endif |
| 1252 | } else { |
| 1253 | BUG(); |
| 1254 | } |
| 1255 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1256 | BUG_ON(num_refs < refs_to_drop); |
| 1257 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1258 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1259 | if (num_refs == 0) { |
| 1260 | ret = btrfs_del_item(trans, root, path); |
| 1261 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1262 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1263 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1264 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1265 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1266 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1267 | else { |
| 1268 | struct btrfs_extent_ref_v0 *ref0; |
| 1269 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1270 | struct btrfs_extent_ref_v0); |
| 1271 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1272 | } |
| 1273 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1274 | btrfs_mark_buffer_dirty(leaf); |
| 1275 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1276 | return ret; |
| 1277 | } |
| 1278 | |
| 1279 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1280 | struct btrfs_path *path, |
| 1281 | struct btrfs_extent_inline_ref *iref) |
| 1282 | { |
| 1283 | struct btrfs_key key; |
| 1284 | struct extent_buffer *leaf; |
| 1285 | struct btrfs_extent_data_ref *ref1; |
| 1286 | struct btrfs_shared_data_ref *ref2; |
| 1287 | u32 num_refs = 0; |
| 1288 | |
| 1289 | leaf = path->nodes[0]; |
| 1290 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1291 | if (iref) { |
| 1292 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1293 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1294 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1295 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1296 | } else { |
| 1297 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1298 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1299 | } |
| 1300 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1301 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1302 | struct btrfs_extent_data_ref); |
| 1303 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1304 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1305 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1306 | struct btrfs_shared_data_ref); |
| 1307 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1308 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1309 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1310 | struct btrfs_extent_ref_v0 *ref0; |
| 1311 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1312 | struct btrfs_extent_ref_v0); |
| 1313 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1314 | #endif |
| 1315 | } else { |
| 1316 | WARN_ON(1); |
| 1317 | } |
| 1318 | return num_refs; |
| 1319 | } |
| 1320 | |
| 1321 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1322 | struct btrfs_root *root, |
| 1323 | struct btrfs_path *path, |
| 1324 | u64 bytenr, u64 parent, |
| 1325 | u64 root_objectid) |
| 1326 | { |
| 1327 | struct btrfs_key key; |
| 1328 | int ret; |
| 1329 | |
| 1330 | key.objectid = bytenr; |
| 1331 | if (parent) { |
| 1332 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1333 | key.offset = parent; |
| 1334 | } else { |
| 1335 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1336 | key.offset = root_objectid; |
| 1337 | } |
| 1338 | |
| 1339 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1340 | if (ret > 0) |
| 1341 | ret = -ENOENT; |
| 1342 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1343 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1344 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1345 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1346 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1347 | if (ret > 0) |
| 1348 | ret = -ENOENT; |
| 1349 | } |
| 1350 | #endif |
| 1351 | return ret; |
| 1352 | } |
| 1353 | |
| 1354 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1355 | struct btrfs_root *root, |
| 1356 | struct btrfs_path *path, |
| 1357 | u64 bytenr, u64 parent, |
| 1358 | u64 root_objectid) |
| 1359 | { |
| 1360 | struct btrfs_key key; |
| 1361 | int ret; |
| 1362 | |
| 1363 | key.objectid = bytenr; |
| 1364 | if (parent) { |
| 1365 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1366 | key.offset = parent; |
| 1367 | } else { |
| 1368 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1369 | key.offset = root_objectid; |
| 1370 | } |
| 1371 | |
| 1372 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1373 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1374 | return ret; |
| 1375 | } |
| 1376 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1377 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1378 | { |
| 1379 | int type; |
| 1380 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1381 | if (parent > 0) |
| 1382 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1383 | else |
| 1384 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1385 | } else { |
| 1386 | if (parent > 0) |
| 1387 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1388 | else |
| 1389 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1390 | } |
| 1391 | return type; |
| 1392 | } |
| 1393 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1394 | static int find_next_key(struct btrfs_path *path, int level, |
| 1395 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1396 | |
| 1397 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1398 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1399 | if (!path->nodes[level]) |
| 1400 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1401 | if (path->slots[level] + 1 >= |
| 1402 | btrfs_header_nritems(path->nodes[level])) |
| 1403 | continue; |
| 1404 | if (level == 0) |
| 1405 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1406 | path->slots[level] + 1); |
| 1407 | else |
| 1408 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1409 | path->slots[level] + 1); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1417 | * to the address of inline back ref, and 0 is returned. |
| 1418 | * |
| 1419 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1420 | * should be inserted, and -ENOENT is returned. |
| 1421 | * |
| 1422 | * if insert is true and there are too many inline back refs, the path |
| 1423 | * points to the extent item, and -EAGAIN is returned. |
| 1424 | * |
| 1425 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1426 | * items in the tree are ordered. |
| 1427 | */ |
| 1428 | static noinline_for_stack |
| 1429 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1430 | struct btrfs_root *root, |
| 1431 | struct btrfs_path *path, |
| 1432 | struct btrfs_extent_inline_ref **ref_ret, |
| 1433 | u64 bytenr, u64 num_bytes, |
| 1434 | u64 parent, u64 root_objectid, |
| 1435 | u64 owner, u64 offset, int insert) |
| 1436 | { |
| 1437 | struct btrfs_key key; |
| 1438 | struct extent_buffer *leaf; |
| 1439 | struct btrfs_extent_item *ei; |
| 1440 | struct btrfs_extent_inline_ref *iref; |
| 1441 | u64 flags; |
| 1442 | u64 item_size; |
| 1443 | unsigned long ptr; |
| 1444 | unsigned long end; |
| 1445 | int extra_size; |
| 1446 | int type; |
| 1447 | int want; |
| 1448 | int ret; |
| 1449 | int err = 0; |
| 1450 | |
| 1451 | key.objectid = bytenr; |
| 1452 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1453 | key.offset = num_bytes; |
| 1454 | |
| 1455 | want = extent_ref_type(parent, owner); |
| 1456 | if (insert) { |
| 1457 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1458 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1459 | } else |
| 1460 | extra_size = -1; |
| 1461 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1462 | if (ret < 0) { |
| 1463 | err = ret; |
| 1464 | goto out; |
| 1465 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1466 | if (ret && !insert) { |
| 1467 | err = -ENOENT; |
| 1468 | goto out; |
| 1469 | } |
| 1470 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1471 | |
| 1472 | leaf = path->nodes[0]; |
| 1473 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1474 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1475 | if (item_size < sizeof(*ei)) { |
| 1476 | if (!insert) { |
| 1477 | err = -ENOENT; |
| 1478 | goto out; |
| 1479 | } |
| 1480 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1481 | extra_size); |
| 1482 | if (ret < 0) { |
| 1483 | err = ret; |
| 1484 | goto out; |
| 1485 | } |
| 1486 | leaf = path->nodes[0]; |
| 1487 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1488 | } |
| 1489 | #endif |
| 1490 | BUG_ON(item_size < sizeof(*ei)); |
| 1491 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1492 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1493 | flags = btrfs_extent_flags(leaf, ei); |
| 1494 | |
| 1495 | ptr = (unsigned long)(ei + 1); |
| 1496 | end = (unsigned long)ei + item_size; |
| 1497 | |
| 1498 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1499 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1500 | BUG_ON(ptr > end); |
| 1501 | } else { |
| 1502 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1503 | } |
| 1504 | |
| 1505 | err = -ENOENT; |
| 1506 | while (1) { |
| 1507 | if (ptr >= end) { |
| 1508 | WARN_ON(ptr > end); |
| 1509 | break; |
| 1510 | } |
| 1511 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1512 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1513 | if (want < type) |
| 1514 | break; |
| 1515 | if (want > type) { |
| 1516 | ptr += btrfs_extent_inline_ref_size(type); |
| 1517 | continue; |
| 1518 | } |
| 1519 | |
| 1520 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1521 | struct btrfs_extent_data_ref *dref; |
| 1522 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1523 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1524 | owner, offset)) { |
| 1525 | err = 0; |
| 1526 | break; |
| 1527 | } |
| 1528 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1529 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1530 | break; |
| 1531 | } else { |
| 1532 | u64 ref_offset; |
| 1533 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1534 | if (parent > 0) { |
| 1535 | if (parent == ref_offset) { |
| 1536 | err = 0; |
| 1537 | break; |
| 1538 | } |
| 1539 | if (ref_offset < parent) |
| 1540 | break; |
| 1541 | } else { |
| 1542 | if (root_objectid == ref_offset) { |
| 1543 | err = 0; |
| 1544 | break; |
| 1545 | } |
| 1546 | if (ref_offset < root_objectid) |
| 1547 | break; |
| 1548 | } |
| 1549 | } |
| 1550 | ptr += btrfs_extent_inline_ref_size(type); |
| 1551 | } |
| 1552 | if (err == -ENOENT && insert) { |
| 1553 | if (item_size + extra_size >= |
| 1554 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1555 | err = -EAGAIN; |
| 1556 | goto out; |
| 1557 | } |
| 1558 | /* |
| 1559 | * To add new inline back ref, we have to make sure |
| 1560 | * there is no corresponding back ref item. |
| 1561 | * For simplicity, we just do not add new inline back |
| 1562 | * ref if there is any kind of item for this block |
| 1563 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1564 | if (find_next_key(path, 0, &key) == 0 && |
| 1565 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1566 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1567 | err = -EAGAIN; |
| 1568 | goto out; |
| 1569 | } |
| 1570 | } |
| 1571 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1572 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1573 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1574 | path->keep_locks = 0; |
| 1575 | btrfs_unlock_up_safe(path, 1); |
| 1576 | } |
| 1577 | return err; |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * helper to add new inline back ref |
| 1582 | */ |
| 1583 | static noinline_for_stack |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1584 | void setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1585 | struct btrfs_root *root, |
| 1586 | struct btrfs_path *path, |
| 1587 | struct btrfs_extent_inline_ref *iref, |
| 1588 | u64 parent, u64 root_objectid, |
| 1589 | u64 owner, u64 offset, int refs_to_add, |
| 1590 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1591 | { |
| 1592 | struct extent_buffer *leaf; |
| 1593 | struct btrfs_extent_item *ei; |
| 1594 | unsigned long ptr; |
| 1595 | unsigned long end; |
| 1596 | unsigned long item_offset; |
| 1597 | u64 refs; |
| 1598 | int size; |
| 1599 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1600 | |
| 1601 | leaf = path->nodes[0]; |
| 1602 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1603 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1604 | |
| 1605 | type = extent_ref_type(parent, owner); |
| 1606 | size = btrfs_extent_inline_ref_size(type); |
| 1607 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1608 | btrfs_extend_item(trans, root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1609 | |
| 1610 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1611 | refs = btrfs_extent_refs(leaf, ei); |
| 1612 | refs += refs_to_add; |
| 1613 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1614 | if (extent_op) |
| 1615 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1616 | |
| 1617 | ptr = (unsigned long)ei + item_offset; |
| 1618 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1619 | if (ptr < end - size) |
| 1620 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1621 | end - size - ptr); |
| 1622 | |
| 1623 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1624 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1625 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1626 | struct btrfs_extent_data_ref *dref; |
| 1627 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1628 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1629 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1630 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1631 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1632 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1633 | struct btrfs_shared_data_ref *sref; |
| 1634 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1635 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1636 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1637 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1638 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1639 | } else { |
| 1640 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1641 | } |
| 1642 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1646 | struct btrfs_root *root, |
| 1647 | struct btrfs_path *path, |
| 1648 | struct btrfs_extent_inline_ref **ref_ret, |
| 1649 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1650 | u64 root_objectid, u64 owner, u64 offset) |
| 1651 | { |
| 1652 | int ret; |
| 1653 | |
| 1654 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1655 | bytenr, num_bytes, parent, |
| 1656 | root_objectid, owner, offset, 0); |
| 1657 | if (ret != -ENOENT) |
| 1658 | return ret; |
| 1659 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1660 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1661 | *ref_ret = NULL; |
| 1662 | |
| 1663 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1664 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1665 | root_objectid); |
| 1666 | } else { |
| 1667 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1668 | root_objectid, owner, offset); |
| 1669 | } |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * helper to update/remove inline back ref |
| 1675 | */ |
| 1676 | static noinline_for_stack |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1677 | void update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1678 | struct btrfs_root *root, |
| 1679 | struct btrfs_path *path, |
| 1680 | struct btrfs_extent_inline_ref *iref, |
| 1681 | int refs_to_mod, |
| 1682 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1683 | { |
| 1684 | struct extent_buffer *leaf; |
| 1685 | struct btrfs_extent_item *ei; |
| 1686 | struct btrfs_extent_data_ref *dref = NULL; |
| 1687 | struct btrfs_shared_data_ref *sref = NULL; |
| 1688 | unsigned long ptr; |
| 1689 | unsigned long end; |
| 1690 | u32 item_size; |
| 1691 | int size; |
| 1692 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1693 | u64 refs; |
| 1694 | |
| 1695 | leaf = path->nodes[0]; |
| 1696 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1697 | refs = btrfs_extent_refs(leaf, ei); |
| 1698 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1699 | refs += refs_to_mod; |
| 1700 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1701 | if (extent_op) |
| 1702 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1703 | |
| 1704 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1705 | |
| 1706 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1707 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1708 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1709 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1710 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1711 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1712 | } else { |
| 1713 | refs = 1; |
| 1714 | BUG_ON(refs_to_mod != -1); |
| 1715 | } |
| 1716 | |
| 1717 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1718 | refs += refs_to_mod; |
| 1719 | |
| 1720 | if (refs > 0) { |
| 1721 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1722 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1723 | else |
| 1724 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1725 | } else { |
| 1726 | size = btrfs_extent_inline_ref_size(type); |
| 1727 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1728 | ptr = (unsigned long)iref; |
| 1729 | end = (unsigned long)ei + item_size; |
| 1730 | if (ptr + size < end) |
| 1731 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1732 | end - ptr - size); |
| 1733 | item_size -= size; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1734 | btrfs_truncate_item(trans, root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1735 | } |
| 1736 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | static noinline_for_stack |
| 1740 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1741 | struct btrfs_root *root, |
| 1742 | struct btrfs_path *path, |
| 1743 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1744 | u64 root_objectid, u64 owner, |
| 1745 | u64 offset, int refs_to_add, |
| 1746 | struct btrfs_delayed_extent_op *extent_op) |
| 1747 | { |
| 1748 | struct btrfs_extent_inline_ref *iref; |
| 1749 | int ret; |
| 1750 | |
| 1751 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1752 | bytenr, num_bytes, parent, |
| 1753 | root_objectid, owner, offset, 1); |
| 1754 | if (ret == 0) { |
| 1755 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1756 | update_inline_extent_backref(trans, root, path, iref, |
| 1757 | refs_to_add, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1758 | } else if (ret == -ENOENT) { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1759 | setup_inline_extent_backref(trans, root, path, iref, parent, |
| 1760 | root_objectid, owner, offset, |
| 1761 | refs_to_add, extent_op); |
| 1762 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1763 | } |
| 1764 | return ret; |
| 1765 | } |
| 1766 | |
| 1767 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1768 | struct btrfs_root *root, |
| 1769 | struct btrfs_path *path, |
| 1770 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1771 | u64 owner, u64 offset, int refs_to_add) |
| 1772 | { |
| 1773 | int ret; |
| 1774 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1775 | BUG_ON(refs_to_add != 1); |
| 1776 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1777 | parent, root_objectid); |
| 1778 | } else { |
| 1779 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1780 | parent, root_objectid, |
| 1781 | owner, offset, refs_to_add); |
| 1782 | } |
| 1783 | return ret; |
| 1784 | } |
| 1785 | |
| 1786 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1787 | struct btrfs_root *root, |
| 1788 | struct btrfs_path *path, |
| 1789 | struct btrfs_extent_inline_ref *iref, |
| 1790 | int refs_to_drop, int is_data) |
| 1791 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1792 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1793 | |
| 1794 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1795 | if (iref) { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1796 | update_inline_extent_backref(trans, root, path, iref, |
| 1797 | -refs_to_drop, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1798 | } else if (is_data) { |
| 1799 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1800 | } else { |
| 1801 | ret = btrfs_del_item(trans, root, path); |
| 1802 | } |
| 1803 | return ret; |
| 1804 | } |
| 1805 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1806 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1807 | u64 start, u64 len) |
| 1808 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1809 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1810 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1811 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1812 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1813 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1814 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1815 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1816 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1817 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1818 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1819 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1820 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1821 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1822 | bytenr, &num_bytes, &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1823 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1824 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1825 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1826 | int i; |
| 1827 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1828 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1829 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1830 | if (!stripe->dev->can_discard) |
| 1831 | continue; |
| 1832 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1833 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1834 | stripe->physical, |
| 1835 | stripe->length); |
| 1836 | if (!ret) |
| 1837 | discarded_bytes += stripe->length; |
| 1838 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1839 | 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] | 1840 | |
| 1841 | /* |
| 1842 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1843 | * just ignore the return value so we don't screw up |
| 1844 | * people calling discard_extent. |
| 1845 | */ |
| 1846 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1847 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1848 | kfree(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1849 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (actual_bytes) |
| 1852 | *actual_bytes = discarded_bytes; |
| 1853 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1854 | |
| 1855 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1856 | } |
| 1857 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1858 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1859 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1860 | struct btrfs_root *root, |
| 1861 | u64 bytenr, u64 num_bytes, u64 parent, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1862 | u64 root_objectid, u64 owner, u64 offset, int for_cow) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1863 | { |
| 1864 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1865 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1866 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1867 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1868 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1869 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1870 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1871 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 1872 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1873 | parent, root_objectid, (int)owner, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1874 | BTRFS_ADD_DELAYED_REF, NULL, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1875 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1876 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 1877 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1878 | parent, root_objectid, owner, offset, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1879 | BTRFS_ADD_DELAYED_REF, NULL, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1880 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1881 | return ret; |
| 1882 | } |
| 1883 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1884 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1885 | struct btrfs_root *root, |
| 1886 | u64 bytenr, u64 num_bytes, |
| 1887 | u64 parent, u64 root_objectid, |
| 1888 | u64 owner, u64 offset, int refs_to_add, |
| 1889 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1890 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1891 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1892 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1893 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1894 | u64 refs; |
| 1895 | int ret; |
| 1896 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1897 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1898 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1899 | if (!path) |
| 1900 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1901 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1902 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1903 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1904 | /* this will setup the path even if it fails to insert the back ref */ |
| 1905 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1906 | path, bytenr, num_bytes, parent, |
| 1907 | root_objectid, owner, offset, |
| 1908 | refs_to_add, extent_op); |
| 1909 | if (ret == 0) |
| 1910 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1911 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1912 | if (ret != -EAGAIN) { |
| 1913 | err = ret; |
| 1914 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1915 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1916 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1917 | leaf = path->nodes[0]; |
| 1918 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1919 | refs = btrfs_extent_refs(leaf, item); |
| 1920 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1921 | if (extent_op) |
| 1922 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1923 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1924 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1925 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1926 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1927 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1928 | path->leave_spinning = 1; |
| 1929 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1930 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1931 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1932 | path, bytenr, parent, root_objectid, |
| 1933 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1934 | if (ret) |
| 1935 | btrfs_abort_transaction(trans, root, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1936 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1937 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1938 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1939 | } |
| 1940 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1941 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1942 | struct btrfs_root *root, |
| 1943 | struct btrfs_delayed_ref_node *node, |
| 1944 | struct btrfs_delayed_extent_op *extent_op, |
| 1945 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1946 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1947 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1948 | struct btrfs_delayed_data_ref *ref; |
| 1949 | struct btrfs_key ins; |
| 1950 | u64 parent = 0; |
| 1951 | u64 ref_root = 0; |
| 1952 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1953 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1954 | ins.objectid = node->bytenr; |
| 1955 | ins.offset = node->num_bytes; |
| 1956 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1957 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1958 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1959 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1960 | parent = ref->parent; |
| 1961 | else |
| 1962 | ref_root = ref->root; |
| 1963 | |
| 1964 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1965 | if (extent_op) { |
| 1966 | BUG_ON(extent_op->update_key); |
| 1967 | flags |= extent_op->flags_to_set; |
| 1968 | } |
| 1969 | ret = alloc_reserved_file_extent(trans, root, |
| 1970 | parent, ref_root, flags, |
| 1971 | ref->objectid, ref->offset, |
| 1972 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1973 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1974 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1975 | node->num_bytes, parent, |
| 1976 | ref_root, ref->objectid, |
| 1977 | ref->offset, node->ref_mod, |
| 1978 | extent_op); |
| 1979 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1980 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1981 | node->num_bytes, parent, |
| 1982 | ref_root, ref->objectid, |
| 1983 | ref->offset, node->ref_mod, |
| 1984 | extent_op); |
| 1985 | } else { |
| 1986 | BUG(); |
| 1987 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1988 | return ret; |
| 1989 | } |
| 1990 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1991 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1992 | struct extent_buffer *leaf, |
| 1993 | struct btrfs_extent_item *ei) |
| 1994 | { |
| 1995 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1996 | if (extent_op->update_flags) { |
| 1997 | flags |= extent_op->flags_to_set; |
| 1998 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1999 | } |
| 2000 | |
| 2001 | if (extent_op->update_key) { |
| 2002 | struct btrfs_tree_block_info *bi; |
| 2003 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2004 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2005 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 2010 | struct btrfs_root *root, |
| 2011 | struct btrfs_delayed_ref_node *node, |
| 2012 | struct btrfs_delayed_extent_op *extent_op) |
| 2013 | { |
| 2014 | struct btrfs_key key; |
| 2015 | struct btrfs_path *path; |
| 2016 | struct btrfs_extent_item *ei; |
| 2017 | struct extent_buffer *leaf; |
| 2018 | u32 item_size; |
| 2019 | int ret; |
| 2020 | int err = 0; |
| 2021 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2022 | if (trans->aborted) |
| 2023 | return 0; |
| 2024 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2025 | path = btrfs_alloc_path(); |
| 2026 | if (!path) |
| 2027 | return -ENOMEM; |
| 2028 | |
| 2029 | key.objectid = node->bytenr; |
| 2030 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2031 | key.offset = node->num_bytes; |
| 2032 | |
| 2033 | path->reada = 1; |
| 2034 | path->leave_spinning = 1; |
| 2035 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 2036 | path, 0, 1); |
| 2037 | if (ret < 0) { |
| 2038 | err = ret; |
| 2039 | goto out; |
| 2040 | } |
| 2041 | if (ret > 0) { |
| 2042 | err = -EIO; |
| 2043 | goto out; |
| 2044 | } |
| 2045 | |
| 2046 | leaf = path->nodes[0]; |
| 2047 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2048 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2049 | if (item_size < sizeof(*ei)) { |
| 2050 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 2051 | path, (u64)-1, 0); |
| 2052 | if (ret < 0) { |
| 2053 | err = ret; |
| 2054 | goto out; |
| 2055 | } |
| 2056 | leaf = path->nodes[0]; |
| 2057 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2058 | } |
| 2059 | #endif |
| 2060 | BUG_ON(item_size < sizeof(*ei)); |
| 2061 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2062 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2063 | |
| 2064 | btrfs_mark_buffer_dirty(leaf); |
| 2065 | out: |
| 2066 | btrfs_free_path(path); |
| 2067 | return err; |
| 2068 | } |
| 2069 | |
| 2070 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2071 | struct btrfs_root *root, |
| 2072 | struct btrfs_delayed_ref_node *node, |
| 2073 | struct btrfs_delayed_extent_op *extent_op, |
| 2074 | int insert_reserved) |
| 2075 | { |
| 2076 | int ret = 0; |
| 2077 | struct btrfs_delayed_tree_ref *ref; |
| 2078 | struct btrfs_key ins; |
| 2079 | u64 parent = 0; |
| 2080 | u64 ref_root = 0; |
| 2081 | |
| 2082 | ins.objectid = node->bytenr; |
| 2083 | ins.offset = node->num_bytes; |
| 2084 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2085 | |
| 2086 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2087 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2088 | parent = ref->parent; |
| 2089 | else |
| 2090 | ref_root = ref->root; |
| 2091 | |
| 2092 | BUG_ON(node->ref_mod != 1); |
| 2093 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2094 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2095 | !extent_op->update_key); |
| 2096 | ret = alloc_reserved_tree_block(trans, root, |
| 2097 | parent, ref_root, |
| 2098 | extent_op->flags_to_set, |
| 2099 | &extent_op->key, |
| 2100 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2101 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2102 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2103 | node->num_bytes, parent, ref_root, |
| 2104 | ref->level, 0, 1, extent_op); |
| 2105 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2106 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2107 | node->num_bytes, parent, ref_root, |
| 2108 | ref->level, 0, 1, extent_op); |
| 2109 | } else { |
| 2110 | BUG(); |
| 2111 | } |
| 2112 | return ret; |
| 2113 | } |
| 2114 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2115 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2116 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2117 | struct btrfs_root *root, |
| 2118 | struct btrfs_delayed_ref_node *node, |
| 2119 | struct btrfs_delayed_extent_op *extent_op, |
| 2120 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2121 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2122 | int ret = 0; |
| 2123 | |
| 2124 | if (trans->aborted) |
| 2125 | return 0; |
| 2126 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2127 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2128 | struct btrfs_delayed_ref_head *head; |
| 2129 | /* |
| 2130 | * we've hit the end of the chain and we were supposed |
| 2131 | * to insert this extent into the tree. But, it got |
| 2132 | * deleted before we ever needed to insert it, so all |
| 2133 | * we have to do is clean up the accounting |
| 2134 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2135 | BUG_ON(extent_op); |
| 2136 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2137 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2138 | btrfs_pin_extent(root, node->bytenr, |
| 2139 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2140 | if (head->is_data) { |
| 2141 | ret = btrfs_del_csums(trans, root, |
| 2142 | node->bytenr, |
| 2143 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2144 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2145 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2146 | mutex_unlock(&head->mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2147 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2148 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2149 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2150 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2151 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2152 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2153 | insert_reserved); |
| 2154 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2155 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2156 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2157 | insert_reserved); |
| 2158 | else |
| 2159 | BUG(); |
| 2160 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2163 | static noinline struct btrfs_delayed_ref_node * |
| 2164 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2165 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2166 | struct rb_node *node; |
| 2167 | struct btrfs_delayed_ref_node *ref; |
| 2168 | int action = BTRFS_ADD_DELAYED_REF; |
| 2169 | again: |
| 2170 | /* |
| 2171 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2172 | * this prevents ref count from going down to zero when |
| 2173 | * there still are pending delayed ref. |
| 2174 | */ |
| 2175 | node = rb_prev(&head->node.rb_node); |
| 2176 | while (1) { |
| 2177 | if (!node) |
| 2178 | break; |
| 2179 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2180 | rb_node); |
| 2181 | if (ref->bytenr != head->node.bytenr) |
| 2182 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2183 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2184 | return ref; |
| 2185 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2186 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2187 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2188 | action = BTRFS_DROP_DELAYED_REF; |
| 2189 | goto again; |
| 2190 | } |
| 2191 | return NULL; |
| 2192 | } |
| 2193 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2194 | /* |
| 2195 | * Returns 0 on success or if called with an already aborted transaction. |
| 2196 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2197 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2198 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2199 | struct btrfs_root *root, |
| 2200 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2201 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2202 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2203 | struct btrfs_delayed_ref_node *ref; |
| 2204 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2205 | struct btrfs_delayed_extent_op *extent_op; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2206 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2207 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2208 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2209 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2210 | |
| 2211 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2212 | while (1) { |
| 2213 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2214 | /* pick a new head ref from the cluster list */ |
| 2215 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2216 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2217 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2218 | locked_ref = list_entry(cluster->next, |
| 2219 | struct btrfs_delayed_ref_head, cluster); |
| 2220 | |
| 2221 | /* grab the lock that says we are going to process |
| 2222 | * all the refs for this head */ |
| 2223 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2224 | |
| 2225 | /* |
| 2226 | * we may have dropped the spin lock to get the head |
| 2227 | * mutex lock, and that might have given someone else |
| 2228 | * time to free the head. If that's true, it has been |
| 2229 | * removed from our list and we can move on. |
| 2230 | */ |
| 2231 | if (ret == -EAGAIN) { |
| 2232 | locked_ref = NULL; |
| 2233 | count++; |
| 2234 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | /* |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2239 | * We need to try and merge add/drops of the same ref since we |
| 2240 | * can run into issues with relocate dropping the implicit ref |
| 2241 | * and then it being added back again before the drop can |
| 2242 | * finish. If we merged anything we need to re-loop so we can |
| 2243 | * get a good ref. |
| 2244 | */ |
| 2245 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2246 | locked_ref); |
| 2247 | |
| 2248 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2249 | * locked_ref is the head node, so we have to go one |
| 2250 | * node back for any delayed ref updates |
| 2251 | */ |
| 2252 | ref = select_delayed_ref(locked_ref); |
| 2253 | |
| 2254 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2255 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2256 | /* |
| 2257 | * there are still refs with lower seq numbers in the |
| 2258 | * process of being added. Don't run this ref yet. |
| 2259 | */ |
| 2260 | list_del_init(&locked_ref->cluster); |
| 2261 | mutex_unlock(&locked_ref->mutex); |
| 2262 | locked_ref = NULL; |
| 2263 | delayed_refs->num_heads_ready++; |
| 2264 | spin_unlock(&delayed_refs->lock); |
| 2265 | cond_resched(); |
| 2266 | spin_lock(&delayed_refs->lock); |
| 2267 | continue; |
| 2268 | } |
| 2269 | |
| 2270 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2271 | * record the must insert reserved flag before we |
| 2272 | * drop the spin lock. |
| 2273 | */ |
| 2274 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2275 | locked_ref->must_insert_reserved = 0; |
| 2276 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2277 | extent_op = locked_ref->extent_op; |
| 2278 | locked_ref->extent_op = NULL; |
| 2279 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2280 | if (!ref) { |
| 2281 | /* All delayed refs have been processed, Go ahead |
| 2282 | * and send the head node to run_one_delayed_ref, |
| 2283 | * so that any accounting fixes can happen |
| 2284 | */ |
| 2285 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2286 | |
| 2287 | if (extent_op && must_insert_reserved) { |
| 2288 | kfree(extent_op); |
| 2289 | extent_op = NULL; |
| 2290 | } |
| 2291 | |
| 2292 | if (extent_op) { |
| 2293 | spin_unlock(&delayed_refs->lock); |
| 2294 | |
| 2295 | ret = run_delayed_extent_op(trans, root, |
| 2296 | ref, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2297 | kfree(extent_op); |
| 2298 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2299 | if (ret) { |
| 2300 | printk(KERN_DEBUG "btrfs: run_delayed_extent_op returned %d\n", ret); |
Dan Carpenter | 253beeb | 2012-04-18 09:59:03 +0300 | [diff] [blame] | 2301 | spin_lock(&delayed_refs->lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2302 | return ret; |
| 2303 | } |
| 2304 | |
Chris Mason | 203bf28 | 2012-01-06 15:23:57 -0500 | [diff] [blame] | 2305 | goto next; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2306 | } |
| 2307 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2308 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2309 | locked_ref = NULL; |
| 2310 | } |
| 2311 | |
| 2312 | ref->in_tree = 0; |
| 2313 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2314 | delayed_refs->num_entries--; |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2315 | if (locked_ref) { |
| 2316 | /* |
| 2317 | * when we play the delayed ref, also correct the |
| 2318 | * ref_mod on head |
| 2319 | */ |
| 2320 | switch (ref->action) { |
| 2321 | case BTRFS_ADD_DELAYED_REF: |
| 2322 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2323 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2324 | break; |
| 2325 | case BTRFS_DROP_DELAYED_REF: |
| 2326 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2327 | break; |
| 2328 | default: |
| 2329 | WARN_ON(1); |
| 2330 | } |
| 2331 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2332 | spin_unlock(&delayed_refs->lock); |
| 2333 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2334 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2335 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2336 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2337 | btrfs_put_delayed_ref(ref); |
| 2338 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2339 | count++; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2340 | |
| 2341 | if (ret) { |
| 2342 | printk(KERN_DEBUG "btrfs: run_one_delayed_ref returned %d\n", ret); |
Dan Carpenter | 253beeb | 2012-04-18 09:59:03 +0300 | [diff] [blame] | 2343 | spin_lock(&delayed_refs->lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2344 | return ret; |
| 2345 | } |
| 2346 | |
Chris Mason | 203bf28 | 2012-01-06 15:23:57 -0500 | [diff] [blame] | 2347 | next: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2348 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2349 | spin_lock(&delayed_refs->lock); |
| 2350 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2351 | return count; |
| 2352 | } |
| 2353 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2354 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2355 | /* |
| 2356 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2357 | * correlates in most cases to the order added. To expose dependencies on this |
| 2358 | * order, we start to process the tree in the middle instead of the beginning |
| 2359 | */ |
| 2360 | static u64 find_middle(struct rb_root *root) |
| 2361 | { |
| 2362 | struct rb_node *n = root->rb_node; |
| 2363 | struct btrfs_delayed_ref_node *entry; |
| 2364 | int alt = 1; |
| 2365 | u64 middle; |
| 2366 | u64 first = 0, last = 0; |
| 2367 | |
| 2368 | n = rb_first(root); |
| 2369 | if (n) { |
| 2370 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2371 | first = entry->bytenr; |
| 2372 | } |
| 2373 | n = rb_last(root); |
| 2374 | if (n) { |
| 2375 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2376 | last = entry->bytenr; |
| 2377 | } |
| 2378 | n = root->rb_node; |
| 2379 | |
| 2380 | while (n) { |
| 2381 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2382 | WARN_ON(!entry->in_tree); |
| 2383 | |
| 2384 | middle = entry->bytenr; |
| 2385 | |
| 2386 | if (alt) |
| 2387 | n = n->rb_left; |
| 2388 | else |
| 2389 | n = n->rb_right; |
| 2390 | |
| 2391 | alt = 1 - alt; |
| 2392 | } |
| 2393 | return middle; |
| 2394 | } |
| 2395 | #endif |
| 2396 | |
Arne Jansen | bed92ea | 2012-06-28 18:03:02 +0200 | [diff] [blame] | 2397 | int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans, |
| 2398 | struct btrfs_fs_info *fs_info) |
| 2399 | { |
| 2400 | struct qgroup_update *qgroup_update; |
| 2401 | int ret = 0; |
| 2402 | |
| 2403 | if (list_empty(&trans->qgroup_ref_list) != |
| 2404 | !trans->delayed_ref_elem.seq) { |
| 2405 | /* list without seq or seq without list */ |
| 2406 | printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n", |
| 2407 | list_empty(&trans->qgroup_ref_list) ? "" : " not", |
| 2408 | trans->delayed_ref_elem.seq); |
| 2409 | BUG(); |
| 2410 | } |
| 2411 | |
| 2412 | if (!trans->delayed_ref_elem.seq) |
| 2413 | return 0; |
| 2414 | |
| 2415 | while (!list_empty(&trans->qgroup_ref_list)) { |
| 2416 | qgroup_update = list_first_entry(&trans->qgroup_ref_list, |
| 2417 | struct qgroup_update, list); |
| 2418 | list_del(&qgroup_update->list); |
| 2419 | if (!ret) |
| 2420 | ret = btrfs_qgroup_account_ref( |
| 2421 | trans, fs_info, qgroup_update->node, |
| 2422 | qgroup_update->extent_op); |
| 2423 | kfree(qgroup_update); |
| 2424 | } |
| 2425 | |
| 2426 | btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem); |
| 2427 | |
| 2428 | return ret; |
| 2429 | } |
| 2430 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2431 | /* |
| 2432 | * this starts processing the delayed reference count updates and |
| 2433 | * extent insertions we have queued up so far. count can be |
| 2434 | * 0, which means to process everything in the tree at the start |
| 2435 | * of the run (but not newly added entries), or it can be some target |
| 2436 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2437 | * |
| 2438 | * Returns 0 on success or if called with an aborted transaction |
| 2439 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2440 | */ |
| 2441 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2442 | struct btrfs_root *root, unsigned long count) |
| 2443 | { |
| 2444 | struct rb_node *node; |
| 2445 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2446 | struct btrfs_delayed_ref_node *ref; |
| 2447 | struct list_head cluster; |
| 2448 | int ret; |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2449 | u64 delayed_start; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2450 | int run_all = count == (unsigned long)-1; |
| 2451 | int run_most = 0; |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2452 | int loops; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2453 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2454 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2455 | if (trans->aborted) |
| 2456 | return 0; |
| 2457 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2458 | if (root == root->fs_info->extent_root) |
| 2459 | root = root->fs_info->tree_root; |
| 2460 | |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 2461 | btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info); |
| 2462 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2463 | delayed_refs = &trans->transaction->delayed_refs; |
| 2464 | INIT_LIST_HEAD(&cluster); |
| 2465 | again: |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2466 | loops = 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2467 | spin_lock(&delayed_refs->lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2468 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2469 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2470 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2471 | #endif |
| 2472 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2473 | if (count == 0) { |
| 2474 | count = delayed_refs->num_entries * 2; |
| 2475 | run_most = 1; |
| 2476 | } |
| 2477 | while (1) { |
| 2478 | if (!(run_all || run_most) && |
| 2479 | delayed_refs->num_heads_ready < 64) |
| 2480 | break; |
| 2481 | |
| 2482 | /* |
| 2483 | * go find something we can process in the rbtree. We start at |
| 2484 | * the beginning of the tree, and then build a cluster |
| 2485 | * of refs to process starting at the first one we are able to |
| 2486 | * lock |
| 2487 | */ |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2488 | delayed_start = delayed_refs->run_delayed_start; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2489 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2490 | delayed_refs->run_delayed_start); |
| 2491 | if (ret) |
| 2492 | break; |
| 2493 | |
| 2494 | ret = run_clustered_refs(trans, root, &cluster); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2495 | if (ret < 0) { |
| 2496 | spin_unlock(&delayed_refs->lock); |
| 2497 | btrfs_abort_transaction(trans, root, ret); |
| 2498 | return ret; |
| 2499 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2500 | |
| 2501 | count -= min_t(unsigned long, ret, count); |
| 2502 | |
| 2503 | if (count == 0) |
| 2504 | break; |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2505 | |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2506 | if (delayed_start >= delayed_refs->run_delayed_start) { |
| 2507 | if (loops == 0) { |
| 2508 | /* |
| 2509 | * btrfs_find_ref_cluster looped. let's do one |
| 2510 | * more cycle. if we don't run any delayed ref |
| 2511 | * during that cycle (because we can't because |
| 2512 | * all of them are blocked), bail out. |
| 2513 | */ |
| 2514 | loops = 1; |
| 2515 | } else { |
| 2516 | /* |
| 2517 | * no runnable refs left, stop trying |
| 2518 | */ |
| 2519 | BUG_ON(run_all); |
| 2520 | break; |
| 2521 | } |
| 2522 | } |
| 2523 | if (ret) { |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2524 | /* refs were run, let's reset staleness detection */ |
Arne Jansen | 1fa11e2 | 2012-08-06 14:18:51 -0600 | [diff] [blame] | 2525 | loops = 0; |
Jan Schmidt | a168650 | 2011-12-12 16:10:07 +0100 | [diff] [blame] | 2526 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2527 | } |
| 2528 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2529 | if (run_all) { |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2530 | if (!list_empty(&trans->new_bgs)) { |
| 2531 | spin_unlock(&delayed_refs->lock); |
| 2532 | btrfs_create_pending_block_groups(trans, root); |
| 2533 | spin_lock(&delayed_refs->lock); |
| 2534 | } |
| 2535 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2536 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2537 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2538 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2539 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2540 | |
| 2541 | while (node) { |
| 2542 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2543 | rb_node); |
| 2544 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2545 | struct btrfs_delayed_ref_head *head; |
| 2546 | |
| 2547 | head = btrfs_delayed_node_to_head(ref); |
| 2548 | atomic_inc(&ref->refs); |
| 2549 | |
| 2550 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2551 | /* |
| 2552 | * Mutex was contended, block until it's |
| 2553 | * released and try again |
| 2554 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2555 | mutex_lock(&head->mutex); |
| 2556 | mutex_unlock(&head->mutex); |
| 2557 | |
| 2558 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2559 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2560 | goto again; |
| 2561 | } |
| 2562 | node = rb_next(node); |
| 2563 | } |
| 2564 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2565 | schedule_timeout(1); |
| 2566 | goto again; |
| 2567 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2568 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2569 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 2570 | assert_qgroups_uptodate(trans); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2571 | return 0; |
| 2572 | } |
| 2573 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2574 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2575 | struct btrfs_root *root, |
| 2576 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2577 | int is_data) |
| 2578 | { |
| 2579 | struct btrfs_delayed_extent_op *extent_op; |
| 2580 | int ret; |
| 2581 | |
| 2582 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2583 | if (!extent_op) |
| 2584 | return -ENOMEM; |
| 2585 | |
| 2586 | extent_op->flags_to_set = flags; |
| 2587 | extent_op->update_flags = 1; |
| 2588 | extent_op->update_key = 0; |
| 2589 | extent_op->is_data = is_data ? 1 : 0; |
| 2590 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2591 | ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr, |
| 2592 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2593 | if (ret) |
| 2594 | kfree(extent_op); |
| 2595 | return ret; |
| 2596 | } |
| 2597 | |
| 2598 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2599 | struct btrfs_root *root, |
| 2600 | struct btrfs_path *path, |
| 2601 | u64 objectid, u64 offset, u64 bytenr) |
| 2602 | { |
| 2603 | struct btrfs_delayed_ref_head *head; |
| 2604 | struct btrfs_delayed_ref_node *ref; |
| 2605 | struct btrfs_delayed_data_ref *data_ref; |
| 2606 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2607 | struct rb_node *node; |
| 2608 | int ret = 0; |
| 2609 | |
| 2610 | ret = -ENOENT; |
| 2611 | delayed_refs = &trans->transaction->delayed_refs; |
| 2612 | spin_lock(&delayed_refs->lock); |
| 2613 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2614 | if (!head) |
| 2615 | goto out; |
| 2616 | |
| 2617 | if (!mutex_trylock(&head->mutex)) { |
| 2618 | atomic_inc(&head->node.refs); |
| 2619 | spin_unlock(&delayed_refs->lock); |
| 2620 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2621 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2622 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2623 | /* |
| 2624 | * Mutex was contended, block until it's released and let |
| 2625 | * caller try again |
| 2626 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2627 | mutex_lock(&head->mutex); |
| 2628 | mutex_unlock(&head->mutex); |
| 2629 | btrfs_put_delayed_ref(&head->node); |
| 2630 | return -EAGAIN; |
| 2631 | } |
| 2632 | |
| 2633 | node = rb_prev(&head->node.rb_node); |
| 2634 | if (!node) |
| 2635 | goto out_unlock; |
| 2636 | |
| 2637 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2638 | |
| 2639 | if (ref->bytenr != bytenr) |
| 2640 | goto out_unlock; |
| 2641 | |
| 2642 | ret = 1; |
| 2643 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2644 | goto out_unlock; |
| 2645 | |
| 2646 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2647 | |
| 2648 | node = rb_prev(node); |
| 2649 | if (node) { |
Liu Bo | df57dbe | 2012-07-23 05:50:03 -0600 | [diff] [blame] | 2650 | int seq = ref->seq; |
| 2651 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2652 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
Liu Bo | df57dbe | 2012-07-23 05:50:03 -0600 | [diff] [blame] | 2653 | if (ref->bytenr == bytenr && ref->seq == seq) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2654 | goto out_unlock; |
| 2655 | } |
| 2656 | |
| 2657 | if (data_ref->root != root->root_key.objectid || |
| 2658 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2659 | goto out_unlock; |
| 2660 | |
| 2661 | ret = 0; |
| 2662 | out_unlock: |
| 2663 | mutex_unlock(&head->mutex); |
| 2664 | out: |
| 2665 | spin_unlock(&delayed_refs->lock); |
| 2666 | return ret; |
| 2667 | } |
| 2668 | |
| 2669 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2670 | struct btrfs_root *root, |
| 2671 | struct btrfs_path *path, |
| 2672 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2673 | { |
| 2674 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2675 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2676 | struct btrfs_extent_data_ref *ref; |
| 2677 | struct btrfs_extent_inline_ref *iref; |
| 2678 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2679 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2680 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2681 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2682 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2683 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2684 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2685 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2686 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2687 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2688 | if (ret < 0) |
| 2689 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2690 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2691 | |
| 2692 | ret = -ENOENT; |
| 2693 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2694 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2695 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2696 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2697 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2698 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2699 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2700 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2701 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2702 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2703 | ret = 1; |
| 2704 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2705 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2706 | if (item_size < sizeof(*ei)) { |
| 2707 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2708 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2709 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2710 | #endif |
| 2711 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2712 | |
| 2713 | if (item_size != sizeof(*ei) + |
| 2714 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2715 | goto out; |
| 2716 | |
| 2717 | if (btrfs_extent_generation(leaf, ei) <= |
| 2718 | btrfs_root_last_snapshot(&root->root_item)) |
| 2719 | goto out; |
| 2720 | |
| 2721 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2722 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2723 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2724 | goto out; |
| 2725 | |
| 2726 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2727 | if (btrfs_extent_refs(leaf, ei) != |
| 2728 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2729 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2730 | root->root_key.objectid || |
| 2731 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2732 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2733 | goto out; |
| 2734 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2735 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2736 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2737 | return ret; |
| 2738 | } |
| 2739 | |
| 2740 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2741 | struct btrfs_root *root, |
| 2742 | u64 objectid, u64 offset, u64 bytenr) |
| 2743 | { |
| 2744 | struct btrfs_path *path; |
| 2745 | int ret; |
| 2746 | int ret2; |
| 2747 | |
| 2748 | path = btrfs_alloc_path(); |
| 2749 | if (!path) |
| 2750 | return -ENOENT; |
| 2751 | |
| 2752 | do { |
| 2753 | ret = check_committed_ref(trans, root, path, objectid, |
| 2754 | offset, bytenr); |
| 2755 | if (ret && ret != -ENOENT) |
| 2756 | goto out; |
| 2757 | |
| 2758 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2759 | offset, bytenr); |
| 2760 | } while (ret2 == -EAGAIN); |
| 2761 | |
| 2762 | if (ret2 && ret2 != -ENOENT) { |
| 2763 | ret = ret2; |
| 2764 | goto out; |
| 2765 | } |
| 2766 | |
| 2767 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2768 | ret = 0; |
| 2769 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2770 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2771 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2772 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2773 | return ret; |
| 2774 | } |
| 2775 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2776 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2777 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2778 | struct extent_buffer *buf, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2779 | int full_backref, int inc, int for_cow) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2780 | { |
| 2781 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2782 | u64 num_bytes; |
| 2783 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2784 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2785 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2786 | struct btrfs_key key; |
| 2787 | struct btrfs_file_extent_item *fi; |
| 2788 | int i; |
| 2789 | int level; |
| 2790 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2791 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2792 | u64, u64, u64, u64, u64, u64, int); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2793 | |
| 2794 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2795 | nritems = btrfs_header_nritems(buf); |
| 2796 | level = btrfs_header_level(buf); |
| 2797 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2798 | if (!root->ref_cows && level == 0) |
| 2799 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2800 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2801 | if (inc) |
| 2802 | process_func = btrfs_inc_extent_ref; |
| 2803 | else |
| 2804 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2805 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2806 | if (full_backref) |
| 2807 | parent = buf->start; |
| 2808 | else |
| 2809 | parent = 0; |
| 2810 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2811 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2812 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2813 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2814 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2815 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2816 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2817 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2818 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2819 | BTRFS_FILE_EXTENT_INLINE) |
| 2820 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2821 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2822 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2823 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2824 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2825 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2826 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2827 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2828 | parent, ref_root, key.objectid, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2829 | key.offset, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2830 | if (ret) |
| 2831 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2832 | } else { |
| 2833 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2834 | num_bytes = btrfs_level_size(root, level - 1); |
| 2835 | ret = process_func(trans, root, bytenr, num_bytes, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2836 | parent, ref_root, level - 1, 0, |
| 2837 | for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2838 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2839 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2840 | } |
| 2841 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2842 | return 0; |
| 2843 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2844 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2845 | } |
| 2846 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2847 | 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] | 2848 | struct extent_buffer *buf, int full_backref, int for_cow) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2849 | { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2850 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2851 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2852 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2853 | 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] | 2854 | struct extent_buffer *buf, int full_backref, int for_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2855 | { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2856 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2857 | } |
| 2858 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2859 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2860 | struct btrfs_root *root, |
| 2861 | struct btrfs_path *path, |
| 2862 | struct btrfs_block_group_cache *cache) |
| 2863 | { |
| 2864 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2865 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2866 | unsigned long bi; |
| 2867 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2868 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2869 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2870 | if (ret < 0) |
| 2871 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2872 | BUG_ON(ret); /* Corruption */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2873 | |
| 2874 | leaf = path->nodes[0]; |
| 2875 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2876 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2877 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2878 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2879 | fail: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2880 | if (ret) { |
| 2881 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2882 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2883 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2884 | return 0; |
| 2885 | |
| 2886 | } |
| 2887 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2888 | static struct btrfs_block_group_cache * |
| 2889 | next_block_group(struct btrfs_root *root, |
| 2890 | struct btrfs_block_group_cache *cache) |
| 2891 | { |
| 2892 | struct rb_node *node; |
| 2893 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2894 | node = rb_next(&cache->cache_node); |
| 2895 | btrfs_put_block_group(cache); |
| 2896 | if (node) { |
| 2897 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2898 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2899 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2900 | } else |
| 2901 | cache = NULL; |
| 2902 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2903 | return cache; |
| 2904 | } |
| 2905 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2906 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2907 | struct btrfs_trans_handle *trans, |
| 2908 | struct btrfs_path *path) |
| 2909 | { |
| 2910 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2911 | struct inode *inode = NULL; |
| 2912 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2913 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2914 | int num_pages = 0; |
| 2915 | int retries = 0; |
| 2916 | int ret = 0; |
| 2917 | |
| 2918 | /* |
| 2919 | * If this block group is smaller than 100 megs don't bother caching the |
| 2920 | * block group. |
| 2921 | */ |
| 2922 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2923 | spin_lock(&block_group->lock); |
| 2924 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2925 | spin_unlock(&block_group->lock); |
| 2926 | return 0; |
| 2927 | } |
| 2928 | |
| 2929 | again: |
| 2930 | inode = lookup_free_space_inode(root, block_group, path); |
| 2931 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2932 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2933 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2934 | goto out; |
| 2935 | } |
| 2936 | |
| 2937 | if (IS_ERR(inode)) { |
| 2938 | BUG_ON(retries); |
| 2939 | retries++; |
| 2940 | |
| 2941 | if (block_group->ro) |
| 2942 | goto out_free; |
| 2943 | |
| 2944 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2945 | if (ret) |
| 2946 | goto out_free; |
| 2947 | goto again; |
| 2948 | } |
| 2949 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2950 | /* We've already setup this transaction, go ahead and exit */ |
| 2951 | if (block_group->cache_generation == trans->transid && |
| 2952 | i_size_read(inode)) { |
| 2953 | dcs = BTRFS_DC_SETUP; |
| 2954 | goto out_put; |
| 2955 | } |
| 2956 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2957 | /* |
| 2958 | * We want to set the generation to 0, that way if anything goes wrong |
| 2959 | * from here on out we know not to trust this cache when we load up next |
| 2960 | * time. |
| 2961 | */ |
| 2962 | BTRFS_I(inode)->generation = 0; |
| 2963 | ret = btrfs_update_inode(trans, root, inode); |
| 2964 | WARN_ON(ret); |
| 2965 | |
| 2966 | if (i_size_read(inode) > 0) { |
| 2967 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2968 | inode); |
| 2969 | if (ret) |
| 2970 | goto out_put; |
| 2971 | } |
| 2972 | |
| 2973 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 2974 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
| 2975 | !btrfs_test_opt(root, SPACE_CACHE)) { |
| 2976 | /* |
| 2977 | * don't bother trying to write stuff out _if_ |
| 2978 | * a) we're not cached, |
| 2979 | * b) we're with nospace_cache mount option. |
| 2980 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2981 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2982 | spin_unlock(&block_group->lock); |
| 2983 | goto out_put; |
| 2984 | } |
| 2985 | spin_unlock(&block_group->lock); |
| 2986 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 2987 | /* |
| 2988 | * Try to preallocate enough space based on how big the block group is. |
| 2989 | * Keep in mind this has to include any pinned space which could end up |
| 2990 | * taking up quite a bit since it's not folded into the other space |
| 2991 | * cache. |
| 2992 | */ |
| 2993 | num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2994 | if (!num_pages) |
| 2995 | num_pages = 1; |
| 2996 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2997 | num_pages *= 16; |
| 2998 | num_pages *= PAGE_CACHE_SIZE; |
| 2999 | |
| 3000 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 3001 | if (ret) |
| 3002 | goto out_put; |
| 3003 | |
| 3004 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3005 | num_pages, num_pages, |
| 3006 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3007 | if (!ret) |
| 3008 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3009 | btrfs_free_reserved_data_space(inode, num_pages); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3010 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3011 | out_put: |
| 3012 | iput(inode); |
| 3013 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3014 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3015 | out: |
| 3016 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3017 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3018 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3019 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3020 | spin_unlock(&block_group->lock); |
| 3021 | |
| 3022 | return ret; |
| 3023 | } |
| 3024 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3025 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 3026 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3027 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3028 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3029 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3030 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3031 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3032 | |
| 3033 | path = btrfs_alloc_path(); |
| 3034 | if (!path) |
| 3035 | return -ENOMEM; |
| 3036 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3037 | again: |
| 3038 | while (1) { |
| 3039 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3040 | while (cache) { |
| 3041 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3042 | break; |
| 3043 | cache = next_block_group(root, cache); |
| 3044 | } |
| 3045 | if (!cache) { |
| 3046 | if (last == 0) |
| 3047 | break; |
| 3048 | last = 0; |
| 3049 | continue; |
| 3050 | } |
| 3051 | err = cache_save_setup(cache, trans, path); |
| 3052 | last = cache->key.objectid + cache->key.offset; |
| 3053 | btrfs_put_block_group(cache); |
| 3054 | } |
| 3055 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3056 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3057 | if (last == 0) { |
| 3058 | err = btrfs_run_delayed_refs(trans, root, |
| 3059 | (unsigned long)-1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3060 | if (err) /* File system offline */ |
| 3061 | goto out; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3062 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3063 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3064 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3065 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3066 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 3067 | btrfs_put_block_group(cache); |
| 3068 | goto again; |
| 3069 | } |
| 3070 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3071 | if (cache->dirty) |
| 3072 | break; |
| 3073 | cache = next_block_group(root, cache); |
| 3074 | } |
| 3075 | if (!cache) { |
| 3076 | if (last == 0) |
| 3077 | break; |
| 3078 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3079 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3080 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3081 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3082 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 3083 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3084 | cache->dirty = 0; |
| 3085 | last = cache->key.objectid + cache->key.offset; |
| 3086 | |
| 3087 | err = write_one_cache_group(trans, root, path, cache); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3088 | if (err) /* File system offline */ |
| 3089 | goto out; |
| 3090 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3091 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3092 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3093 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3094 | while (1) { |
| 3095 | /* |
| 3096 | * I don't think this is needed since we're just marking our |
| 3097 | * preallocated extent as written, but just in case it can't |
| 3098 | * hurt. |
| 3099 | */ |
| 3100 | if (last == 0) { |
| 3101 | err = btrfs_run_delayed_refs(trans, root, |
| 3102 | (unsigned long)-1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3103 | if (err) /* File system offline */ |
| 3104 | goto out; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3108 | while (cache) { |
| 3109 | /* |
| 3110 | * Really this shouldn't happen, but it could if we |
| 3111 | * couldn't write the entire preallocated extent and |
| 3112 | * splitting the extent resulted in a new block. |
| 3113 | */ |
| 3114 | if (cache->dirty) { |
| 3115 | btrfs_put_block_group(cache); |
| 3116 | goto again; |
| 3117 | } |
| 3118 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 3119 | break; |
| 3120 | cache = next_block_group(root, cache); |
| 3121 | } |
| 3122 | if (!cache) { |
| 3123 | if (last == 0) |
| 3124 | break; |
| 3125 | last = 0; |
| 3126 | continue; |
| 3127 | } |
| 3128 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3129 | err = btrfs_write_out_cache(root, trans, cache, path); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3130 | |
| 3131 | /* |
| 3132 | * If we didn't have an error then the cache state is still |
| 3133 | * NEED_WRITE, so we can set it to WRITTEN. |
| 3134 | */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3135 | if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3136 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3137 | last = cache->key.objectid + cache->key.offset; |
| 3138 | btrfs_put_block_group(cache); |
| 3139 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3140 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3141 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3142 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3143 | return err; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3144 | } |
| 3145 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3146 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 3147 | { |
| 3148 | struct btrfs_block_group_cache *block_group; |
| 3149 | int readonly = 0; |
| 3150 | |
| 3151 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 3152 | if (!block_group || block_group->ro) |
| 3153 | readonly = 1; |
| 3154 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3155 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3156 | return readonly; |
| 3157 | } |
| 3158 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3159 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3160 | u64 total_bytes, u64 bytes_used, |
| 3161 | struct btrfs_space_info **space_info) |
| 3162 | { |
| 3163 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3164 | int i; |
| 3165 | int factor; |
| 3166 | |
| 3167 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3168 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3169 | factor = 2; |
| 3170 | else |
| 3171 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3172 | |
| 3173 | found = __find_space_info(info, flags); |
| 3174 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3175 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3176 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3177 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3178 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3179 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3180 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3181 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3182 | *space_info = found; |
| 3183 | return 0; |
| 3184 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3185 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3186 | if (!found) |
| 3187 | return -ENOMEM; |
| 3188 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3189 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 3190 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3191 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3192 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3193 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3194 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3195 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3196 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3197 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3198 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3199 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3200 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3201 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3202 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3203 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3204 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3205 | found->flush = 0; |
| 3206 | init_waitqueue_head(&found->wait); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3207 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3208 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3209 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3210 | info->data_sinfo = found; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3211 | return 0; |
| 3212 | } |
| 3213 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3214 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3215 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3216 | u64 extra_flags = chunk_to_extended(flags) & |
| 3217 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3218 | |
| 3219 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3220 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3221 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3222 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3223 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3224 | fs_info->avail_system_alloc_bits |= extra_flags; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3225 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3226 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3227 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3228 | * returns target flags in extended format or 0 if restripe for this |
| 3229 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3230 | * |
| 3231 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3232 | */ |
| 3233 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 3234 | { |
| 3235 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3236 | u64 target = 0; |
| 3237 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3238 | if (!bctl) |
| 3239 | return 0; |
| 3240 | |
| 3241 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 3242 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3243 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 3244 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 3245 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3246 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 3247 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 3248 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3249 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 3250 | } |
| 3251 | |
| 3252 | return target; |
| 3253 | } |
| 3254 | |
| 3255 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3256 | * @flags: available profiles in extended format (see ctree.h) |
| 3257 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3258 | * Returns reduced profile in chunk format. If profile changing is in |
| 3259 | * progress (either running or paused) picks the target profile (if it's |
| 3260 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3261 | */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3262 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3263 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 3264 | /* |
| 3265 | * we add in the count of missing devices because we want |
| 3266 | * to make sure that any RAID levels on a degraded FS |
| 3267 | * continue to be honored. |
| 3268 | */ |
| 3269 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 3270 | root->fs_info->fs_devices->missing_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3271 | u64 target; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3272 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3273 | /* |
| 3274 | * see if restripe for this chunk_type is in progress, if so |
| 3275 | * try to reduce to the target profile |
| 3276 | */ |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3277 | spin_lock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3278 | target = get_restripe_target(root->fs_info, flags); |
| 3279 | if (target) { |
| 3280 | /* pick target profile only if it's already available */ |
| 3281 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3282 | spin_unlock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3283 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3284 | } |
| 3285 | } |
| 3286 | spin_unlock(&root->fs_info->balance_lock); |
| 3287 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3288 | if (num_devices == 1) |
| 3289 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 3290 | if (num_devices < 4) |
| 3291 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 3292 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3293 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 3294 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3295 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3296 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3297 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3298 | |
| 3299 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3300 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3301 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3302 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3303 | |
| 3304 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 3305 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 3306 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3307 | (flags & BTRFS_BLOCK_GROUP_DUP))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3308 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3309 | } |
| 3310 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3311 | return extended_to_chunk(flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3312 | } |
| 3313 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3314 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3315 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3316 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3317 | flags |= root->fs_info->avail_data_alloc_bits; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3318 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3319 | flags |= root->fs_info->avail_system_alloc_bits; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3320 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3321 | flags |= root->fs_info->avail_metadata_alloc_bits; |
| 3322 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3323 | return btrfs_reduce_alloc_profile(root, flags); |
| 3324 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3325 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3326 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3327 | { |
| 3328 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3329 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3330 | if (data) |
| 3331 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3332 | else if (root == root->fs_info->chunk_root) |
| 3333 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3334 | else |
| 3335 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3336 | |
| 3337 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3338 | } |
| 3339 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3340 | /* |
| 3341 | * This will check the space that the inode allocates from to make sure we have |
| 3342 | * enough space for bytes. |
| 3343 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3344 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3345 | { |
| 3346 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3347 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3348 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3349 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3350 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3351 | |
| 3352 | /* make sure bytes are sectorsize aligned */ |
| 3353 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3354 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3355 | if (root == root->fs_info->tree_root || |
| 3356 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3357 | alloc_chunk = 0; |
| 3358 | committed = 1; |
| 3359 | } |
| 3360 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3361 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3362 | if (!data_sinfo) |
| 3363 | goto alloc; |
| 3364 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3365 | again: |
| 3366 | /* make sure we have enough space to handle the data first */ |
| 3367 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3368 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3369 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3370 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3371 | |
| 3372 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3373 | struct btrfs_trans_handle *trans; |
| 3374 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3375 | /* |
| 3376 | * if we don't have enough free bytes in this space then we need |
| 3377 | * to alloc a new chunk. |
| 3378 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3379 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3380 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3381 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3382 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3383 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3384 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3385 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3386 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3387 | if (IS_ERR(trans)) |
| 3388 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3389 | |
| 3390 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3391 | alloc_target, |
| 3392 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3393 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3394 | if (ret < 0) { |
| 3395 | if (ret != -ENOSPC) |
| 3396 | return ret; |
| 3397 | else |
| 3398 | goto commit_trans; |
| 3399 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3400 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3401 | if (!data_sinfo) |
| 3402 | data_sinfo = fs_info->data_sinfo; |
| 3403 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3404 | goto again; |
| 3405 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3406 | |
| 3407 | /* |
| 3408 | * If we have less pinned bytes than we want to allocate then |
| 3409 | * don't bother committing the transaction, it won't help us. |
| 3410 | */ |
| 3411 | if (data_sinfo->bytes_pinned < bytes) |
| 3412 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3413 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3414 | |
| 3415 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3416 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3417 | if (!committed && |
| 3418 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3419 | committed = 1; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3420 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3421 | if (IS_ERR(trans)) |
| 3422 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3423 | ret = btrfs_commit_transaction(trans, root); |
| 3424 | if (ret) |
| 3425 | return ret; |
| 3426 | goto again; |
| 3427 | } |
| 3428 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3429 | return -ENOSPC; |
| 3430 | } |
| 3431 | data_sinfo->bytes_may_use += bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3432 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3433 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3434 | spin_unlock(&data_sinfo->lock); |
| 3435 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3436 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3440 | * Called if we need to clear a data reservation for this inode. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3441 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3442 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3443 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3444 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3445 | struct btrfs_space_info *data_sinfo; |
| 3446 | |
| 3447 | /* make sure bytes are sectorsize aligned */ |
| 3448 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3449 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3450 | data_sinfo = root->fs_info->data_sinfo; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3451 | spin_lock(&data_sinfo->lock); |
| 3452 | data_sinfo->bytes_may_use -= bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3453 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3454 | data_sinfo->flags, bytes, 0); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3455 | spin_unlock(&data_sinfo->lock); |
| 3456 | } |
| 3457 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3458 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3459 | { |
| 3460 | struct list_head *head = &info->space_info; |
| 3461 | struct btrfs_space_info *found; |
| 3462 | |
| 3463 | rcu_read_lock(); |
| 3464 | list_for_each_entry_rcu(found, head, list) { |
| 3465 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3466 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3467 | } |
| 3468 | rcu_read_unlock(); |
| 3469 | } |
| 3470 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3471 | static int should_alloc_chunk(struct btrfs_root *root, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3472 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3473 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3474 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3475 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3476 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3477 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3478 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3479 | if (force == CHUNK_ALLOC_FORCE) |
| 3480 | return 1; |
| 3481 | |
| 3482 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3483 | * We need to take into account the global rsv because for all intents |
| 3484 | * and purposes it's used space. Don't worry about locking the |
| 3485 | * global_rsv, it doesn't change except when the transaction commits. |
| 3486 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 3487 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3488 | num_allocated += global_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3489 | |
| 3490 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3491 | * in limited mode, we want to have some free space up to |
| 3492 | * about 1% of the FS size. |
| 3493 | */ |
| 3494 | if (force == CHUNK_ALLOC_LIMITED) { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3495 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3496 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3497 | div_factor_fine(thresh, 1)); |
| 3498 | |
| 3499 | if (num_bytes - num_allocated < thresh) |
| 3500 | return 1; |
| 3501 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3502 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3503 | if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3504 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3505 | return 1; |
| 3506 | } |
| 3507 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3508 | static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type) |
| 3509 | { |
| 3510 | u64 num_dev; |
| 3511 | |
| 3512 | if (type & BTRFS_BLOCK_GROUP_RAID10 || |
| 3513 | type & BTRFS_BLOCK_GROUP_RAID0) |
| 3514 | num_dev = root->fs_info->fs_devices->rw_devices; |
| 3515 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 3516 | num_dev = 2; |
| 3517 | else |
| 3518 | num_dev = 1; /* DUP or single */ |
| 3519 | |
| 3520 | /* metadata for updaing devices and chunk tree */ |
| 3521 | return btrfs_calc_trans_metadata_size(root, num_dev + 1); |
| 3522 | } |
| 3523 | |
| 3524 | static void check_system_chunk(struct btrfs_trans_handle *trans, |
| 3525 | struct btrfs_root *root, u64 type) |
| 3526 | { |
| 3527 | struct btrfs_space_info *info; |
| 3528 | u64 left; |
| 3529 | u64 thresh; |
| 3530 | |
| 3531 | info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3532 | spin_lock(&info->lock); |
| 3533 | left = info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 3534 | info->bytes_reserved - info->bytes_readonly; |
| 3535 | spin_unlock(&info->lock); |
| 3536 | |
| 3537 | thresh = get_system_chunk_thresh(root, type); |
| 3538 | if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 3539 | printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n", |
| 3540 | left, thresh, type); |
| 3541 | dump_space_info(info, 0, 0); |
| 3542 | } |
| 3543 | |
| 3544 | if (left < thresh) { |
| 3545 | u64 flags; |
| 3546 | |
| 3547 | flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0); |
| 3548 | btrfs_alloc_chunk(trans, root, flags); |
| 3549 | } |
| 3550 | } |
| 3551 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3552 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3553 | struct btrfs_root *extent_root, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3554 | { |
| 3555 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3556 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3557 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3558 | int ret = 0; |
| 3559 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3560 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3561 | if (!space_info) { |
| 3562 | ret = update_space_info(extent_root->fs_info, flags, |
| 3563 | 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3564 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3565 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3566 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3567 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3568 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3569 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 3570 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3571 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3572 | if (space_info->full) { |
| 3573 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3574 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3575 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3576 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3577 | if (!should_alloc_chunk(extent_root, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3578 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3579 | return 0; |
| 3580 | } else if (space_info->chunk_alloc) { |
| 3581 | wait_for_alloc = 1; |
| 3582 | } else { |
| 3583 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3584 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3585 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3586 | spin_unlock(&space_info->lock); |
| 3587 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3588 | mutex_lock(&fs_info->chunk_mutex); |
| 3589 | |
| 3590 | /* |
| 3591 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3592 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3593 | * other guy is done and we need to recheck and see if we should |
| 3594 | * allocate. |
| 3595 | */ |
| 3596 | if (wait_for_alloc) { |
| 3597 | mutex_unlock(&fs_info->chunk_mutex); |
| 3598 | wait_for_alloc = 0; |
| 3599 | goto again; |
| 3600 | } |
| 3601 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3602 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3603 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3604 | * allocating mixed chunks instead of individual chunks. |
| 3605 | */ |
| 3606 | if (btrfs_mixed_space_info(space_info)) |
| 3607 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3608 | |
| 3609 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3610 | * if we're doing a data chunk, go ahead and make sure that |
| 3611 | * we keep a reasonable number of metadata chunks allocated in the |
| 3612 | * FS as well. |
| 3613 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3614 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3615 | fs_info->data_chunk_allocations++; |
| 3616 | if (!(fs_info->data_chunk_allocations % |
| 3617 | fs_info->metadata_ratio)) |
| 3618 | force_metadata_allocation(fs_info); |
| 3619 | } |
| 3620 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3621 | /* |
| 3622 | * Check if we have enough space in SYSTEM chunk because we may need |
| 3623 | * to update devices. |
| 3624 | */ |
| 3625 | check_system_chunk(trans, extent_root, flags); |
| 3626 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3627 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3628 | if (ret < 0 && ret != -ENOSPC) |
| 3629 | goto out; |
| 3630 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3631 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3632 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3633 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3634 | else |
| 3635 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3636 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3637 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3638 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3639 | spin_unlock(&space_info->lock); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3640 | out: |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 3641 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3642 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3643 | } |
| 3644 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3645 | static int can_overcommit(struct btrfs_root *root, |
| 3646 | struct btrfs_space_info *space_info, u64 bytes, |
| 3647 | int flush) |
| 3648 | { |
| 3649 | u64 profile = btrfs_get_alloc_profile(root, 0); |
| 3650 | u64 avail; |
| 3651 | u64 used; |
| 3652 | |
| 3653 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 3654 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3655 | space_info->bytes_may_use; |
| 3656 | |
| 3657 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3658 | avail = root->fs_info->free_chunk_space; |
| 3659 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3660 | |
| 3661 | /* |
| 3662 | * If we have dup, raid1 or raid10 then only half of the free |
| 3663 | * space is actually useable. |
| 3664 | */ |
| 3665 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 3666 | BTRFS_BLOCK_GROUP_RAID1 | |
| 3667 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3668 | avail >>= 1; |
| 3669 | |
| 3670 | /* |
| 3671 | * If we aren't flushing don't let us overcommit too much, say |
| 3672 | * 1/8th of the space. If we can flush, let it overcommit up to |
| 3673 | * 1/2 of the space. |
| 3674 | */ |
| 3675 | if (flush) |
| 3676 | avail >>= 3; |
| 3677 | else |
| 3678 | avail >>= 1; |
| 3679 | |
| 3680 | if (used + bytes < space_info->total_bytes + avail) |
| 3681 | return 1; |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3685 | /* |
| 3686 | * shrink metadata reservation for delalloc |
| 3687 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3688 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 3689 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3690 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3691 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3692 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3693 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3694 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3695 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3696 | long time_left; |
Josef Bacik | 877da17 | 2011-10-14 14:02:10 -0400 | [diff] [blame] | 3697 | unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3698 | int loops = 0; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3699 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3700 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3701 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3702 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3703 | |
| 3704 | smp_mb(); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3705 | delalloc_bytes = root->fs_info->delalloc_bytes; |
| 3706 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3707 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3708 | return; |
Liu Bo | 6bbe3a9 | 2012-09-14 02:58:07 -0600 | [diff] [blame] | 3709 | btrfs_wait_ordered_extents(root, 0); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3710 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3711 | } |
| 3712 | |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3713 | while (delalloc_bytes && loops < 3) { |
| 3714 | max_reclaim = min(delalloc_bytes, to_reclaim); |
| 3715 | nr_pages = max_reclaim >> PAGE_CACHE_SHIFT; |
Curt Wohlgemuth | 0e175a1 | 2011-10-07 21:54:10 -0600 | [diff] [blame] | 3716 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages, |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3717 | WB_REASON_FS_FREE_SPACE); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3718 | |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 3719 | /* |
| 3720 | * We need to wait for the async pages to actually start before |
| 3721 | * we do anything. |
| 3722 | */ |
| 3723 | wait_event(root->fs_info->async_submit_wait, |
| 3724 | !atomic_read(&root->fs_info->async_delalloc_pages)); |
| 3725 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3726 | spin_lock(&space_info->lock); |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 3727 | if (can_overcommit(root, space_info, orig, !trans)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3728 | spin_unlock(&space_info->lock); |
| 3729 | break; |
| 3730 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3731 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3732 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3733 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3734 | if (wait_ordered && !trans) { |
Liu Bo | 6bbe3a9 | 2012-09-14 02:58:07 -0600 | [diff] [blame] | 3735 | btrfs_wait_ordered_extents(root, 0); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3736 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3737 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3738 | if (time_left) |
| 3739 | break; |
| 3740 | } |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3741 | smp_mb(); |
| 3742 | delalloc_bytes = root->fs_info->delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3743 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3744 | } |
| 3745 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3746 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3747 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 3748 | * @root - the root we're allocating for |
| 3749 | * @bytes - the number of bytes we want to reserve |
| 3750 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3751 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3752 | * This will check to make sure that committing the transaction will actually |
| 3753 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 3754 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3755 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3756 | static int may_commit_transaction(struct btrfs_root *root, |
| 3757 | struct btrfs_space_info *space_info, |
| 3758 | u64 bytes, int force) |
| 3759 | { |
| 3760 | struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; |
| 3761 | struct btrfs_trans_handle *trans; |
| 3762 | |
| 3763 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 3764 | if (trans) |
| 3765 | return -EAGAIN; |
| 3766 | |
| 3767 | if (force) |
| 3768 | goto commit; |
| 3769 | |
| 3770 | /* See if there is enough pinned space to make this reservation */ |
| 3771 | spin_lock(&space_info->lock); |
| 3772 | if (space_info->bytes_pinned >= bytes) { |
| 3773 | spin_unlock(&space_info->lock); |
| 3774 | goto commit; |
| 3775 | } |
| 3776 | spin_unlock(&space_info->lock); |
| 3777 | |
| 3778 | /* |
| 3779 | * See if there is some space in the delayed insertion reservation for |
| 3780 | * this reservation. |
| 3781 | */ |
| 3782 | if (space_info != delayed_rsv->space_info) |
| 3783 | return -ENOSPC; |
| 3784 | |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 3785 | spin_lock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3786 | spin_lock(&delayed_rsv->lock); |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 3787 | if (space_info->bytes_pinned + delayed_rsv->size < bytes) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3788 | spin_unlock(&delayed_rsv->lock); |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 3789 | spin_unlock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3790 | return -ENOSPC; |
| 3791 | } |
| 3792 | spin_unlock(&delayed_rsv->lock); |
Liu Bo | d9b0218 | 2012-02-16 18:34:39 +0800 | [diff] [blame] | 3793 | spin_unlock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3794 | |
| 3795 | commit: |
| 3796 | trans = btrfs_join_transaction(root); |
| 3797 | if (IS_ERR(trans)) |
| 3798 | return -ENOSPC; |
| 3799 | |
| 3800 | return btrfs_commit_transaction(trans, root); |
| 3801 | } |
| 3802 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3803 | enum flush_state { |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 3804 | FLUSH_DELAYED_ITEMS_NR = 1, |
| 3805 | FLUSH_DELAYED_ITEMS = 2, |
| 3806 | FLUSH_DELALLOC = 3, |
| 3807 | FLUSH_DELALLOC_WAIT = 4, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 3808 | ALLOC_CHUNK = 5, |
| 3809 | COMMIT_TRANS = 6, |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3810 | }; |
| 3811 | |
| 3812 | static int flush_space(struct btrfs_root *root, |
| 3813 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 3814 | u64 orig_bytes, int state) |
| 3815 | { |
| 3816 | struct btrfs_trans_handle *trans; |
| 3817 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 3818 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3819 | |
| 3820 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3821 | case FLUSH_DELAYED_ITEMS_NR: |
| 3822 | case FLUSH_DELAYED_ITEMS: |
| 3823 | if (state == FLUSH_DELAYED_ITEMS_NR) { |
| 3824 | u64 bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 3825 | |
| 3826 | nr = (int)div64_u64(num_bytes, bytes); |
| 3827 | if (!nr) |
| 3828 | nr = 1; |
| 3829 | nr *= 2; |
| 3830 | } else { |
| 3831 | nr = -1; |
| 3832 | } |
| 3833 | trans = btrfs_join_transaction(root); |
| 3834 | if (IS_ERR(trans)) { |
| 3835 | ret = PTR_ERR(trans); |
| 3836 | break; |
| 3837 | } |
| 3838 | ret = btrfs_run_delayed_items_nr(trans, root, nr); |
| 3839 | btrfs_end_transaction(trans, root); |
| 3840 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 3841 | case FLUSH_DELALLOC: |
| 3842 | case FLUSH_DELALLOC_WAIT: |
| 3843 | shrink_delalloc(root, num_bytes, orig_bytes, |
| 3844 | state == FLUSH_DELALLOC_WAIT); |
| 3845 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 3846 | case ALLOC_CHUNK: |
| 3847 | trans = btrfs_join_transaction(root); |
| 3848 | if (IS_ERR(trans)) { |
| 3849 | ret = PTR_ERR(trans); |
| 3850 | break; |
| 3851 | } |
| 3852 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 3853 | btrfs_get_alloc_profile(root, 0), |
| 3854 | CHUNK_ALLOC_NO_FORCE); |
| 3855 | btrfs_end_transaction(trans, root); |
| 3856 | if (ret == -ENOSPC) |
| 3857 | ret = 0; |
| 3858 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3859 | case COMMIT_TRANS: |
| 3860 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
| 3861 | break; |
| 3862 | default: |
| 3863 | ret = -ENOSPC; |
| 3864 | break; |
| 3865 | } |
| 3866 | |
| 3867 | return ret; |
| 3868 | } |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3869 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3870 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 3871 | * @root - the root we're allocating for |
| 3872 | * @block_rsv - the block_rsv we're allocating for |
| 3873 | * @orig_bytes - the number of bytes we want |
| 3874 | * @flush - wether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3875 | * |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3876 | * This will reserve orgi_bytes number of bytes from the space info associated |
| 3877 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 3878 | * flush out space to make room. It will do this by flushing delalloc if |
| 3879 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 3880 | * regain reservations will be made and this will fail if there is not enough |
| 3881 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3882 | */ |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3883 | static int reserve_metadata_bytes(struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3884 | struct btrfs_block_rsv *block_rsv, |
| 3885 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3886 | { |
| 3887 | struct btrfs_space_info *space_info = block_rsv->space_info; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3888 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3889 | u64 num_bytes = orig_bytes; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 3890 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3891 | int ret = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3892 | bool flushing = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3893 | |
| 3894 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3895 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3896 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3897 | /* |
| 3898 | * We only want to wait if somebody other than us is flushing and we are |
| 3899 | * actually alloed to flush. |
| 3900 | */ |
| 3901 | while (flush && !flushing && space_info->flush) { |
| 3902 | spin_unlock(&space_info->lock); |
| 3903 | /* |
| 3904 | * If we have a trans handle we can't wait because the flusher |
| 3905 | * may have to commit the transaction, which would mean we would |
| 3906 | * deadlock since we are waiting for the flusher to finish, but |
| 3907 | * hold the current transaction open. |
| 3908 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3909 | if (current->journal_info) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3910 | return -EAGAIN; |
Arne Jansen | b9688bb | 2012-04-18 10:27:16 +0200 | [diff] [blame] | 3911 | ret = wait_event_killable(space_info->wait, !space_info->flush); |
| 3912 | /* Must have been killed, return */ |
| 3913 | if (ret) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3914 | return -EINTR; |
| 3915 | |
| 3916 | spin_lock(&space_info->lock); |
| 3917 | } |
| 3918 | |
| 3919 | ret = -ENOSPC; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3920 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 3921 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3922 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3923 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3924 | /* |
| 3925 | * The idea here is that we've not already over-reserved the block group |
| 3926 | * then we can go ahead and save our reservation first and then start |
| 3927 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3928 | * lets start flushing stuff first and then come back and try to make |
| 3929 | * our reservation. |
| 3930 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3931 | if (used <= space_info->total_bytes) { |
| 3932 | if (used + orig_bytes <= space_info->total_bytes) { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3933 | space_info->bytes_may_use += orig_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3934 | trace_btrfs_space_reservation(root->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3935 | "space_info", space_info->flags, orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3936 | ret = 0; |
| 3937 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3938 | /* |
| 3939 | * Ok set num_bytes to orig_bytes since we aren't |
| 3940 | * overocmmitted, this way we only try and reclaim what |
| 3941 | * we need. |
| 3942 | */ |
| 3943 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3944 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3945 | } else { |
| 3946 | /* |
| 3947 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3948 | * amount plus the amount of bytes that we need for this |
| 3949 | * reservation. |
| 3950 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3951 | num_bytes = used - space_info->total_bytes + |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3952 | (orig_bytes * 2); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3953 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3954 | |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 3955 | if (ret && can_overcommit(root, space_info, orig_bytes, flush)) { |
| 3956 | space_info->bytes_may_use += orig_bytes; |
| 3957 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
| 3958 | space_info->flags, orig_bytes, |
| 3959 | 1); |
| 3960 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3961 | } |
| 3962 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3963 | /* |
| 3964 | * Couldn't make our reservation, save our place so while we're trying |
| 3965 | * to reclaim space we can actually use it instead of somebody else |
| 3966 | * stealing it from us. |
| 3967 | */ |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3968 | if (ret && flush) { |
| 3969 | flushing = true; |
| 3970 | space_info->flush = 1; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3971 | } |
| 3972 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3973 | spin_unlock(&space_info->lock); |
| 3974 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3975 | if (!ret || !flush) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3976 | goto out; |
| 3977 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3978 | ret = flush_space(root, space_info, num_bytes, orig_bytes, |
| 3979 | flush_state); |
| 3980 | flush_state++; |
| 3981 | if (!ret) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3982 | goto again; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 3983 | else if (flush_state <= COMMIT_TRANS) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3984 | goto again; |
| 3985 | |
| 3986 | out: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3987 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3988 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3989 | space_info->flush = 0; |
| 3990 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3991 | spin_unlock(&space_info->lock); |
| 3992 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3993 | return ret; |
| 3994 | } |
| 3995 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3996 | static struct btrfs_block_rsv *get_block_rsv( |
| 3997 | const struct btrfs_trans_handle *trans, |
| 3998 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3999 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4000 | struct btrfs_block_rsv *block_rsv = NULL; |
| 4001 | |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 4002 | if (root->ref_cows) |
| 4003 | block_rsv = trans->block_rsv; |
| 4004 | |
| 4005 | if (root == root->fs_info->csum_root && trans->adding_csums) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4006 | block_rsv = trans->block_rsv; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4007 | |
| 4008 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4009 | block_rsv = root->block_rsv; |
| 4010 | |
| 4011 | if (!block_rsv) |
| 4012 | block_rsv = &root->fs_info->empty_block_rsv; |
| 4013 | |
| 4014 | return block_rsv; |
| 4015 | } |
| 4016 | |
| 4017 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 4018 | u64 num_bytes) |
| 4019 | { |
| 4020 | int ret = -ENOSPC; |
| 4021 | spin_lock(&block_rsv->lock); |
| 4022 | if (block_rsv->reserved >= num_bytes) { |
| 4023 | block_rsv->reserved -= num_bytes; |
| 4024 | if (block_rsv->reserved < block_rsv->size) |
| 4025 | block_rsv->full = 0; |
| 4026 | ret = 0; |
| 4027 | } |
| 4028 | spin_unlock(&block_rsv->lock); |
| 4029 | return ret; |
| 4030 | } |
| 4031 | |
| 4032 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 4033 | u64 num_bytes, int update_size) |
| 4034 | { |
| 4035 | spin_lock(&block_rsv->lock); |
| 4036 | block_rsv->reserved += num_bytes; |
| 4037 | if (update_size) |
| 4038 | block_rsv->size += num_bytes; |
| 4039 | else if (block_rsv->reserved >= block_rsv->size) |
| 4040 | block_rsv->full = 1; |
| 4041 | spin_unlock(&block_rsv->lock); |
| 4042 | } |
| 4043 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4044 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 4045 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 4046 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4047 | { |
| 4048 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 4049 | |
| 4050 | spin_lock(&block_rsv->lock); |
| 4051 | if (num_bytes == (u64)-1) |
| 4052 | num_bytes = block_rsv->size; |
| 4053 | block_rsv->size -= num_bytes; |
| 4054 | if (block_rsv->reserved >= block_rsv->size) { |
| 4055 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 4056 | block_rsv->reserved = block_rsv->size; |
| 4057 | block_rsv->full = 1; |
| 4058 | } else { |
| 4059 | num_bytes = 0; |
| 4060 | } |
| 4061 | spin_unlock(&block_rsv->lock); |
| 4062 | |
| 4063 | if (num_bytes > 0) { |
| 4064 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 4065 | spin_lock(&dest->lock); |
| 4066 | if (!dest->full) { |
| 4067 | u64 bytes_to_add; |
| 4068 | |
| 4069 | bytes_to_add = dest->size - dest->reserved; |
| 4070 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 4071 | dest->reserved += bytes_to_add; |
| 4072 | if (dest->reserved >= dest->size) |
| 4073 | dest->full = 1; |
| 4074 | num_bytes -= bytes_to_add; |
| 4075 | } |
| 4076 | spin_unlock(&dest->lock); |
| 4077 | } |
| 4078 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4079 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4080 | space_info->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4081 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4082 | space_info->flags, num_bytes, 0); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4083 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4084 | spin_unlock(&space_info->lock); |
| 4085 | } |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 4090 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 4091 | { |
| 4092 | int ret; |
| 4093 | |
| 4094 | ret = block_rsv_use_bytes(src, num_bytes); |
| 4095 | if (ret) |
| 4096 | return ret; |
| 4097 | |
| 4098 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 4099 | return 0; |
| 4100 | } |
| 4101 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4102 | 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] | 4103 | { |
| 4104 | memset(rsv, 0, sizeof(*rsv)); |
| 4105 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4106 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4107 | } |
| 4108 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4109 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, |
| 4110 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4111 | { |
| 4112 | struct btrfs_block_rsv *block_rsv; |
| 4113 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4114 | |
| 4115 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 4116 | if (!block_rsv) |
| 4117 | return NULL; |
| 4118 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4119 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4120 | block_rsv->space_info = __find_space_info(fs_info, |
| 4121 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4122 | return block_rsv; |
| 4123 | } |
| 4124 | |
| 4125 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 4126 | struct btrfs_block_rsv *rsv) |
| 4127 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4128 | if (!rsv) |
| 4129 | return; |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 4130 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 4131 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4132 | } |
| 4133 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 4134 | static inline int __block_rsv_add(struct btrfs_root *root, |
| 4135 | struct btrfs_block_rsv *block_rsv, |
| 4136 | u64 num_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4137 | { |
| 4138 | int ret; |
| 4139 | |
| 4140 | if (num_bytes == 0) |
| 4141 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4142 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 4143 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4144 | if (!ret) { |
| 4145 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 4146 | return 0; |
| 4147 | } |
| 4148 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4149 | return ret; |
| 4150 | } |
| 4151 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 4152 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 4153 | struct btrfs_block_rsv *block_rsv, |
| 4154 | u64 num_bytes) |
| 4155 | { |
| 4156 | return __block_rsv_add(root, block_rsv, num_bytes, 1); |
| 4157 | } |
| 4158 | |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 4159 | int btrfs_block_rsv_add_noflush(struct btrfs_root *root, |
| 4160 | struct btrfs_block_rsv *block_rsv, |
| 4161 | u64 num_bytes) |
| 4162 | { |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 4163 | return __block_rsv_add(root, block_rsv, num_bytes, 0); |
Josef Bacik | c06a0e1 | 2011-11-04 19:56:02 -0400 | [diff] [blame] | 4164 | } |
| 4165 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4166 | int btrfs_block_rsv_check(struct btrfs_root *root, |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4167 | struct btrfs_block_rsv *block_rsv, int min_factor) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4168 | { |
| 4169 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4170 | int ret = -ENOSPC; |
| 4171 | |
| 4172 | if (!block_rsv) |
| 4173 | return 0; |
| 4174 | |
| 4175 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4176 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 4177 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4178 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4179 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4180 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4181 | return ret; |
| 4182 | } |
| 4183 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 4184 | static inline int __btrfs_block_rsv_refill(struct btrfs_root *root, |
| 4185 | struct btrfs_block_rsv *block_rsv, |
| 4186 | u64 min_reserved, int flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4187 | { |
| 4188 | u64 num_bytes = 0; |
| 4189 | int ret = -ENOSPC; |
| 4190 | |
| 4191 | if (!block_rsv) |
| 4192 | return 0; |
| 4193 | |
| 4194 | spin_lock(&block_rsv->lock); |
| 4195 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4196 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4197 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4198 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4199 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4200 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4201 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4202 | if (!ret) |
| 4203 | return 0; |
| 4204 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 4205 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 4206 | if (!ret) { |
| 4207 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4208 | return 0; |
| 4209 | } |
| 4210 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4211 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4212 | } |
| 4213 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 4214 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 4215 | struct btrfs_block_rsv *block_rsv, |
| 4216 | u64 min_reserved) |
| 4217 | { |
| 4218 | return __btrfs_block_rsv_refill(root, block_rsv, min_reserved, 1); |
| 4219 | } |
| 4220 | |
| 4221 | int btrfs_block_rsv_refill_noflush(struct btrfs_root *root, |
| 4222 | struct btrfs_block_rsv *block_rsv, |
| 4223 | u64 min_reserved) |
| 4224 | { |
| 4225 | return __btrfs_block_rsv_refill(root, block_rsv, min_reserved, 0); |
| 4226 | } |
| 4227 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4228 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 4229 | struct btrfs_block_rsv *dst_rsv, |
| 4230 | u64 num_bytes) |
| 4231 | { |
| 4232 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4233 | } |
| 4234 | |
| 4235 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 4236 | struct btrfs_block_rsv *block_rsv, |
| 4237 | u64 num_bytes) |
| 4238 | { |
| 4239 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 4240 | if (global_rsv->full || global_rsv == block_rsv || |
| 4241 | block_rsv->space_info != global_rsv->space_info) |
| 4242 | global_rsv = NULL; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4243 | block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv, |
| 4244 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4245 | } |
| 4246 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4247 | /* |
| 4248 | * helper to calculate size of global block reservation. |
| 4249 | * the desired value is sum of space used by extent tree, |
| 4250 | * checksum tree and root tree |
| 4251 | */ |
| 4252 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 4253 | { |
| 4254 | struct btrfs_space_info *sinfo; |
| 4255 | u64 num_bytes; |
| 4256 | u64 meta_used; |
| 4257 | u64 data_used; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4258 | int csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4259 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4260 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 4261 | spin_lock(&sinfo->lock); |
| 4262 | data_used = sinfo->bytes_used; |
| 4263 | spin_unlock(&sinfo->lock); |
| 4264 | |
| 4265 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4266 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 4267 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 4268 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4269 | meta_used = sinfo->bytes_used; |
| 4270 | spin_unlock(&sinfo->lock); |
| 4271 | |
| 4272 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 4273 | csum_size * 2; |
| 4274 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 4275 | |
| 4276 | if (num_bytes * 3 > meta_used) |
Chris Mason | 8e62c2d | 2012-04-12 13:46:48 -0400 | [diff] [blame] | 4277 | num_bytes = div64_u64(meta_used, 3); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4278 | |
| 4279 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 4280 | } |
| 4281 | |
| 4282 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4283 | { |
| 4284 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 4285 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 4286 | u64 num_bytes; |
| 4287 | |
| 4288 | num_bytes = calc_global_metadata_size(fs_info); |
| 4289 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4290 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 4291 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4292 | |
| 4293 | block_rsv->size = num_bytes; |
| 4294 | |
| 4295 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 4296 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 4297 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4298 | |
| 4299 | if (sinfo->total_bytes > num_bytes) { |
| 4300 | num_bytes = sinfo->total_bytes - num_bytes; |
| 4301 | block_rsv->reserved += num_bytes; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4302 | sinfo->bytes_may_use += num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4303 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4304 | sinfo->flags, num_bytes, 1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | if (block_rsv->reserved >= block_rsv->size) { |
| 4308 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4309 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4310 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4311 | sinfo->flags, num_bytes, 0); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4312 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4313 | block_rsv->reserved = block_rsv->size; |
| 4314 | block_rsv->full = 1; |
| 4315 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 4316 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4317 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 4318 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4319 | } |
| 4320 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4321 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4322 | { |
| 4323 | struct btrfs_space_info *space_info; |
| 4324 | |
| 4325 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 4326 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4327 | |
| 4328 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4329 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4330 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4331 | fs_info->trans_block_rsv.space_info = space_info; |
| 4332 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4333 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4334 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4335 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 4336 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 4337 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 4338 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4339 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4340 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4341 | update_global_block_rsv(fs_info); |
| 4342 | } |
| 4343 | |
| 4344 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4345 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4346 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 4347 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4348 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 4349 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 4350 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 4351 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 4352 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 4353 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4354 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 4355 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4356 | } |
| 4357 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4358 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 4359 | struct btrfs_root *root) |
| 4360 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 4361 | if (!trans->block_rsv) |
| 4362 | return; |
| 4363 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4364 | if (!trans->bytes_reserved) |
| 4365 | return; |
| 4366 | |
Chris Mason | e77266e | 2012-02-24 10:39:05 -0500 | [diff] [blame] | 4367 | trace_btrfs_space_reservation(root->fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4368 | trans->transid, trans->bytes_reserved, 0); |
Josef Bacik | b24e03d | 2011-10-14 14:40:17 -0400 | [diff] [blame] | 4369 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4370 | trans->bytes_reserved = 0; |
| 4371 | } |
| 4372 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4373 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4374 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4375 | struct inode *inode) |
| 4376 | { |
| 4377 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4378 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4379 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 4380 | |
| 4381 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4382 | * We need to hold space in order to delete our orphan item once we've |
| 4383 | * added it, so this takes the reservation so we can release it later |
| 4384 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4385 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4386 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4387 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 4388 | btrfs_ino(inode), num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4389 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4390 | } |
| 4391 | |
| 4392 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 4393 | { |
| 4394 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4395 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4396 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 4397 | btrfs_ino(inode), num_bytes, 0); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4398 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 4399 | } |
| 4400 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4401 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4402 | struct btrfs_pending_snapshot *pending) |
| 4403 | { |
| 4404 | struct btrfs_root *root = pending->root; |
| 4405 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4406 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 4407 | /* |
Miao Xie | 48c03c4 | 2012-09-06 04:03:56 -0600 | [diff] [blame] | 4408 | * two for root back/forward refs, two for directory entries, |
| 4409 | * one for root of the snapshot and one for parent inode. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4410 | */ |
Miao Xie | 48c03c4 | 2012-09-06 04:03:56 -0600 | [diff] [blame] | 4411 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4412 | dst_rsv->space_info = src_rsv->space_info; |
| 4413 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4414 | } |
| 4415 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4416 | /** |
| 4417 | * drop_outstanding_extent - drop an outstanding extent |
| 4418 | * @inode: the inode we're dropping the extent for |
| 4419 | * |
| 4420 | * This is called when we are freeing up an outstanding extent, either called |
| 4421 | * after an error or after an extent is written. This will return the number of |
| 4422 | * reserved extents that need to be freed. This must be called with |
| 4423 | * BTRFS_I(inode)->lock held. |
| 4424 | */ |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4425 | static unsigned drop_outstanding_extent(struct inode *inode) |
| 4426 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4427 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4428 | unsigned dropped_extents = 0; |
| 4429 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4430 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
| 4431 | BTRFS_I(inode)->outstanding_extents--; |
| 4432 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4433 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4434 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 4435 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4436 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4437 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4438 | /* |
| 4439 | * If we have more or the same amount of outsanding extents than we have |
| 4440 | * reserved then we need to leave the reserved extents count alone. |
| 4441 | */ |
| 4442 | if (BTRFS_I(inode)->outstanding_extents >= |
| 4443 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4444 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4445 | |
| 4446 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 4447 | BTRFS_I(inode)->outstanding_extents; |
| 4448 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4449 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4452 | /** |
| 4453 | * calc_csum_metadata_size - return the amount of metada space that must be |
| 4454 | * reserved/free'd for the given bytes. |
| 4455 | * @inode: the inode we're manipulating |
| 4456 | * @num_bytes: the number of bytes in question |
| 4457 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 4458 | * |
| 4459 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 4460 | * correct amount of metadata that must either be reserved or freed. We |
| 4461 | * calculate how many checksums we can fit into one leaf and then divide the |
| 4462 | * number of bytes that will need to be checksumed by this value to figure out |
| 4463 | * how many checksums will be required. If we are adding bytes then the number |
| 4464 | * may go up and we will return the number of additional bytes that must be |
| 4465 | * reserved. If it is going down we will return the number of bytes that must |
| 4466 | * be freed. |
| 4467 | * |
| 4468 | * This must be called with BTRFS_I(inode)->lock held. |
| 4469 | */ |
| 4470 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 4471 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4472 | { |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4473 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4474 | u64 csum_size; |
| 4475 | int num_csums_per_leaf; |
| 4476 | int num_csums; |
| 4477 | int old_csums; |
| 4478 | |
| 4479 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 4480 | BTRFS_I(inode)->csum_bytes == 0) |
| 4481 | return 0; |
| 4482 | |
| 4483 | old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 4484 | if (reserve) |
| 4485 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 4486 | else |
| 4487 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
| 4488 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); |
| 4489 | num_csums_per_leaf = (int)div64_u64(csum_size, |
| 4490 | sizeof(struct btrfs_csum_item) + |
| 4491 | sizeof(struct btrfs_disk_key)); |
| 4492 | num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 4493 | num_csums = num_csums + num_csums_per_leaf - 1; |
| 4494 | num_csums = num_csums / num_csums_per_leaf; |
| 4495 | |
| 4496 | old_csums = old_csums + num_csums_per_leaf - 1; |
| 4497 | old_csums = old_csums / num_csums_per_leaf; |
| 4498 | |
| 4499 | /* No change, no need to reserve more */ |
| 4500 | if (old_csums == num_csums) |
| 4501 | return 0; |
| 4502 | |
| 4503 | if (reserve) |
| 4504 | return btrfs_calc_trans_metadata_size(root, |
| 4505 | num_csums - old_csums); |
| 4506 | |
| 4507 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 4511 | { |
| 4512 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4513 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4514 | u64 to_reserve = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4515 | u64 csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4516 | unsigned nr_extents = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4517 | int extra_reserve = 0; |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4518 | int flush = 1; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4519 | int ret; |
| 4520 | |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4521 | /* Need to be holding the i_mutex here if we aren't free space cache */ |
Liu Bo | 83eea1f | 2012-07-10 05:28:39 -0600 | [diff] [blame] | 4522 | if (btrfs_is_free_space_inode(inode)) |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4523 | flush = 0; |
| 4524 | |
| 4525 | if (flush && btrfs_transaction_in_commit(root->fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4526 | schedule_timeout(1); |
| 4527 | |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 4528 | mutex_lock(&BTRFS_I(inode)->delalloc_mutex); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4529 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4530 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4531 | spin_lock(&BTRFS_I(inode)->lock); |
| 4532 | BTRFS_I(inode)->outstanding_extents++; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4533 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4534 | if (BTRFS_I(inode)->outstanding_extents > |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4535 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4536 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 4537 | BTRFS_I(inode)->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4538 | |
| 4539 | /* |
| 4540 | * Add an item to reserve for updating the inode when we complete the |
| 4541 | * delalloc io. |
| 4542 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4543 | if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 4544 | &BTRFS_I(inode)->runtime_flags)) { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4545 | nr_extents++; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4546 | extra_reserve = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4550 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4551 | csum_bytes = BTRFS_I(inode)->csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4552 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4553 | |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 4554 | if (root->fs_info->quota_enabled) { |
| 4555 | ret = btrfs_qgroup_reserve(root, num_bytes + |
| 4556 | nr_extents * root->leafsize); |
Dan Carpenter | 55e591f | 2012-07-30 02:15:15 -0600 | [diff] [blame] | 4557 | if (ret) { |
| 4558 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 4559 | return ret; |
Dan Carpenter | 55e591f | 2012-07-30 02:15:15 -0600 | [diff] [blame] | 4560 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 4561 | } |
| 4562 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4563 | ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4564 | if (ret) { |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4565 | u64 to_free = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4566 | unsigned dropped; |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4567 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4568 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4569 | dropped = drop_outstanding_extent(inode); |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4570 | /* |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4571 | * If the inodes csum_bytes is the same as the original |
| 4572 | * csum_bytes then we know we haven't raced with any free()ers |
| 4573 | * so we can just reduce our inodes csum bytes and carry on. |
| 4574 | * Otherwise we have to do the normal free thing to account for |
| 4575 | * the case that the free side didn't free up its reserve |
| 4576 | * because of this outstanding reservation. |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4577 | */ |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4578 | if (BTRFS_I(inode)->csum_bytes == csum_bytes) |
| 4579 | calc_csum_metadata_size(inode, num_bytes, 0); |
| 4580 | else |
| 4581 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
| 4582 | spin_unlock(&BTRFS_I(inode)->lock); |
| 4583 | if (dropped) |
| 4584 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
| 4585 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4586 | if (to_free) { |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4587 | btrfs_block_rsv_release(root, block_rsv, to_free); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4588 | trace_btrfs_space_reservation(root->fs_info, |
| 4589 | "delalloc", |
| 4590 | btrfs_ino(inode), |
| 4591 | to_free, 0); |
| 4592 | } |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 4593 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4594 | return ret; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4595 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4596 | |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4597 | spin_lock(&BTRFS_I(inode)->lock); |
| 4598 | if (extra_reserve) { |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 4599 | set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 4600 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4601 | nr_extents--; |
| 4602 | } |
| 4603 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 4604 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | f248679 | 2012-01-13 12:09:22 -0500 | [diff] [blame] | 4605 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 4606 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4607 | if (to_reserve) |
| 4608 | trace_btrfs_space_reservation(root->fs_info,"delalloc", |
| 4609 | btrfs_ino(inode), to_reserve, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4610 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4611 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4612 | return 0; |
| 4613 | } |
| 4614 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4615 | /** |
| 4616 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 4617 | * @inode: the inode to release the reservation for |
| 4618 | * @num_bytes: the number of bytes we're releasing |
| 4619 | * |
| 4620 | * This will release the metadata reservation for an inode. This can be called |
| 4621 | * once we complete IO for a given set of bytes to release their metadata |
| 4622 | * reservations. |
| 4623 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4624 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4625 | { |
| 4626 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4627 | u64 to_free = 0; |
| 4628 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4629 | |
| 4630 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4631 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4632 | dropped = drop_outstanding_extent(inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4633 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4634 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
| 4635 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4636 | if (dropped > 0) |
| 4637 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4638 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4639 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 4640 | btrfs_ino(inode), to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 4641 | if (root->fs_info->quota_enabled) { |
| 4642 | btrfs_qgroup_free(root, num_bytes + |
| 4643 | dropped * root->leafsize); |
| 4644 | } |
| 4645 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4646 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4647 | to_free); |
| 4648 | } |
| 4649 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4650 | /** |
| 4651 | * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc |
| 4652 | * @inode: inode we're writing to |
| 4653 | * @num_bytes: the number of bytes we want to allocate |
| 4654 | * |
| 4655 | * This will do the following things |
| 4656 | * |
| 4657 | * o reserve space in the data space info for num_bytes |
| 4658 | * o reserve space in the metadata space info based on number of outstanding |
| 4659 | * extents and how much csums will be needed |
| 4660 | * o add to the inodes ->delalloc_bytes |
| 4661 | * o add it to the fs_info's delalloc inodes list. |
| 4662 | * |
| 4663 | * This will return 0 for success and -ENOSPC if there is no space left. |
| 4664 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4665 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4666 | { |
| 4667 | int ret; |
| 4668 | |
| 4669 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4670 | if (ret) |
| 4671 | return ret; |
| 4672 | |
| 4673 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4674 | if (ret) { |
| 4675 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4676 | return ret; |
| 4677 | } |
| 4678 | |
| 4679 | return 0; |
| 4680 | } |
| 4681 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4682 | /** |
| 4683 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
| 4684 | * @inode: inode we're releasing space for |
| 4685 | * @num_bytes: the number of bytes we want to free up |
| 4686 | * |
| 4687 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 4688 | * called in the case that we don't need the metadata AND data reservations |
| 4689 | * anymore. So if there is an error or we insert an inline extent. |
| 4690 | * |
| 4691 | * This function will release the metadata space that was not used and will |
| 4692 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 4693 | * list if there are no delalloc bytes left. |
| 4694 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4695 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4696 | { |
| 4697 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4698 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4699 | } |
| 4700 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4701 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4702 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4703 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4704 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4705 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4706 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4707 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4708 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4709 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4710 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4711 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4712 | /* block accounting for super block */ |
| 4713 | spin_lock(&info->delalloc_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4714 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4715 | if (alloc) |
| 4716 | old_val += num_bytes; |
| 4717 | else |
| 4718 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4719 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4720 | spin_unlock(&info->delalloc_lock); |
| 4721 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4722 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4723 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4724 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4725 | return -ENOENT; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4726 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4727 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4728 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4729 | factor = 2; |
| 4730 | else |
| 4731 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4732 | /* |
| 4733 | * If this block group has free space cache written out, we |
| 4734 | * need to make sure to load it if we are removing space. This |
| 4735 | * is because we need the unpinning stage to actually add the |
| 4736 | * space back to the block group, otherwise we will leak space. |
| 4737 | */ |
| 4738 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4739 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4740 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4741 | byte_in_group = bytenr - cache->key.objectid; |
| 4742 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4743 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4744 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4745 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4746 | |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 4747 | if (btrfs_test_opt(root, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4748 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4749 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4750 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4751 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4752 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4753 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4754 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4755 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4756 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4757 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4758 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4759 | cache->space_info->bytes_used += num_bytes; |
| 4760 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4761 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4762 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4763 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4764 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4765 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4766 | cache->pinned += num_bytes; |
| 4767 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4768 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4769 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4770 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4771 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4772 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4773 | set_extent_dirty(info->pinned_extents, |
| 4774 | bytenr, bytenr + num_bytes - 1, |
| 4775 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4776 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4777 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4778 | total -= num_bytes; |
| 4779 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4780 | } |
| 4781 | return 0; |
| 4782 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4783 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4784 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4785 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4786 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4787 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4788 | |
| 4789 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4790 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4791 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4792 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4793 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4794 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4795 | |
| 4796 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4797 | } |
| 4798 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4799 | static int pin_down_extent(struct btrfs_root *root, |
| 4800 | struct btrfs_block_group_cache *cache, |
| 4801 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4802 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4803 | spin_lock(&cache->space_info->lock); |
| 4804 | spin_lock(&cache->lock); |
| 4805 | cache->pinned += num_bytes; |
| 4806 | cache->space_info->bytes_pinned += num_bytes; |
| 4807 | if (reserved) { |
| 4808 | cache->reserved -= num_bytes; |
| 4809 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4810 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4811 | spin_unlock(&cache->lock); |
| 4812 | spin_unlock(&cache->space_info->lock); |
| 4813 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4814 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4815 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4816 | return 0; |
| 4817 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4818 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4819 | /* |
| 4820 | * this function must be called within transaction |
| 4821 | */ |
| 4822 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4823 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4824 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4825 | struct btrfs_block_group_cache *cache; |
| 4826 | |
| 4827 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4828 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4829 | |
| 4830 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4831 | |
| 4832 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4833 | return 0; |
| 4834 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4835 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4836 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4837 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4838 | */ |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4839 | int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, |
| 4840 | struct btrfs_root *root, |
| 4841 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4842 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4843 | struct btrfs_block_group_cache *cache; |
| 4844 | |
| 4845 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4846 | BUG_ON(!cache); /* Logic error */ |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4847 | |
| 4848 | /* |
| 4849 | * pull in the free space cache (if any) so that our pin |
| 4850 | * removes the free space from the cache. We have load_only set |
| 4851 | * to one because the slow code to read in the free extents does check |
| 4852 | * the pinned extents. |
| 4853 | */ |
| 4854 | cache_block_group(cache, trans, root, 1); |
| 4855 | |
| 4856 | pin_down_extent(root, cache, bytenr, num_bytes, 0); |
| 4857 | |
| 4858 | /* remove us from the free space cache (if we're there at all) */ |
| 4859 | btrfs_remove_free_space(cache, bytenr, num_bytes); |
| 4860 | btrfs_put_block_group(cache); |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4864 | /** |
| 4865 | * btrfs_update_reserved_bytes - update the block_group and space info counters |
| 4866 | * @cache: The cache we are manipulating |
| 4867 | * @num_bytes: The number of bytes in question |
| 4868 | * @reserve: One of the reservation enums |
| 4869 | * |
| 4870 | * This is called by the allocator when it reserves space, or by somebody who is |
| 4871 | * freeing space that was never actually used on disk. For example if you |
| 4872 | * reserve some space for a new leaf in transaction A and before transaction A |
| 4873 | * commits you free that leaf, you call this with reserve set to 0 in order to |
| 4874 | * clear the reservation. |
| 4875 | * |
| 4876 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper |
| 4877 | * ENOSPC accounting. For data we handle the reservation through clearing the |
| 4878 | * delalloc bits in the io_tree. We have to do this since we could end up |
| 4879 | * allocating less disk space for the amount of data we have reserved in the |
| 4880 | * case of compression. |
| 4881 | * |
| 4882 | * If this is a reservation and the block group has become read only we cannot |
| 4883 | * make the reservation and return -EAGAIN, otherwise this function always |
| 4884 | * succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4885 | */ |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4886 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4887 | u64 num_bytes, int reserve) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4888 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4889 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4890 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4891 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4892 | spin_lock(&space_info->lock); |
| 4893 | spin_lock(&cache->lock); |
| 4894 | if (reserve != RESERVE_FREE) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4895 | if (cache->ro) { |
| 4896 | ret = -EAGAIN; |
| 4897 | } else { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4898 | cache->reserved += num_bytes; |
| 4899 | space_info->bytes_reserved += num_bytes; |
| 4900 | if (reserve == RESERVE_ALLOC) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4901 | trace_btrfs_space_reservation(cache->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4902 | "space_info", space_info->flags, |
| 4903 | num_bytes, 0); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4904 | space_info->bytes_may_use -= num_bytes; |
| 4905 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4906 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4907 | } else { |
| 4908 | if (cache->ro) |
| 4909 | space_info->bytes_readonly += num_bytes; |
| 4910 | cache->reserved -= num_bytes; |
| 4911 | space_info->bytes_reserved -= num_bytes; |
| 4912 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4913 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4914 | spin_unlock(&cache->lock); |
| 4915 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4916 | return ret; |
| 4917 | } |
| 4918 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4919 | void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4920 | struct btrfs_root *root) |
| 4921 | { |
| 4922 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4923 | struct btrfs_caching_control *next; |
| 4924 | struct btrfs_caching_control *caching_ctl; |
| 4925 | struct btrfs_block_group_cache *cache; |
| 4926 | |
| 4927 | down_write(&fs_info->extent_commit_sem); |
| 4928 | |
| 4929 | list_for_each_entry_safe(caching_ctl, next, |
| 4930 | &fs_info->caching_block_groups, list) { |
| 4931 | cache = caching_ctl->block_group; |
| 4932 | if (block_group_cache_done(cache)) { |
| 4933 | cache->last_byte_to_unpin = (u64)-1; |
| 4934 | list_del_init(&caching_ctl->list); |
| 4935 | put_caching_control(caching_ctl); |
| 4936 | } else { |
| 4937 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4938 | } |
| 4939 | } |
| 4940 | |
| 4941 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4942 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4943 | else |
| 4944 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4945 | |
| 4946 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4947 | |
| 4948 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4952 | { |
| 4953 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4954 | struct btrfs_block_group_cache *cache = NULL; |
| 4955 | u64 len; |
| 4956 | |
| 4957 | while (start <= end) { |
| 4958 | if (!cache || |
| 4959 | start >= cache->key.objectid + cache->key.offset) { |
| 4960 | if (cache) |
| 4961 | btrfs_put_block_group(cache); |
| 4962 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4963 | BUG_ON(!cache); /* Logic error */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | len = cache->key.objectid + cache->key.offset - start; |
| 4967 | len = min(len, end + 1 - start); |
| 4968 | |
| 4969 | if (start < cache->last_byte_to_unpin) { |
| 4970 | len = min(len, cache->last_byte_to_unpin - start); |
| 4971 | btrfs_add_free_space(cache, start, len); |
| 4972 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4973 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4974 | start += len; |
| 4975 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4976 | spin_lock(&cache->space_info->lock); |
| 4977 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4978 | cache->pinned -= len; |
| 4979 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4980 | if (cache->ro) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4981 | cache->space_info->bytes_readonly += len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4982 | spin_unlock(&cache->lock); |
| 4983 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4987 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4992 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4993 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4994 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4995 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4996 | u64 start; |
| 4997 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4998 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4999 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5000 | if (trans->aborted) |
| 5001 | return 0; |
| 5002 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5003 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 5004 | unpin = &fs_info->freed_extents[1]; |
| 5005 | else |
| 5006 | unpin = &fs_info->freed_extents[0]; |
| 5007 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5008 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5009 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 5010 | EXTENT_DIRTY, NULL); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5011 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5012 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5013 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5014 | if (btrfs_test_opt(root, DISCARD)) |
| 5015 | ret = btrfs_discard_extent(root, start, |
| 5016 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5017 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5018 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5019 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5020 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5021 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5022 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 5023 | return 0; |
| 5024 | } |
| 5025 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5026 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 5027 | struct btrfs_root *root, |
| 5028 | u64 bytenr, u64 num_bytes, u64 parent, |
| 5029 | u64 root_objectid, u64 owner_objectid, |
| 5030 | u64 owner_offset, int refs_to_drop, |
| 5031 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5032 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5033 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5034 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 5035 | struct btrfs_fs_info *info = root->fs_info; |
| 5036 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5037 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5038 | struct btrfs_extent_item *ei; |
| 5039 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5040 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5041 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5042 | int extent_slot = 0; |
| 5043 | int found_extent = 0; |
| 5044 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5045 | u32 item_size; |
| 5046 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 5047 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5048 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5049 | if (!path) |
| 5050 | return -ENOMEM; |
| 5051 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 5052 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5053 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5054 | |
| 5055 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 5056 | BUG_ON(!is_data && refs_to_drop != 1); |
| 5057 | |
| 5058 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 5059 | bytenr, num_bytes, parent, |
| 5060 | root_objectid, owner_objectid, |
| 5061 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5062 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5063 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5064 | while (extent_slot >= 0) { |
| 5065 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5066 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5067 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5068 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5069 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 5070 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5071 | found_extent = 1; |
| 5072 | break; |
| 5073 | } |
| 5074 | if (path->slots[0] - extent_slot > 5) |
| 5075 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5076 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5077 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5078 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 5079 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 5080 | if (found_extent && item_size < sizeof(*ei)) |
| 5081 | found_extent = 0; |
| 5082 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5083 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5084 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5085 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5086 | NULL, refs_to_drop, |
| 5087 | is_data); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5088 | if (ret) { |
| 5089 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5090 | goto out; |
| 5091 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5092 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5093 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5094 | |
| 5095 | key.objectid = bytenr; |
| 5096 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5097 | key.offset = num_bytes; |
| 5098 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5099 | ret = btrfs_search_slot(trans, extent_root, |
| 5100 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5101 | if (ret) { |
| 5102 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5103 | ", was looking for %llu\n", ret, |
| 5104 | (unsigned long long)bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 5105 | if (ret > 0) |
| 5106 | btrfs_print_leaf(extent_root, |
| 5107 | path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5108 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5109 | if (ret < 0) { |
| 5110 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5111 | goto out; |
| 5112 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5113 | extent_slot = path->slots[0]; |
| 5114 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5115 | } else if (ret == -ENOENT) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5116 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 5117 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5118 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5119 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5120 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5121 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5122 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5123 | (unsigned long long)owner_objectid, |
| 5124 | (unsigned long long)owner_offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5125 | } else { |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5126 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5127 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5128 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5129 | |
| 5130 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5131 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 5132 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 5133 | if (item_size < sizeof(*ei)) { |
| 5134 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 5135 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 5136 | owner_objectid, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5137 | if (ret < 0) { |
| 5138 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5139 | goto out; |
| 5140 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5141 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5142 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5143 | path->leave_spinning = 1; |
| 5144 | |
| 5145 | key.objectid = bytenr; |
| 5146 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5147 | key.offset = num_bytes; |
| 5148 | |
| 5149 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 5150 | -1, 1); |
| 5151 | if (ret) { |
| 5152 | printk(KERN_ERR "umm, got %d back from search" |
| 5153 | ", was looking for %llu\n", ret, |
| 5154 | (unsigned long long)bytenr); |
| 5155 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 5156 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5157 | if (ret < 0) { |
| 5158 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5159 | goto out; |
| 5160 | } |
| 5161 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5162 | extent_slot = path->slots[0]; |
| 5163 | leaf = path->nodes[0]; |
| 5164 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 5165 | } |
| 5166 | #endif |
| 5167 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5168 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 5169 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5170 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5171 | struct btrfs_tree_block_info *bi; |
| 5172 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 5173 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 5174 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5175 | } |
| 5176 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5177 | refs = btrfs_extent_refs(leaf, ei); |
| 5178 | BUG_ON(refs < refs_to_drop); |
| 5179 | refs -= refs_to_drop; |
| 5180 | |
| 5181 | if (refs > 0) { |
| 5182 | if (extent_op) |
| 5183 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 5184 | /* |
| 5185 | * In the case of inline back ref, reference count will |
| 5186 | * be updated by remove_extent_backref |
| 5187 | */ |
| 5188 | if (iref) { |
| 5189 | BUG_ON(!found_extent); |
| 5190 | } else { |
| 5191 | btrfs_set_extent_refs(leaf, ei, refs); |
| 5192 | btrfs_mark_buffer_dirty(leaf); |
| 5193 | } |
| 5194 | if (found_extent) { |
| 5195 | ret = remove_extent_backref(trans, extent_root, path, |
| 5196 | iref, refs_to_drop, |
| 5197 | is_data); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5198 | if (ret) { |
| 5199 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5200 | goto out; |
| 5201 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5202 | } |
| 5203 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5204 | if (found_extent) { |
| 5205 | BUG_ON(is_data && refs_to_drop != |
| 5206 | extent_data_ref_count(root, path, iref)); |
| 5207 | if (iref) { |
| 5208 | BUG_ON(path->slots[0] != extent_slot); |
| 5209 | } else { |
| 5210 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 5211 | path->slots[0] = extent_slot; |
| 5212 | num_to_del = 2; |
| 5213 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 5214 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5215 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5216 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 5217 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5218 | if (ret) { |
| 5219 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5220 | goto out; |
| 5221 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5222 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 5223 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5224 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 5225 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5226 | if (ret) { |
| 5227 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5228 | goto out; |
| 5229 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 5230 | } |
| 5231 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5232 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5233 | if (ret) { |
| 5234 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5235 | goto out; |
| 5236 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5237 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5238 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5239 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5240 | return ret; |
| 5241 | } |
| 5242 | |
| 5243 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5244 | * 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] | 5245 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 5246 | * a given extent, and if there are no other delayed refs to be processed, it |
| 5247 | * removes it from the tree. |
| 5248 | */ |
| 5249 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 5250 | struct btrfs_root *root, u64 bytenr) |
| 5251 | { |
| 5252 | struct btrfs_delayed_ref_head *head; |
| 5253 | struct btrfs_delayed_ref_root *delayed_refs; |
| 5254 | struct btrfs_delayed_ref_node *ref; |
| 5255 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5256 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5257 | |
| 5258 | delayed_refs = &trans->transaction->delayed_refs; |
| 5259 | spin_lock(&delayed_refs->lock); |
| 5260 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 5261 | if (!head) |
| 5262 | goto out; |
| 5263 | |
| 5264 | node = rb_prev(&head->node.rb_node); |
| 5265 | if (!node) |
| 5266 | goto out; |
| 5267 | |
| 5268 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 5269 | |
| 5270 | /* there are still entries for this ref, we can't drop it */ |
| 5271 | if (ref->bytenr == bytenr) |
| 5272 | goto out; |
| 5273 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5274 | if (head->extent_op) { |
| 5275 | if (!head->must_insert_reserved) |
| 5276 | goto out; |
| 5277 | kfree(head->extent_op); |
| 5278 | head->extent_op = NULL; |
| 5279 | } |
| 5280 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5281 | /* |
| 5282 | * waiting for the lock here would deadlock. If someone else has it |
| 5283 | * locked they are already in the process of dropping it anyway |
| 5284 | */ |
| 5285 | if (!mutex_trylock(&head->mutex)) |
| 5286 | goto out; |
| 5287 | |
| 5288 | /* |
| 5289 | * at this point we have a head with no other entries. Go |
| 5290 | * ahead and process it. |
| 5291 | */ |
| 5292 | head->node.in_tree = 0; |
| 5293 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5294 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5295 | delayed_refs->num_entries--; |
| 5296 | |
| 5297 | /* |
| 5298 | * we don't take a ref on the node because we're removing it from the |
| 5299 | * tree, so we just steal the ref the tree was holding. |
| 5300 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5301 | delayed_refs->num_heads--; |
| 5302 | if (list_empty(&head->cluster)) |
| 5303 | delayed_refs->num_heads_ready--; |
| 5304 | |
| 5305 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5306 | spin_unlock(&delayed_refs->lock); |
| 5307 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5308 | BUG_ON(head->extent_op); |
| 5309 | if (head->must_insert_reserved) |
| 5310 | ret = 1; |
| 5311 | |
| 5312 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5313 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5314 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 5315 | out: |
| 5316 | spin_unlock(&delayed_refs->lock); |
| 5317 | return 0; |
| 5318 | } |
| 5319 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5320 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 5321 | struct btrfs_root *root, |
| 5322 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5323 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5324 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5325 | struct btrfs_block_group_cache *cache = NULL; |
| 5326 | int ret; |
| 5327 | |
| 5328 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5329 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 5330 | buf->start, buf->len, |
| 5331 | parent, root->root_key.objectid, |
| 5332 | btrfs_header_level(buf), |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5333 | BTRFS_DROP_DELAYED_REF, NULL, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5334 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | if (!last_ref) |
| 5338 | return; |
| 5339 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5340 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5341 | |
| 5342 | if (btrfs_header_generation(buf) == trans->transid) { |
| 5343 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5344 | ret = check_ref_cleanup(trans, root, buf->start); |
| 5345 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 5346 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 5350 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 5351 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 5355 | |
| 5356 | btrfs_add_free_space(cache, buf->start, buf->len); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5357 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5358 | } |
| 5359 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 5360 | /* |
| 5361 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 5362 | * anymore. |
| 5363 | */ |
| 5364 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5365 | btrfs_put_block_group(cache); |
| 5366 | } |
| 5367 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5368 | /* Can return -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5369 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 5370 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
| 5371 | u64 owner, u64 offset, int for_cow) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5372 | { |
| 5373 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5374 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5375 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5376 | /* |
| 5377 | * tree log blocks never actually go into the extent allocation |
| 5378 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5379 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5380 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 5381 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5382 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5383 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5384 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5385 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5386 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 5387 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5388 | parent, root_objectid, (int)owner, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5389 | BTRFS_DROP_DELAYED_REF, NULL, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5390 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 5391 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 5392 | num_bytes, |
| 5393 | parent, root_objectid, owner, |
| 5394 | offset, BTRFS_DROP_DELAYED_REF, |
| 5395 | NULL, for_cow); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5396 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5397 | return ret; |
| 5398 | } |
| 5399 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 5400 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 5401 | { |
| 5402 | u64 mask = ((u64)root->stripesize - 1); |
| 5403 | u64 ret = (val + mask) & ~mask; |
| 5404 | return ret; |
| 5405 | } |
| 5406 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5407 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5408 | * when we wait for progress in the block group caching, its because |
| 5409 | * our allocation attempt failed at least once. So, we must sleep |
| 5410 | * and let some progress happen before we try again. |
| 5411 | * |
| 5412 | * This function will sleep at least once waiting for new free space to |
| 5413 | * show up, and then it will check the block group free space numbers |
| 5414 | * for our min num_bytes. Another option is to have it go ahead |
| 5415 | * and look in the rbtree for a free extent of a given size, but this |
| 5416 | * is a good start. |
| 5417 | */ |
| 5418 | static noinline int |
| 5419 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 5420 | u64 num_bytes) |
| 5421 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5422 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5423 | DEFINE_WAIT(wait); |
| 5424 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5425 | caching_ctl = get_caching_control(cache); |
| 5426 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5427 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5428 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5429 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 5430 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5431 | |
| 5432 | put_caching_control(caching_ctl); |
| 5433 | return 0; |
| 5434 | } |
| 5435 | |
| 5436 | static noinline int |
| 5437 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 5438 | { |
| 5439 | struct btrfs_caching_control *caching_ctl; |
| 5440 | DEFINE_WAIT(wait); |
| 5441 | |
| 5442 | caching_ctl = get_caching_control(cache); |
| 5443 | if (!caching_ctl) |
| 5444 | return 0; |
| 5445 | |
| 5446 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 5447 | |
| 5448 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5449 | return 0; |
| 5450 | } |
| 5451 | |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5452 | static int __get_block_group_index(u64 flags) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5453 | { |
| 5454 | int index; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5455 | |
| 5456 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5457 | index = 0; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5458 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5459 | index = 1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5460 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5461 | index = 2; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5462 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5463 | index = 3; |
| 5464 | else |
| 5465 | index = 4; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5466 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5467 | return index; |
| 5468 | } |
| 5469 | |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 5470 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 5471 | { |
| 5472 | return __get_block_group_index(cache->flags); |
| 5473 | } |
| 5474 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5475 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 5476 | LOOP_CACHING_NOWAIT = 0, |
| 5477 | LOOP_CACHING_WAIT = 1, |
| 5478 | LOOP_ALLOC_CHUNK = 2, |
| 5479 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5480 | }; |
| 5481 | |
| 5482 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5483 | * walks the btree of allocated extents and find a hole of a given size. |
| 5484 | * The key ins is changed to record the hole: |
| 5485 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 5486 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5487 | * ins->offset == number of blocks |
| 5488 | * Any available blocks before search_start are skipped. |
| 5489 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5490 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5491 | struct btrfs_root *orig_root, |
| 5492 | u64 num_bytes, u64 empty_size, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5493 | u64 hint_byte, struct btrfs_key *ins, |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 5494 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5495 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5496 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5497 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5498 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5499 | struct btrfs_block_group_cache *block_group = NULL; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5500 | struct btrfs_block_group_cache *used_block_group; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 5501 | u64 search_start = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5502 | int empty_cluster = 2 * 1024 * 1024; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5503 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5504 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5505 | int index = 0; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5506 | int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? |
| 5507 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5508 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5509 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5510 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5511 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5512 | bool have_caching_bg = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5513 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5514 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5515 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5516 | ins->objectid = 0; |
| 5517 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5518 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 5519 | trace_find_free_extent(orig_root, num_bytes, empty_size, data); |
| 5520 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5521 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5522 | if (!space_info) { |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 5523 | printk(KERN_ERR "No space info for %llu\n", data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5524 | return -ENOSPC; |
| 5525 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5526 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5527 | /* |
| 5528 | * If the space info is for both data and metadata it means we have a |
| 5529 | * small filesystem and we can't use the clustering stuff. |
| 5530 | */ |
| 5531 | if (btrfs_mixed_space_info(space_info)) |
| 5532 | use_cluster = false; |
| 5533 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5534 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5535 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 5536 | if (!btrfs_test_opt(root, SSD)) |
| 5537 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5538 | } |
| 5539 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5540 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 5541 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5542 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 5543 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5544 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5545 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5546 | spin_lock(&last_ptr->lock); |
| 5547 | if (last_ptr->block_group) |
| 5548 | hint_byte = last_ptr->window_start; |
| 5549 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5550 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5551 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5552 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5553 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5554 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5555 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5556 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5557 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5558 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5559 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 5560 | search_start); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5561 | used_block_group = block_group; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5562 | /* |
| 5563 | * we don't want to use the block group if it doesn't match our |
| 5564 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5565 | * |
| 5566 | * However if we are re-searching with an ideal block group |
| 5567 | * 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] | 5568 | */ |
| 5569 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 5570 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5571 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5572 | if (list_empty(&block_group->list) || |
| 5573 | block_group->ro) { |
| 5574 | /* |
| 5575 | * someone is removing this block group, |
| 5576 | * we can't jump into the have_block_group |
| 5577 | * target because our list pointers are not |
| 5578 | * valid |
| 5579 | */ |
| 5580 | btrfs_put_block_group(block_group); |
| 5581 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5582 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5583 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5584 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5585 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5586 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5587 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5588 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5589 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5590 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5591 | have_caching_bg = false; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5592 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5593 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 5594 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5595 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5596 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 5597 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5598 | used_block_group = block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5599 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5600 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5601 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5602 | /* |
| 5603 | * this can happen if we end up cycling through all the |
| 5604 | * raid types, but we want to make sure we only allocate |
| 5605 | * for the proper type. |
| 5606 | */ |
| 5607 | if (!block_group_bits(block_group, data)) { |
| 5608 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 5609 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5610 | BTRFS_BLOCK_GROUP_RAID10; |
| 5611 | |
| 5612 | /* |
| 5613 | * if they asked for extra copies and this block group |
| 5614 | * doesn't provide them, bail. This does allow us to |
| 5615 | * fill raid0 from raid1. |
| 5616 | */ |
| 5617 | if ((data & extra) && !(block_group->flags & extra)) |
| 5618 | goto loop; |
| 5619 | } |
| 5620 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5621 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 5622 | cached = block_group_cache_done(block_group); |
| 5623 | if (unlikely(!cached)) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 5624 | found_uncached_bg = true; |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5625 | ret = cache_block_group(block_group, trans, |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 5626 | orig_root, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 5627 | BUG_ON(ret < 0); |
| 5628 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5629 | } |
| 5630 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5631 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5632 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5633 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5634 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 5635 | * Ok we want to try and use the cluster allocator, so |
| 5636 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5637 | */ |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 5638 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5639 | /* |
| 5640 | * the refill lock keeps out other |
| 5641 | * people trying to start a new cluster |
| 5642 | */ |
| 5643 | spin_lock(&last_ptr->refill_lock); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5644 | used_block_group = last_ptr->block_group; |
| 5645 | if (used_block_group != block_group && |
| 5646 | (!used_block_group || |
| 5647 | used_block_group->ro || |
| 5648 | !block_group_bits(used_block_group, data))) { |
| 5649 | used_block_group = block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5650 | goto refill_cluster; |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5651 | } |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5652 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5653 | if (used_block_group != block_group) |
| 5654 | btrfs_get_block_group(used_block_group); |
| 5655 | |
| 5656 | offset = btrfs_alloc_from_cluster(used_block_group, |
| 5657 | last_ptr, num_bytes, used_block_group->key.objectid); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5658 | if (offset) { |
| 5659 | /* we have a block, we're done */ |
| 5660 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 5661 | trace_btrfs_reserve_extent_cluster(root, |
| 5662 | block_group, search_start, num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5663 | goto checks; |
| 5664 | } |
| 5665 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5666 | WARN_ON(last_ptr->block_group != used_block_group); |
| 5667 | if (used_block_group != block_group) { |
| 5668 | btrfs_put_block_group(used_block_group); |
| 5669 | used_block_group = block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5670 | } |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5671 | refill_cluster: |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5672 | BUG_ON(used_block_group != block_group); |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 5673 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 5674 | * set up a new clusters, so lets just skip it |
| 5675 | * and let the allocator find whatever block |
| 5676 | * it can find. If we reach this point, we |
| 5677 | * will have tried the cluster allocator |
| 5678 | * plenty of times and not have found |
| 5679 | * anything, so we are likely way too |
| 5680 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 5681 | * anything. |
| 5682 | * |
| 5683 | * However, if the cluster is taken from the |
| 5684 | * current block group, release the cluster |
| 5685 | * first, so that we stand a better chance of |
| 5686 | * succeeding in the unclustered |
| 5687 | * allocation. */ |
| 5688 | if (loop >= LOOP_NO_EMPTY_SIZE && |
| 5689 | last_ptr->block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 5690 | spin_unlock(&last_ptr->refill_lock); |
| 5691 | goto unclustered_alloc; |
| 5692 | } |
| 5693 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5694 | /* |
| 5695 | * this cluster didn't work out, free it and |
| 5696 | * start over |
| 5697 | */ |
| 5698 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5699 | |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 5700 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 5701 | spin_unlock(&last_ptr->refill_lock); |
| 5702 | goto unclustered_alloc; |
| 5703 | } |
| 5704 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5705 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5706 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5707 | block_group, last_ptr, |
Alexandre Oliva | 1b22bad | 2011-11-30 13:43:00 -0500 | [diff] [blame] | 5708 | search_start, num_bytes, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5709 | empty_cluster + empty_size); |
| 5710 | if (ret == 0) { |
| 5711 | /* |
| 5712 | * now pull our allocation out of this |
| 5713 | * cluster |
| 5714 | */ |
| 5715 | offset = btrfs_alloc_from_cluster(block_group, |
| 5716 | last_ptr, num_bytes, |
| 5717 | search_start); |
| 5718 | if (offset) { |
| 5719 | /* we found one, proceed */ |
| 5720 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 5721 | trace_btrfs_reserve_extent_cluster(root, |
| 5722 | block_group, search_start, |
| 5723 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5724 | goto checks; |
| 5725 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5726 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5727 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5728 | spin_unlock(&last_ptr->refill_lock); |
| 5729 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5730 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5731 | wait_block_group_cache_progress(block_group, |
| 5732 | num_bytes + empty_cluster + empty_size); |
| 5733 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5734 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5735 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5736 | /* |
| 5737 | * at this point we either didn't find a cluster |
| 5738 | * or we weren't able to allocate a block from our |
| 5739 | * cluster. Free the cluster we've been trying |
| 5740 | * to use, and go to the next block group |
| 5741 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5742 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5743 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5744 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5745 | } |
| 5746 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 5747 | unclustered_alloc: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 5748 | spin_lock(&block_group->free_space_ctl->tree_lock); |
| 5749 | if (cached && |
| 5750 | block_group->free_space_ctl->free_space < |
| 5751 | num_bytes + empty_cluster + empty_size) { |
| 5752 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 5753 | goto loop; |
| 5754 | } |
| 5755 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 5756 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5757 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5758 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5759 | /* |
| 5760 | * If we didn't find a chunk, and we haven't failed on this |
| 5761 | * block group before, and this block group is in the middle of |
| 5762 | * caching and we are ok with waiting, then go ahead and wait |
| 5763 | * for progress to be made, and set failed_alloc to true. |
| 5764 | * |
| 5765 | * If failed_alloc is true then we've already waited on this |
| 5766 | * block group once and should move on to the next block group. |
| 5767 | */ |
| 5768 | if (!offset && !failed_alloc && !cached && |
| 5769 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5770 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5771 | num_bytes + empty_size); |
| 5772 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5773 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5774 | } else if (!offset) { |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5775 | if (!cached) |
| 5776 | have_caching_bg = true; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5777 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5778 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5779 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5780 | search_start = stripe_align(root, offset); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5781 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5782 | /* move on to the next group */ |
| 5783 | if (search_start + num_bytes > |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5784 | used_block_group->key.objectid + used_block_group->key.offset) { |
| 5785 | btrfs_add_free_space(used_block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5786 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5787 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5788 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5789 | if (offset < search_start) |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5790 | btrfs_add_free_space(used_block_group, offset, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5791 | search_start - offset); |
| 5792 | BUG_ON(offset > search_start); |
| 5793 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5794 | ret = btrfs_update_reserved_bytes(used_block_group, num_bytes, |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5795 | alloc_type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5796 | if (ret == -EAGAIN) { |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5797 | btrfs_add_free_space(used_block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5798 | goto loop; |
| 5799 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5800 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5801 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5802 | ins->objectid = search_start; |
| 5803 | ins->offset = num_bytes; |
| 5804 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 5805 | trace_btrfs_reserve_extent(orig_root, block_group, |
| 5806 | search_start, num_bytes); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5807 | if (used_block_group != block_group) |
| 5808 | btrfs_put_block_group(used_block_group); |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5809 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5810 | break; |
| 5811 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5812 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5813 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5814 | BUG_ON(index != get_block_group_index(block_group)); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 5815 | if (used_block_group != block_group) |
| 5816 | btrfs_put_block_group(used_block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5817 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5818 | } |
| 5819 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5820 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5821 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 5822 | goto search; |
| 5823 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5824 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5825 | goto search; |
| 5826 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 5827 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5828 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5829 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5830 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5831 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5832 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5833 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5834 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5835 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5836 | index = 0; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5837 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5838 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 5839 | ret = do_chunk_alloc(trans, root, data, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 5840 | CHUNK_ALLOC_FORCE); |
| 5841 | /* |
| 5842 | * Do not bail out on ENOSPC since we |
| 5843 | * can do more things. |
| 5844 | */ |
| 5845 | if (ret < 0 && ret != -ENOSPC) { |
| 5846 | btrfs_abort_transaction(trans, |
| 5847 | root, ret); |
| 5848 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5849 | } |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5853 | empty_size = 0; |
| 5854 | empty_cluster = 0; |
| 5855 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5856 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5857 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5858 | } else if (!ins->objectid) { |
| 5859 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5860 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5861 | ret = 0; |
| 5862 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5863 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5864 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5865 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5866 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5867 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5868 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5869 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5870 | { |
| 5871 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5872 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5873 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5874 | spin_lock(&info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5875 | printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n", |
| 5876 | (unsigned long long)info->flags, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5877 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5878 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5879 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5880 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5881 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5882 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5883 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5884 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5885 | (unsigned long long)info->bytes_pinned, |
| 5886 | (unsigned long long)info->bytes_reserved, |
| 5887 | (unsigned long long)info->bytes_may_use, |
| 5888 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5889 | spin_unlock(&info->lock); |
| 5890 | |
| 5891 | if (!dump_block_groups) |
| 5892 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5893 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5894 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5895 | again: |
| 5896 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5897 | spin_lock(&cache->lock); |
Liu Bo | 799ffc3 | 2012-07-06 03:31:35 -0600 | [diff] [blame] | 5898 | 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] | 5899 | (unsigned long long)cache->key.objectid, |
| 5900 | (unsigned long long)cache->key.offset, |
| 5901 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5902 | (unsigned long long)cache->pinned, |
Liu Bo | 799ffc3 | 2012-07-06 03:31:35 -0600 | [diff] [blame] | 5903 | (unsigned long long)cache->reserved, |
| 5904 | cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5905 | btrfs_dump_free_space(cache, bytes); |
| 5906 | spin_unlock(&cache->lock); |
| 5907 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5908 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5909 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5910 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5911 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5912 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5913 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5914 | struct btrfs_root *root, |
| 5915 | u64 num_bytes, u64 min_alloc_size, |
| 5916 | u64 empty_size, u64 hint_byte, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 5917 | struct btrfs_key *ins, u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5918 | { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5919 | bool final_tried = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5920 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5921 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5922 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5923 | again: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5924 | WARN_ON(num_bytes < root->sectorsize); |
| 5925 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 5926 | hint_byte, ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5927 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5928 | if (ret == -ENOSPC) { |
| 5929 | if (!final_tried) { |
| 5930 | num_bytes = num_bytes >> 1; |
| 5931 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
| 5932 | num_bytes = max(num_bytes, min_alloc_size); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5933 | if (num_bytes == min_alloc_size) |
| 5934 | final_tried = true; |
| 5935 | goto again; |
| 5936 | } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 5937 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5938 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5939 | sinfo = __find_space_info(root->fs_info, data); |
| 5940 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5941 | "wanted %llu\n", (unsigned long long)data, |
| 5942 | (unsigned long long)num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 5943 | if (sinfo) |
| 5944 | dump_space_info(sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 5945 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5946 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5947 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5948 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5949 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5950 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5951 | } |
| 5952 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5953 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
| 5954 | u64 start, u64 len, int pin) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5955 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5956 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5957 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5958 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5959 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5960 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5961 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5962 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5963 | return -ENOSPC; |
| 5964 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5965 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5966 | if (btrfs_test_opt(root, DISCARD)) |
| 5967 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5968 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5969 | if (pin) |
| 5970 | pin_down_extent(root, cache, start, len, 1); |
| 5971 | else { |
| 5972 | btrfs_add_free_space(cache, start, len); |
| 5973 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); |
| 5974 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5975 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5976 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5977 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5978 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5979 | return ret; |
| 5980 | } |
| 5981 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5982 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
| 5983 | u64 start, u64 len) |
| 5984 | { |
| 5985 | return __btrfs_free_reserved_extent(root, start, len, 0); |
| 5986 | } |
| 5987 | |
| 5988 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, |
| 5989 | u64 start, u64 len) |
| 5990 | { |
| 5991 | return __btrfs_free_reserved_extent(root, start, len, 1); |
| 5992 | } |
| 5993 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5994 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5995 | struct btrfs_root *root, |
| 5996 | u64 parent, u64 root_objectid, |
| 5997 | u64 flags, u64 owner, u64 offset, |
| 5998 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5999 | { |
| 6000 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6001 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6002 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6003 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6004 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6005 | struct extent_buffer *leaf; |
| 6006 | int type; |
| 6007 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 6008 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6009 | if (parent > 0) |
| 6010 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 6011 | else |
| 6012 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6013 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6014 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6015 | |
| 6016 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6017 | if (!path) |
| 6018 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6019 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6020 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6021 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 6022 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6023 | if (ret) { |
| 6024 | btrfs_free_path(path); |
| 6025 | return ret; |
| 6026 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6027 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6028 | leaf = path->nodes[0]; |
| 6029 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6030 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6031 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 6032 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 6033 | btrfs_set_extent_flags(leaf, extent_item, |
| 6034 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6035 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6036 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 6037 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 6038 | if (parent > 0) { |
| 6039 | struct btrfs_shared_data_ref *ref; |
| 6040 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 6041 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 6042 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 6043 | } else { |
| 6044 | struct btrfs_extent_data_ref *ref; |
| 6045 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 6046 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 6047 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 6048 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 6049 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 6050 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6051 | |
| 6052 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6053 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 6054 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6055 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6056 | if (ret) { /* -ENOENT, logic error */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6057 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 6058 | "%llu\n", (unsigned long long)ins->objectid, |
| 6059 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 6060 | BUG(); |
| 6061 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6062 | return ret; |
| 6063 | } |
| 6064 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6065 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 6066 | struct btrfs_root *root, |
| 6067 | u64 parent, u64 root_objectid, |
| 6068 | u64 flags, struct btrfs_disk_key *key, |
| 6069 | int level, struct btrfs_key *ins) |
| 6070 | { |
| 6071 | int ret; |
| 6072 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 6073 | struct btrfs_extent_item *extent_item; |
| 6074 | struct btrfs_tree_block_info *block_info; |
| 6075 | struct btrfs_extent_inline_ref *iref; |
| 6076 | struct btrfs_path *path; |
| 6077 | struct extent_buffer *leaf; |
| 6078 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 6079 | |
| 6080 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 6081 | if (!path) |
| 6082 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6083 | |
| 6084 | path->leave_spinning = 1; |
| 6085 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 6086 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6087 | if (ret) { |
| 6088 | btrfs_free_path(path); |
| 6089 | return ret; |
| 6090 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6091 | |
| 6092 | leaf = path->nodes[0]; |
| 6093 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 6094 | struct btrfs_extent_item); |
| 6095 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 6096 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 6097 | btrfs_set_extent_flags(leaf, extent_item, |
| 6098 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 6099 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 6100 | |
| 6101 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 6102 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 6103 | |
| 6104 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 6105 | if (parent > 0) { |
| 6106 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 6107 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 6108 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 6109 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 6110 | } else { |
| 6111 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 6112 | BTRFS_TREE_BLOCK_REF_KEY); |
| 6113 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 6114 | } |
| 6115 | |
| 6116 | btrfs_mark_buffer_dirty(leaf); |
| 6117 | btrfs_free_path(path); |
| 6118 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6119 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6120 | if (ret) { /* -ENOENT, logic error */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6121 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 6122 | "%llu\n", (unsigned long long)ins->objectid, |
| 6123 | (unsigned long long)ins->offset); |
| 6124 | BUG(); |
| 6125 | } |
| 6126 | return ret; |
| 6127 | } |
| 6128 | |
| 6129 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 6130 | struct btrfs_root *root, |
| 6131 | u64 root_objectid, u64 owner, |
| 6132 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6133 | { |
| 6134 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 6135 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6136 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6137 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6138 | ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, |
| 6139 | ins->offset, 0, |
| 6140 | root_objectid, owner, offset, |
| 6141 | BTRFS_ADD_DELAYED_EXTENT, NULL, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6142 | return ret; |
| 6143 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6144 | |
| 6145 | /* |
| 6146 | * this is used by the tree logging recovery code. It records that |
| 6147 | * an extent has been allocated and makes sure to clear the free |
| 6148 | * space cache bits as well |
| 6149 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6150 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 6151 | struct btrfs_root *root, |
| 6152 | u64 root_objectid, u64 owner, u64 offset, |
| 6153 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6154 | { |
| 6155 | int ret; |
| 6156 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6157 | struct btrfs_caching_control *caching_ctl; |
| 6158 | u64 start = ins->objectid; |
| 6159 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6160 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6161 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 6162 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6163 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6164 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6165 | if (!caching_ctl) { |
| 6166 | BUG_ON(!block_group_cache_done(block_group)); |
| 6167 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6168 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6169 | } else { |
| 6170 | mutex_lock(&caching_ctl->mutex); |
| 6171 | |
| 6172 | if (start >= caching_ctl->progress) { |
| 6173 | ret = add_excluded_extent(root, start, num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6174 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6175 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 6176 | ret = btrfs_remove_free_space(block_group, |
| 6177 | start, num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6178 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6179 | } else { |
| 6180 | num_bytes = caching_ctl->progress - start; |
| 6181 | ret = btrfs_remove_free_space(block_group, |
| 6182 | start, num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6183 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6184 | |
| 6185 | start = caching_ctl->progress; |
| 6186 | num_bytes = ins->objectid + ins->offset - |
| 6187 | caching_ctl->progress; |
| 6188 | ret = add_excluded_extent(root, start, num_bytes); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6189 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | mutex_unlock(&caching_ctl->mutex); |
| 6193 | put_caching_control(caching_ctl); |
| 6194 | } |
| 6195 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6196 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
| 6197 | RESERVE_ALLOC_NO_ACCOUNT); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6198 | BUG_ON(ret); /* logic error */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6199 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6200 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 6201 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6202 | return ret; |
| 6203 | } |
| 6204 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6205 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 6206 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6207 | u64 bytenr, u32 blocksize, |
| 6208 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6209 | { |
| 6210 | struct extent_buffer *buf; |
| 6211 | |
| 6212 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 6213 | if (!buf) |
| 6214 | return ERR_PTR(-ENOMEM); |
| 6215 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6216 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6217 | btrfs_tree_lock(buf); |
| 6218 | clean_tree_block(trans, root, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 6219 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6220 | |
| 6221 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6222 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6223 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6224 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 6225 | /* |
| 6226 | * we allow two log transactions at a time, use different |
| 6227 | * EXENT bit to differentiate dirty pages. |
| 6228 | */ |
| 6229 | if (root->log_transid % 2 == 0) |
| 6230 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 6231 | buf->start + buf->len - 1, GFP_NOFS); |
| 6232 | else |
| 6233 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 6234 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 6235 | } else { |
| 6236 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 6237 | buf->start + buf->len - 1, GFP_NOFS); |
| 6238 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6239 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 6240 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6241 | return buf; |
| 6242 | } |
| 6243 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6244 | static struct btrfs_block_rsv * |
| 6245 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 6246 | struct btrfs_root *root, u32 blocksize) |
| 6247 | { |
| 6248 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 6249 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6250 | int ret; |
| 6251 | |
| 6252 | block_rsv = get_block_rsv(trans, root); |
| 6253 | |
| 6254 | if (block_rsv->size == 0) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 6255 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 6256 | /* |
| 6257 | * If we couldn't reserve metadata bytes try and use some from |
| 6258 | * the global reserve. |
| 6259 | */ |
| 6260 | if (ret && block_rsv != global_rsv) { |
| 6261 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 6262 | if (!ret) |
| 6263 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6264 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 6265 | } else if (ret) { |
| 6266 | return ERR_PTR(ret); |
| 6267 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6268 | return block_rsv; |
| 6269 | } |
| 6270 | |
| 6271 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 6272 | if (!ret) |
| 6273 | return block_rsv; |
Josef Bacik | ca7e70f | 2012-08-27 17:48:15 -0400 | [diff] [blame] | 6274 | if (ret && !block_rsv->failfast) { |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 6275 | static DEFINE_RATELIMIT_STATE(_rs, |
| 6276 | DEFAULT_RATELIMIT_INTERVAL, |
| 6277 | /*DEFAULT_RATELIMIT_BURST*/ 2); |
| 6278 | if (__ratelimit(&_rs)) { |
| 6279 | printk(KERN_DEBUG "btrfs: block rsv returned %d\n", ret); |
| 6280 | WARN_ON(1); |
| 6281 | } |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 6282 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 6283 | if (!ret) { |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 6284 | return block_rsv; |
| 6285 | } else if (ret && block_rsv != global_rsv) { |
| 6286 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 6287 | if (!ret) |
| 6288 | return global_rsv; |
| 6289 | } |
| 6290 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6291 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6292 | return ERR_PTR(-ENOSPC); |
| 6293 | } |
| 6294 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6295 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 6296 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6297 | { |
| 6298 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6299 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6300 | } |
| 6301 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6302 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6303 | * finds a free extent and does all the dirty work required for allocation |
| 6304 | * returns the key for the extent through ins, and a tree buffer for |
| 6305 | * the first block of the extent through buf. |
| 6306 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6307 | * returns the tree buffer or NULL. |
| 6308 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6309 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6310 | struct btrfs_root *root, u32 blocksize, |
| 6311 | u64 parent, u64 root_objectid, |
| 6312 | struct btrfs_disk_key *key, int level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6313 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6314 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 6315 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6316 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6317 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6318 | u64 flags = 0; |
| 6319 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6320 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6321 | |
| 6322 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 6323 | if (IS_ERR(block_rsv)) |
| 6324 | return ERR_CAST(block_rsv); |
| 6325 | |
| 6326 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6327 | empty_size, hint, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6328 | if (ret) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 6329 | unuse_block_rsv(root->fs_info, block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 6330 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6331 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 6332 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6333 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 6334 | blocksize, level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6335 | BUG_ON(IS_ERR(buf)); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6336 | |
| 6337 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 6338 | if (parent == 0) |
| 6339 | parent = ins.objectid; |
| 6340 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6341 | } else |
| 6342 | BUG_ON(parent > 0); |
| 6343 | |
| 6344 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 6345 | struct btrfs_delayed_extent_op *extent_op; |
| 6346 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6347 | BUG_ON(!extent_op); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6348 | if (key) |
| 6349 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 6350 | else |
| 6351 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 6352 | extent_op->flags_to_set = flags; |
| 6353 | extent_op->update_key = 1; |
| 6354 | extent_op->update_flags = 1; |
| 6355 | extent_op->is_data = 0; |
| 6356 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6357 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 6358 | ins.objectid, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6359 | ins.offset, parent, root_objectid, |
| 6360 | level, BTRFS_ADD_DELAYED_EXTENT, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6361 | extent_op, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6362 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6363 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6364 | return buf; |
| 6365 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6366 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6367 | struct walk_control { |
| 6368 | u64 refs[BTRFS_MAX_LEVEL]; |
| 6369 | u64 flags[BTRFS_MAX_LEVEL]; |
| 6370 | struct btrfs_key update_progress; |
| 6371 | int stage; |
| 6372 | int level; |
| 6373 | int shared_level; |
| 6374 | int update_ref; |
| 6375 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6376 | int reada_slot; |
| 6377 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6378 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6379 | }; |
| 6380 | |
| 6381 | #define DROP_REFERENCE 1 |
| 6382 | #define UPDATE_BACKREF 2 |
| 6383 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6384 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 6385 | struct btrfs_root *root, |
| 6386 | struct walk_control *wc, |
| 6387 | struct btrfs_path *path) |
| 6388 | { |
| 6389 | u64 bytenr; |
| 6390 | u64 generation; |
| 6391 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6392 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6393 | u32 nritems; |
| 6394 | u32 blocksize; |
| 6395 | struct btrfs_key key; |
| 6396 | struct extent_buffer *eb; |
| 6397 | int ret; |
| 6398 | int slot; |
| 6399 | int nread = 0; |
| 6400 | |
| 6401 | if (path->slots[wc->level] < wc->reada_slot) { |
| 6402 | wc->reada_count = wc->reada_count * 2 / 3; |
| 6403 | wc->reada_count = max(wc->reada_count, 2); |
| 6404 | } else { |
| 6405 | wc->reada_count = wc->reada_count * 3 / 2; |
| 6406 | wc->reada_count = min_t(int, wc->reada_count, |
| 6407 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 6408 | } |
| 6409 | |
| 6410 | eb = path->nodes[wc->level]; |
| 6411 | nritems = btrfs_header_nritems(eb); |
| 6412 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 6413 | |
| 6414 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 6415 | if (nread >= wc->reada_count) |
| 6416 | break; |
| 6417 | |
| 6418 | cond_resched(); |
| 6419 | bytenr = btrfs_node_blockptr(eb, slot); |
| 6420 | generation = btrfs_node_ptr_generation(eb, slot); |
| 6421 | |
| 6422 | if (slot == path->slots[wc->level]) |
| 6423 | goto reada; |
| 6424 | |
| 6425 | if (wc->stage == UPDATE_BACKREF && |
| 6426 | generation <= root->root_key.offset) |
| 6427 | continue; |
| 6428 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6429 | /* We don't lock the tree block, it's OK to be racy here */ |
| 6430 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6431 | &refs, &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6432 | /* We don't care about errors in readahead. */ |
| 6433 | if (ret < 0) |
| 6434 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6435 | BUG_ON(refs == 0); |
| 6436 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6437 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6438 | if (refs == 1) |
| 6439 | goto reada; |
| 6440 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6441 | if (wc->level == 1 && |
| 6442 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6443 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6444 | if (!wc->update_ref || |
| 6445 | generation <= root->root_key.offset) |
| 6446 | continue; |
| 6447 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 6448 | ret = btrfs_comp_cpu_keys(&key, |
| 6449 | &wc->update_progress); |
| 6450 | if (ret < 0) |
| 6451 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6452 | } else { |
| 6453 | if (wc->level == 1 && |
| 6454 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6455 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6456 | } |
| 6457 | reada: |
| 6458 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 6459 | generation); |
| 6460 | if (ret) |
| 6461 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6462 | nread++; |
| 6463 | } |
| 6464 | wc->reada_slot = slot; |
| 6465 | } |
| 6466 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6467 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6468 | * hepler to process tree block while walking down the tree. |
| 6469 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6470 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 6471 | * back refs for pointers in the block. |
| 6472 | * |
| 6473 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6474 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6475 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 6476 | struct btrfs_root *root, |
| 6477 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6478 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6479 | { |
| 6480 | int level = wc->level; |
| 6481 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6482 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6483 | int ret; |
| 6484 | |
| 6485 | if (wc->stage == UPDATE_BACKREF && |
| 6486 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 6487 | return 1; |
| 6488 | |
| 6489 | /* |
| 6490 | * when reference count of tree block is 1, it won't increase |
| 6491 | * again. once full backref flag is set, we never clear it. |
| 6492 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6493 | if (lookup_info && |
| 6494 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 6495 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6496 | BUG_ON(!path->locks[level]); |
| 6497 | ret = btrfs_lookup_extent_info(trans, root, |
| 6498 | eb->start, eb->len, |
| 6499 | &wc->refs[level], |
| 6500 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6501 | BUG_ON(ret == -ENOMEM); |
| 6502 | if (ret) |
| 6503 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6504 | BUG_ON(wc->refs[level] == 0); |
| 6505 | } |
| 6506 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6507 | if (wc->stage == DROP_REFERENCE) { |
| 6508 | if (wc->refs[level] > 1) |
| 6509 | return 1; |
| 6510 | |
| 6511 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6512 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6513 | path->locks[level] = 0; |
| 6514 | } |
| 6515 | return 0; |
| 6516 | } |
| 6517 | |
| 6518 | /* wc->stage == UPDATE_BACKREF */ |
| 6519 | if (!(wc->flags[level] & flag)) { |
| 6520 | BUG_ON(!path->locks[level]); |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6521 | ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6522 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6523 | ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6524 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6525 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 6526 | eb->len, flag, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6527 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6528 | wc->flags[level] |= flag; |
| 6529 | } |
| 6530 | |
| 6531 | /* |
| 6532 | * the block is shared by multiple trees, so it's not good to |
| 6533 | * keep the tree lock |
| 6534 | */ |
| 6535 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6536 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6537 | path->locks[level] = 0; |
| 6538 | } |
| 6539 | return 0; |
| 6540 | } |
| 6541 | |
| 6542 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6543 | * hepler to process tree block pointer. |
| 6544 | * |
| 6545 | * when wc->stage == DROP_REFERENCE, this function checks |
| 6546 | * reference count of the block pointed to. if the block |
| 6547 | * is shared and we need update back refs for the subtree |
| 6548 | * rooted at the block, this function changes wc->stage to |
| 6549 | * UPDATE_BACKREF. if the block is shared and there is no |
| 6550 | * need to update back, this function drops the reference |
| 6551 | * to the block. |
| 6552 | * |
| 6553 | * NOTE: return value 1 means we should stop walking down. |
| 6554 | */ |
| 6555 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 6556 | struct btrfs_root *root, |
| 6557 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6558 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6559 | { |
| 6560 | u64 bytenr; |
| 6561 | u64 generation; |
| 6562 | u64 parent; |
| 6563 | u32 blocksize; |
| 6564 | struct btrfs_key key; |
| 6565 | struct extent_buffer *next; |
| 6566 | int level = wc->level; |
| 6567 | int reada = 0; |
| 6568 | int ret = 0; |
| 6569 | |
| 6570 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 6571 | path->slots[level]); |
| 6572 | /* |
| 6573 | * if the lower level block was created before the snapshot |
| 6574 | * was created, we know there is no need to update back refs |
| 6575 | * for the subtree |
| 6576 | */ |
| 6577 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6578 | generation <= root->root_key.offset) { |
| 6579 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6580 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6581 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6582 | |
| 6583 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 6584 | blocksize = btrfs_level_size(root, level - 1); |
| 6585 | |
| 6586 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 6587 | if (!next) { |
| 6588 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6589 | if (!next) |
| 6590 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6591 | reada = 1; |
| 6592 | } |
| 6593 | btrfs_tree_lock(next); |
| 6594 | btrfs_set_lock_blocking(next); |
| 6595 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6596 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6597 | &wc->refs[level - 1], |
| 6598 | &wc->flags[level - 1]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6599 | if (ret < 0) { |
| 6600 | btrfs_tree_unlock(next); |
| 6601 | return ret; |
| 6602 | } |
| 6603 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6604 | BUG_ON(wc->refs[level - 1] == 0); |
| 6605 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6606 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6607 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6608 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6609 | if (level == 1 && |
| 6610 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6611 | goto skip; |
| 6612 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6613 | if (!wc->update_ref || |
| 6614 | generation <= root->root_key.offset) |
| 6615 | goto skip; |
| 6616 | |
| 6617 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6618 | path->slots[level]); |
| 6619 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6620 | if (ret < 0) |
| 6621 | goto skip; |
| 6622 | |
| 6623 | wc->stage = UPDATE_BACKREF; |
| 6624 | wc->shared_level = level - 1; |
| 6625 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6626 | } else { |
| 6627 | if (level == 1 && |
| 6628 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6629 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6630 | } |
| 6631 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 6632 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6633 | btrfs_tree_unlock(next); |
| 6634 | free_extent_buffer(next); |
| 6635 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6636 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | if (!next) { |
| 6640 | if (reada && level == 1) |
| 6641 | reada_walk_down(trans, root, wc, path); |
| 6642 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6643 | if (!next) |
| 6644 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6645 | btrfs_tree_lock(next); |
| 6646 | btrfs_set_lock_blocking(next); |
| 6647 | } |
| 6648 | |
| 6649 | level--; |
| 6650 | BUG_ON(level != btrfs_header_level(next)); |
| 6651 | path->nodes[level] = next; |
| 6652 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6653 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6654 | wc->level = level; |
| 6655 | if (wc->level == 1) |
| 6656 | wc->reada_slot = 0; |
| 6657 | return 0; |
| 6658 | skip: |
| 6659 | wc->refs[level - 1] = 0; |
| 6660 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6661 | if (wc->stage == DROP_REFERENCE) { |
| 6662 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6663 | parent = path->nodes[level]->start; |
| 6664 | } else { |
| 6665 | BUG_ON(root->root_key.objectid != |
| 6666 | btrfs_header_owner(path->nodes[level])); |
| 6667 | parent = 0; |
| 6668 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6669 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6670 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6671 | root->root_key.objectid, level - 1, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6672 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6673 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6674 | btrfs_tree_unlock(next); |
| 6675 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6676 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6677 | return 1; |
| 6678 | } |
| 6679 | |
| 6680 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6681 | * hepler to process tree block while walking up the tree. |
| 6682 | * |
| 6683 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6684 | * reference count on the block. |
| 6685 | * |
| 6686 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6687 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6688 | * to UPDATE_BACKREF previously while processing the block. |
| 6689 | * |
| 6690 | * NOTE: return value 1 means we should stop walking up. |
| 6691 | */ |
| 6692 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6693 | struct btrfs_root *root, |
| 6694 | struct btrfs_path *path, |
| 6695 | struct walk_control *wc) |
| 6696 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6697 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6698 | int level = wc->level; |
| 6699 | struct extent_buffer *eb = path->nodes[level]; |
| 6700 | u64 parent = 0; |
| 6701 | |
| 6702 | if (wc->stage == UPDATE_BACKREF) { |
| 6703 | BUG_ON(wc->shared_level < level); |
| 6704 | if (level < wc->shared_level) |
| 6705 | goto out; |
| 6706 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6707 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6708 | if (ret > 0) |
| 6709 | wc->update_ref = 0; |
| 6710 | |
| 6711 | wc->stage = DROP_REFERENCE; |
| 6712 | wc->shared_level = -1; |
| 6713 | path->slots[level] = 0; |
| 6714 | |
| 6715 | /* |
| 6716 | * check reference count again if the block isn't locked. |
| 6717 | * we should start walking down the tree again if reference |
| 6718 | * count is one. |
| 6719 | */ |
| 6720 | if (!path->locks[level]) { |
| 6721 | BUG_ON(level == 0); |
| 6722 | btrfs_tree_lock(eb); |
| 6723 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6724 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6725 | |
| 6726 | ret = btrfs_lookup_extent_info(trans, root, |
| 6727 | eb->start, eb->len, |
| 6728 | &wc->refs[level], |
| 6729 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6730 | if (ret < 0) { |
| 6731 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
| 6732 | return ret; |
| 6733 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6734 | BUG_ON(wc->refs[level] == 0); |
| 6735 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6736 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6737 | return 1; |
| 6738 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6739 | } |
| 6740 | } |
| 6741 | |
| 6742 | /* wc->stage == DROP_REFERENCE */ |
| 6743 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6744 | |
| 6745 | if (wc->refs[level] == 1) { |
| 6746 | if (level == 0) { |
| 6747 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6748 | ret = btrfs_dec_ref(trans, root, eb, 1, |
| 6749 | wc->for_reloc); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6750 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6751 | ret = btrfs_dec_ref(trans, root, eb, 0, |
| 6752 | wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6753 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6754 | } |
| 6755 | /* make block locked assertion in clean_tree_block happy */ |
| 6756 | if (!path->locks[level] && |
| 6757 | btrfs_header_generation(eb) == trans->transid) { |
| 6758 | btrfs_tree_lock(eb); |
| 6759 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6760 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6761 | } |
| 6762 | clean_tree_block(trans, root, eb); |
| 6763 | } |
| 6764 | |
| 6765 | if (eb == root->node) { |
| 6766 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6767 | parent = eb->start; |
| 6768 | else |
| 6769 | BUG_ON(root->root_key.objectid != |
| 6770 | btrfs_header_owner(eb)); |
| 6771 | } else { |
| 6772 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6773 | parent = path->nodes[level + 1]->start; |
| 6774 | else |
| 6775 | BUG_ON(root->root_key.objectid != |
| 6776 | btrfs_header_owner(path->nodes[level + 1])); |
| 6777 | } |
| 6778 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6779 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6780 | out: |
| 6781 | wc->refs[level] = 0; |
| 6782 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6783 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6784 | } |
| 6785 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6786 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6787 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6788 | struct btrfs_path *path, |
| 6789 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6790 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6791 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6792 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6793 | int ret; |
| 6794 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6795 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6796 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6797 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6798 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6799 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6800 | if (level == 0) |
| 6801 | break; |
| 6802 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6803 | if (path->slots[level] >= |
| 6804 | btrfs_header_nritems(path->nodes[level])) |
| 6805 | break; |
| 6806 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6807 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6808 | if (ret > 0) { |
| 6809 | path->slots[level]++; |
| 6810 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6811 | } else if (ret < 0) |
| 6812 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6813 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6814 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6815 | return 0; |
| 6816 | } |
| 6817 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6818 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6819 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6820 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6821 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6822 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6823 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6824 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6825 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6826 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6827 | while (level < max_level && path->nodes[level]) { |
| 6828 | wc->level = level; |
| 6829 | if (path->slots[level] + 1 < |
| 6830 | btrfs_header_nritems(path->nodes[level])) { |
| 6831 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6832 | return 0; |
| 6833 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6834 | ret = walk_up_proc(trans, root, path, wc); |
| 6835 | if (ret > 0) |
| 6836 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6837 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6838 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6839 | btrfs_tree_unlock_rw(path->nodes[level], |
| 6840 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6841 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6842 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6843 | free_extent_buffer(path->nodes[level]); |
| 6844 | path->nodes[level] = NULL; |
| 6845 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6846 | } |
| 6847 | } |
| 6848 | return 1; |
| 6849 | } |
| 6850 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6851 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6852 | * drop a subvolume tree. |
| 6853 | * |
| 6854 | * this function traverses the tree freeing any blocks that only |
| 6855 | * referenced by the tree. |
| 6856 | * |
| 6857 | * when a shared tree block is found. this function decreases its |
| 6858 | * reference count by one. if update_ref is true, this function |
| 6859 | * also make sure backrefs for the shared block and all lower level |
| 6860 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6861 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 6862 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6863 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 6864 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6865 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6866 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6867 | struct btrfs_trans_handle *trans; |
| 6868 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6869 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6870 | struct walk_control *wc; |
| 6871 | struct btrfs_key key; |
| 6872 | int err = 0; |
| 6873 | int ret; |
| 6874 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6875 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6876 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6877 | if (!path) { |
| 6878 | err = -ENOMEM; |
| 6879 | goto out; |
| 6880 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6881 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6882 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6883 | if (!wc) { |
| 6884 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6885 | err = -ENOMEM; |
| 6886 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6887 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6888 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6889 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6890 | if (IS_ERR(trans)) { |
| 6891 | err = PTR_ERR(trans); |
| 6892 | goto out_free; |
| 6893 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6894 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6895 | if (block_rsv) |
| 6896 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6897 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6898 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6899 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6900 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6901 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6902 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6903 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6904 | memset(&wc->update_progress, 0, |
| 6905 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6906 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6907 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6908 | memcpy(&wc->update_progress, &key, |
| 6909 | sizeof(wc->update_progress)); |
| 6910 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6911 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6912 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6913 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6914 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6915 | path->lowest_level = 0; |
| 6916 | if (ret < 0) { |
| 6917 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6918 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6919 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6920 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6921 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6922 | /* |
| 6923 | * unlock our path, this is safe because only this |
| 6924 | * function is allowed to delete this snapshot |
| 6925 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6926 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6927 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6928 | level = btrfs_header_level(root->node); |
| 6929 | while (1) { |
| 6930 | btrfs_tree_lock(path->nodes[level]); |
| 6931 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6932 | |
| 6933 | ret = btrfs_lookup_extent_info(trans, root, |
| 6934 | path->nodes[level]->start, |
| 6935 | path->nodes[level]->len, |
| 6936 | &wc->refs[level], |
| 6937 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6938 | if (ret < 0) { |
| 6939 | err = ret; |
| 6940 | goto out_end_trans; |
| 6941 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6942 | BUG_ON(wc->refs[level] == 0); |
| 6943 | |
| 6944 | if (level == root_item->drop_level) |
| 6945 | break; |
| 6946 | |
| 6947 | btrfs_tree_unlock(path->nodes[level]); |
| 6948 | WARN_ON(wc->refs[level] != 1); |
| 6949 | level--; |
| 6950 | } |
| 6951 | } |
| 6952 | |
| 6953 | wc->level = level; |
| 6954 | wc->shared_level = -1; |
| 6955 | wc->stage = DROP_REFERENCE; |
| 6956 | wc->update_ref = update_ref; |
| 6957 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6958 | wc->for_reloc = for_reloc; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6959 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6960 | |
| 6961 | while (1) { |
| 6962 | ret = walk_down_tree(trans, root, path, wc); |
| 6963 | if (ret < 0) { |
| 6964 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6965 | break; |
| 6966 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6967 | |
| 6968 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6969 | if (ret < 0) { |
| 6970 | err = ret; |
| 6971 | break; |
| 6972 | } |
| 6973 | |
| 6974 | if (ret > 0) { |
| 6975 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6976 | break; |
| 6977 | } |
| 6978 | |
| 6979 | if (wc->stage == DROP_REFERENCE) { |
| 6980 | level = wc->level; |
| 6981 | btrfs_node_key(path->nodes[level], |
| 6982 | &root_item->drop_progress, |
| 6983 | path->slots[level]); |
| 6984 | root_item->drop_level = level; |
| 6985 | } |
| 6986 | |
| 6987 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6988 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6989 | ret = btrfs_update_root(trans, tree_root, |
| 6990 | &root->root_key, |
| 6991 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6992 | if (ret) { |
| 6993 | btrfs_abort_transaction(trans, tree_root, ret); |
| 6994 | err = ret; |
| 6995 | goto out_end_trans; |
| 6996 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6997 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6998 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6999 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7000 | if (IS_ERR(trans)) { |
| 7001 | err = PTR_ERR(trans); |
| 7002 | goto out_free; |
| 7003 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7004 | if (block_rsv) |
| 7005 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7006 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7007 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7008 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7009 | if (err) |
| 7010 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7011 | |
| 7012 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7013 | if (ret) { |
| 7014 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7015 | goto out_end_trans; |
| 7016 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7017 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7018 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 7019 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 7020 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7021 | if (ret < 0) { |
| 7022 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7023 | err = ret; |
| 7024 | goto out_end_trans; |
| 7025 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 7026 | /* if we fail to delete the orphan item this time |
| 7027 | * around, it'll get picked up the next time. |
| 7028 | * |
| 7029 | * The most common failure here is just -ENOENT. |
| 7030 | */ |
| 7031 | btrfs_del_orphan_item(trans, tree_root, |
| 7032 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7033 | } |
| 7034 | } |
| 7035 | |
| 7036 | if (root->in_radix) { |
| 7037 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 7038 | } else { |
| 7039 | free_extent_buffer(root->node); |
| 7040 | free_extent_buffer(root->commit_root); |
| 7041 | kfree(root); |
| 7042 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7043 | out_end_trans: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7044 | btrfs_end_transaction_throttle(trans, tree_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7045 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7046 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7047 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7048 | out: |
| 7049 | if (err) |
| 7050 | btrfs_std_error(root->fs_info, err); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 7051 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7052 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7053 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7054 | /* |
| 7055 | * drop subtree rooted at tree block 'node'. |
| 7056 | * |
| 7057 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7058 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7059 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7060 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 7061 | struct btrfs_root *root, |
| 7062 | struct extent_buffer *node, |
| 7063 | struct extent_buffer *parent) |
| 7064 | { |
| 7065 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7066 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7067 | int level; |
| 7068 | int parent_level; |
| 7069 | int ret = 0; |
| 7070 | int wret; |
| 7071 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7072 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 7073 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7074 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7075 | if (!path) |
| 7076 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7077 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7078 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7079 | if (!wc) { |
| 7080 | btrfs_free_path(path); |
| 7081 | return -ENOMEM; |
| 7082 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7083 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 7084 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7085 | parent_level = btrfs_header_level(parent); |
| 7086 | extent_buffer_get(parent); |
| 7087 | path->nodes[parent_level] = parent; |
| 7088 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 7089 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 7090 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7091 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7092 | path->nodes[level] = node; |
| 7093 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7094 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7095 | |
| 7096 | wc->refs[parent_level] = 1; |
| 7097 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7098 | wc->level = level; |
| 7099 | wc->shared_level = -1; |
| 7100 | wc->stage = DROP_REFERENCE; |
| 7101 | wc->update_ref = 0; |
| 7102 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7103 | wc->for_reloc = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7104 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7105 | |
| 7106 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7107 | wret = walk_down_tree(trans, root, path, wc); |
| 7108 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7109 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7110 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7111 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7112 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7113 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7114 | if (wret < 0) |
| 7115 | ret = wret; |
| 7116 | if (wret != 0) |
| 7117 | break; |
| 7118 | } |
| 7119 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7120 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7121 | btrfs_free_path(path); |
| 7122 | return ret; |
| 7123 | } |
| 7124 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7125 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7126 | { |
| 7127 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7128 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7129 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7130 | /* |
| 7131 | * if restripe for this chunk_type is on pick target profile and |
| 7132 | * return, otherwise do the usual balance |
| 7133 | */ |
| 7134 | stripped = get_restripe_target(root->fs_info, flags); |
| 7135 | if (stripped) |
| 7136 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 7137 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 7138 | /* |
| 7139 | * we add in the count of missing devices because we want |
| 7140 | * to make sure that any RAID levels on a degraded FS |
| 7141 | * continue to be honored. |
| 7142 | */ |
| 7143 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 7144 | root->fs_info->fs_devices->missing_devices; |
| 7145 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7146 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7147 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7148 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7149 | if (num_devices == 1) { |
| 7150 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7151 | stripped = flags & ~stripped; |
| 7152 | |
| 7153 | /* turn raid0 into single device chunks */ |
| 7154 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7155 | return stripped; |
| 7156 | |
| 7157 | /* turn mirroring into duplication */ |
| 7158 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7159 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7160 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7161 | } else { |
| 7162 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7163 | if (flags & stripped) |
| 7164 | return flags; |
| 7165 | |
| 7166 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7167 | stripped = flags & ~stripped; |
| 7168 | |
| 7169 | /* switch duplicated blocks with raid1 */ |
| 7170 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7171 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7172 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7173 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7174 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7175 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7176 | return flags; |
| 7177 | } |
| 7178 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7179 | 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] | 7180 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7181 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7182 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7183 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7184 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7185 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7186 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7187 | /* |
| 7188 | * We need some metadata space and system metadata space for |
| 7189 | * allocating chunks in some corner cases until we force to set |
| 7190 | * it to be readonly. |
| 7191 | */ |
| 7192 | if ((sinfo->flags & |
| 7193 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 7194 | !force) |
| 7195 | min_allocable_bytes = 1 * 1024 * 1024; |
| 7196 | else |
| 7197 | min_allocable_bytes = 0; |
| 7198 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7199 | spin_lock(&sinfo->lock); |
| 7200 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7201 | |
| 7202 | if (cache->ro) { |
| 7203 | ret = 0; |
| 7204 | goto out; |
| 7205 | } |
| 7206 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7207 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7208 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7209 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7210 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 7211 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 7212 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7213 | sinfo->bytes_readonly += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7214 | cache->ro = 1; |
| 7215 | ret = 0; |
| 7216 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 7217 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7218 | spin_unlock(&cache->lock); |
| 7219 | spin_unlock(&sinfo->lock); |
| 7220 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7221 | } |
| 7222 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7223 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 7224 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7225 | |
| 7226 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7227 | struct btrfs_trans_handle *trans; |
| 7228 | u64 alloc_flags; |
| 7229 | int ret; |
| 7230 | |
| 7231 | BUG_ON(cache->ro); |
| 7232 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 7233 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7234 | if (IS_ERR(trans)) |
| 7235 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7236 | |
| 7237 | alloc_flags = update_block_group_flags(root, cache->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7238 | if (alloc_flags != cache->flags) { |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 7239 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7240 | CHUNK_ALLOC_FORCE); |
| 7241 | if (ret < 0) |
| 7242 | goto out; |
| 7243 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7244 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7245 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7246 | if (!ret) |
| 7247 | goto out; |
| 7248 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 7249 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 7250 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7251 | if (ret < 0) |
| 7252 | goto out; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7253 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7254 | out: |
| 7255 | btrfs_end_transaction(trans, root); |
| 7256 | return ret; |
| 7257 | } |
| 7258 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 7259 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 7260 | struct btrfs_root *root, u64 type) |
| 7261 | { |
| 7262 | u64 alloc_flags = get_alloc_profile(root, type); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 7263 | return do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 7264 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 7265 | } |
| 7266 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 7267 | /* |
| 7268 | * helper to account the unused space of all the readonly block group in the |
| 7269 | * list. takes mirrors into account. |
| 7270 | */ |
| 7271 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 7272 | { |
| 7273 | struct btrfs_block_group_cache *block_group; |
| 7274 | u64 free_bytes = 0; |
| 7275 | int factor; |
| 7276 | |
| 7277 | list_for_each_entry(block_group, groups_list, list) { |
| 7278 | spin_lock(&block_group->lock); |
| 7279 | |
| 7280 | if (!block_group->ro) { |
| 7281 | spin_unlock(&block_group->lock); |
| 7282 | continue; |
| 7283 | } |
| 7284 | |
| 7285 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7286 | BTRFS_BLOCK_GROUP_RAID10 | |
| 7287 | BTRFS_BLOCK_GROUP_DUP)) |
| 7288 | factor = 2; |
| 7289 | else |
| 7290 | factor = 1; |
| 7291 | |
| 7292 | free_bytes += (block_group->key.offset - |
| 7293 | btrfs_block_group_used(&block_group->item)) * |
| 7294 | factor; |
| 7295 | |
| 7296 | spin_unlock(&block_group->lock); |
| 7297 | } |
| 7298 | |
| 7299 | return free_bytes; |
| 7300 | } |
| 7301 | |
| 7302 | /* |
| 7303 | * helper to account the unused space of all the readonly block group in the |
| 7304 | * space_info. takes mirrors into account. |
| 7305 | */ |
| 7306 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 7307 | { |
| 7308 | int i; |
| 7309 | u64 free_bytes = 0; |
| 7310 | |
| 7311 | spin_lock(&sinfo->lock); |
| 7312 | |
| 7313 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 7314 | if (!list_empty(&sinfo->block_groups[i])) |
| 7315 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 7316 | &sinfo->block_groups[i]); |
| 7317 | |
| 7318 | spin_unlock(&sinfo->lock); |
| 7319 | |
| 7320 | return free_bytes; |
| 7321 | } |
| 7322 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 7323 | void btrfs_set_block_group_rw(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7324 | struct btrfs_block_group_cache *cache) |
| 7325 | { |
| 7326 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7327 | u64 num_bytes; |
| 7328 | |
| 7329 | BUG_ON(!cache->ro); |
| 7330 | |
| 7331 | spin_lock(&sinfo->lock); |
| 7332 | spin_lock(&cache->lock); |
| 7333 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7334 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 7335 | sinfo->bytes_readonly -= num_bytes; |
| 7336 | cache->ro = 0; |
| 7337 | spin_unlock(&cache->lock); |
| 7338 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7339 | } |
| 7340 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7341 | /* |
| 7342 | * checks to see if its even possible to relocate this block group. |
| 7343 | * |
| 7344 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7345 | * ok to go ahead and try. |
| 7346 | */ |
| 7347 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7348 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7349 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7350 | struct btrfs_space_info *space_info; |
| 7351 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7352 | struct btrfs_device *device; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7353 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 7354 | u64 dev_min = 1; |
| 7355 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7356 | u64 target; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7357 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7358 | int full = 0; |
| 7359 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7360 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7361 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7362 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7363 | /* odd, couldn't find the block group, leave it alone */ |
| 7364 | if (!block_group) |
| 7365 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7366 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7367 | min_free = btrfs_block_group_used(&block_group->item); |
| 7368 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7369 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7370 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7371 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7372 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7373 | space_info = block_group->space_info; |
| 7374 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7375 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7376 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7377 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7378 | /* |
| 7379 | * 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] | 7380 | * relocate it unless we're able to allocate a new chunk below. |
| 7381 | * |
| 7382 | * Otherwise, we need to make sure we have room in the space to handle |
| 7383 | * 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] | 7384 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7385 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7386 | (space_info->bytes_used + space_info->bytes_reserved + |
| 7387 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7388 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7389 | spin_unlock(&space_info->lock); |
| 7390 | goto out; |
| 7391 | } |
| 7392 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7393 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7394 | /* |
| 7395 | * ok we don't have enough space, but maybe we have free space on our |
| 7396 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7397 | * alloc devices and guess if we have enough space. if this block |
| 7398 | * group is going to be restriped, run checks against the target |
| 7399 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7400 | */ |
| 7401 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 7402 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7403 | /* |
| 7404 | * index: |
| 7405 | * 0: raid10 |
| 7406 | * 1: raid1 |
| 7407 | * 2: dup |
| 7408 | * 3: raid0 |
| 7409 | * 4: single |
| 7410 | */ |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 7411 | target = get_restripe_target(root->fs_info, block_group->flags); |
| 7412 | if (target) { |
| 7413 | index = __get_block_group_index(extended_to_chunk(target)); |
| 7414 | } else { |
| 7415 | /* |
| 7416 | * this is just a balance, so if we were marked as full |
| 7417 | * we know there is no space for a new chunk |
| 7418 | */ |
| 7419 | if (full) |
| 7420 | goto out; |
| 7421 | |
| 7422 | index = get_block_group_index(block_group); |
| 7423 | } |
| 7424 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7425 | if (index == 0) { |
| 7426 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 7427 | /* Divide by 2 */ |
| 7428 | min_free >>= 1; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7429 | } else if (index == 1) { |
| 7430 | dev_min = 2; |
| 7431 | } else if (index == 2) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 7432 | /* Multiply by 2 */ |
| 7433 | min_free <<= 1; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7434 | } else if (index == 3) { |
| 7435 | dev_min = fs_devices->rw_devices; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 7436 | do_div(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7437 | } |
| 7438 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7439 | mutex_lock(&root->fs_info->chunk_mutex); |
| 7440 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 7441 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7442 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7443 | /* |
| 7444 | * check to make sure we can actually find a chunk with enough |
| 7445 | * space to fit our block group in. |
| 7446 | */ |
| 7447 | if (device->total_bytes > device->bytes_used + min_free) { |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 7448 | ret = find_free_dev_extent(device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 7449 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7450 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7451 | dev_nr++; |
| 7452 | |
| 7453 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7454 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 7455 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7456 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7457 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7458 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7459 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7460 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7461 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7462 | return ret; |
| 7463 | } |
| 7464 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7465 | static int find_first_block_group(struct btrfs_root *root, |
| 7466 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7467 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7468 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7469 | struct btrfs_key found_key; |
| 7470 | struct extent_buffer *leaf; |
| 7471 | int slot; |
| 7472 | |
| 7473 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7474 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7475 | goto out; |
| 7476 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7477 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7478 | slot = path->slots[0]; |
| 7479 | leaf = path->nodes[0]; |
| 7480 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7481 | ret = btrfs_next_leaf(root, path); |
| 7482 | if (ret == 0) |
| 7483 | continue; |
| 7484 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7485 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7486 | break; |
| 7487 | } |
| 7488 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7489 | |
| 7490 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7491 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7492 | ret = 0; |
| 7493 | goto out; |
| 7494 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7495 | path->slots[0]++; |
| 7496 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7497 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7498 | return ret; |
| 7499 | } |
| 7500 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7501 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 7502 | { |
| 7503 | struct btrfs_block_group_cache *block_group; |
| 7504 | u64 last = 0; |
| 7505 | |
| 7506 | while (1) { |
| 7507 | struct inode *inode; |
| 7508 | |
| 7509 | block_group = btrfs_lookup_first_block_group(info, last); |
| 7510 | while (block_group) { |
| 7511 | spin_lock(&block_group->lock); |
| 7512 | if (block_group->iref) |
| 7513 | break; |
| 7514 | spin_unlock(&block_group->lock); |
| 7515 | block_group = next_block_group(info->tree_root, |
| 7516 | block_group); |
| 7517 | } |
| 7518 | if (!block_group) { |
| 7519 | if (last == 0) |
| 7520 | break; |
| 7521 | last = 0; |
| 7522 | continue; |
| 7523 | } |
| 7524 | |
| 7525 | inode = block_group->inode; |
| 7526 | block_group->iref = 0; |
| 7527 | block_group->inode = NULL; |
| 7528 | spin_unlock(&block_group->lock); |
| 7529 | iput(inode); |
| 7530 | last = block_group->key.objectid + block_group->key.offset; |
| 7531 | btrfs_put_block_group(block_group); |
| 7532 | } |
| 7533 | } |
| 7534 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7535 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7536 | { |
| 7537 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7538 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7539 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7540 | struct rb_node *n; |
| 7541 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7542 | down_write(&info->extent_commit_sem); |
| 7543 | while (!list_empty(&info->caching_block_groups)) { |
| 7544 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7545 | struct btrfs_caching_control, list); |
| 7546 | list_del(&caching_ctl->list); |
| 7547 | put_caching_control(caching_ctl); |
| 7548 | } |
| 7549 | up_write(&info->extent_commit_sem); |
| 7550 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7551 | spin_lock(&info->block_group_cache_lock); |
| 7552 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7553 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7554 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7555 | rb_erase(&block_group->cache_node, |
| 7556 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7557 | spin_unlock(&info->block_group_cache_lock); |
| 7558 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7559 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7560 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7561 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7562 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7563 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7564 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7565 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7566 | /* |
| 7567 | * We haven't cached this block group, which means we could |
| 7568 | * possibly have excluded extents on this block group. |
| 7569 | */ |
| 7570 | if (block_group->cached == BTRFS_CACHE_NO) |
| 7571 | free_excluded_extents(info->extent_root, block_group); |
| 7572 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7573 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 7574 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7575 | |
| 7576 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7577 | } |
| 7578 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7579 | |
| 7580 | /* now that all the block groups are freed, go through and |
| 7581 | * free all the space_info structs. This is only called during |
| 7582 | * the final stages of unmount, and so we know nobody is |
| 7583 | * using them. We call synchronize_rcu() once before we start, |
| 7584 | * just to be on the safe side. |
| 7585 | */ |
| 7586 | synchronize_rcu(); |
| 7587 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7588 | release_global_block_rsv(info); |
| 7589 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7590 | while(!list_empty(&info->space_info)) { |
| 7591 | space_info = list_entry(info->space_info.next, |
| 7592 | struct btrfs_space_info, |
| 7593 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7594 | if (space_info->bytes_pinned > 0 || |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 7595 | space_info->bytes_reserved > 0 || |
| 7596 | space_info->bytes_may_use > 0) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7597 | WARN_ON(1); |
| 7598 | dump_space_info(space_info, 0, 0); |
| 7599 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7600 | list_del(&space_info->list); |
| 7601 | kfree(space_info); |
| 7602 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7603 | return 0; |
| 7604 | } |
| 7605 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7606 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 7607 | struct btrfs_block_group_cache *cache) |
| 7608 | { |
| 7609 | int index = get_block_group_index(cache); |
| 7610 | |
| 7611 | down_write(&space_info->groups_sem); |
| 7612 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 7613 | up_write(&space_info->groups_sem); |
| 7614 | } |
| 7615 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7616 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7617 | { |
| 7618 | struct btrfs_path *path; |
| 7619 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7620 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7621 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7622 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7623 | struct btrfs_key key; |
| 7624 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7625 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7626 | int need_clear = 0; |
| 7627 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7628 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7629 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7630 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7631 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7632 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7633 | path = btrfs_alloc_path(); |
| 7634 | if (!path) |
| 7635 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 7636 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7637 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 7638 | cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 7639 | if (btrfs_test_opt(root, SPACE_CACHE) && |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 7640 | btrfs_super_generation(root->fs_info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7641 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 7642 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 7643 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7644 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7645 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7646 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7647 | if (ret > 0) |
| 7648 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7649 | if (ret != 0) |
| 7650 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7651 | leaf = path->nodes[0]; |
| 7652 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7653 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7654 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7655 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7656 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7657 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7658 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7659 | GFP_NOFS); |
| 7660 | if (!cache->free_space_ctl) { |
| 7661 | kfree(cache); |
| 7662 | ret = -ENOMEM; |
| 7663 | goto error; |
| 7664 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7665 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7666 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7667 | spin_lock_init(&cache->lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7668 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7669 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7670 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7671 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 7672 | if (need_clear) { |
| 7673 | /* |
| 7674 | * When we mount with old space cache, we need to |
| 7675 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 7676 | * |
| 7677 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 7678 | * truncate the old free space cache inode and |
| 7679 | * setup a new one. |
| 7680 | * b) Setting 'dirty flag' makes sure that we flush |
| 7681 | * the new space cache info onto disk. |
| 7682 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7683 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 7684 | if (btrfs_test_opt(root, SPACE_CACHE)) |
| 7685 | cache->dirty = 1; |
| 7686 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7687 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7688 | read_extent_buffer(leaf, &cache->item, |
| 7689 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7690 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7691 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7692 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7693 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7694 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7695 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7696 | cache->sectorsize = root->sectorsize; |
| 7697 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7698 | btrfs_init_free_space_ctl(cache); |
| 7699 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7700 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7701 | * We need to exclude the super stripes now so that the space |
| 7702 | * info has super bytes accounted for, otherwise we'll think |
| 7703 | * we have more space than we actually do. |
| 7704 | */ |
| 7705 | exclude_super_stripes(root, cache); |
| 7706 | |
| 7707 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7708 | * check for two cases, either we are full, and therefore |
| 7709 | * don't need to bother with the caching work since we won't |
| 7710 | * find any space, or we are empty, and we can just add all |
| 7711 | * the space in and be done with it. This saves us _alot_ of |
| 7712 | * time, particularly in the full case. |
| 7713 | */ |
| 7714 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7715 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7716 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7717 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7718 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7719 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7720 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7721 | add_new_free_space(cache, root->fs_info, |
| 7722 | found_key.objectid, |
| 7723 | found_key.objectid + |
| 7724 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7725 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7726 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7727 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7728 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7729 | btrfs_block_group_used(&cache->item), |
| 7730 | &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7731 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7732 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7733 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7734 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7735 | spin_unlock(&cache->space_info->lock); |
| 7736 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7737 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7738 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7739 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7740 | BUG_ON(ret); /* Logic error */ |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7741 | |
| 7742 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7743 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7744 | set_block_group_ro(cache, 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7745 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7746 | |
| 7747 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 7748 | if (!(get_alloc_profile(root, space_info->flags) & |
| 7749 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 7750 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7751 | BTRFS_BLOCK_GROUP_DUP))) |
| 7752 | continue; |
| 7753 | /* |
| 7754 | * avoid allocating from un-mirrored block group if there are |
| 7755 | * mirrored block groups. |
| 7756 | */ |
| 7757 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7758 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7759 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7760 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7761 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7762 | |
| 7763 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7764 | ret = 0; |
| 7765 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7766 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7767 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7768 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7769 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7770 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
| 7771 | struct btrfs_root *root) |
| 7772 | { |
| 7773 | struct btrfs_block_group_cache *block_group, *tmp; |
| 7774 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 7775 | struct btrfs_block_group_item item; |
| 7776 | struct btrfs_key key; |
| 7777 | int ret = 0; |
| 7778 | |
| 7779 | list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, |
| 7780 | new_bg_list) { |
| 7781 | list_del_init(&block_group->new_bg_list); |
| 7782 | |
| 7783 | if (ret) |
| 7784 | continue; |
| 7785 | |
| 7786 | spin_lock(&block_group->lock); |
| 7787 | memcpy(&item, &block_group->item, sizeof(item)); |
| 7788 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7789 | spin_unlock(&block_group->lock); |
| 7790 | |
| 7791 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 7792 | sizeof(item)); |
| 7793 | if (ret) |
| 7794 | btrfs_abort_transaction(trans, extent_root, ret); |
| 7795 | } |
| 7796 | } |
| 7797 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7798 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7799 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7800 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7801 | u64 size) |
| 7802 | { |
| 7803 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7804 | struct btrfs_root *extent_root; |
| 7805 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7806 | |
| 7807 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7808 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7809 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7810 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7811 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7812 | if (!cache) |
| 7813 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7814 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7815 | GFP_NOFS); |
| 7816 | if (!cache->free_space_ctl) { |
| 7817 | kfree(cache); |
| 7818 | return -ENOMEM; |
| 7819 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7820 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7821 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7822 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7823 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7824 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7825 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7826 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7827 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7828 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7829 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7830 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7831 | INIT_LIST_HEAD(&cache->new_bg_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7832 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7833 | btrfs_init_free_space_ctl(cache); |
| 7834 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7835 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7836 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7837 | cache->flags = type; |
| 7838 | btrfs_set_block_group_flags(&cache->item, type); |
| 7839 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7840 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7841 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7842 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7843 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7844 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7845 | chunk_offset + size); |
| 7846 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7847 | free_excluded_extents(root, cache); |
| 7848 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7849 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7850 | &cache->space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7851 | BUG_ON(ret); /* -ENOMEM */ |
Li Zefan | c7c144d | 2011-12-07 10:39:22 +0800 | [diff] [blame] | 7852 | update_global_block_rsv(root->fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7853 | |
| 7854 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7855 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7856 | spin_unlock(&cache->space_info->lock); |
| 7857 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7858 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7859 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7860 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7861 | BUG_ON(ret); /* Logic error */ |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7862 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 7863 | list_add_tail(&cache->new_bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7864 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7865 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7866 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7867 | return 0; |
| 7868 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7869 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 7870 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 7871 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 7872 | u64 extra_flags = chunk_to_extended(flags) & |
| 7873 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 7874 | |
| 7875 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 7876 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 7877 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 7878 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 7879 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 7880 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
| 7881 | } |
| 7882 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7883 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7884 | struct btrfs_root *root, u64 group_start) |
| 7885 | { |
| 7886 | struct btrfs_path *path; |
| 7887 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7888 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7889 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7890 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7891 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7892 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 7893 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7894 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7895 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7896 | root = root->fs_info->extent_root; |
| 7897 | |
| 7898 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7899 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7900 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7901 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 7902 | /* |
| 7903 | * Free the reserved super bytes from this block group before |
| 7904 | * remove it. |
| 7905 | */ |
| 7906 | free_excluded_extents(root, block_group); |
| 7907 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7908 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 7909 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7910 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 7911 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7912 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7913 | factor = 2; |
| 7914 | else |
| 7915 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7916 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7917 | /* make sure this block group isn't part of an allocation cluster */ |
| 7918 | cluster = &root->fs_info->data_alloc_cluster; |
| 7919 | spin_lock(&cluster->refill_lock); |
| 7920 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7921 | spin_unlock(&cluster->refill_lock); |
| 7922 | |
| 7923 | /* |
| 7924 | * make sure this block group isn't part of a metadata |
| 7925 | * allocation cluster |
| 7926 | */ |
| 7927 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7928 | spin_lock(&cluster->refill_lock); |
| 7929 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7930 | spin_unlock(&cluster->refill_lock); |
| 7931 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7932 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7933 | if (!path) { |
| 7934 | ret = -ENOMEM; |
| 7935 | goto out; |
| 7936 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7937 | |
Ilya Dryomov | 10b2f34 | 2011-10-02 13:56:53 +0300 | [diff] [blame] | 7938 | inode = lookup_free_space_inode(tree_root, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7939 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 7940 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7941 | if (ret) { |
| 7942 | btrfs_add_delayed_iput(inode); |
| 7943 | goto out; |
| 7944 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7945 | clear_nlink(inode); |
| 7946 | /* One for the block groups ref */ |
| 7947 | spin_lock(&block_group->lock); |
| 7948 | if (block_group->iref) { |
| 7949 | block_group->iref = 0; |
| 7950 | block_group->inode = NULL; |
| 7951 | spin_unlock(&block_group->lock); |
| 7952 | iput(inode); |
| 7953 | } else { |
| 7954 | spin_unlock(&block_group->lock); |
| 7955 | } |
| 7956 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 7957 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7958 | } |
| 7959 | |
| 7960 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 7961 | key.offset = block_group->key.objectid; |
| 7962 | key.type = 0; |
| 7963 | |
| 7964 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 7965 | if (ret < 0) |
| 7966 | goto out; |
| 7967 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7968 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7969 | if (ret == 0) { |
| 7970 | ret = btrfs_del_item(trans, tree_root, path); |
| 7971 | if (ret) |
| 7972 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7973 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7974 | } |
| 7975 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7976 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7977 | rb_erase(&block_group->cache_node, |
| 7978 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7979 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7980 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7981 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7982 | /* |
| 7983 | * we must use list_del_init so people can check to see if they |
| 7984 | * are still on the list after taking the semaphore |
| 7985 | */ |
| 7986 | list_del_init(&block_group->list); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 7987 | if (list_empty(&block_group->space_info->block_groups[index])) |
| 7988 | clear_avail_alloc_bits(root->fs_info, block_group->flags); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7989 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7990 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7991 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7992 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7993 | |
| 7994 | btrfs_remove_free_space_cache(block_group); |
| 7995 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7996 | spin_lock(&block_group->space_info->lock); |
| 7997 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7998 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7999 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8000 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8001 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8002 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8003 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8004 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8005 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8006 | btrfs_put_block_group(block_group); |
| 8007 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8008 | |
| 8009 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8010 | if (ret > 0) |
| 8011 | ret = -EIO; |
| 8012 | if (ret < 0) |
| 8013 | goto out; |
| 8014 | |
| 8015 | ret = btrfs_del_item(trans, root, path); |
| 8016 | out: |
| 8017 | btrfs_free_path(path); |
| 8018 | return ret; |
| 8019 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8020 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8021 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 8022 | { |
| 8023 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8024 | struct btrfs_super_block *disk_super; |
| 8025 | u64 features; |
| 8026 | u64 flags; |
| 8027 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8028 | int ret; |
| 8029 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 8030 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8031 | if (!btrfs_super_root(disk_super)) |
| 8032 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8033 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8034 | features = btrfs_super_incompat_flags(disk_super); |
| 8035 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 8036 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8037 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8038 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 8039 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8040 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8041 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8042 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8043 | if (mixed) { |
| 8044 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 8045 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8046 | } else { |
| 8047 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 8048 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8049 | if (ret) |
| 8050 | goto out; |
| 8051 | |
| 8052 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 8053 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8054 | } |
| 8055 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8056 | return ret; |
| 8057 | } |
| 8058 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8059 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 8060 | { |
| 8061 | return unpin_extent_range(root, start, end); |
| 8062 | } |
| 8063 | |
| 8064 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8065 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8066 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8067 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8068 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8069 | |
| 8070 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 8071 | { |
| 8072 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 8073 | struct btrfs_block_group_cache *cache = NULL; |
| 8074 | u64 group_trimmed; |
| 8075 | u64 start; |
| 8076 | u64 end; |
| 8077 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 8078 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8079 | int ret = 0; |
| 8080 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 8081 | /* |
| 8082 | * try to trim all FS space, our block group may start from non-zero. |
| 8083 | */ |
| 8084 | if (range->len == total_bytes) |
| 8085 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 8086 | else |
| 8087 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8088 | |
| 8089 | while (cache) { |
| 8090 | if (cache->key.objectid >= (range->start + range->len)) { |
| 8091 | btrfs_put_block_group(cache); |
| 8092 | break; |
| 8093 | } |
| 8094 | |
| 8095 | start = max(range->start, cache->key.objectid); |
| 8096 | end = min(range->start + range->len, |
| 8097 | cache->key.objectid + cache->key.offset); |
| 8098 | |
| 8099 | if (end - start >= range->minlen) { |
| 8100 | if (!block_group_cache_done(cache)) { |
| 8101 | ret = cache_block_group(cache, NULL, root, 0); |
| 8102 | if (!ret) |
| 8103 | wait_block_group_cache_done(cache); |
| 8104 | } |
| 8105 | ret = btrfs_trim_block_group(cache, |
| 8106 | &group_trimmed, |
| 8107 | start, |
| 8108 | end, |
| 8109 | range->minlen); |
| 8110 | |
| 8111 | trimmed += group_trimmed; |
| 8112 | if (ret) { |
| 8113 | btrfs_put_block_group(cache); |
| 8114 | break; |
| 8115 | } |
| 8116 | } |
| 8117 | |
| 8118 | cache = next_block_group(fs_info->tree_root, cache); |
| 8119 | } |
| 8120 | |
| 8121 | range->len = trimmed; |
| 8122 | return ret; |
| 8123 | } |