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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 26 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 27 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 33 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 34 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 36 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 37 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 38 | u64 bytenr, u64 num_bytes, int alloc); |
| 39 | static int update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 40 | u64 num_bytes, int reserve, int sinfo); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 41 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 42 | struct btrfs_root *root, |
| 43 | u64 bytenr, u64 num_bytes, u64 parent, |
| 44 | u64 root_objectid, u64 owner_objectid, |
| 45 | u64 owner_offset, int refs_to_drop, |
| 46 | struct btrfs_delayed_extent_op *extra_op); |
| 47 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 48 | struct extent_buffer *leaf, |
| 49 | struct btrfs_extent_item *ei); |
| 50 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 51 | struct btrfs_root *root, |
| 52 | u64 parent, u64 root_objectid, |
| 53 | u64 flags, u64 owner, u64 offset, |
| 54 | struct btrfs_key *ins, int ref_mod); |
| 55 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
| 57 | u64 parent, u64 root_objectid, |
| 58 | u64 flags, struct btrfs_disk_key *key, |
| 59 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 60 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 62 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 63 | static int find_next_key(struct btrfs_path *path, int level, |
| 64 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 65 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 66 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 67 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 68 | static noinline int |
| 69 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 70 | { |
| 71 | smp_mb(); |
| 72 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 73 | } |
| 74 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 75 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 76 | { |
| 77 | return (cache->flags & bits) == bits; |
| 78 | } |
| 79 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 80 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 81 | { |
| 82 | atomic_inc(&cache->count); |
| 83 | } |
| 84 | |
| 85 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 86 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 87 | if (atomic_dec_and_test(&cache->count)) { |
| 88 | WARN_ON(cache->pinned > 0); |
| 89 | WARN_ON(cache->reserved > 0); |
| 90 | WARN_ON(cache->reserved_pinned > 0); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 91 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 92 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 95 | /* |
| 96 | * this adds the block group to the fs_info rb tree for the block group |
| 97 | * cache |
| 98 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 99 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 100 | struct btrfs_block_group_cache *block_group) |
| 101 | { |
| 102 | struct rb_node **p; |
| 103 | struct rb_node *parent = NULL; |
| 104 | struct btrfs_block_group_cache *cache; |
| 105 | |
| 106 | spin_lock(&info->block_group_cache_lock); |
| 107 | p = &info->block_group_cache_tree.rb_node; |
| 108 | |
| 109 | while (*p) { |
| 110 | parent = *p; |
| 111 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 112 | cache_node); |
| 113 | if (block_group->key.objectid < cache->key.objectid) { |
| 114 | p = &(*p)->rb_left; |
| 115 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 116 | p = &(*p)->rb_right; |
| 117 | } else { |
| 118 | spin_unlock(&info->block_group_cache_lock); |
| 119 | return -EEXIST; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | rb_link_node(&block_group->cache_node, parent, p); |
| 124 | rb_insert_color(&block_group->cache_node, |
| 125 | &info->block_group_cache_tree); |
| 126 | spin_unlock(&info->block_group_cache_lock); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * This will return the block group at or after bytenr if contains is 0, else |
| 133 | * it will return the block group that contains the bytenr |
| 134 | */ |
| 135 | static struct btrfs_block_group_cache * |
| 136 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 137 | int contains) |
| 138 | { |
| 139 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 140 | struct rb_node *n; |
| 141 | u64 end, start; |
| 142 | |
| 143 | spin_lock(&info->block_group_cache_lock); |
| 144 | n = info->block_group_cache_tree.rb_node; |
| 145 | |
| 146 | while (n) { |
| 147 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 148 | cache_node); |
| 149 | end = cache->key.objectid + cache->key.offset - 1; |
| 150 | start = cache->key.objectid; |
| 151 | |
| 152 | if (bytenr < start) { |
| 153 | if (!contains && (!ret || start < ret->key.objectid)) |
| 154 | ret = cache; |
| 155 | n = n->rb_left; |
| 156 | } else if (bytenr > start) { |
| 157 | if (contains && bytenr <= end) { |
| 158 | ret = cache; |
| 159 | break; |
| 160 | } |
| 161 | n = n->rb_right; |
| 162 | } else { |
| 163 | ret = cache; |
| 164 | break; |
| 165 | } |
| 166 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 167 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 168 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 169 | spin_unlock(&info->block_group_cache_lock); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 174 | static int add_excluded_extent(struct btrfs_root *root, |
| 175 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 176 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 177 | u64 end = start + num_bytes - 1; |
| 178 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 179 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 180 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 181 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 182 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 185 | static void free_excluded_extents(struct btrfs_root *root, |
| 186 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 187 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 188 | u64 start, end; |
| 189 | |
| 190 | start = cache->key.objectid; |
| 191 | end = start + cache->key.offset - 1; |
| 192 | |
| 193 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 194 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 195 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 196 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 197 | } |
| 198 | |
| 199 | static int exclude_super_stripes(struct btrfs_root *root, |
| 200 | struct btrfs_block_group_cache *cache) |
| 201 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 202 | u64 bytenr; |
| 203 | u64 *logical; |
| 204 | int stripe_len; |
| 205 | int i, nr, ret; |
| 206 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 207 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 208 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 209 | cache->bytes_super += stripe_len; |
| 210 | ret = add_excluded_extent(root, cache->key.objectid, |
| 211 | stripe_len); |
| 212 | BUG_ON(ret); |
| 213 | } |
| 214 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 215 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 216 | bytenr = btrfs_sb_offset(i); |
| 217 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 218 | cache->key.objectid, bytenr, |
| 219 | 0, &logical, &nr, &stripe_len); |
| 220 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 221 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 222 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 223 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 224 | ret = add_excluded_extent(root, logical[nr], |
| 225 | stripe_len); |
| 226 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 227 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 229 | kfree(logical); |
| 230 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 234 | static struct btrfs_caching_control * |
| 235 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 236 | { |
| 237 | struct btrfs_caching_control *ctl; |
| 238 | |
| 239 | spin_lock(&cache->lock); |
| 240 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 241 | spin_unlock(&cache->lock); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | ctl = cache->caching_ctl; |
| 246 | atomic_inc(&ctl->count); |
| 247 | spin_unlock(&cache->lock); |
| 248 | return ctl; |
| 249 | } |
| 250 | |
| 251 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 252 | { |
| 253 | if (atomic_dec_and_test(&ctl->count)) |
| 254 | kfree(ctl); |
| 255 | } |
| 256 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 257 | /* |
| 258 | * this is only called by cache_block_group, since we could have freed extents |
| 259 | * we need to check the pinned_extents for any extents that can't be used yet |
| 260 | * since their free space will be released as soon as the transaction commits. |
| 261 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 262 | 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] | 263 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 264 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 265 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 266 | int ret; |
| 267 | |
| 268 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 269 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 270 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 271 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | if (ret) |
| 273 | break; |
| 274 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 275 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 276 | start = extent_end + 1; |
| 277 | } else if (extent_start > start && extent_start < end) { |
| 278 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 279 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 280 | ret = btrfs_add_free_space(block_group, start, |
| 281 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 282 | BUG_ON(ret); |
| 283 | start = extent_end + 1; |
| 284 | } else { |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (start < end) { |
| 290 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 291 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 292 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 293 | BUG_ON(ret); |
| 294 | } |
| 295 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 296 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 299 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 300 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 301 | struct btrfs_block_group_cache *block_group = data; |
| 302 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 303 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 304 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 305 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 306 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 307 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 308 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 309 | u64 last = 0; |
| 310 | u32 nritems; |
| 311 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 312 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 313 | path = btrfs_alloc_path(); |
| 314 | if (!path) |
| 315 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 316 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 317 | exclude_super_stripes(extent_root, block_group); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 318 | spin_lock(&block_group->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 319 | block_group->space_info->bytes_readonly += block_group->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 320 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 321 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 322 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 323 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 324 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 325 | * We don't want to deadlock with somebody trying to allocate a new |
| 326 | * extent for the extent root while also trying to search the extent |
| 327 | * root to add free space. So we skip locking and search the commit |
| 328 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 329 | */ |
| 330 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 331 | path->search_commit_root = 1; |
| 332 | path->reada = 2; |
| 333 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 334 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 335 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 336 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 337 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 338 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 339 | /* need to make sure the commit_root doesn't disappear */ |
| 340 | down_read(&fs_info->extent_commit_sem); |
| 341 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 342 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 343 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 344 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 345 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 346 | leaf = path->nodes[0]; |
| 347 | nritems = btrfs_header_nritems(leaf); |
| 348 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 349 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 350 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 351 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 352 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 353 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 354 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 355 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 356 | if (path->slots[0] < nritems) { |
| 357 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 358 | } else { |
| 359 | ret = find_next_key(path, 0, &key); |
| 360 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 361 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 362 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 363 | caching_ctl->progress = last; |
| 364 | btrfs_release_path(extent_root, path); |
| 365 | up_read(&fs_info->extent_commit_sem); |
| 366 | mutex_unlock(&caching_ctl->mutex); |
| 367 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 368 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 369 | else |
| 370 | cond_resched(); |
| 371 | goto again; |
| 372 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 373 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 374 | if (key.objectid < block_group->key.objectid) { |
| 375 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 376 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 377 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 378 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 379 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 380 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 381 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 382 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 383 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 384 | total_found += add_new_free_space(block_group, |
| 385 | fs_info, last, |
| 386 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 387 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 388 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 389 | if (total_found > (1024 * 1024 * 2)) { |
| 390 | total_found = 0; |
| 391 | wake_up(&caching_ctl->wait); |
| 392 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 393 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 394 | path->slots[0]++; |
| 395 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 396 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 397 | |
| 398 | total_found += add_new_free_space(block_group, fs_info, last, |
| 399 | block_group->key.objectid + |
| 400 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 401 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | |
| 403 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 404 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 405 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 406 | spin_unlock(&block_group->lock); |
| 407 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 408 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 409 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 410 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 411 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 412 | free_excluded_extents(extent_root, block_group); |
| 413 | |
| 414 | mutex_unlock(&caching_ctl->mutex); |
| 415 | wake_up(&caching_ctl->wait); |
| 416 | |
| 417 | put_caching_control(caching_ctl); |
| 418 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 419 | btrfs_put_block_group(block_group); |
| 420 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int cache_block_group(struct btrfs_block_group_cache *cache) |
| 425 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 426 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 427 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 428 | struct task_struct *tsk; |
| 429 | int ret = 0; |
| 430 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | smp_mb(); |
| 432 | if (cache->cached != BTRFS_CACHE_NO) |
| 433 | return 0; |
| 434 | |
| 435 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); |
| 436 | BUG_ON(!caching_ctl); |
| 437 | |
| 438 | INIT_LIST_HEAD(&caching_ctl->list); |
| 439 | mutex_init(&caching_ctl->mutex); |
| 440 | init_waitqueue_head(&caching_ctl->wait); |
| 441 | caching_ctl->block_group = cache; |
| 442 | caching_ctl->progress = cache->key.objectid; |
| 443 | /* one for caching kthread, one for caching block group list */ |
| 444 | atomic_set(&caching_ctl->count, 2); |
| 445 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 446 | spin_lock(&cache->lock); |
| 447 | if (cache->cached != BTRFS_CACHE_NO) { |
| 448 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 449 | kfree(caching_ctl); |
| 450 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 451 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 452 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 453 | cache->cached = BTRFS_CACHE_STARTED; |
| 454 | spin_unlock(&cache->lock); |
| 455 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 456 | down_write(&fs_info->extent_commit_sem); |
| 457 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 458 | up_write(&fs_info->extent_commit_sem); |
| 459 | |
| 460 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 461 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 462 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 463 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 464 | cache->key.objectid); |
| 465 | if (IS_ERR(tsk)) { |
| 466 | ret = PTR_ERR(tsk); |
| 467 | printk(KERN_ERR "error running thread %d\n", ret); |
| 468 | BUG(); |
| 469 | } |
| 470 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 471 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 474 | /* |
| 475 | * return the block group that starts at or after bytenr |
| 476 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 477 | static struct btrfs_block_group_cache * |
| 478 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 479 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 480 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 481 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 482 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 483 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 484 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 487 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 488 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 489 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 490 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 491 | struct btrfs_fs_info *info, |
| 492 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 493 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 494 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 495 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 496 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 497 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 498 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 499 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 500 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 501 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 502 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 503 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 504 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 505 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 506 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 507 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 508 | BTRFS_BLOCK_GROUP_METADATA; |
| 509 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 510 | rcu_read_lock(); |
| 511 | list_for_each_entry_rcu(found, head, list) { |
| 512 | if (found->flags == flags) { |
| 513 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 514 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 515 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 516 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 517 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 518 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 521 | /* |
| 522 | * after adding space to the filesystem, we need to clear the full flags |
| 523 | * on all the space infos. |
| 524 | */ |
| 525 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 526 | { |
| 527 | struct list_head *head = &info->space_info; |
| 528 | struct btrfs_space_info *found; |
| 529 | |
| 530 | rcu_read_lock(); |
| 531 | list_for_each_entry_rcu(found, head, list) |
| 532 | found->full = 0; |
| 533 | rcu_read_unlock(); |
| 534 | } |
| 535 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 536 | static u64 div_factor(u64 num, int factor) |
| 537 | { |
| 538 | if (factor == 10) |
| 539 | return num; |
| 540 | num *= factor; |
| 541 | do_div(num, 10); |
| 542 | return num; |
| 543 | } |
| 544 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 545 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 546 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 547 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 548 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 549 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 550 | u64 last = max(search_hint, search_start); |
| 551 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 552 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 553 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 554 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 555 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 556 | while (1) { |
| 557 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 558 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 559 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 560 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 561 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 562 | last = cache->key.objectid + cache->key.offset; |
| 563 | used = btrfs_block_group_used(&cache->item); |
| 564 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 565 | if ((full_search || !cache->ro) && |
| 566 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 567 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 568 | div_factor(cache->key.offset, factor)) { |
| 569 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 570 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 571 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 572 | goto found; |
| 573 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 574 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 575 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 576 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 577 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 578 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 579 | if (!wrapped) { |
| 580 | last = search_start; |
| 581 | wrapped = 1; |
| 582 | goto again; |
| 583 | } |
| 584 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 585 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 586 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 587 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 588 | goto again; |
| 589 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 590 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 591 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 592 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 593 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 594 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 595 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 596 | { |
| 597 | int ret; |
| 598 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 599 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 600 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 601 | path = btrfs_alloc_path(); |
| 602 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 603 | key.objectid = start; |
| 604 | key.offset = len; |
| 605 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 606 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 607 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 608 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 609 | return ret; |
| 610 | } |
| 611 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 612 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 613 | * helper function to lookup reference count and flags of extent. |
| 614 | * |
| 615 | * the head node for delayed ref is used to store the sum of all the |
| 616 | * reference count modifications queued up in the rbtree. the head |
| 617 | * node may also store the extent flags to set. This way you can check |
| 618 | * to see what the reference count and extent flags would be if all of |
| 619 | * the delayed refs are not processed. |
| 620 | */ |
| 621 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 622 | struct btrfs_root *root, u64 bytenr, |
| 623 | u64 num_bytes, u64 *refs, u64 *flags) |
| 624 | { |
| 625 | struct btrfs_delayed_ref_head *head; |
| 626 | struct btrfs_delayed_ref_root *delayed_refs; |
| 627 | struct btrfs_path *path; |
| 628 | struct btrfs_extent_item *ei; |
| 629 | struct extent_buffer *leaf; |
| 630 | struct btrfs_key key; |
| 631 | u32 item_size; |
| 632 | u64 num_refs; |
| 633 | u64 extent_flags; |
| 634 | int ret; |
| 635 | |
| 636 | path = btrfs_alloc_path(); |
| 637 | if (!path) |
| 638 | return -ENOMEM; |
| 639 | |
| 640 | key.objectid = bytenr; |
| 641 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 642 | key.offset = num_bytes; |
| 643 | if (!trans) { |
| 644 | path->skip_locking = 1; |
| 645 | path->search_commit_root = 1; |
| 646 | } |
| 647 | again: |
| 648 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 649 | &key, path, 0, 0); |
| 650 | if (ret < 0) |
| 651 | goto out_free; |
| 652 | |
| 653 | if (ret == 0) { |
| 654 | leaf = path->nodes[0]; |
| 655 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 656 | if (item_size >= sizeof(*ei)) { |
| 657 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 658 | struct btrfs_extent_item); |
| 659 | num_refs = btrfs_extent_refs(leaf, ei); |
| 660 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 661 | } else { |
| 662 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 663 | struct btrfs_extent_item_v0 *ei0; |
| 664 | BUG_ON(item_size != sizeof(*ei0)); |
| 665 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 666 | struct btrfs_extent_item_v0); |
| 667 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 668 | /* FIXME: this isn't correct for data */ |
| 669 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 670 | #else |
| 671 | BUG(); |
| 672 | #endif |
| 673 | } |
| 674 | BUG_ON(num_refs == 0); |
| 675 | } else { |
| 676 | num_refs = 0; |
| 677 | extent_flags = 0; |
| 678 | ret = 0; |
| 679 | } |
| 680 | |
| 681 | if (!trans) |
| 682 | goto out; |
| 683 | |
| 684 | delayed_refs = &trans->transaction->delayed_refs; |
| 685 | spin_lock(&delayed_refs->lock); |
| 686 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 687 | if (head) { |
| 688 | if (!mutex_trylock(&head->mutex)) { |
| 689 | atomic_inc(&head->node.refs); |
| 690 | spin_unlock(&delayed_refs->lock); |
| 691 | |
| 692 | btrfs_release_path(root->fs_info->extent_root, path); |
| 693 | |
| 694 | mutex_lock(&head->mutex); |
| 695 | mutex_unlock(&head->mutex); |
| 696 | btrfs_put_delayed_ref(&head->node); |
| 697 | goto again; |
| 698 | } |
| 699 | if (head->extent_op && head->extent_op->update_flags) |
| 700 | extent_flags |= head->extent_op->flags_to_set; |
| 701 | else |
| 702 | BUG_ON(num_refs == 0); |
| 703 | |
| 704 | num_refs += head->node.ref_mod; |
| 705 | mutex_unlock(&head->mutex); |
| 706 | } |
| 707 | spin_unlock(&delayed_refs->lock); |
| 708 | out: |
| 709 | WARN_ON(num_refs == 0); |
| 710 | if (refs) |
| 711 | *refs = num_refs; |
| 712 | if (flags) |
| 713 | *flags = extent_flags; |
| 714 | out_free: |
| 715 | btrfs_free_path(path); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 720 | * Back reference rules. Back refs have three main goals: |
| 721 | * |
| 722 | * 1) differentiate between all holders of references to an extent so that |
| 723 | * when a reference is dropped we can make sure it was a valid reference |
| 724 | * before freeing the extent. |
| 725 | * |
| 726 | * 2) Provide enough information to quickly find the holders of an extent |
| 727 | * if we notice a given block is corrupted or bad. |
| 728 | * |
| 729 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 730 | * maintenance. This is actually the same as #2, but with a slightly |
| 731 | * different use case. |
| 732 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 733 | * There are two kinds of back refs. The implicit back refs is optimized |
| 734 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 735 | * back refs of this kind provide information about the block's owner tree |
| 736 | * and the pointer's key. These information allow us to find the block by |
| 737 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 738 | * referenced by their owner trees. The location of tree block is recorded |
| 739 | * in the back refs. Actually the full back refs is generic, and can be |
| 740 | * used in all cases the implicit back refs is used. The major shortcoming |
| 741 | * of the full back refs is its overhead. Every time a tree block gets |
| 742 | * COWed, we have to update back refs entry for all pointers in it. |
| 743 | * |
| 744 | * For a newly allocated tree block, we use implicit back refs for |
| 745 | * pointers in it. This means most tree related operations only involve |
| 746 | * implicit back refs. For a tree block created in old transaction, the |
| 747 | * only way to drop a reference to it is COW it. So we can detect the |
| 748 | * event that tree block loses its owner tree's reference and do the |
| 749 | * back refs conversion. |
| 750 | * |
| 751 | * When a tree block is COW'd through a tree, there are four cases: |
| 752 | * |
| 753 | * The reference count of the block is one and the tree is the block's |
| 754 | * owner tree. Nothing to do in this case. |
| 755 | * |
| 756 | * The reference count of the block is one and the tree is not the |
| 757 | * block's owner tree. In this case, full back refs is used for pointers |
| 758 | * in the block. Remove these full back refs, add implicit back refs for |
| 759 | * every pointers in the new block. |
| 760 | * |
| 761 | * The reference count of the block is greater than one and the tree is |
| 762 | * the block's owner tree. In this case, implicit back refs is used for |
| 763 | * pointers in the block. Add full back refs for every pointers in the |
| 764 | * block, increase lower level extents' reference counts. The original |
| 765 | * implicit back refs are entailed to the new block. |
| 766 | * |
| 767 | * The reference count of the block is greater than one and the tree is |
| 768 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 769 | * the new block, increase lower level extents' reference count. |
| 770 | * |
| 771 | * Back Reference Key composing: |
| 772 | * |
| 773 | * The key objectid corresponds to the first byte in the extent, |
| 774 | * The key type is used to differentiate between types of back refs. |
| 775 | * There are different meanings of the key offset for different types |
| 776 | * of back refs. |
| 777 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 778 | * File extents can be referenced by: |
| 779 | * |
| 780 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 781 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 782 | * - different offsets inside a file (bookend extents in file.c) |
| 783 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 784 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 785 | * |
| 786 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 787 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 788 | * - original offset in the file |
| 789 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 790 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 791 | * The key offset for the implicit back refs is hash of the first |
| 792 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 793 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 794 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 795 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 796 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 797 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 798 | * The key offset for the implicit back refs is the first byte of |
| 799 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 800 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 801 | * When a file extent is allocated, The implicit back refs is used. |
| 802 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 803 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 804 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 805 | * |
| 806 | * When a file extent is removed file truncation, we find the |
| 807 | * corresponding implicit back refs and check the following fields: |
| 808 | * |
| 809 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 810 | * |
| 811 | * Btree extents can be referenced by: |
| 812 | * |
| 813 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 814 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 815 | * Both the implicit back refs and the full back refs for tree blocks |
| 816 | * only consist of key. The key offset for the implicit back refs is |
| 817 | * objectid of block's owner tree. The key offset for the full back refs |
| 818 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 819 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 820 | * When implicit back refs is used, information about the lowest key and |
| 821 | * level of the tree block are required. These information are stored in |
| 822 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 823 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 824 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 825 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 826 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 827 | struct btrfs_root *root, |
| 828 | struct btrfs_path *path, |
| 829 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 830 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 831 | struct btrfs_extent_item *item; |
| 832 | struct btrfs_extent_item_v0 *ei0; |
| 833 | struct btrfs_extent_ref_v0 *ref0; |
| 834 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 835 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 836 | struct btrfs_key key; |
| 837 | struct btrfs_key found_key; |
| 838 | u32 new_size = sizeof(*item); |
| 839 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 840 | int ret; |
| 841 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 842 | leaf = path->nodes[0]; |
| 843 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 844 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 845 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 846 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 847 | struct btrfs_extent_item_v0); |
| 848 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 849 | |
| 850 | if (owner == (u64)-1) { |
| 851 | while (1) { |
| 852 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 853 | ret = btrfs_next_leaf(root, path); |
| 854 | if (ret < 0) |
| 855 | return ret; |
| 856 | BUG_ON(ret > 0); |
| 857 | leaf = path->nodes[0]; |
| 858 | } |
| 859 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 860 | path->slots[0]); |
| 861 | BUG_ON(key.objectid != found_key.objectid); |
| 862 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 863 | path->slots[0]++; |
| 864 | continue; |
| 865 | } |
| 866 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 867 | struct btrfs_extent_ref_v0); |
| 868 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | btrfs_release_path(root, path); |
| 873 | |
| 874 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 875 | new_size += sizeof(*bi); |
| 876 | |
| 877 | new_size -= sizeof(*ei0); |
| 878 | ret = btrfs_search_slot(trans, root, &key, path, |
| 879 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 880 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 881 | return ret; |
| 882 | BUG_ON(ret); |
| 883 | |
| 884 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 885 | BUG_ON(ret); |
| 886 | |
| 887 | leaf = path->nodes[0]; |
| 888 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 889 | btrfs_set_extent_refs(leaf, item, refs); |
| 890 | /* FIXME: get real generation */ |
| 891 | btrfs_set_extent_generation(leaf, item, 0); |
| 892 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 893 | btrfs_set_extent_flags(leaf, item, |
| 894 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 895 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 896 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 897 | /* FIXME: get first key of the block */ |
| 898 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 899 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 900 | } else { |
| 901 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 902 | } |
| 903 | btrfs_mark_buffer_dirty(leaf); |
| 904 | return 0; |
| 905 | } |
| 906 | #endif |
| 907 | |
| 908 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 909 | { |
| 910 | u32 high_crc = ~(u32)0; |
| 911 | u32 low_crc = ~(u32)0; |
| 912 | __le64 lenum; |
| 913 | |
| 914 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 915 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 916 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 917 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 919 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 920 | |
| 921 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 922 | } |
| 923 | |
| 924 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 925 | struct btrfs_extent_data_ref *ref) |
| 926 | { |
| 927 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 928 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 929 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 930 | } |
| 931 | |
| 932 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 933 | struct btrfs_extent_data_ref *ref, |
| 934 | u64 root_objectid, u64 owner, u64 offset) |
| 935 | { |
| 936 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 937 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 938 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 939 | return 0; |
| 940 | return 1; |
| 941 | } |
| 942 | |
| 943 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 944 | struct btrfs_root *root, |
| 945 | struct btrfs_path *path, |
| 946 | u64 bytenr, u64 parent, |
| 947 | u64 root_objectid, |
| 948 | u64 owner, u64 offset) |
| 949 | { |
| 950 | struct btrfs_key key; |
| 951 | struct btrfs_extent_data_ref *ref; |
| 952 | struct extent_buffer *leaf; |
| 953 | u32 nritems; |
| 954 | int ret; |
| 955 | int recow; |
| 956 | int err = -ENOENT; |
| 957 | |
| 958 | key.objectid = bytenr; |
| 959 | if (parent) { |
| 960 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 961 | key.offset = parent; |
| 962 | } else { |
| 963 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 964 | key.offset = hash_extent_data_ref(root_objectid, |
| 965 | owner, offset); |
| 966 | } |
| 967 | again: |
| 968 | recow = 0; |
| 969 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 970 | if (ret < 0) { |
| 971 | err = ret; |
| 972 | goto fail; |
| 973 | } |
| 974 | |
| 975 | if (parent) { |
| 976 | if (!ret) |
| 977 | return 0; |
| 978 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 979 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 980 | btrfs_release_path(root, path); |
| 981 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 982 | if (ret < 0) { |
| 983 | err = ret; |
| 984 | goto fail; |
| 985 | } |
| 986 | if (!ret) |
| 987 | return 0; |
| 988 | #endif |
| 989 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 993 | nritems = btrfs_header_nritems(leaf); |
| 994 | while (1) { |
| 995 | if (path->slots[0] >= nritems) { |
| 996 | ret = btrfs_next_leaf(root, path); |
| 997 | if (ret < 0) |
| 998 | err = ret; |
| 999 | if (ret) |
| 1000 | goto fail; |
| 1001 | |
| 1002 | leaf = path->nodes[0]; |
| 1003 | nritems = btrfs_header_nritems(leaf); |
| 1004 | recow = 1; |
| 1005 | } |
| 1006 | |
| 1007 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1008 | if (key.objectid != bytenr || |
| 1009 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1010 | goto fail; |
| 1011 | |
| 1012 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1013 | struct btrfs_extent_data_ref); |
| 1014 | |
| 1015 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1016 | owner, offset)) { |
| 1017 | if (recow) { |
| 1018 | btrfs_release_path(root, path); |
| 1019 | goto again; |
| 1020 | } |
| 1021 | err = 0; |
| 1022 | break; |
| 1023 | } |
| 1024 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1025 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1026 | fail: |
| 1027 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1030 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1031 | struct btrfs_root *root, |
| 1032 | struct btrfs_path *path, |
| 1033 | u64 bytenr, u64 parent, |
| 1034 | u64 root_objectid, u64 owner, |
| 1035 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1036 | { |
| 1037 | struct btrfs_key key; |
| 1038 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1039 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1040 | u32 num_refs; |
| 1041 | int ret; |
| 1042 | |
| 1043 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1044 | if (parent) { |
| 1045 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1046 | key.offset = parent; |
| 1047 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1048 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1049 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1050 | key.offset = hash_extent_data_ref(root_objectid, |
| 1051 | owner, offset); |
| 1052 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1053 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1054 | |
| 1055 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1056 | if (ret && ret != -EEXIST) |
| 1057 | goto fail; |
| 1058 | |
| 1059 | leaf = path->nodes[0]; |
| 1060 | if (parent) { |
| 1061 | struct btrfs_shared_data_ref *ref; |
| 1062 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1063 | struct btrfs_shared_data_ref); |
| 1064 | if (ret == 0) { |
| 1065 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1066 | } else { |
| 1067 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1068 | num_refs += refs_to_add; |
| 1069 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1070 | } |
| 1071 | } else { |
| 1072 | struct btrfs_extent_data_ref *ref; |
| 1073 | while (ret == -EEXIST) { |
| 1074 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1075 | struct btrfs_extent_data_ref); |
| 1076 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1077 | owner, offset)) |
| 1078 | break; |
| 1079 | btrfs_release_path(root, path); |
| 1080 | key.offset++; |
| 1081 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1082 | size); |
| 1083 | if (ret && ret != -EEXIST) |
| 1084 | goto fail; |
| 1085 | |
| 1086 | leaf = path->nodes[0]; |
| 1087 | } |
| 1088 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1089 | struct btrfs_extent_data_ref); |
| 1090 | if (ret == 0) { |
| 1091 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1092 | root_objectid); |
| 1093 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1094 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1095 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1096 | } else { |
| 1097 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1098 | num_refs += refs_to_add; |
| 1099 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1100 | } |
| 1101 | } |
| 1102 | btrfs_mark_buffer_dirty(leaf); |
| 1103 | ret = 0; |
| 1104 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1105 | btrfs_release_path(root, path); |
| 1106 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1107 | } |
| 1108 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1109 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1110 | struct btrfs_root *root, |
| 1111 | struct btrfs_path *path, |
| 1112 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1113 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1114 | struct btrfs_key key; |
| 1115 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1116 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1117 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1118 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1119 | int ret = 0; |
| 1120 | |
| 1121 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1122 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1123 | |
| 1124 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1125 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1126 | struct btrfs_extent_data_ref); |
| 1127 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1128 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1129 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1130 | struct btrfs_shared_data_ref); |
| 1131 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1132 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1133 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1134 | struct btrfs_extent_ref_v0 *ref0; |
| 1135 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1136 | struct btrfs_extent_ref_v0); |
| 1137 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1138 | #endif |
| 1139 | } else { |
| 1140 | BUG(); |
| 1141 | } |
| 1142 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1143 | BUG_ON(num_refs < refs_to_drop); |
| 1144 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1145 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1146 | if (num_refs == 0) { |
| 1147 | ret = btrfs_del_item(trans, root, path); |
| 1148 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1149 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1150 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1151 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1152 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1153 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1154 | else { |
| 1155 | struct btrfs_extent_ref_v0 *ref0; |
| 1156 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1157 | struct btrfs_extent_ref_v0); |
| 1158 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1159 | } |
| 1160 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1161 | btrfs_mark_buffer_dirty(leaf); |
| 1162 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1167 | struct btrfs_path *path, |
| 1168 | struct btrfs_extent_inline_ref *iref) |
| 1169 | { |
| 1170 | struct btrfs_key key; |
| 1171 | struct extent_buffer *leaf; |
| 1172 | struct btrfs_extent_data_ref *ref1; |
| 1173 | struct btrfs_shared_data_ref *ref2; |
| 1174 | u32 num_refs = 0; |
| 1175 | |
| 1176 | leaf = path->nodes[0]; |
| 1177 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1178 | if (iref) { |
| 1179 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1180 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1181 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1182 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1183 | } else { |
| 1184 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1185 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1186 | } |
| 1187 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1188 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1189 | struct btrfs_extent_data_ref); |
| 1190 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1191 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1192 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1193 | struct btrfs_shared_data_ref); |
| 1194 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1195 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1196 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1197 | struct btrfs_extent_ref_v0 *ref0; |
| 1198 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1199 | struct btrfs_extent_ref_v0); |
| 1200 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1201 | #endif |
| 1202 | } else { |
| 1203 | WARN_ON(1); |
| 1204 | } |
| 1205 | return num_refs; |
| 1206 | } |
| 1207 | |
| 1208 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1209 | struct btrfs_root *root, |
| 1210 | struct btrfs_path *path, |
| 1211 | u64 bytenr, u64 parent, |
| 1212 | u64 root_objectid) |
| 1213 | { |
| 1214 | struct btrfs_key key; |
| 1215 | int ret; |
| 1216 | |
| 1217 | key.objectid = bytenr; |
| 1218 | if (parent) { |
| 1219 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1220 | key.offset = parent; |
| 1221 | } else { |
| 1222 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1223 | key.offset = root_objectid; |
| 1224 | } |
| 1225 | |
| 1226 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1227 | if (ret > 0) |
| 1228 | ret = -ENOENT; |
| 1229 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1230 | if (ret == -ENOENT && parent) { |
| 1231 | btrfs_release_path(root, path); |
| 1232 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1233 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1234 | if (ret > 0) |
| 1235 | ret = -ENOENT; |
| 1236 | } |
| 1237 | #endif |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1242 | struct btrfs_root *root, |
| 1243 | struct btrfs_path *path, |
| 1244 | u64 bytenr, u64 parent, |
| 1245 | u64 root_objectid) |
| 1246 | { |
| 1247 | struct btrfs_key key; |
| 1248 | int ret; |
| 1249 | |
| 1250 | key.objectid = bytenr; |
| 1251 | if (parent) { |
| 1252 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1253 | key.offset = parent; |
| 1254 | } else { |
| 1255 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1256 | key.offset = root_objectid; |
| 1257 | } |
| 1258 | |
| 1259 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1260 | btrfs_release_path(root, path); |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1264 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1265 | { |
| 1266 | int type; |
| 1267 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1268 | if (parent > 0) |
| 1269 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1270 | else |
| 1271 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1272 | } else { |
| 1273 | if (parent > 0) |
| 1274 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1275 | else |
| 1276 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1277 | } |
| 1278 | return type; |
| 1279 | } |
| 1280 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1281 | static int find_next_key(struct btrfs_path *path, int level, |
| 1282 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1283 | |
| 1284 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1285 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1286 | if (!path->nodes[level]) |
| 1287 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1288 | if (path->slots[level] + 1 >= |
| 1289 | btrfs_header_nritems(path->nodes[level])) |
| 1290 | continue; |
| 1291 | if (level == 0) |
| 1292 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1293 | path->slots[level] + 1); |
| 1294 | else |
| 1295 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1296 | path->slots[level] + 1); |
| 1297 | return 0; |
| 1298 | } |
| 1299 | return 1; |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1304 | * to the address of inline back ref, and 0 is returned. |
| 1305 | * |
| 1306 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1307 | * should be inserted, and -ENOENT is returned. |
| 1308 | * |
| 1309 | * if insert is true and there are too many inline back refs, the path |
| 1310 | * points to the extent item, and -EAGAIN is returned. |
| 1311 | * |
| 1312 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1313 | * items in the tree are ordered. |
| 1314 | */ |
| 1315 | static noinline_for_stack |
| 1316 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1317 | struct btrfs_root *root, |
| 1318 | struct btrfs_path *path, |
| 1319 | struct btrfs_extent_inline_ref **ref_ret, |
| 1320 | u64 bytenr, u64 num_bytes, |
| 1321 | u64 parent, u64 root_objectid, |
| 1322 | u64 owner, u64 offset, int insert) |
| 1323 | { |
| 1324 | struct btrfs_key key; |
| 1325 | struct extent_buffer *leaf; |
| 1326 | struct btrfs_extent_item *ei; |
| 1327 | struct btrfs_extent_inline_ref *iref; |
| 1328 | u64 flags; |
| 1329 | u64 item_size; |
| 1330 | unsigned long ptr; |
| 1331 | unsigned long end; |
| 1332 | int extra_size; |
| 1333 | int type; |
| 1334 | int want; |
| 1335 | int ret; |
| 1336 | int err = 0; |
| 1337 | |
| 1338 | key.objectid = bytenr; |
| 1339 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1340 | key.offset = num_bytes; |
| 1341 | |
| 1342 | want = extent_ref_type(parent, owner); |
| 1343 | if (insert) { |
| 1344 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1345 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1346 | } else |
| 1347 | extra_size = -1; |
| 1348 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1349 | if (ret < 0) { |
| 1350 | err = ret; |
| 1351 | goto out; |
| 1352 | } |
| 1353 | BUG_ON(ret); |
| 1354 | |
| 1355 | leaf = path->nodes[0]; |
| 1356 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1357 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1358 | if (item_size < sizeof(*ei)) { |
| 1359 | if (!insert) { |
| 1360 | err = -ENOENT; |
| 1361 | goto out; |
| 1362 | } |
| 1363 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1364 | extra_size); |
| 1365 | if (ret < 0) { |
| 1366 | err = ret; |
| 1367 | goto out; |
| 1368 | } |
| 1369 | leaf = path->nodes[0]; |
| 1370 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1371 | } |
| 1372 | #endif |
| 1373 | BUG_ON(item_size < sizeof(*ei)); |
| 1374 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1375 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1376 | flags = btrfs_extent_flags(leaf, ei); |
| 1377 | |
| 1378 | ptr = (unsigned long)(ei + 1); |
| 1379 | end = (unsigned long)ei + item_size; |
| 1380 | |
| 1381 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1382 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1383 | BUG_ON(ptr > end); |
| 1384 | } else { |
| 1385 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1386 | } |
| 1387 | |
| 1388 | err = -ENOENT; |
| 1389 | while (1) { |
| 1390 | if (ptr >= end) { |
| 1391 | WARN_ON(ptr > end); |
| 1392 | break; |
| 1393 | } |
| 1394 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1395 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1396 | if (want < type) |
| 1397 | break; |
| 1398 | if (want > type) { |
| 1399 | ptr += btrfs_extent_inline_ref_size(type); |
| 1400 | continue; |
| 1401 | } |
| 1402 | |
| 1403 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1404 | struct btrfs_extent_data_ref *dref; |
| 1405 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1406 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1407 | owner, offset)) { |
| 1408 | err = 0; |
| 1409 | break; |
| 1410 | } |
| 1411 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1412 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1413 | break; |
| 1414 | } else { |
| 1415 | u64 ref_offset; |
| 1416 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1417 | if (parent > 0) { |
| 1418 | if (parent == ref_offset) { |
| 1419 | err = 0; |
| 1420 | break; |
| 1421 | } |
| 1422 | if (ref_offset < parent) |
| 1423 | break; |
| 1424 | } else { |
| 1425 | if (root_objectid == ref_offset) { |
| 1426 | err = 0; |
| 1427 | break; |
| 1428 | } |
| 1429 | if (ref_offset < root_objectid) |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | ptr += btrfs_extent_inline_ref_size(type); |
| 1434 | } |
| 1435 | if (err == -ENOENT && insert) { |
| 1436 | if (item_size + extra_size >= |
| 1437 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1438 | err = -EAGAIN; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | /* |
| 1442 | * To add new inline back ref, we have to make sure |
| 1443 | * there is no corresponding back ref item. |
| 1444 | * For simplicity, we just do not add new inline back |
| 1445 | * ref if there is any kind of item for this block |
| 1446 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1447 | if (find_next_key(path, 0, &key) == 0 && |
| 1448 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1449 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1450 | err = -EAGAIN; |
| 1451 | goto out; |
| 1452 | } |
| 1453 | } |
| 1454 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1455 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1456 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1457 | path->keep_locks = 0; |
| 1458 | btrfs_unlock_up_safe(path, 1); |
| 1459 | } |
| 1460 | return err; |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * helper to add new inline back ref |
| 1465 | */ |
| 1466 | static noinline_for_stack |
| 1467 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1468 | struct btrfs_root *root, |
| 1469 | struct btrfs_path *path, |
| 1470 | struct btrfs_extent_inline_ref *iref, |
| 1471 | u64 parent, u64 root_objectid, |
| 1472 | u64 owner, u64 offset, int refs_to_add, |
| 1473 | struct btrfs_delayed_extent_op *extent_op) |
| 1474 | { |
| 1475 | struct extent_buffer *leaf; |
| 1476 | struct btrfs_extent_item *ei; |
| 1477 | unsigned long ptr; |
| 1478 | unsigned long end; |
| 1479 | unsigned long item_offset; |
| 1480 | u64 refs; |
| 1481 | int size; |
| 1482 | int type; |
| 1483 | int ret; |
| 1484 | |
| 1485 | leaf = path->nodes[0]; |
| 1486 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1487 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1488 | |
| 1489 | type = extent_ref_type(parent, owner); |
| 1490 | size = btrfs_extent_inline_ref_size(type); |
| 1491 | |
| 1492 | ret = btrfs_extend_item(trans, root, path, size); |
| 1493 | BUG_ON(ret); |
| 1494 | |
| 1495 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1496 | refs = btrfs_extent_refs(leaf, ei); |
| 1497 | refs += refs_to_add; |
| 1498 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1499 | if (extent_op) |
| 1500 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1501 | |
| 1502 | ptr = (unsigned long)ei + item_offset; |
| 1503 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1504 | if (ptr < end - size) |
| 1505 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1506 | end - size - ptr); |
| 1507 | |
| 1508 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1509 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1510 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1511 | struct btrfs_extent_data_ref *dref; |
| 1512 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1513 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1514 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1515 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1516 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1517 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1518 | struct btrfs_shared_data_ref *sref; |
| 1519 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1520 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1521 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1522 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1523 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1524 | } else { |
| 1525 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1526 | } |
| 1527 | btrfs_mark_buffer_dirty(leaf); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1532 | struct btrfs_root *root, |
| 1533 | struct btrfs_path *path, |
| 1534 | struct btrfs_extent_inline_ref **ref_ret, |
| 1535 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1536 | u64 root_objectid, u64 owner, u64 offset) |
| 1537 | { |
| 1538 | int ret; |
| 1539 | |
| 1540 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1541 | bytenr, num_bytes, parent, |
| 1542 | root_objectid, owner, offset, 0); |
| 1543 | if (ret != -ENOENT) |
| 1544 | return ret; |
| 1545 | |
| 1546 | btrfs_release_path(root, path); |
| 1547 | *ref_ret = NULL; |
| 1548 | |
| 1549 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1550 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1551 | root_objectid); |
| 1552 | } else { |
| 1553 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1554 | root_objectid, owner, offset); |
| 1555 | } |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * helper to update/remove inline back ref |
| 1561 | */ |
| 1562 | static noinline_for_stack |
| 1563 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1564 | struct btrfs_root *root, |
| 1565 | struct btrfs_path *path, |
| 1566 | struct btrfs_extent_inline_ref *iref, |
| 1567 | int refs_to_mod, |
| 1568 | struct btrfs_delayed_extent_op *extent_op) |
| 1569 | { |
| 1570 | struct extent_buffer *leaf; |
| 1571 | struct btrfs_extent_item *ei; |
| 1572 | struct btrfs_extent_data_ref *dref = NULL; |
| 1573 | struct btrfs_shared_data_ref *sref = NULL; |
| 1574 | unsigned long ptr; |
| 1575 | unsigned long end; |
| 1576 | u32 item_size; |
| 1577 | int size; |
| 1578 | int type; |
| 1579 | int ret; |
| 1580 | u64 refs; |
| 1581 | |
| 1582 | leaf = path->nodes[0]; |
| 1583 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1584 | refs = btrfs_extent_refs(leaf, ei); |
| 1585 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1586 | refs += refs_to_mod; |
| 1587 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1588 | if (extent_op) |
| 1589 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1590 | |
| 1591 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1592 | |
| 1593 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1594 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1595 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1596 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1597 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1598 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1599 | } else { |
| 1600 | refs = 1; |
| 1601 | BUG_ON(refs_to_mod != -1); |
| 1602 | } |
| 1603 | |
| 1604 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1605 | refs += refs_to_mod; |
| 1606 | |
| 1607 | if (refs > 0) { |
| 1608 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1609 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1610 | else |
| 1611 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1612 | } else { |
| 1613 | size = btrfs_extent_inline_ref_size(type); |
| 1614 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1615 | ptr = (unsigned long)iref; |
| 1616 | end = (unsigned long)ei + item_size; |
| 1617 | if (ptr + size < end) |
| 1618 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1619 | end - ptr - size); |
| 1620 | item_size -= size; |
| 1621 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1622 | BUG_ON(ret); |
| 1623 | } |
| 1624 | btrfs_mark_buffer_dirty(leaf); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static noinline_for_stack |
| 1629 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1630 | struct btrfs_root *root, |
| 1631 | struct btrfs_path *path, |
| 1632 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1633 | u64 root_objectid, u64 owner, |
| 1634 | u64 offset, int refs_to_add, |
| 1635 | struct btrfs_delayed_extent_op *extent_op) |
| 1636 | { |
| 1637 | struct btrfs_extent_inline_ref *iref; |
| 1638 | int ret; |
| 1639 | |
| 1640 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1641 | bytenr, num_bytes, parent, |
| 1642 | root_objectid, owner, offset, 1); |
| 1643 | if (ret == 0) { |
| 1644 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1645 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1646 | refs_to_add, extent_op); |
| 1647 | } else if (ret == -ENOENT) { |
| 1648 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1649 | parent, root_objectid, |
| 1650 | owner, offset, refs_to_add, |
| 1651 | extent_op); |
| 1652 | } |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1657 | struct btrfs_root *root, |
| 1658 | struct btrfs_path *path, |
| 1659 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1660 | u64 owner, u64 offset, int refs_to_add) |
| 1661 | { |
| 1662 | int ret; |
| 1663 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1664 | BUG_ON(refs_to_add != 1); |
| 1665 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1666 | parent, root_objectid); |
| 1667 | } else { |
| 1668 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1669 | parent, root_objectid, |
| 1670 | owner, offset, refs_to_add); |
| 1671 | } |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1676 | struct btrfs_root *root, |
| 1677 | struct btrfs_path *path, |
| 1678 | struct btrfs_extent_inline_ref *iref, |
| 1679 | int refs_to_drop, int is_data) |
| 1680 | { |
| 1681 | int ret; |
| 1682 | |
| 1683 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1684 | if (iref) { |
| 1685 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1686 | -refs_to_drop, NULL); |
| 1687 | } else if (is_data) { |
| 1688 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1689 | } else { |
| 1690 | ret = btrfs_del_item(trans, root, path); |
| 1691 | } |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1695 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1696 | u64 start, u64 len) |
| 1697 | { |
Christoph Hellwig | 746cd1e | 2009-09-12 07:35:43 +0200 | [diff] [blame] | 1698 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 1699 | BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1700 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1701 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1702 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1703 | u64 num_bytes) |
| 1704 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1705 | int ret; |
| 1706 | u64 map_length = num_bytes; |
| 1707 | struct btrfs_multi_bio *multi = NULL; |
| 1708 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1709 | if (!btrfs_test_opt(root, DISCARD)) |
| 1710 | return 0; |
| 1711 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1712 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1713 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1714 | bytenr, &map_length, &multi, 0); |
| 1715 | if (!ret) { |
| 1716 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1717 | int i; |
| 1718 | |
| 1719 | if (map_length > num_bytes) |
| 1720 | map_length = num_bytes; |
| 1721 | |
| 1722 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1723 | btrfs_issue_discard(stripe->dev->bdev, |
| 1724 | stripe->physical, |
| 1725 | map_length); |
| 1726 | } |
| 1727 | kfree(multi); |
| 1728 | } |
| 1729 | |
| 1730 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1731 | } |
| 1732 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1733 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1734 | struct btrfs_root *root, |
| 1735 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1736 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1737 | { |
| 1738 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1739 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1740 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1741 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1742 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1743 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1744 | parent, root_objectid, (int)owner, |
| 1745 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1746 | } else { |
| 1747 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1748 | parent, root_objectid, owner, offset, |
| 1749 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1750 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1751 | return ret; |
| 1752 | } |
| 1753 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1754 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1755 | struct btrfs_root *root, |
| 1756 | u64 bytenr, u64 num_bytes, |
| 1757 | u64 parent, u64 root_objectid, |
| 1758 | u64 owner, u64 offset, int refs_to_add, |
| 1759 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1760 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1761 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1762 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1763 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1764 | u64 refs; |
| 1765 | int ret; |
| 1766 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1767 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1768 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1769 | if (!path) |
| 1770 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1771 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1772 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1773 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1774 | /* this will setup the path even if it fails to insert the back ref */ |
| 1775 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1776 | path, bytenr, num_bytes, parent, |
| 1777 | root_objectid, owner, offset, |
| 1778 | refs_to_add, extent_op); |
| 1779 | if (ret == 0) |
| 1780 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1781 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1782 | if (ret != -EAGAIN) { |
| 1783 | err = ret; |
| 1784 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1785 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1786 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1787 | leaf = path->nodes[0]; |
| 1788 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1789 | refs = btrfs_extent_refs(leaf, item); |
| 1790 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1791 | if (extent_op) |
| 1792 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1793 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1794 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1795 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1796 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1797 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1798 | path->leave_spinning = 1; |
| 1799 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1800 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1801 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1802 | path, bytenr, parent, root_objectid, |
| 1803 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1804 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1805 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1806 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1807 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1808 | } |
| 1809 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1810 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1811 | struct btrfs_root *root, |
| 1812 | struct btrfs_delayed_ref_node *node, |
| 1813 | struct btrfs_delayed_extent_op *extent_op, |
| 1814 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1815 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1816 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1817 | struct btrfs_delayed_data_ref *ref; |
| 1818 | struct btrfs_key ins; |
| 1819 | u64 parent = 0; |
| 1820 | u64 ref_root = 0; |
| 1821 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1822 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1823 | ins.objectid = node->bytenr; |
| 1824 | ins.offset = node->num_bytes; |
| 1825 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1826 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1827 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1828 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1829 | parent = ref->parent; |
| 1830 | else |
| 1831 | ref_root = ref->root; |
| 1832 | |
| 1833 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1834 | if (extent_op) { |
| 1835 | BUG_ON(extent_op->update_key); |
| 1836 | flags |= extent_op->flags_to_set; |
| 1837 | } |
| 1838 | ret = alloc_reserved_file_extent(trans, root, |
| 1839 | parent, ref_root, flags, |
| 1840 | ref->objectid, ref->offset, |
| 1841 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1842 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1843 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1844 | node->num_bytes, parent, |
| 1845 | ref_root, ref->objectid, |
| 1846 | ref->offset, node->ref_mod, |
| 1847 | extent_op); |
| 1848 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1849 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1850 | node->num_bytes, parent, |
| 1851 | ref_root, ref->objectid, |
| 1852 | ref->offset, node->ref_mod, |
| 1853 | extent_op); |
| 1854 | } else { |
| 1855 | BUG(); |
| 1856 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1857 | return ret; |
| 1858 | } |
| 1859 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1860 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1861 | struct extent_buffer *leaf, |
| 1862 | struct btrfs_extent_item *ei) |
| 1863 | { |
| 1864 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1865 | if (extent_op->update_flags) { |
| 1866 | flags |= extent_op->flags_to_set; |
| 1867 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1868 | } |
| 1869 | |
| 1870 | if (extent_op->update_key) { |
| 1871 | struct btrfs_tree_block_info *bi; |
| 1872 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1873 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1874 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1879 | struct btrfs_root *root, |
| 1880 | struct btrfs_delayed_ref_node *node, |
| 1881 | struct btrfs_delayed_extent_op *extent_op) |
| 1882 | { |
| 1883 | struct btrfs_key key; |
| 1884 | struct btrfs_path *path; |
| 1885 | struct btrfs_extent_item *ei; |
| 1886 | struct extent_buffer *leaf; |
| 1887 | u32 item_size; |
| 1888 | int ret; |
| 1889 | int err = 0; |
| 1890 | |
| 1891 | path = btrfs_alloc_path(); |
| 1892 | if (!path) |
| 1893 | return -ENOMEM; |
| 1894 | |
| 1895 | key.objectid = node->bytenr; |
| 1896 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1897 | key.offset = node->num_bytes; |
| 1898 | |
| 1899 | path->reada = 1; |
| 1900 | path->leave_spinning = 1; |
| 1901 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1902 | path, 0, 1); |
| 1903 | if (ret < 0) { |
| 1904 | err = ret; |
| 1905 | goto out; |
| 1906 | } |
| 1907 | if (ret > 0) { |
| 1908 | err = -EIO; |
| 1909 | goto out; |
| 1910 | } |
| 1911 | |
| 1912 | leaf = path->nodes[0]; |
| 1913 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1914 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1915 | if (item_size < sizeof(*ei)) { |
| 1916 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1917 | path, (u64)-1, 0); |
| 1918 | if (ret < 0) { |
| 1919 | err = ret; |
| 1920 | goto out; |
| 1921 | } |
| 1922 | leaf = path->nodes[0]; |
| 1923 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1924 | } |
| 1925 | #endif |
| 1926 | BUG_ON(item_size < sizeof(*ei)); |
| 1927 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1928 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1929 | |
| 1930 | btrfs_mark_buffer_dirty(leaf); |
| 1931 | out: |
| 1932 | btrfs_free_path(path); |
| 1933 | return err; |
| 1934 | } |
| 1935 | |
| 1936 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1937 | struct btrfs_root *root, |
| 1938 | struct btrfs_delayed_ref_node *node, |
| 1939 | struct btrfs_delayed_extent_op *extent_op, |
| 1940 | int insert_reserved) |
| 1941 | { |
| 1942 | int ret = 0; |
| 1943 | struct btrfs_delayed_tree_ref *ref; |
| 1944 | struct btrfs_key ins; |
| 1945 | u64 parent = 0; |
| 1946 | u64 ref_root = 0; |
| 1947 | |
| 1948 | ins.objectid = node->bytenr; |
| 1949 | ins.offset = node->num_bytes; |
| 1950 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1951 | |
| 1952 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1953 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1954 | parent = ref->parent; |
| 1955 | else |
| 1956 | ref_root = ref->root; |
| 1957 | |
| 1958 | BUG_ON(node->ref_mod != 1); |
| 1959 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1960 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1961 | !extent_op->update_key); |
| 1962 | ret = alloc_reserved_tree_block(trans, root, |
| 1963 | parent, ref_root, |
| 1964 | extent_op->flags_to_set, |
| 1965 | &extent_op->key, |
| 1966 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1967 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1968 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1969 | node->num_bytes, parent, ref_root, |
| 1970 | ref->level, 0, 1, extent_op); |
| 1971 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1972 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1973 | node->num_bytes, parent, ref_root, |
| 1974 | ref->level, 0, 1, extent_op); |
| 1975 | } else { |
| 1976 | BUG(); |
| 1977 | } |
| 1978 | return ret; |
| 1979 | } |
| 1980 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1981 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1982 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1983 | struct btrfs_root *root, |
| 1984 | struct btrfs_delayed_ref_node *node, |
| 1985 | struct btrfs_delayed_extent_op *extent_op, |
| 1986 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1987 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1988 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1989 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1990 | struct btrfs_delayed_ref_head *head; |
| 1991 | /* |
| 1992 | * we've hit the end of the chain and we were supposed |
| 1993 | * to insert this extent into the tree. But, it got |
| 1994 | * deleted before we ever needed to insert it, so all |
| 1995 | * we have to do is clean up the accounting |
| 1996 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1997 | BUG_ON(extent_op); |
| 1998 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1999 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2000 | btrfs_pin_extent(root, node->bytenr, |
| 2001 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2002 | if (head->is_data) { |
| 2003 | ret = btrfs_del_csums(trans, root, |
| 2004 | node->bytenr, |
| 2005 | node->num_bytes); |
| 2006 | BUG_ON(ret); |
| 2007 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2008 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2009 | mutex_unlock(&head->mutex); |
| 2010 | return 0; |
| 2011 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2012 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2013 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2014 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2015 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2016 | insert_reserved); |
| 2017 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2018 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2019 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2020 | insert_reserved); |
| 2021 | else |
| 2022 | BUG(); |
| 2023 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2024 | } |
| 2025 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2026 | static noinline struct btrfs_delayed_ref_node * |
| 2027 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2028 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2029 | struct rb_node *node; |
| 2030 | struct btrfs_delayed_ref_node *ref; |
| 2031 | int action = BTRFS_ADD_DELAYED_REF; |
| 2032 | again: |
| 2033 | /* |
| 2034 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2035 | * this prevents ref count from going down to zero when |
| 2036 | * there still are pending delayed ref. |
| 2037 | */ |
| 2038 | node = rb_prev(&head->node.rb_node); |
| 2039 | while (1) { |
| 2040 | if (!node) |
| 2041 | break; |
| 2042 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2043 | rb_node); |
| 2044 | if (ref->bytenr != head->node.bytenr) |
| 2045 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2046 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2047 | return ref; |
| 2048 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2049 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2050 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2051 | action = BTRFS_DROP_DELAYED_REF; |
| 2052 | goto again; |
| 2053 | } |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2057 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2058 | struct btrfs_root *root, |
| 2059 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2060 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2061 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2062 | struct btrfs_delayed_ref_node *ref; |
| 2063 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2064 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2065 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2066 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2067 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2068 | |
| 2069 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2070 | while (1) { |
| 2071 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2072 | /* pick a new head ref from the cluster list */ |
| 2073 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2074 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2075 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2076 | locked_ref = list_entry(cluster->next, |
| 2077 | struct btrfs_delayed_ref_head, cluster); |
| 2078 | |
| 2079 | /* grab the lock that says we are going to process |
| 2080 | * all the refs for this head */ |
| 2081 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2082 | |
| 2083 | /* |
| 2084 | * we may have dropped the spin lock to get the head |
| 2085 | * mutex lock, and that might have given someone else |
| 2086 | * time to free the head. If that's true, it has been |
| 2087 | * removed from our list and we can move on. |
| 2088 | */ |
| 2089 | if (ret == -EAGAIN) { |
| 2090 | locked_ref = NULL; |
| 2091 | count++; |
| 2092 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * record the must insert reserved flag before we |
| 2098 | * drop the spin lock. |
| 2099 | */ |
| 2100 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2101 | locked_ref->must_insert_reserved = 0; |
| 2102 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2103 | extent_op = locked_ref->extent_op; |
| 2104 | locked_ref->extent_op = NULL; |
| 2105 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2106 | /* |
| 2107 | * locked_ref is the head node, so we have to go one |
| 2108 | * node back for any delayed ref updates |
| 2109 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2110 | ref = select_delayed_ref(locked_ref); |
| 2111 | if (!ref) { |
| 2112 | /* All delayed refs have been processed, Go ahead |
| 2113 | * and send the head node to run_one_delayed_ref, |
| 2114 | * so that any accounting fixes can happen |
| 2115 | */ |
| 2116 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2117 | |
| 2118 | if (extent_op && must_insert_reserved) { |
| 2119 | kfree(extent_op); |
| 2120 | extent_op = NULL; |
| 2121 | } |
| 2122 | |
| 2123 | if (extent_op) { |
| 2124 | spin_unlock(&delayed_refs->lock); |
| 2125 | |
| 2126 | ret = run_delayed_extent_op(trans, root, |
| 2127 | ref, extent_op); |
| 2128 | BUG_ON(ret); |
| 2129 | kfree(extent_op); |
| 2130 | |
| 2131 | cond_resched(); |
| 2132 | spin_lock(&delayed_refs->lock); |
| 2133 | continue; |
| 2134 | } |
| 2135 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2136 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2137 | locked_ref = NULL; |
| 2138 | } |
| 2139 | |
| 2140 | ref->in_tree = 0; |
| 2141 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2142 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2143 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2144 | spin_unlock(&delayed_refs->lock); |
| 2145 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2146 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2147 | must_insert_reserved); |
| 2148 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2149 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2150 | btrfs_put_delayed_ref(ref); |
| 2151 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2152 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2153 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2154 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2155 | spin_lock(&delayed_refs->lock); |
| 2156 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2157 | return count; |
| 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * this starts processing the delayed reference count updates and |
| 2162 | * extent insertions we have queued up so far. count can be |
| 2163 | * 0, which means to process everything in the tree at the start |
| 2164 | * of the run (but not newly added entries), or it can be some target |
| 2165 | * number you'd like to process. |
| 2166 | */ |
| 2167 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2168 | struct btrfs_root *root, unsigned long count) |
| 2169 | { |
| 2170 | struct rb_node *node; |
| 2171 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2172 | struct btrfs_delayed_ref_node *ref; |
| 2173 | struct list_head cluster; |
| 2174 | int ret; |
| 2175 | int run_all = count == (unsigned long)-1; |
| 2176 | int run_most = 0; |
| 2177 | |
| 2178 | if (root == root->fs_info->extent_root) |
| 2179 | root = root->fs_info->tree_root; |
| 2180 | |
| 2181 | delayed_refs = &trans->transaction->delayed_refs; |
| 2182 | INIT_LIST_HEAD(&cluster); |
| 2183 | again: |
| 2184 | spin_lock(&delayed_refs->lock); |
| 2185 | if (count == 0) { |
| 2186 | count = delayed_refs->num_entries * 2; |
| 2187 | run_most = 1; |
| 2188 | } |
| 2189 | while (1) { |
| 2190 | if (!(run_all || run_most) && |
| 2191 | delayed_refs->num_heads_ready < 64) |
| 2192 | break; |
| 2193 | |
| 2194 | /* |
| 2195 | * go find something we can process in the rbtree. We start at |
| 2196 | * the beginning of the tree, and then build a cluster |
| 2197 | * of refs to process starting at the first one we are able to |
| 2198 | * lock |
| 2199 | */ |
| 2200 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2201 | delayed_refs->run_delayed_start); |
| 2202 | if (ret) |
| 2203 | break; |
| 2204 | |
| 2205 | ret = run_clustered_refs(trans, root, &cluster); |
| 2206 | BUG_ON(ret < 0); |
| 2207 | |
| 2208 | count -= min_t(unsigned long, ret, count); |
| 2209 | |
| 2210 | if (count == 0) |
| 2211 | break; |
| 2212 | } |
| 2213 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2214 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2215 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2216 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2217 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2218 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2219 | |
| 2220 | while (node) { |
| 2221 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2222 | rb_node); |
| 2223 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2224 | struct btrfs_delayed_ref_head *head; |
| 2225 | |
| 2226 | head = btrfs_delayed_node_to_head(ref); |
| 2227 | atomic_inc(&ref->refs); |
| 2228 | |
| 2229 | spin_unlock(&delayed_refs->lock); |
| 2230 | mutex_lock(&head->mutex); |
| 2231 | mutex_unlock(&head->mutex); |
| 2232 | |
| 2233 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2234 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2235 | goto again; |
| 2236 | } |
| 2237 | node = rb_next(node); |
| 2238 | } |
| 2239 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2240 | schedule_timeout(1); |
| 2241 | goto again; |
| 2242 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2243 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2244 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
| 2247 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2248 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2249 | struct btrfs_root *root, |
| 2250 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2251 | int is_data) |
| 2252 | { |
| 2253 | struct btrfs_delayed_extent_op *extent_op; |
| 2254 | int ret; |
| 2255 | |
| 2256 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2257 | if (!extent_op) |
| 2258 | return -ENOMEM; |
| 2259 | |
| 2260 | extent_op->flags_to_set = flags; |
| 2261 | extent_op->update_flags = 1; |
| 2262 | extent_op->update_key = 0; |
| 2263 | extent_op->is_data = is_data ? 1 : 0; |
| 2264 | |
| 2265 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2266 | if (ret) |
| 2267 | kfree(extent_op); |
| 2268 | return ret; |
| 2269 | } |
| 2270 | |
| 2271 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2272 | struct btrfs_root *root, |
| 2273 | struct btrfs_path *path, |
| 2274 | u64 objectid, u64 offset, u64 bytenr) |
| 2275 | { |
| 2276 | struct btrfs_delayed_ref_head *head; |
| 2277 | struct btrfs_delayed_ref_node *ref; |
| 2278 | struct btrfs_delayed_data_ref *data_ref; |
| 2279 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2280 | struct rb_node *node; |
| 2281 | int ret = 0; |
| 2282 | |
| 2283 | ret = -ENOENT; |
| 2284 | delayed_refs = &trans->transaction->delayed_refs; |
| 2285 | spin_lock(&delayed_refs->lock); |
| 2286 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2287 | if (!head) |
| 2288 | goto out; |
| 2289 | |
| 2290 | if (!mutex_trylock(&head->mutex)) { |
| 2291 | atomic_inc(&head->node.refs); |
| 2292 | spin_unlock(&delayed_refs->lock); |
| 2293 | |
| 2294 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2295 | |
| 2296 | mutex_lock(&head->mutex); |
| 2297 | mutex_unlock(&head->mutex); |
| 2298 | btrfs_put_delayed_ref(&head->node); |
| 2299 | return -EAGAIN; |
| 2300 | } |
| 2301 | |
| 2302 | node = rb_prev(&head->node.rb_node); |
| 2303 | if (!node) |
| 2304 | goto out_unlock; |
| 2305 | |
| 2306 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2307 | |
| 2308 | if (ref->bytenr != bytenr) |
| 2309 | goto out_unlock; |
| 2310 | |
| 2311 | ret = 1; |
| 2312 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2313 | goto out_unlock; |
| 2314 | |
| 2315 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2316 | |
| 2317 | node = rb_prev(node); |
| 2318 | if (node) { |
| 2319 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2320 | if (ref->bytenr == bytenr) |
| 2321 | goto out_unlock; |
| 2322 | } |
| 2323 | |
| 2324 | if (data_ref->root != root->root_key.objectid || |
| 2325 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2326 | goto out_unlock; |
| 2327 | |
| 2328 | ret = 0; |
| 2329 | out_unlock: |
| 2330 | mutex_unlock(&head->mutex); |
| 2331 | out: |
| 2332 | spin_unlock(&delayed_refs->lock); |
| 2333 | return ret; |
| 2334 | } |
| 2335 | |
| 2336 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2337 | struct btrfs_root *root, |
| 2338 | struct btrfs_path *path, |
| 2339 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2340 | { |
| 2341 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2342 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2343 | struct btrfs_extent_data_ref *ref; |
| 2344 | struct btrfs_extent_inline_ref *iref; |
| 2345 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2346 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2347 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2348 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2349 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2350 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2351 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2352 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2353 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2354 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2355 | if (ret < 0) |
| 2356 | goto out; |
| 2357 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2358 | |
| 2359 | ret = -ENOENT; |
| 2360 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2361 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2362 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2363 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2364 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2365 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2366 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2367 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2368 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2369 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2370 | ret = 1; |
| 2371 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2372 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2373 | if (item_size < sizeof(*ei)) { |
| 2374 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2375 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2376 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2377 | #endif |
| 2378 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2379 | |
| 2380 | if (item_size != sizeof(*ei) + |
| 2381 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2382 | goto out; |
| 2383 | |
| 2384 | if (btrfs_extent_generation(leaf, ei) <= |
| 2385 | btrfs_root_last_snapshot(&root->root_item)) |
| 2386 | goto out; |
| 2387 | |
| 2388 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2389 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2390 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2391 | goto out; |
| 2392 | |
| 2393 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2394 | if (btrfs_extent_refs(leaf, ei) != |
| 2395 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2396 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2397 | root->root_key.objectid || |
| 2398 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2399 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2400 | goto out; |
| 2401 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2402 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2403 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2404 | return ret; |
| 2405 | } |
| 2406 | |
| 2407 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2408 | struct btrfs_root *root, |
| 2409 | u64 objectid, u64 offset, u64 bytenr) |
| 2410 | { |
| 2411 | struct btrfs_path *path; |
| 2412 | int ret; |
| 2413 | int ret2; |
| 2414 | |
| 2415 | path = btrfs_alloc_path(); |
| 2416 | if (!path) |
| 2417 | return -ENOENT; |
| 2418 | |
| 2419 | do { |
| 2420 | ret = check_committed_ref(trans, root, path, objectid, |
| 2421 | offset, bytenr); |
| 2422 | if (ret && ret != -ENOENT) |
| 2423 | goto out; |
| 2424 | |
| 2425 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2426 | offset, bytenr); |
| 2427 | } while (ret2 == -EAGAIN); |
| 2428 | |
| 2429 | if (ret2 && ret2 != -ENOENT) { |
| 2430 | ret = ret2; |
| 2431 | goto out; |
| 2432 | } |
| 2433 | |
| 2434 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2435 | ret = 0; |
| 2436 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2437 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2438 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2439 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2440 | return ret; |
| 2441 | } |
| 2442 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2443 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2444 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2445 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2446 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2447 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2448 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2449 | u64 root_gen; |
| 2450 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2451 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2452 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2453 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2454 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2455 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2456 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2457 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2458 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2459 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2460 | shared = 0; |
| 2461 | root_gen = root->root_key.offset; |
| 2462 | } else { |
| 2463 | shared = 1; |
| 2464 | root_gen = trans->transid - 1; |
| 2465 | } |
| 2466 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2467 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2468 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2469 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2470 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2471 | struct btrfs_leaf_ref *ref; |
| 2472 | struct btrfs_extent_info *info; |
| 2473 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2474 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2475 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2476 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2477 | goto out; |
| 2478 | } |
| 2479 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2480 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2481 | ref->bytenr = buf->start; |
| 2482 | ref->owner = btrfs_header_owner(buf); |
| 2483 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2484 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2485 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2486 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2487 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2488 | u64 disk_bytenr; |
| 2489 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2490 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2491 | continue; |
| 2492 | fi = btrfs_item_ptr(buf, i, |
| 2493 | struct btrfs_file_extent_item); |
| 2494 | if (btrfs_file_extent_type(buf, fi) == |
| 2495 | BTRFS_FILE_EXTENT_INLINE) |
| 2496 | continue; |
| 2497 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2498 | if (disk_bytenr == 0) |
| 2499 | continue; |
| 2500 | |
| 2501 | info->bytenr = disk_bytenr; |
| 2502 | info->num_bytes = |
| 2503 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2504 | info->objectid = key.objectid; |
| 2505 | info->offset = key.offset; |
| 2506 | info++; |
| 2507 | } |
| 2508 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2509 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2510 | if (ret == -EEXIST && shared) { |
| 2511 | struct btrfs_leaf_ref *old; |
| 2512 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2513 | BUG_ON(!old); |
| 2514 | btrfs_remove_leaf_ref(root, old); |
| 2515 | btrfs_free_leaf_ref(root, old); |
| 2516 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2517 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2518 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2519 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2520 | } |
| 2521 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2522 | return ret; |
| 2523 | } |
| 2524 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2525 | /* when a block goes through cow, we update the reference counts of |
| 2526 | * everything that block points to. The internal pointers of the block |
| 2527 | * can be in just about any order, and it is likely to have clusters of |
| 2528 | * things that are close together and clusters of things that are not. |
| 2529 | * |
| 2530 | * To help reduce the seeks that come with updating all of these reference |
| 2531 | * counts, sort them by byte number before actual updates are done. |
| 2532 | * |
| 2533 | * struct refsort is used to match byte number to slot in the btree block. |
| 2534 | * we sort based on the byte number and then use the slot to actually |
| 2535 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2536 | * |
| 2537 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2538 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2539 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2540 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2541 | */ |
| 2542 | struct refsort { |
| 2543 | u64 bytenr; |
| 2544 | u32 slot; |
| 2545 | }; |
| 2546 | |
| 2547 | /* |
| 2548 | * for passing into sort() |
| 2549 | */ |
| 2550 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2551 | { |
| 2552 | const struct refsort *a = a_void; |
| 2553 | const struct refsort *b = b_void; |
| 2554 | |
| 2555 | if (a->bytenr < b->bytenr) |
| 2556 | return -1; |
| 2557 | if (a->bytenr > b->bytenr) |
| 2558 | return 1; |
| 2559 | return 0; |
| 2560 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2561 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2562 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2563 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2564 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2565 | struct extent_buffer *buf, |
| 2566 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2567 | { |
| 2568 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2569 | u64 num_bytes; |
| 2570 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2571 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2572 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2573 | struct btrfs_key key; |
| 2574 | struct btrfs_file_extent_item *fi; |
| 2575 | int i; |
| 2576 | int level; |
| 2577 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2578 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2579 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2580 | |
| 2581 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2582 | nritems = btrfs_header_nritems(buf); |
| 2583 | level = btrfs_header_level(buf); |
| 2584 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2585 | if (!root->ref_cows && level == 0) |
| 2586 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2587 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2588 | if (inc) |
| 2589 | process_func = btrfs_inc_extent_ref; |
| 2590 | else |
| 2591 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2592 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2593 | if (full_backref) |
| 2594 | parent = buf->start; |
| 2595 | else |
| 2596 | parent = 0; |
| 2597 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2598 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2599 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2600 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2601 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2602 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2603 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2604 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2605 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2606 | BTRFS_FILE_EXTENT_INLINE) |
| 2607 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2608 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2609 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2610 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2611 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2612 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2613 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2614 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2615 | parent, ref_root, key.objectid, |
| 2616 | key.offset); |
| 2617 | if (ret) |
| 2618 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2619 | } else { |
| 2620 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2621 | num_bytes = btrfs_level_size(root, level - 1); |
| 2622 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2623 | parent, ref_root, level - 1, 0); |
| 2624 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2625 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2626 | } |
| 2627 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2628 | return 0; |
| 2629 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2630 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2631 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2632 | } |
| 2633 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2634 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2635 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2636 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2637 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2638 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2639 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2640 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2641 | struct extent_buffer *buf, int full_backref) |
| 2642 | { |
| 2643 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2644 | } |
| 2645 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2646 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2647 | struct btrfs_root *root, |
| 2648 | struct btrfs_path *path, |
| 2649 | struct btrfs_block_group_cache *cache) |
| 2650 | { |
| 2651 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2652 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2653 | unsigned long bi; |
| 2654 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2655 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2656 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2657 | if (ret < 0) |
| 2658 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2659 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2660 | |
| 2661 | leaf = path->nodes[0]; |
| 2662 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2663 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2664 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2665 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2666 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2667 | if (ret) |
| 2668 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2669 | return 0; |
| 2670 | |
| 2671 | } |
| 2672 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2673 | static struct btrfs_block_group_cache * |
| 2674 | next_block_group(struct btrfs_root *root, |
| 2675 | struct btrfs_block_group_cache *cache) |
| 2676 | { |
| 2677 | struct rb_node *node; |
| 2678 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2679 | node = rb_next(&cache->cache_node); |
| 2680 | btrfs_put_block_group(cache); |
| 2681 | if (node) { |
| 2682 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2683 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2684 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2685 | } else |
| 2686 | cache = NULL; |
| 2687 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2688 | return cache; |
| 2689 | } |
| 2690 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2691 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2692 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2693 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2694 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2695 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2696 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2697 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2698 | |
| 2699 | path = btrfs_alloc_path(); |
| 2700 | if (!path) |
| 2701 | return -ENOMEM; |
| 2702 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2703 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2704 | if (last == 0) { |
| 2705 | err = btrfs_run_delayed_refs(trans, root, |
| 2706 | (unsigned long)-1); |
| 2707 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2708 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2709 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2710 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2711 | while (cache) { |
| 2712 | if (cache->dirty) |
| 2713 | break; |
| 2714 | cache = next_block_group(root, cache); |
| 2715 | } |
| 2716 | if (!cache) { |
| 2717 | if (last == 0) |
| 2718 | break; |
| 2719 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2720 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2721 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2722 | |
| 2723 | cache->dirty = 0; |
| 2724 | last = cache->key.objectid + cache->key.offset; |
| 2725 | |
| 2726 | err = write_one_cache_group(trans, root, path, cache); |
| 2727 | BUG_ON(err); |
| 2728 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2729 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2730 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2731 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2732 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2733 | } |
| 2734 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2735 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2736 | { |
| 2737 | struct btrfs_block_group_cache *block_group; |
| 2738 | int readonly = 0; |
| 2739 | |
| 2740 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2741 | if (!block_group || block_group->ro) |
| 2742 | readonly = 1; |
| 2743 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2744 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2745 | return readonly; |
| 2746 | } |
| 2747 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2748 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2749 | u64 total_bytes, u64 bytes_used, |
| 2750 | struct btrfs_space_info **space_info) |
| 2751 | { |
| 2752 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2753 | int i; |
| 2754 | int factor; |
| 2755 | |
| 2756 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2757 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2758 | factor = 2; |
| 2759 | else |
| 2760 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2761 | |
| 2762 | found = __find_space_info(info, flags); |
| 2763 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2764 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2765 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2766 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2767 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2768 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2769 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2770 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2771 | *space_info = found; |
| 2772 | return 0; |
| 2773 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2774 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2775 | if (!found) |
| 2776 | return -ENOMEM; |
| 2777 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2778 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 2779 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2780 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2781 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2782 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 2783 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 2784 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2785 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2786 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2787 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2788 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2789 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2790 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2791 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2792 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2793 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2794 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2795 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2796 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2797 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2798 | return 0; |
| 2799 | } |
| 2800 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2801 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2802 | { |
| 2803 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2804 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2805 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2806 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2807 | if (extra_flags) { |
| 2808 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2809 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2810 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2811 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2812 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2813 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2814 | } |
| 2815 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2816 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2817 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2818 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2819 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2820 | |
| 2821 | if (num_devices == 1) |
| 2822 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2823 | if (num_devices < 4) |
| 2824 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2825 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2826 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2827 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2828 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2829 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2830 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2831 | |
| 2832 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2833 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2834 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2835 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2836 | |
| 2837 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2838 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2839 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2840 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2841 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2842 | return flags; |
| 2843 | } |
| 2844 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2845 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2846 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2847 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2848 | flags |= root->fs_info->avail_data_alloc_bits & |
| 2849 | root->fs_info->data_alloc_profile; |
| 2850 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2851 | flags |= root->fs_info->avail_system_alloc_bits & |
| 2852 | root->fs_info->system_alloc_profile; |
| 2853 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2854 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 2855 | root->fs_info->metadata_alloc_profile; |
| 2856 | return btrfs_reduce_alloc_profile(root, flags); |
| 2857 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2858 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2859 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
| 2860 | { |
| 2861 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2862 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2863 | if (data) |
| 2864 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 2865 | else if (root == root->fs_info->chunk_root) |
| 2866 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2867 | else |
| 2868 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 2869 | |
| 2870 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2874 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2875 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2876 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /* |
| 2880 | * This will check the space that the inode allocates from to make sure we have |
| 2881 | * enough space for bytes. |
| 2882 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2883 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2884 | { |
| 2885 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2886 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 2887 | u64 used; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2888 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2889 | |
| 2890 | /* make sure bytes are sectorsize aligned */ |
| 2891 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2892 | |
| 2893 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 2894 | if (!data_sinfo) |
| 2895 | goto alloc; |
| 2896 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2897 | again: |
| 2898 | /* make sure we have enough space to handle the data first */ |
| 2899 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2900 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 2901 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 2902 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 2903 | |
| 2904 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2905 | struct btrfs_trans_handle *trans; |
| 2906 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2907 | /* |
| 2908 | * if we don't have enough free bytes in this space then we need |
| 2909 | * to alloc a new chunk. |
| 2910 | */ |
| 2911 | if (!data_sinfo->full) { |
| 2912 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2913 | |
| 2914 | data_sinfo->force_alloc = 1; |
| 2915 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 2916 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2917 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2918 | trans = btrfs_join_transaction(root, 1); |
| 2919 | if (IS_ERR(trans)) |
| 2920 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2921 | |
| 2922 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2923 | bytes + 2 * 1024 * 1024, |
| 2924 | alloc_target, 0); |
| 2925 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2926 | if (ret < 0) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2927 | return ret; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 2928 | |
| 2929 | if (!data_sinfo) { |
| 2930 | btrfs_set_inode_space_info(root, inode); |
| 2931 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2932 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2933 | goto again; |
| 2934 | } |
| 2935 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2936 | |
| 2937 | /* commit the current transaction and try again */ |
Sage Weil | dd7e0b7 | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2938 | if (!committed && !root->fs_info->open_ioctl_trans) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2939 | committed = 1; |
| 2940 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2941 | if (IS_ERR(trans)) |
| 2942 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2943 | ret = btrfs_commit_transaction(trans, root); |
| 2944 | if (ret) |
| 2945 | return ret; |
| 2946 | goto again; |
| 2947 | } |
| 2948 | |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 2949 | #if 0 /* I hope we never need this code again, just in case */ |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2950 | printk(KERN_ERR "no space left, need %llu, %llu bytes_used, " |
| 2951 | "%llu bytes_reserved, " "%llu bytes_pinned, " |
| 2952 | "%llu bytes_readonly, %llu may use %llu total\n", |
| 2953 | (unsigned long long)bytes, |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 2954 | (unsigned long long)data_sinfo->bytes_used, |
| 2955 | (unsigned long long)data_sinfo->bytes_reserved, |
| 2956 | (unsigned long long)data_sinfo->bytes_pinned, |
| 2957 | (unsigned long long)data_sinfo->bytes_readonly, |
| 2958 | (unsigned long long)data_sinfo->bytes_may_use, |
| 2959 | (unsigned long long)data_sinfo->total_bytes); |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 2960 | #endif |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2961 | return -ENOSPC; |
| 2962 | } |
| 2963 | data_sinfo->bytes_may_use += bytes; |
| 2964 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 2965 | spin_unlock(&data_sinfo->lock); |
| 2966 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2967 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | /* |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2971 | * called when we are clearing an delalloc extent from the |
| 2972 | * inode's io_tree or there was an error for whatever reason |
| 2973 | * after calling btrfs_check_data_free_space |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2974 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2975 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2976 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2977 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2978 | struct btrfs_space_info *data_sinfo; |
| 2979 | |
| 2980 | /* make sure bytes are sectorsize aligned */ |
| 2981 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2982 | |
| 2983 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2984 | spin_lock(&data_sinfo->lock); |
| 2985 | data_sinfo->bytes_may_use -= bytes; |
| 2986 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2987 | spin_unlock(&data_sinfo->lock); |
| 2988 | } |
| 2989 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2990 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 2991 | { |
| 2992 | struct list_head *head = &info->space_info; |
| 2993 | struct btrfs_space_info *found; |
| 2994 | |
| 2995 | rcu_read_lock(); |
| 2996 | list_for_each_entry_rcu(found, head, list) { |
| 2997 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2998 | found->force_alloc = 1; |
| 2999 | } |
| 3000 | rcu_read_unlock(); |
| 3001 | } |
| 3002 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3003 | static int should_alloc_chunk(struct btrfs_space_info *sinfo, |
| 3004 | u64 alloc_bytes) |
| 3005 | { |
| 3006 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
| 3007 | |
| 3008 | if (sinfo->bytes_used + sinfo->bytes_reserved + |
| 3009 | alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
| 3010 | return 0; |
| 3011 | |
| 3012 | if (sinfo->bytes_used + sinfo->bytes_reserved + |
| 3013 | alloc_bytes < div_factor(num_bytes, 8)) |
| 3014 | return 0; |
| 3015 | |
| 3016 | return 1; |
| 3017 | } |
| 3018 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3019 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3020 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3021 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3022 | { |
| 3023 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3024 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3025 | int ret = 0; |
| 3026 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3027 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3028 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3029 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3030 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3031 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3032 | if (!space_info) { |
| 3033 | ret = update_space_info(extent_root->fs_info, flags, |
| 3034 | 0, 0, &space_info); |
| 3035 | BUG_ON(ret); |
| 3036 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3037 | BUG_ON(!space_info); |
| 3038 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3039 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3040 | if (space_info->force_alloc) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3041 | force = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3042 | if (space_info->full) { |
| 3043 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3044 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3045 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3046 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3047 | if (!force && !should_alloc_chunk(space_info, alloc_bytes)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3048 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3049 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3050 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3051 | spin_unlock(&space_info->lock); |
| 3052 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3053 | /* |
| 3054 | * if we're doing a data chunk, go ahead and make sure that |
| 3055 | * we keep a reasonable number of metadata chunks allocated in the |
| 3056 | * FS as well. |
| 3057 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3058 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3059 | fs_info->data_chunk_allocations++; |
| 3060 | if (!(fs_info->data_chunk_allocations % |
| 3061 | fs_info->metadata_ratio)) |
| 3062 | force_metadata_allocation(fs_info); |
| 3063 | } |
| 3064 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3065 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3066 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3067 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3068 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3069 | else |
| 3070 | ret = 1; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3071 | space_info->force_alloc = 0; |
| 3072 | spin_unlock(&space_info->lock); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3073 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3074 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3075 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3076 | } |
| 3077 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3078 | static int maybe_allocate_chunk(struct btrfs_trans_handle *trans, |
| 3079 | struct btrfs_root *root, |
| 3080 | struct btrfs_space_info *sinfo, u64 num_bytes) |
| 3081 | { |
| 3082 | int ret; |
| 3083 | int end_trans = 0; |
| 3084 | |
| 3085 | if (sinfo->full) |
| 3086 | return 0; |
| 3087 | |
| 3088 | spin_lock(&sinfo->lock); |
| 3089 | ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024); |
| 3090 | spin_unlock(&sinfo->lock); |
| 3091 | if (!ret) |
| 3092 | return 0; |
| 3093 | |
| 3094 | if (!trans) { |
| 3095 | trans = btrfs_join_transaction(root, 1); |
| 3096 | BUG_ON(IS_ERR(trans)); |
| 3097 | end_trans = 1; |
| 3098 | } |
| 3099 | |
| 3100 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3101 | num_bytes + 2 * 1024 * 1024, |
| 3102 | get_alloc_profile(root, sinfo->flags), 0); |
| 3103 | |
| 3104 | if (end_trans) |
| 3105 | btrfs_end_transaction(trans, root); |
| 3106 | |
| 3107 | return ret == 1 ? 1 : 0; |
| 3108 | } |
| 3109 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3110 | /* |
| 3111 | * shrink metadata reservation for delalloc |
| 3112 | */ |
| 3113 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3114 | struct btrfs_root *root, u64 to_reclaim) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3115 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3116 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3117 | u64 reserved; |
| 3118 | u64 max_reclaim; |
| 3119 | u64 reclaimed = 0; |
Josef Bacik | a1f7650 | 2010-09-16 14:29:55 -0400 | [diff] [blame] | 3120 | int no_reclaim = 0; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3121 | int pause = 1; |
| 3122 | int ret; |
| 3123 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3124 | block_rsv = &root->fs_info->delalloc_block_rsv; |
| 3125 | spin_lock(&block_rsv->lock); |
| 3126 | reserved = block_rsv->reserved; |
| 3127 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3128 | |
| 3129 | if (reserved == 0) |
| 3130 | return 0; |
| 3131 | |
| 3132 | max_reclaim = min(reserved, to_reclaim); |
| 3133 | |
| 3134 | while (1) { |
| 3135 | ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0); |
| 3136 | if (!ret) { |
Josef Bacik | a1f7650 | 2010-09-16 14:29:55 -0400 | [diff] [blame] | 3137 | if (no_reclaim > 2) |
| 3138 | break; |
| 3139 | no_reclaim++; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3140 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3141 | schedule_timeout(pause); |
| 3142 | pause <<= 1; |
| 3143 | if (pause > HZ / 10) |
| 3144 | pause = HZ / 10; |
| 3145 | } else { |
Josef Bacik | a1f7650 | 2010-09-16 14:29:55 -0400 | [diff] [blame] | 3146 | no_reclaim = 0; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3147 | pause = 1; |
| 3148 | } |
| 3149 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3150 | spin_lock(&block_rsv->lock); |
| 3151 | if (reserved > block_rsv->reserved) |
| 3152 | reclaimed = reserved - block_rsv->reserved; |
| 3153 | reserved = block_rsv->reserved; |
| 3154 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3155 | |
| 3156 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3157 | break; |
| 3158 | |
| 3159 | if (trans && trans->transaction->blocked) |
| 3160 | return -EAGAIN; |
| 3161 | } |
| 3162 | return reclaimed >= to_reclaim; |
| 3163 | } |
| 3164 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3165 | static int should_retry_reserve(struct btrfs_trans_handle *trans, |
| 3166 | struct btrfs_root *root, |
| 3167 | struct btrfs_block_rsv *block_rsv, |
| 3168 | u64 num_bytes, int *retries) |
| 3169 | { |
| 3170 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3171 | int ret; |
| 3172 | |
| 3173 | if ((*retries) > 2) |
| 3174 | return -ENOSPC; |
| 3175 | |
| 3176 | ret = maybe_allocate_chunk(trans, root, space_info, num_bytes); |
| 3177 | if (ret) |
| 3178 | return 1; |
| 3179 | |
| 3180 | if (trans && trans->transaction->in_commit) |
| 3181 | return -ENOSPC; |
| 3182 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3183 | ret = shrink_delalloc(trans, root, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3184 | if (ret) |
| 3185 | return ret; |
| 3186 | |
| 3187 | spin_lock(&space_info->lock); |
| 3188 | if (space_info->bytes_pinned < num_bytes) |
| 3189 | ret = 1; |
| 3190 | spin_unlock(&space_info->lock); |
| 3191 | if (ret) |
| 3192 | return -ENOSPC; |
| 3193 | |
| 3194 | (*retries)++; |
| 3195 | |
| 3196 | if (trans) |
| 3197 | return -EAGAIN; |
| 3198 | |
| 3199 | trans = btrfs_join_transaction(root, 1); |
| 3200 | BUG_ON(IS_ERR(trans)); |
| 3201 | ret = btrfs_commit_transaction(trans, root); |
| 3202 | BUG_ON(ret); |
| 3203 | |
| 3204 | return 1; |
| 3205 | } |
| 3206 | |
| 3207 | static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv, |
| 3208 | u64 num_bytes) |
| 3209 | { |
| 3210 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3211 | u64 unused; |
| 3212 | int ret = -ENOSPC; |
| 3213 | |
| 3214 | spin_lock(&space_info->lock); |
| 3215 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame^] | 3216 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3217 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3218 | |
| 3219 | if (unused < space_info->total_bytes) |
| 3220 | unused = space_info->total_bytes - unused; |
| 3221 | else |
| 3222 | unused = 0; |
| 3223 | |
| 3224 | if (unused >= num_bytes) { |
| 3225 | if (block_rsv->priority >= 10) { |
| 3226 | space_info->bytes_reserved += num_bytes; |
| 3227 | ret = 0; |
| 3228 | } else { |
| 3229 | if ((unused + block_rsv->reserved) * |
| 3230 | block_rsv->priority >= |
| 3231 | (num_bytes + block_rsv->reserved) * 10) { |
| 3232 | space_info->bytes_reserved += num_bytes; |
| 3233 | ret = 0; |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | spin_unlock(&space_info->lock); |
| 3238 | |
| 3239 | return ret; |
| 3240 | } |
| 3241 | |
| 3242 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3243 | struct btrfs_root *root) |
| 3244 | { |
| 3245 | struct btrfs_block_rsv *block_rsv; |
| 3246 | if (root->ref_cows) |
| 3247 | block_rsv = trans->block_rsv; |
| 3248 | else |
| 3249 | block_rsv = root->block_rsv; |
| 3250 | |
| 3251 | if (!block_rsv) |
| 3252 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3253 | |
| 3254 | return block_rsv; |
| 3255 | } |
| 3256 | |
| 3257 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3258 | u64 num_bytes) |
| 3259 | { |
| 3260 | int ret = -ENOSPC; |
| 3261 | spin_lock(&block_rsv->lock); |
| 3262 | if (block_rsv->reserved >= num_bytes) { |
| 3263 | block_rsv->reserved -= num_bytes; |
| 3264 | if (block_rsv->reserved < block_rsv->size) |
| 3265 | block_rsv->full = 0; |
| 3266 | ret = 0; |
| 3267 | } |
| 3268 | spin_unlock(&block_rsv->lock); |
| 3269 | return ret; |
| 3270 | } |
| 3271 | |
| 3272 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3273 | u64 num_bytes, int update_size) |
| 3274 | { |
| 3275 | spin_lock(&block_rsv->lock); |
| 3276 | block_rsv->reserved += num_bytes; |
| 3277 | if (update_size) |
| 3278 | block_rsv->size += num_bytes; |
| 3279 | else if (block_rsv->reserved >= block_rsv->size) |
| 3280 | block_rsv->full = 1; |
| 3281 | spin_unlock(&block_rsv->lock); |
| 3282 | } |
| 3283 | |
| 3284 | void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3285 | struct btrfs_block_rsv *dest, u64 num_bytes) |
| 3286 | { |
| 3287 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3288 | |
| 3289 | spin_lock(&block_rsv->lock); |
| 3290 | if (num_bytes == (u64)-1) |
| 3291 | num_bytes = block_rsv->size; |
| 3292 | block_rsv->size -= num_bytes; |
| 3293 | if (block_rsv->reserved >= block_rsv->size) { |
| 3294 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3295 | block_rsv->reserved = block_rsv->size; |
| 3296 | block_rsv->full = 1; |
| 3297 | } else { |
| 3298 | num_bytes = 0; |
| 3299 | } |
| 3300 | spin_unlock(&block_rsv->lock); |
| 3301 | |
| 3302 | if (num_bytes > 0) { |
| 3303 | if (dest) { |
| 3304 | block_rsv_add_bytes(dest, num_bytes, 0); |
| 3305 | } else { |
| 3306 | spin_lock(&space_info->lock); |
| 3307 | space_info->bytes_reserved -= num_bytes; |
| 3308 | spin_unlock(&space_info->lock); |
| 3309 | } |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3314 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3315 | { |
| 3316 | int ret; |
| 3317 | |
| 3318 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3319 | if (ret) |
| 3320 | return ret; |
| 3321 | |
| 3322 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3323 | return 0; |
| 3324 | } |
| 3325 | |
| 3326 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3327 | { |
| 3328 | memset(rsv, 0, sizeof(*rsv)); |
| 3329 | spin_lock_init(&rsv->lock); |
| 3330 | atomic_set(&rsv->usage, 1); |
| 3331 | rsv->priority = 6; |
| 3332 | INIT_LIST_HEAD(&rsv->list); |
| 3333 | } |
| 3334 | |
| 3335 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3336 | { |
| 3337 | struct btrfs_block_rsv *block_rsv; |
| 3338 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3339 | u64 alloc_target; |
| 3340 | |
| 3341 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3342 | if (!block_rsv) |
| 3343 | return NULL; |
| 3344 | |
| 3345 | btrfs_init_block_rsv(block_rsv); |
| 3346 | |
| 3347 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3348 | block_rsv->space_info = __find_space_info(fs_info, |
| 3349 | BTRFS_BLOCK_GROUP_METADATA); |
| 3350 | |
| 3351 | return block_rsv; |
| 3352 | } |
| 3353 | |
| 3354 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3355 | struct btrfs_block_rsv *rsv) |
| 3356 | { |
| 3357 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3358 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3359 | if (!rsv->durable) |
| 3360 | kfree(rsv); |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * make the block_rsv struct be able to capture freed space. |
| 3366 | * the captured space will re-add to the the block_rsv struct |
| 3367 | * after transaction commit |
| 3368 | */ |
| 3369 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3370 | struct btrfs_block_rsv *block_rsv) |
| 3371 | { |
| 3372 | block_rsv->durable = 1; |
| 3373 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3374 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3375 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3376 | } |
| 3377 | |
| 3378 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3379 | struct btrfs_root *root, |
| 3380 | struct btrfs_block_rsv *block_rsv, |
| 3381 | u64 num_bytes, int *retries) |
| 3382 | { |
| 3383 | int ret; |
| 3384 | |
| 3385 | if (num_bytes == 0) |
| 3386 | return 0; |
| 3387 | again: |
| 3388 | ret = reserve_metadata_bytes(block_rsv, num_bytes); |
| 3389 | if (!ret) { |
| 3390 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3391 | return 0; |
| 3392 | } |
| 3393 | |
| 3394 | ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries); |
| 3395 | if (ret > 0) |
| 3396 | goto again; |
| 3397 | |
| 3398 | return ret; |
| 3399 | } |
| 3400 | |
| 3401 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3402 | struct btrfs_root *root, |
| 3403 | struct btrfs_block_rsv *block_rsv, |
| 3404 | u64 min_reserved, int min_factor) |
| 3405 | { |
| 3406 | u64 num_bytes = 0; |
| 3407 | int commit_trans = 0; |
| 3408 | int ret = -ENOSPC; |
| 3409 | |
| 3410 | if (!block_rsv) |
| 3411 | return 0; |
| 3412 | |
| 3413 | spin_lock(&block_rsv->lock); |
| 3414 | if (min_factor > 0) |
| 3415 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3416 | if (min_reserved > num_bytes) |
| 3417 | num_bytes = min_reserved; |
| 3418 | |
| 3419 | if (block_rsv->reserved >= num_bytes) { |
| 3420 | ret = 0; |
| 3421 | } else { |
| 3422 | num_bytes -= block_rsv->reserved; |
| 3423 | if (block_rsv->durable && |
| 3424 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3425 | commit_trans = 1; |
| 3426 | } |
| 3427 | spin_unlock(&block_rsv->lock); |
| 3428 | if (!ret) |
| 3429 | return 0; |
| 3430 | |
| 3431 | if (block_rsv->refill_used) { |
| 3432 | ret = reserve_metadata_bytes(block_rsv, num_bytes); |
| 3433 | if (!ret) { |
| 3434 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3435 | return 0; |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | if (commit_trans) { |
| 3440 | if (trans) |
| 3441 | return -EAGAIN; |
| 3442 | |
| 3443 | trans = btrfs_join_transaction(root, 1); |
| 3444 | BUG_ON(IS_ERR(trans)); |
| 3445 | ret = btrfs_commit_transaction(trans, root); |
| 3446 | return 0; |
| 3447 | } |
| 3448 | |
| 3449 | WARN_ON(1); |
| 3450 | printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n", |
| 3451 | block_rsv->size, block_rsv->reserved, |
| 3452 | block_rsv->freed[0], block_rsv->freed[1]); |
| 3453 | |
| 3454 | return -ENOSPC; |
| 3455 | } |
| 3456 | |
| 3457 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3458 | struct btrfs_block_rsv *dst_rsv, |
| 3459 | u64 num_bytes) |
| 3460 | { |
| 3461 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3462 | } |
| 3463 | |
| 3464 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3465 | struct btrfs_block_rsv *block_rsv, |
| 3466 | u64 num_bytes) |
| 3467 | { |
| 3468 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3469 | if (global_rsv->full || global_rsv == block_rsv || |
| 3470 | block_rsv->space_info != global_rsv->space_info) |
| 3471 | global_rsv = NULL; |
| 3472 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3473 | } |
| 3474 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3475 | /* |
| 3476 | * helper to calculate size of global block reservation. |
| 3477 | * the desired value is sum of space used by extent tree, |
| 3478 | * checksum tree and root tree |
| 3479 | */ |
| 3480 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3481 | { |
| 3482 | struct btrfs_space_info *sinfo; |
| 3483 | u64 num_bytes; |
| 3484 | u64 meta_used; |
| 3485 | u64 data_used; |
| 3486 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
| 3487 | #if 0 |
| 3488 | /* |
| 3489 | * per tree used space accounting can be inaccuracy, so we |
| 3490 | * can't rely on it. |
| 3491 | */ |
| 3492 | spin_lock(&fs_info->extent_root->accounting_lock); |
| 3493 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item); |
| 3494 | spin_unlock(&fs_info->extent_root->accounting_lock); |
| 3495 | |
| 3496 | spin_lock(&fs_info->csum_root->accounting_lock); |
| 3497 | num_bytes += btrfs_root_used(&fs_info->csum_root->root_item); |
| 3498 | spin_unlock(&fs_info->csum_root->accounting_lock); |
| 3499 | |
| 3500 | spin_lock(&fs_info->tree_root->accounting_lock); |
| 3501 | num_bytes += btrfs_root_used(&fs_info->tree_root->root_item); |
| 3502 | spin_unlock(&fs_info->tree_root->accounting_lock); |
| 3503 | #endif |
| 3504 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3505 | spin_lock(&sinfo->lock); |
| 3506 | data_used = sinfo->bytes_used; |
| 3507 | spin_unlock(&sinfo->lock); |
| 3508 | |
| 3509 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3510 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame^] | 3511 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3512 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3513 | meta_used = sinfo->bytes_used; |
| 3514 | spin_unlock(&sinfo->lock); |
| 3515 | |
| 3516 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3517 | csum_size * 2; |
| 3518 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3519 | |
| 3520 | if (num_bytes * 3 > meta_used) |
| 3521 | num_bytes = div64_u64(meta_used, 3); |
| 3522 | |
| 3523 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3524 | } |
| 3525 | |
| 3526 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3527 | { |
| 3528 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3529 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3530 | u64 num_bytes; |
| 3531 | |
| 3532 | num_bytes = calc_global_metadata_size(fs_info); |
| 3533 | |
| 3534 | spin_lock(&block_rsv->lock); |
| 3535 | spin_lock(&sinfo->lock); |
| 3536 | |
| 3537 | block_rsv->size = num_bytes; |
| 3538 | |
| 3539 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame^] | 3540 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3541 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3542 | |
| 3543 | if (sinfo->total_bytes > num_bytes) { |
| 3544 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3545 | block_rsv->reserved += num_bytes; |
| 3546 | sinfo->bytes_reserved += num_bytes; |
| 3547 | } |
| 3548 | |
| 3549 | if (block_rsv->reserved >= block_rsv->size) { |
| 3550 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3551 | sinfo->bytes_reserved -= num_bytes; |
| 3552 | block_rsv->reserved = block_rsv->size; |
| 3553 | block_rsv->full = 1; |
| 3554 | } |
| 3555 | #if 0 |
| 3556 | printk(KERN_INFO"global block rsv size %llu reserved %llu\n", |
| 3557 | block_rsv->size, block_rsv->reserved); |
| 3558 | #endif |
| 3559 | spin_unlock(&sinfo->lock); |
| 3560 | spin_unlock(&block_rsv->lock); |
| 3561 | } |
| 3562 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3563 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3564 | { |
| 3565 | struct btrfs_space_info *space_info; |
| 3566 | |
| 3567 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3568 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3569 | fs_info->chunk_block_rsv.priority = 10; |
| 3570 | |
| 3571 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3572 | fs_info->global_block_rsv.space_info = space_info; |
| 3573 | fs_info->global_block_rsv.priority = 10; |
| 3574 | fs_info->global_block_rsv.refill_used = 1; |
| 3575 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3576 | fs_info->trans_block_rsv.space_info = space_info; |
| 3577 | fs_info->empty_block_rsv.space_info = space_info; |
| 3578 | fs_info->empty_block_rsv.priority = 10; |
| 3579 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3580 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3581 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3582 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3583 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3584 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3585 | |
| 3586 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3587 | |
| 3588 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3589 | |
| 3590 | update_global_block_rsv(fs_info); |
| 3591 | } |
| 3592 | |
| 3593 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3594 | { |
| 3595 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3596 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3597 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3598 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3599 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3600 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3601 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3602 | } |
| 3603 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3604 | static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items) |
| 3605 | { |
| 3606 | return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) * |
| 3607 | 3 * num_items; |
| 3608 | } |
| 3609 | |
| 3610 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3611 | struct btrfs_root *root, |
| 3612 | int num_items, int *retries) |
| 3613 | { |
| 3614 | u64 num_bytes; |
| 3615 | int ret; |
| 3616 | |
| 3617 | if (num_items == 0 || root->fs_info->chunk_root == root) |
| 3618 | return 0; |
| 3619 | |
| 3620 | num_bytes = calc_trans_metadata_size(root, num_items); |
| 3621 | ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv, |
| 3622 | num_bytes, retries); |
| 3623 | if (!ret) { |
| 3624 | trans->bytes_reserved += num_bytes; |
| 3625 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 3626 | } |
| 3627 | return ret; |
| 3628 | } |
| 3629 | |
| 3630 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3631 | struct btrfs_root *root) |
| 3632 | { |
| 3633 | if (!trans->bytes_reserved) |
| 3634 | return; |
| 3635 | |
| 3636 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 3637 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3638 | trans->bytes_reserved); |
| 3639 | trans->bytes_reserved = 0; |
| 3640 | } |
| 3641 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3642 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3643 | struct inode *inode) |
| 3644 | { |
| 3645 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3646 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3647 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3648 | |
| 3649 | /* |
| 3650 | * one for deleting orphan item, one for updating inode and |
| 3651 | * two for calling btrfs_truncate_inode_items. |
| 3652 | * |
| 3653 | * btrfs_truncate_inode_items is a delete operation, it frees |
| 3654 | * more space than it uses in most cases. So two units of |
| 3655 | * metadata space should be enough for calling it many times. |
| 3656 | * If all of the metadata space is used, we can commit |
| 3657 | * transaction and use space it freed. |
| 3658 | */ |
| 3659 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3660 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3661 | } |
| 3662 | |
| 3663 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3664 | { |
| 3665 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3666 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3667 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3668 | } |
| 3669 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3670 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3671 | struct btrfs_pending_snapshot *pending) |
| 3672 | { |
| 3673 | struct btrfs_root *root = pending->root; |
| 3674 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3675 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3676 | /* |
| 3677 | * two for root back/forward refs, two for directory entries |
| 3678 | * and one for root of the snapshot. |
| 3679 | */ |
| 3680 | u64 num_bytes = calc_trans_metadata_size(root, 5); |
| 3681 | dst_rsv->space_info = src_rsv->space_info; |
| 3682 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3683 | } |
| 3684 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3685 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 3686 | { |
| 3687 | return num_bytes >>= 3; |
| 3688 | } |
| 3689 | |
| 3690 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 3691 | { |
| 3692 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3693 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
| 3694 | u64 to_reserve; |
| 3695 | int nr_extents; |
| 3696 | int retries = 0; |
| 3697 | int ret; |
| 3698 | |
| 3699 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 3700 | schedule_timeout(1); |
| 3701 | |
| 3702 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 3703 | again: |
| 3704 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 3705 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; |
| 3706 | if (nr_extents > BTRFS_I(inode)->reserved_extents) { |
| 3707 | nr_extents -= BTRFS_I(inode)->reserved_extents; |
| 3708 | to_reserve = calc_trans_metadata_size(root, nr_extents); |
| 3709 | } else { |
| 3710 | nr_extents = 0; |
| 3711 | to_reserve = 0; |
| 3712 | } |
| 3713 | |
| 3714 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
| 3715 | ret = reserve_metadata_bytes(block_rsv, to_reserve); |
| 3716 | if (ret) { |
| 3717 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 3718 | ret = should_retry_reserve(NULL, root, block_rsv, to_reserve, |
| 3719 | &retries); |
| 3720 | if (ret > 0) |
| 3721 | goto again; |
| 3722 | return ret; |
| 3723 | } |
| 3724 | |
| 3725 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 3726 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
| 3727 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 3728 | |
| 3729 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 3730 | |
| 3731 | if (block_rsv->size > 512 * 1024 * 1024) |
| 3732 | shrink_delalloc(NULL, root, to_reserve); |
| 3733 | |
| 3734 | return 0; |
| 3735 | } |
| 3736 | |
| 3737 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 3738 | { |
| 3739 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3740 | u64 to_free; |
| 3741 | int nr_extents; |
| 3742 | |
| 3743 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 3744 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 3745 | |
| 3746 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 3747 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); |
| 3748 | if (nr_extents < BTRFS_I(inode)->reserved_extents) { |
| 3749 | nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents; |
| 3750 | BTRFS_I(inode)->reserved_extents -= nr_extents; |
| 3751 | } else { |
| 3752 | nr_extents = 0; |
| 3753 | } |
| 3754 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 3755 | |
| 3756 | to_free = calc_csum_metadata_size(inode, num_bytes); |
| 3757 | if (nr_extents > 0) |
| 3758 | to_free += calc_trans_metadata_size(root, nr_extents); |
| 3759 | |
| 3760 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 3761 | to_free); |
| 3762 | } |
| 3763 | |
| 3764 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 3765 | { |
| 3766 | int ret; |
| 3767 | |
| 3768 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 3769 | if (ret) |
| 3770 | return ret; |
| 3771 | |
| 3772 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 3773 | if (ret) { |
| 3774 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 3775 | return ret; |
| 3776 | } |
| 3777 | |
| 3778 | return 0; |
| 3779 | } |
| 3780 | |
| 3781 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 3782 | { |
| 3783 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 3784 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 3785 | } |
| 3786 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3787 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 3788 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3789 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3790 | { |
| 3791 | struct btrfs_block_group_cache *cache; |
| 3792 | struct btrfs_fs_info *info = root->fs_info; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3793 | int factor; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3794 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3795 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3796 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3797 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3798 | /* block accounting for super block */ |
| 3799 | spin_lock(&info->delalloc_lock); |
| 3800 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 3801 | if (alloc) |
| 3802 | old_val += num_bytes; |
| 3803 | else |
| 3804 | old_val -= num_bytes; |
| 3805 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3806 | spin_unlock(&info->delalloc_lock); |
| 3807 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3808 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3809 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3810 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3811 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3812 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 3813 | BTRFS_BLOCK_GROUP_RAID1 | |
| 3814 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3815 | factor = 2; |
| 3816 | else |
| 3817 | factor = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3818 | byte_in_group = bytenr - cache->key.objectid; |
| 3819 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3820 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3821 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3822 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3823 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3824 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3825 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3826 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3827 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3828 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3829 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3830 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3831 | cache->space_info->bytes_used += num_bytes; |
| 3832 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3833 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3834 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3835 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3836 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3837 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3838 | cache->pinned += num_bytes; |
| 3839 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3840 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3841 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3842 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3843 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3844 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3845 | set_extent_dirty(info->pinned_extents, |
| 3846 | bytenr, bytenr + num_bytes - 1, |
| 3847 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3848 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3849 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3850 | total -= num_bytes; |
| 3851 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3852 | } |
| 3853 | return 0; |
| 3854 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3855 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3856 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 3857 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3858 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3859 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3860 | |
| 3861 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 3862 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3863 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3864 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3865 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3866 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3867 | |
| 3868 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3869 | } |
| 3870 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3871 | static int pin_down_extent(struct btrfs_root *root, |
| 3872 | struct btrfs_block_group_cache *cache, |
| 3873 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3874 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3875 | spin_lock(&cache->space_info->lock); |
| 3876 | spin_lock(&cache->lock); |
| 3877 | cache->pinned += num_bytes; |
| 3878 | cache->space_info->bytes_pinned += num_bytes; |
| 3879 | if (reserved) { |
| 3880 | cache->reserved -= num_bytes; |
| 3881 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3882 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3883 | spin_unlock(&cache->lock); |
| 3884 | spin_unlock(&cache->space_info->lock); |
| 3885 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3886 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 3887 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3888 | return 0; |
| 3889 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3890 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3891 | /* |
| 3892 | * this function must be called within transaction |
| 3893 | */ |
| 3894 | int btrfs_pin_extent(struct btrfs_root *root, |
| 3895 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3896 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3897 | struct btrfs_block_group_cache *cache; |
| 3898 | |
| 3899 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 3900 | BUG_ON(!cache); |
| 3901 | |
| 3902 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 3903 | |
| 3904 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3905 | return 0; |
| 3906 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3907 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3908 | /* |
| 3909 | * update size of reserved extents. this function may return -EAGAIN |
| 3910 | * if 'reserve' is true or 'sinfo' is false. |
| 3911 | */ |
| 3912 | static int update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 3913 | u64 num_bytes, int reserve, int sinfo) |
| 3914 | { |
| 3915 | int ret = 0; |
| 3916 | if (sinfo) { |
| 3917 | struct btrfs_space_info *space_info = cache->space_info; |
| 3918 | spin_lock(&space_info->lock); |
| 3919 | spin_lock(&cache->lock); |
| 3920 | if (reserve) { |
| 3921 | if (cache->ro) { |
| 3922 | ret = -EAGAIN; |
| 3923 | } else { |
| 3924 | cache->reserved += num_bytes; |
| 3925 | space_info->bytes_reserved += num_bytes; |
| 3926 | } |
| 3927 | } else { |
| 3928 | if (cache->ro) |
| 3929 | space_info->bytes_readonly += num_bytes; |
| 3930 | cache->reserved -= num_bytes; |
| 3931 | space_info->bytes_reserved -= num_bytes; |
| 3932 | } |
| 3933 | spin_unlock(&cache->lock); |
| 3934 | spin_unlock(&space_info->lock); |
| 3935 | } else { |
| 3936 | spin_lock(&cache->lock); |
| 3937 | if (cache->ro) { |
| 3938 | ret = -EAGAIN; |
| 3939 | } else { |
| 3940 | if (reserve) |
| 3941 | cache->reserved += num_bytes; |
| 3942 | else |
| 3943 | cache->reserved -= num_bytes; |
| 3944 | } |
| 3945 | spin_unlock(&cache->lock); |
| 3946 | } |
| 3947 | return ret; |
| 3948 | } |
| 3949 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3950 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 3951 | struct btrfs_root *root) |
| 3952 | { |
| 3953 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3954 | struct btrfs_caching_control *next; |
| 3955 | struct btrfs_caching_control *caching_ctl; |
| 3956 | struct btrfs_block_group_cache *cache; |
| 3957 | |
| 3958 | down_write(&fs_info->extent_commit_sem); |
| 3959 | |
| 3960 | list_for_each_entry_safe(caching_ctl, next, |
| 3961 | &fs_info->caching_block_groups, list) { |
| 3962 | cache = caching_ctl->block_group; |
| 3963 | if (block_group_cache_done(cache)) { |
| 3964 | cache->last_byte_to_unpin = (u64)-1; |
| 3965 | list_del_init(&caching_ctl->list); |
| 3966 | put_caching_control(caching_ctl); |
| 3967 | } else { |
| 3968 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3973 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 3974 | else |
| 3975 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 3976 | |
| 3977 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3978 | |
| 3979 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3980 | return 0; |
| 3981 | } |
| 3982 | |
| 3983 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 3984 | { |
| 3985 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3986 | struct btrfs_block_group_cache *cache = NULL; |
| 3987 | u64 len; |
| 3988 | |
| 3989 | while (start <= end) { |
| 3990 | if (!cache || |
| 3991 | start >= cache->key.objectid + cache->key.offset) { |
| 3992 | if (cache) |
| 3993 | btrfs_put_block_group(cache); |
| 3994 | cache = btrfs_lookup_block_group(fs_info, start); |
| 3995 | BUG_ON(!cache); |
| 3996 | } |
| 3997 | |
| 3998 | len = cache->key.objectid + cache->key.offset - start; |
| 3999 | len = min(len, end + 1 - start); |
| 4000 | |
| 4001 | if (start < cache->last_byte_to_unpin) { |
| 4002 | len = min(len, cache->last_byte_to_unpin - start); |
| 4003 | btrfs_add_free_space(cache, start, len); |
| 4004 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4005 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4006 | start += len; |
| 4007 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4008 | spin_lock(&cache->space_info->lock); |
| 4009 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4010 | cache->pinned -= len; |
| 4011 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4012 | if (cache->ro) { |
| 4013 | cache->space_info->bytes_readonly += len; |
| 4014 | } else if (cache->reserved_pinned > 0) { |
| 4015 | len = min(len, cache->reserved_pinned); |
| 4016 | cache->reserved_pinned -= len; |
| 4017 | cache->space_info->bytes_reserved += len; |
| 4018 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4019 | spin_unlock(&cache->lock); |
| 4020 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4024 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4025 | return 0; |
| 4026 | } |
| 4027 | |
| 4028 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4029 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4030 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4031 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4032 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4033 | struct btrfs_block_rsv *block_rsv; |
| 4034 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4035 | u64 start; |
| 4036 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4037 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4038 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4039 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4040 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4041 | unpin = &fs_info->freed_extents[1]; |
| 4042 | else |
| 4043 | unpin = &fs_info->freed_extents[0]; |
| 4044 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4045 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4046 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4047 | EXTENT_DIRTY); |
| 4048 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4049 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4050 | |
| 4051 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 4052 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4053 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4054 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4055 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4056 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4057 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4058 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4059 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4060 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4061 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4062 | idx = trans->transid & 0x1; |
| 4063 | if (block_rsv->freed[idx] > 0) { |
| 4064 | block_rsv_add_bytes(block_rsv, |
| 4065 | block_rsv->freed[idx], 0); |
| 4066 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4067 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4068 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4069 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4070 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4071 | if (block_rsv->freed[0] == 0 && |
| 4072 | block_rsv->freed[1] == 0) { |
| 4073 | list_del_init(&block_rsv->list); |
| 4074 | kfree(block_rsv); |
| 4075 | } |
| 4076 | } else { |
| 4077 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4078 | } |
| 4079 | } |
| 4080 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4081 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4082 | return 0; |
| 4083 | } |
| 4084 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4085 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4086 | struct btrfs_root *root, |
| 4087 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4088 | u64 root_objectid, u64 owner_objectid, |
| 4089 | u64 owner_offset, int refs_to_drop, |
| 4090 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4091 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4092 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4093 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4094 | struct btrfs_fs_info *info = root->fs_info; |
| 4095 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4096 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4097 | struct btrfs_extent_item *ei; |
| 4098 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4099 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4100 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4101 | int extent_slot = 0; |
| 4102 | int found_extent = 0; |
| 4103 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4104 | u32 item_size; |
| 4105 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4106 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4107 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4108 | if (!path) |
| 4109 | return -ENOMEM; |
| 4110 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4111 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4112 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4113 | |
| 4114 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4115 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4116 | |
| 4117 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4118 | bytenr, num_bytes, parent, |
| 4119 | root_objectid, owner_objectid, |
| 4120 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4121 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4122 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4123 | while (extent_slot >= 0) { |
| 4124 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4125 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4126 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4127 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4128 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4129 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4130 | found_extent = 1; |
| 4131 | break; |
| 4132 | } |
| 4133 | if (path->slots[0] - extent_slot > 5) |
| 4134 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4135 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4136 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4137 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4138 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4139 | if (found_extent && item_size < sizeof(*ei)) |
| 4140 | found_extent = 0; |
| 4141 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4142 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4143 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4144 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4145 | NULL, refs_to_drop, |
| 4146 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4147 | BUG_ON(ret); |
| 4148 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4149 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4150 | |
| 4151 | key.objectid = bytenr; |
| 4152 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4153 | key.offset = num_bytes; |
| 4154 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4155 | ret = btrfs_search_slot(trans, extent_root, |
| 4156 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4157 | if (ret) { |
| 4158 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4159 | ", was looking for %llu\n", ret, |
| 4160 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4161 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4162 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4163 | BUG_ON(ret); |
| 4164 | extent_slot = path->slots[0]; |
| 4165 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4166 | } else { |
| 4167 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4168 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4169 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4170 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4171 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4172 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4173 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4174 | (unsigned long long)owner_objectid, |
| 4175 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4176 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4177 | |
| 4178 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4179 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4180 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4181 | if (item_size < sizeof(*ei)) { |
| 4182 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4183 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4184 | owner_objectid, 0); |
| 4185 | BUG_ON(ret < 0); |
| 4186 | |
| 4187 | btrfs_release_path(extent_root, path); |
| 4188 | path->leave_spinning = 1; |
| 4189 | |
| 4190 | key.objectid = bytenr; |
| 4191 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4192 | key.offset = num_bytes; |
| 4193 | |
| 4194 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4195 | -1, 1); |
| 4196 | if (ret) { |
| 4197 | printk(KERN_ERR "umm, got %d back from search" |
| 4198 | ", was looking for %llu\n", ret, |
| 4199 | (unsigned long long)bytenr); |
| 4200 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4201 | } |
| 4202 | BUG_ON(ret); |
| 4203 | extent_slot = path->slots[0]; |
| 4204 | leaf = path->nodes[0]; |
| 4205 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4206 | } |
| 4207 | #endif |
| 4208 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4209 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4210 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4211 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4212 | struct btrfs_tree_block_info *bi; |
| 4213 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4214 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4215 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4216 | } |
| 4217 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4218 | refs = btrfs_extent_refs(leaf, ei); |
| 4219 | BUG_ON(refs < refs_to_drop); |
| 4220 | refs -= refs_to_drop; |
| 4221 | |
| 4222 | if (refs > 0) { |
| 4223 | if (extent_op) |
| 4224 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4225 | /* |
| 4226 | * In the case of inline back ref, reference count will |
| 4227 | * be updated by remove_extent_backref |
| 4228 | */ |
| 4229 | if (iref) { |
| 4230 | BUG_ON(!found_extent); |
| 4231 | } else { |
| 4232 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4233 | btrfs_mark_buffer_dirty(leaf); |
| 4234 | } |
| 4235 | if (found_extent) { |
| 4236 | ret = remove_extent_backref(trans, extent_root, path, |
| 4237 | iref, refs_to_drop, |
| 4238 | is_data); |
| 4239 | BUG_ON(ret); |
| 4240 | } |
| 4241 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4242 | if (found_extent) { |
| 4243 | BUG_ON(is_data && refs_to_drop != |
| 4244 | extent_data_ref_count(root, path, iref)); |
| 4245 | if (iref) { |
| 4246 | BUG_ON(path->slots[0] != extent_slot); |
| 4247 | } else { |
| 4248 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4249 | path->slots[0] = extent_slot; |
| 4250 | num_to_del = 2; |
| 4251 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4252 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4253 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4254 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4255 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4256 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4257 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4258 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4259 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4260 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4261 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4262 | } else { |
| 4263 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4264 | bytenr >> PAGE_CACHE_SHIFT, |
| 4265 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4266 | } |
| 4267 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4268 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4269 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4270 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4271 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4272 | return ret; |
| 4273 | } |
| 4274 | |
| 4275 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4276 | * 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] | 4277 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4278 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4279 | * removes it from the tree. |
| 4280 | */ |
| 4281 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4282 | struct btrfs_root *root, u64 bytenr) |
| 4283 | { |
| 4284 | struct btrfs_delayed_ref_head *head; |
| 4285 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4286 | struct btrfs_delayed_ref_node *ref; |
| 4287 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4288 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4289 | |
| 4290 | delayed_refs = &trans->transaction->delayed_refs; |
| 4291 | spin_lock(&delayed_refs->lock); |
| 4292 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4293 | if (!head) |
| 4294 | goto out; |
| 4295 | |
| 4296 | node = rb_prev(&head->node.rb_node); |
| 4297 | if (!node) |
| 4298 | goto out; |
| 4299 | |
| 4300 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4301 | |
| 4302 | /* there are still entries for this ref, we can't drop it */ |
| 4303 | if (ref->bytenr == bytenr) |
| 4304 | goto out; |
| 4305 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4306 | if (head->extent_op) { |
| 4307 | if (!head->must_insert_reserved) |
| 4308 | goto out; |
| 4309 | kfree(head->extent_op); |
| 4310 | head->extent_op = NULL; |
| 4311 | } |
| 4312 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4313 | /* |
| 4314 | * waiting for the lock here would deadlock. If someone else has it |
| 4315 | * locked they are already in the process of dropping it anyway |
| 4316 | */ |
| 4317 | if (!mutex_trylock(&head->mutex)) |
| 4318 | goto out; |
| 4319 | |
| 4320 | /* |
| 4321 | * at this point we have a head with no other entries. Go |
| 4322 | * ahead and process it. |
| 4323 | */ |
| 4324 | head->node.in_tree = 0; |
| 4325 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4326 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4327 | delayed_refs->num_entries--; |
| 4328 | |
| 4329 | /* |
| 4330 | * we don't take a ref on the node because we're removing it from the |
| 4331 | * tree, so we just steal the ref the tree was holding. |
| 4332 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4333 | delayed_refs->num_heads--; |
| 4334 | if (list_empty(&head->cluster)) |
| 4335 | delayed_refs->num_heads_ready--; |
| 4336 | |
| 4337 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4338 | spin_unlock(&delayed_refs->lock); |
| 4339 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4340 | BUG_ON(head->extent_op); |
| 4341 | if (head->must_insert_reserved) |
| 4342 | ret = 1; |
| 4343 | |
| 4344 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4345 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4346 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4347 | out: |
| 4348 | spin_unlock(&delayed_refs->lock); |
| 4349 | return 0; |
| 4350 | } |
| 4351 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4352 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4353 | struct btrfs_root *root, |
| 4354 | struct extent_buffer *buf, |
| 4355 | u64 parent, int last_ref) |
| 4356 | { |
| 4357 | struct btrfs_block_rsv *block_rsv; |
| 4358 | struct btrfs_block_group_cache *cache = NULL; |
| 4359 | int ret; |
| 4360 | |
| 4361 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4362 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4363 | parent, root->root_key.objectid, |
| 4364 | btrfs_header_level(buf), |
| 4365 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4366 | BUG_ON(ret); |
| 4367 | } |
| 4368 | |
| 4369 | if (!last_ref) |
| 4370 | return; |
| 4371 | |
| 4372 | block_rsv = get_block_rsv(trans, root); |
| 4373 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4374 | if (block_rsv->space_info != cache->space_info) |
| 4375 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4376 | |
| 4377 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4378 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4379 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4380 | if (!ret) |
| 4381 | goto pin; |
| 4382 | } |
| 4383 | |
| 4384 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4385 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4386 | goto pin; |
| 4387 | } |
| 4388 | |
| 4389 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4390 | |
| 4391 | btrfs_add_free_space(cache, buf->start, buf->len); |
| 4392 | ret = update_reserved_bytes(cache, buf->len, 0, 0); |
| 4393 | if (ret == -EAGAIN) { |
| 4394 | /* block group became read-only */ |
| 4395 | update_reserved_bytes(cache, buf->len, 0, 1); |
| 4396 | goto out; |
| 4397 | } |
| 4398 | |
| 4399 | ret = 1; |
| 4400 | spin_lock(&block_rsv->lock); |
| 4401 | if (block_rsv->reserved < block_rsv->size) { |
| 4402 | block_rsv->reserved += buf->len; |
| 4403 | ret = 0; |
| 4404 | } |
| 4405 | spin_unlock(&block_rsv->lock); |
| 4406 | |
| 4407 | if (ret) { |
| 4408 | spin_lock(&cache->space_info->lock); |
| 4409 | cache->space_info->bytes_reserved -= buf->len; |
| 4410 | spin_unlock(&cache->space_info->lock); |
| 4411 | } |
| 4412 | goto out; |
| 4413 | } |
| 4414 | pin: |
| 4415 | if (block_rsv->durable && !cache->ro) { |
| 4416 | ret = 0; |
| 4417 | spin_lock(&cache->lock); |
| 4418 | if (!cache->ro) { |
| 4419 | cache->reserved_pinned += buf->len; |
| 4420 | ret = 1; |
| 4421 | } |
| 4422 | spin_unlock(&cache->lock); |
| 4423 | |
| 4424 | if (ret) { |
| 4425 | spin_lock(&block_rsv->lock); |
| 4426 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4427 | spin_unlock(&block_rsv->lock); |
| 4428 | } |
| 4429 | } |
| 4430 | out: |
| 4431 | btrfs_put_block_group(cache); |
| 4432 | } |
| 4433 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4434 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4435 | struct btrfs_root *root, |
| 4436 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4437 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4438 | { |
| 4439 | int ret; |
| 4440 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4441 | /* |
| 4442 | * tree log blocks never actually go into the extent allocation |
| 4443 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4444 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4445 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4446 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4447 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4448 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4449 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4450 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4451 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4452 | parent, root_objectid, (int)owner, |
| 4453 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4454 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4455 | } else { |
| 4456 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4457 | parent, root_objectid, owner, |
| 4458 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4459 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4460 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4461 | return ret; |
| 4462 | } |
| 4463 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4464 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4465 | { |
| 4466 | u64 mask = ((u64)root->stripesize - 1); |
| 4467 | u64 ret = (val + mask) & ~mask; |
| 4468 | return ret; |
| 4469 | } |
| 4470 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4471 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4472 | * when we wait for progress in the block group caching, its because |
| 4473 | * our allocation attempt failed at least once. So, we must sleep |
| 4474 | * and let some progress happen before we try again. |
| 4475 | * |
| 4476 | * This function will sleep at least once waiting for new free space to |
| 4477 | * show up, and then it will check the block group free space numbers |
| 4478 | * for our min num_bytes. Another option is to have it go ahead |
| 4479 | * and look in the rbtree for a free extent of a given size, but this |
| 4480 | * is a good start. |
| 4481 | */ |
| 4482 | static noinline int |
| 4483 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4484 | u64 num_bytes) |
| 4485 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4486 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4487 | DEFINE_WAIT(wait); |
| 4488 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4489 | caching_ctl = get_caching_control(cache); |
| 4490 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4491 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4492 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4493 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4494 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4495 | |
| 4496 | put_caching_control(caching_ctl); |
| 4497 | return 0; |
| 4498 | } |
| 4499 | |
| 4500 | static noinline int |
| 4501 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4502 | { |
| 4503 | struct btrfs_caching_control *caching_ctl; |
| 4504 | DEFINE_WAIT(wait); |
| 4505 | |
| 4506 | caching_ctl = get_caching_control(cache); |
| 4507 | if (!caching_ctl) |
| 4508 | return 0; |
| 4509 | |
| 4510 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4511 | |
| 4512 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4513 | return 0; |
| 4514 | } |
| 4515 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4516 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4517 | { |
| 4518 | int index; |
| 4519 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4520 | index = 0; |
| 4521 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4522 | index = 1; |
| 4523 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4524 | index = 2; |
| 4525 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4526 | index = 3; |
| 4527 | else |
| 4528 | index = 4; |
| 4529 | return index; |
| 4530 | } |
| 4531 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4532 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4533 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4534 | LOOP_CACHING_NOWAIT = 1, |
| 4535 | LOOP_CACHING_WAIT = 2, |
| 4536 | LOOP_ALLOC_CHUNK = 3, |
| 4537 | LOOP_NO_EMPTY_SIZE = 4, |
| 4538 | }; |
| 4539 | |
| 4540 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4541 | * walks the btree of allocated extents and find a hole of a given size. |
| 4542 | * The key ins is changed to record the hole: |
| 4543 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4544 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4545 | * ins->offset == number of blocks |
| 4546 | * Any available blocks before search_start are skipped. |
| 4547 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4548 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4549 | struct btrfs_root *orig_root, |
| 4550 | u64 num_bytes, u64 empty_size, |
| 4551 | u64 search_start, u64 search_end, |
| 4552 | u64 hint_byte, struct btrfs_key *ins, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4553 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4554 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4555 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4556 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4557 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4558 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4559 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4560 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4561 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4562 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4563 | int last_ptr_loop = 0; |
| 4564 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4565 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4566 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4567 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4568 | bool failed_alloc = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4569 | u64 ideal_cache_percent = 0; |
| 4570 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4571 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4572 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4573 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4574 | ins->objectid = 0; |
| 4575 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4576 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4577 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4578 | if (!space_info) { |
| 4579 | printk(KERN_ERR "No space info for %d\n", data); |
| 4580 | return -ENOSPC; |
| 4581 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4582 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4583 | if (orig_root->ref_cows || empty_size) |
| 4584 | allowed_chunk_alloc = 1; |
| 4585 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4586 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4587 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4588 | if (!btrfs_test_opt(root, SSD)) |
| 4589 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4590 | } |
| 4591 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4592 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 4593 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4594 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4595 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4596 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4597 | spin_lock(&last_ptr->lock); |
| 4598 | if (last_ptr->block_group) |
| 4599 | hint_byte = last_ptr->window_start; |
| 4600 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4601 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4602 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4603 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4604 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4605 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4606 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4607 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4608 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4609 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4610 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4611 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4612 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4613 | /* |
| 4614 | * we don't want to use the block group if it doesn't match our |
| 4615 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4616 | * |
| 4617 | * However if we are re-searching with an ideal block group |
| 4618 | * 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] | 4619 | */ |
| 4620 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4621 | (block_group->cached != BTRFS_CACHE_NO || |
| 4622 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4623 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4624 | if (list_empty(&block_group->list) || |
| 4625 | block_group->ro) { |
| 4626 | /* |
| 4627 | * someone is removing this block group, |
| 4628 | * we can't jump into the have_block_group |
| 4629 | * target because our list pointers are not |
| 4630 | * valid |
| 4631 | */ |
| 4632 | btrfs_put_block_group(block_group); |
| 4633 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4634 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4635 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4636 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4637 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4638 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4639 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4640 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4641 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4642 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4643 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4644 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4645 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4646 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4647 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4648 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4649 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4650 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4651 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4652 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4653 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4654 | u64 free_percent; |
| 4655 | |
| 4656 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4657 | free_percent *= 100; |
| 4658 | free_percent = div64_u64(free_percent, |
| 4659 | block_group->key.offset); |
| 4660 | free_percent = 100 - free_percent; |
| 4661 | if (free_percent > ideal_cache_percent && |
| 4662 | likely(!block_group->ro)) { |
| 4663 | ideal_cache_offset = block_group->key.objectid; |
| 4664 | ideal_cache_percent = free_percent; |
| 4665 | } |
| 4666 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4667 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4668 | * We only want to start kthread caching if we are at |
| 4669 | * the point where we will wait for caching to make |
| 4670 | * progress, or if our ideal search is over and we've |
| 4671 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4672 | */ |
| 4673 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4674 | (loop > LOOP_FIND_IDEAL && |
| 4675 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4676 | ret = cache_block_group(block_group); |
| 4677 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4678 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4679 | found_uncached_bg = true; |
| 4680 | |
| 4681 | /* |
| 4682 | * If loop is set for cached only, try the next block |
| 4683 | * group. |
| 4684 | */ |
| 4685 | if (loop == LOOP_FIND_IDEAL) |
| 4686 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4687 | } |
| 4688 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4689 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4690 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4691 | found_uncached_bg = true; |
| 4692 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4693 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4694 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4695 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4696 | /* |
| 4697 | * Ok we want to try and use the cluster allocator, so lets look |
| 4698 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 4699 | * have tried the cluster allocator plenty of times at this |
| 4700 | * point and not have found anything, so we are likely way too |
| 4701 | * fragmented for the clustering stuff to find anything, so lets |
| 4702 | * just skip it and let the allocator find whatever block it can |
| 4703 | * find |
| 4704 | */ |
| 4705 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4706 | /* |
| 4707 | * the refill lock keeps out other |
| 4708 | * people trying to start a new cluster |
| 4709 | */ |
| 4710 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4711 | if (last_ptr->block_group && |
| 4712 | (last_ptr->block_group->ro || |
| 4713 | !block_group_bits(last_ptr->block_group, data))) { |
| 4714 | offset = 0; |
| 4715 | goto refill_cluster; |
| 4716 | } |
| 4717 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4718 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 4719 | num_bytes, search_start); |
| 4720 | if (offset) { |
| 4721 | /* we have a block, we're done */ |
| 4722 | spin_unlock(&last_ptr->refill_lock); |
| 4723 | goto checks; |
| 4724 | } |
| 4725 | |
| 4726 | spin_lock(&last_ptr->lock); |
| 4727 | /* |
| 4728 | * whoops, this cluster doesn't actually point to |
| 4729 | * this block group. Get a ref on the block |
| 4730 | * group is does point to and try again |
| 4731 | */ |
| 4732 | if (!last_ptr_loop && last_ptr->block_group && |
| 4733 | last_ptr->block_group != block_group) { |
| 4734 | |
| 4735 | btrfs_put_block_group(block_group); |
| 4736 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4737 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4738 | spin_unlock(&last_ptr->lock); |
| 4739 | spin_unlock(&last_ptr->refill_lock); |
| 4740 | |
| 4741 | last_ptr_loop = 1; |
| 4742 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4743 | /* |
| 4744 | * we know this block group is properly |
| 4745 | * in the list because |
| 4746 | * btrfs_remove_block_group, drops the |
| 4747 | * cluster before it removes the block |
| 4748 | * group from the list |
| 4749 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4750 | goto have_block_group; |
| 4751 | } |
| 4752 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4753 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4754 | /* |
| 4755 | * this cluster didn't work out, free it and |
| 4756 | * start over |
| 4757 | */ |
| 4758 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 4759 | |
| 4760 | last_ptr_loop = 0; |
| 4761 | |
| 4762 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 4763 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4764 | block_group, last_ptr, |
| 4765 | offset, num_bytes, |
| 4766 | empty_cluster + empty_size); |
| 4767 | if (ret == 0) { |
| 4768 | /* |
| 4769 | * now pull our allocation out of this |
| 4770 | * cluster |
| 4771 | */ |
| 4772 | offset = btrfs_alloc_from_cluster(block_group, |
| 4773 | last_ptr, num_bytes, |
| 4774 | search_start); |
| 4775 | if (offset) { |
| 4776 | /* we found one, proceed */ |
| 4777 | spin_unlock(&last_ptr->refill_lock); |
| 4778 | goto checks; |
| 4779 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4780 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 4781 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4782 | spin_unlock(&last_ptr->refill_lock); |
| 4783 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4784 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4785 | wait_block_group_cache_progress(block_group, |
| 4786 | num_bytes + empty_cluster + empty_size); |
| 4787 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4788 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4789 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4790 | /* |
| 4791 | * at this point we either didn't find a cluster |
| 4792 | * or we weren't able to allocate a block from our |
| 4793 | * cluster. Free the cluster we've been trying |
| 4794 | * to use, and go to the next block group |
| 4795 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4796 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4797 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4798 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4799 | } |
| 4800 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4801 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 4802 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4803 | /* |
| 4804 | * If we didn't find a chunk, and we haven't failed on this |
| 4805 | * block group before, and this block group is in the middle of |
| 4806 | * caching and we are ok with waiting, then go ahead and wait |
| 4807 | * for progress to be made, and set failed_alloc to true. |
| 4808 | * |
| 4809 | * If failed_alloc is true then we've already waited on this |
| 4810 | * block group once and should move on to the next block group. |
| 4811 | */ |
| 4812 | if (!offset && !failed_alloc && !cached && |
| 4813 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4814 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4815 | num_bytes + empty_size); |
| 4816 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4817 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4818 | } else if (!offset) { |
| 4819 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4820 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4821 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4822 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4823 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4824 | if (search_start + num_bytes >= search_end) { |
| 4825 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4826 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4827 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4828 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4829 | /* move on to the next group */ |
| 4830 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4831 | block_group->key.objectid + block_group->key.offset) { |
| 4832 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4833 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4834 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4835 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4836 | ins->objectid = search_start; |
| 4837 | ins->offset = num_bytes; |
| 4838 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4839 | if (offset < search_start) |
| 4840 | btrfs_add_free_space(block_group, offset, |
| 4841 | search_start - offset); |
| 4842 | BUG_ON(offset > search_start); |
| 4843 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4844 | ret = update_reserved_bytes(block_group, num_bytes, 1, |
| 4845 | (data & BTRFS_BLOCK_GROUP_DATA)); |
| 4846 | if (ret == -EAGAIN) { |
| 4847 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 4848 | goto loop; |
| 4849 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4850 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4851 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4852 | ins->objectid = search_start; |
| 4853 | ins->offset = num_bytes; |
| 4854 | |
| 4855 | if (offset < search_start) |
| 4856 | btrfs_add_free_space(block_group, offset, |
| 4857 | search_start - offset); |
| 4858 | BUG_ON(offset > search_start); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4859 | break; |
| 4860 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4861 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4862 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4863 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4864 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4865 | } |
| 4866 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4867 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4868 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 4869 | goto search; |
| 4870 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4871 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 4872 | * for them to make caching progress. Also |
| 4873 | * determine the best possible bg to cache |
| 4874 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 4875 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4876 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 4877 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 4878 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 4879 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4880 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4881 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 4882 | (found_uncached_bg || empty_size || empty_cluster || |
| 4883 | allowed_chunk_alloc)) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4884 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4885 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4886 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4887 | loop++; |
| 4888 | if (!ideal_cache_percent && |
| 4889 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4890 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4891 | |
| 4892 | /* |
| 4893 | * 1 of the following 2 things have happened so far |
| 4894 | * |
| 4895 | * 1) We found an ideal block group for caching that |
| 4896 | * is mostly full and will cache quickly, so we might |
| 4897 | * as well wait for it. |
| 4898 | * |
| 4899 | * 2) We searched for cached only and we didn't find |
| 4900 | * anything, and we didn't start any caching kthreads |
| 4901 | * either, so chances are we will loop through and |
| 4902 | * start a couple caching kthreads, and then come back |
| 4903 | * around and just wait for them. This will be slower |
| 4904 | * because we will have 2 caching kthreads reading at |
| 4905 | * the same time when we could have just started one |
| 4906 | * and waited for it to get far enough to give us an |
| 4907 | * allocation, so go ahead and go to the wait caching |
| 4908 | * loop. |
| 4909 | */ |
| 4910 | loop = LOOP_CACHING_WAIT; |
| 4911 | search_start = ideal_cache_offset; |
| 4912 | ideal_cache_percent = 0; |
| 4913 | goto ideal_cache; |
| 4914 | } else if (loop == LOOP_FIND_IDEAL) { |
| 4915 | /* |
| 4916 | * Didn't find a uncached bg, wait on anything we find |
| 4917 | * next. |
| 4918 | */ |
| 4919 | loop = LOOP_CACHING_WAIT; |
| 4920 | goto search; |
| 4921 | } |
| 4922 | |
| 4923 | if (loop < LOOP_CACHING_WAIT) { |
| 4924 | loop++; |
| 4925 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4929 | empty_size = 0; |
| 4930 | empty_cluster = 0; |
| 4931 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4932 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4933 | if (allowed_chunk_alloc) { |
| 4934 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 4935 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4936 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4937 | done_chunk_alloc = 1; |
| 4938 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4939 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4940 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4941 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4942 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4943 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4944 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4945 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4946 | ret = -ENOSPC; |
| 4947 | } else if (!ins->objectid) { |
| 4948 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 4949 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4950 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4951 | /* we found what we needed */ |
| 4952 | if (ins->objectid) { |
| 4953 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4954 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4955 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4956 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4957 | ret = 0; |
| 4958 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4959 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4960 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4961 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4962 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4963 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 4964 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4965 | { |
| 4966 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4967 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4968 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4969 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4970 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 4971 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4972 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4973 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4974 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4975 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 4976 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 4977 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4978 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4979 | (unsigned long long)info->bytes_pinned, |
| 4980 | (unsigned long long)info->bytes_reserved, |
| 4981 | (unsigned long long)info->bytes_may_use, |
| 4982 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4983 | spin_unlock(&info->lock); |
| 4984 | |
| 4985 | if (!dump_block_groups) |
| 4986 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4987 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4988 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4989 | again: |
| 4990 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4991 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4992 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 4993 | "%llu pinned %llu reserved\n", |
| 4994 | (unsigned long long)cache->key.objectid, |
| 4995 | (unsigned long long)cache->key.offset, |
| 4996 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 4997 | (unsigned long long)cache->pinned, |
| 4998 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4999 | btrfs_dump_free_space(cache, bytes); |
| 5000 | spin_unlock(&cache->lock); |
| 5001 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5002 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5003 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5004 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5005 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5006 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5007 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5008 | struct btrfs_root *root, |
| 5009 | u64 num_bytes, u64 min_alloc_size, |
| 5010 | u64 empty_size, u64 hint_byte, |
| 5011 | u64 search_end, struct btrfs_key *ins, |
| 5012 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5013 | { |
| 5014 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5015 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5016 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5017 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5018 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5019 | /* |
| 5020 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5021 | * is not called recursively on allocations |
| 5022 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5023 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5024 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5025 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5026 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5027 | WARN_ON(num_bytes < root->sectorsize); |
| 5028 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5029 | search_start, search_end, hint_byte, |
| 5030 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5031 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5032 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5033 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5034 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5035 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5036 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5037 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5038 | goto again; |
| 5039 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5040 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5041 | struct btrfs_space_info *sinfo; |
| 5042 | |
| 5043 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5044 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5045 | "wanted %llu\n", (unsigned long long)data, |
| 5046 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5047 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5048 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5049 | |
| 5050 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5051 | } |
| 5052 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5053 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5054 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5055 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5056 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5057 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5058 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5059 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5060 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5061 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5062 | return -ENOSPC; |
| 5063 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5064 | |
| 5065 | ret = btrfs_discard_extent(root, start, len); |
| 5066 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5067 | btrfs_add_free_space(cache, start, len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5068 | update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5069 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5070 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5071 | return ret; |
| 5072 | } |
| 5073 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5074 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5075 | struct btrfs_root *root, |
| 5076 | u64 parent, u64 root_objectid, |
| 5077 | u64 flags, u64 owner, u64 offset, |
| 5078 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5079 | { |
| 5080 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5081 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5082 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5083 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5084 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5085 | struct extent_buffer *leaf; |
| 5086 | int type; |
| 5087 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5088 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5089 | if (parent > 0) |
| 5090 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5091 | else |
| 5092 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5093 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5094 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5095 | |
| 5096 | path = btrfs_alloc_path(); |
| 5097 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5098 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5099 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5100 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5101 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5102 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5103 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5104 | leaf = path->nodes[0]; |
| 5105 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5106 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5107 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5108 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5109 | btrfs_set_extent_flags(leaf, extent_item, |
| 5110 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5111 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5112 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5113 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5114 | if (parent > 0) { |
| 5115 | struct btrfs_shared_data_ref *ref; |
| 5116 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5117 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5118 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5119 | } else { |
| 5120 | struct btrfs_extent_data_ref *ref; |
| 5121 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5122 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5123 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5124 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5125 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5126 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5127 | |
| 5128 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5129 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5130 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5131 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5132 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5133 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5134 | "%llu\n", (unsigned long long)ins->objectid, |
| 5135 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5136 | BUG(); |
| 5137 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5138 | return ret; |
| 5139 | } |
| 5140 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5141 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5142 | struct btrfs_root *root, |
| 5143 | u64 parent, u64 root_objectid, |
| 5144 | u64 flags, struct btrfs_disk_key *key, |
| 5145 | int level, struct btrfs_key *ins) |
| 5146 | { |
| 5147 | int ret; |
| 5148 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5149 | struct btrfs_extent_item *extent_item; |
| 5150 | struct btrfs_tree_block_info *block_info; |
| 5151 | struct btrfs_extent_inline_ref *iref; |
| 5152 | struct btrfs_path *path; |
| 5153 | struct extent_buffer *leaf; |
| 5154 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5155 | |
| 5156 | path = btrfs_alloc_path(); |
| 5157 | BUG_ON(!path); |
| 5158 | |
| 5159 | path->leave_spinning = 1; |
| 5160 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5161 | ins, size); |
| 5162 | BUG_ON(ret); |
| 5163 | |
| 5164 | leaf = path->nodes[0]; |
| 5165 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5166 | struct btrfs_extent_item); |
| 5167 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5168 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5169 | btrfs_set_extent_flags(leaf, extent_item, |
| 5170 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5171 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5172 | |
| 5173 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5174 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5175 | |
| 5176 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5177 | if (parent > 0) { |
| 5178 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5179 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5180 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5181 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5182 | } else { |
| 5183 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5184 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5185 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5186 | } |
| 5187 | |
| 5188 | btrfs_mark_buffer_dirty(leaf); |
| 5189 | btrfs_free_path(path); |
| 5190 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5191 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5192 | if (ret) { |
| 5193 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5194 | "%llu\n", (unsigned long long)ins->objectid, |
| 5195 | (unsigned long long)ins->offset); |
| 5196 | BUG(); |
| 5197 | } |
| 5198 | return ret; |
| 5199 | } |
| 5200 | |
| 5201 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5202 | struct btrfs_root *root, |
| 5203 | u64 root_objectid, u64 owner, |
| 5204 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5205 | { |
| 5206 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5207 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5208 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5209 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5210 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5211 | 0, root_objectid, owner, offset, |
| 5212 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5213 | return ret; |
| 5214 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5215 | |
| 5216 | /* |
| 5217 | * this is used by the tree logging recovery code. It records that |
| 5218 | * an extent has been allocated and makes sure to clear the free |
| 5219 | * space cache bits as well |
| 5220 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5221 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5222 | struct btrfs_root *root, |
| 5223 | u64 root_objectid, u64 owner, u64 offset, |
| 5224 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5225 | { |
| 5226 | int ret; |
| 5227 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5228 | struct btrfs_caching_control *caching_ctl; |
| 5229 | u64 start = ins->objectid; |
| 5230 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5231 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5232 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5233 | cache_block_group(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5234 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5235 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5236 | if (!caching_ctl) { |
| 5237 | BUG_ON(!block_group_cache_done(block_group)); |
| 5238 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5239 | BUG_ON(ret); |
| 5240 | } else { |
| 5241 | mutex_lock(&caching_ctl->mutex); |
| 5242 | |
| 5243 | if (start >= caching_ctl->progress) { |
| 5244 | ret = add_excluded_extent(root, start, num_bytes); |
| 5245 | BUG_ON(ret); |
| 5246 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5247 | ret = btrfs_remove_free_space(block_group, |
| 5248 | start, num_bytes); |
| 5249 | BUG_ON(ret); |
| 5250 | } else { |
| 5251 | num_bytes = caching_ctl->progress - start; |
| 5252 | ret = btrfs_remove_free_space(block_group, |
| 5253 | start, num_bytes); |
| 5254 | BUG_ON(ret); |
| 5255 | |
| 5256 | start = caching_ctl->progress; |
| 5257 | num_bytes = ins->objectid + ins->offset - |
| 5258 | caching_ctl->progress; |
| 5259 | ret = add_excluded_extent(root, start, num_bytes); |
| 5260 | BUG_ON(ret); |
| 5261 | } |
| 5262 | |
| 5263 | mutex_unlock(&caching_ctl->mutex); |
| 5264 | put_caching_control(caching_ctl); |
| 5265 | } |
| 5266 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5267 | ret = update_reserved_bytes(block_group, ins->offset, 1, 1); |
| 5268 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5269 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5270 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5271 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5272 | return ret; |
| 5273 | } |
| 5274 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5275 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5276 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5277 | u64 bytenr, u32 blocksize, |
| 5278 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5279 | { |
| 5280 | struct extent_buffer *buf; |
| 5281 | |
| 5282 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5283 | if (!buf) |
| 5284 | return ERR_PTR(-ENOMEM); |
| 5285 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5286 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5287 | btrfs_tree_lock(buf); |
| 5288 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5289 | |
| 5290 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5291 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5292 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5293 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5294 | /* |
| 5295 | * we allow two log transactions at a time, use different |
| 5296 | * EXENT bit to differentiate dirty pages. |
| 5297 | */ |
| 5298 | if (root->log_transid % 2 == 0) |
| 5299 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5300 | buf->start + buf->len - 1, GFP_NOFS); |
| 5301 | else |
| 5302 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5303 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5304 | } else { |
| 5305 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5306 | buf->start + buf->len - 1, GFP_NOFS); |
| 5307 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5308 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5309 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5310 | return buf; |
| 5311 | } |
| 5312 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5313 | static struct btrfs_block_rsv * |
| 5314 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5315 | struct btrfs_root *root, u32 blocksize) |
| 5316 | { |
| 5317 | struct btrfs_block_rsv *block_rsv; |
| 5318 | int ret; |
| 5319 | |
| 5320 | block_rsv = get_block_rsv(trans, root); |
| 5321 | |
| 5322 | if (block_rsv->size == 0) { |
| 5323 | ret = reserve_metadata_bytes(block_rsv, blocksize); |
| 5324 | if (ret) |
| 5325 | return ERR_PTR(ret); |
| 5326 | return block_rsv; |
| 5327 | } |
| 5328 | |
| 5329 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5330 | if (!ret) |
| 5331 | return block_rsv; |
| 5332 | |
| 5333 | WARN_ON(1); |
| 5334 | printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n", |
| 5335 | block_rsv->size, block_rsv->reserved, |
| 5336 | block_rsv->freed[0], block_rsv->freed[1]); |
| 5337 | |
| 5338 | return ERR_PTR(-ENOSPC); |
| 5339 | } |
| 5340 | |
| 5341 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5342 | { |
| 5343 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5344 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5345 | } |
| 5346 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5347 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5348 | * finds a free extent and does all the dirty work required for allocation |
| 5349 | * returns the key for the extent through ins, and a tree buffer for |
| 5350 | * the first block of the extent through buf. |
| 5351 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5352 | * returns the tree buffer or NULL. |
| 5353 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5354 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5355 | struct btrfs_root *root, u32 blocksize, |
| 5356 | u64 parent, u64 root_objectid, |
| 5357 | struct btrfs_disk_key *key, int level, |
| 5358 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5359 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5360 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5361 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5362 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5363 | u64 flags = 0; |
| 5364 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5365 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5366 | |
| 5367 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5368 | if (IS_ERR(block_rsv)) |
| 5369 | return ERR_CAST(block_rsv); |
| 5370 | |
| 5371 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5372 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5373 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5374 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5375 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5376 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5377 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5378 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5379 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5380 | BUG_ON(IS_ERR(buf)); |
| 5381 | |
| 5382 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5383 | if (parent == 0) |
| 5384 | parent = ins.objectid; |
| 5385 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5386 | } else |
| 5387 | BUG_ON(parent > 0); |
| 5388 | |
| 5389 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5390 | struct btrfs_delayed_extent_op *extent_op; |
| 5391 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5392 | BUG_ON(!extent_op); |
| 5393 | if (key) |
| 5394 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5395 | else |
| 5396 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5397 | extent_op->flags_to_set = flags; |
| 5398 | extent_op->update_key = 1; |
| 5399 | extent_op->update_flags = 1; |
| 5400 | extent_op->is_data = 0; |
| 5401 | |
| 5402 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5403 | ins.offset, parent, root_objectid, |
| 5404 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5405 | extent_op); |
| 5406 | BUG_ON(ret); |
| 5407 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5408 | return buf; |
| 5409 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5410 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5411 | struct walk_control { |
| 5412 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5413 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5414 | struct btrfs_key update_progress; |
| 5415 | int stage; |
| 5416 | int level; |
| 5417 | int shared_level; |
| 5418 | int update_ref; |
| 5419 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5420 | int reada_slot; |
| 5421 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5422 | }; |
| 5423 | |
| 5424 | #define DROP_REFERENCE 1 |
| 5425 | #define UPDATE_BACKREF 2 |
| 5426 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5427 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5428 | struct btrfs_root *root, |
| 5429 | struct walk_control *wc, |
| 5430 | struct btrfs_path *path) |
| 5431 | { |
| 5432 | u64 bytenr; |
| 5433 | u64 generation; |
| 5434 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5435 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5436 | u64 last = 0; |
| 5437 | u32 nritems; |
| 5438 | u32 blocksize; |
| 5439 | struct btrfs_key key; |
| 5440 | struct extent_buffer *eb; |
| 5441 | int ret; |
| 5442 | int slot; |
| 5443 | int nread = 0; |
| 5444 | |
| 5445 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5446 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5447 | wc->reada_count = max(wc->reada_count, 2); |
| 5448 | } else { |
| 5449 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5450 | wc->reada_count = min_t(int, wc->reada_count, |
| 5451 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5452 | } |
| 5453 | |
| 5454 | eb = path->nodes[wc->level]; |
| 5455 | nritems = btrfs_header_nritems(eb); |
| 5456 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5457 | |
| 5458 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5459 | if (nread >= wc->reada_count) |
| 5460 | break; |
| 5461 | |
| 5462 | cond_resched(); |
| 5463 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5464 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5465 | |
| 5466 | if (slot == path->slots[wc->level]) |
| 5467 | goto reada; |
| 5468 | |
| 5469 | if (wc->stage == UPDATE_BACKREF && |
| 5470 | generation <= root->root_key.offset) |
| 5471 | continue; |
| 5472 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5473 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5474 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5475 | &refs, &flags); |
| 5476 | BUG_ON(ret); |
| 5477 | BUG_ON(refs == 0); |
| 5478 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5479 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5480 | if (refs == 1) |
| 5481 | goto reada; |
| 5482 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5483 | if (wc->level == 1 && |
| 5484 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5485 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5486 | if (!wc->update_ref || |
| 5487 | generation <= root->root_key.offset) |
| 5488 | continue; |
| 5489 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5490 | ret = btrfs_comp_cpu_keys(&key, |
| 5491 | &wc->update_progress); |
| 5492 | if (ret < 0) |
| 5493 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5494 | } else { |
| 5495 | if (wc->level == 1 && |
| 5496 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5497 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5498 | } |
| 5499 | reada: |
| 5500 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5501 | generation); |
| 5502 | if (ret) |
| 5503 | break; |
| 5504 | last = bytenr + blocksize; |
| 5505 | nread++; |
| 5506 | } |
| 5507 | wc->reada_slot = slot; |
| 5508 | } |
| 5509 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5510 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5511 | * hepler to process tree block while walking down the tree. |
| 5512 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5513 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5514 | * back refs for pointers in the block. |
| 5515 | * |
| 5516 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5517 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5518 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5519 | struct btrfs_root *root, |
| 5520 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5521 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5522 | { |
| 5523 | int level = wc->level; |
| 5524 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5525 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5526 | int ret; |
| 5527 | |
| 5528 | if (wc->stage == UPDATE_BACKREF && |
| 5529 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5530 | return 1; |
| 5531 | |
| 5532 | /* |
| 5533 | * when reference count of tree block is 1, it won't increase |
| 5534 | * again. once full backref flag is set, we never clear it. |
| 5535 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5536 | if (lookup_info && |
| 5537 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5538 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5539 | BUG_ON(!path->locks[level]); |
| 5540 | ret = btrfs_lookup_extent_info(trans, root, |
| 5541 | eb->start, eb->len, |
| 5542 | &wc->refs[level], |
| 5543 | &wc->flags[level]); |
| 5544 | BUG_ON(ret); |
| 5545 | BUG_ON(wc->refs[level] == 0); |
| 5546 | } |
| 5547 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5548 | if (wc->stage == DROP_REFERENCE) { |
| 5549 | if (wc->refs[level] > 1) |
| 5550 | return 1; |
| 5551 | |
| 5552 | if (path->locks[level] && !wc->keep_locks) { |
| 5553 | btrfs_tree_unlock(eb); |
| 5554 | path->locks[level] = 0; |
| 5555 | } |
| 5556 | return 0; |
| 5557 | } |
| 5558 | |
| 5559 | /* wc->stage == UPDATE_BACKREF */ |
| 5560 | if (!(wc->flags[level] & flag)) { |
| 5561 | BUG_ON(!path->locks[level]); |
| 5562 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5563 | BUG_ON(ret); |
| 5564 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5565 | BUG_ON(ret); |
| 5566 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5567 | eb->len, flag, 0); |
| 5568 | BUG_ON(ret); |
| 5569 | wc->flags[level] |= flag; |
| 5570 | } |
| 5571 | |
| 5572 | /* |
| 5573 | * the block is shared by multiple trees, so it's not good to |
| 5574 | * keep the tree lock |
| 5575 | */ |
| 5576 | if (path->locks[level] && level > 0) { |
| 5577 | btrfs_tree_unlock(eb); |
| 5578 | path->locks[level] = 0; |
| 5579 | } |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
| 5583 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5584 | * hepler to process tree block pointer. |
| 5585 | * |
| 5586 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5587 | * reference count of the block pointed to. if the block |
| 5588 | * is shared and we need update back refs for the subtree |
| 5589 | * rooted at the block, this function changes wc->stage to |
| 5590 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5591 | * need to update back, this function drops the reference |
| 5592 | * to the block. |
| 5593 | * |
| 5594 | * NOTE: return value 1 means we should stop walking down. |
| 5595 | */ |
| 5596 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5597 | struct btrfs_root *root, |
| 5598 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5599 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5600 | { |
| 5601 | u64 bytenr; |
| 5602 | u64 generation; |
| 5603 | u64 parent; |
| 5604 | u32 blocksize; |
| 5605 | struct btrfs_key key; |
| 5606 | struct extent_buffer *next; |
| 5607 | int level = wc->level; |
| 5608 | int reada = 0; |
| 5609 | int ret = 0; |
| 5610 | |
| 5611 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5612 | path->slots[level]); |
| 5613 | /* |
| 5614 | * if the lower level block was created before the snapshot |
| 5615 | * was created, we know there is no need to update back refs |
| 5616 | * for the subtree |
| 5617 | */ |
| 5618 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5619 | generation <= root->root_key.offset) { |
| 5620 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5621 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5622 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5623 | |
| 5624 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5625 | blocksize = btrfs_level_size(root, level - 1); |
| 5626 | |
| 5627 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5628 | if (!next) { |
| 5629 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5630 | if (!next) |
| 5631 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5632 | reada = 1; |
| 5633 | } |
| 5634 | btrfs_tree_lock(next); |
| 5635 | btrfs_set_lock_blocking(next); |
| 5636 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5637 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5638 | &wc->refs[level - 1], |
| 5639 | &wc->flags[level - 1]); |
| 5640 | BUG_ON(ret); |
| 5641 | BUG_ON(wc->refs[level - 1] == 0); |
| 5642 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5643 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5644 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5645 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5646 | if (level == 1 && |
| 5647 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5648 | goto skip; |
| 5649 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5650 | if (!wc->update_ref || |
| 5651 | generation <= root->root_key.offset) |
| 5652 | goto skip; |
| 5653 | |
| 5654 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5655 | path->slots[level]); |
| 5656 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5657 | if (ret < 0) |
| 5658 | goto skip; |
| 5659 | |
| 5660 | wc->stage = UPDATE_BACKREF; |
| 5661 | wc->shared_level = level - 1; |
| 5662 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5663 | } else { |
| 5664 | if (level == 1 && |
| 5665 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5666 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5670 | btrfs_tree_unlock(next); |
| 5671 | free_extent_buffer(next); |
| 5672 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5673 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | if (!next) { |
| 5677 | if (reada && level == 1) |
| 5678 | reada_walk_down(trans, root, wc, path); |
| 5679 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5680 | btrfs_tree_lock(next); |
| 5681 | btrfs_set_lock_blocking(next); |
| 5682 | } |
| 5683 | |
| 5684 | level--; |
| 5685 | BUG_ON(level != btrfs_header_level(next)); |
| 5686 | path->nodes[level] = next; |
| 5687 | path->slots[level] = 0; |
| 5688 | path->locks[level] = 1; |
| 5689 | wc->level = level; |
| 5690 | if (wc->level == 1) |
| 5691 | wc->reada_slot = 0; |
| 5692 | return 0; |
| 5693 | skip: |
| 5694 | wc->refs[level - 1] = 0; |
| 5695 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5696 | if (wc->stage == DROP_REFERENCE) { |
| 5697 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5698 | parent = path->nodes[level]->start; |
| 5699 | } else { |
| 5700 | BUG_ON(root->root_key.objectid != |
| 5701 | btrfs_header_owner(path->nodes[level])); |
| 5702 | parent = 0; |
| 5703 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5704 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5705 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 5706 | root->root_key.objectid, level - 1, 0); |
| 5707 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5708 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5709 | btrfs_tree_unlock(next); |
| 5710 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5711 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5712 | return 1; |
| 5713 | } |
| 5714 | |
| 5715 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5716 | * hepler to process tree block while walking up the tree. |
| 5717 | * |
| 5718 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5719 | * reference count on the block. |
| 5720 | * |
| 5721 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5722 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5723 | * to UPDATE_BACKREF previously while processing the block. |
| 5724 | * |
| 5725 | * NOTE: return value 1 means we should stop walking up. |
| 5726 | */ |
| 5727 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5728 | struct btrfs_root *root, |
| 5729 | struct btrfs_path *path, |
| 5730 | struct walk_control *wc) |
| 5731 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5732 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5733 | int level = wc->level; |
| 5734 | struct extent_buffer *eb = path->nodes[level]; |
| 5735 | u64 parent = 0; |
| 5736 | |
| 5737 | if (wc->stage == UPDATE_BACKREF) { |
| 5738 | BUG_ON(wc->shared_level < level); |
| 5739 | if (level < wc->shared_level) |
| 5740 | goto out; |
| 5741 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5742 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5743 | if (ret > 0) |
| 5744 | wc->update_ref = 0; |
| 5745 | |
| 5746 | wc->stage = DROP_REFERENCE; |
| 5747 | wc->shared_level = -1; |
| 5748 | path->slots[level] = 0; |
| 5749 | |
| 5750 | /* |
| 5751 | * check reference count again if the block isn't locked. |
| 5752 | * we should start walking down the tree again if reference |
| 5753 | * count is one. |
| 5754 | */ |
| 5755 | if (!path->locks[level]) { |
| 5756 | BUG_ON(level == 0); |
| 5757 | btrfs_tree_lock(eb); |
| 5758 | btrfs_set_lock_blocking(eb); |
| 5759 | path->locks[level] = 1; |
| 5760 | |
| 5761 | ret = btrfs_lookup_extent_info(trans, root, |
| 5762 | eb->start, eb->len, |
| 5763 | &wc->refs[level], |
| 5764 | &wc->flags[level]); |
| 5765 | BUG_ON(ret); |
| 5766 | BUG_ON(wc->refs[level] == 0); |
| 5767 | if (wc->refs[level] == 1) { |
| 5768 | btrfs_tree_unlock(eb); |
| 5769 | path->locks[level] = 0; |
| 5770 | return 1; |
| 5771 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | |
| 5775 | /* wc->stage == DROP_REFERENCE */ |
| 5776 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5777 | |
| 5778 | if (wc->refs[level] == 1) { |
| 5779 | if (level == 0) { |
| 5780 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5781 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 5782 | else |
| 5783 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5784 | BUG_ON(ret); |
| 5785 | } |
| 5786 | /* make block locked assertion in clean_tree_block happy */ |
| 5787 | if (!path->locks[level] && |
| 5788 | btrfs_header_generation(eb) == trans->transid) { |
| 5789 | btrfs_tree_lock(eb); |
| 5790 | btrfs_set_lock_blocking(eb); |
| 5791 | path->locks[level] = 1; |
| 5792 | } |
| 5793 | clean_tree_block(trans, root, eb); |
| 5794 | } |
| 5795 | |
| 5796 | if (eb == root->node) { |
| 5797 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5798 | parent = eb->start; |
| 5799 | else |
| 5800 | BUG_ON(root->root_key.objectid != |
| 5801 | btrfs_header_owner(eb)); |
| 5802 | } else { |
| 5803 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5804 | parent = path->nodes[level + 1]->start; |
| 5805 | else |
| 5806 | BUG_ON(root->root_key.objectid != |
| 5807 | btrfs_header_owner(path->nodes[level + 1])); |
| 5808 | } |
| 5809 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5810 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5811 | out: |
| 5812 | wc->refs[level] = 0; |
| 5813 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5814 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5815 | } |
| 5816 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5817 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5818 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5819 | struct btrfs_path *path, |
| 5820 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5821 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5822 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5823 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5824 | int ret; |
| 5825 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5826 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5827 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5828 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5829 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5830 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5831 | if (level == 0) |
| 5832 | break; |
| 5833 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5834 | if (path->slots[level] >= |
| 5835 | btrfs_header_nritems(path->nodes[level])) |
| 5836 | break; |
| 5837 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5838 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5839 | if (ret > 0) { |
| 5840 | path->slots[level]++; |
| 5841 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5842 | } else if (ret < 0) |
| 5843 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5844 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5845 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5846 | return 0; |
| 5847 | } |
| 5848 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5849 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5850 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5851 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5852 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5853 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5854 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5855 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5856 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5857 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5858 | while (level < max_level && path->nodes[level]) { |
| 5859 | wc->level = level; |
| 5860 | if (path->slots[level] + 1 < |
| 5861 | btrfs_header_nritems(path->nodes[level])) { |
| 5862 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5863 | return 0; |
| 5864 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5865 | ret = walk_up_proc(trans, root, path, wc); |
| 5866 | if (ret > 0) |
| 5867 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5868 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5869 | if (path->locks[level]) { |
| 5870 | btrfs_tree_unlock(path->nodes[level]); |
| 5871 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5872 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5873 | free_extent_buffer(path->nodes[level]); |
| 5874 | path->nodes[level] = NULL; |
| 5875 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5876 | } |
| 5877 | } |
| 5878 | return 1; |
| 5879 | } |
| 5880 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5881 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5882 | * drop a subvolume tree. |
| 5883 | * |
| 5884 | * this function traverses the tree freeing any blocks that only |
| 5885 | * referenced by the tree. |
| 5886 | * |
| 5887 | * when a shared tree block is found. this function decreases its |
| 5888 | * reference count by one. if update_ref is true, this function |
| 5889 | * also make sure backrefs for the shared block and all lower level |
| 5890 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5891 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5892 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 5893 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5894 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5895 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5896 | struct btrfs_trans_handle *trans; |
| 5897 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5898 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5899 | struct walk_control *wc; |
| 5900 | struct btrfs_key key; |
| 5901 | int err = 0; |
| 5902 | int ret; |
| 5903 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5904 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5905 | path = btrfs_alloc_path(); |
| 5906 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5907 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5908 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5909 | BUG_ON(!wc); |
| 5910 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5911 | trans = btrfs_start_transaction(tree_root, 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5912 | if (block_rsv) |
| 5913 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5914 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5915 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5916 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5917 | path->nodes[level] = btrfs_lock_root_node(root); |
| 5918 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5919 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5920 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5921 | memset(&wc->update_progress, 0, |
| 5922 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5923 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5924 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5925 | memcpy(&wc->update_progress, &key, |
| 5926 | sizeof(wc->update_progress)); |
| 5927 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5928 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5929 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5930 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5931 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5932 | path->lowest_level = 0; |
| 5933 | if (ret < 0) { |
| 5934 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5935 | goto out; |
| 5936 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5937 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5938 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5939 | /* |
| 5940 | * unlock our path, this is safe because only this |
| 5941 | * function is allowed to delete this snapshot |
| 5942 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5943 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5944 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5945 | level = btrfs_header_level(root->node); |
| 5946 | while (1) { |
| 5947 | btrfs_tree_lock(path->nodes[level]); |
| 5948 | btrfs_set_lock_blocking(path->nodes[level]); |
| 5949 | |
| 5950 | ret = btrfs_lookup_extent_info(trans, root, |
| 5951 | path->nodes[level]->start, |
| 5952 | path->nodes[level]->len, |
| 5953 | &wc->refs[level], |
| 5954 | &wc->flags[level]); |
| 5955 | BUG_ON(ret); |
| 5956 | BUG_ON(wc->refs[level] == 0); |
| 5957 | |
| 5958 | if (level == root_item->drop_level) |
| 5959 | break; |
| 5960 | |
| 5961 | btrfs_tree_unlock(path->nodes[level]); |
| 5962 | WARN_ON(wc->refs[level] != 1); |
| 5963 | level--; |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | wc->level = level; |
| 5968 | wc->shared_level = -1; |
| 5969 | wc->stage = DROP_REFERENCE; |
| 5970 | wc->update_ref = update_ref; |
| 5971 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5972 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5973 | |
| 5974 | while (1) { |
| 5975 | ret = walk_down_tree(trans, root, path, wc); |
| 5976 | if (ret < 0) { |
| 5977 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5978 | break; |
| 5979 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5980 | |
| 5981 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5982 | if (ret < 0) { |
| 5983 | err = ret; |
| 5984 | break; |
| 5985 | } |
| 5986 | |
| 5987 | if (ret > 0) { |
| 5988 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5989 | break; |
| 5990 | } |
| 5991 | |
| 5992 | if (wc->stage == DROP_REFERENCE) { |
| 5993 | level = wc->level; |
| 5994 | btrfs_node_key(path->nodes[level], |
| 5995 | &root_item->drop_progress, |
| 5996 | path->slots[level]); |
| 5997 | root_item->drop_level = level; |
| 5998 | } |
| 5999 | |
| 6000 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6001 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6002 | ret = btrfs_update_root(trans, tree_root, |
| 6003 | &root->root_key, |
| 6004 | root_item); |
| 6005 | BUG_ON(ret); |
| 6006 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6007 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6008 | trans = btrfs_start_transaction(tree_root, 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6009 | if (block_rsv) |
| 6010 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6011 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6012 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6013 | btrfs_release_path(root, path); |
| 6014 | BUG_ON(err); |
| 6015 | |
| 6016 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6017 | BUG_ON(ret); |
| 6018 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6019 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6020 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6021 | NULL, NULL); |
| 6022 | BUG_ON(ret < 0); |
| 6023 | if (ret > 0) { |
| 6024 | ret = btrfs_del_orphan_item(trans, tree_root, |
| 6025 | root->root_key.objectid); |
| 6026 | BUG_ON(ret); |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | if (root->in_radix) { |
| 6031 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6032 | } else { |
| 6033 | free_extent_buffer(root->node); |
| 6034 | free_extent_buffer(root->commit_root); |
| 6035 | kfree(root); |
| 6036 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6037 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6038 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6039 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6040 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6041 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6042 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6043 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6044 | /* |
| 6045 | * drop subtree rooted at tree block 'node'. |
| 6046 | * |
| 6047 | * NOTE: this function will unlock and release tree block 'node' |
| 6048 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6049 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6050 | struct btrfs_root *root, |
| 6051 | struct extent_buffer *node, |
| 6052 | struct extent_buffer *parent) |
| 6053 | { |
| 6054 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6055 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6056 | int level; |
| 6057 | int parent_level; |
| 6058 | int ret = 0; |
| 6059 | int wret; |
| 6060 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6061 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6062 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6063 | path = btrfs_alloc_path(); |
| 6064 | BUG_ON(!path); |
| 6065 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6066 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6067 | BUG_ON(!wc); |
| 6068 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6069 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6070 | parent_level = btrfs_header_level(parent); |
| 6071 | extent_buffer_get(parent); |
| 6072 | path->nodes[parent_level] = parent; |
| 6073 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6074 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6075 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6076 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6077 | path->nodes[level] = node; |
| 6078 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6079 | path->locks[level] = 1; |
| 6080 | |
| 6081 | wc->refs[parent_level] = 1; |
| 6082 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6083 | wc->level = level; |
| 6084 | wc->shared_level = -1; |
| 6085 | wc->stage = DROP_REFERENCE; |
| 6086 | wc->update_ref = 0; |
| 6087 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6088 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6089 | |
| 6090 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6091 | wret = walk_down_tree(trans, root, path, wc); |
| 6092 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6093 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6094 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6095 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6096 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6097 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6098 | if (wret < 0) |
| 6099 | ret = wret; |
| 6100 | if (wret != 0) |
| 6101 | break; |
| 6102 | } |
| 6103 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6104 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6105 | btrfs_free_path(path); |
| 6106 | return ret; |
| 6107 | } |
| 6108 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6109 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6110 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 6111 | unsigned long nr) |
| 6112 | { |
| 6113 | return min(last, start + nr - 1); |
| 6114 | } |
| 6115 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6116 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6117 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6118 | { |
| 6119 | u64 page_start; |
| 6120 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6121 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6122 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6123 | unsigned long i; |
| 6124 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6125 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6126 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6127 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6128 | unsigned int total_read = 0; |
| 6129 | unsigned int total_dirty = 0; |
| 6130 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6131 | |
| 6132 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6133 | |
| 6134 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6135 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6136 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 6137 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6138 | /* make sure the dirty trick played by the caller work */ |
| 6139 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6140 | first_index, last_index); |
| 6141 | if (ret) |
| 6142 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6143 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6144 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6145 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6146 | for (i = first_index ; i <= last_index; i++) { |
| 6147 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6148 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6149 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6150 | } |
| 6151 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6152 | again: |
| 6153 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6154 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6155 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6156 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6157 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6158 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6159 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6160 | if (!PageUptodate(page)) { |
| 6161 | btrfs_readpage(NULL, page); |
| 6162 | lock_page(page); |
| 6163 | if (!PageUptodate(page)) { |
| 6164 | unlock_page(page); |
| 6165 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6166 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6167 | goto out_unlock; |
| 6168 | } |
| 6169 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6170 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6171 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6172 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 6173 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6174 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6175 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6176 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6177 | if (ordered) { |
| 6178 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 6179 | unlock_page(page); |
| 6180 | page_cache_release(page); |
| 6181 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6182 | btrfs_put_ordered_extent(ordered); |
| 6183 | goto again; |
| 6184 | } |
| 6185 | set_page_extent_mapped(page); |
| 6186 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6187 | if (i == first_index) |
| 6188 | set_extent_bits(io_tree, page_start, page_end, |
| 6189 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6190 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6191 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6192 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6193 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6194 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6195 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6196 | unlock_page(page); |
| 6197 | page_cache_release(page); |
| 6198 | } |
| 6199 | |
| 6200 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6201 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6202 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6203 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6204 | return ret; |
| 6205 | } |
| 6206 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6207 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6208 | struct btrfs_key *extent_key, |
| 6209 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6210 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6211 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6212 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 6213 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6214 | u64 start = extent_key->objectid - offset; |
| 6215 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6216 | |
| 6217 | em = alloc_extent_map(GFP_NOFS); |
| 6218 | BUG_ON(!em || IS_ERR(em)); |
| 6219 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6220 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6221 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6222 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6223 | em->block_start = extent_key->objectid; |
| 6224 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 6225 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6226 | |
| 6227 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6228 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6229 | while (1) { |
| 6230 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6231 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6232 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6233 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6234 | if (ret != -EEXIST) { |
| 6235 | free_extent_map(em); |
| 6236 | break; |
| 6237 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6238 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6239 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6240 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6241 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6242 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6243 | } |
| 6244 | |
| 6245 | struct btrfs_ref_path { |
| 6246 | u64 extent_start; |
| 6247 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 6248 | u64 root_objectid; |
| 6249 | u64 root_generation; |
| 6250 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6251 | u32 num_refs; |
| 6252 | int lowest_level; |
| 6253 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6254 | int shared_level; |
| 6255 | |
| 6256 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 6257 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6258 | }; |
| 6259 | |
| 6260 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6261 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6262 | u64 disk_bytenr; |
| 6263 | u64 disk_num_bytes; |
| 6264 | u64 offset; |
| 6265 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6266 | u8 compression; |
| 6267 | u8 encryption; |
| 6268 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6269 | }; |
| 6270 | |
| 6271 | static int is_cowonly_root(u64 root_objectid) |
| 6272 | { |
| 6273 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 6274 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 6275 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 6276 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 6277 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6278 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6279 | return 1; |
| 6280 | return 0; |
| 6281 | } |
| 6282 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6283 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6284 | struct btrfs_root *extent_root, |
| 6285 | struct btrfs_ref_path *ref_path, |
| 6286 | int first_time) |
| 6287 | { |
| 6288 | struct extent_buffer *leaf; |
| 6289 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6290 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6291 | struct btrfs_key key; |
| 6292 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6293 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6294 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6295 | int level; |
| 6296 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6297 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6298 | path = btrfs_alloc_path(); |
| 6299 | if (!path) |
| 6300 | return -ENOMEM; |
| 6301 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6302 | if (first_time) { |
| 6303 | ref_path->lowest_level = -1; |
| 6304 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6305 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6306 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6307 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6308 | walk_down: |
| 6309 | level = ref_path->current_level - 1; |
| 6310 | while (level >= -1) { |
| 6311 | u64 parent; |
| 6312 | if (level < ref_path->lowest_level) |
| 6313 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6314 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6315 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6316 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6317 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6318 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6319 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6320 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6321 | parent = ref_path->nodes[level + 1]; |
| 6322 | ref_path->nodes[level + 1] = 0; |
| 6323 | ref_path->current_level = level; |
| 6324 | BUG_ON(parent == 0); |
| 6325 | |
| 6326 | key.objectid = bytenr; |
| 6327 | key.offset = parent + 1; |
| 6328 | key.type = BTRFS_EXTENT_REF_KEY; |
| 6329 | |
| 6330 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6331 | if (ret < 0) |
| 6332 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6333 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6334 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6335 | leaf = path->nodes[0]; |
| 6336 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6337 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6338 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6339 | if (ret < 0) |
| 6340 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6341 | if (ret > 0) |
| 6342 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6343 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6344 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6345 | |
| 6346 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6347 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6348 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 6349 | if (level < ref_path->shared_level) |
| 6350 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6351 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6352 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6353 | next: |
| 6354 | level--; |
| 6355 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6356 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6357 | } |
| 6358 | /* reached lowest level */ |
| 6359 | ret = 1; |
| 6360 | goto out; |
| 6361 | walk_up: |
| 6362 | level = ref_path->current_level; |
| 6363 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 6364 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6365 | |
| 6366 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6367 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6368 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6369 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6370 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6371 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6372 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6373 | key.objectid = bytenr; |
| 6374 | key.offset = 0; |
| 6375 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6376 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6377 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 6378 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6379 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6380 | |
| 6381 | leaf = path->nodes[0]; |
| 6382 | nritems = btrfs_header_nritems(leaf); |
| 6383 | if (path->slots[0] >= nritems) { |
| 6384 | ret = btrfs_next_leaf(extent_root, path); |
| 6385 | if (ret < 0) |
| 6386 | goto out; |
| 6387 | if (ret > 0) { |
| 6388 | /* the extent was freed by someone */ |
| 6389 | if (ref_path->lowest_level == level) |
| 6390 | goto out; |
| 6391 | btrfs_release_path(extent_root, path); |
| 6392 | goto walk_down; |
| 6393 | } |
| 6394 | leaf = path->nodes[0]; |
| 6395 | } |
| 6396 | |
| 6397 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6398 | if (found_key.objectid != bytenr || |
| 6399 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 6400 | /* the extent was freed by someone */ |
| 6401 | if (ref_path->lowest_level == level) { |
| 6402 | ret = 1; |
| 6403 | goto out; |
| 6404 | } |
| 6405 | btrfs_release_path(extent_root, path); |
| 6406 | goto walk_down; |
| 6407 | } |
| 6408 | found: |
| 6409 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 6410 | struct btrfs_extent_ref); |
| 6411 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 6412 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 6413 | if (first_time) { |
| 6414 | level = (int)ref_objectid; |
| 6415 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 6416 | ref_path->lowest_level = level; |
| 6417 | ref_path->current_level = level; |
| 6418 | ref_path->nodes[level] = bytenr; |
| 6419 | } else { |
| 6420 | WARN_ON(ref_objectid != level); |
| 6421 | } |
| 6422 | } else { |
| 6423 | WARN_ON(level != -1); |
| 6424 | } |
| 6425 | first_time = 0; |
| 6426 | |
| 6427 | if (ref_path->lowest_level == level) { |
| 6428 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6429 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 6430 | } |
| 6431 | |
| 6432 | /* |
| 6433 | * the block is tree root or the block isn't in reference |
| 6434 | * counted tree. |
| 6435 | */ |
| 6436 | if (found_key.objectid == found_key.offset || |
| 6437 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 6438 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6439 | ref_path->root_generation = |
| 6440 | btrfs_ref_generation(leaf, ref); |
| 6441 | if (level < 0) { |
| 6442 | /* special reference from the tree log */ |
| 6443 | ref_path->nodes[0] = found_key.offset; |
| 6444 | ref_path->current_level = 0; |
| 6445 | } |
| 6446 | ret = 0; |
| 6447 | goto out; |
| 6448 | } |
| 6449 | |
| 6450 | level++; |
| 6451 | BUG_ON(ref_path->nodes[level] != 0); |
| 6452 | ref_path->nodes[level] = found_key.offset; |
| 6453 | ref_path->current_level = level; |
| 6454 | |
| 6455 | /* |
| 6456 | * the reference was created in the running transaction, |
| 6457 | * no need to continue walking up. |
| 6458 | */ |
| 6459 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6460 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6461 | ref_path->root_generation = |
| 6462 | btrfs_ref_generation(leaf, ref); |
| 6463 | ret = 0; |
| 6464 | goto out; |
| 6465 | } |
| 6466 | |
| 6467 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6468 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6469 | } |
| 6470 | /* reached max tree level, but no tree root found. */ |
| 6471 | BUG(); |
| 6472 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6473 | btrfs_free_path(path); |
| 6474 | return ret; |
| 6475 | } |
| 6476 | |
| 6477 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6478 | struct btrfs_root *extent_root, |
| 6479 | struct btrfs_ref_path *ref_path, |
| 6480 | u64 extent_start) |
| 6481 | { |
| 6482 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6483 | ref_path->extent_start = extent_start; |
| 6484 | |
| 6485 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6486 | } |
| 6487 | |
| 6488 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6489 | struct btrfs_root *extent_root, |
| 6490 | struct btrfs_ref_path *ref_path) |
| 6491 | { |
| 6492 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6493 | } |
| 6494 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6495 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6496 | struct btrfs_key *extent_key, |
| 6497 | u64 offset, int no_fragment, |
| 6498 | struct disk_extent **extents, |
| 6499 | int *nr_extents) |
| 6500 | { |
| 6501 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6502 | struct btrfs_path *path; |
| 6503 | struct btrfs_file_extent_item *fi; |
| 6504 | struct extent_buffer *leaf; |
| 6505 | struct disk_extent *exts = *extents; |
| 6506 | struct btrfs_key found_key; |
| 6507 | u64 cur_pos; |
| 6508 | u64 last_byte; |
| 6509 | u32 nritems; |
| 6510 | int nr = 0; |
| 6511 | int max = *nr_extents; |
| 6512 | int ret; |
| 6513 | |
| 6514 | WARN_ON(!no_fragment && *extents); |
| 6515 | if (!exts) { |
| 6516 | max = 1; |
| 6517 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6518 | if (!exts) |
| 6519 | return -ENOMEM; |
| 6520 | } |
| 6521 | |
| 6522 | path = btrfs_alloc_path(); |
| 6523 | BUG_ON(!path); |
| 6524 | |
| 6525 | cur_pos = extent_key->objectid - offset; |
| 6526 | last_byte = extent_key->objectid + extent_key->offset; |
| 6527 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6528 | cur_pos, 0); |
| 6529 | if (ret < 0) |
| 6530 | goto out; |
| 6531 | if (ret > 0) { |
| 6532 | ret = -ENOENT; |
| 6533 | goto out; |
| 6534 | } |
| 6535 | |
| 6536 | while (1) { |
| 6537 | leaf = path->nodes[0]; |
| 6538 | nritems = btrfs_header_nritems(leaf); |
| 6539 | if (path->slots[0] >= nritems) { |
| 6540 | ret = btrfs_next_leaf(root, path); |
| 6541 | if (ret < 0) |
| 6542 | goto out; |
| 6543 | if (ret > 0) |
| 6544 | break; |
| 6545 | leaf = path->nodes[0]; |
| 6546 | } |
| 6547 | |
| 6548 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6549 | if (found_key.offset != cur_pos || |
| 6550 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6551 | found_key.objectid != reloc_inode->i_ino) |
| 6552 | break; |
| 6553 | |
| 6554 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6555 | struct btrfs_file_extent_item); |
| 6556 | if (btrfs_file_extent_type(leaf, fi) != |
| 6557 | BTRFS_FILE_EXTENT_REG || |
| 6558 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6559 | break; |
| 6560 | |
| 6561 | if (nr == max) { |
| 6562 | struct disk_extent *old = exts; |
| 6563 | max *= 2; |
| 6564 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6565 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6566 | if (old != *extents) |
| 6567 | kfree(old); |
| 6568 | } |
| 6569 | |
| 6570 | exts[nr].disk_bytenr = |
| 6571 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6572 | exts[nr].disk_num_bytes = |
| 6573 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6574 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6575 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6576 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6577 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6578 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6579 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6580 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6581 | BUG_ON(exts[nr].offset > 0); |
| 6582 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6583 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6584 | |
| 6585 | cur_pos += exts[nr].num_bytes; |
| 6586 | nr++; |
| 6587 | |
| 6588 | if (cur_pos + offset >= last_byte) |
| 6589 | break; |
| 6590 | |
| 6591 | if (no_fragment) { |
| 6592 | ret = 1; |
| 6593 | goto out; |
| 6594 | } |
| 6595 | path->slots[0]++; |
| 6596 | } |
| 6597 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6598 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6599 | if (cur_pos + offset < last_byte) { |
| 6600 | ret = -ENOENT; |
| 6601 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6602 | } |
| 6603 | ret = 0; |
| 6604 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6605 | btrfs_free_path(path); |
| 6606 | if (ret) { |
| 6607 | if (exts != *extents) |
| 6608 | kfree(exts); |
| 6609 | } else { |
| 6610 | *extents = exts; |
| 6611 | *nr_extents = nr; |
| 6612 | } |
| 6613 | return ret; |
| 6614 | } |
| 6615 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6616 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6617 | struct btrfs_root *root, |
| 6618 | struct btrfs_path *path, |
| 6619 | struct btrfs_key *extent_key, |
| 6620 | struct btrfs_key *leaf_key, |
| 6621 | struct btrfs_ref_path *ref_path, |
| 6622 | struct disk_extent *new_extents, |
| 6623 | int nr_extents) |
| 6624 | { |
| 6625 | struct extent_buffer *leaf; |
| 6626 | struct btrfs_file_extent_item *fi; |
| 6627 | struct inode *inode = NULL; |
| 6628 | struct btrfs_key key; |
| 6629 | u64 lock_start = 0; |
| 6630 | u64 lock_end = 0; |
| 6631 | u64 num_bytes; |
| 6632 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6633 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6634 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6635 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6636 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6637 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6638 | int ret; |
| 6639 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6640 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6641 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6642 | if (key.objectid < ref_path->owner_objectid || |
| 6643 | (key.objectid == ref_path->owner_objectid && |
| 6644 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6645 | key.objectid = ref_path->owner_objectid; |
| 6646 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6647 | key.offset = 0; |
| 6648 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | while (1) { |
| 6652 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6653 | if (ret < 0) |
| 6654 | goto out; |
| 6655 | |
| 6656 | leaf = path->nodes[0]; |
| 6657 | nritems = btrfs_header_nritems(leaf); |
| 6658 | next: |
| 6659 | if (extent_locked && ret > 0) { |
| 6660 | /* |
| 6661 | * the file extent item was modified by someone |
| 6662 | * before the extent got locked. |
| 6663 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6664 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6665 | lock_end, GFP_NOFS); |
| 6666 | extent_locked = 0; |
| 6667 | } |
| 6668 | |
| 6669 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6670 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6671 | break; |
| 6672 | |
| 6673 | BUG_ON(extent_locked); |
| 6674 | ret = btrfs_next_leaf(root, path); |
| 6675 | if (ret < 0) |
| 6676 | goto out; |
| 6677 | if (ret > 0) |
| 6678 | break; |
| 6679 | leaf = path->nodes[0]; |
| 6680 | nritems = btrfs_header_nritems(leaf); |
| 6681 | } |
| 6682 | |
| 6683 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 6684 | |
| 6685 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 6686 | if ((key.objectid > ref_path->owner_objectid) || |
| 6687 | (key.objectid == ref_path->owner_objectid && |
| 6688 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6689 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6690 | break; |
| 6691 | } |
| 6692 | |
| 6693 | if (inode && key.objectid != inode->i_ino) { |
| 6694 | BUG_ON(extent_locked); |
| 6695 | btrfs_release_path(root, path); |
| 6696 | mutex_unlock(&inode->i_mutex); |
| 6697 | iput(inode); |
| 6698 | inode = NULL; |
| 6699 | continue; |
| 6700 | } |
| 6701 | |
| 6702 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6703 | path->slots[0]++; |
| 6704 | ret = 1; |
| 6705 | goto next; |
| 6706 | } |
| 6707 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6708 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6709 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 6710 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 6711 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6712 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 6713 | extent_key->objectid)) { |
| 6714 | path->slots[0]++; |
| 6715 | ret = 1; |
| 6716 | goto next; |
| 6717 | } |
| 6718 | |
| 6719 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6720 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 6721 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6722 | if (search_end == (u64)-1) { |
| 6723 | search_end = key.offset - ext_offset + |
| 6724 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 6725 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6726 | |
| 6727 | if (!extent_locked) { |
| 6728 | lock_start = key.offset; |
| 6729 | lock_end = lock_start + num_bytes - 1; |
| 6730 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6731 | if (lock_start > key.offset || |
| 6732 | lock_end + 1 < key.offset + num_bytes) { |
| 6733 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6734 | lock_start, lock_end, GFP_NOFS); |
| 6735 | extent_locked = 0; |
| 6736 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | if (!inode) { |
| 6740 | btrfs_release_path(root, path); |
| 6741 | |
| 6742 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 6743 | key.objectid, root); |
| 6744 | if (inode->i_state & I_NEW) { |
| 6745 | BTRFS_I(inode)->root = root; |
| 6746 | BTRFS_I(inode)->location.objectid = |
| 6747 | key.objectid; |
| 6748 | BTRFS_I(inode)->location.type = |
| 6749 | BTRFS_INODE_ITEM_KEY; |
| 6750 | BTRFS_I(inode)->location.offset = 0; |
| 6751 | btrfs_read_locked_inode(inode); |
| 6752 | unlock_new_inode(inode); |
| 6753 | } |
| 6754 | /* |
| 6755 | * some code call btrfs_commit_transaction while |
| 6756 | * holding the i_mutex, so we can't use mutex_lock |
| 6757 | * here. |
| 6758 | */ |
| 6759 | if (is_bad_inode(inode) || |
| 6760 | !mutex_trylock(&inode->i_mutex)) { |
| 6761 | iput(inode); |
| 6762 | inode = NULL; |
| 6763 | key.offset = (u64)-1; |
| 6764 | goto skip; |
| 6765 | } |
| 6766 | } |
| 6767 | |
| 6768 | if (!extent_locked) { |
| 6769 | struct btrfs_ordered_extent *ordered; |
| 6770 | |
| 6771 | btrfs_release_path(root, path); |
| 6772 | |
| 6773 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6774 | lock_end, GFP_NOFS); |
| 6775 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6776 | lock_end); |
| 6777 | if (ordered && |
| 6778 | ordered->file_offset <= lock_end && |
| 6779 | ordered->file_offset + ordered->len > lock_start) { |
| 6780 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6781 | lock_start, lock_end, GFP_NOFS); |
| 6782 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6783 | btrfs_put_ordered_extent(ordered); |
| 6784 | key.offset += num_bytes; |
| 6785 | goto skip; |
| 6786 | } |
| 6787 | if (ordered) |
| 6788 | btrfs_put_ordered_extent(ordered); |
| 6789 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6790 | extent_locked = 1; |
| 6791 | continue; |
| 6792 | } |
| 6793 | |
| 6794 | if (nr_extents == 1) { |
| 6795 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6796 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6797 | new_extents[0].disk_bytenr); |
| 6798 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6799 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6800 | btrfs_mark_buffer_dirty(leaf); |
| 6801 | |
| 6802 | btrfs_drop_extent_cache(inode, key.offset, |
| 6803 | key.offset + num_bytes - 1, 0); |
| 6804 | |
| 6805 | ret = btrfs_inc_extent_ref(trans, root, |
| 6806 | new_extents[0].disk_bytenr, |
| 6807 | new_extents[0].disk_num_bytes, |
| 6808 | leaf->start, |
| 6809 | root->root_key.objectid, |
| 6810 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6811 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6812 | BUG_ON(ret); |
| 6813 | |
| 6814 | ret = btrfs_free_extent(trans, root, |
| 6815 | extent_key->objectid, |
| 6816 | extent_key->offset, |
| 6817 | leaf->start, |
| 6818 | btrfs_header_owner(leaf), |
| 6819 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6820 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6821 | BUG_ON(ret); |
| 6822 | |
| 6823 | btrfs_release_path(root, path); |
| 6824 | key.offset += num_bytes; |
| 6825 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6826 | BUG_ON(1); |
| 6827 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6828 | u64 alloc_hint; |
| 6829 | u64 extent_len; |
| 6830 | int i; |
| 6831 | /* |
| 6832 | * drop old extent pointer at first, then insert the |
| 6833 | * new pointers one bye one |
| 6834 | */ |
| 6835 | btrfs_release_path(root, path); |
| 6836 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 6837 | key.offset + num_bytes, |
| 6838 | key.offset, &alloc_hint); |
| 6839 | BUG_ON(ret); |
| 6840 | |
| 6841 | for (i = 0; i < nr_extents; i++) { |
| 6842 | if (ext_offset >= new_extents[i].num_bytes) { |
| 6843 | ext_offset -= new_extents[i].num_bytes; |
| 6844 | continue; |
| 6845 | } |
| 6846 | extent_len = min(new_extents[i].num_bytes - |
| 6847 | ext_offset, num_bytes); |
| 6848 | |
| 6849 | ret = btrfs_insert_empty_item(trans, root, |
| 6850 | path, &key, |
| 6851 | sizeof(*fi)); |
| 6852 | BUG_ON(ret); |
| 6853 | |
| 6854 | leaf = path->nodes[0]; |
| 6855 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6856 | struct btrfs_file_extent_item); |
| 6857 | btrfs_set_file_extent_generation(leaf, fi, |
| 6858 | trans->transid); |
| 6859 | btrfs_set_file_extent_type(leaf, fi, |
| 6860 | BTRFS_FILE_EXTENT_REG); |
| 6861 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6862 | new_extents[i].disk_bytenr); |
| 6863 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6864 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6865 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 6866 | new_extents[i].ram_bytes); |
| 6867 | |
| 6868 | btrfs_set_file_extent_compression(leaf, fi, |
| 6869 | new_extents[i].compression); |
| 6870 | btrfs_set_file_extent_encryption(leaf, fi, |
| 6871 | new_extents[i].encryption); |
| 6872 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 6873 | new_extents[i].other_encoding); |
| 6874 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6875 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 6876 | extent_len); |
| 6877 | ext_offset += new_extents[i].offset; |
| 6878 | btrfs_set_file_extent_offset(leaf, fi, |
| 6879 | ext_offset); |
| 6880 | btrfs_mark_buffer_dirty(leaf); |
| 6881 | |
| 6882 | btrfs_drop_extent_cache(inode, key.offset, |
| 6883 | key.offset + extent_len - 1, 0); |
| 6884 | |
| 6885 | ret = btrfs_inc_extent_ref(trans, root, |
| 6886 | new_extents[i].disk_bytenr, |
| 6887 | new_extents[i].disk_num_bytes, |
| 6888 | leaf->start, |
| 6889 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6890 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6891 | BUG_ON(ret); |
| 6892 | btrfs_release_path(root, path); |
| 6893 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6894 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6895 | |
| 6896 | ext_offset = 0; |
| 6897 | num_bytes -= extent_len; |
| 6898 | key.offset += extent_len; |
| 6899 | |
| 6900 | if (num_bytes == 0) |
| 6901 | break; |
| 6902 | } |
| 6903 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6904 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6905 | } |
| 6906 | |
| 6907 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6908 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6909 | lock_end, GFP_NOFS); |
| 6910 | extent_locked = 0; |
| 6911 | } |
| 6912 | skip: |
| 6913 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6914 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6915 | break; |
| 6916 | |
| 6917 | cond_resched(); |
| 6918 | } |
| 6919 | ret = 0; |
| 6920 | out: |
| 6921 | btrfs_release_path(root, path); |
| 6922 | if (inode) { |
| 6923 | mutex_unlock(&inode->i_mutex); |
| 6924 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6925 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6926 | lock_end, GFP_NOFS); |
| 6927 | } |
| 6928 | iput(inode); |
| 6929 | } |
| 6930 | return ret; |
| 6931 | } |
| 6932 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6933 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 6934 | struct btrfs_root *root, |
| 6935 | struct extent_buffer *buf, u64 orig_start) |
| 6936 | { |
| 6937 | int level; |
| 6938 | int ret; |
| 6939 | |
| 6940 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 6941 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6942 | |
| 6943 | level = btrfs_header_level(buf); |
| 6944 | if (level == 0) { |
| 6945 | struct btrfs_leaf_ref *ref; |
| 6946 | struct btrfs_leaf_ref *orig_ref; |
| 6947 | |
| 6948 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 6949 | if (!orig_ref) |
| 6950 | return -ENOENT; |
| 6951 | |
| 6952 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 6953 | if (!ref) { |
| 6954 | btrfs_free_leaf_ref(root, orig_ref); |
| 6955 | return -ENOMEM; |
| 6956 | } |
| 6957 | |
| 6958 | ref->nritems = orig_ref->nritems; |
| 6959 | memcpy(ref->extents, orig_ref->extents, |
| 6960 | sizeof(ref->extents[0]) * ref->nritems); |
| 6961 | |
| 6962 | btrfs_free_leaf_ref(root, orig_ref); |
| 6963 | |
| 6964 | ref->root_gen = trans->transid; |
| 6965 | ref->bytenr = buf->start; |
| 6966 | ref->owner = btrfs_header_owner(buf); |
| 6967 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6968 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6969 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 6970 | WARN_ON(ret); |
| 6971 | btrfs_free_leaf_ref(root, ref); |
| 6972 | } |
| 6973 | return 0; |
| 6974 | } |
| 6975 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6976 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6977 | struct extent_buffer *leaf, |
| 6978 | struct btrfs_block_group_cache *group, |
| 6979 | struct btrfs_root *target_root) |
| 6980 | { |
| 6981 | struct btrfs_key key; |
| 6982 | struct inode *inode = NULL; |
| 6983 | struct btrfs_file_extent_item *fi; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6984 | struct extent_state *cached_state = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6985 | u64 num_bytes; |
| 6986 | u64 skip_objectid = 0; |
| 6987 | u32 nritems; |
| 6988 | u32 i; |
| 6989 | |
| 6990 | nritems = btrfs_header_nritems(leaf); |
| 6991 | for (i = 0; i < nritems; i++) { |
| 6992 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6993 | if (key.objectid == skip_objectid || |
| 6994 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 6995 | continue; |
| 6996 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6997 | if (btrfs_file_extent_type(leaf, fi) == |
| 6998 | BTRFS_FILE_EXTENT_INLINE) |
| 6999 | continue; |
| 7000 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 7001 | continue; |
| 7002 | if (!inode || inode->i_ino != key.objectid) { |
| 7003 | iput(inode); |
| 7004 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 7005 | key.objectid, target_root, 1); |
| 7006 | } |
| 7007 | if (!inode) { |
| 7008 | skip_objectid = key.objectid; |
| 7009 | continue; |
| 7010 | } |
| 7011 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7012 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7013 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, |
| 7014 | key.offset + num_bytes - 1, 0, &cached_state, |
| 7015 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7016 | btrfs_drop_extent_cache(inode, key.offset, |
| 7017 | key.offset + num_bytes - 1, 1); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7018 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, |
| 7019 | key.offset + num_bytes - 1, &cached_state, |
| 7020 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7021 | cond_resched(); |
| 7022 | } |
| 7023 | iput(inode); |
| 7024 | return 0; |
| 7025 | } |
| 7026 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7027 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7028 | struct btrfs_root *root, |
| 7029 | struct extent_buffer *leaf, |
| 7030 | struct btrfs_block_group_cache *group, |
| 7031 | struct inode *reloc_inode) |
| 7032 | { |
| 7033 | struct btrfs_key key; |
| 7034 | struct btrfs_key extent_key; |
| 7035 | struct btrfs_file_extent_item *fi; |
| 7036 | struct btrfs_leaf_ref *ref; |
| 7037 | struct disk_extent *new_extent; |
| 7038 | u64 bytenr; |
| 7039 | u64 num_bytes; |
| 7040 | u32 nritems; |
| 7041 | u32 i; |
| 7042 | int ext_index; |
| 7043 | int nr_extent; |
| 7044 | int ret; |
| 7045 | |
| 7046 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 7047 | BUG_ON(!new_extent); |
| 7048 | |
| 7049 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 7050 | BUG_ON(!ref); |
| 7051 | |
| 7052 | ext_index = -1; |
| 7053 | nritems = btrfs_header_nritems(leaf); |
| 7054 | for (i = 0; i < nritems; i++) { |
| 7055 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7056 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 7057 | continue; |
| 7058 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7059 | if (btrfs_file_extent_type(leaf, fi) == |
| 7060 | BTRFS_FILE_EXTENT_INLINE) |
| 7061 | continue; |
| 7062 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 7063 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7064 | if (bytenr == 0) |
| 7065 | continue; |
| 7066 | |
| 7067 | ext_index++; |
| 7068 | if (bytenr >= group->key.objectid + group->key.offset || |
| 7069 | bytenr + num_bytes <= group->key.objectid) |
| 7070 | continue; |
| 7071 | |
| 7072 | extent_key.objectid = bytenr; |
| 7073 | extent_key.offset = num_bytes; |
| 7074 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 7075 | nr_extent = 1; |
| 7076 | ret = get_new_locations(reloc_inode, &extent_key, |
| 7077 | group->key.objectid, 1, |
| 7078 | &new_extent, &nr_extent); |
| 7079 | if (ret > 0) |
| 7080 | continue; |
| 7081 | BUG_ON(ret < 0); |
| 7082 | |
| 7083 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 7084 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 7085 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 7086 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 7087 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7088 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7089 | new_extent->disk_bytenr); |
| 7090 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7091 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7092 | btrfs_mark_buffer_dirty(leaf); |
| 7093 | |
| 7094 | ret = btrfs_inc_extent_ref(trans, root, |
| 7095 | new_extent->disk_bytenr, |
| 7096 | new_extent->disk_num_bytes, |
| 7097 | leaf->start, |
| 7098 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7099 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7100 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7101 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7102 | ret = btrfs_free_extent(trans, root, |
| 7103 | bytenr, num_bytes, leaf->start, |
| 7104 | btrfs_header_owner(leaf), |
| 7105 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7106 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7107 | BUG_ON(ret); |
| 7108 | cond_resched(); |
| 7109 | } |
| 7110 | kfree(new_extent); |
| 7111 | BUG_ON(ext_index + 1 != ref->nritems); |
| 7112 | btrfs_free_leaf_ref(root, ref); |
| 7113 | return 0; |
| 7114 | } |
| 7115 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7116 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 7117 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7118 | { |
| 7119 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7120 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7121 | |
| 7122 | if (root->reloc_root) { |
| 7123 | reloc_root = root->reloc_root; |
| 7124 | root->reloc_root = NULL; |
| 7125 | list_add(&reloc_root->dead_list, |
| 7126 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7127 | |
| 7128 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 7129 | reloc_root->node->start); |
| 7130 | btrfs_set_root_level(&root->root_item, |
| 7131 | btrfs_header_level(reloc_root->node)); |
| 7132 | memset(&reloc_root->root_item.drop_progress, 0, |
| 7133 | sizeof(struct btrfs_disk_key)); |
| 7134 | reloc_root->root_item.drop_level = 0; |
| 7135 | |
| 7136 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 7137 | &reloc_root->root_key, |
| 7138 | &reloc_root->root_item); |
| 7139 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7140 | } |
| 7141 | return 0; |
| 7142 | } |
| 7143 | |
| 7144 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 7145 | { |
| 7146 | struct btrfs_trans_handle *trans; |
| 7147 | struct btrfs_root *reloc_root; |
| 7148 | struct btrfs_root *prev_root = NULL; |
| 7149 | struct list_head dead_roots; |
| 7150 | int ret; |
| 7151 | unsigned long nr; |
| 7152 | |
| 7153 | INIT_LIST_HEAD(&dead_roots); |
| 7154 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 7155 | |
| 7156 | while (!list_empty(&dead_roots)) { |
| 7157 | reloc_root = list_entry(dead_roots.prev, |
| 7158 | struct btrfs_root, dead_list); |
| 7159 | list_del_init(&reloc_root->dead_list); |
| 7160 | |
| 7161 | BUG_ON(reloc_root->commit_root != NULL); |
| 7162 | while (1) { |
| 7163 | trans = btrfs_join_transaction(root, 1); |
| 7164 | BUG_ON(!trans); |
| 7165 | |
| 7166 | mutex_lock(&root->fs_info->drop_mutex); |
| 7167 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 7168 | if (ret != -EAGAIN) |
| 7169 | break; |
| 7170 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7171 | |
| 7172 | nr = trans->blocks_used; |
| 7173 | ret = btrfs_end_transaction(trans, root); |
| 7174 | BUG_ON(ret); |
| 7175 | btrfs_btree_balance_dirty(root, nr); |
| 7176 | } |
| 7177 | |
| 7178 | free_extent_buffer(reloc_root->node); |
| 7179 | |
| 7180 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 7181 | &reloc_root->root_key); |
| 7182 | BUG_ON(ret); |
| 7183 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7184 | |
| 7185 | nr = trans->blocks_used; |
| 7186 | ret = btrfs_end_transaction(trans, root); |
| 7187 | BUG_ON(ret); |
| 7188 | btrfs_btree_balance_dirty(root, nr); |
| 7189 | |
| 7190 | kfree(prev_root); |
| 7191 | prev_root = reloc_root; |
| 7192 | } |
| 7193 | if (prev_root) { |
| 7194 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 7195 | kfree(prev_root); |
| 7196 | } |
| 7197 | return 0; |
| 7198 | } |
| 7199 | |
| 7200 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 7201 | { |
| 7202 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 7203 | return 0; |
| 7204 | } |
| 7205 | |
| 7206 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 7207 | { |
| 7208 | struct btrfs_root *reloc_root; |
| 7209 | struct btrfs_trans_handle *trans; |
| 7210 | struct btrfs_key location; |
| 7211 | int found; |
| 7212 | int ret; |
| 7213 | |
| 7214 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7215 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 7216 | BUG_ON(ret); |
| 7217 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 7218 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 7219 | |
| 7220 | if (found) { |
| 7221 | trans = btrfs_start_transaction(root, 1); |
| 7222 | BUG_ON(!trans); |
| 7223 | ret = btrfs_commit_transaction(trans, root); |
| 7224 | BUG_ON(ret); |
| 7225 | } |
| 7226 | |
| 7227 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 7228 | location.offset = (u64)-1; |
| 7229 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 7230 | |
| 7231 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 7232 | BUG_ON(!reloc_root); |
| 7233 | btrfs_orphan_cleanup(reloc_root); |
| 7234 | return 0; |
| 7235 | } |
| 7236 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7237 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7238 | struct btrfs_root *root) |
| 7239 | { |
| 7240 | struct btrfs_root *reloc_root; |
| 7241 | struct extent_buffer *eb; |
| 7242 | struct btrfs_root_item *root_item; |
| 7243 | struct btrfs_key root_key; |
| 7244 | int ret; |
| 7245 | |
| 7246 | BUG_ON(!root->ref_cows); |
| 7247 | if (root->reloc_root) |
| 7248 | return 0; |
| 7249 | |
| 7250 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 7251 | BUG_ON(!root_item); |
| 7252 | |
| 7253 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 7254 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 7255 | BUG_ON(ret); |
| 7256 | |
| 7257 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 7258 | root_key.offset = root->root_key.objectid; |
| 7259 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7260 | |
| 7261 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 7262 | btrfs_set_root_refs(root_item, 0); |
| 7263 | btrfs_set_root_bytenr(root_item, eb->start); |
| 7264 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7265 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7266 | |
| 7267 | btrfs_tree_unlock(eb); |
| 7268 | free_extent_buffer(eb); |
| 7269 | |
| 7270 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 7271 | &root_key, root_item); |
| 7272 | BUG_ON(ret); |
| 7273 | kfree(root_item); |
| 7274 | |
| 7275 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 7276 | &root_key); |
| 7277 | BUG_ON(!reloc_root); |
| 7278 | reloc_root->last_trans = trans->transid; |
| 7279 | reloc_root->commit_root = NULL; |
| 7280 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 7281 | |
| 7282 | root->reloc_root = reloc_root; |
| 7283 | return 0; |
| 7284 | } |
| 7285 | |
| 7286 | /* |
| 7287 | * Core function of space balance. |
| 7288 | * |
| 7289 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7290 | * counted roots. There is one reloc tree for each subvol, and all |
| 7291 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 7292 | * of the latest committed roots of subvols (root->commit_root). |
| 7293 | * |
| 7294 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 7295 | * COW the block through subvol's reloc tree, then update block pointer |
| 7296 | * in the subvol to point to the new block. Since all reloc trees share |
| 7297 | * same root key objectid, doing special handing for tree blocks owned |
| 7298 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 7299 | * we can use the resulting new block directly when the same block is |
| 7300 | * required to COW again through other reloc trees. By this way, relocated |
| 7301 | * tree blocks are shared between reloc trees, so they are also shared |
| 7302 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7303 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7304 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7305 | struct btrfs_root *root, |
| 7306 | struct btrfs_path *path, |
| 7307 | struct btrfs_key *first_key, |
| 7308 | struct btrfs_ref_path *ref_path, |
| 7309 | struct btrfs_block_group_cache *group, |
| 7310 | struct inode *reloc_inode) |
| 7311 | { |
| 7312 | struct btrfs_root *reloc_root; |
| 7313 | struct extent_buffer *eb = NULL; |
| 7314 | struct btrfs_key *keys; |
| 7315 | u64 *nodes; |
| 7316 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7317 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7318 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7319 | int ret; |
| 7320 | |
| 7321 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 7322 | lowest_level = ref_path->owner_objectid; |
| 7323 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7324 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7325 | path->lowest_level = lowest_level; |
| 7326 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 7327 | BUG_ON(ret < 0); |
| 7328 | path->lowest_level = 0; |
| 7329 | btrfs_release_path(root, path); |
| 7330 | return 0; |
| 7331 | } |
| 7332 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7333 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7334 | ret = init_reloc_tree(trans, root); |
| 7335 | BUG_ON(ret); |
| 7336 | reloc_root = root->reloc_root; |
| 7337 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7338 | shared_level = ref_path->shared_level; |
| 7339 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7340 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7341 | keys = ref_path->node_keys; |
| 7342 | nodes = ref_path->new_nodes; |
| 7343 | memset(&keys[shared_level + 1], 0, |
| 7344 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 7345 | memset(&nodes[shared_level + 1], 0, |
| 7346 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7347 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7348 | if (nodes[lowest_level] == 0) { |
| 7349 | path->lowest_level = lowest_level; |
| 7350 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7351 | 0, 1); |
| 7352 | BUG_ON(ret); |
| 7353 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 7354 | eb = path->nodes[level]; |
| 7355 | if (!eb || eb == reloc_root->node) |
| 7356 | break; |
| 7357 | nodes[level] = eb->start; |
| 7358 | if (level == 0) |
| 7359 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 7360 | else |
| 7361 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 7362 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7363 | if (nodes[0] && |
| 7364 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7365 | eb = path->nodes[0]; |
| 7366 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 7367 | group, reloc_inode); |
| 7368 | BUG_ON(ret); |
| 7369 | } |
| 7370 | btrfs_release_path(reloc_root, path); |
| 7371 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7372 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7373 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7374 | BUG_ON(ret); |
| 7375 | } |
| 7376 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7377 | /* |
| 7378 | * replace tree blocks in the fs tree with tree blocks in |
| 7379 | * the reloc tree. |
| 7380 | */ |
| 7381 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 7382 | BUG_ON(ret < 0); |
| 7383 | |
| 7384 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7385 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7386 | 0, 0); |
| 7387 | BUG_ON(ret); |
| 7388 | extent_buffer_get(path->nodes[0]); |
| 7389 | eb = path->nodes[0]; |
| 7390 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7391 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 7392 | BUG_ON(ret); |
| 7393 | free_extent_buffer(eb); |
| 7394 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7395 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7396 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7397 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7398 | return 0; |
| 7399 | } |
| 7400 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7401 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7402 | struct btrfs_root *root, |
| 7403 | struct btrfs_path *path, |
| 7404 | struct btrfs_key *first_key, |
| 7405 | struct btrfs_ref_path *ref_path) |
| 7406 | { |
| 7407 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7408 | |
| 7409 | ret = relocate_one_path(trans, root, path, first_key, |
| 7410 | ref_path, NULL, NULL); |
| 7411 | BUG_ON(ret); |
| 7412 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7413 | return 0; |
| 7414 | } |
| 7415 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7416 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7417 | struct btrfs_root *extent_root, |
| 7418 | struct btrfs_path *path, |
| 7419 | struct btrfs_key *extent_key) |
| 7420 | { |
| 7421 | int ret; |
| 7422 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7423 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 7424 | if (ret) |
| 7425 | goto out; |
| 7426 | ret = btrfs_del_item(trans, extent_root, path); |
| 7427 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7428 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7429 | return ret; |
| 7430 | } |
| 7431 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7432 | static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7433 | struct btrfs_ref_path *ref_path) |
| 7434 | { |
| 7435 | struct btrfs_key root_key; |
| 7436 | |
| 7437 | root_key.objectid = ref_path->root_objectid; |
| 7438 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7439 | if (is_cowonly_root(ref_path->root_objectid)) |
| 7440 | root_key.offset = 0; |
| 7441 | else |
| 7442 | root_key.offset = (u64)-1; |
| 7443 | |
| 7444 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7445 | } |
| 7446 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7447 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7448 | struct btrfs_path *path, |
| 7449 | struct btrfs_key *extent_key, |
| 7450 | struct btrfs_block_group_cache *group, |
| 7451 | struct inode *reloc_inode, int pass) |
| 7452 | { |
| 7453 | struct btrfs_trans_handle *trans; |
| 7454 | struct btrfs_root *found_root; |
| 7455 | struct btrfs_ref_path *ref_path = NULL; |
| 7456 | struct disk_extent *new_extents = NULL; |
| 7457 | int nr_extents = 0; |
| 7458 | int loops; |
| 7459 | int ret; |
| 7460 | int level; |
| 7461 | struct btrfs_key first_key; |
| 7462 | u64 prev_block = 0; |
| 7463 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7464 | |
| 7465 | trans = btrfs_start_transaction(extent_root, 1); |
| 7466 | BUG_ON(!trans); |
| 7467 | |
| 7468 | if (extent_key->objectid == 0) { |
| 7469 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7470 | goto out; |
| 7471 | } |
| 7472 | |
| 7473 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7474 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7475 | ret = -ENOMEM; |
| 7476 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7477 | } |
| 7478 | |
| 7479 | for (loops = 0; ; loops++) { |
| 7480 | if (loops == 0) { |
| 7481 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7482 | extent_key->objectid); |
| 7483 | } else { |
| 7484 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7485 | } |
| 7486 | if (ret < 0) |
| 7487 | goto out; |
| 7488 | if (ret > 0) |
| 7489 | break; |
| 7490 | |
| 7491 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7492 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7493 | continue; |
| 7494 | |
| 7495 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7496 | BUG_ON(!found_root); |
| 7497 | /* |
| 7498 | * for reference counted tree, only process reference paths |
| 7499 | * rooted at the latest committed root. |
| 7500 | */ |
| 7501 | if (found_root->ref_cows && |
| 7502 | ref_path->root_generation != found_root->root_key.offset) |
| 7503 | continue; |
| 7504 | |
| 7505 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7506 | if (pass == 0) { |
| 7507 | /* |
| 7508 | * copy data extents to new locations |
| 7509 | */ |
| 7510 | u64 group_start = group->key.objectid; |
| 7511 | ret = relocate_data_extent(reloc_inode, |
| 7512 | extent_key, |
| 7513 | group_start); |
| 7514 | if (ret < 0) |
| 7515 | goto out; |
| 7516 | break; |
| 7517 | } |
| 7518 | level = 0; |
| 7519 | } else { |
| 7520 | level = ref_path->owner_objectid; |
| 7521 | } |
| 7522 | |
| 7523 | if (prev_block != ref_path->nodes[level]) { |
| 7524 | struct extent_buffer *eb; |
| 7525 | u64 block_start = ref_path->nodes[level]; |
| 7526 | u64 block_size = btrfs_level_size(found_root, level); |
| 7527 | |
| 7528 | eb = read_tree_block(found_root, block_start, |
| 7529 | block_size, 0); |
| 7530 | btrfs_tree_lock(eb); |
| 7531 | BUG_ON(level != btrfs_header_level(eb)); |
| 7532 | |
| 7533 | if (level == 0) |
| 7534 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7535 | else |
| 7536 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7537 | |
| 7538 | btrfs_tree_unlock(eb); |
| 7539 | free_extent_buffer(eb); |
| 7540 | prev_block = block_start; |
| 7541 | } |
| 7542 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7543 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7544 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7545 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7546 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7547 | /* |
| 7548 | * try to update data extent references while |
| 7549 | * keeping metadata shared between snapshots. |
| 7550 | */ |
| 7551 | if (pass == 1) { |
| 7552 | ret = relocate_one_path(trans, found_root, |
| 7553 | path, &first_key, ref_path, |
| 7554 | group, reloc_inode); |
| 7555 | if (ret < 0) |
| 7556 | goto out; |
| 7557 | continue; |
| 7558 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7559 | /* |
| 7560 | * use fallback method to process the remaining |
| 7561 | * references. |
| 7562 | */ |
| 7563 | if (!new_extents) { |
| 7564 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7565 | new_extents = kmalloc(sizeof(*new_extents), |
| 7566 | GFP_NOFS); |
| 7567 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7568 | ret = get_new_locations(reloc_inode, |
| 7569 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7570 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7571 | &new_extents, |
| 7572 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7573 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7574 | goto out; |
| 7575 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7576 | ret = replace_one_extent(trans, found_root, |
| 7577 | path, extent_key, |
| 7578 | &first_key, ref_path, |
| 7579 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7580 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7581 | ret = relocate_tree_block(trans, found_root, path, |
| 7582 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7583 | } |
| 7584 | if (ret < 0) |
| 7585 | goto out; |
| 7586 | } |
| 7587 | ret = 0; |
| 7588 | out: |
| 7589 | btrfs_end_transaction(trans, extent_root); |
| 7590 | kfree(new_extents); |
| 7591 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7592 | return ret; |
| 7593 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7594 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7595 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7596 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7597 | { |
| 7598 | u64 num_devices; |
| 7599 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7600 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7601 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7602 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7603 | if (num_devices == 1) { |
| 7604 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7605 | stripped = flags & ~stripped; |
| 7606 | |
| 7607 | /* turn raid0 into single device chunks */ |
| 7608 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7609 | return stripped; |
| 7610 | |
| 7611 | /* turn mirroring into duplication */ |
| 7612 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7613 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7614 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7615 | return flags; |
| 7616 | } else { |
| 7617 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7618 | if (flags & stripped) |
| 7619 | return flags; |
| 7620 | |
| 7621 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7622 | stripped = flags & ~stripped; |
| 7623 | |
| 7624 | /* switch duplicated blocks with raid1 */ |
| 7625 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7626 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7627 | |
| 7628 | /* turn single device chunks into raid0 */ |
| 7629 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7630 | } |
| 7631 | return flags; |
| 7632 | } |
| 7633 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7634 | static int set_block_group_ro(struct btrfs_block_group_cache *cache) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7635 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7636 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7637 | u64 num_bytes; |
| 7638 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7639 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7640 | if (cache->ro) |
| 7641 | return 0; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7642 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7643 | spin_lock(&sinfo->lock); |
| 7644 | spin_lock(&cache->lock); |
| 7645 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7646 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7647 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7648 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 7649 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
| 7650 | cache->reserved_pinned + num_bytes < sinfo->total_bytes) { |
| 7651 | sinfo->bytes_readonly += num_bytes; |
| 7652 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 7653 | cache->reserved_pinned = 0; |
| 7654 | cache->ro = 1; |
| 7655 | ret = 0; |
| 7656 | } |
| 7657 | spin_unlock(&cache->lock); |
| 7658 | spin_unlock(&sinfo->lock); |
| 7659 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7660 | } |
| 7661 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7662 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 7663 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7664 | |
| 7665 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7666 | struct btrfs_trans_handle *trans; |
| 7667 | u64 alloc_flags; |
| 7668 | int ret; |
| 7669 | |
| 7670 | BUG_ON(cache->ro); |
| 7671 | |
| 7672 | trans = btrfs_join_transaction(root, 1); |
| 7673 | BUG_ON(IS_ERR(trans)); |
| 7674 | |
| 7675 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 7676 | if (alloc_flags != cache->flags) |
| 7677 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 7678 | |
| 7679 | ret = set_block_group_ro(cache); |
| 7680 | if (!ret) |
| 7681 | goto out; |
| 7682 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
| 7683 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 7684 | if (ret < 0) |
| 7685 | goto out; |
| 7686 | ret = set_block_group_ro(cache); |
| 7687 | out: |
| 7688 | btrfs_end_transaction(trans, root); |
| 7689 | return ret; |
| 7690 | } |
| 7691 | |
| 7692 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 7693 | struct btrfs_block_group_cache *cache) |
| 7694 | { |
| 7695 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7696 | u64 num_bytes; |
| 7697 | |
| 7698 | BUG_ON(!cache->ro); |
| 7699 | |
| 7700 | spin_lock(&sinfo->lock); |
| 7701 | spin_lock(&cache->lock); |
| 7702 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7703 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 7704 | sinfo->bytes_readonly -= num_bytes; |
| 7705 | cache->ro = 0; |
| 7706 | spin_unlock(&cache->lock); |
| 7707 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7708 | return 0; |
| 7709 | } |
| 7710 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7711 | /* |
| 7712 | * checks to see if its even possible to relocate this block group. |
| 7713 | * |
| 7714 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7715 | * ok to go ahead and try. |
| 7716 | */ |
| 7717 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7718 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7719 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7720 | struct btrfs_space_info *space_info; |
| 7721 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7722 | struct btrfs_device *device; |
| 7723 | int full = 0; |
| 7724 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7725 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7726 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7727 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7728 | /* odd, couldn't find the block group, leave it alone */ |
| 7729 | if (!block_group) |
| 7730 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7731 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7732 | /* no bytes used, we're good */ |
| 7733 | if (!btrfs_block_group_used(&block_group->item)) |
| 7734 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7735 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7736 | space_info = block_group->space_info; |
| 7737 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7738 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7739 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7740 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7741 | /* |
| 7742 | * 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] | 7743 | * relocate it unless we're able to allocate a new chunk below. |
| 7744 | * |
| 7745 | * Otherwise, we need to make sure we have room in the space to handle |
| 7746 | * 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] | 7747 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7748 | if ((space_info->total_bytes != block_group->key.offset) && |
| 7749 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7750 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7751 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7752 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7753 | spin_unlock(&space_info->lock); |
| 7754 | goto out; |
| 7755 | } |
| 7756 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7757 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7758 | /* |
| 7759 | * ok we don't have enough space, but maybe we have free space on our |
| 7760 | * devices to allocate new chunks for relocation, so loop through our |
| 7761 | * alloc devices and guess if we have enough space. However, if we |
| 7762 | * were marked as full, then we know there aren't enough chunks, and we |
| 7763 | * can just return. |
| 7764 | */ |
| 7765 | ret = -1; |
| 7766 | if (full) |
| 7767 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 7768 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7769 | mutex_lock(&root->fs_info->chunk_mutex); |
| 7770 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 7771 | u64 min_free = btrfs_block_group_used(&block_group->item); |
| 7772 | u64 dev_offset, max_avail; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7773 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7774 | /* |
| 7775 | * check to make sure we can actually find a chunk with enough |
| 7776 | * space to fit our block group in. |
| 7777 | */ |
| 7778 | if (device->total_bytes > device->bytes_used + min_free) { |
| 7779 | ret = find_free_dev_extent(NULL, device, min_free, |
| 7780 | &dev_offset, &max_avail); |
| 7781 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7782 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7783 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7784 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7785 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7786 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7787 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7788 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7789 | return ret; |
| 7790 | } |
| 7791 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7792 | static int find_first_block_group(struct btrfs_root *root, |
| 7793 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7794 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7795 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7796 | struct btrfs_key found_key; |
| 7797 | struct extent_buffer *leaf; |
| 7798 | int slot; |
| 7799 | |
| 7800 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7801 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7802 | goto out; |
| 7803 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7804 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7805 | slot = path->slots[0]; |
| 7806 | leaf = path->nodes[0]; |
| 7807 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7808 | ret = btrfs_next_leaf(root, path); |
| 7809 | if (ret == 0) |
| 7810 | continue; |
| 7811 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7812 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7813 | break; |
| 7814 | } |
| 7815 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7816 | |
| 7817 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7818 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7819 | ret = 0; |
| 7820 | goto out; |
| 7821 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7822 | path->slots[0]++; |
| 7823 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7824 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7825 | return ret; |
| 7826 | } |
| 7827 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7828 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7829 | { |
| 7830 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7831 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7832 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7833 | struct rb_node *n; |
| 7834 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7835 | down_write(&info->extent_commit_sem); |
| 7836 | while (!list_empty(&info->caching_block_groups)) { |
| 7837 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7838 | struct btrfs_caching_control, list); |
| 7839 | list_del(&caching_ctl->list); |
| 7840 | put_caching_control(caching_ctl); |
| 7841 | } |
| 7842 | up_write(&info->extent_commit_sem); |
| 7843 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7844 | spin_lock(&info->block_group_cache_lock); |
| 7845 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7846 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7847 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7848 | rb_erase(&block_group->cache_node, |
| 7849 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7850 | spin_unlock(&info->block_group_cache_lock); |
| 7851 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7852 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7853 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7854 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7855 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7856 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7857 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7858 | |
| 7859 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 7860 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7861 | |
| 7862 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7863 | } |
| 7864 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7865 | |
| 7866 | /* now that all the block groups are freed, go through and |
| 7867 | * free all the space_info structs. This is only called during |
| 7868 | * the final stages of unmount, and so we know nobody is |
| 7869 | * using them. We call synchronize_rcu() once before we start, |
| 7870 | * just to be on the safe side. |
| 7871 | */ |
| 7872 | synchronize_rcu(); |
| 7873 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7874 | release_global_block_rsv(info); |
| 7875 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7876 | while(!list_empty(&info->space_info)) { |
| 7877 | space_info = list_entry(info->space_info.next, |
| 7878 | struct btrfs_space_info, |
| 7879 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7880 | if (space_info->bytes_pinned > 0 || |
| 7881 | space_info->bytes_reserved > 0) { |
| 7882 | WARN_ON(1); |
| 7883 | dump_space_info(space_info, 0, 0); |
| 7884 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7885 | list_del(&space_info->list); |
| 7886 | kfree(space_info); |
| 7887 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7888 | return 0; |
| 7889 | } |
| 7890 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7891 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 7892 | struct btrfs_block_group_cache *cache) |
| 7893 | { |
| 7894 | int index = get_block_group_index(cache); |
| 7895 | |
| 7896 | down_write(&space_info->groups_sem); |
| 7897 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 7898 | up_write(&space_info->groups_sem); |
| 7899 | } |
| 7900 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7901 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7902 | { |
| 7903 | struct btrfs_path *path; |
| 7904 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7905 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7906 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7907 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7908 | struct btrfs_key key; |
| 7909 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7910 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7911 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7912 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7913 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7914 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7915 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7916 | path = btrfs_alloc_path(); |
| 7917 | if (!path) |
| 7918 | return -ENOMEM; |
| 7919 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7920 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7921 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7922 | if (ret > 0) |
| 7923 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7924 | if (ret != 0) |
| 7925 | goto error; |
| 7926 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7927 | leaf = path->nodes[0]; |
| 7928 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7929 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7930 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7931 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7932 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7933 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7934 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7935 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7936 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7937 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7938 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7939 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7940 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7941 | |
| 7942 | /* |
| 7943 | * we only want to have 32k of ram per block group for keeping |
| 7944 | * track of free space, and if we pass 1/2 of that we want to |
| 7945 | * start converting things over to using bitmaps |
| 7946 | */ |
| 7947 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7948 | sizeof(struct btrfs_free_space); |
| 7949 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7950 | read_extent_buffer(leaf, &cache->item, |
| 7951 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7952 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7953 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7954 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7955 | key.objectid = found_key.objectid + found_key.offset; |
| 7956 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7957 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7958 | cache->sectorsize = root->sectorsize; |
| 7959 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7960 | /* |
| 7961 | * check for two cases, either we are full, and therefore |
| 7962 | * don't need to bother with the caching work since we won't |
| 7963 | * find any space, or we are empty, and we can just add all |
| 7964 | * the space in and be done with it. This saves us _alot_ of |
| 7965 | * time, particularly in the full case. |
| 7966 | */ |
| 7967 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7968 | exclude_super_stripes(root, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7969 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7970 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7971 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7972 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7973 | exclude_super_stripes(root, cache); |
| 7974 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7975 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7976 | add_new_free_space(cache, root->fs_info, |
| 7977 | found_key.objectid, |
| 7978 | found_key.objectid + |
| 7979 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7980 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7981 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7982 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7983 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7984 | btrfs_block_group_used(&cache->item), |
| 7985 | &space_info); |
| 7986 | BUG_ON(ret); |
| 7987 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7988 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7989 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7990 | spin_unlock(&cache->space_info->lock); |
| 7991 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7992 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7993 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7994 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7995 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7996 | |
| 7997 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7998 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7999 | set_block_group_ro(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8000 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8001 | |
| 8002 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 8003 | if (!(get_alloc_profile(root, space_info->flags) & |
| 8004 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 8005 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8006 | BTRFS_BLOCK_GROUP_DUP))) |
| 8007 | continue; |
| 8008 | /* |
| 8009 | * avoid allocating from un-mirrored block group if there are |
| 8010 | * mirrored block groups. |
| 8011 | */ |
| 8012 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8013 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8014 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8015 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8016 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8017 | |
| 8018 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8019 | ret = 0; |
| 8020 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8021 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8022 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8023 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8024 | |
| 8025 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8026 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8027 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8028 | u64 size) |
| 8029 | { |
| 8030 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8031 | struct btrfs_root *extent_root; |
| 8032 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8033 | |
| 8034 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8035 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8036 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8037 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8038 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8039 | if (!cache) |
| 8040 | return -ENOMEM; |
| 8041 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8042 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8043 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8044 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8045 | cache->sectorsize = root->sectorsize; |
| 8046 | |
| 8047 | /* |
| 8048 | * we only want to have 32k of ram per block group for keeping track |
| 8049 | * of free space, and if we pass 1/2 of that we want to start |
| 8050 | * converting things over to using bitmaps |
| 8051 | */ |
| 8052 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8053 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8054 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8055 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8056 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8057 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8058 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8059 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8060 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8061 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 8062 | cache->flags = type; |
| 8063 | btrfs_set_block_group_flags(&cache->item, type); |
| 8064 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8065 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8066 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8067 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8068 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8069 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8070 | chunk_offset + size); |
| 8071 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8072 | free_excluded_extents(root, cache); |
| 8073 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8074 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8075 | &cache->space_info); |
| 8076 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8077 | |
| 8078 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8079 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8080 | spin_unlock(&cache->space_info->lock); |
| 8081 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8082 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8083 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8084 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8085 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8086 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8087 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 8088 | sizeof(cache->item)); |
| 8089 | BUG_ON(ret); |
| 8090 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8091 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8092 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8093 | return 0; |
| 8094 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8095 | |
| 8096 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8097 | struct btrfs_root *root, u64 group_start) |
| 8098 | { |
| 8099 | struct btrfs_path *path; |
| 8100 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8101 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8102 | struct btrfs_key key; |
| 8103 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8104 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8105 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8106 | root = root->fs_info->extent_root; |
| 8107 | |
| 8108 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8109 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8110 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8111 | |
| 8112 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8113 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 8114 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8115 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8116 | factor = 2; |
| 8117 | else |
| 8118 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8119 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8120 | /* make sure this block group isn't part of an allocation cluster */ |
| 8121 | cluster = &root->fs_info->data_alloc_cluster; |
| 8122 | spin_lock(&cluster->refill_lock); |
| 8123 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8124 | spin_unlock(&cluster->refill_lock); |
| 8125 | |
| 8126 | /* |
| 8127 | * make sure this block group isn't part of a metadata |
| 8128 | * allocation cluster |
| 8129 | */ |
| 8130 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8131 | spin_lock(&cluster->refill_lock); |
| 8132 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8133 | spin_unlock(&cluster->refill_lock); |
| 8134 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8135 | path = btrfs_alloc_path(); |
| 8136 | BUG_ON(!path); |
| 8137 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8138 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8139 | rb_erase(&block_group->cache_node, |
| 8140 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8141 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8142 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8143 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8144 | /* |
| 8145 | * we must use list_del_init so people can check to see if they |
| 8146 | * are still on the list after taking the semaphore |
| 8147 | */ |
| 8148 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8149 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8150 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8151 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8152 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8153 | |
| 8154 | btrfs_remove_free_space_cache(block_group); |
| 8155 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8156 | spin_lock(&block_group->space_info->lock); |
| 8157 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8158 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8159 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8160 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8161 | |
| 8162 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8163 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8164 | btrfs_put_block_group(block_group); |
| 8165 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8166 | |
| 8167 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8168 | if (ret > 0) |
| 8169 | ret = -EIO; |
| 8170 | if (ret < 0) |
| 8171 | goto out; |
| 8172 | |
| 8173 | ret = btrfs_del_item(trans, root, path); |
| 8174 | out: |
| 8175 | btrfs_free_path(path); |
| 8176 | return ret; |
| 8177 | } |