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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 25 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 26 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 27 | #include "ctree.h" |
| 28 | #include "disk-io.h" |
| 29 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 30 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 32 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 33 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 34 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 35 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 36 | struct btrfs_root *root, |
| 37 | u64 bytenr, u64 num_bytes, int alloc, |
| 38 | int mark_free); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 39 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 40 | u64 num_bytes, int reserve); |
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 pin_down_bytes(struct btrfs_trans_handle *trans, |
| 64 | struct btrfs_root *root, |
| 65 | struct btrfs_path *path, |
| 66 | u64 bytenr, u64 num_bytes, |
| 67 | int is_data, int reserved, |
| 68 | struct extent_buffer **must_clean); |
| 69 | static int find_next_key(struct btrfs_path *path, int level, |
| 70 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 71 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 72 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 73 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 74 | static noinline int |
| 75 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 76 | { |
| 77 | smp_mb(); |
| 78 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 79 | } |
| 80 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 81 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 82 | { |
| 83 | return (cache->flags & bits) == bits; |
| 84 | } |
| 85 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 86 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 87 | { |
| 88 | atomic_inc(&cache->count); |
| 89 | } |
| 90 | |
| 91 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 92 | { |
| 93 | if (atomic_dec_and_test(&cache->count)) |
| 94 | kfree(cache); |
| 95 | } |
| 96 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 97 | /* |
| 98 | * this adds the block group to the fs_info rb tree for the block group |
| 99 | * cache |
| 100 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 101 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 102 | struct btrfs_block_group_cache *block_group) |
| 103 | { |
| 104 | struct rb_node **p; |
| 105 | struct rb_node *parent = NULL; |
| 106 | struct btrfs_block_group_cache *cache; |
| 107 | |
| 108 | spin_lock(&info->block_group_cache_lock); |
| 109 | p = &info->block_group_cache_tree.rb_node; |
| 110 | |
| 111 | while (*p) { |
| 112 | parent = *p; |
| 113 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 114 | cache_node); |
| 115 | if (block_group->key.objectid < cache->key.objectid) { |
| 116 | p = &(*p)->rb_left; |
| 117 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 118 | p = &(*p)->rb_right; |
| 119 | } else { |
| 120 | spin_unlock(&info->block_group_cache_lock); |
| 121 | return -EEXIST; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | rb_link_node(&block_group->cache_node, parent, p); |
| 126 | rb_insert_color(&block_group->cache_node, |
| 127 | &info->block_group_cache_tree); |
| 128 | spin_unlock(&info->block_group_cache_lock); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * This will return the block group at or after bytenr if contains is 0, else |
| 135 | * it will return the block group that contains the bytenr |
| 136 | */ |
| 137 | static struct btrfs_block_group_cache * |
| 138 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 139 | int contains) |
| 140 | { |
| 141 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 142 | struct rb_node *n; |
| 143 | u64 end, start; |
| 144 | |
| 145 | spin_lock(&info->block_group_cache_lock); |
| 146 | n = info->block_group_cache_tree.rb_node; |
| 147 | |
| 148 | while (n) { |
| 149 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 150 | cache_node); |
| 151 | end = cache->key.objectid + cache->key.offset - 1; |
| 152 | start = cache->key.objectid; |
| 153 | |
| 154 | if (bytenr < start) { |
| 155 | if (!contains && (!ret || start < ret->key.objectid)) |
| 156 | ret = cache; |
| 157 | n = n->rb_left; |
| 158 | } else if (bytenr > start) { |
| 159 | if (contains && bytenr <= end) { |
| 160 | ret = cache; |
| 161 | break; |
| 162 | } |
| 163 | n = n->rb_right; |
| 164 | } else { |
| 165 | ret = cache; |
| 166 | break; |
| 167 | } |
| 168 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 169 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 170 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 171 | spin_unlock(&info->block_group_cache_lock); |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 176 | static int add_excluded_extent(struct btrfs_root *root, |
| 177 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 178 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 179 | u64 end = start + num_bytes - 1; |
| 180 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 181 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 182 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 183 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 184 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 187 | static void free_excluded_extents(struct btrfs_root *root, |
| 188 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 189 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 190 | u64 start, end; |
| 191 | |
| 192 | start = cache->key.objectid; |
| 193 | end = start + cache->key.offset - 1; |
| 194 | |
| 195 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 196 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 197 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 198 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 199 | } |
| 200 | |
| 201 | static int exclude_super_stripes(struct btrfs_root *root, |
| 202 | struct btrfs_block_group_cache *cache) |
| 203 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 204 | u64 bytenr; |
| 205 | u64 *logical; |
| 206 | int stripe_len; |
| 207 | int i, nr, ret; |
| 208 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 209 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 210 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 211 | cache->bytes_super += stripe_len; |
| 212 | ret = add_excluded_extent(root, cache->key.objectid, |
| 213 | stripe_len); |
| 214 | BUG_ON(ret); |
| 215 | } |
| 216 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 217 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 218 | bytenr = btrfs_sb_offset(i); |
| 219 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 220 | cache->key.objectid, bytenr, |
| 221 | 0, &logical, &nr, &stripe_len); |
| 222 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 223 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 224 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 225 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 226 | ret = add_excluded_extent(root, logical[nr], |
| 227 | stripe_len); |
| 228 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 229 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 230 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 231 | kfree(logical); |
| 232 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 236 | static struct btrfs_caching_control * |
| 237 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 238 | { |
| 239 | struct btrfs_caching_control *ctl; |
| 240 | |
| 241 | spin_lock(&cache->lock); |
| 242 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 243 | spin_unlock(&cache->lock); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | ctl = cache->caching_ctl; |
| 248 | atomic_inc(&ctl->count); |
| 249 | spin_unlock(&cache->lock); |
| 250 | return ctl; |
| 251 | } |
| 252 | |
| 253 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 254 | { |
| 255 | if (atomic_dec_and_test(&ctl->count)) |
| 256 | kfree(ctl); |
| 257 | } |
| 258 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 259 | /* |
| 260 | * this is only called by cache_block_group, since we could have freed extents |
| 261 | * we need to check the pinned_extents for any extents that can't be used yet |
| 262 | * since their free space will be released as soon as the transaction commits. |
| 263 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 264 | 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] | 265 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 266 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 267 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 268 | int ret; |
| 269 | |
| 270 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 271 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 273 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | if (ret) |
| 275 | break; |
| 276 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 277 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 278 | start = extent_end + 1; |
| 279 | } else if (extent_start > start && extent_start < end) { |
| 280 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 281 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 282 | ret = btrfs_add_free_space(block_group, start, |
| 283 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 284 | BUG_ON(ret); |
| 285 | start = extent_end + 1; |
| 286 | } else { |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (start < end) { |
| 292 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 293 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 294 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 295 | BUG_ON(ret); |
| 296 | } |
| 297 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 298 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 301 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 302 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 303 | struct btrfs_block_group_cache *block_group = data; |
| 304 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 305 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 306 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 307 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 308 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 309 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 310 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 311 | u64 last = 0; |
| 312 | u32 nritems; |
| 313 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 314 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 315 | path = btrfs_alloc_path(); |
| 316 | if (!path) |
| 317 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 318 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 319 | exclude_super_stripes(extent_root, block_group); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 320 | spin_lock(&block_group->space_info->lock); |
| 321 | block_group->space_info->bytes_super += block_group->bytes_super; |
| 322 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 323 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 324 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 325 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 326 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 327 | * We don't want to deadlock with somebody trying to allocate a new |
| 328 | * extent for the extent root while also trying to search the extent |
| 329 | * root to add free space. So we skip locking and search the commit |
| 330 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 331 | */ |
| 332 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 333 | path->search_commit_root = 1; |
| 334 | path->reada = 2; |
| 335 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 336 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 337 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 338 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 339 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 340 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 341 | /* need to make sure the commit_root doesn't disappear */ |
| 342 | down_read(&fs_info->extent_commit_sem); |
| 343 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 344 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 345 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 346 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 347 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 348 | leaf = path->nodes[0]; |
| 349 | nritems = btrfs_header_nritems(leaf); |
| 350 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 351 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 352 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 353 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 354 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 355 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 356 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 357 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 358 | if (path->slots[0] < nritems) { |
| 359 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 360 | } else { |
| 361 | ret = find_next_key(path, 0, &key); |
| 362 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 363 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 364 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 365 | caching_ctl->progress = last; |
| 366 | btrfs_release_path(extent_root, path); |
| 367 | up_read(&fs_info->extent_commit_sem); |
| 368 | mutex_unlock(&caching_ctl->mutex); |
| 369 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 370 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 371 | else |
| 372 | cond_resched(); |
| 373 | goto again; |
| 374 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 375 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 376 | if (key.objectid < block_group->key.objectid) { |
| 377 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 378 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 379 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 380 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 381 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 382 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 383 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 384 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 385 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 386 | total_found += add_new_free_space(block_group, |
| 387 | fs_info, last, |
| 388 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 389 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 390 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 391 | if (total_found > (1024 * 1024 * 2)) { |
| 392 | total_found = 0; |
| 393 | wake_up(&caching_ctl->wait); |
| 394 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 395 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 396 | path->slots[0]++; |
| 397 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 398 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 399 | |
| 400 | total_found += add_new_free_space(block_group, fs_info, last, |
| 401 | block_group->key.objectid + |
| 402 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 403 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 404 | |
| 405 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 406 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 407 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 408 | spin_unlock(&block_group->lock); |
| 409 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 410 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 411 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 412 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 413 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 414 | free_excluded_extents(extent_root, block_group); |
| 415 | |
| 416 | mutex_unlock(&caching_ctl->mutex); |
| 417 | wake_up(&caching_ctl->wait); |
| 418 | |
| 419 | put_caching_control(caching_ctl); |
| 420 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 421 | btrfs_put_block_group(block_group); |
| 422 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int cache_block_group(struct btrfs_block_group_cache *cache) |
| 427 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 428 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 429 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 430 | struct task_struct *tsk; |
| 431 | int ret = 0; |
| 432 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 433 | smp_mb(); |
| 434 | if (cache->cached != BTRFS_CACHE_NO) |
| 435 | return 0; |
| 436 | |
| 437 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); |
| 438 | BUG_ON(!caching_ctl); |
| 439 | |
| 440 | INIT_LIST_HEAD(&caching_ctl->list); |
| 441 | mutex_init(&caching_ctl->mutex); |
| 442 | init_waitqueue_head(&caching_ctl->wait); |
| 443 | caching_ctl->block_group = cache; |
| 444 | caching_ctl->progress = cache->key.objectid; |
| 445 | /* one for caching kthread, one for caching block group list */ |
| 446 | atomic_set(&caching_ctl->count, 2); |
| 447 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 448 | spin_lock(&cache->lock); |
| 449 | if (cache->cached != BTRFS_CACHE_NO) { |
| 450 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 451 | kfree(caching_ctl); |
| 452 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 453 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 454 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 455 | cache->cached = BTRFS_CACHE_STARTED; |
| 456 | spin_unlock(&cache->lock); |
| 457 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 458 | down_write(&fs_info->extent_commit_sem); |
| 459 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 460 | up_write(&fs_info->extent_commit_sem); |
| 461 | |
| 462 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 463 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 464 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 465 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 466 | cache->key.objectid); |
| 467 | if (IS_ERR(tsk)) { |
| 468 | ret = PTR_ERR(tsk); |
| 469 | printk(KERN_ERR "error running thread %d\n", ret); |
| 470 | BUG(); |
| 471 | } |
| 472 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 473 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 476 | /* |
| 477 | * return the block group that starts at or after bytenr |
| 478 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 479 | static struct btrfs_block_group_cache * |
| 480 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
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 | struct btrfs_block_group_cache *cache; |
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 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 485 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 486 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 487 | } |
| 488 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 489 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 490 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 491 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 492 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 493 | struct btrfs_fs_info *info, |
| 494 | u64 bytenr) |
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 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 497 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 498 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 499 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 500 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 501 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 502 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 503 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 504 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 505 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 506 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 507 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 508 | |
| 509 | rcu_read_lock(); |
| 510 | list_for_each_entry_rcu(found, head, list) { |
| 511 | if (found->flags == flags) { |
| 512 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 513 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 514 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 515 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 516 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 517 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 520 | /* |
| 521 | * after adding space to the filesystem, we need to clear the full flags |
| 522 | * on all the space infos. |
| 523 | */ |
| 524 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 525 | { |
| 526 | struct list_head *head = &info->space_info; |
| 527 | struct btrfs_space_info *found; |
| 528 | |
| 529 | rcu_read_lock(); |
| 530 | list_for_each_entry_rcu(found, head, list) |
| 531 | found->full = 0; |
| 532 | rcu_read_unlock(); |
| 533 | } |
| 534 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 535 | static u64 div_factor(u64 num, int factor) |
| 536 | { |
| 537 | if (factor == 10) |
| 538 | return num; |
| 539 | num *= factor; |
| 540 | do_div(num, 10); |
| 541 | return num; |
| 542 | } |
| 543 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 544 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 545 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 546 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 547 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 548 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 549 | u64 last = max(search_hint, search_start); |
| 550 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 551 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 552 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 553 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 554 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 555 | while (1) { |
| 556 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 557 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 558 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 559 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 560 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 561 | last = cache->key.objectid + cache->key.offset; |
| 562 | used = btrfs_block_group_used(&cache->item); |
| 563 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 564 | if ((full_search || !cache->ro) && |
| 565 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 566 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 567 | div_factor(cache->key.offset, factor)) { |
| 568 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 569 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 570 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 571 | goto found; |
| 572 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 573 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 574 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 575 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 576 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 577 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 578 | if (!wrapped) { |
| 579 | last = search_start; |
| 580 | wrapped = 1; |
| 581 | goto again; |
| 582 | } |
| 583 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 584 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 585 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 586 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 587 | goto again; |
| 588 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 589 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 590 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 591 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 592 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 593 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 594 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 595 | { |
| 596 | int ret; |
| 597 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 598 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 599 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 600 | path = btrfs_alloc_path(); |
| 601 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 602 | key.objectid = start; |
| 603 | key.offset = len; |
| 604 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 605 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 606 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 607 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 608 | return ret; |
| 609 | } |
| 610 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 611 | /* |
| 612 | * Back reference rules. Back refs have three main goals: |
| 613 | * |
| 614 | * 1) differentiate between all holders of references to an extent so that |
| 615 | * when a reference is dropped we can make sure it was a valid reference |
| 616 | * before freeing the extent. |
| 617 | * |
| 618 | * 2) Provide enough information to quickly find the holders of an extent |
| 619 | * if we notice a given block is corrupted or bad. |
| 620 | * |
| 621 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 622 | * maintenance. This is actually the same as #2, but with a slightly |
| 623 | * different use case. |
| 624 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 625 | * There are two kinds of back refs. The implicit back refs is optimized |
| 626 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 627 | * back refs of this kind provide information about the block's owner tree |
| 628 | * and the pointer's key. These information allow us to find the block by |
| 629 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 630 | * referenced by their owner trees. The location of tree block is recorded |
| 631 | * in the back refs. Actually the full back refs is generic, and can be |
| 632 | * used in all cases the implicit back refs is used. The major shortcoming |
| 633 | * of the full back refs is its overhead. Every time a tree block gets |
| 634 | * COWed, we have to update back refs entry for all pointers in it. |
| 635 | * |
| 636 | * For a newly allocated tree block, we use implicit back refs for |
| 637 | * pointers in it. This means most tree related operations only involve |
| 638 | * implicit back refs. For a tree block created in old transaction, the |
| 639 | * only way to drop a reference to it is COW it. So we can detect the |
| 640 | * event that tree block loses its owner tree's reference and do the |
| 641 | * back refs conversion. |
| 642 | * |
| 643 | * When a tree block is COW'd through a tree, there are four cases: |
| 644 | * |
| 645 | * The reference count of the block is one and the tree is the block's |
| 646 | * owner tree. Nothing to do in this case. |
| 647 | * |
| 648 | * The reference count of the block is one and the tree is not the |
| 649 | * block's owner tree. In this case, full back refs is used for pointers |
| 650 | * in the block. Remove these full back refs, add implicit back refs for |
| 651 | * every pointers in the new block. |
| 652 | * |
| 653 | * The reference count of the block is greater than one and the tree is |
| 654 | * the block's owner tree. In this case, implicit back refs is used for |
| 655 | * pointers in the block. Add full back refs for every pointers in the |
| 656 | * block, increase lower level extents' reference counts. The original |
| 657 | * implicit back refs are entailed to the new block. |
| 658 | * |
| 659 | * The reference count of the block is greater than one and the tree is |
| 660 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 661 | * the new block, increase lower level extents' reference count. |
| 662 | * |
| 663 | * Back Reference Key composing: |
| 664 | * |
| 665 | * The key objectid corresponds to the first byte in the extent, |
| 666 | * The key type is used to differentiate between types of back refs. |
| 667 | * There are different meanings of the key offset for different types |
| 668 | * of back refs. |
| 669 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 670 | * File extents can be referenced by: |
| 671 | * |
| 672 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 673 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 674 | * - different offsets inside a file (bookend extents in file.c) |
| 675 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 676 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 677 | * |
| 678 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 679 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 680 | * - original offset in the file |
| 681 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 682 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 683 | * The key offset for the implicit back refs is hash of the first |
| 684 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 685 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 686 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 687 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 688 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 689 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 690 | * The key offset for the implicit back refs is the first byte of |
| 691 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 692 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 693 | * When a file extent is allocated, The implicit back refs is used. |
| 694 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 695 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 696 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 697 | * |
| 698 | * When a file extent is removed file truncation, we find the |
| 699 | * corresponding implicit back refs and check the following fields: |
| 700 | * |
| 701 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 702 | * |
| 703 | * Btree extents can be referenced by: |
| 704 | * |
| 705 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 706 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 707 | * Both the implicit back refs and the full back refs for tree blocks |
| 708 | * only consist of key. The key offset for the implicit back refs is |
| 709 | * objectid of block's owner tree. The key offset for the full back refs |
| 710 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 711 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 712 | * When implicit back refs is used, information about the lowest key and |
| 713 | * level of the tree block are required. These information are stored in |
| 714 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 715 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 716 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 717 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 718 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 719 | struct btrfs_root *root, |
| 720 | struct btrfs_path *path, |
| 721 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 722 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 723 | struct btrfs_extent_item *item; |
| 724 | struct btrfs_extent_item_v0 *ei0; |
| 725 | struct btrfs_extent_ref_v0 *ref0; |
| 726 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 727 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 728 | struct btrfs_key key; |
| 729 | struct btrfs_key found_key; |
| 730 | u32 new_size = sizeof(*item); |
| 731 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 732 | int ret; |
| 733 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 734 | leaf = path->nodes[0]; |
| 735 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 736 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 737 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 738 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 739 | struct btrfs_extent_item_v0); |
| 740 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 741 | |
| 742 | if (owner == (u64)-1) { |
| 743 | while (1) { |
| 744 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 745 | ret = btrfs_next_leaf(root, path); |
| 746 | if (ret < 0) |
| 747 | return ret; |
| 748 | BUG_ON(ret > 0); |
| 749 | leaf = path->nodes[0]; |
| 750 | } |
| 751 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 752 | path->slots[0]); |
| 753 | BUG_ON(key.objectid != found_key.objectid); |
| 754 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 755 | path->slots[0]++; |
| 756 | continue; |
| 757 | } |
| 758 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 759 | struct btrfs_extent_ref_v0); |
| 760 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | btrfs_release_path(root, path); |
| 765 | |
| 766 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 767 | new_size += sizeof(*bi); |
| 768 | |
| 769 | new_size -= sizeof(*ei0); |
| 770 | ret = btrfs_search_slot(trans, root, &key, path, |
| 771 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 772 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 773 | return ret; |
| 774 | BUG_ON(ret); |
| 775 | |
| 776 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 777 | BUG_ON(ret); |
| 778 | |
| 779 | leaf = path->nodes[0]; |
| 780 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 781 | btrfs_set_extent_refs(leaf, item, refs); |
| 782 | /* FIXME: get real generation */ |
| 783 | btrfs_set_extent_generation(leaf, item, 0); |
| 784 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 785 | btrfs_set_extent_flags(leaf, item, |
| 786 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 787 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 788 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 789 | /* FIXME: get first key of the block */ |
| 790 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 791 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 792 | } else { |
| 793 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 794 | } |
| 795 | btrfs_mark_buffer_dirty(leaf); |
| 796 | return 0; |
| 797 | } |
| 798 | #endif |
| 799 | |
| 800 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 801 | { |
| 802 | u32 high_crc = ~(u32)0; |
| 803 | u32 low_crc = ~(u32)0; |
| 804 | __le64 lenum; |
| 805 | |
| 806 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 807 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 808 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 809 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 810 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 811 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 812 | |
| 813 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 814 | } |
| 815 | |
| 816 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 817 | struct btrfs_extent_data_ref *ref) |
| 818 | { |
| 819 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 820 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 821 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 822 | } |
| 823 | |
| 824 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 825 | struct btrfs_extent_data_ref *ref, |
| 826 | u64 root_objectid, u64 owner, u64 offset) |
| 827 | { |
| 828 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 829 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 830 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 831 | return 0; |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 836 | struct btrfs_root *root, |
| 837 | struct btrfs_path *path, |
| 838 | u64 bytenr, u64 parent, |
| 839 | u64 root_objectid, |
| 840 | u64 owner, u64 offset) |
| 841 | { |
| 842 | struct btrfs_key key; |
| 843 | struct btrfs_extent_data_ref *ref; |
| 844 | struct extent_buffer *leaf; |
| 845 | u32 nritems; |
| 846 | int ret; |
| 847 | int recow; |
| 848 | int err = -ENOENT; |
| 849 | |
| 850 | key.objectid = bytenr; |
| 851 | if (parent) { |
| 852 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 853 | key.offset = parent; |
| 854 | } else { |
| 855 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 856 | key.offset = hash_extent_data_ref(root_objectid, |
| 857 | owner, offset); |
| 858 | } |
| 859 | again: |
| 860 | recow = 0; |
| 861 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 862 | if (ret < 0) { |
| 863 | err = ret; |
| 864 | goto fail; |
| 865 | } |
| 866 | |
| 867 | if (parent) { |
| 868 | if (!ret) |
| 869 | return 0; |
| 870 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 871 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 872 | btrfs_release_path(root, path); |
| 873 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 874 | if (ret < 0) { |
| 875 | err = ret; |
| 876 | goto fail; |
| 877 | } |
| 878 | if (!ret) |
| 879 | return 0; |
| 880 | #endif |
| 881 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 885 | nritems = btrfs_header_nritems(leaf); |
| 886 | while (1) { |
| 887 | if (path->slots[0] >= nritems) { |
| 888 | ret = btrfs_next_leaf(root, path); |
| 889 | if (ret < 0) |
| 890 | err = ret; |
| 891 | if (ret) |
| 892 | goto fail; |
| 893 | |
| 894 | leaf = path->nodes[0]; |
| 895 | nritems = btrfs_header_nritems(leaf); |
| 896 | recow = 1; |
| 897 | } |
| 898 | |
| 899 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 900 | if (key.objectid != bytenr || |
| 901 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 902 | goto fail; |
| 903 | |
| 904 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 905 | struct btrfs_extent_data_ref); |
| 906 | |
| 907 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 908 | owner, offset)) { |
| 909 | if (recow) { |
| 910 | btrfs_release_path(root, path); |
| 911 | goto again; |
| 912 | } |
| 913 | err = 0; |
| 914 | break; |
| 915 | } |
| 916 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 917 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | fail: |
| 919 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 920 | } |
| 921 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 922 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 923 | struct btrfs_root *root, |
| 924 | struct btrfs_path *path, |
| 925 | u64 bytenr, u64 parent, |
| 926 | u64 root_objectid, u64 owner, |
| 927 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 928 | { |
| 929 | struct btrfs_key key; |
| 930 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 931 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 932 | u32 num_refs; |
| 933 | int ret; |
| 934 | |
| 935 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 936 | if (parent) { |
| 937 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 938 | key.offset = parent; |
| 939 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 940 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 941 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 942 | key.offset = hash_extent_data_ref(root_objectid, |
| 943 | owner, offset); |
| 944 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 945 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 946 | |
| 947 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 948 | if (ret && ret != -EEXIST) |
| 949 | goto fail; |
| 950 | |
| 951 | leaf = path->nodes[0]; |
| 952 | if (parent) { |
| 953 | struct btrfs_shared_data_ref *ref; |
| 954 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 955 | struct btrfs_shared_data_ref); |
| 956 | if (ret == 0) { |
| 957 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 958 | } else { |
| 959 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 960 | num_refs += refs_to_add; |
| 961 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 962 | } |
| 963 | } else { |
| 964 | struct btrfs_extent_data_ref *ref; |
| 965 | while (ret == -EEXIST) { |
| 966 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 967 | struct btrfs_extent_data_ref); |
| 968 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 969 | owner, offset)) |
| 970 | break; |
| 971 | btrfs_release_path(root, path); |
| 972 | key.offset++; |
| 973 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 974 | size); |
| 975 | if (ret && ret != -EEXIST) |
| 976 | goto fail; |
| 977 | |
| 978 | leaf = path->nodes[0]; |
| 979 | } |
| 980 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 981 | struct btrfs_extent_data_ref); |
| 982 | if (ret == 0) { |
| 983 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 984 | root_objectid); |
| 985 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 986 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 987 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 988 | } else { |
| 989 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 990 | num_refs += refs_to_add; |
| 991 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 992 | } |
| 993 | } |
| 994 | btrfs_mark_buffer_dirty(leaf); |
| 995 | ret = 0; |
| 996 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 997 | btrfs_release_path(root, path); |
| 998 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1001 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1002 | struct btrfs_root *root, |
| 1003 | struct btrfs_path *path, |
| 1004 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1005 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1006 | struct btrfs_key key; |
| 1007 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1008 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1009 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1010 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1011 | int ret = 0; |
| 1012 | |
| 1013 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1014 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1015 | |
| 1016 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1017 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1018 | struct btrfs_extent_data_ref); |
| 1019 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1020 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1021 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1022 | struct btrfs_shared_data_ref); |
| 1023 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1024 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1025 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1026 | struct btrfs_extent_ref_v0 *ref0; |
| 1027 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1028 | struct btrfs_extent_ref_v0); |
| 1029 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1030 | #endif |
| 1031 | } else { |
| 1032 | BUG(); |
| 1033 | } |
| 1034 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1035 | BUG_ON(num_refs < refs_to_drop); |
| 1036 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1037 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1038 | if (num_refs == 0) { |
| 1039 | ret = btrfs_del_item(trans, root, path); |
| 1040 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1041 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1042 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1043 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1044 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1045 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1046 | else { |
| 1047 | struct btrfs_extent_ref_v0 *ref0; |
| 1048 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1049 | struct btrfs_extent_ref_v0); |
| 1050 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1051 | } |
| 1052 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1053 | btrfs_mark_buffer_dirty(leaf); |
| 1054 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1059 | struct btrfs_path *path, |
| 1060 | struct btrfs_extent_inline_ref *iref) |
| 1061 | { |
| 1062 | struct btrfs_key key; |
| 1063 | struct extent_buffer *leaf; |
| 1064 | struct btrfs_extent_data_ref *ref1; |
| 1065 | struct btrfs_shared_data_ref *ref2; |
| 1066 | u32 num_refs = 0; |
| 1067 | |
| 1068 | leaf = path->nodes[0]; |
| 1069 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1070 | if (iref) { |
| 1071 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1072 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1073 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1074 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1075 | } else { |
| 1076 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1077 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1078 | } |
| 1079 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1080 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1081 | struct btrfs_extent_data_ref); |
| 1082 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1083 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1084 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1085 | struct btrfs_shared_data_ref); |
| 1086 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1087 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1088 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1089 | struct btrfs_extent_ref_v0 *ref0; |
| 1090 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1091 | struct btrfs_extent_ref_v0); |
| 1092 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1093 | #endif |
| 1094 | } else { |
| 1095 | WARN_ON(1); |
| 1096 | } |
| 1097 | return num_refs; |
| 1098 | } |
| 1099 | |
| 1100 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1101 | struct btrfs_root *root, |
| 1102 | struct btrfs_path *path, |
| 1103 | u64 bytenr, u64 parent, |
| 1104 | u64 root_objectid) |
| 1105 | { |
| 1106 | struct btrfs_key key; |
| 1107 | int ret; |
| 1108 | |
| 1109 | key.objectid = bytenr; |
| 1110 | if (parent) { |
| 1111 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1112 | key.offset = parent; |
| 1113 | } else { |
| 1114 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1115 | key.offset = root_objectid; |
| 1116 | } |
| 1117 | |
| 1118 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1119 | if (ret > 0) |
| 1120 | ret = -ENOENT; |
| 1121 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1122 | if (ret == -ENOENT && parent) { |
| 1123 | btrfs_release_path(root, path); |
| 1124 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1125 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1126 | if (ret > 0) |
| 1127 | ret = -ENOENT; |
| 1128 | } |
| 1129 | #endif |
| 1130 | return ret; |
| 1131 | } |
| 1132 | |
| 1133 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1134 | struct btrfs_root *root, |
| 1135 | struct btrfs_path *path, |
| 1136 | u64 bytenr, u64 parent, |
| 1137 | u64 root_objectid) |
| 1138 | { |
| 1139 | struct btrfs_key key; |
| 1140 | int ret; |
| 1141 | |
| 1142 | key.objectid = bytenr; |
| 1143 | if (parent) { |
| 1144 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1145 | key.offset = parent; |
| 1146 | } else { |
| 1147 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1148 | key.offset = root_objectid; |
| 1149 | } |
| 1150 | |
| 1151 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1152 | btrfs_release_path(root, path); |
| 1153 | return ret; |
| 1154 | } |
| 1155 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1156 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1157 | { |
| 1158 | int type; |
| 1159 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1160 | if (parent > 0) |
| 1161 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1162 | else |
| 1163 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1164 | } else { |
| 1165 | if (parent > 0) |
| 1166 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1167 | else |
| 1168 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1169 | } |
| 1170 | return type; |
| 1171 | } |
| 1172 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1173 | static int find_next_key(struct btrfs_path *path, int level, |
| 1174 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1175 | |
| 1176 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1177 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1178 | if (!path->nodes[level]) |
| 1179 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1180 | if (path->slots[level] + 1 >= |
| 1181 | btrfs_header_nritems(path->nodes[level])) |
| 1182 | continue; |
| 1183 | if (level == 0) |
| 1184 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1185 | path->slots[level] + 1); |
| 1186 | else |
| 1187 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1188 | path->slots[level] + 1); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1196 | * to the address of inline back ref, and 0 is returned. |
| 1197 | * |
| 1198 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1199 | * should be inserted, and -ENOENT is returned. |
| 1200 | * |
| 1201 | * if insert is true and there are too many inline back refs, the path |
| 1202 | * points to the extent item, and -EAGAIN is returned. |
| 1203 | * |
| 1204 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1205 | * items in the tree are ordered. |
| 1206 | */ |
| 1207 | static noinline_for_stack |
| 1208 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1209 | struct btrfs_root *root, |
| 1210 | struct btrfs_path *path, |
| 1211 | struct btrfs_extent_inline_ref **ref_ret, |
| 1212 | u64 bytenr, u64 num_bytes, |
| 1213 | u64 parent, u64 root_objectid, |
| 1214 | u64 owner, u64 offset, int insert) |
| 1215 | { |
| 1216 | struct btrfs_key key; |
| 1217 | struct extent_buffer *leaf; |
| 1218 | struct btrfs_extent_item *ei; |
| 1219 | struct btrfs_extent_inline_ref *iref; |
| 1220 | u64 flags; |
| 1221 | u64 item_size; |
| 1222 | unsigned long ptr; |
| 1223 | unsigned long end; |
| 1224 | int extra_size; |
| 1225 | int type; |
| 1226 | int want; |
| 1227 | int ret; |
| 1228 | int err = 0; |
| 1229 | |
| 1230 | key.objectid = bytenr; |
| 1231 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1232 | key.offset = num_bytes; |
| 1233 | |
| 1234 | want = extent_ref_type(parent, owner); |
| 1235 | if (insert) { |
| 1236 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1237 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1238 | } else |
| 1239 | extra_size = -1; |
| 1240 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1241 | if (ret < 0) { |
| 1242 | err = ret; |
| 1243 | goto out; |
| 1244 | } |
| 1245 | BUG_ON(ret); |
| 1246 | |
| 1247 | leaf = path->nodes[0]; |
| 1248 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1249 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1250 | if (item_size < sizeof(*ei)) { |
| 1251 | if (!insert) { |
| 1252 | err = -ENOENT; |
| 1253 | goto out; |
| 1254 | } |
| 1255 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1256 | extra_size); |
| 1257 | if (ret < 0) { |
| 1258 | err = ret; |
| 1259 | goto out; |
| 1260 | } |
| 1261 | leaf = path->nodes[0]; |
| 1262 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1263 | } |
| 1264 | #endif |
| 1265 | BUG_ON(item_size < sizeof(*ei)); |
| 1266 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1267 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1268 | flags = btrfs_extent_flags(leaf, ei); |
| 1269 | |
| 1270 | ptr = (unsigned long)(ei + 1); |
| 1271 | end = (unsigned long)ei + item_size; |
| 1272 | |
| 1273 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1274 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1275 | BUG_ON(ptr > end); |
| 1276 | } else { |
| 1277 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1278 | } |
| 1279 | |
| 1280 | err = -ENOENT; |
| 1281 | while (1) { |
| 1282 | if (ptr >= end) { |
| 1283 | WARN_ON(ptr > end); |
| 1284 | break; |
| 1285 | } |
| 1286 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1287 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1288 | if (want < type) |
| 1289 | break; |
| 1290 | if (want > type) { |
| 1291 | ptr += btrfs_extent_inline_ref_size(type); |
| 1292 | continue; |
| 1293 | } |
| 1294 | |
| 1295 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1296 | struct btrfs_extent_data_ref *dref; |
| 1297 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1298 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1299 | owner, offset)) { |
| 1300 | err = 0; |
| 1301 | break; |
| 1302 | } |
| 1303 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1304 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1305 | break; |
| 1306 | } else { |
| 1307 | u64 ref_offset; |
| 1308 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1309 | if (parent > 0) { |
| 1310 | if (parent == ref_offset) { |
| 1311 | err = 0; |
| 1312 | break; |
| 1313 | } |
| 1314 | if (ref_offset < parent) |
| 1315 | break; |
| 1316 | } else { |
| 1317 | if (root_objectid == ref_offset) { |
| 1318 | err = 0; |
| 1319 | break; |
| 1320 | } |
| 1321 | if (ref_offset < root_objectid) |
| 1322 | break; |
| 1323 | } |
| 1324 | } |
| 1325 | ptr += btrfs_extent_inline_ref_size(type); |
| 1326 | } |
| 1327 | if (err == -ENOENT && insert) { |
| 1328 | if (item_size + extra_size >= |
| 1329 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1330 | err = -EAGAIN; |
| 1331 | goto out; |
| 1332 | } |
| 1333 | /* |
| 1334 | * To add new inline back ref, we have to make sure |
| 1335 | * there is no corresponding back ref item. |
| 1336 | * For simplicity, we just do not add new inline back |
| 1337 | * ref if there is any kind of item for this block |
| 1338 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1339 | if (find_next_key(path, 0, &key) == 0 && |
| 1340 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1341 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1342 | err = -EAGAIN; |
| 1343 | goto out; |
| 1344 | } |
| 1345 | } |
| 1346 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1347 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1348 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1349 | path->keep_locks = 0; |
| 1350 | btrfs_unlock_up_safe(path, 1); |
| 1351 | } |
| 1352 | return err; |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * helper to add new inline back ref |
| 1357 | */ |
| 1358 | static noinline_for_stack |
| 1359 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1360 | struct btrfs_root *root, |
| 1361 | struct btrfs_path *path, |
| 1362 | struct btrfs_extent_inline_ref *iref, |
| 1363 | u64 parent, u64 root_objectid, |
| 1364 | u64 owner, u64 offset, int refs_to_add, |
| 1365 | struct btrfs_delayed_extent_op *extent_op) |
| 1366 | { |
| 1367 | struct extent_buffer *leaf; |
| 1368 | struct btrfs_extent_item *ei; |
| 1369 | unsigned long ptr; |
| 1370 | unsigned long end; |
| 1371 | unsigned long item_offset; |
| 1372 | u64 refs; |
| 1373 | int size; |
| 1374 | int type; |
| 1375 | int ret; |
| 1376 | |
| 1377 | leaf = path->nodes[0]; |
| 1378 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1379 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1380 | |
| 1381 | type = extent_ref_type(parent, owner); |
| 1382 | size = btrfs_extent_inline_ref_size(type); |
| 1383 | |
| 1384 | ret = btrfs_extend_item(trans, root, path, size); |
| 1385 | BUG_ON(ret); |
| 1386 | |
| 1387 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1388 | refs = btrfs_extent_refs(leaf, ei); |
| 1389 | refs += refs_to_add; |
| 1390 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1391 | if (extent_op) |
| 1392 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1393 | |
| 1394 | ptr = (unsigned long)ei + item_offset; |
| 1395 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1396 | if (ptr < end - size) |
| 1397 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1398 | end - size - ptr); |
| 1399 | |
| 1400 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1401 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1402 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1403 | struct btrfs_extent_data_ref *dref; |
| 1404 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1405 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1406 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1407 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1408 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1409 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1410 | struct btrfs_shared_data_ref *sref; |
| 1411 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1412 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1413 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1414 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1415 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1416 | } else { |
| 1417 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1418 | } |
| 1419 | btrfs_mark_buffer_dirty(leaf); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1424 | struct btrfs_root *root, |
| 1425 | struct btrfs_path *path, |
| 1426 | struct btrfs_extent_inline_ref **ref_ret, |
| 1427 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1428 | u64 root_objectid, u64 owner, u64 offset) |
| 1429 | { |
| 1430 | int ret; |
| 1431 | |
| 1432 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1433 | bytenr, num_bytes, parent, |
| 1434 | root_objectid, owner, offset, 0); |
| 1435 | if (ret != -ENOENT) |
| 1436 | return ret; |
| 1437 | |
| 1438 | btrfs_release_path(root, path); |
| 1439 | *ref_ret = NULL; |
| 1440 | |
| 1441 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1442 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1443 | root_objectid); |
| 1444 | } else { |
| 1445 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1446 | root_objectid, owner, offset); |
| 1447 | } |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * helper to update/remove inline back ref |
| 1453 | */ |
| 1454 | static noinline_for_stack |
| 1455 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1456 | struct btrfs_root *root, |
| 1457 | struct btrfs_path *path, |
| 1458 | struct btrfs_extent_inline_ref *iref, |
| 1459 | int refs_to_mod, |
| 1460 | struct btrfs_delayed_extent_op *extent_op) |
| 1461 | { |
| 1462 | struct extent_buffer *leaf; |
| 1463 | struct btrfs_extent_item *ei; |
| 1464 | struct btrfs_extent_data_ref *dref = NULL; |
| 1465 | struct btrfs_shared_data_ref *sref = NULL; |
| 1466 | unsigned long ptr; |
| 1467 | unsigned long end; |
| 1468 | u32 item_size; |
| 1469 | int size; |
| 1470 | int type; |
| 1471 | int ret; |
| 1472 | u64 refs; |
| 1473 | |
| 1474 | leaf = path->nodes[0]; |
| 1475 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1476 | refs = btrfs_extent_refs(leaf, ei); |
| 1477 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1478 | refs += refs_to_mod; |
| 1479 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1480 | if (extent_op) |
| 1481 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1482 | |
| 1483 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1484 | |
| 1485 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1486 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1487 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1488 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1489 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1490 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1491 | } else { |
| 1492 | refs = 1; |
| 1493 | BUG_ON(refs_to_mod != -1); |
| 1494 | } |
| 1495 | |
| 1496 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1497 | refs += refs_to_mod; |
| 1498 | |
| 1499 | if (refs > 0) { |
| 1500 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1501 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1502 | else |
| 1503 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1504 | } else { |
| 1505 | size = btrfs_extent_inline_ref_size(type); |
| 1506 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1507 | ptr = (unsigned long)iref; |
| 1508 | end = (unsigned long)ei + item_size; |
| 1509 | if (ptr + size < end) |
| 1510 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1511 | end - ptr - size); |
| 1512 | item_size -= size; |
| 1513 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1514 | BUG_ON(ret); |
| 1515 | } |
| 1516 | btrfs_mark_buffer_dirty(leaf); |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static noinline_for_stack |
| 1521 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1522 | struct btrfs_root *root, |
| 1523 | struct btrfs_path *path, |
| 1524 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1525 | u64 root_objectid, u64 owner, |
| 1526 | u64 offset, int refs_to_add, |
| 1527 | struct btrfs_delayed_extent_op *extent_op) |
| 1528 | { |
| 1529 | struct btrfs_extent_inline_ref *iref; |
| 1530 | int ret; |
| 1531 | |
| 1532 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1533 | bytenr, num_bytes, parent, |
| 1534 | root_objectid, owner, offset, 1); |
| 1535 | if (ret == 0) { |
| 1536 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1537 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1538 | refs_to_add, extent_op); |
| 1539 | } else if (ret == -ENOENT) { |
| 1540 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1541 | parent, root_objectid, |
| 1542 | owner, offset, refs_to_add, |
| 1543 | extent_op); |
| 1544 | } |
| 1545 | return ret; |
| 1546 | } |
| 1547 | |
| 1548 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1549 | struct btrfs_root *root, |
| 1550 | struct btrfs_path *path, |
| 1551 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1552 | u64 owner, u64 offset, int refs_to_add) |
| 1553 | { |
| 1554 | int ret; |
| 1555 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1556 | BUG_ON(refs_to_add != 1); |
| 1557 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1558 | parent, root_objectid); |
| 1559 | } else { |
| 1560 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1561 | parent, root_objectid, |
| 1562 | owner, offset, refs_to_add); |
| 1563 | } |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1568 | struct btrfs_root *root, |
| 1569 | struct btrfs_path *path, |
| 1570 | struct btrfs_extent_inline_ref *iref, |
| 1571 | int refs_to_drop, int is_data) |
| 1572 | { |
| 1573 | int ret; |
| 1574 | |
| 1575 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1576 | if (iref) { |
| 1577 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1578 | -refs_to_drop, NULL); |
| 1579 | } else if (is_data) { |
| 1580 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1581 | } else { |
| 1582 | ret = btrfs_del_item(trans, root, path); |
| 1583 | } |
| 1584 | return ret; |
| 1585 | } |
| 1586 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1587 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1588 | u64 start, u64 len) |
| 1589 | { |
Christoph Hellwig | 746cd1e | 2009-09-12 07:35:43 +0200 | [diff] [blame] | 1590 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, |
| 1591 | DISCARD_FL_BARRIER); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1592 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1593 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1594 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1595 | u64 num_bytes) |
| 1596 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1597 | int ret; |
| 1598 | u64 map_length = num_bytes; |
| 1599 | struct btrfs_multi_bio *multi = NULL; |
| 1600 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1601 | if (!btrfs_test_opt(root, DISCARD)) |
| 1602 | return 0; |
| 1603 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1604 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1605 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1606 | bytenr, &map_length, &multi, 0); |
| 1607 | if (!ret) { |
| 1608 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1609 | int i; |
| 1610 | |
| 1611 | if (map_length > num_bytes) |
| 1612 | map_length = num_bytes; |
| 1613 | |
| 1614 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1615 | btrfs_issue_discard(stripe->dev->bdev, |
| 1616 | stripe->physical, |
| 1617 | map_length); |
| 1618 | } |
| 1619 | kfree(multi); |
| 1620 | } |
| 1621 | |
| 1622 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1623 | } |
| 1624 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1625 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1626 | struct btrfs_root *root, |
| 1627 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1628 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1629 | { |
| 1630 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1631 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1632 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1633 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1634 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1635 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1636 | parent, root_objectid, (int)owner, |
| 1637 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1638 | } else { |
| 1639 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1640 | parent, root_objectid, owner, offset, |
| 1641 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1642 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1643 | return ret; |
| 1644 | } |
| 1645 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1646 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1647 | struct btrfs_root *root, |
| 1648 | u64 bytenr, u64 num_bytes, |
| 1649 | u64 parent, u64 root_objectid, |
| 1650 | u64 owner, u64 offset, int refs_to_add, |
| 1651 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1652 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1653 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1654 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1655 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1656 | u64 refs; |
| 1657 | int ret; |
| 1658 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1659 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1660 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1661 | if (!path) |
| 1662 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1663 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1664 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1665 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1666 | /* this will setup the path even if it fails to insert the back ref */ |
| 1667 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1668 | path, bytenr, num_bytes, parent, |
| 1669 | root_objectid, owner, offset, |
| 1670 | refs_to_add, extent_op); |
| 1671 | if (ret == 0) |
| 1672 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1673 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1674 | if (ret != -EAGAIN) { |
| 1675 | err = ret; |
| 1676 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1677 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1678 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1679 | leaf = path->nodes[0]; |
| 1680 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1681 | refs = btrfs_extent_refs(leaf, item); |
| 1682 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1683 | if (extent_op) |
| 1684 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1685 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1686 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1687 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1688 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1689 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1690 | path->leave_spinning = 1; |
| 1691 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1692 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1693 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1694 | path, bytenr, parent, root_objectid, |
| 1695 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1696 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1697 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1698 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1699 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1700 | } |
| 1701 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1702 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1703 | struct btrfs_root *root, |
| 1704 | struct btrfs_delayed_ref_node *node, |
| 1705 | struct btrfs_delayed_extent_op *extent_op, |
| 1706 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1707 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1708 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1709 | struct btrfs_delayed_data_ref *ref; |
| 1710 | struct btrfs_key ins; |
| 1711 | u64 parent = 0; |
| 1712 | u64 ref_root = 0; |
| 1713 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1714 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1715 | ins.objectid = node->bytenr; |
| 1716 | ins.offset = node->num_bytes; |
| 1717 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1718 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1719 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1720 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1721 | parent = ref->parent; |
| 1722 | else |
| 1723 | ref_root = ref->root; |
| 1724 | |
| 1725 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1726 | if (extent_op) { |
| 1727 | BUG_ON(extent_op->update_key); |
| 1728 | flags |= extent_op->flags_to_set; |
| 1729 | } |
| 1730 | ret = alloc_reserved_file_extent(trans, root, |
| 1731 | parent, ref_root, flags, |
| 1732 | ref->objectid, ref->offset, |
| 1733 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1734 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1735 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1736 | node->num_bytes, parent, |
| 1737 | ref_root, ref->objectid, |
| 1738 | ref->offset, node->ref_mod, |
| 1739 | extent_op); |
| 1740 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1741 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1742 | node->num_bytes, parent, |
| 1743 | ref_root, ref->objectid, |
| 1744 | ref->offset, node->ref_mod, |
| 1745 | extent_op); |
| 1746 | } else { |
| 1747 | BUG(); |
| 1748 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1749 | return ret; |
| 1750 | } |
| 1751 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1752 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1753 | struct extent_buffer *leaf, |
| 1754 | struct btrfs_extent_item *ei) |
| 1755 | { |
| 1756 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1757 | if (extent_op->update_flags) { |
| 1758 | flags |= extent_op->flags_to_set; |
| 1759 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1760 | } |
| 1761 | |
| 1762 | if (extent_op->update_key) { |
| 1763 | struct btrfs_tree_block_info *bi; |
| 1764 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1765 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1766 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1771 | struct btrfs_root *root, |
| 1772 | struct btrfs_delayed_ref_node *node, |
| 1773 | struct btrfs_delayed_extent_op *extent_op) |
| 1774 | { |
| 1775 | struct btrfs_key key; |
| 1776 | struct btrfs_path *path; |
| 1777 | struct btrfs_extent_item *ei; |
| 1778 | struct extent_buffer *leaf; |
| 1779 | u32 item_size; |
| 1780 | int ret; |
| 1781 | int err = 0; |
| 1782 | |
| 1783 | path = btrfs_alloc_path(); |
| 1784 | if (!path) |
| 1785 | return -ENOMEM; |
| 1786 | |
| 1787 | key.objectid = node->bytenr; |
| 1788 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1789 | key.offset = node->num_bytes; |
| 1790 | |
| 1791 | path->reada = 1; |
| 1792 | path->leave_spinning = 1; |
| 1793 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1794 | path, 0, 1); |
| 1795 | if (ret < 0) { |
| 1796 | err = ret; |
| 1797 | goto out; |
| 1798 | } |
| 1799 | if (ret > 0) { |
| 1800 | err = -EIO; |
| 1801 | goto out; |
| 1802 | } |
| 1803 | |
| 1804 | leaf = path->nodes[0]; |
| 1805 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1806 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1807 | if (item_size < sizeof(*ei)) { |
| 1808 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1809 | path, (u64)-1, 0); |
| 1810 | if (ret < 0) { |
| 1811 | err = ret; |
| 1812 | goto out; |
| 1813 | } |
| 1814 | leaf = path->nodes[0]; |
| 1815 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1816 | } |
| 1817 | #endif |
| 1818 | BUG_ON(item_size < sizeof(*ei)); |
| 1819 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1820 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1821 | |
| 1822 | btrfs_mark_buffer_dirty(leaf); |
| 1823 | out: |
| 1824 | btrfs_free_path(path); |
| 1825 | return err; |
| 1826 | } |
| 1827 | |
| 1828 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1829 | struct btrfs_root *root, |
| 1830 | struct btrfs_delayed_ref_node *node, |
| 1831 | struct btrfs_delayed_extent_op *extent_op, |
| 1832 | int insert_reserved) |
| 1833 | { |
| 1834 | int ret = 0; |
| 1835 | struct btrfs_delayed_tree_ref *ref; |
| 1836 | struct btrfs_key ins; |
| 1837 | u64 parent = 0; |
| 1838 | u64 ref_root = 0; |
| 1839 | |
| 1840 | ins.objectid = node->bytenr; |
| 1841 | ins.offset = node->num_bytes; |
| 1842 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1843 | |
| 1844 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1845 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1846 | parent = ref->parent; |
| 1847 | else |
| 1848 | ref_root = ref->root; |
| 1849 | |
| 1850 | BUG_ON(node->ref_mod != 1); |
| 1851 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1852 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1853 | !extent_op->update_key); |
| 1854 | ret = alloc_reserved_tree_block(trans, root, |
| 1855 | parent, ref_root, |
| 1856 | extent_op->flags_to_set, |
| 1857 | &extent_op->key, |
| 1858 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1859 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1860 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1861 | node->num_bytes, parent, ref_root, |
| 1862 | ref->level, 0, 1, extent_op); |
| 1863 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1864 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1865 | node->num_bytes, parent, ref_root, |
| 1866 | ref->level, 0, 1, extent_op); |
| 1867 | } else { |
| 1868 | BUG(); |
| 1869 | } |
| 1870 | return ret; |
| 1871 | } |
| 1872 | |
| 1873 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1874 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1875 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1876 | struct btrfs_root *root, |
| 1877 | struct btrfs_delayed_ref_node *node, |
| 1878 | struct btrfs_delayed_extent_op *extent_op, |
| 1879 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1880 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1881 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1882 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1883 | struct btrfs_delayed_ref_head *head; |
| 1884 | /* |
| 1885 | * we've hit the end of the chain and we were supposed |
| 1886 | * to insert this extent into the tree. But, it got |
| 1887 | * deleted before we ever needed to insert it, so all |
| 1888 | * we have to do is clean up the accounting |
| 1889 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1890 | BUG_ON(extent_op); |
| 1891 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1892 | if (insert_reserved) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1893 | int mark_free = 0; |
| 1894 | struct extent_buffer *must_clean = NULL; |
| 1895 | |
| 1896 | ret = pin_down_bytes(trans, root, NULL, |
| 1897 | node->bytenr, node->num_bytes, |
| 1898 | head->is_data, 1, &must_clean); |
| 1899 | if (ret > 0) |
| 1900 | mark_free = 1; |
| 1901 | |
| 1902 | if (must_clean) { |
| 1903 | clean_tree_block(NULL, root, must_clean); |
| 1904 | btrfs_tree_unlock(must_clean); |
| 1905 | free_extent_buffer(must_clean); |
| 1906 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1907 | if (head->is_data) { |
| 1908 | ret = btrfs_del_csums(trans, root, |
| 1909 | node->bytenr, |
| 1910 | node->num_bytes); |
| 1911 | BUG_ON(ret); |
| 1912 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1913 | if (mark_free) { |
| 1914 | ret = btrfs_free_reserved_extent(root, |
| 1915 | node->bytenr, |
| 1916 | node->num_bytes); |
| 1917 | BUG_ON(ret); |
| 1918 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1919 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1920 | mutex_unlock(&head->mutex); |
| 1921 | return 0; |
| 1922 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1923 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1924 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1925 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1926 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 1927 | insert_reserved); |
| 1928 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1929 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1930 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 1931 | insert_reserved); |
| 1932 | else |
| 1933 | BUG(); |
| 1934 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1935 | } |
| 1936 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1937 | static noinline struct btrfs_delayed_ref_node * |
| 1938 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1939 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1940 | struct rb_node *node; |
| 1941 | struct btrfs_delayed_ref_node *ref; |
| 1942 | int action = BTRFS_ADD_DELAYED_REF; |
| 1943 | again: |
| 1944 | /* |
| 1945 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1946 | * this prevents ref count from going down to zero when |
| 1947 | * there still are pending delayed ref. |
| 1948 | */ |
| 1949 | node = rb_prev(&head->node.rb_node); |
| 1950 | while (1) { |
| 1951 | if (!node) |
| 1952 | break; |
| 1953 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1954 | rb_node); |
| 1955 | if (ref->bytenr != head->node.bytenr) |
| 1956 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1957 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1958 | return ref; |
| 1959 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1960 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1961 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 1962 | action = BTRFS_DROP_DELAYED_REF; |
| 1963 | goto again; |
| 1964 | } |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1968 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 1969 | struct btrfs_root *root, |
| 1970 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1971 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1972 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1973 | struct btrfs_delayed_ref_node *ref; |
| 1974 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1975 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1976 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1977 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1978 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1979 | |
| 1980 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1981 | while (1) { |
| 1982 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1983 | /* pick a new head ref from the cluster list */ |
| 1984 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1985 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1986 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1987 | locked_ref = list_entry(cluster->next, |
| 1988 | struct btrfs_delayed_ref_head, cluster); |
| 1989 | |
| 1990 | /* grab the lock that says we are going to process |
| 1991 | * all the refs for this head */ |
| 1992 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 1993 | |
| 1994 | /* |
| 1995 | * we may have dropped the spin lock to get the head |
| 1996 | * mutex lock, and that might have given someone else |
| 1997 | * time to free the head. If that's true, it has been |
| 1998 | * removed from our list and we can move on. |
| 1999 | */ |
| 2000 | if (ret == -EAGAIN) { |
| 2001 | locked_ref = NULL; |
| 2002 | count++; |
| 2003 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | /* |
| 2008 | * record the must insert reserved flag before we |
| 2009 | * drop the spin lock. |
| 2010 | */ |
| 2011 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2012 | locked_ref->must_insert_reserved = 0; |
| 2013 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2014 | extent_op = locked_ref->extent_op; |
| 2015 | locked_ref->extent_op = NULL; |
| 2016 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2017 | /* |
| 2018 | * locked_ref is the head node, so we have to go one |
| 2019 | * node back for any delayed ref updates |
| 2020 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2021 | ref = select_delayed_ref(locked_ref); |
| 2022 | if (!ref) { |
| 2023 | /* All delayed refs have been processed, Go ahead |
| 2024 | * and send the head node to run_one_delayed_ref, |
| 2025 | * so that any accounting fixes can happen |
| 2026 | */ |
| 2027 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2028 | |
| 2029 | if (extent_op && must_insert_reserved) { |
| 2030 | kfree(extent_op); |
| 2031 | extent_op = NULL; |
| 2032 | } |
| 2033 | |
| 2034 | if (extent_op) { |
| 2035 | spin_unlock(&delayed_refs->lock); |
| 2036 | |
| 2037 | ret = run_delayed_extent_op(trans, root, |
| 2038 | ref, extent_op); |
| 2039 | BUG_ON(ret); |
| 2040 | kfree(extent_op); |
| 2041 | |
| 2042 | cond_resched(); |
| 2043 | spin_lock(&delayed_refs->lock); |
| 2044 | continue; |
| 2045 | } |
| 2046 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2047 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2048 | locked_ref = NULL; |
| 2049 | } |
| 2050 | |
| 2051 | ref->in_tree = 0; |
| 2052 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2053 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2054 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2055 | spin_unlock(&delayed_refs->lock); |
| 2056 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2057 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2058 | must_insert_reserved); |
| 2059 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2060 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2061 | btrfs_put_delayed_ref(ref); |
| 2062 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2063 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2064 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2065 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2066 | spin_lock(&delayed_refs->lock); |
| 2067 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2068 | return count; |
| 2069 | } |
| 2070 | |
| 2071 | /* |
| 2072 | * this starts processing the delayed reference count updates and |
| 2073 | * extent insertions we have queued up so far. count can be |
| 2074 | * 0, which means to process everything in the tree at the start |
| 2075 | * of the run (but not newly added entries), or it can be some target |
| 2076 | * number you'd like to process. |
| 2077 | */ |
| 2078 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2079 | struct btrfs_root *root, unsigned long count) |
| 2080 | { |
| 2081 | struct rb_node *node; |
| 2082 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2083 | struct btrfs_delayed_ref_node *ref; |
| 2084 | struct list_head cluster; |
| 2085 | int ret; |
| 2086 | int run_all = count == (unsigned long)-1; |
| 2087 | int run_most = 0; |
| 2088 | |
| 2089 | if (root == root->fs_info->extent_root) |
| 2090 | root = root->fs_info->tree_root; |
| 2091 | |
| 2092 | delayed_refs = &trans->transaction->delayed_refs; |
| 2093 | INIT_LIST_HEAD(&cluster); |
| 2094 | again: |
| 2095 | spin_lock(&delayed_refs->lock); |
| 2096 | if (count == 0) { |
| 2097 | count = delayed_refs->num_entries * 2; |
| 2098 | run_most = 1; |
| 2099 | } |
| 2100 | while (1) { |
| 2101 | if (!(run_all || run_most) && |
| 2102 | delayed_refs->num_heads_ready < 64) |
| 2103 | break; |
| 2104 | |
| 2105 | /* |
| 2106 | * go find something we can process in the rbtree. We start at |
| 2107 | * the beginning of the tree, and then build a cluster |
| 2108 | * of refs to process starting at the first one we are able to |
| 2109 | * lock |
| 2110 | */ |
| 2111 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2112 | delayed_refs->run_delayed_start); |
| 2113 | if (ret) |
| 2114 | break; |
| 2115 | |
| 2116 | ret = run_clustered_refs(trans, root, &cluster); |
| 2117 | BUG_ON(ret < 0); |
| 2118 | |
| 2119 | count -= min_t(unsigned long, ret, count); |
| 2120 | |
| 2121 | if (count == 0) |
| 2122 | break; |
| 2123 | } |
| 2124 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2125 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2126 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2127 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2128 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2129 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2130 | |
| 2131 | while (node) { |
| 2132 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2133 | rb_node); |
| 2134 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2135 | struct btrfs_delayed_ref_head *head; |
| 2136 | |
| 2137 | head = btrfs_delayed_node_to_head(ref); |
| 2138 | atomic_inc(&ref->refs); |
| 2139 | |
| 2140 | spin_unlock(&delayed_refs->lock); |
| 2141 | mutex_lock(&head->mutex); |
| 2142 | mutex_unlock(&head->mutex); |
| 2143 | |
| 2144 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2145 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2146 | goto again; |
| 2147 | } |
| 2148 | node = rb_next(node); |
| 2149 | } |
| 2150 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2151 | schedule_timeout(1); |
| 2152 | goto again; |
| 2153 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2154 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2155 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2156 | return 0; |
| 2157 | } |
| 2158 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2159 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2160 | struct btrfs_root *root, |
| 2161 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2162 | int is_data) |
| 2163 | { |
| 2164 | struct btrfs_delayed_extent_op *extent_op; |
| 2165 | int ret; |
| 2166 | |
| 2167 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2168 | if (!extent_op) |
| 2169 | return -ENOMEM; |
| 2170 | |
| 2171 | extent_op->flags_to_set = flags; |
| 2172 | extent_op->update_flags = 1; |
| 2173 | extent_op->update_key = 0; |
| 2174 | extent_op->is_data = is_data ? 1 : 0; |
| 2175 | |
| 2176 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2177 | if (ret) |
| 2178 | kfree(extent_op); |
| 2179 | return ret; |
| 2180 | } |
| 2181 | |
| 2182 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2183 | struct btrfs_root *root, |
| 2184 | struct btrfs_path *path, |
| 2185 | u64 objectid, u64 offset, u64 bytenr) |
| 2186 | { |
| 2187 | struct btrfs_delayed_ref_head *head; |
| 2188 | struct btrfs_delayed_ref_node *ref; |
| 2189 | struct btrfs_delayed_data_ref *data_ref; |
| 2190 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2191 | struct rb_node *node; |
| 2192 | int ret = 0; |
| 2193 | |
| 2194 | ret = -ENOENT; |
| 2195 | delayed_refs = &trans->transaction->delayed_refs; |
| 2196 | spin_lock(&delayed_refs->lock); |
| 2197 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2198 | if (!head) |
| 2199 | goto out; |
| 2200 | |
| 2201 | if (!mutex_trylock(&head->mutex)) { |
| 2202 | atomic_inc(&head->node.refs); |
| 2203 | spin_unlock(&delayed_refs->lock); |
| 2204 | |
| 2205 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2206 | |
| 2207 | mutex_lock(&head->mutex); |
| 2208 | mutex_unlock(&head->mutex); |
| 2209 | btrfs_put_delayed_ref(&head->node); |
| 2210 | return -EAGAIN; |
| 2211 | } |
| 2212 | |
| 2213 | node = rb_prev(&head->node.rb_node); |
| 2214 | if (!node) |
| 2215 | goto out_unlock; |
| 2216 | |
| 2217 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2218 | |
| 2219 | if (ref->bytenr != bytenr) |
| 2220 | goto out_unlock; |
| 2221 | |
| 2222 | ret = 1; |
| 2223 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2224 | goto out_unlock; |
| 2225 | |
| 2226 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2227 | |
| 2228 | node = rb_prev(node); |
| 2229 | if (node) { |
| 2230 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2231 | if (ref->bytenr == bytenr) |
| 2232 | goto out_unlock; |
| 2233 | } |
| 2234 | |
| 2235 | if (data_ref->root != root->root_key.objectid || |
| 2236 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2237 | goto out_unlock; |
| 2238 | |
| 2239 | ret = 0; |
| 2240 | out_unlock: |
| 2241 | mutex_unlock(&head->mutex); |
| 2242 | out: |
| 2243 | spin_unlock(&delayed_refs->lock); |
| 2244 | return ret; |
| 2245 | } |
| 2246 | |
| 2247 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2248 | struct btrfs_root *root, |
| 2249 | struct btrfs_path *path, |
| 2250 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2251 | { |
| 2252 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2253 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2254 | struct btrfs_extent_data_ref *ref; |
| 2255 | struct btrfs_extent_inline_ref *iref; |
| 2256 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2257 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2258 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2259 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2260 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2261 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2262 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2263 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2264 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2265 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2266 | if (ret < 0) |
| 2267 | goto out; |
| 2268 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2269 | |
| 2270 | ret = -ENOENT; |
| 2271 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2272 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2273 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2274 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2275 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2276 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2277 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2278 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2279 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2280 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2281 | ret = 1; |
| 2282 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2283 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2284 | if (item_size < sizeof(*ei)) { |
| 2285 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2286 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2287 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2288 | #endif |
| 2289 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2290 | |
| 2291 | if (item_size != sizeof(*ei) + |
| 2292 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2293 | goto out; |
| 2294 | |
| 2295 | if (btrfs_extent_generation(leaf, ei) <= |
| 2296 | btrfs_root_last_snapshot(&root->root_item)) |
| 2297 | goto out; |
| 2298 | |
| 2299 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2300 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2301 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2302 | goto out; |
| 2303 | |
| 2304 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2305 | if (btrfs_extent_refs(leaf, ei) != |
| 2306 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2307 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2308 | root->root_key.objectid || |
| 2309 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2310 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2311 | goto out; |
| 2312 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2313 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2314 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2315 | return ret; |
| 2316 | } |
| 2317 | |
| 2318 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2319 | struct btrfs_root *root, |
| 2320 | u64 objectid, u64 offset, u64 bytenr) |
| 2321 | { |
| 2322 | struct btrfs_path *path; |
| 2323 | int ret; |
| 2324 | int ret2; |
| 2325 | |
| 2326 | path = btrfs_alloc_path(); |
| 2327 | if (!path) |
| 2328 | return -ENOENT; |
| 2329 | |
| 2330 | do { |
| 2331 | ret = check_committed_ref(trans, root, path, objectid, |
| 2332 | offset, bytenr); |
| 2333 | if (ret && ret != -ENOENT) |
| 2334 | goto out; |
| 2335 | |
| 2336 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2337 | offset, bytenr); |
| 2338 | } while (ret2 == -EAGAIN); |
| 2339 | |
| 2340 | if (ret2 && ret2 != -ENOENT) { |
| 2341 | ret = ret2; |
| 2342 | goto out; |
| 2343 | } |
| 2344 | |
| 2345 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2346 | ret = 0; |
| 2347 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2348 | btrfs_free_path(path); |
| 2349 | return ret; |
| 2350 | } |
| 2351 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2352 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2353 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2354 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2355 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2356 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2357 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2358 | u64 root_gen; |
| 2359 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2360 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2361 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2362 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2363 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2364 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2365 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2366 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2367 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2368 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2369 | shared = 0; |
| 2370 | root_gen = root->root_key.offset; |
| 2371 | } else { |
| 2372 | shared = 1; |
| 2373 | root_gen = trans->transid - 1; |
| 2374 | } |
| 2375 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2376 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2377 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2378 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2379 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2380 | struct btrfs_leaf_ref *ref; |
| 2381 | struct btrfs_extent_info *info; |
| 2382 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2383 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2384 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2385 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2386 | goto out; |
| 2387 | } |
| 2388 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2389 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2390 | ref->bytenr = buf->start; |
| 2391 | ref->owner = btrfs_header_owner(buf); |
| 2392 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2393 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2394 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2395 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2396 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2397 | u64 disk_bytenr; |
| 2398 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2399 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2400 | continue; |
| 2401 | fi = btrfs_item_ptr(buf, i, |
| 2402 | struct btrfs_file_extent_item); |
| 2403 | if (btrfs_file_extent_type(buf, fi) == |
| 2404 | BTRFS_FILE_EXTENT_INLINE) |
| 2405 | continue; |
| 2406 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2407 | if (disk_bytenr == 0) |
| 2408 | continue; |
| 2409 | |
| 2410 | info->bytenr = disk_bytenr; |
| 2411 | info->num_bytes = |
| 2412 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2413 | info->objectid = key.objectid; |
| 2414 | info->offset = key.offset; |
| 2415 | info++; |
| 2416 | } |
| 2417 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2418 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2419 | if (ret == -EEXIST && shared) { |
| 2420 | struct btrfs_leaf_ref *old; |
| 2421 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2422 | BUG_ON(!old); |
| 2423 | btrfs_remove_leaf_ref(root, old); |
| 2424 | btrfs_free_leaf_ref(root, old); |
| 2425 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2426 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2427 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2428 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2429 | } |
| 2430 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2431 | return ret; |
| 2432 | } |
| 2433 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2434 | /* when a block goes through cow, we update the reference counts of |
| 2435 | * everything that block points to. The internal pointers of the block |
| 2436 | * can be in just about any order, and it is likely to have clusters of |
| 2437 | * things that are close together and clusters of things that are not. |
| 2438 | * |
| 2439 | * To help reduce the seeks that come with updating all of these reference |
| 2440 | * counts, sort them by byte number before actual updates are done. |
| 2441 | * |
| 2442 | * struct refsort is used to match byte number to slot in the btree block. |
| 2443 | * we sort based on the byte number and then use the slot to actually |
| 2444 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2445 | * |
| 2446 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2447 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2448 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2449 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2450 | */ |
| 2451 | struct refsort { |
| 2452 | u64 bytenr; |
| 2453 | u32 slot; |
| 2454 | }; |
| 2455 | |
| 2456 | /* |
| 2457 | * for passing into sort() |
| 2458 | */ |
| 2459 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2460 | { |
| 2461 | const struct refsort *a = a_void; |
| 2462 | const struct refsort *b = b_void; |
| 2463 | |
| 2464 | if (a->bytenr < b->bytenr) |
| 2465 | return -1; |
| 2466 | if (a->bytenr > b->bytenr) |
| 2467 | return 1; |
| 2468 | return 0; |
| 2469 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2470 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2471 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2472 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2473 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2474 | struct extent_buffer *buf, |
| 2475 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2476 | { |
| 2477 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2478 | u64 num_bytes; |
| 2479 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2480 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2481 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2482 | struct btrfs_key key; |
| 2483 | struct btrfs_file_extent_item *fi; |
| 2484 | int i; |
| 2485 | int level; |
| 2486 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2487 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2488 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2489 | |
| 2490 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2491 | nritems = btrfs_header_nritems(buf); |
| 2492 | level = btrfs_header_level(buf); |
| 2493 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2494 | if (!root->ref_cows && level == 0) |
| 2495 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2496 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2497 | if (inc) |
| 2498 | process_func = btrfs_inc_extent_ref; |
| 2499 | else |
| 2500 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2501 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2502 | if (full_backref) |
| 2503 | parent = buf->start; |
| 2504 | else |
| 2505 | parent = 0; |
| 2506 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2507 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2508 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2509 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2510 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2511 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2512 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2513 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2514 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2515 | BTRFS_FILE_EXTENT_INLINE) |
| 2516 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2517 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2518 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2519 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2520 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2521 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2522 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2523 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2524 | parent, ref_root, key.objectid, |
| 2525 | key.offset); |
| 2526 | if (ret) |
| 2527 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2528 | } else { |
| 2529 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2530 | num_bytes = btrfs_level_size(root, level - 1); |
| 2531 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2532 | parent, ref_root, level - 1, 0); |
| 2533 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2534 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2535 | } |
| 2536 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2537 | return 0; |
| 2538 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2539 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2540 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2541 | } |
| 2542 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2543 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2544 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2545 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2546 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2547 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2549 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2550 | struct extent_buffer *buf, int full_backref) |
| 2551 | { |
| 2552 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2553 | } |
| 2554 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2555 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2556 | struct btrfs_root *root, |
| 2557 | struct btrfs_path *path, |
| 2558 | struct btrfs_block_group_cache *cache) |
| 2559 | { |
| 2560 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2561 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2562 | unsigned long bi; |
| 2563 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2564 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2565 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2566 | if (ret < 0) |
| 2567 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2568 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2569 | |
| 2570 | leaf = path->nodes[0]; |
| 2571 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2572 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2573 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2574 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2575 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2576 | if (ret) |
| 2577 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2578 | return 0; |
| 2579 | |
| 2580 | } |
| 2581 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2582 | static struct btrfs_block_group_cache * |
| 2583 | next_block_group(struct btrfs_root *root, |
| 2584 | struct btrfs_block_group_cache *cache) |
| 2585 | { |
| 2586 | struct rb_node *node; |
| 2587 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2588 | node = rb_next(&cache->cache_node); |
| 2589 | btrfs_put_block_group(cache); |
| 2590 | if (node) { |
| 2591 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2592 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2593 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2594 | } else |
| 2595 | cache = NULL; |
| 2596 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2597 | return cache; |
| 2598 | } |
| 2599 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2600 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2601 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2602 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2603 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2604 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2605 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2606 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2607 | |
| 2608 | path = btrfs_alloc_path(); |
| 2609 | if (!path) |
| 2610 | return -ENOMEM; |
| 2611 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2612 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2613 | if (last == 0) { |
| 2614 | err = btrfs_run_delayed_refs(trans, root, |
| 2615 | (unsigned long)-1); |
| 2616 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2617 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2618 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2619 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2620 | while (cache) { |
| 2621 | if (cache->dirty) |
| 2622 | break; |
| 2623 | cache = next_block_group(root, cache); |
| 2624 | } |
| 2625 | if (!cache) { |
| 2626 | if (last == 0) |
| 2627 | break; |
| 2628 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2629 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2630 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2631 | |
| 2632 | cache->dirty = 0; |
| 2633 | last = cache->key.objectid + cache->key.offset; |
| 2634 | |
| 2635 | err = write_one_cache_group(trans, root, path, cache); |
| 2636 | BUG_ON(err); |
| 2637 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2638 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2639 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2640 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2641 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2642 | } |
| 2643 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2644 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2645 | { |
| 2646 | struct btrfs_block_group_cache *block_group; |
| 2647 | int readonly = 0; |
| 2648 | |
| 2649 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2650 | if (!block_group || block_group->ro) |
| 2651 | readonly = 1; |
| 2652 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2653 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2654 | return readonly; |
| 2655 | } |
| 2656 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2657 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2658 | u64 total_bytes, u64 bytes_used, |
| 2659 | struct btrfs_space_info **space_info) |
| 2660 | { |
| 2661 | struct btrfs_space_info *found; |
| 2662 | |
| 2663 | found = __find_space_info(info, flags); |
| 2664 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2665 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2666 | found->total_bytes += total_bytes; |
| 2667 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2668 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2669 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2670 | *space_info = found; |
| 2671 | return 0; |
| 2672 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2673 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2674 | if (!found) |
| 2675 | return -ENOMEM; |
| 2676 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2677 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2678 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2679 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2680 | found->flags = flags; |
| 2681 | found->total_bytes = total_bytes; |
| 2682 | found->bytes_used = bytes_used; |
| 2683 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2684 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2685 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2686 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2687 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2688 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2689 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2690 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2691 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2692 | return 0; |
| 2693 | } |
| 2694 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2695 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2696 | { |
| 2697 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2698 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2699 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2700 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2701 | if (extra_flags) { |
| 2702 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2703 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2704 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2705 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2706 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2707 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2708 | } |
| 2709 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2710 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2711 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 2712 | { |
| 2713 | spin_lock(&cache->space_info->lock); |
| 2714 | spin_lock(&cache->lock); |
| 2715 | if (!cache->ro) { |
| 2716 | cache->space_info->bytes_readonly += cache->key.offset - |
| 2717 | btrfs_block_group_used(&cache->item); |
| 2718 | cache->ro = 1; |
| 2719 | } |
| 2720 | spin_unlock(&cache->lock); |
| 2721 | spin_unlock(&cache->space_info->lock); |
| 2722 | } |
| 2723 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2724 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2725 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2726 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2727 | |
| 2728 | if (num_devices == 1) |
| 2729 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2730 | if (num_devices < 4) |
| 2731 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2732 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2733 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2734 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2735 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2736 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2737 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2738 | |
| 2739 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2740 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2741 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2742 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2743 | |
| 2744 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2745 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2746 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2747 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2748 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2749 | return flags; |
| 2750 | } |
| 2751 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2752 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 2753 | { |
| 2754 | struct btrfs_fs_info *info = root->fs_info; |
| 2755 | u64 alloc_profile; |
| 2756 | |
| 2757 | if (data) { |
| 2758 | alloc_profile = info->avail_data_alloc_bits & |
| 2759 | info->data_alloc_profile; |
| 2760 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 2761 | } else if (root == root->fs_info->chunk_root) { |
| 2762 | alloc_profile = info->avail_system_alloc_bits & |
| 2763 | info->system_alloc_profile; |
| 2764 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 2765 | } else { |
| 2766 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2767 | info->metadata_alloc_profile; |
| 2768 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 2769 | } |
| 2770 | |
| 2771 | return btrfs_reduce_alloc_profile(root, data); |
| 2772 | } |
| 2773 | |
| 2774 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2775 | { |
| 2776 | u64 alloc_target; |
| 2777 | |
| 2778 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2779 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 2780 | alloc_target); |
| 2781 | } |
| 2782 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2783 | static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items) |
| 2784 | { |
| 2785 | u64 num_bytes; |
| 2786 | int level; |
| 2787 | |
| 2788 | level = BTRFS_MAX_LEVEL - 2; |
| 2789 | /* |
| 2790 | * NOTE: these calculations are absolutely the worst possible case. |
| 2791 | * This assumes that _every_ item we insert will require a new leaf, and |
| 2792 | * that the tree has grown to its maximum level size. |
| 2793 | */ |
| 2794 | |
| 2795 | /* |
| 2796 | * for every item we insert we could insert both an extent item and a |
| 2797 | * extent ref item. Then for ever item we insert, we will need to cow |
| 2798 | * both the original leaf, plus the leaf to the left and right of it. |
| 2799 | * |
| 2800 | * Unless we are talking about the extent root, then we just want the |
| 2801 | * number of items * 2, since we just need the extent item plus its ref. |
| 2802 | */ |
| 2803 | if (root == root->fs_info->extent_root) |
| 2804 | num_bytes = num_items * 2; |
| 2805 | else |
| 2806 | num_bytes = (num_items + (2 * num_items)) * 3; |
| 2807 | |
| 2808 | /* |
| 2809 | * num_bytes is total number of leaves we could need times the leaf |
| 2810 | * size, and then for every leaf we could end up cow'ing 2 nodes per |
| 2811 | * level, down to the leaf level. |
| 2812 | */ |
| 2813 | num_bytes = (num_bytes * root->leafsize) + |
| 2814 | (num_bytes * (level * 2)) * root->nodesize; |
| 2815 | |
| 2816 | return num_bytes; |
| 2817 | } |
| 2818 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2819 | /* |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2820 | * Unreserve metadata space for delalloc. If we have less reserved credits than |
| 2821 | * we have extents, this function does nothing. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2822 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2823 | int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root, |
| 2824 | struct inode *inode, int num_items) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2825 | { |
| 2826 | struct btrfs_fs_info *info = root->fs_info; |
| 2827 | struct btrfs_space_info *meta_sinfo; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2828 | u64 num_bytes; |
| 2829 | u64 alloc_target; |
| 2830 | bool bug = false; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2831 | |
| 2832 | /* get the space info for where the metadata will live */ |
| 2833 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 2834 | meta_sinfo = __find_space_info(info, alloc_target); |
| 2835 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2836 | num_bytes = calculate_bytes_needed(root->fs_info->extent_root, |
| 2837 | num_items); |
| 2838 | |
| 2839 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2840 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 2841 | if (BTRFS_I(inode)->reserved_extents <= |
| 2842 | BTRFS_I(inode)->outstanding_extents) { |
| 2843 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2844 | spin_unlock(&meta_sinfo->lock); |
| 2845 | return 0; |
| 2846 | } |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2847 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2848 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2849 | BTRFS_I(inode)->reserved_extents--; |
| 2850 | BUG_ON(BTRFS_I(inode)->reserved_extents < 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2851 | |
| 2852 | if (meta_sinfo->bytes_delalloc < num_bytes) { |
| 2853 | bug = true; |
| 2854 | meta_sinfo->bytes_delalloc = 0; |
| 2855 | } else { |
| 2856 | meta_sinfo->bytes_delalloc -= num_bytes; |
| 2857 | } |
| 2858 | spin_unlock(&meta_sinfo->lock); |
| 2859 | |
| 2860 | BUG_ON(bug); |
| 2861 | |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | static void check_force_delalloc(struct btrfs_space_info *meta_sinfo) |
| 2866 | { |
| 2867 | u64 thresh; |
| 2868 | |
| 2869 | thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 2870 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 2871 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 2872 | meta_sinfo->bytes_may_use; |
| 2873 | |
| 2874 | thresh = meta_sinfo->total_bytes - thresh; |
| 2875 | thresh *= 80; |
| 2876 | do_div(thresh, 100); |
| 2877 | if (thresh <= meta_sinfo->bytes_delalloc) |
| 2878 | meta_sinfo->force_delalloc = 1; |
| 2879 | else |
| 2880 | meta_sinfo->force_delalloc = 0; |
| 2881 | } |
| 2882 | |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2883 | struct async_flush { |
| 2884 | struct btrfs_root *root; |
| 2885 | struct btrfs_space_info *info; |
| 2886 | struct btrfs_work work; |
| 2887 | }; |
| 2888 | |
| 2889 | static noinline void flush_delalloc_async(struct btrfs_work *work) |
| 2890 | { |
| 2891 | struct async_flush *async; |
| 2892 | struct btrfs_root *root; |
| 2893 | struct btrfs_space_info *info; |
| 2894 | |
| 2895 | async = container_of(work, struct async_flush, work); |
| 2896 | root = async->root; |
| 2897 | info = async->info; |
| 2898 | |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2899 | btrfs_start_delalloc_inodes(root, 0); |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2900 | wake_up(&info->flush_wait); |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2901 | btrfs_wait_ordered_extents(root, 0, 0); |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2902 | |
| 2903 | spin_lock(&info->lock); |
| 2904 | info->flushing = 0; |
| 2905 | spin_unlock(&info->lock); |
| 2906 | wake_up(&info->flush_wait); |
| 2907 | |
| 2908 | kfree(async); |
| 2909 | } |
| 2910 | |
| 2911 | static void wait_on_flush(struct btrfs_space_info *info) |
| 2912 | { |
| 2913 | DEFINE_WAIT(wait); |
| 2914 | u64 used; |
| 2915 | |
| 2916 | while (1) { |
| 2917 | prepare_to_wait(&info->flush_wait, &wait, |
| 2918 | TASK_UNINTERRUPTIBLE); |
| 2919 | spin_lock(&info->lock); |
| 2920 | if (!info->flushing) { |
| 2921 | spin_unlock(&info->lock); |
| 2922 | break; |
| 2923 | } |
| 2924 | |
| 2925 | used = info->bytes_used + info->bytes_reserved + |
| 2926 | info->bytes_pinned + info->bytes_readonly + |
| 2927 | info->bytes_super + info->bytes_root + |
| 2928 | info->bytes_may_use + info->bytes_delalloc; |
| 2929 | if (used < info->total_bytes) { |
| 2930 | spin_unlock(&info->lock); |
| 2931 | break; |
| 2932 | } |
| 2933 | spin_unlock(&info->lock); |
| 2934 | schedule(); |
| 2935 | } |
| 2936 | finish_wait(&info->flush_wait, &wait); |
| 2937 | } |
| 2938 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2939 | static void flush_delalloc(struct btrfs_root *root, |
| 2940 | struct btrfs_space_info *info) |
| 2941 | { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2942 | struct async_flush *async; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2943 | bool wait = false; |
| 2944 | |
| 2945 | spin_lock(&info->lock); |
| 2946 | |
| 2947 | if (!info->flushing) { |
| 2948 | info->flushing = 1; |
| 2949 | init_waitqueue_head(&info->flush_wait); |
| 2950 | } else { |
| 2951 | wait = true; |
| 2952 | } |
| 2953 | |
| 2954 | spin_unlock(&info->lock); |
| 2955 | |
| 2956 | if (wait) { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2957 | wait_on_flush(info); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2958 | return; |
| 2959 | } |
| 2960 | |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2961 | async = kzalloc(sizeof(*async), GFP_NOFS); |
| 2962 | if (!async) |
| 2963 | goto flush; |
| 2964 | |
| 2965 | async->root = root; |
| 2966 | async->info = info; |
| 2967 | async->work.func = flush_delalloc_async; |
| 2968 | |
| 2969 | btrfs_queue_worker(&root->fs_info->enospc_workers, |
| 2970 | &async->work); |
| 2971 | wait_on_flush(info); |
| 2972 | return; |
| 2973 | |
| 2974 | flush: |
Yan, Zheng | 24bbcf0 | 2009-11-12 09:36:34 +0000 | [diff] [blame] | 2975 | btrfs_start_delalloc_inodes(root, 0); |
| 2976 | btrfs_wait_ordered_extents(root, 0, 0); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2977 | |
| 2978 | spin_lock(&info->lock); |
| 2979 | info->flushing = 0; |
| 2980 | spin_unlock(&info->lock); |
| 2981 | wake_up(&info->flush_wait); |
| 2982 | } |
| 2983 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2984 | static int maybe_allocate_chunk(struct btrfs_root *root, |
| 2985 | struct btrfs_space_info *info) |
| 2986 | { |
| 2987 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
| 2988 | struct btrfs_trans_handle *trans; |
| 2989 | bool wait = false; |
| 2990 | int ret = 0; |
| 2991 | u64 min_metadata; |
| 2992 | u64 free_space; |
| 2993 | |
| 2994 | free_space = btrfs_super_total_bytes(disk_super); |
| 2995 | /* |
Chris Mason | 33b2580 | 2009-11-11 10:16:57 -0500 | [diff] [blame] | 2996 | * we allow the metadata to grow to a max of either 10gb or 5% of the |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2997 | * space in the volume. |
| 2998 | */ |
Chris Mason | 33b2580 | 2009-11-11 10:16:57 -0500 | [diff] [blame] | 2999 | min_metadata = min((u64)10 * 1024 * 1024 * 1024, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3000 | div64_u64(free_space * 5, 100)); |
| 3001 | if (info->total_bytes >= min_metadata) { |
| 3002 | spin_unlock(&info->lock); |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | if (info->full) { |
| 3007 | spin_unlock(&info->lock); |
| 3008 | return 0; |
| 3009 | } |
| 3010 | |
| 3011 | if (!info->allocating_chunk) { |
| 3012 | info->force_alloc = 1; |
| 3013 | info->allocating_chunk = 1; |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3014 | init_waitqueue_head(&info->allocate_wait); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3015 | } else { |
| 3016 | wait = true; |
| 3017 | } |
| 3018 | |
| 3019 | spin_unlock(&info->lock); |
| 3020 | |
| 3021 | if (wait) { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3022 | wait_event(info->allocate_wait, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3023 | !info->allocating_chunk); |
| 3024 | return 1; |
| 3025 | } |
| 3026 | |
| 3027 | trans = btrfs_start_transaction(root, 1); |
| 3028 | if (!trans) { |
| 3029 | ret = -ENOMEM; |
| 3030 | goto out; |
| 3031 | } |
| 3032 | |
| 3033 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3034 | 4096 + 2 * 1024 * 1024, |
| 3035 | info->flags, 0); |
| 3036 | btrfs_end_transaction(trans, root); |
| 3037 | if (ret) |
| 3038 | goto out; |
| 3039 | out: |
| 3040 | spin_lock(&info->lock); |
| 3041 | info->allocating_chunk = 0; |
| 3042 | spin_unlock(&info->lock); |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3043 | wake_up(&info->allocate_wait); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3044 | |
| 3045 | if (ret) |
| 3046 | return 0; |
| 3047 | return 1; |
| 3048 | } |
| 3049 | |
| 3050 | /* |
| 3051 | * Reserve metadata space for delalloc. |
| 3052 | */ |
| 3053 | int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root, |
| 3054 | struct inode *inode, int num_items) |
| 3055 | { |
| 3056 | struct btrfs_fs_info *info = root->fs_info; |
| 3057 | struct btrfs_space_info *meta_sinfo; |
| 3058 | u64 num_bytes; |
| 3059 | u64 used; |
| 3060 | u64 alloc_target; |
| 3061 | int flushed = 0; |
| 3062 | int force_delalloc; |
| 3063 | |
| 3064 | /* get the space info for where the metadata will live */ |
| 3065 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3066 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3067 | |
| 3068 | num_bytes = calculate_bytes_needed(root->fs_info->extent_root, |
| 3069 | num_items); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3070 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3071 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3072 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3073 | force_delalloc = meta_sinfo->force_delalloc; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3074 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3075 | if (unlikely(!meta_sinfo->bytes_root)) |
| 3076 | meta_sinfo->bytes_root = calculate_bytes_needed(root, 6); |
| 3077 | |
| 3078 | if (!flushed) |
| 3079 | meta_sinfo->bytes_delalloc += num_bytes; |
| 3080 | |
| 3081 | used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 3082 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 3083 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 3084 | meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc; |
| 3085 | |
| 3086 | if (used > meta_sinfo->total_bytes) { |
| 3087 | flushed++; |
| 3088 | |
| 3089 | if (flushed == 1) { |
| 3090 | if (maybe_allocate_chunk(root, meta_sinfo)) |
| 3091 | goto again; |
| 3092 | flushed++; |
| 3093 | } else { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3094 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3095 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3096 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3097 | if (flushed == 2) { |
| 3098 | filemap_flush(inode->i_mapping); |
| 3099 | goto again; |
| 3100 | } else if (flushed == 3) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3101 | flush_delalloc(root, meta_sinfo); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3102 | goto again; |
| 3103 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3104 | spin_lock(&meta_sinfo->lock); |
| 3105 | meta_sinfo->bytes_delalloc -= num_bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3106 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3107 | printk(KERN_ERR "enospc, has %d, reserved %d\n", |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3108 | BTRFS_I(inode)->outstanding_extents, |
| 3109 | BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3110 | dump_space_info(meta_sinfo, 0, 0); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3111 | return -ENOSPC; |
| 3112 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3113 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3114 | BTRFS_I(inode)->reserved_extents++; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3115 | check_force_delalloc(meta_sinfo); |
| 3116 | spin_unlock(&meta_sinfo->lock); |
| 3117 | |
| 3118 | if (!flushed && force_delalloc) |
| 3119 | filemap_flush(inode->i_mapping); |
| 3120 | |
| 3121 | return 0; |
| 3122 | } |
| 3123 | |
| 3124 | /* |
| 3125 | * unreserve num_items number of items worth of metadata space. This needs to |
| 3126 | * be paired with btrfs_reserve_metadata_space. |
| 3127 | * |
| 3128 | * NOTE: if you have the option, run this _AFTER_ you do a |
| 3129 | * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref |
| 3130 | * oprations which will result in more used metadata, so we want to make sure we |
| 3131 | * can do that without issue. |
| 3132 | */ |
| 3133 | int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items) |
| 3134 | { |
| 3135 | struct btrfs_fs_info *info = root->fs_info; |
| 3136 | struct btrfs_space_info *meta_sinfo; |
| 3137 | u64 num_bytes; |
| 3138 | u64 alloc_target; |
| 3139 | bool bug = false; |
| 3140 | |
| 3141 | /* get the space info for where the metadata will live */ |
| 3142 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3143 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3144 | |
| 3145 | num_bytes = calculate_bytes_needed(root, num_items); |
| 3146 | |
| 3147 | spin_lock(&meta_sinfo->lock); |
| 3148 | if (meta_sinfo->bytes_may_use < num_bytes) { |
| 3149 | bug = true; |
| 3150 | meta_sinfo->bytes_may_use = 0; |
| 3151 | } else { |
| 3152 | meta_sinfo->bytes_may_use -= num_bytes; |
| 3153 | } |
| 3154 | spin_unlock(&meta_sinfo->lock); |
| 3155 | |
| 3156 | BUG_ON(bug); |
| 3157 | |
| 3158 | return 0; |
| 3159 | } |
| 3160 | |
| 3161 | /* |
| 3162 | * Reserve some metadata space for use. We'll calculate the worste case number |
| 3163 | * of bytes that would be needed to modify num_items number of items. If we |
| 3164 | * have space, fantastic, if not, you get -ENOSPC. Please call |
| 3165 | * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of |
| 3166 | * items you reserved, since whatever metadata you needed should have already |
| 3167 | * been allocated. |
| 3168 | * |
| 3169 | * This will commit the transaction to make more space if we don't have enough |
| 3170 | * metadata space. THe only time we don't do this is if we're reserving space |
| 3171 | * inside of a transaction, then we will just return -ENOSPC and it is the |
| 3172 | * callers responsibility to handle it properly. |
| 3173 | */ |
| 3174 | int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items) |
| 3175 | { |
| 3176 | struct btrfs_fs_info *info = root->fs_info; |
| 3177 | struct btrfs_space_info *meta_sinfo; |
| 3178 | u64 num_bytes; |
| 3179 | u64 used; |
| 3180 | u64 alloc_target; |
| 3181 | int retries = 0; |
| 3182 | |
| 3183 | /* get the space info for where the metadata will live */ |
| 3184 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3185 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3186 | |
| 3187 | num_bytes = calculate_bytes_needed(root, num_items); |
| 3188 | again: |
| 3189 | spin_lock(&meta_sinfo->lock); |
| 3190 | |
| 3191 | if (unlikely(!meta_sinfo->bytes_root)) |
| 3192 | meta_sinfo->bytes_root = calculate_bytes_needed(root, 6); |
| 3193 | |
| 3194 | if (!retries) |
| 3195 | meta_sinfo->bytes_may_use += num_bytes; |
| 3196 | |
| 3197 | used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 3198 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 3199 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 3200 | meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc; |
| 3201 | |
| 3202 | if (used > meta_sinfo->total_bytes) { |
| 3203 | retries++; |
| 3204 | if (retries == 1) { |
| 3205 | if (maybe_allocate_chunk(root, meta_sinfo)) |
| 3206 | goto again; |
| 3207 | retries++; |
| 3208 | } else { |
| 3209 | spin_unlock(&meta_sinfo->lock); |
| 3210 | } |
| 3211 | |
| 3212 | if (retries == 2) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3213 | flush_delalloc(root, meta_sinfo); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3214 | goto again; |
| 3215 | } |
| 3216 | spin_lock(&meta_sinfo->lock); |
| 3217 | meta_sinfo->bytes_may_use -= num_bytes; |
| 3218 | spin_unlock(&meta_sinfo->lock); |
| 3219 | |
| 3220 | dump_space_info(meta_sinfo, 0, 0); |
| 3221 | return -ENOSPC; |
| 3222 | } |
| 3223 | |
| 3224 | check_force_delalloc(meta_sinfo); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3225 | spin_unlock(&meta_sinfo->lock); |
| 3226 | |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
| 3230 | /* |
| 3231 | * This will check the space that the inode allocates from to make sure we have |
| 3232 | * enough space for bytes. |
| 3233 | */ |
| 3234 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 3235 | u64 bytes) |
| 3236 | { |
| 3237 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3238 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3239 | |
| 3240 | /* make sure bytes are sectorsize aligned */ |
| 3241 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3242 | |
| 3243 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3244 | if (!data_sinfo) |
| 3245 | goto alloc; |
| 3246 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3247 | again: |
| 3248 | /* make sure we have enough space to handle the data first */ |
| 3249 | spin_lock(&data_sinfo->lock); |
| 3250 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 3251 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 3252 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3253 | data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3254 | struct btrfs_trans_handle *trans; |
| 3255 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3256 | /* |
| 3257 | * if we don't have enough free bytes in this space then we need |
| 3258 | * to alloc a new chunk. |
| 3259 | */ |
| 3260 | if (!data_sinfo->full) { |
| 3261 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3262 | |
| 3263 | data_sinfo->force_alloc = 1; |
| 3264 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3265 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3266 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 3267 | trans = btrfs_start_transaction(root, 1); |
| 3268 | if (!trans) |
| 3269 | return -ENOMEM; |
| 3270 | |
| 3271 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3272 | bytes + 2 * 1024 * 1024, |
| 3273 | alloc_target, 0); |
| 3274 | btrfs_end_transaction(trans, root); |
| 3275 | if (ret) |
| 3276 | return ret; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3277 | |
| 3278 | if (!data_sinfo) { |
| 3279 | btrfs_set_inode_space_info(root, inode); |
| 3280 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3281 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3282 | goto again; |
| 3283 | } |
| 3284 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3285 | |
| 3286 | /* commit the current transaction and try again */ |
Sage Weil | dd7e0b7 | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3287 | if (!committed && !root->fs_info->open_ioctl_trans) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3288 | committed = 1; |
| 3289 | trans = btrfs_join_transaction(root, 1); |
| 3290 | if (!trans) |
| 3291 | return -ENOMEM; |
| 3292 | ret = btrfs_commit_transaction(trans, root); |
| 3293 | if (ret) |
| 3294 | return ret; |
| 3295 | goto again; |
| 3296 | } |
| 3297 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3298 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 3299 | ", %llu bytes_used, %llu bytes_reserved, " |
Hu Tao | 68f5a38 | 2009-07-02 13:55:45 -0400 | [diff] [blame] | 3300 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use " |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 3301 | "%llu total\n", (unsigned long long)bytes, |
| 3302 | (unsigned long long)data_sinfo->bytes_delalloc, |
| 3303 | (unsigned long long)data_sinfo->bytes_used, |
| 3304 | (unsigned long long)data_sinfo->bytes_reserved, |
| 3305 | (unsigned long long)data_sinfo->bytes_pinned, |
| 3306 | (unsigned long long)data_sinfo->bytes_readonly, |
| 3307 | (unsigned long long)data_sinfo->bytes_may_use, |
| 3308 | (unsigned long long)data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3309 | return -ENOSPC; |
| 3310 | } |
| 3311 | data_sinfo->bytes_may_use += bytes; |
| 3312 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3313 | spin_unlock(&data_sinfo->lock); |
| 3314 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3315 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | /* |
| 3319 | * if there was an error for whatever reason after calling |
| 3320 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 3321 | */ |
| 3322 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 3323 | struct inode *inode, u64 bytes) |
| 3324 | { |
| 3325 | struct btrfs_space_info *data_sinfo; |
| 3326 | |
| 3327 | /* make sure bytes are sectorsize aligned */ |
| 3328 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3329 | |
| 3330 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3331 | spin_lock(&data_sinfo->lock); |
| 3332 | data_sinfo->bytes_may_use -= bytes; |
| 3333 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3334 | spin_unlock(&data_sinfo->lock); |
| 3335 | } |
| 3336 | |
| 3337 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 3338 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 3339 | u64 bytes) |
| 3340 | { |
| 3341 | struct btrfs_space_info *data_sinfo; |
| 3342 | |
| 3343 | /* get the space info for where this inode will be storing its data */ |
| 3344 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3345 | |
| 3346 | /* make sure we have enough space to handle the data first */ |
| 3347 | spin_lock(&data_sinfo->lock); |
| 3348 | data_sinfo->bytes_delalloc += bytes; |
| 3349 | |
| 3350 | /* |
| 3351 | * we are adding a delalloc extent without calling |
| 3352 | * btrfs_check_data_free_space first. This happens on a weird |
| 3353 | * writepage condition, but shouldn't hurt our accounting |
| 3354 | */ |
| 3355 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 3356 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 3357 | BTRFS_I(inode)->reserved_bytes = 0; |
| 3358 | } else { |
| 3359 | data_sinfo->bytes_may_use -= bytes; |
| 3360 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3361 | } |
| 3362 | |
| 3363 | spin_unlock(&data_sinfo->lock); |
| 3364 | } |
| 3365 | |
| 3366 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 3367 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 3368 | u64 bytes) |
| 3369 | { |
| 3370 | struct btrfs_space_info *info; |
| 3371 | |
| 3372 | info = BTRFS_I(inode)->space_info; |
| 3373 | |
| 3374 | spin_lock(&info->lock); |
| 3375 | info->bytes_delalloc -= bytes; |
| 3376 | spin_unlock(&info->lock); |
| 3377 | } |
| 3378 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3379 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3380 | { |
| 3381 | struct list_head *head = &info->space_info; |
| 3382 | struct btrfs_space_info *found; |
| 3383 | |
| 3384 | rcu_read_lock(); |
| 3385 | list_for_each_entry_rcu(found, head, list) { |
| 3386 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3387 | found->force_alloc = 1; |
| 3388 | } |
| 3389 | rcu_read_unlock(); |
| 3390 | } |
| 3391 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3392 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3393 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3394 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3395 | { |
| 3396 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3397 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3398 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3399 | int ret = 0; |
| 3400 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3401 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3402 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3403 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3404 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3405 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3406 | if (!space_info) { |
| 3407 | ret = update_space_info(extent_root->fs_info, flags, |
| 3408 | 0, 0, &space_info); |
| 3409 | BUG_ON(ret); |
| 3410 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3411 | BUG_ON(!space_info); |
| 3412 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3413 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3414 | if (space_info->force_alloc) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3415 | force = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3416 | if (space_info->full) { |
| 3417 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3418 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3419 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3420 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3421 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3422 | thresh = div_factor(thresh, 8); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3423 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3424 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3425 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 3426 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3427 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3428 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3429 | spin_unlock(&space_info->lock); |
| 3430 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3431 | /* |
| 3432 | * if we're doing a data chunk, go ahead and make sure that |
| 3433 | * we keep a reasonable number of metadata chunks allocated in the |
| 3434 | * FS as well. |
| 3435 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3436 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3437 | fs_info->data_chunk_allocations++; |
| 3438 | if (!(fs_info->data_chunk_allocations % |
| 3439 | fs_info->metadata_ratio)) |
| 3440 | force_metadata_allocation(fs_info); |
| 3441 | } |
| 3442 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3443 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3444 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3445 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3446 | space_info->full = 1; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3447 | space_info->force_alloc = 0; |
| 3448 | spin_unlock(&space_info->lock); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3449 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3450 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3451 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3452 | } |
| 3453 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3454 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 3455 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3456 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3457 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3458 | { |
| 3459 | struct btrfs_block_group_cache *cache; |
| 3460 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3461 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3462 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3463 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3464 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3465 | /* block accounting for super block */ |
| 3466 | spin_lock(&info->delalloc_lock); |
| 3467 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 3468 | if (alloc) |
| 3469 | old_val += num_bytes; |
| 3470 | else |
| 3471 | old_val -= num_bytes; |
| 3472 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3473 | spin_unlock(&info->delalloc_lock); |
| 3474 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3475 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3476 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3477 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3478 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3479 | byte_in_group = bytenr - cache->key.objectid; |
| 3480 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3481 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3482 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3483 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3484 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3485 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3486 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3487 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3488 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3489 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3490 | cache->reserved -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3491 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3492 | cache->space_info->bytes_reserved -= num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 3493 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3494 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3495 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3496 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3497 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3498 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3499 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3500 | if (cache->ro) |
| 3501 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3502 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3503 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3504 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3505 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3506 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3507 | |
| 3508 | ret = btrfs_discard_extent(root, bytenr, |
| 3509 | num_bytes); |
| 3510 | WARN_ON(ret); |
| 3511 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3512 | ret = btrfs_add_free_space(cache, bytenr, |
| 3513 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3514 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3515 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3516 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3517 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3518 | total -= num_bytes; |
| 3519 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3520 | } |
| 3521 | return 0; |
| 3522 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3523 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3524 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 3525 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3526 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3527 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3528 | |
| 3529 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 3530 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3531 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3532 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3533 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3534 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3535 | |
| 3536 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3537 | } |
| 3538 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3539 | /* |
| 3540 | * this function must be called within transaction |
| 3541 | */ |
| 3542 | int btrfs_pin_extent(struct btrfs_root *root, |
| 3543 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3544 | { |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3545 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3546 | struct btrfs_block_group_cache *cache; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3547 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3548 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 3549 | BUG_ON(!cache); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3550 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3551 | spin_lock(&cache->space_info->lock); |
| 3552 | spin_lock(&cache->lock); |
| 3553 | cache->pinned += num_bytes; |
| 3554 | cache->space_info->bytes_pinned += num_bytes; |
| 3555 | if (reserved) { |
| 3556 | cache->reserved -= num_bytes; |
| 3557 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3558 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3559 | spin_unlock(&cache->lock); |
| 3560 | spin_unlock(&cache->space_info->lock); |
| 3561 | |
| 3562 | btrfs_put_block_group(cache); |
| 3563 | |
| 3564 | set_extent_dirty(fs_info->pinned_extents, |
| 3565 | bytenr, bytenr + num_bytes - 1, GFP_NOFS); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3566 | return 0; |
| 3567 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3568 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3569 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 3570 | u64 num_bytes, int reserve) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3571 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3572 | spin_lock(&cache->space_info->lock); |
| 3573 | spin_lock(&cache->lock); |
| 3574 | if (reserve) { |
| 3575 | cache->reserved += num_bytes; |
| 3576 | cache->space_info->bytes_reserved += num_bytes; |
| 3577 | } else { |
| 3578 | cache->reserved -= num_bytes; |
| 3579 | cache->space_info->bytes_reserved -= num_bytes; |
| 3580 | } |
| 3581 | spin_unlock(&cache->lock); |
| 3582 | spin_unlock(&cache->space_info->lock); |
| 3583 | return 0; |
| 3584 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3585 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3586 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 3587 | struct btrfs_root *root) |
| 3588 | { |
| 3589 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3590 | struct btrfs_caching_control *next; |
| 3591 | struct btrfs_caching_control *caching_ctl; |
| 3592 | struct btrfs_block_group_cache *cache; |
| 3593 | |
| 3594 | down_write(&fs_info->extent_commit_sem); |
| 3595 | |
| 3596 | list_for_each_entry_safe(caching_ctl, next, |
| 3597 | &fs_info->caching_block_groups, list) { |
| 3598 | cache = caching_ctl->block_group; |
| 3599 | if (block_group_cache_done(cache)) { |
| 3600 | cache->last_byte_to_unpin = (u64)-1; |
| 3601 | list_del_init(&caching_ctl->list); |
| 3602 | put_caching_control(caching_ctl); |
| 3603 | } else { |
| 3604 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3609 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 3610 | else |
| 3611 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 3612 | |
| 3613 | up_write(&fs_info->extent_commit_sem); |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
| 3617 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 3618 | { |
| 3619 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3620 | struct btrfs_block_group_cache *cache = NULL; |
| 3621 | u64 len; |
| 3622 | |
| 3623 | while (start <= end) { |
| 3624 | if (!cache || |
| 3625 | start >= cache->key.objectid + cache->key.offset) { |
| 3626 | if (cache) |
| 3627 | btrfs_put_block_group(cache); |
| 3628 | cache = btrfs_lookup_block_group(fs_info, start); |
| 3629 | BUG_ON(!cache); |
| 3630 | } |
| 3631 | |
| 3632 | len = cache->key.objectid + cache->key.offset - start; |
| 3633 | len = min(len, end + 1 - start); |
| 3634 | |
| 3635 | if (start < cache->last_byte_to_unpin) { |
| 3636 | len = min(len, cache->last_byte_to_unpin - start); |
| 3637 | btrfs_add_free_space(cache, start, len); |
| 3638 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3639 | |
| 3640 | spin_lock(&cache->space_info->lock); |
| 3641 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3642 | cache->pinned -= len; |
| 3643 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3644 | spin_unlock(&cache->lock); |
| 3645 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3646 | |
| 3647 | start += len; |
| 3648 | } |
| 3649 | |
| 3650 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3651 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3652 | return 0; |
| 3653 | } |
| 3654 | |
| 3655 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3656 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3657 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3658 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3659 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3660 | u64 start; |
| 3661 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3662 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3663 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3664 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3665 | unpin = &fs_info->freed_extents[1]; |
| 3666 | else |
| 3667 | unpin = &fs_info->freed_extents[0]; |
| 3668 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3669 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3670 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 3671 | EXTENT_DIRTY); |
| 3672 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3673 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3674 | |
| 3675 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 3676 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3677 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3678 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3679 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3680 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3681 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3682 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3683 | } |
| 3684 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3685 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 3686 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3687 | struct btrfs_path *path, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3688 | u64 bytenr, u64 num_bytes, |
| 3689 | int is_data, int reserved, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3690 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3691 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3692 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3693 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3694 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3695 | if (is_data) |
| 3696 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3697 | |
Chris Mason | 444528b | 2009-10-14 09:38:28 -0400 | [diff] [blame] | 3698 | /* |
| 3699 | * discard is sloooow, and so triggering discards on |
| 3700 | * individual btree blocks isn't a good plan. Just |
| 3701 | * pin everything in discard mode. |
| 3702 | */ |
| 3703 | if (btrfs_test_opt(root, DISCARD)) |
| 3704 | goto pinit; |
| 3705 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3706 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 3707 | if (!buf) |
| 3708 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3709 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3710 | /* we can reuse a block if it hasn't been written |
| 3711 | * and it is from this transaction. We can't |
| 3712 | * reuse anything from the tree log root because |
| 3713 | * it has tiny sub-transactions. |
| 3714 | */ |
| 3715 | if (btrfs_buffer_uptodate(buf, 0) && |
| 3716 | btrfs_try_tree_lock(buf)) { |
| 3717 | u64 header_owner = btrfs_header_owner(buf); |
| 3718 | u64 header_transid = btrfs_header_generation(buf); |
| 3719 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
| 3720 | header_transid == trans->transid && |
| 3721 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3722 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3723 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 3724 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3725 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 3726 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3727 | free_extent_buffer(buf); |
| 3728 | pinit: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3729 | if (path) |
| 3730 | btrfs_set_path_blocking(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3731 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3732 | btrfs_pin_extent(root, bytenr, num_bytes, reserved); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3733 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3734 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3735 | return 0; |
| 3736 | } |
| 3737 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3738 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 3739 | struct btrfs_root *root, |
| 3740 | u64 bytenr, u64 num_bytes, u64 parent, |
| 3741 | u64 root_objectid, u64 owner_objectid, |
| 3742 | u64 owner_offset, int refs_to_drop, |
| 3743 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3744 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3745 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3746 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3747 | struct btrfs_fs_info *info = root->fs_info; |
| 3748 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3749 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3750 | struct btrfs_extent_item *ei; |
| 3751 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3752 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3753 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3754 | int extent_slot = 0; |
| 3755 | int found_extent = 0; |
| 3756 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3757 | u32 item_size; |
| 3758 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 3759 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3760 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3761 | if (!path) |
| 3762 | return -ENOMEM; |
| 3763 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 3764 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3765 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3766 | |
| 3767 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 3768 | BUG_ON(!is_data && refs_to_drop != 1); |
| 3769 | |
| 3770 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 3771 | bytenr, num_bytes, parent, |
| 3772 | root_objectid, owner_objectid, |
| 3773 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3774 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3775 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3776 | while (extent_slot >= 0) { |
| 3777 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3778 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3779 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3780 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3781 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3782 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3783 | found_extent = 1; |
| 3784 | break; |
| 3785 | } |
| 3786 | if (path->slots[0] - extent_slot > 5) |
| 3787 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3788 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3789 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3790 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3791 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 3792 | if (found_extent && item_size < sizeof(*ei)) |
| 3793 | found_extent = 0; |
| 3794 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3795 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3796 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3797 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3798 | NULL, refs_to_drop, |
| 3799 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3800 | BUG_ON(ret); |
| 3801 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3802 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3803 | |
| 3804 | key.objectid = bytenr; |
| 3805 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3806 | key.offset = num_bytes; |
| 3807 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3808 | ret = btrfs_search_slot(trans, extent_root, |
| 3809 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3810 | if (ret) { |
| 3811 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3812 | ", was looking for %llu\n", ret, |
| 3813 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3814 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3815 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3816 | BUG_ON(ret); |
| 3817 | extent_slot = path->slots[0]; |
| 3818 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3819 | } else { |
| 3820 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3821 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3822 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3823 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3824 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3825 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3826 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3827 | (unsigned long long)owner_objectid, |
| 3828 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3829 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3830 | |
| 3831 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3832 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3833 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3834 | if (item_size < sizeof(*ei)) { |
| 3835 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 3836 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 3837 | owner_objectid, 0); |
| 3838 | BUG_ON(ret < 0); |
| 3839 | |
| 3840 | btrfs_release_path(extent_root, path); |
| 3841 | path->leave_spinning = 1; |
| 3842 | |
| 3843 | key.objectid = bytenr; |
| 3844 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3845 | key.offset = num_bytes; |
| 3846 | |
| 3847 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 3848 | -1, 1); |
| 3849 | if (ret) { |
| 3850 | printk(KERN_ERR "umm, got %d back from search" |
| 3851 | ", was looking for %llu\n", ret, |
| 3852 | (unsigned long long)bytenr); |
| 3853 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3854 | } |
| 3855 | BUG_ON(ret); |
| 3856 | extent_slot = path->slots[0]; |
| 3857 | leaf = path->nodes[0]; |
| 3858 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3859 | } |
| 3860 | #endif |
| 3861 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3862 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3863 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3864 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3865 | struct btrfs_tree_block_info *bi; |
| 3866 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3867 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3868 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3869 | } |
| 3870 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3871 | refs = btrfs_extent_refs(leaf, ei); |
| 3872 | BUG_ON(refs < refs_to_drop); |
| 3873 | refs -= refs_to_drop; |
| 3874 | |
| 3875 | if (refs > 0) { |
| 3876 | if (extent_op) |
| 3877 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3878 | /* |
| 3879 | * In the case of inline back ref, reference count will |
| 3880 | * be updated by remove_extent_backref |
| 3881 | */ |
| 3882 | if (iref) { |
| 3883 | BUG_ON(!found_extent); |
| 3884 | } else { |
| 3885 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3886 | btrfs_mark_buffer_dirty(leaf); |
| 3887 | } |
| 3888 | if (found_extent) { |
| 3889 | ret = remove_extent_backref(trans, extent_root, path, |
| 3890 | iref, refs_to_drop, |
| 3891 | is_data); |
| 3892 | BUG_ON(ret); |
| 3893 | } |
| 3894 | } else { |
| 3895 | int mark_free = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3896 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3897 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3898 | if (found_extent) { |
| 3899 | BUG_ON(is_data && refs_to_drop != |
| 3900 | extent_data_ref_count(root, path, iref)); |
| 3901 | if (iref) { |
| 3902 | BUG_ON(path->slots[0] != extent_slot); |
| 3903 | } else { |
| 3904 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3905 | path->slots[0] = extent_slot; |
| 3906 | num_to_del = 2; |
| 3907 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3908 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3909 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3910 | ret = pin_down_bytes(trans, root, path, bytenr, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3911 | num_bytes, is_data, 0, &must_clean); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3912 | if (ret > 0) |
| 3913 | mark_free = 1; |
| 3914 | BUG_ON(ret < 0); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3915 | /* |
| 3916 | * it is going to be very rare for someone to be waiting |
| 3917 | * on the block we're freeing. del_items might need to |
| 3918 | * schedule, so rather than get fancy, just force it |
| 3919 | * to blocking here |
| 3920 | */ |
| 3921 | if (must_clean) |
| 3922 | btrfs_set_lock_blocking(must_clean); |
| 3923 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3924 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3925 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3926 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3927 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3928 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3929 | if (must_clean) { |
| 3930 | clean_tree_block(NULL, root, must_clean); |
| 3931 | btrfs_tree_unlock(must_clean); |
| 3932 | free_extent_buffer(must_clean); |
| 3933 | } |
| 3934 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3935 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3936 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 3937 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 3938 | } else { |
| 3939 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 3940 | bytenr >> PAGE_CACHE_SHIFT, |
| 3941 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3942 | } |
| 3943 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 3944 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 3945 | mark_free); |
| 3946 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3947 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3948 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3949 | return ret; |
| 3950 | } |
| 3951 | |
| 3952 | /* |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3953 | * when we free an extent, it is possible (and likely) that we free the last |
| 3954 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3955 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3956 | * removes it from the tree. |
| 3957 | */ |
| 3958 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 3959 | struct btrfs_root *root, u64 bytenr) |
| 3960 | { |
| 3961 | struct btrfs_delayed_ref_head *head; |
| 3962 | struct btrfs_delayed_ref_root *delayed_refs; |
| 3963 | struct btrfs_delayed_ref_node *ref; |
| 3964 | struct rb_node *node; |
| 3965 | int ret; |
| 3966 | |
| 3967 | delayed_refs = &trans->transaction->delayed_refs; |
| 3968 | spin_lock(&delayed_refs->lock); |
| 3969 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 3970 | if (!head) |
| 3971 | goto out; |
| 3972 | |
| 3973 | node = rb_prev(&head->node.rb_node); |
| 3974 | if (!node) |
| 3975 | goto out; |
| 3976 | |
| 3977 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 3978 | |
| 3979 | /* there are still entries for this ref, we can't drop it */ |
| 3980 | if (ref->bytenr == bytenr) |
| 3981 | goto out; |
| 3982 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3983 | if (head->extent_op) { |
| 3984 | if (!head->must_insert_reserved) |
| 3985 | goto out; |
| 3986 | kfree(head->extent_op); |
| 3987 | head->extent_op = NULL; |
| 3988 | } |
| 3989 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3990 | /* |
| 3991 | * waiting for the lock here would deadlock. If someone else has it |
| 3992 | * locked they are already in the process of dropping it anyway |
| 3993 | */ |
| 3994 | if (!mutex_trylock(&head->mutex)) |
| 3995 | goto out; |
| 3996 | |
| 3997 | /* |
| 3998 | * at this point we have a head with no other entries. Go |
| 3999 | * ahead and process it. |
| 4000 | */ |
| 4001 | head->node.in_tree = 0; |
| 4002 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4003 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4004 | delayed_refs->num_entries--; |
| 4005 | |
| 4006 | /* |
| 4007 | * we don't take a ref on the node because we're removing it from the |
| 4008 | * tree, so we just steal the ref the tree was holding. |
| 4009 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4010 | delayed_refs->num_heads--; |
| 4011 | if (list_empty(&head->cluster)) |
| 4012 | delayed_refs->num_heads_ready--; |
| 4013 | |
| 4014 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4015 | spin_unlock(&delayed_refs->lock); |
| 4016 | |
| 4017 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4018 | &head->node, head->extent_op, |
| 4019 | head->must_insert_reserved); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4020 | BUG_ON(ret); |
| 4021 | btrfs_put_delayed_ref(&head->node); |
| 4022 | return 0; |
| 4023 | out: |
| 4024 | spin_unlock(&delayed_refs->lock); |
| 4025 | return 0; |
| 4026 | } |
| 4027 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4028 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4029 | struct btrfs_root *root, |
| 4030 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4031 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4032 | { |
| 4033 | int ret; |
| 4034 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4035 | /* |
| 4036 | * tree log blocks never actually go into the extent allocation |
| 4037 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4038 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4039 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4040 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4041 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4042 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4043 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4044 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4045 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4046 | parent, root_objectid, (int)owner, |
| 4047 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4048 | BUG_ON(ret); |
| 4049 | ret = check_ref_cleanup(trans, root, bytenr); |
| 4050 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4051 | } else { |
| 4052 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4053 | parent, root_objectid, owner, |
| 4054 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4055 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4056 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4057 | return ret; |
| 4058 | } |
| 4059 | |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 4060 | int btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4061 | struct btrfs_root *root, |
| 4062 | u64 bytenr, u32 blocksize, |
| 4063 | u64 parent, u64 root_objectid, int level) |
| 4064 | { |
| 4065 | u64 used; |
| 4066 | spin_lock(&root->node_lock); |
| 4067 | used = btrfs_root_used(&root->root_item) - blocksize; |
| 4068 | btrfs_set_root_used(&root->root_item, used); |
| 4069 | spin_unlock(&root->node_lock); |
| 4070 | |
| 4071 | return btrfs_free_extent(trans, root, bytenr, blocksize, |
| 4072 | parent, root_objectid, level, 0); |
| 4073 | } |
| 4074 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4075 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4076 | { |
| 4077 | u64 mask = ((u64)root->stripesize - 1); |
| 4078 | u64 ret = (val + mask) & ~mask; |
| 4079 | return ret; |
| 4080 | } |
| 4081 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4082 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4083 | * when we wait for progress in the block group caching, its because |
| 4084 | * our allocation attempt failed at least once. So, we must sleep |
| 4085 | * and let some progress happen before we try again. |
| 4086 | * |
| 4087 | * This function will sleep at least once waiting for new free space to |
| 4088 | * show up, and then it will check the block group free space numbers |
| 4089 | * for our min num_bytes. Another option is to have it go ahead |
| 4090 | * and look in the rbtree for a free extent of a given size, but this |
| 4091 | * is a good start. |
| 4092 | */ |
| 4093 | static noinline int |
| 4094 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4095 | u64 num_bytes) |
| 4096 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4097 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4098 | DEFINE_WAIT(wait); |
| 4099 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4100 | caching_ctl = get_caching_control(cache); |
| 4101 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4102 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4103 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4104 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4105 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4106 | |
| 4107 | put_caching_control(caching_ctl); |
| 4108 | return 0; |
| 4109 | } |
| 4110 | |
| 4111 | static noinline int |
| 4112 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4113 | { |
| 4114 | struct btrfs_caching_control *caching_ctl; |
| 4115 | DEFINE_WAIT(wait); |
| 4116 | |
| 4117 | caching_ctl = get_caching_control(cache); |
| 4118 | if (!caching_ctl) |
| 4119 | return 0; |
| 4120 | |
| 4121 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4122 | |
| 4123 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4124 | return 0; |
| 4125 | } |
| 4126 | |
| 4127 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4128 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4129 | LOOP_CACHING_NOWAIT = 1, |
| 4130 | LOOP_CACHING_WAIT = 2, |
| 4131 | LOOP_ALLOC_CHUNK = 3, |
| 4132 | LOOP_NO_EMPTY_SIZE = 4, |
| 4133 | }; |
| 4134 | |
| 4135 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4136 | * walks the btree of allocated extents and find a hole of a given size. |
| 4137 | * The key ins is changed to record the hole: |
| 4138 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4139 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4140 | * ins->offset == number of blocks |
| 4141 | * Any available blocks before search_start are skipped. |
| 4142 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4143 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4144 | struct btrfs_root *orig_root, |
| 4145 | u64 num_bytes, u64 empty_size, |
| 4146 | u64 search_start, u64 search_end, |
| 4147 | u64 hint_byte, struct btrfs_key *ins, |
| 4148 | u64 exclude_start, u64 exclude_nr, |
| 4149 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4150 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4151 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4152 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4153 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4154 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4155 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4156 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4157 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4158 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4159 | int last_ptr_loop = 0; |
| 4160 | int loop = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4161 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4162 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4163 | bool failed_alloc = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4164 | u64 ideal_cache_percent = 0; |
| 4165 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4166 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4167 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4168 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4169 | ins->objectid = 0; |
| 4170 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4171 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4172 | space_info = __find_space_info(root->fs_info, data); |
| 4173 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4174 | if (orig_root->ref_cows || empty_size) |
| 4175 | allowed_chunk_alloc = 1; |
| 4176 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4177 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4178 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4179 | if (!btrfs_test_opt(root, SSD)) |
| 4180 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4181 | } |
| 4182 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4183 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 4184 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4185 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4186 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4187 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4188 | spin_lock(&last_ptr->lock); |
| 4189 | if (last_ptr->block_group) |
| 4190 | hint_byte = last_ptr->window_start; |
| 4191 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4192 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4193 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4194 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4195 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4196 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4197 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4198 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4199 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4200 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4201 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4202 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4203 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4204 | /* |
| 4205 | * we don't want to use the block group if it doesn't match our |
| 4206 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4207 | * |
| 4208 | * However if we are re-searching with an ideal block group |
| 4209 | * 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] | 4210 | */ |
| 4211 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4212 | (block_group->cached != BTRFS_CACHE_NO || |
| 4213 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4214 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4215 | if (list_empty(&block_group->list) || |
| 4216 | block_group->ro) { |
| 4217 | /* |
| 4218 | * someone is removing this block group, |
| 4219 | * we can't jump into the have_block_group |
| 4220 | * target because our list pointers are not |
| 4221 | * valid |
| 4222 | */ |
| 4223 | btrfs_put_block_group(block_group); |
| 4224 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4225 | } else { |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4226 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4227 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4228 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4229 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4230 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4231 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4232 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4233 | down_read(&space_info->groups_sem); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4234 | list_for_each_entry(block_group, &space_info->block_groups, list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4235 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4236 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4237 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4238 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4239 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4240 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4241 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4242 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4243 | u64 free_percent; |
| 4244 | |
| 4245 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4246 | free_percent *= 100; |
| 4247 | free_percent = div64_u64(free_percent, |
| 4248 | block_group->key.offset); |
| 4249 | free_percent = 100 - free_percent; |
| 4250 | if (free_percent > ideal_cache_percent && |
| 4251 | likely(!block_group->ro)) { |
| 4252 | ideal_cache_offset = block_group->key.objectid; |
| 4253 | ideal_cache_percent = free_percent; |
| 4254 | } |
| 4255 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4256 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4257 | * We only want to start kthread caching if we are at |
| 4258 | * the point where we will wait for caching to make |
| 4259 | * progress, or if our ideal search is over and we've |
| 4260 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4261 | */ |
| 4262 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4263 | (loop > LOOP_FIND_IDEAL && |
| 4264 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4265 | ret = cache_block_group(block_group); |
| 4266 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4267 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4268 | found_uncached_bg = true; |
| 4269 | |
| 4270 | /* |
| 4271 | * If loop is set for cached only, try the next block |
| 4272 | * group. |
| 4273 | */ |
| 4274 | if (loop == LOOP_FIND_IDEAL) |
| 4275 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4276 | } |
| 4277 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4278 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4279 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4280 | found_uncached_bg = true; |
| 4281 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4282 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4283 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4284 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4285 | /* |
| 4286 | * Ok we want to try and use the cluster allocator, so lets look |
| 4287 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 4288 | * have tried the cluster allocator plenty of times at this |
| 4289 | * point and not have found anything, so we are likely way too |
| 4290 | * fragmented for the clustering stuff to find anything, so lets |
| 4291 | * just skip it and let the allocator find whatever block it can |
| 4292 | * find |
| 4293 | */ |
| 4294 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4295 | /* |
| 4296 | * the refill lock keeps out other |
| 4297 | * people trying to start a new cluster |
| 4298 | */ |
| 4299 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4300 | if (last_ptr->block_group && |
| 4301 | (last_ptr->block_group->ro || |
| 4302 | !block_group_bits(last_ptr->block_group, data))) { |
| 4303 | offset = 0; |
| 4304 | goto refill_cluster; |
| 4305 | } |
| 4306 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4307 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 4308 | num_bytes, search_start); |
| 4309 | if (offset) { |
| 4310 | /* we have a block, we're done */ |
| 4311 | spin_unlock(&last_ptr->refill_lock); |
| 4312 | goto checks; |
| 4313 | } |
| 4314 | |
| 4315 | spin_lock(&last_ptr->lock); |
| 4316 | /* |
| 4317 | * whoops, this cluster doesn't actually point to |
| 4318 | * this block group. Get a ref on the block |
| 4319 | * group is does point to and try again |
| 4320 | */ |
| 4321 | if (!last_ptr_loop && last_ptr->block_group && |
| 4322 | last_ptr->block_group != block_group) { |
| 4323 | |
| 4324 | btrfs_put_block_group(block_group); |
| 4325 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4326 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4327 | spin_unlock(&last_ptr->lock); |
| 4328 | spin_unlock(&last_ptr->refill_lock); |
| 4329 | |
| 4330 | last_ptr_loop = 1; |
| 4331 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4332 | /* |
| 4333 | * we know this block group is properly |
| 4334 | * in the list because |
| 4335 | * btrfs_remove_block_group, drops the |
| 4336 | * cluster before it removes the block |
| 4337 | * group from the list |
| 4338 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4339 | goto have_block_group; |
| 4340 | } |
| 4341 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4342 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4343 | /* |
| 4344 | * this cluster didn't work out, free it and |
| 4345 | * start over |
| 4346 | */ |
| 4347 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 4348 | |
| 4349 | last_ptr_loop = 0; |
| 4350 | |
| 4351 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 4352 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4353 | block_group, last_ptr, |
| 4354 | offset, num_bytes, |
| 4355 | empty_cluster + empty_size); |
| 4356 | if (ret == 0) { |
| 4357 | /* |
| 4358 | * now pull our allocation out of this |
| 4359 | * cluster |
| 4360 | */ |
| 4361 | offset = btrfs_alloc_from_cluster(block_group, |
| 4362 | last_ptr, num_bytes, |
| 4363 | search_start); |
| 4364 | if (offset) { |
| 4365 | /* we found one, proceed */ |
| 4366 | spin_unlock(&last_ptr->refill_lock); |
| 4367 | goto checks; |
| 4368 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4369 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 4370 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4371 | spin_unlock(&last_ptr->refill_lock); |
| 4372 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4373 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4374 | wait_block_group_cache_progress(block_group, |
| 4375 | num_bytes + empty_cluster + empty_size); |
| 4376 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4377 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4378 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4379 | /* |
| 4380 | * at this point we either didn't find a cluster |
| 4381 | * or we weren't able to allocate a block from our |
| 4382 | * cluster. Free the cluster we've been trying |
| 4383 | * to use, and go to the next block group |
| 4384 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4385 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4386 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4387 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4388 | } |
| 4389 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4390 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 4391 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4392 | /* |
| 4393 | * If we didn't find a chunk, and we haven't failed on this |
| 4394 | * block group before, and this block group is in the middle of |
| 4395 | * caching and we are ok with waiting, then go ahead and wait |
| 4396 | * for progress to be made, and set failed_alloc to true. |
| 4397 | * |
| 4398 | * If failed_alloc is true then we've already waited on this |
| 4399 | * block group once and should move on to the next block group. |
| 4400 | */ |
| 4401 | if (!offset && !failed_alloc && !cached && |
| 4402 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4403 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4404 | num_bytes + empty_size); |
| 4405 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4406 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4407 | } else if (!offset) { |
| 4408 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4409 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4410 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4411 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4412 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4413 | if (search_start + num_bytes >= search_end) { |
| 4414 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4415 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4416 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4417 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4418 | /* move on to the next group */ |
| 4419 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4420 | block_group->key.objectid + block_group->key.offset) { |
| 4421 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4422 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4423 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4424 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4425 | if (exclude_nr > 0 && |
| 4426 | (search_start + num_bytes > exclude_start && |
| 4427 | search_start < exclude_start + exclude_nr)) { |
| 4428 | search_start = exclude_start + exclude_nr; |
| 4429 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4430 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4431 | /* |
| 4432 | * if search_start is still in this block group |
| 4433 | * then we just re-search this block group |
| 4434 | */ |
| 4435 | if (search_start >= block_group->key.objectid && |
| 4436 | search_start < (block_group->key.objectid + |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4437 | block_group->key.offset)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4438 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4439 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4440 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4441 | |
| 4442 | ins->objectid = search_start; |
| 4443 | ins->offset = num_bytes; |
| 4444 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4445 | if (offset < search_start) |
| 4446 | btrfs_add_free_space(block_group, offset, |
| 4447 | search_start - offset); |
| 4448 | BUG_ON(offset > search_start); |
| 4449 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4450 | update_reserved_extents(block_group, num_bytes, 1); |
| 4451 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4452 | /* we are all good, lets return */ |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4453 | break; |
| 4454 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4455 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4456 | failed_alloc = false; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4457 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4458 | } |
| 4459 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4460 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4461 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 4462 | * for them to make caching progress. Also |
| 4463 | * determine the best possible bg to cache |
| 4464 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 4465 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4466 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 4467 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 4468 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 4469 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4470 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4471 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 4472 | (found_uncached_bg || empty_size || empty_cluster || |
| 4473 | allowed_chunk_alloc)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4474 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4475 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4476 | loop++; |
| 4477 | if (!ideal_cache_percent && |
| 4478 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4479 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4480 | |
| 4481 | /* |
| 4482 | * 1 of the following 2 things have happened so far |
| 4483 | * |
| 4484 | * 1) We found an ideal block group for caching that |
| 4485 | * is mostly full and will cache quickly, so we might |
| 4486 | * as well wait for it. |
| 4487 | * |
| 4488 | * 2) We searched for cached only and we didn't find |
| 4489 | * anything, and we didn't start any caching kthreads |
| 4490 | * either, so chances are we will loop through and |
| 4491 | * start a couple caching kthreads, and then come back |
| 4492 | * around and just wait for them. This will be slower |
| 4493 | * because we will have 2 caching kthreads reading at |
| 4494 | * the same time when we could have just started one |
| 4495 | * and waited for it to get far enough to give us an |
| 4496 | * allocation, so go ahead and go to the wait caching |
| 4497 | * loop. |
| 4498 | */ |
| 4499 | loop = LOOP_CACHING_WAIT; |
| 4500 | search_start = ideal_cache_offset; |
| 4501 | ideal_cache_percent = 0; |
| 4502 | goto ideal_cache; |
| 4503 | } else if (loop == LOOP_FIND_IDEAL) { |
| 4504 | /* |
| 4505 | * Didn't find a uncached bg, wait on anything we find |
| 4506 | * next. |
| 4507 | */ |
| 4508 | loop = LOOP_CACHING_WAIT; |
| 4509 | goto search; |
| 4510 | } |
| 4511 | |
| 4512 | if (loop < LOOP_CACHING_WAIT) { |
| 4513 | loop++; |
| 4514 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4518 | empty_size = 0; |
| 4519 | empty_cluster = 0; |
| 4520 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4521 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4522 | if (allowed_chunk_alloc) { |
| 4523 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 4524 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4525 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4526 | done_chunk_alloc = 1; |
| 4527 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4528 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4529 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4530 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4531 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4532 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4533 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4534 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4535 | ret = -ENOSPC; |
| 4536 | } else if (!ins->objectid) { |
| 4537 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 4538 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4539 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4540 | /* we found what we needed */ |
| 4541 | if (ins->objectid) { |
| 4542 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4543 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4544 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4545 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4546 | ret = 0; |
| 4547 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4548 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4549 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4550 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4551 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4552 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 4553 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4554 | { |
| 4555 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4556 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4557 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4558 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 4559 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4560 | info->bytes_pinned - info->bytes_reserved - |
| 4561 | info->bytes_super), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4562 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4563 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4564 | " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu" |
| 4565 | "\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 4566 | (unsigned long long)info->total_bytes, |
| 4567 | (unsigned long long)info->bytes_pinned, |
| 4568 | (unsigned long long)info->bytes_delalloc, |
| 4569 | (unsigned long long)info->bytes_may_use, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4570 | (unsigned long long)info->bytes_used, |
| 4571 | (unsigned long long)info->bytes_root, |
| 4572 | (unsigned long long)info->bytes_super, |
| 4573 | (unsigned long long)info->bytes_reserved); |
| 4574 | spin_unlock(&info->lock); |
| 4575 | |
| 4576 | if (!dump_block_groups) |
| 4577 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4578 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4579 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 4580 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4581 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4582 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 4583 | "%llu pinned %llu reserved\n", |
| 4584 | (unsigned long long)cache->key.objectid, |
| 4585 | (unsigned long long)cache->key.offset, |
| 4586 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 4587 | (unsigned long long)cache->pinned, |
| 4588 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4589 | btrfs_dump_free_space(cache, bytes); |
| 4590 | spin_unlock(&cache->lock); |
| 4591 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4592 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4593 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4594 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4595 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 4596 | struct btrfs_root *root, |
| 4597 | u64 num_bytes, u64 min_alloc_size, |
| 4598 | u64 empty_size, u64 hint_byte, |
| 4599 | u64 search_end, struct btrfs_key *ins, |
| 4600 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4601 | { |
| 4602 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 4603 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4604 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4605 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4606 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4607 | /* |
| 4608 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 4609 | * is not called recursively on allocations |
| 4610 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 4611 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4612 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4613 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4614 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4615 | WARN_ON(num_bytes < root->sectorsize); |
| 4616 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 4617 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 4618 | trans->alloc_exclude_start, |
| 4619 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4620 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4621 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 4622 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4623 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4624 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4625 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 4626 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4627 | goto again; |
| 4628 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4629 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4630 | struct btrfs_space_info *sinfo; |
| 4631 | |
| 4632 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4633 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 4634 | "wanted %llu\n", (unsigned long long)data, |
| 4635 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4636 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4637 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4638 | |
| 4639 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4640 | } |
| 4641 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4642 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 4643 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4644 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4645 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4646 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4647 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 4648 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4649 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 4650 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4651 | return -ENOSPC; |
| 4652 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4653 | |
| 4654 | ret = btrfs_discard_extent(root, start, len); |
| 4655 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4656 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4657 | update_reserved_extents(cache, len, 0); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4658 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4659 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4660 | return ret; |
| 4661 | } |
| 4662 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4663 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4664 | struct btrfs_root *root, |
| 4665 | u64 parent, u64 root_objectid, |
| 4666 | u64 flags, u64 owner, u64 offset, |
| 4667 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4668 | { |
| 4669 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4670 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4671 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4672 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4673 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4674 | struct extent_buffer *leaf; |
| 4675 | int type; |
| 4676 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4677 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4678 | if (parent > 0) |
| 4679 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4680 | else |
| 4681 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4682 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4683 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4684 | |
| 4685 | path = btrfs_alloc_path(); |
| 4686 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4687 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4688 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4689 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4690 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4691 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4692 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4693 | leaf = path->nodes[0]; |
| 4694 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4695 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4696 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4697 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4698 | btrfs_set_extent_flags(leaf, extent_item, |
| 4699 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4700 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4701 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4702 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4703 | if (parent > 0) { |
| 4704 | struct btrfs_shared_data_ref *ref; |
| 4705 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4706 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4707 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4708 | } else { |
| 4709 | struct btrfs_extent_data_ref *ref; |
| 4710 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4711 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4712 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4713 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4714 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4715 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4716 | |
| 4717 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4718 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4719 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4720 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4721 | 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4722 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4723 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4724 | "%llu\n", (unsigned long long)ins->objectid, |
| 4725 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4726 | BUG(); |
| 4727 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4728 | return ret; |
| 4729 | } |
| 4730 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4731 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 4732 | struct btrfs_root *root, |
| 4733 | u64 parent, u64 root_objectid, |
| 4734 | u64 flags, struct btrfs_disk_key *key, |
| 4735 | int level, struct btrfs_key *ins) |
| 4736 | { |
| 4737 | int ret; |
| 4738 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4739 | struct btrfs_extent_item *extent_item; |
| 4740 | struct btrfs_tree_block_info *block_info; |
| 4741 | struct btrfs_extent_inline_ref *iref; |
| 4742 | struct btrfs_path *path; |
| 4743 | struct extent_buffer *leaf; |
| 4744 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 4745 | |
| 4746 | path = btrfs_alloc_path(); |
| 4747 | BUG_ON(!path); |
| 4748 | |
| 4749 | path->leave_spinning = 1; |
| 4750 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4751 | ins, size); |
| 4752 | BUG_ON(ret); |
| 4753 | |
| 4754 | leaf = path->nodes[0]; |
| 4755 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4756 | struct btrfs_extent_item); |
| 4757 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4758 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4759 | btrfs_set_extent_flags(leaf, extent_item, |
| 4760 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 4761 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 4762 | |
| 4763 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 4764 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 4765 | |
| 4766 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4767 | if (parent > 0) { |
| 4768 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4769 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4770 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 4771 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4772 | } else { |
| 4773 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4774 | BTRFS_TREE_BLOCK_REF_KEY); |
| 4775 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 4776 | } |
| 4777 | |
| 4778 | btrfs_mark_buffer_dirty(leaf); |
| 4779 | btrfs_free_path(path); |
| 4780 | |
| 4781 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4782 | 1, 0); |
| 4783 | if (ret) { |
| 4784 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4785 | "%llu\n", (unsigned long long)ins->objectid, |
| 4786 | (unsigned long long)ins->offset); |
| 4787 | BUG(); |
| 4788 | } |
| 4789 | return ret; |
| 4790 | } |
| 4791 | |
| 4792 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4793 | struct btrfs_root *root, |
| 4794 | u64 root_objectid, u64 owner, |
| 4795 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4796 | { |
| 4797 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4798 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4799 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4800 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4801 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 4802 | 0, root_objectid, owner, offset, |
| 4803 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4804 | return ret; |
| 4805 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4806 | |
| 4807 | /* |
| 4808 | * this is used by the tree logging recovery code. It records that |
| 4809 | * an extent has been allocated and makes sure to clear the free |
| 4810 | * space cache bits as well |
| 4811 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4812 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 4813 | struct btrfs_root *root, |
| 4814 | u64 root_objectid, u64 owner, u64 offset, |
| 4815 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4816 | { |
| 4817 | int ret; |
| 4818 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4819 | struct btrfs_caching_control *caching_ctl; |
| 4820 | u64 start = ins->objectid; |
| 4821 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4822 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4823 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4824 | cache_block_group(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4825 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4826 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4827 | if (!caching_ctl) { |
| 4828 | BUG_ON(!block_group_cache_done(block_group)); |
| 4829 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 4830 | BUG_ON(ret); |
| 4831 | } else { |
| 4832 | mutex_lock(&caching_ctl->mutex); |
| 4833 | |
| 4834 | if (start >= caching_ctl->progress) { |
| 4835 | ret = add_excluded_extent(root, start, num_bytes); |
| 4836 | BUG_ON(ret); |
| 4837 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 4838 | ret = btrfs_remove_free_space(block_group, |
| 4839 | start, num_bytes); |
| 4840 | BUG_ON(ret); |
| 4841 | } else { |
| 4842 | num_bytes = caching_ctl->progress - start; |
| 4843 | ret = btrfs_remove_free_space(block_group, |
| 4844 | start, num_bytes); |
| 4845 | BUG_ON(ret); |
| 4846 | |
| 4847 | start = caching_ctl->progress; |
| 4848 | num_bytes = ins->objectid + ins->offset - |
| 4849 | caching_ctl->progress; |
| 4850 | ret = add_excluded_extent(root, start, num_bytes); |
| 4851 | BUG_ON(ret); |
| 4852 | } |
| 4853 | |
| 4854 | mutex_unlock(&caching_ctl->mutex); |
| 4855 | put_caching_control(caching_ctl); |
| 4856 | } |
| 4857 | |
| 4858 | update_reserved_extents(block_group, ins->offset, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4859 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4860 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 4861 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4862 | return ret; |
| 4863 | } |
| 4864 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4865 | /* |
| 4866 | * finds a free extent and does all the dirty work required for allocation |
| 4867 | * returns the key for the extent through ins, and a tree buffer for |
| 4868 | * the first block of the extent through buf. |
| 4869 | * |
| 4870 | * returns 0 if everything worked, non-zero otherwise. |
| 4871 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4872 | static int alloc_tree_block(struct btrfs_trans_handle *trans, |
| 4873 | struct btrfs_root *root, |
| 4874 | u64 num_bytes, u64 parent, u64 root_objectid, |
| 4875 | struct btrfs_disk_key *key, int level, |
| 4876 | u64 empty_size, u64 hint_byte, u64 search_end, |
| 4877 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4878 | { |
| 4879 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4880 | u64 flags = 0; |
| 4881 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4882 | ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes, |
| 4883 | empty_size, hint_byte, search_end, |
| 4884 | ins, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4885 | if (ret) |
| 4886 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4887 | |
| 4888 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4889 | if (parent == 0) |
| 4890 | parent = ins->objectid; |
| 4891 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4892 | } else |
| 4893 | BUG_ON(parent > 0); |
| 4894 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4895 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4896 | struct btrfs_delayed_extent_op *extent_op; |
| 4897 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 4898 | BUG_ON(!extent_op); |
| 4899 | if (key) |
| 4900 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4901 | else |
| 4902 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4903 | extent_op->flags_to_set = flags; |
| 4904 | extent_op->update_key = 1; |
| 4905 | extent_op->update_flags = 1; |
| 4906 | extent_op->is_data = 0; |
| 4907 | |
| 4908 | ret = btrfs_add_delayed_tree_ref(trans, ins->objectid, |
| 4909 | ins->offset, parent, root_objectid, |
| 4910 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 4911 | extent_op); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4912 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4913 | } |
Yan, Zheng | 86b9f2e | 2009-11-12 09:36:50 +0000 | [diff] [blame] | 4914 | |
| 4915 | if (root_objectid == root->root_key.objectid) { |
| 4916 | u64 used; |
| 4917 | spin_lock(&root->node_lock); |
| 4918 | used = btrfs_root_used(&root->root_item) + num_bytes; |
| 4919 | btrfs_set_root_used(&root->root_item, used); |
| 4920 | spin_unlock(&root->node_lock); |
| 4921 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4922 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4923 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4924 | |
| 4925 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 4926 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4927 | u64 bytenr, u32 blocksize, |
| 4928 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4929 | { |
| 4930 | struct extent_buffer *buf; |
| 4931 | |
| 4932 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 4933 | if (!buf) |
| 4934 | return ERR_PTR(-ENOMEM); |
| 4935 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4936 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4937 | btrfs_tree_lock(buf); |
| 4938 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4939 | |
| 4940 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4941 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4942 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4943 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 4944 | /* |
| 4945 | * we allow two log transactions at a time, use different |
| 4946 | * EXENT bit to differentiate dirty pages. |
| 4947 | */ |
| 4948 | if (root->log_transid % 2 == 0) |
| 4949 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 4950 | buf->start + buf->len - 1, GFP_NOFS); |
| 4951 | else |
| 4952 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 4953 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4954 | } else { |
| 4955 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4956 | buf->start + buf->len - 1, GFP_NOFS); |
| 4957 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4958 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4959 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4960 | return buf; |
| 4961 | } |
| 4962 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4963 | /* |
| 4964 | * helper function to allocate a block for a given tree |
| 4965 | * returns the tree buffer or NULL. |
| 4966 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4967 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4968 | struct btrfs_root *root, u32 blocksize, |
| 4969 | u64 parent, u64 root_objectid, |
| 4970 | struct btrfs_disk_key *key, int level, |
| 4971 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4972 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4973 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4974 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4975 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4976 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4977 | ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid, |
| 4978 | key, level, empty_size, hint, (u64)-1, &ins); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4979 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4980 | BUG_ON(ret > 0); |
| 4981 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4982 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4983 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4984 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 4985 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4986 | return buf; |
| 4987 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4988 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4989 | struct walk_control { |
| 4990 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4991 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4992 | struct btrfs_key update_progress; |
| 4993 | int stage; |
| 4994 | int level; |
| 4995 | int shared_level; |
| 4996 | int update_ref; |
| 4997 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4998 | int reada_slot; |
| 4999 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5000 | }; |
| 5001 | |
| 5002 | #define DROP_REFERENCE 1 |
| 5003 | #define UPDATE_BACKREF 2 |
| 5004 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5005 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5006 | struct btrfs_root *root, |
| 5007 | struct walk_control *wc, |
| 5008 | struct btrfs_path *path) |
| 5009 | { |
| 5010 | u64 bytenr; |
| 5011 | u64 generation; |
| 5012 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5013 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5014 | u64 last = 0; |
| 5015 | u32 nritems; |
| 5016 | u32 blocksize; |
| 5017 | struct btrfs_key key; |
| 5018 | struct extent_buffer *eb; |
| 5019 | int ret; |
| 5020 | int slot; |
| 5021 | int nread = 0; |
| 5022 | |
| 5023 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5024 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5025 | wc->reada_count = max(wc->reada_count, 2); |
| 5026 | } else { |
| 5027 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5028 | wc->reada_count = min_t(int, wc->reada_count, |
| 5029 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5030 | } |
| 5031 | |
| 5032 | eb = path->nodes[wc->level]; |
| 5033 | nritems = btrfs_header_nritems(eb); |
| 5034 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5035 | |
| 5036 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5037 | if (nread >= wc->reada_count) |
| 5038 | break; |
| 5039 | |
| 5040 | cond_resched(); |
| 5041 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5042 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5043 | |
| 5044 | if (slot == path->slots[wc->level]) |
| 5045 | goto reada; |
| 5046 | |
| 5047 | if (wc->stage == UPDATE_BACKREF && |
| 5048 | generation <= root->root_key.offset) |
| 5049 | continue; |
| 5050 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5051 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5052 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5053 | &refs, &flags); |
| 5054 | BUG_ON(ret); |
| 5055 | BUG_ON(refs == 0); |
| 5056 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5057 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5058 | if (refs == 1) |
| 5059 | goto reada; |
| 5060 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5061 | if (wc->level == 1 && |
| 5062 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5063 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5064 | if (!wc->update_ref || |
| 5065 | generation <= root->root_key.offset) |
| 5066 | continue; |
| 5067 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5068 | ret = btrfs_comp_cpu_keys(&key, |
| 5069 | &wc->update_progress); |
| 5070 | if (ret < 0) |
| 5071 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5072 | } else { |
| 5073 | if (wc->level == 1 && |
| 5074 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5075 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5076 | } |
| 5077 | reada: |
| 5078 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5079 | generation); |
| 5080 | if (ret) |
| 5081 | break; |
| 5082 | last = bytenr + blocksize; |
| 5083 | nread++; |
| 5084 | } |
| 5085 | wc->reada_slot = slot; |
| 5086 | } |
| 5087 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5088 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5089 | * hepler to process tree block while walking down the tree. |
| 5090 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5091 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5092 | * back refs for pointers in the block. |
| 5093 | * |
| 5094 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5095 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5096 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5097 | struct btrfs_root *root, |
| 5098 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5099 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5100 | { |
| 5101 | int level = wc->level; |
| 5102 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5103 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5104 | int ret; |
| 5105 | |
| 5106 | if (wc->stage == UPDATE_BACKREF && |
| 5107 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5108 | return 1; |
| 5109 | |
| 5110 | /* |
| 5111 | * when reference count of tree block is 1, it won't increase |
| 5112 | * again. once full backref flag is set, we never clear it. |
| 5113 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5114 | if (lookup_info && |
| 5115 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5116 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5117 | BUG_ON(!path->locks[level]); |
| 5118 | ret = btrfs_lookup_extent_info(trans, root, |
| 5119 | eb->start, eb->len, |
| 5120 | &wc->refs[level], |
| 5121 | &wc->flags[level]); |
| 5122 | BUG_ON(ret); |
| 5123 | BUG_ON(wc->refs[level] == 0); |
| 5124 | } |
| 5125 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5126 | if (wc->stage == DROP_REFERENCE) { |
| 5127 | if (wc->refs[level] > 1) |
| 5128 | return 1; |
| 5129 | |
| 5130 | if (path->locks[level] && !wc->keep_locks) { |
| 5131 | btrfs_tree_unlock(eb); |
| 5132 | path->locks[level] = 0; |
| 5133 | } |
| 5134 | return 0; |
| 5135 | } |
| 5136 | |
| 5137 | /* wc->stage == UPDATE_BACKREF */ |
| 5138 | if (!(wc->flags[level] & flag)) { |
| 5139 | BUG_ON(!path->locks[level]); |
| 5140 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5141 | BUG_ON(ret); |
| 5142 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5143 | BUG_ON(ret); |
| 5144 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5145 | eb->len, flag, 0); |
| 5146 | BUG_ON(ret); |
| 5147 | wc->flags[level] |= flag; |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * the block is shared by multiple trees, so it's not good to |
| 5152 | * keep the tree lock |
| 5153 | */ |
| 5154 | if (path->locks[level] && level > 0) { |
| 5155 | btrfs_tree_unlock(eb); |
| 5156 | path->locks[level] = 0; |
| 5157 | } |
| 5158 | return 0; |
| 5159 | } |
| 5160 | |
| 5161 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5162 | * hepler to process tree block pointer. |
| 5163 | * |
| 5164 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5165 | * reference count of the block pointed to. if the block |
| 5166 | * is shared and we need update back refs for the subtree |
| 5167 | * rooted at the block, this function changes wc->stage to |
| 5168 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5169 | * need to update back, this function drops the reference |
| 5170 | * to the block. |
| 5171 | * |
| 5172 | * NOTE: return value 1 means we should stop walking down. |
| 5173 | */ |
| 5174 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5175 | struct btrfs_root *root, |
| 5176 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5177 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5178 | { |
| 5179 | u64 bytenr; |
| 5180 | u64 generation; |
| 5181 | u64 parent; |
| 5182 | u32 blocksize; |
| 5183 | struct btrfs_key key; |
| 5184 | struct extent_buffer *next; |
| 5185 | int level = wc->level; |
| 5186 | int reada = 0; |
| 5187 | int ret = 0; |
| 5188 | |
| 5189 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5190 | path->slots[level]); |
| 5191 | /* |
| 5192 | * if the lower level block was created before the snapshot |
| 5193 | * was created, we know there is no need to update back refs |
| 5194 | * for the subtree |
| 5195 | */ |
| 5196 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5197 | generation <= root->root_key.offset) { |
| 5198 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5199 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5200 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5201 | |
| 5202 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5203 | blocksize = btrfs_level_size(root, level - 1); |
| 5204 | |
| 5205 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5206 | if (!next) { |
| 5207 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame^] | 5208 | if (!next) |
| 5209 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5210 | reada = 1; |
| 5211 | } |
| 5212 | btrfs_tree_lock(next); |
| 5213 | btrfs_set_lock_blocking(next); |
| 5214 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5215 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5216 | &wc->refs[level - 1], |
| 5217 | &wc->flags[level - 1]); |
| 5218 | BUG_ON(ret); |
| 5219 | BUG_ON(wc->refs[level - 1] == 0); |
| 5220 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5221 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5222 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5223 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5224 | if (level == 1 && |
| 5225 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5226 | goto skip; |
| 5227 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5228 | if (!wc->update_ref || |
| 5229 | generation <= root->root_key.offset) |
| 5230 | goto skip; |
| 5231 | |
| 5232 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5233 | path->slots[level]); |
| 5234 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5235 | if (ret < 0) |
| 5236 | goto skip; |
| 5237 | |
| 5238 | wc->stage = UPDATE_BACKREF; |
| 5239 | wc->shared_level = level - 1; |
| 5240 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5241 | } else { |
| 5242 | if (level == 1 && |
| 5243 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5244 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5248 | btrfs_tree_unlock(next); |
| 5249 | free_extent_buffer(next); |
| 5250 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5251 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | if (!next) { |
| 5255 | if (reada && level == 1) |
| 5256 | reada_walk_down(trans, root, wc, path); |
| 5257 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5258 | btrfs_tree_lock(next); |
| 5259 | btrfs_set_lock_blocking(next); |
| 5260 | } |
| 5261 | |
| 5262 | level--; |
| 5263 | BUG_ON(level != btrfs_header_level(next)); |
| 5264 | path->nodes[level] = next; |
| 5265 | path->slots[level] = 0; |
| 5266 | path->locks[level] = 1; |
| 5267 | wc->level = level; |
| 5268 | if (wc->level == 1) |
| 5269 | wc->reada_slot = 0; |
| 5270 | return 0; |
| 5271 | skip: |
| 5272 | wc->refs[level - 1] = 0; |
| 5273 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5274 | if (wc->stage == DROP_REFERENCE) { |
| 5275 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5276 | parent = path->nodes[level]->start; |
| 5277 | } else { |
| 5278 | BUG_ON(root->root_key.objectid != |
| 5279 | btrfs_header_owner(path->nodes[level])); |
| 5280 | parent = 0; |
| 5281 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5282 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5283 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 5284 | root->root_key.objectid, level - 1, 0); |
| 5285 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5286 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5287 | btrfs_tree_unlock(next); |
| 5288 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5289 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5290 | return 1; |
| 5291 | } |
| 5292 | |
| 5293 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5294 | * hepler to process tree block while walking up the tree. |
| 5295 | * |
| 5296 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5297 | * reference count on the block. |
| 5298 | * |
| 5299 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5300 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5301 | * to UPDATE_BACKREF previously while processing the block. |
| 5302 | * |
| 5303 | * NOTE: return value 1 means we should stop walking up. |
| 5304 | */ |
| 5305 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5306 | struct btrfs_root *root, |
| 5307 | struct btrfs_path *path, |
| 5308 | struct walk_control *wc) |
| 5309 | { |
| 5310 | int ret = 0; |
| 5311 | int level = wc->level; |
| 5312 | struct extent_buffer *eb = path->nodes[level]; |
| 5313 | u64 parent = 0; |
| 5314 | |
| 5315 | if (wc->stage == UPDATE_BACKREF) { |
| 5316 | BUG_ON(wc->shared_level < level); |
| 5317 | if (level < wc->shared_level) |
| 5318 | goto out; |
| 5319 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5320 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5321 | if (ret > 0) |
| 5322 | wc->update_ref = 0; |
| 5323 | |
| 5324 | wc->stage = DROP_REFERENCE; |
| 5325 | wc->shared_level = -1; |
| 5326 | path->slots[level] = 0; |
| 5327 | |
| 5328 | /* |
| 5329 | * check reference count again if the block isn't locked. |
| 5330 | * we should start walking down the tree again if reference |
| 5331 | * count is one. |
| 5332 | */ |
| 5333 | if (!path->locks[level]) { |
| 5334 | BUG_ON(level == 0); |
| 5335 | btrfs_tree_lock(eb); |
| 5336 | btrfs_set_lock_blocking(eb); |
| 5337 | path->locks[level] = 1; |
| 5338 | |
| 5339 | ret = btrfs_lookup_extent_info(trans, root, |
| 5340 | eb->start, eb->len, |
| 5341 | &wc->refs[level], |
| 5342 | &wc->flags[level]); |
| 5343 | BUG_ON(ret); |
| 5344 | BUG_ON(wc->refs[level] == 0); |
| 5345 | if (wc->refs[level] == 1) { |
| 5346 | btrfs_tree_unlock(eb); |
| 5347 | path->locks[level] = 0; |
| 5348 | return 1; |
| 5349 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | /* wc->stage == DROP_REFERENCE */ |
| 5354 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5355 | |
| 5356 | if (wc->refs[level] == 1) { |
| 5357 | if (level == 0) { |
| 5358 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5359 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 5360 | else |
| 5361 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5362 | BUG_ON(ret); |
| 5363 | } |
| 5364 | /* make block locked assertion in clean_tree_block happy */ |
| 5365 | if (!path->locks[level] && |
| 5366 | btrfs_header_generation(eb) == trans->transid) { |
| 5367 | btrfs_tree_lock(eb); |
| 5368 | btrfs_set_lock_blocking(eb); |
| 5369 | path->locks[level] = 1; |
| 5370 | } |
| 5371 | clean_tree_block(trans, root, eb); |
| 5372 | } |
| 5373 | |
| 5374 | if (eb == root->node) { |
| 5375 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5376 | parent = eb->start; |
| 5377 | else |
| 5378 | BUG_ON(root->root_key.objectid != |
| 5379 | btrfs_header_owner(eb)); |
| 5380 | } else { |
| 5381 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5382 | parent = path->nodes[level + 1]->start; |
| 5383 | else |
| 5384 | BUG_ON(root->root_key.objectid != |
| 5385 | btrfs_header_owner(path->nodes[level + 1])); |
| 5386 | } |
| 5387 | |
| 5388 | ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent, |
| 5389 | root->root_key.objectid, level, 0); |
| 5390 | BUG_ON(ret); |
| 5391 | out: |
| 5392 | wc->refs[level] = 0; |
| 5393 | wc->flags[level] = 0; |
| 5394 | return ret; |
| 5395 | } |
| 5396 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5397 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5398 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5399 | struct btrfs_path *path, |
| 5400 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5401 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5402 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5403 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5404 | int ret; |
| 5405 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5406 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5407 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5408 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5409 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5410 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5411 | if (level == 0) |
| 5412 | break; |
| 5413 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5414 | if (path->slots[level] >= |
| 5415 | btrfs_header_nritems(path->nodes[level])) |
| 5416 | break; |
| 5417 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5418 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5419 | if (ret > 0) { |
| 5420 | path->slots[level]++; |
| 5421 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame^] | 5422 | } else if (ret < 0) |
| 5423 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5424 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5425 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5426 | return 0; |
| 5427 | } |
| 5428 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5429 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5430 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5431 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5432 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5433 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5434 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5435 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5436 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5437 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5438 | while (level < max_level && path->nodes[level]) { |
| 5439 | wc->level = level; |
| 5440 | if (path->slots[level] + 1 < |
| 5441 | btrfs_header_nritems(path->nodes[level])) { |
| 5442 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5443 | return 0; |
| 5444 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5445 | ret = walk_up_proc(trans, root, path, wc); |
| 5446 | if (ret > 0) |
| 5447 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5448 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5449 | if (path->locks[level]) { |
| 5450 | btrfs_tree_unlock(path->nodes[level]); |
| 5451 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5452 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5453 | free_extent_buffer(path->nodes[level]); |
| 5454 | path->nodes[level] = NULL; |
| 5455 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5456 | } |
| 5457 | } |
| 5458 | return 1; |
| 5459 | } |
| 5460 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5461 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5462 | * drop a subvolume tree. |
| 5463 | * |
| 5464 | * this function traverses the tree freeing any blocks that only |
| 5465 | * referenced by the tree. |
| 5466 | * |
| 5467 | * when a shared tree block is found. this function decreases its |
| 5468 | * reference count by one. if update_ref is true, this function |
| 5469 | * also make sure backrefs for the shared block and all lower level |
| 5470 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5471 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5472 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5473 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5474 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5475 | struct btrfs_trans_handle *trans; |
| 5476 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5477 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5478 | struct walk_control *wc; |
| 5479 | struct btrfs_key key; |
| 5480 | int err = 0; |
| 5481 | int ret; |
| 5482 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5483 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5484 | path = btrfs_alloc_path(); |
| 5485 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5486 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5487 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5488 | BUG_ON(!wc); |
| 5489 | |
| 5490 | trans = btrfs_start_transaction(tree_root, 1); |
| 5491 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5492 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5493 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5494 | path->nodes[level] = btrfs_lock_root_node(root); |
| 5495 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5496 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5497 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5498 | memset(&wc->update_progress, 0, |
| 5499 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5500 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5501 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5502 | memcpy(&wc->update_progress, &key, |
| 5503 | sizeof(wc->update_progress)); |
| 5504 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5505 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5506 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5507 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5508 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5509 | path->lowest_level = 0; |
| 5510 | if (ret < 0) { |
| 5511 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5512 | goto out; |
| 5513 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5514 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5515 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5516 | /* |
| 5517 | * unlock our path, this is safe because only this |
| 5518 | * function is allowed to delete this snapshot |
| 5519 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5520 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5521 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5522 | level = btrfs_header_level(root->node); |
| 5523 | while (1) { |
| 5524 | btrfs_tree_lock(path->nodes[level]); |
| 5525 | btrfs_set_lock_blocking(path->nodes[level]); |
| 5526 | |
| 5527 | ret = btrfs_lookup_extent_info(trans, root, |
| 5528 | path->nodes[level]->start, |
| 5529 | path->nodes[level]->len, |
| 5530 | &wc->refs[level], |
| 5531 | &wc->flags[level]); |
| 5532 | BUG_ON(ret); |
| 5533 | BUG_ON(wc->refs[level] == 0); |
| 5534 | |
| 5535 | if (level == root_item->drop_level) |
| 5536 | break; |
| 5537 | |
| 5538 | btrfs_tree_unlock(path->nodes[level]); |
| 5539 | WARN_ON(wc->refs[level] != 1); |
| 5540 | level--; |
| 5541 | } |
| 5542 | } |
| 5543 | |
| 5544 | wc->level = level; |
| 5545 | wc->shared_level = -1; |
| 5546 | wc->stage = DROP_REFERENCE; |
| 5547 | wc->update_ref = update_ref; |
| 5548 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5549 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5550 | |
| 5551 | while (1) { |
| 5552 | ret = walk_down_tree(trans, root, path, wc); |
| 5553 | if (ret < 0) { |
| 5554 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5555 | break; |
| 5556 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5557 | |
| 5558 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5559 | if (ret < 0) { |
| 5560 | err = ret; |
| 5561 | break; |
| 5562 | } |
| 5563 | |
| 5564 | if (ret > 0) { |
| 5565 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5566 | break; |
| 5567 | } |
| 5568 | |
| 5569 | if (wc->stage == DROP_REFERENCE) { |
| 5570 | level = wc->level; |
| 5571 | btrfs_node_key(path->nodes[level], |
| 5572 | &root_item->drop_progress, |
| 5573 | path->slots[level]); |
| 5574 | root_item->drop_level = level; |
| 5575 | } |
| 5576 | |
| 5577 | BUG_ON(wc->level == 0); |
| 5578 | if (trans->transaction->in_commit || |
| 5579 | trans->transaction->delayed_refs.flushing) { |
| 5580 | ret = btrfs_update_root(trans, tree_root, |
| 5581 | &root->root_key, |
| 5582 | root_item); |
| 5583 | BUG_ON(ret); |
| 5584 | |
| 5585 | btrfs_end_transaction(trans, tree_root); |
| 5586 | trans = btrfs_start_transaction(tree_root, 1); |
| 5587 | } else { |
| 5588 | unsigned long update; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5589 | update = trans->delayed_ref_updates; |
| 5590 | trans->delayed_ref_updates = 0; |
| 5591 | if (update) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5592 | btrfs_run_delayed_refs(trans, tree_root, |
| 5593 | update); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5594 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5595 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5596 | btrfs_release_path(root, path); |
| 5597 | BUG_ON(err); |
| 5598 | |
| 5599 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 5600 | BUG_ON(ret); |
| 5601 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5602 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 5603 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 5604 | NULL, NULL); |
| 5605 | BUG_ON(ret < 0); |
| 5606 | if (ret > 0) { |
| 5607 | ret = btrfs_del_orphan_item(trans, tree_root, |
| 5608 | root->root_key.objectid); |
| 5609 | BUG_ON(ret); |
| 5610 | } |
| 5611 | } |
| 5612 | |
| 5613 | if (root->in_radix) { |
| 5614 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 5615 | } else { |
| 5616 | free_extent_buffer(root->node); |
| 5617 | free_extent_buffer(root->commit_root); |
| 5618 | kfree(root); |
| 5619 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5620 | out: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5621 | btrfs_end_transaction(trans, tree_root); |
| 5622 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5623 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5624 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5625 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5626 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5627 | /* |
| 5628 | * drop subtree rooted at tree block 'node'. |
| 5629 | * |
| 5630 | * NOTE: this function will unlock and release tree block 'node' |
| 5631 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5632 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5633 | struct btrfs_root *root, |
| 5634 | struct extent_buffer *node, |
| 5635 | struct extent_buffer *parent) |
| 5636 | { |
| 5637 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5638 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5639 | int level; |
| 5640 | int parent_level; |
| 5641 | int ret = 0; |
| 5642 | int wret; |
| 5643 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5644 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5645 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5646 | path = btrfs_alloc_path(); |
| 5647 | BUG_ON(!path); |
| 5648 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5649 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5650 | BUG_ON(!wc); |
| 5651 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5652 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5653 | parent_level = btrfs_header_level(parent); |
| 5654 | extent_buffer_get(parent); |
| 5655 | path->nodes[parent_level] = parent; |
| 5656 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5657 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5658 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5659 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5660 | path->nodes[level] = node; |
| 5661 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5662 | path->locks[level] = 1; |
| 5663 | |
| 5664 | wc->refs[parent_level] = 1; |
| 5665 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5666 | wc->level = level; |
| 5667 | wc->shared_level = -1; |
| 5668 | wc->stage = DROP_REFERENCE; |
| 5669 | wc->update_ref = 0; |
| 5670 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5671 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5672 | |
| 5673 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5674 | wret = walk_down_tree(trans, root, path, wc); |
| 5675 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5676 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5677 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5678 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5679 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5680 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5681 | if (wret < 0) |
| 5682 | ret = wret; |
| 5683 | if (wret != 0) |
| 5684 | break; |
| 5685 | } |
| 5686 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5687 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5688 | btrfs_free_path(path); |
| 5689 | return ret; |
| 5690 | } |
| 5691 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5692 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5693 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 5694 | unsigned long nr) |
| 5695 | { |
| 5696 | return min(last, start + nr - 1); |
| 5697 | } |
| 5698 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5699 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5700 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5701 | { |
| 5702 | u64 page_start; |
| 5703 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5704 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5705 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5706 | unsigned long i; |
| 5707 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5708 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5709 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5710 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5711 | unsigned int total_read = 0; |
| 5712 | unsigned int total_dirty = 0; |
| 5713 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5714 | |
| 5715 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5716 | |
| 5717 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5718 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5719 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 5720 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5721 | /* make sure the dirty trick played by the caller work */ |
| 5722 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5723 | first_index, last_index); |
| 5724 | if (ret) |
| 5725 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5726 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5727 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5728 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5729 | for (i = first_index ; i <= last_index; i++) { |
| 5730 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5731 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5732 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5733 | } |
| 5734 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5735 | again: |
| 5736 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5737 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5738 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5739 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5740 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5741 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5742 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5743 | if (!PageUptodate(page)) { |
| 5744 | btrfs_readpage(NULL, page); |
| 5745 | lock_page(page); |
| 5746 | if (!PageUptodate(page)) { |
| 5747 | unlock_page(page); |
| 5748 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5749 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5750 | goto out_unlock; |
| 5751 | } |
| 5752 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5753 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5754 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5755 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 5756 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5757 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5758 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5759 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5760 | if (ordered) { |
| 5761 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5762 | unlock_page(page); |
| 5763 | page_cache_release(page); |
| 5764 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5765 | btrfs_put_ordered_extent(ordered); |
| 5766 | goto again; |
| 5767 | } |
| 5768 | set_page_extent_mapped(page); |
| 5769 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5770 | if (i == first_index) |
| 5771 | set_extent_bits(io_tree, page_start, page_end, |
| 5772 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 5773 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5774 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5775 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5776 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5777 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5778 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5779 | unlock_page(page); |
| 5780 | page_cache_release(page); |
| 5781 | } |
| 5782 | |
| 5783 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5784 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5785 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5786 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5787 | return ret; |
| 5788 | } |
| 5789 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5790 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5791 | struct btrfs_key *extent_key, |
| 5792 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5793 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5794 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5795 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 5796 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5797 | u64 start = extent_key->objectid - offset; |
| 5798 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5799 | |
| 5800 | em = alloc_extent_map(GFP_NOFS); |
| 5801 | BUG_ON(!em || IS_ERR(em)); |
| 5802 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5803 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5804 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5805 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5806 | em->block_start = extent_key->objectid; |
| 5807 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5808 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5809 | |
| 5810 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5811 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5812 | while (1) { |
| 5813 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5814 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5815 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5816 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5817 | if (ret != -EEXIST) { |
| 5818 | free_extent_map(em); |
| 5819 | break; |
| 5820 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5821 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5822 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5823 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5824 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5825 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | struct btrfs_ref_path { |
| 5829 | u64 extent_start; |
| 5830 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 5831 | u64 root_objectid; |
| 5832 | u64 root_generation; |
| 5833 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5834 | u32 num_refs; |
| 5835 | int lowest_level; |
| 5836 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5837 | int shared_level; |
| 5838 | |
| 5839 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 5840 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5841 | }; |
| 5842 | |
| 5843 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5844 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5845 | u64 disk_bytenr; |
| 5846 | u64 disk_num_bytes; |
| 5847 | u64 offset; |
| 5848 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5849 | u8 compression; |
| 5850 | u8 encryption; |
| 5851 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5852 | }; |
| 5853 | |
| 5854 | static int is_cowonly_root(u64 root_objectid) |
| 5855 | { |
| 5856 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 5857 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 5858 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 5859 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5860 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5861 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5862 | return 1; |
| 5863 | return 0; |
| 5864 | } |
| 5865 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5866 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5867 | struct btrfs_root *extent_root, |
| 5868 | struct btrfs_ref_path *ref_path, |
| 5869 | int first_time) |
| 5870 | { |
| 5871 | struct extent_buffer *leaf; |
| 5872 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5873 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5874 | struct btrfs_key key; |
| 5875 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5876 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5877 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5878 | int level; |
| 5879 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5880 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5881 | path = btrfs_alloc_path(); |
| 5882 | if (!path) |
| 5883 | return -ENOMEM; |
| 5884 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5885 | if (first_time) { |
| 5886 | ref_path->lowest_level = -1; |
| 5887 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5888 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5889 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5890 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5891 | walk_down: |
| 5892 | level = ref_path->current_level - 1; |
| 5893 | while (level >= -1) { |
| 5894 | u64 parent; |
| 5895 | if (level < ref_path->lowest_level) |
| 5896 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5897 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5898 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5899 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5900 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5901 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5902 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5903 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5904 | parent = ref_path->nodes[level + 1]; |
| 5905 | ref_path->nodes[level + 1] = 0; |
| 5906 | ref_path->current_level = level; |
| 5907 | BUG_ON(parent == 0); |
| 5908 | |
| 5909 | key.objectid = bytenr; |
| 5910 | key.offset = parent + 1; |
| 5911 | key.type = BTRFS_EXTENT_REF_KEY; |
| 5912 | |
| 5913 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5914 | if (ret < 0) |
| 5915 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5916 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5917 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5918 | leaf = path->nodes[0]; |
| 5919 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5920 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5921 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5922 | if (ret < 0) |
| 5923 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5924 | if (ret > 0) |
| 5925 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5926 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5927 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5928 | |
| 5929 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5930 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5931 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 5932 | if (level < ref_path->shared_level) |
| 5933 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5934 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5935 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5936 | next: |
| 5937 | level--; |
| 5938 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5939 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5940 | } |
| 5941 | /* reached lowest level */ |
| 5942 | ret = 1; |
| 5943 | goto out; |
| 5944 | walk_up: |
| 5945 | level = ref_path->current_level; |
| 5946 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 5947 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5948 | |
| 5949 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5950 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5951 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5952 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5953 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5954 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5955 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5956 | key.objectid = bytenr; |
| 5957 | key.offset = 0; |
| 5958 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5959 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5960 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 5961 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5962 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5963 | |
| 5964 | leaf = path->nodes[0]; |
| 5965 | nritems = btrfs_header_nritems(leaf); |
| 5966 | if (path->slots[0] >= nritems) { |
| 5967 | ret = btrfs_next_leaf(extent_root, path); |
| 5968 | if (ret < 0) |
| 5969 | goto out; |
| 5970 | if (ret > 0) { |
| 5971 | /* the extent was freed by someone */ |
| 5972 | if (ref_path->lowest_level == level) |
| 5973 | goto out; |
| 5974 | btrfs_release_path(extent_root, path); |
| 5975 | goto walk_down; |
| 5976 | } |
| 5977 | leaf = path->nodes[0]; |
| 5978 | } |
| 5979 | |
| 5980 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5981 | if (found_key.objectid != bytenr || |
| 5982 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 5983 | /* the extent was freed by someone */ |
| 5984 | if (ref_path->lowest_level == level) { |
| 5985 | ret = 1; |
| 5986 | goto out; |
| 5987 | } |
| 5988 | btrfs_release_path(extent_root, path); |
| 5989 | goto walk_down; |
| 5990 | } |
| 5991 | found: |
| 5992 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 5993 | struct btrfs_extent_ref); |
| 5994 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 5995 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5996 | if (first_time) { |
| 5997 | level = (int)ref_objectid; |
| 5998 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 5999 | ref_path->lowest_level = level; |
| 6000 | ref_path->current_level = level; |
| 6001 | ref_path->nodes[level] = bytenr; |
| 6002 | } else { |
| 6003 | WARN_ON(ref_objectid != level); |
| 6004 | } |
| 6005 | } else { |
| 6006 | WARN_ON(level != -1); |
| 6007 | } |
| 6008 | first_time = 0; |
| 6009 | |
| 6010 | if (ref_path->lowest_level == level) { |
| 6011 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6012 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 6013 | } |
| 6014 | |
| 6015 | /* |
| 6016 | * the block is tree root or the block isn't in reference |
| 6017 | * counted tree. |
| 6018 | */ |
| 6019 | if (found_key.objectid == found_key.offset || |
| 6020 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 6021 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6022 | ref_path->root_generation = |
| 6023 | btrfs_ref_generation(leaf, ref); |
| 6024 | if (level < 0) { |
| 6025 | /* special reference from the tree log */ |
| 6026 | ref_path->nodes[0] = found_key.offset; |
| 6027 | ref_path->current_level = 0; |
| 6028 | } |
| 6029 | ret = 0; |
| 6030 | goto out; |
| 6031 | } |
| 6032 | |
| 6033 | level++; |
| 6034 | BUG_ON(ref_path->nodes[level] != 0); |
| 6035 | ref_path->nodes[level] = found_key.offset; |
| 6036 | ref_path->current_level = level; |
| 6037 | |
| 6038 | /* |
| 6039 | * the reference was created in the running transaction, |
| 6040 | * no need to continue walking up. |
| 6041 | */ |
| 6042 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6043 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6044 | ref_path->root_generation = |
| 6045 | btrfs_ref_generation(leaf, ref); |
| 6046 | ret = 0; |
| 6047 | goto out; |
| 6048 | } |
| 6049 | |
| 6050 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6051 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6052 | } |
| 6053 | /* reached max tree level, but no tree root found. */ |
| 6054 | BUG(); |
| 6055 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6056 | btrfs_free_path(path); |
| 6057 | return ret; |
| 6058 | } |
| 6059 | |
| 6060 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6061 | struct btrfs_root *extent_root, |
| 6062 | struct btrfs_ref_path *ref_path, |
| 6063 | u64 extent_start) |
| 6064 | { |
| 6065 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6066 | ref_path->extent_start = extent_start; |
| 6067 | |
| 6068 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6069 | } |
| 6070 | |
| 6071 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6072 | struct btrfs_root *extent_root, |
| 6073 | struct btrfs_ref_path *ref_path) |
| 6074 | { |
| 6075 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6076 | } |
| 6077 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6078 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6079 | struct btrfs_key *extent_key, |
| 6080 | u64 offset, int no_fragment, |
| 6081 | struct disk_extent **extents, |
| 6082 | int *nr_extents) |
| 6083 | { |
| 6084 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6085 | struct btrfs_path *path; |
| 6086 | struct btrfs_file_extent_item *fi; |
| 6087 | struct extent_buffer *leaf; |
| 6088 | struct disk_extent *exts = *extents; |
| 6089 | struct btrfs_key found_key; |
| 6090 | u64 cur_pos; |
| 6091 | u64 last_byte; |
| 6092 | u32 nritems; |
| 6093 | int nr = 0; |
| 6094 | int max = *nr_extents; |
| 6095 | int ret; |
| 6096 | |
| 6097 | WARN_ON(!no_fragment && *extents); |
| 6098 | if (!exts) { |
| 6099 | max = 1; |
| 6100 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6101 | if (!exts) |
| 6102 | return -ENOMEM; |
| 6103 | } |
| 6104 | |
| 6105 | path = btrfs_alloc_path(); |
| 6106 | BUG_ON(!path); |
| 6107 | |
| 6108 | cur_pos = extent_key->objectid - offset; |
| 6109 | last_byte = extent_key->objectid + extent_key->offset; |
| 6110 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6111 | cur_pos, 0); |
| 6112 | if (ret < 0) |
| 6113 | goto out; |
| 6114 | if (ret > 0) { |
| 6115 | ret = -ENOENT; |
| 6116 | goto out; |
| 6117 | } |
| 6118 | |
| 6119 | while (1) { |
| 6120 | leaf = path->nodes[0]; |
| 6121 | nritems = btrfs_header_nritems(leaf); |
| 6122 | if (path->slots[0] >= nritems) { |
| 6123 | ret = btrfs_next_leaf(root, path); |
| 6124 | if (ret < 0) |
| 6125 | goto out; |
| 6126 | if (ret > 0) |
| 6127 | break; |
| 6128 | leaf = path->nodes[0]; |
| 6129 | } |
| 6130 | |
| 6131 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6132 | if (found_key.offset != cur_pos || |
| 6133 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6134 | found_key.objectid != reloc_inode->i_ino) |
| 6135 | break; |
| 6136 | |
| 6137 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6138 | struct btrfs_file_extent_item); |
| 6139 | if (btrfs_file_extent_type(leaf, fi) != |
| 6140 | BTRFS_FILE_EXTENT_REG || |
| 6141 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6142 | break; |
| 6143 | |
| 6144 | if (nr == max) { |
| 6145 | struct disk_extent *old = exts; |
| 6146 | max *= 2; |
| 6147 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6148 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6149 | if (old != *extents) |
| 6150 | kfree(old); |
| 6151 | } |
| 6152 | |
| 6153 | exts[nr].disk_bytenr = |
| 6154 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6155 | exts[nr].disk_num_bytes = |
| 6156 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6157 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6158 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6159 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6160 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6161 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6162 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6163 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6164 | BUG_ON(exts[nr].offset > 0); |
| 6165 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6166 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6167 | |
| 6168 | cur_pos += exts[nr].num_bytes; |
| 6169 | nr++; |
| 6170 | |
| 6171 | if (cur_pos + offset >= last_byte) |
| 6172 | break; |
| 6173 | |
| 6174 | if (no_fragment) { |
| 6175 | ret = 1; |
| 6176 | goto out; |
| 6177 | } |
| 6178 | path->slots[0]++; |
| 6179 | } |
| 6180 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6181 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6182 | if (cur_pos + offset < last_byte) { |
| 6183 | ret = -ENOENT; |
| 6184 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6185 | } |
| 6186 | ret = 0; |
| 6187 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6188 | btrfs_free_path(path); |
| 6189 | if (ret) { |
| 6190 | if (exts != *extents) |
| 6191 | kfree(exts); |
| 6192 | } else { |
| 6193 | *extents = exts; |
| 6194 | *nr_extents = nr; |
| 6195 | } |
| 6196 | return ret; |
| 6197 | } |
| 6198 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6199 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6200 | struct btrfs_root *root, |
| 6201 | struct btrfs_path *path, |
| 6202 | struct btrfs_key *extent_key, |
| 6203 | struct btrfs_key *leaf_key, |
| 6204 | struct btrfs_ref_path *ref_path, |
| 6205 | struct disk_extent *new_extents, |
| 6206 | int nr_extents) |
| 6207 | { |
| 6208 | struct extent_buffer *leaf; |
| 6209 | struct btrfs_file_extent_item *fi; |
| 6210 | struct inode *inode = NULL; |
| 6211 | struct btrfs_key key; |
| 6212 | u64 lock_start = 0; |
| 6213 | u64 lock_end = 0; |
| 6214 | u64 num_bytes; |
| 6215 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6216 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6217 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6218 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6219 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6220 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6221 | int ret; |
| 6222 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6223 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6224 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6225 | if (key.objectid < ref_path->owner_objectid || |
| 6226 | (key.objectid == ref_path->owner_objectid && |
| 6227 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6228 | key.objectid = ref_path->owner_objectid; |
| 6229 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6230 | key.offset = 0; |
| 6231 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6232 | } |
| 6233 | |
| 6234 | while (1) { |
| 6235 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6236 | if (ret < 0) |
| 6237 | goto out; |
| 6238 | |
| 6239 | leaf = path->nodes[0]; |
| 6240 | nritems = btrfs_header_nritems(leaf); |
| 6241 | next: |
| 6242 | if (extent_locked && ret > 0) { |
| 6243 | /* |
| 6244 | * the file extent item was modified by someone |
| 6245 | * before the extent got locked. |
| 6246 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6247 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6248 | lock_end, GFP_NOFS); |
| 6249 | extent_locked = 0; |
| 6250 | } |
| 6251 | |
| 6252 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6253 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6254 | break; |
| 6255 | |
| 6256 | BUG_ON(extent_locked); |
| 6257 | ret = btrfs_next_leaf(root, path); |
| 6258 | if (ret < 0) |
| 6259 | goto out; |
| 6260 | if (ret > 0) |
| 6261 | break; |
| 6262 | leaf = path->nodes[0]; |
| 6263 | nritems = btrfs_header_nritems(leaf); |
| 6264 | } |
| 6265 | |
| 6266 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 6267 | |
| 6268 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 6269 | if ((key.objectid > ref_path->owner_objectid) || |
| 6270 | (key.objectid == ref_path->owner_objectid && |
| 6271 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6272 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6273 | break; |
| 6274 | } |
| 6275 | |
| 6276 | if (inode && key.objectid != inode->i_ino) { |
| 6277 | BUG_ON(extent_locked); |
| 6278 | btrfs_release_path(root, path); |
| 6279 | mutex_unlock(&inode->i_mutex); |
| 6280 | iput(inode); |
| 6281 | inode = NULL; |
| 6282 | continue; |
| 6283 | } |
| 6284 | |
| 6285 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6286 | path->slots[0]++; |
| 6287 | ret = 1; |
| 6288 | goto next; |
| 6289 | } |
| 6290 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6291 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6292 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 6293 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 6294 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6295 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 6296 | extent_key->objectid)) { |
| 6297 | path->slots[0]++; |
| 6298 | ret = 1; |
| 6299 | goto next; |
| 6300 | } |
| 6301 | |
| 6302 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6303 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 6304 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6305 | if (search_end == (u64)-1) { |
| 6306 | search_end = key.offset - ext_offset + |
| 6307 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 6308 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6309 | |
| 6310 | if (!extent_locked) { |
| 6311 | lock_start = key.offset; |
| 6312 | lock_end = lock_start + num_bytes - 1; |
| 6313 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6314 | if (lock_start > key.offset || |
| 6315 | lock_end + 1 < key.offset + num_bytes) { |
| 6316 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6317 | lock_start, lock_end, GFP_NOFS); |
| 6318 | extent_locked = 0; |
| 6319 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | if (!inode) { |
| 6323 | btrfs_release_path(root, path); |
| 6324 | |
| 6325 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 6326 | key.objectid, root); |
| 6327 | if (inode->i_state & I_NEW) { |
| 6328 | BTRFS_I(inode)->root = root; |
| 6329 | BTRFS_I(inode)->location.objectid = |
| 6330 | key.objectid; |
| 6331 | BTRFS_I(inode)->location.type = |
| 6332 | BTRFS_INODE_ITEM_KEY; |
| 6333 | BTRFS_I(inode)->location.offset = 0; |
| 6334 | btrfs_read_locked_inode(inode); |
| 6335 | unlock_new_inode(inode); |
| 6336 | } |
| 6337 | /* |
| 6338 | * some code call btrfs_commit_transaction while |
| 6339 | * holding the i_mutex, so we can't use mutex_lock |
| 6340 | * here. |
| 6341 | */ |
| 6342 | if (is_bad_inode(inode) || |
| 6343 | !mutex_trylock(&inode->i_mutex)) { |
| 6344 | iput(inode); |
| 6345 | inode = NULL; |
| 6346 | key.offset = (u64)-1; |
| 6347 | goto skip; |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | if (!extent_locked) { |
| 6352 | struct btrfs_ordered_extent *ordered; |
| 6353 | |
| 6354 | btrfs_release_path(root, path); |
| 6355 | |
| 6356 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6357 | lock_end, GFP_NOFS); |
| 6358 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6359 | lock_end); |
| 6360 | if (ordered && |
| 6361 | ordered->file_offset <= lock_end && |
| 6362 | ordered->file_offset + ordered->len > lock_start) { |
| 6363 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6364 | lock_start, lock_end, GFP_NOFS); |
| 6365 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6366 | btrfs_put_ordered_extent(ordered); |
| 6367 | key.offset += num_bytes; |
| 6368 | goto skip; |
| 6369 | } |
| 6370 | if (ordered) |
| 6371 | btrfs_put_ordered_extent(ordered); |
| 6372 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6373 | extent_locked = 1; |
| 6374 | continue; |
| 6375 | } |
| 6376 | |
| 6377 | if (nr_extents == 1) { |
| 6378 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6379 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6380 | new_extents[0].disk_bytenr); |
| 6381 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6382 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6383 | btrfs_mark_buffer_dirty(leaf); |
| 6384 | |
| 6385 | btrfs_drop_extent_cache(inode, key.offset, |
| 6386 | key.offset + num_bytes - 1, 0); |
| 6387 | |
| 6388 | ret = btrfs_inc_extent_ref(trans, root, |
| 6389 | new_extents[0].disk_bytenr, |
| 6390 | new_extents[0].disk_num_bytes, |
| 6391 | leaf->start, |
| 6392 | root->root_key.objectid, |
| 6393 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6394 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6395 | BUG_ON(ret); |
| 6396 | |
| 6397 | ret = btrfs_free_extent(trans, root, |
| 6398 | extent_key->objectid, |
| 6399 | extent_key->offset, |
| 6400 | leaf->start, |
| 6401 | btrfs_header_owner(leaf), |
| 6402 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6403 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6404 | BUG_ON(ret); |
| 6405 | |
| 6406 | btrfs_release_path(root, path); |
| 6407 | key.offset += num_bytes; |
| 6408 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6409 | BUG_ON(1); |
| 6410 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6411 | u64 alloc_hint; |
| 6412 | u64 extent_len; |
| 6413 | int i; |
| 6414 | /* |
| 6415 | * drop old extent pointer at first, then insert the |
| 6416 | * new pointers one bye one |
| 6417 | */ |
| 6418 | btrfs_release_path(root, path); |
| 6419 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 6420 | key.offset + num_bytes, |
| 6421 | key.offset, &alloc_hint); |
| 6422 | BUG_ON(ret); |
| 6423 | |
| 6424 | for (i = 0; i < nr_extents; i++) { |
| 6425 | if (ext_offset >= new_extents[i].num_bytes) { |
| 6426 | ext_offset -= new_extents[i].num_bytes; |
| 6427 | continue; |
| 6428 | } |
| 6429 | extent_len = min(new_extents[i].num_bytes - |
| 6430 | ext_offset, num_bytes); |
| 6431 | |
| 6432 | ret = btrfs_insert_empty_item(trans, root, |
| 6433 | path, &key, |
| 6434 | sizeof(*fi)); |
| 6435 | BUG_ON(ret); |
| 6436 | |
| 6437 | leaf = path->nodes[0]; |
| 6438 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6439 | struct btrfs_file_extent_item); |
| 6440 | btrfs_set_file_extent_generation(leaf, fi, |
| 6441 | trans->transid); |
| 6442 | btrfs_set_file_extent_type(leaf, fi, |
| 6443 | BTRFS_FILE_EXTENT_REG); |
| 6444 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6445 | new_extents[i].disk_bytenr); |
| 6446 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6447 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6448 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 6449 | new_extents[i].ram_bytes); |
| 6450 | |
| 6451 | btrfs_set_file_extent_compression(leaf, fi, |
| 6452 | new_extents[i].compression); |
| 6453 | btrfs_set_file_extent_encryption(leaf, fi, |
| 6454 | new_extents[i].encryption); |
| 6455 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 6456 | new_extents[i].other_encoding); |
| 6457 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6458 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 6459 | extent_len); |
| 6460 | ext_offset += new_extents[i].offset; |
| 6461 | btrfs_set_file_extent_offset(leaf, fi, |
| 6462 | ext_offset); |
| 6463 | btrfs_mark_buffer_dirty(leaf); |
| 6464 | |
| 6465 | btrfs_drop_extent_cache(inode, key.offset, |
| 6466 | key.offset + extent_len - 1, 0); |
| 6467 | |
| 6468 | ret = btrfs_inc_extent_ref(trans, root, |
| 6469 | new_extents[i].disk_bytenr, |
| 6470 | new_extents[i].disk_num_bytes, |
| 6471 | leaf->start, |
| 6472 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6473 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6474 | BUG_ON(ret); |
| 6475 | btrfs_release_path(root, path); |
| 6476 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6477 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6478 | |
| 6479 | ext_offset = 0; |
| 6480 | num_bytes -= extent_len; |
| 6481 | key.offset += extent_len; |
| 6482 | |
| 6483 | if (num_bytes == 0) |
| 6484 | break; |
| 6485 | } |
| 6486 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6487 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6488 | } |
| 6489 | |
| 6490 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6491 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6492 | lock_end, GFP_NOFS); |
| 6493 | extent_locked = 0; |
| 6494 | } |
| 6495 | skip: |
| 6496 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6497 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6498 | break; |
| 6499 | |
| 6500 | cond_resched(); |
| 6501 | } |
| 6502 | ret = 0; |
| 6503 | out: |
| 6504 | btrfs_release_path(root, path); |
| 6505 | if (inode) { |
| 6506 | mutex_unlock(&inode->i_mutex); |
| 6507 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6508 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6509 | lock_end, GFP_NOFS); |
| 6510 | } |
| 6511 | iput(inode); |
| 6512 | } |
| 6513 | return ret; |
| 6514 | } |
| 6515 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6516 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 6517 | struct btrfs_root *root, |
| 6518 | struct extent_buffer *buf, u64 orig_start) |
| 6519 | { |
| 6520 | int level; |
| 6521 | int ret; |
| 6522 | |
| 6523 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 6524 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6525 | |
| 6526 | level = btrfs_header_level(buf); |
| 6527 | if (level == 0) { |
| 6528 | struct btrfs_leaf_ref *ref; |
| 6529 | struct btrfs_leaf_ref *orig_ref; |
| 6530 | |
| 6531 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 6532 | if (!orig_ref) |
| 6533 | return -ENOENT; |
| 6534 | |
| 6535 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 6536 | if (!ref) { |
| 6537 | btrfs_free_leaf_ref(root, orig_ref); |
| 6538 | return -ENOMEM; |
| 6539 | } |
| 6540 | |
| 6541 | ref->nritems = orig_ref->nritems; |
| 6542 | memcpy(ref->extents, orig_ref->extents, |
| 6543 | sizeof(ref->extents[0]) * ref->nritems); |
| 6544 | |
| 6545 | btrfs_free_leaf_ref(root, orig_ref); |
| 6546 | |
| 6547 | ref->root_gen = trans->transid; |
| 6548 | ref->bytenr = buf->start; |
| 6549 | ref->owner = btrfs_header_owner(buf); |
| 6550 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6551 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6552 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 6553 | WARN_ON(ret); |
| 6554 | btrfs_free_leaf_ref(root, ref); |
| 6555 | } |
| 6556 | return 0; |
| 6557 | } |
| 6558 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6559 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6560 | struct extent_buffer *leaf, |
| 6561 | struct btrfs_block_group_cache *group, |
| 6562 | struct btrfs_root *target_root) |
| 6563 | { |
| 6564 | struct btrfs_key key; |
| 6565 | struct inode *inode = NULL; |
| 6566 | struct btrfs_file_extent_item *fi; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6567 | struct extent_state *cached_state = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6568 | u64 num_bytes; |
| 6569 | u64 skip_objectid = 0; |
| 6570 | u32 nritems; |
| 6571 | u32 i; |
| 6572 | |
| 6573 | nritems = btrfs_header_nritems(leaf); |
| 6574 | for (i = 0; i < nritems; i++) { |
| 6575 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6576 | if (key.objectid == skip_objectid || |
| 6577 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 6578 | continue; |
| 6579 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6580 | if (btrfs_file_extent_type(leaf, fi) == |
| 6581 | BTRFS_FILE_EXTENT_INLINE) |
| 6582 | continue; |
| 6583 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6584 | continue; |
| 6585 | if (!inode || inode->i_ino != key.objectid) { |
| 6586 | iput(inode); |
| 6587 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 6588 | key.objectid, target_root, 1); |
| 6589 | } |
| 6590 | if (!inode) { |
| 6591 | skip_objectid = key.objectid; |
| 6592 | continue; |
| 6593 | } |
| 6594 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6595 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6596 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, |
| 6597 | key.offset + num_bytes - 1, 0, &cached_state, |
| 6598 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6599 | btrfs_drop_extent_cache(inode, key.offset, |
| 6600 | key.offset + num_bytes - 1, 1); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6601 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, |
| 6602 | key.offset + num_bytes - 1, &cached_state, |
| 6603 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6604 | cond_resched(); |
| 6605 | } |
| 6606 | iput(inode); |
| 6607 | return 0; |
| 6608 | } |
| 6609 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6610 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6611 | struct btrfs_root *root, |
| 6612 | struct extent_buffer *leaf, |
| 6613 | struct btrfs_block_group_cache *group, |
| 6614 | struct inode *reloc_inode) |
| 6615 | { |
| 6616 | struct btrfs_key key; |
| 6617 | struct btrfs_key extent_key; |
| 6618 | struct btrfs_file_extent_item *fi; |
| 6619 | struct btrfs_leaf_ref *ref; |
| 6620 | struct disk_extent *new_extent; |
| 6621 | u64 bytenr; |
| 6622 | u64 num_bytes; |
| 6623 | u32 nritems; |
| 6624 | u32 i; |
| 6625 | int ext_index; |
| 6626 | int nr_extent; |
| 6627 | int ret; |
| 6628 | |
| 6629 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 6630 | BUG_ON(!new_extent); |
| 6631 | |
| 6632 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 6633 | BUG_ON(!ref); |
| 6634 | |
| 6635 | ext_index = -1; |
| 6636 | nritems = btrfs_header_nritems(leaf); |
| 6637 | for (i = 0; i < nritems; i++) { |
| 6638 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6639 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 6640 | continue; |
| 6641 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6642 | if (btrfs_file_extent_type(leaf, fi) == |
| 6643 | BTRFS_FILE_EXTENT_INLINE) |
| 6644 | continue; |
| 6645 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6646 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6647 | if (bytenr == 0) |
| 6648 | continue; |
| 6649 | |
| 6650 | ext_index++; |
| 6651 | if (bytenr >= group->key.objectid + group->key.offset || |
| 6652 | bytenr + num_bytes <= group->key.objectid) |
| 6653 | continue; |
| 6654 | |
| 6655 | extent_key.objectid = bytenr; |
| 6656 | extent_key.offset = num_bytes; |
| 6657 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6658 | nr_extent = 1; |
| 6659 | ret = get_new_locations(reloc_inode, &extent_key, |
| 6660 | group->key.objectid, 1, |
| 6661 | &new_extent, &nr_extent); |
| 6662 | if (ret > 0) |
| 6663 | continue; |
| 6664 | BUG_ON(ret < 0); |
| 6665 | |
| 6666 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 6667 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 6668 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 6669 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 6670 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6671 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6672 | new_extent->disk_bytenr); |
| 6673 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6674 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6675 | btrfs_mark_buffer_dirty(leaf); |
| 6676 | |
| 6677 | ret = btrfs_inc_extent_ref(trans, root, |
| 6678 | new_extent->disk_bytenr, |
| 6679 | new_extent->disk_num_bytes, |
| 6680 | leaf->start, |
| 6681 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6682 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6683 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6684 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6685 | ret = btrfs_free_extent(trans, root, |
| 6686 | bytenr, num_bytes, leaf->start, |
| 6687 | btrfs_header_owner(leaf), |
| 6688 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6689 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6690 | BUG_ON(ret); |
| 6691 | cond_resched(); |
| 6692 | } |
| 6693 | kfree(new_extent); |
| 6694 | BUG_ON(ext_index + 1 != ref->nritems); |
| 6695 | btrfs_free_leaf_ref(root, ref); |
| 6696 | return 0; |
| 6697 | } |
| 6698 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6699 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 6700 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6701 | { |
| 6702 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6703 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6704 | |
| 6705 | if (root->reloc_root) { |
| 6706 | reloc_root = root->reloc_root; |
| 6707 | root->reloc_root = NULL; |
| 6708 | list_add(&reloc_root->dead_list, |
| 6709 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6710 | |
| 6711 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 6712 | reloc_root->node->start); |
| 6713 | btrfs_set_root_level(&root->root_item, |
| 6714 | btrfs_header_level(reloc_root->node)); |
| 6715 | memset(&reloc_root->root_item.drop_progress, 0, |
| 6716 | sizeof(struct btrfs_disk_key)); |
| 6717 | reloc_root->root_item.drop_level = 0; |
| 6718 | |
| 6719 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 6720 | &reloc_root->root_key, |
| 6721 | &reloc_root->root_item); |
| 6722 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6723 | } |
| 6724 | return 0; |
| 6725 | } |
| 6726 | |
| 6727 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 6728 | { |
| 6729 | struct btrfs_trans_handle *trans; |
| 6730 | struct btrfs_root *reloc_root; |
| 6731 | struct btrfs_root *prev_root = NULL; |
| 6732 | struct list_head dead_roots; |
| 6733 | int ret; |
| 6734 | unsigned long nr; |
| 6735 | |
| 6736 | INIT_LIST_HEAD(&dead_roots); |
| 6737 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 6738 | |
| 6739 | while (!list_empty(&dead_roots)) { |
| 6740 | reloc_root = list_entry(dead_roots.prev, |
| 6741 | struct btrfs_root, dead_list); |
| 6742 | list_del_init(&reloc_root->dead_list); |
| 6743 | |
| 6744 | BUG_ON(reloc_root->commit_root != NULL); |
| 6745 | while (1) { |
| 6746 | trans = btrfs_join_transaction(root, 1); |
| 6747 | BUG_ON(!trans); |
| 6748 | |
| 6749 | mutex_lock(&root->fs_info->drop_mutex); |
| 6750 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 6751 | if (ret != -EAGAIN) |
| 6752 | break; |
| 6753 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6754 | |
| 6755 | nr = trans->blocks_used; |
| 6756 | ret = btrfs_end_transaction(trans, root); |
| 6757 | BUG_ON(ret); |
| 6758 | btrfs_btree_balance_dirty(root, nr); |
| 6759 | } |
| 6760 | |
| 6761 | free_extent_buffer(reloc_root->node); |
| 6762 | |
| 6763 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 6764 | &reloc_root->root_key); |
| 6765 | BUG_ON(ret); |
| 6766 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6767 | |
| 6768 | nr = trans->blocks_used; |
| 6769 | ret = btrfs_end_transaction(trans, root); |
| 6770 | BUG_ON(ret); |
| 6771 | btrfs_btree_balance_dirty(root, nr); |
| 6772 | |
| 6773 | kfree(prev_root); |
| 6774 | prev_root = reloc_root; |
| 6775 | } |
| 6776 | if (prev_root) { |
| 6777 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 6778 | kfree(prev_root); |
| 6779 | } |
| 6780 | return 0; |
| 6781 | } |
| 6782 | |
| 6783 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 6784 | { |
| 6785 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 6786 | return 0; |
| 6787 | } |
| 6788 | |
| 6789 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 6790 | { |
| 6791 | struct btrfs_root *reloc_root; |
| 6792 | struct btrfs_trans_handle *trans; |
| 6793 | struct btrfs_key location; |
| 6794 | int found; |
| 6795 | int ret; |
| 6796 | |
| 6797 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6798 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 6799 | BUG_ON(ret); |
| 6800 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 6801 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 6802 | |
| 6803 | if (found) { |
| 6804 | trans = btrfs_start_transaction(root, 1); |
| 6805 | BUG_ON(!trans); |
| 6806 | ret = btrfs_commit_transaction(trans, root); |
| 6807 | BUG_ON(ret); |
| 6808 | } |
| 6809 | |
| 6810 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6811 | location.offset = (u64)-1; |
| 6812 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 6813 | |
| 6814 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 6815 | BUG_ON(!reloc_root); |
| 6816 | btrfs_orphan_cleanup(reloc_root); |
| 6817 | return 0; |
| 6818 | } |
| 6819 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6820 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6821 | struct btrfs_root *root) |
| 6822 | { |
| 6823 | struct btrfs_root *reloc_root; |
| 6824 | struct extent_buffer *eb; |
| 6825 | struct btrfs_root_item *root_item; |
| 6826 | struct btrfs_key root_key; |
| 6827 | int ret; |
| 6828 | |
| 6829 | BUG_ON(!root->ref_cows); |
| 6830 | if (root->reloc_root) |
| 6831 | return 0; |
| 6832 | |
| 6833 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 6834 | BUG_ON(!root_item); |
| 6835 | |
| 6836 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 6837 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 6838 | BUG_ON(ret); |
| 6839 | |
| 6840 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 6841 | root_key.offset = root->root_key.objectid; |
| 6842 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6843 | |
| 6844 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 6845 | btrfs_set_root_refs(root_item, 0); |
| 6846 | btrfs_set_root_bytenr(root_item, eb->start); |
| 6847 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6848 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6849 | |
| 6850 | btrfs_tree_unlock(eb); |
| 6851 | free_extent_buffer(eb); |
| 6852 | |
| 6853 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 6854 | &root_key, root_item); |
| 6855 | BUG_ON(ret); |
| 6856 | kfree(root_item); |
| 6857 | |
| 6858 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 6859 | &root_key); |
| 6860 | BUG_ON(!reloc_root); |
| 6861 | reloc_root->last_trans = trans->transid; |
| 6862 | reloc_root->commit_root = NULL; |
| 6863 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 6864 | |
| 6865 | root->reloc_root = reloc_root; |
| 6866 | return 0; |
| 6867 | } |
| 6868 | |
| 6869 | /* |
| 6870 | * Core function of space balance. |
| 6871 | * |
| 6872 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6873 | * counted roots. There is one reloc tree for each subvol, and all |
| 6874 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 6875 | * of the latest committed roots of subvols (root->commit_root). |
| 6876 | * |
| 6877 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 6878 | * COW the block through subvol's reloc tree, then update block pointer |
| 6879 | * in the subvol to point to the new block. Since all reloc trees share |
| 6880 | * same root key objectid, doing special handing for tree blocks owned |
| 6881 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 6882 | * we can use the resulting new block directly when the same block is |
| 6883 | * required to COW again through other reloc trees. By this way, relocated |
| 6884 | * tree blocks are shared between reloc trees, so they are also shared |
| 6885 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6886 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6887 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6888 | struct btrfs_root *root, |
| 6889 | struct btrfs_path *path, |
| 6890 | struct btrfs_key *first_key, |
| 6891 | struct btrfs_ref_path *ref_path, |
| 6892 | struct btrfs_block_group_cache *group, |
| 6893 | struct inode *reloc_inode) |
| 6894 | { |
| 6895 | struct btrfs_root *reloc_root; |
| 6896 | struct extent_buffer *eb = NULL; |
| 6897 | struct btrfs_key *keys; |
| 6898 | u64 *nodes; |
| 6899 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6900 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6901 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6902 | int ret; |
| 6903 | |
| 6904 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 6905 | lowest_level = ref_path->owner_objectid; |
| 6906 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6907 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6908 | path->lowest_level = lowest_level; |
| 6909 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 6910 | BUG_ON(ret < 0); |
| 6911 | path->lowest_level = 0; |
| 6912 | btrfs_release_path(root, path); |
| 6913 | return 0; |
| 6914 | } |
| 6915 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6916 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6917 | ret = init_reloc_tree(trans, root); |
| 6918 | BUG_ON(ret); |
| 6919 | reloc_root = root->reloc_root; |
| 6920 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6921 | shared_level = ref_path->shared_level; |
| 6922 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6923 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6924 | keys = ref_path->node_keys; |
| 6925 | nodes = ref_path->new_nodes; |
| 6926 | memset(&keys[shared_level + 1], 0, |
| 6927 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 6928 | memset(&nodes[shared_level + 1], 0, |
| 6929 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6930 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6931 | if (nodes[lowest_level] == 0) { |
| 6932 | path->lowest_level = lowest_level; |
| 6933 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6934 | 0, 1); |
| 6935 | BUG_ON(ret); |
| 6936 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 6937 | eb = path->nodes[level]; |
| 6938 | if (!eb || eb == reloc_root->node) |
| 6939 | break; |
| 6940 | nodes[level] = eb->start; |
| 6941 | if (level == 0) |
| 6942 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 6943 | else |
| 6944 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 6945 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6946 | if (nodes[0] && |
| 6947 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6948 | eb = path->nodes[0]; |
| 6949 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 6950 | group, reloc_inode); |
| 6951 | BUG_ON(ret); |
| 6952 | } |
| 6953 | btrfs_release_path(reloc_root, path); |
| 6954 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6955 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6956 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6957 | BUG_ON(ret); |
| 6958 | } |
| 6959 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6960 | /* |
| 6961 | * replace tree blocks in the fs tree with tree blocks in |
| 6962 | * the reloc tree. |
| 6963 | */ |
| 6964 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 6965 | BUG_ON(ret < 0); |
| 6966 | |
| 6967 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6968 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6969 | 0, 0); |
| 6970 | BUG_ON(ret); |
| 6971 | extent_buffer_get(path->nodes[0]); |
| 6972 | eb = path->nodes[0]; |
| 6973 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6974 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 6975 | BUG_ON(ret); |
| 6976 | free_extent_buffer(eb); |
| 6977 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6978 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6979 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6980 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6981 | return 0; |
| 6982 | } |
| 6983 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6984 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6985 | struct btrfs_root *root, |
| 6986 | struct btrfs_path *path, |
| 6987 | struct btrfs_key *first_key, |
| 6988 | struct btrfs_ref_path *ref_path) |
| 6989 | { |
| 6990 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6991 | |
| 6992 | ret = relocate_one_path(trans, root, path, first_key, |
| 6993 | ref_path, NULL, NULL); |
| 6994 | BUG_ON(ret); |
| 6995 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6996 | return 0; |
| 6997 | } |
| 6998 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6999 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7000 | struct btrfs_root *extent_root, |
| 7001 | struct btrfs_path *path, |
| 7002 | struct btrfs_key *extent_key) |
| 7003 | { |
| 7004 | int ret; |
| 7005 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7006 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 7007 | if (ret) |
| 7008 | goto out; |
| 7009 | ret = btrfs_del_item(trans, extent_root, path); |
| 7010 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7011 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7012 | return ret; |
| 7013 | } |
| 7014 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7015 | 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] | 7016 | struct btrfs_ref_path *ref_path) |
| 7017 | { |
| 7018 | struct btrfs_key root_key; |
| 7019 | |
| 7020 | root_key.objectid = ref_path->root_objectid; |
| 7021 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7022 | if (is_cowonly_root(ref_path->root_objectid)) |
| 7023 | root_key.offset = 0; |
| 7024 | else |
| 7025 | root_key.offset = (u64)-1; |
| 7026 | |
| 7027 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7028 | } |
| 7029 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7030 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7031 | struct btrfs_path *path, |
| 7032 | struct btrfs_key *extent_key, |
| 7033 | struct btrfs_block_group_cache *group, |
| 7034 | struct inode *reloc_inode, int pass) |
| 7035 | { |
| 7036 | struct btrfs_trans_handle *trans; |
| 7037 | struct btrfs_root *found_root; |
| 7038 | struct btrfs_ref_path *ref_path = NULL; |
| 7039 | struct disk_extent *new_extents = NULL; |
| 7040 | int nr_extents = 0; |
| 7041 | int loops; |
| 7042 | int ret; |
| 7043 | int level; |
| 7044 | struct btrfs_key first_key; |
| 7045 | u64 prev_block = 0; |
| 7046 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7047 | |
| 7048 | trans = btrfs_start_transaction(extent_root, 1); |
| 7049 | BUG_ON(!trans); |
| 7050 | |
| 7051 | if (extent_key->objectid == 0) { |
| 7052 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7053 | goto out; |
| 7054 | } |
| 7055 | |
| 7056 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7057 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7058 | ret = -ENOMEM; |
| 7059 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | for (loops = 0; ; loops++) { |
| 7063 | if (loops == 0) { |
| 7064 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7065 | extent_key->objectid); |
| 7066 | } else { |
| 7067 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7068 | } |
| 7069 | if (ret < 0) |
| 7070 | goto out; |
| 7071 | if (ret > 0) |
| 7072 | break; |
| 7073 | |
| 7074 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7075 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7076 | continue; |
| 7077 | |
| 7078 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7079 | BUG_ON(!found_root); |
| 7080 | /* |
| 7081 | * for reference counted tree, only process reference paths |
| 7082 | * rooted at the latest committed root. |
| 7083 | */ |
| 7084 | if (found_root->ref_cows && |
| 7085 | ref_path->root_generation != found_root->root_key.offset) |
| 7086 | continue; |
| 7087 | |
| 7088 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7089 | if (pass == 0) { |
| 7090 | /* |
| 7091 | * copy data extents to new locations |
| 7092 | */ |
| 7093 | u64 group_start = group->key.objectid; |
| 7094 | ret = relocate_data_extent(reloc_inode, |
| 7095 | extent_key, |
| 7096 | group_start); |
| 7097 | if (ret < 0) |
| 7098 | goto out; |
| 7099 | break; |
| 7100 | } |
| 7101 | level = 0; |
| 7102 | } else { |
| 7103 | level = ref_path->owner_objectid; |
| 7104 | } |
| 7105 | |
| 7106 | if (prev_block != ref_path->nodes[level]) { |
| 7107 | struct extent_buffer *eb; |
| 7108 | u64 block_start = ref_path->nodes[level]; |
| 7109 | u64 block_size = btrfs_level_size(found_root, level); |
| 7110 | |
| 7111 | eb = read_tree_block(found_root, block_start, |
| 7112 | block_size, 0); |
| 7113 | btrfs_tree_lock(eb); |
| 7114 | BUG_ON(level != btrfs_header_level(eb)); |
| 7115 | |
| 7116 | if (level == 0) |
| 7117 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7118 | else |
| 7119 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7120 | |
| 7121 | btrfs_tree_unlock(eb); |
| 7122 | free_extent_buffer(eb); |
| 7123 | prev_block = block_start; |
| 7124 | } |
| 7125 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7126 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7127 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7128 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7129 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7130 | /* |
| 7131 | * try to update data extent references while |
| 7132 | * keeping metadata shared between snapshots. |
| 7133 | */ |
| 7134 | if (pass == 1) { |
| 7135 | ret = relocate_one_path(trans, found_root, |
| 7136 | path, &first_key, ref_path, |
| 7137 | group, reloc_inode); |
| 7138 | if (ret < 0) |
| 7139 | goto out; |
| 7140 | continue; |
| 7141 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7142 | /* |
| 7143 | * use fallback method to process the remaining |
| 7144 | * references. |
| 7145 | */ |
| 7146 | if (!new_extents) { |
| 7147 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7148 | new_extents = kmalloc(sizeof(*new_extents), |
| 7149 | GFP_NOFS); |
| 7150 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7151 | ret = get_new_locations(reloc_inode, |
| 7152 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7153 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7154 | &new_extents, |
| 7155 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7156 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7157 | goto out; |
| 7158 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7159 | ret = replace_one_extent(trans, found_root, |
| 7160 | path, extent_key, |
| 7161 | &first_key, ref_path, |
| 7162 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7163 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7164 | ret = relocate_tree_block(trans, found_root, path, |
| 7165 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7166 | } |
| 7167 | if (ret < 0) |
| 7168 | goto out; |
| 7169 | } |
| 7170 | ret = 0; |
| 7171 | out: |
| 7172 | btrfs_end_transaction(trans, extent_root); |
| 7173 | kfree(new_extents); |
| 7174 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7175 | return ret; |
| 7176 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7177 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7178 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7179 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7180 | { |
| 7181 | u64 num_devices; |
| 7182 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7183 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7184 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7185 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7186 | if (num_devices == 1) { |
| 7187 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7188 | stripped = flags & ~stripped; |
| 7189 | |
| 7190 | /* turn raid0 into single device chunks */ |
| 7191 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7192 | return stripped; |
| 7193 | |
| 7194 | /* turn mirroring into duplication */ |
| 7195 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7196 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7197 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7198 | return flags; |
| 7199 | } else { |
| 7200 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7201 | if (flags & stripped) |
| 7202 | return flags; |
| 7203 | |
| 7204 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7205 | stripped = flags & ~stripped; |
| 7206 | |
| 7207 | /* switch duplicated blocks with raid1 */ |
| 7208 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7209 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7210 | |
| 7211 | /* turn single device chunks into raid0 */ |
| 7212 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7213 | } |
| 7214 | return flags; |
| 7215 | } |
| 7216 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7217 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7218 | struct btrfs_block_group_cache *shrink_block_group, |
| 7219 | int force) |
| 7220 | { |
| 7221 | struct btrfs_trans_handle *trans; |
| 7222 | u64 new_alloc_flags; |
| 7223 | u64 calc; |
| 7224 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7225 | spin_lock(&shrink_block_group->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7226 | if (btrfs_block_group_used(&shrink_block_group->item) + |
| 7227 | shrink_block_group->reserved > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7228 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7229 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7230 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7231 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7232 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7233 | new_alloc_flags = update_block_group_flags(root, |
| 7234 | shrink_block_group->flags); |
| 7235 | if (new_alloc_flags != shrink_block_group->flags) { |
| 7236 | calc = |
| 7237 | btrfs_block_group_used(&shrink_block_group->item); |
| 7238 | } else { |
| 7239 | calc = shrink_block_group->key.offset; |
| 7240 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7241 | spin_unlock(&shrink_block_group->lock); |
| 7242 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7243 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 7244 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7245 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7246 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7247 | } else |
| 7248 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7249 | return 0; |
| 7250 | } |
| 7251 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7252 | |
| 7253 | int btrfs_prepare_block_group_relocation(struct btrfs_root *root, |
| 7254 | struct btrfs_block_group_cache *group) |
| 7255 | |
| 7256 | { |
| 7257 | __alloc_chunk_for_shrink(root, group, 1); |
| 7258 | set_block_group_readonly(group); |
| 7259 | return 0; |
| 7260 | } |
| 7261 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7262 | /* |
| 7263 | * checks to see if its even possible to relocate this block group. |
| 7264 | * |
| 7265 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7266 | * ok to go ahead and try. |
| 7267 | */ |
| 7268 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7269 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7270 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7271 | struct btrfs_space_info *space_info; |
| 7272 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7273 | struct btrfs_device *device; |
| 7274 | int full = 0; |
| 7275 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7276 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7277 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7278 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7279 | /* odd, couldn't find the block group, leave it alone */ |
| 7280 | if (!block_group) |
| 7281 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7282 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7283 | /* no bytes used, we're good */ |
| 7284 | if (!btrfs_block_group_used(&block_group->item)) |
| 7285 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7286 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7287 | space_info = block_group->space_info; |
| 7288 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7289 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7290 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7291 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7292 | /* |
| 7293 | * 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] | 7294 | * relocate it unless we're able to allocate a new chunk below. |
| 7295 | * |
| 7296 | * Otherwise, we need to make sure we have room in the space to handle |
| 7297 | * 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] | 7298 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7299 | if ((space_info->total_bytes != block_group->key.offset) && |
| 7300 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7301 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7302 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7303 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7304 | spin_unlock(&space_info->lock); |
| 7305 | goto out; |
| 7306 | } |
| 7307 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7308 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7309 | /* |
| 7310 | * ok we don't have enough space, but maybe we have free space on our |
| 7311 | * devices to allocate new chunks for relocation, so loop through our |
| 7312 | * alloc devices and guess if we have enough space. However, if we |
| 7313 | * were marked as full, then we know there aren't enough chunks, and we |
| 7314 | * can just return. |
| 7315 | */ |
| 7316 | ret = -1; |
| 7317 | if (full) |
| 7318 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 7319 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7320 | mutex_lock(&root->fs_info->chunk_mutex); |
| 7321 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 7322 | u64 min_free = btrfs_block_group_used(&block_group->item); |
| 7323 | u64 dev_offset, max_avail; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7324 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7325 | /* |
| 7326 | * check to make sure we can actually find a chunk with enough |
| 7327 | * space to fit our block group in. |
| 7328 | */ |
| 7329 | if (device->total_bytes > device->bytes_used + min_free) { |
| 7330 | ret = find_free_dev_extent(NULL, device, min_free, |
| 7331 | &dev_offset, &max_avail); |
| 7332 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7333 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7334 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7335 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7336 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7337 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7338 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7339 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7340 | return ret; |
| 7341 | } |
| 7342 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7343 | static int find_first_block_group(struct btrfs_root *root, |
| 7344 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7345 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7346 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7347 | struct btrfs_key found_key; |
| 7348 | struct extent_buffer *leaf; |
| 7349 | int slot; |
| 7350 | |
| 7351 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7352 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7353 | goto out; |
| 7354 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7355 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7356 | slot = path->slots[0]; |
| 7357 | leaf = path->nodes[0]; |
| 7358 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7359 | ret = btrfs_next_leaf(root, path); |
| 7360 | if (ret == 0) |
| 7361 | continue; |
| 7362 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7363 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7364 | break; |
| 7365 | } |
| 7366 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7367 | |
| 7368 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7369 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7370 | ret = 0; |
| 7371 | goto out; |
| 7372 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7373 | path->slots[0]++; |
| 7374 | } |
| 7375 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7376 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7377 | return ret; |
| 7378 | } |
| 7379 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7380 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7381 | { |
| 7382 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7383 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7384 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7385 | struct rb_node *n; |
| 7386 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7387 | down_write(&info->extent_commit_sem); |
| 7388 | while (!list_empty(&info->caching_block_groups)) { |
| 7389 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7390 | struct btrfs_caching_control, list); |
| 7391 | list_del(&caching_ctl->list); |
| 7392 | put_caching_control(caching_ctl); |
| 7393 | } |
| 7394 | up_write(&info->extent_commit_sem); |
| 7395 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7396 | spin_lock(&info->block_group_cache_lock); |
| 7397 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7398 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7399 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7400 | rb_erase(&block_group->cache_node, |
| 7401 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7402 | spin_unlock(&info->block_group_cache_lock); |
| 7403 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7404 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7405 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7406 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7407 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7408 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7409 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7410 | |
| 7411 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 7412 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7413 | |
| 7414 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7415 | } |
| 7416 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7417 | |
| 7418 | /* now that all the block groups are freed, go through and |
| 7419 | * free all the space_info structs. This is only called during |
| 7420 | * the final stages of unmount, and so we know nobody is |
| 7421 | * using them. We call synchronize_rcu() once before we start, |
| 7422 | * just to be on the safe side. |
| 7423 | */ |
| 7424 | synchronize_rcu(); |
| 7425 | |
| 7426 | while(!list_empty(&info->space_info)) { |
| 7427 | space_info = list_entry(info->space_info.next, |
| 7428 | struct btrfs_space_info, |
| 7429 | list); |
| 7430 | |
| 7431 | list_del(&space_info->list); |
| 7432 | kfree(space_info); |
| 7433 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7434 | return 0; |
| 7435 | } |
| 7436 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7437 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7438 | { |
| 7439 | struct btrfs_path *path; |
| 7440 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7441 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7442 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7443 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7444 | struct btrfs_key key; |
| 7445 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7446 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7447 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7448 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7449 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7450 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7451 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7452 | path = btrfs_alloc_path(); |
| 7453 | if (!path) |
| 7454 | return -ENOMEM; |
| 7455 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7456 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7457 | ret = find_first_block_group(root, path, &key); |
| 7458 | if (ret > 0) { |
| 7459 | ret = 0; |
| 7460 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7461 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7462 | if (ret != 0) |
| 7463 | goto error; |
| 7464 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7465 | leaf = path->nodes[0]; |
| 7466 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7467 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7468 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7469 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7470 | break; |
| 7471 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7472 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7473 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7474 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7475 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7476 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7477 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7478 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7479 | |
| 7480 | /* |
| 7481 | * we only want to have 32k of ram per block group for keeping |
| 7482 | * track of free space, and if we pass 1/2 of that we want to |
| 7483 | * start converting things over to using bitmaps |
| 7484 | */ |
| 7485 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7486 | sizeof(struct btrfs_free_space); |
| 7487 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7488 | read_extent_buffer(leaf, &cache->item, |
| 7489 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7490 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7491 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7492 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7493 | key.objectid = found_key.objectid + found_key.offset; |
| 7494 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7495 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7496 | cache->sectorsize = root->sectorsize; |
| 7497 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7498 | /* |
| 7499 | * check for two cases, either we are full, and therefore |
| 7500 | * don't need to bother with the caching work since we won't |
| 7501 | * find any space, or we are empty, and we can just add all |
| 7502 | * the space in and be done with it. This saves us _alot_ of |
| 7503 | * time, particularly in the full case. |
| 7504 | */ |
| 7505 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7506 | exclude_super_stripes(root, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7507 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7508 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7509 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7510 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7511 | exclude_super_stripes(root, cache); |
| 7512 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7513 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7514 | add_new_free_space(cache, root->fs_info, |
| 7515 | found_key.objectid, |
| 7516 | found_key.objectid + |
| 7517 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7518 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7519 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7520 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7521 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7522 | btrfs_block_group_used(&cache->item), |
| 7523 | &space_info); |
| 7524 | BUG_ON(ret); |
| 7525 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7526 | spin_lock(&cache->space_info->lock); |
| 7527 | cache->space_info->bytes_super += cache->bytes_super; |
| 7528 | spin_unlock(&cache->space_info->lock); |
| 7529 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7530 | down_write(&space_info->groups_sem); |
| 7531 | list_add_tail(&cache->list, &space_info->block_groups); |
| 7532 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7533 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7534 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7535 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7536 | |
| 7537 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7538 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 7539 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7540 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7541 | ret = 0; |
| 7542 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7543 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7544 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7545 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7546 | |
| 7547 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7548 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7549 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7550 | u64 size) |
| 7551 | { |
| 7552 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7553 | struct btrfs_root *extent_root; |
| 7554 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7555 | |
| 7556 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7557 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7558 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7559 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7560 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7561 | if (!cache) |
| 7562 | return -ENOMEM; |
| 7563 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7564 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7565 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7566 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7567 | cache->sectorsize = root->sectorsize; |
| 7568 | |
| 7569 | /* |
| 7570 | * we only want to have 32k of ram per block group for keeping track |
| 7571 | * of free space, and if we pass 1/2 of that we want to start |
| 7572 | * converting things over to using bitmaps |
| 7573 | */ |
| 7574 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7575 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7576 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7577 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7578 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7579 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7580 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7581 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7582 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7583 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7584 | cache->flags = type; |
| 7585 | btrfs_set_block_group_flags(&cache->item, type); |
| 7586 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7587 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7588 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7589 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7590 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7591 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7592 | chunk_offset + size); |
| 7593 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7594 | free_excluded_extents(root, cache); |
| 7595 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7596 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7597 | &cache->space_info); |
| 7598 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7599 | |
| 7600 | spin_lock(&cache->space_info->lock); |
| 7601 | cache->space_info->bytes_super += cache->bytes_super; |
| 7602 | spin_unlock(&cache->space_info->lock); |
| 7603 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7604 | down_write(&cache->space_info->groups_sem); |
| 7605 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 7606 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7607 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7608 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7609 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7610 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7611 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7612 | sizeof(cache->item)); |
| 7613 | BUG_ON(ret); |
| 7614 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7615 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7616 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7617 | return 0; |
| 7618 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7619 | |
| 7620 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7621 | struct btrfs_root *root, u64 group_start) |
| 7622 | { |
| 7623 | struct btrfs_path *path; |
| 7624 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7625 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7626 | struct btrfs_key key; |
| 7627 | int ret; |
| 7628 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7629 | root = root->fs_info->extent_root; |
| 7630 | |
| 7631 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7632 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7633 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7634 | |
| 7635 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7636 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7637 | /* make sure this block group isn't part of an allocation cluster */ |
| 7638 | cluster = &root->fs_info->data_alloc_cluster; |
| 7639 | spin_lock(&cluster->refill_lock); |
| 7640 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7641 | spin_unlock(&cluster->refill_lock); |
| 7642 | |
| 7643 | /* |
| 7644 | * make sure this block group isn't part of a metadata |
| 7645 | * allocation cluster |
| 7646 | */ |
| 7647 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7648 | spin_lock(&cluster->refill_lock); |
| 7649 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7650 | spin_unlock(&cluster->refill_lock); |
| 7651 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7652 | path = btrfs_alloc_path(); |
| 7653 | BUG_ON(!path); |
| 7654 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7655 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7656 | rb_erase(&block_group->cache_node, |
| 7657 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7658 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7659 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7660 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7661 | /* |
| 7662 | * we must use list_del_init so people can check to see if they |
| 7663 | * are still on the list after taking the semaphore |
| 7664 | */ |
| 7665 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7666 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7667 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7668 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7669 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7670 | |
| 7671 | btrfs_remove_free_space_cache(block_group); |
| 7672 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7673 | spin_lock(&block_group->space_info->lock); |
| 7674 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7675 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 7676 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7677 | |
| 7678 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7679 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7680 | btrfs_put_block_group(block_group); |
| 7681 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7682 | |
| 7683 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7684 | if (ret > 0) |
| 7685 | ret = -EIO; |
| 7686 | if (ret < 0) |
| 7687 | goto out; |
| 7688 | |
| 7689 | ret = btrfs_del_item(trans, root, path); |
| 7690 | out: |
| 7691 | btrfs_free_path(path); |
| 7692 | return ret; |
| 7693 | } |