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 | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 71 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 72 | static noinline int |
| 73 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 74 | { |
| 75 | smp_mb(); |
| 76 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 77 | } |
| 78 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 79 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 80 | { |
| 81 | return (cache->flags & bits) == bits; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * this adds the block group to the fs_info rb tree for the block group |
| 86 | * cache |
| 87 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 88 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 89 | struct btrfs_block_group_cache *block_group) |
| 90 | { |
| 91 | struct rb_node **p; |
| 92 | struct rb_node *parent = NULL; |
| 93 | struct btrfs_block_group_cache *cache; |
| 94 | |
| 95 | spin_lock(&info->block_group_cache_lock); |
| 96 | p = &info->block_group_cache_tree.rb_node; |
| 97 | |
| 98 | while (*p) { |
| 99 | parent = *p; |
| 100 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 101 | cache_node); |
| 102 | if (block_group->key.objectid < cache->key.objectid) { |
| 103 | p = &(*p)->rb_left; |
| 104 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 105 | p = &(*p)->rb_right; |
| 106 | } else { |
| 107 | spin_unlock(&info->block_group_cache_lock); |
| 108 | return -EEXIST; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | rb_link_node(&block_group->cache_node, parent, p); |
| 113 | rb_insert_color(&block_group->cache_node, |
| 114 | &info->block_group_cache_tree); |
| 115 | spin_unlock(&info->block_group_cache_lock); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * This will return the block group at or after bytenr if contains is 0, else |
| 122 | * it will return the block group that contains the bytenr |
| 123 | */ |
| 124 | static struct btrfs_block_group_cache * |
| 125 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 126 | int contains) |
| 127 | { |
| 128 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 129 | struct rb_node *n; |
| 130 | u64 end, start; |
| 131 | |
| 132 | spin_lock(&info->block_group_cache_lock); |
| 133 | n = info->block_group_cache_tree.rb_node; |
| 134 | |
| 135 | while (n) { |
| 136 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 137 | cache_node); |
| 138 | end = cache->key.objectid + cache->key.offset - 1; |
| 139 | start = cache->key.objectid; |
| 140 | |
| 141 | if (bytenr < start) { |
| 142 | if (!contains && (!ret || start < ret->key.objectid)) |
| 143 | ret = cache; |
| 144 | n = n->rb_left; |
| 145 | } else if (bytenr > start) { |
| 146 | if (contains && bytenr <= end) { |
| 147 | ret = cache; |
| 148 | break; |
| 149 | } |
| 150 | n = n->rb_right; |
| 151 | } else { |
| 152 | ret = cache; |
| 153 | break; |
| 154 | } |
| 155 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 156 | if (ret) |
| 157 | atomic_inc(&ret->count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 158 | spin_unlock(&info->block_group_cache_lock); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 163 | static int add_excluded_extent(struct btrfs_root *root, |
| 164 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 165 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 166 | u64 end = start + num_bytes - 1; |
| 167 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 168 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 169 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 170 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 171 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 174 | static void free_excluded_extents(struct btrfs_root *root, |
| 175 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 176 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 177 | u64 start, end; |
| 178 | |
| 179 | start = cache->key.objectid; |
| 180 | end = start + cache->key.offset - 1; |
| 181 | |
| 182 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 183 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 184 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 185 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 186 | } |
| 187 | |
| 188 | static int exclude_super_stripes(struct btrfs_root *root, |
| 189 | struct btrfs_block_group_cache *cache) |
| 190 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 191 | u64 bytenr; |
| 192 | u64 *logical; |
| 193 | int stripe_len; |
| 194 | int i, nr, ret; |
| 195 | |
| 196 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 197 | bytenr = btrfs_sb_offset(i); |
| 198 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 199 | cache->key.objectid, bytenr, |
| 200 | 0, &logical, &nr, &stripe_len); |
| 201 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 202 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 203 | while (nr--) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 204 | ret = add_excluded_extent(root, logical[nr], |
| 205 | stripe_len); |
| 206 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 207 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 208 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 209 | kfree(logical); |
| 210 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 214 | static struct btrfs_caching_control * |
| 215 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 216 | { |
| 217 | struct btrfs_caching_control *ctl; |
| 218 | |
| 219 | spin_lock(&cache->lock); |
| 220 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 221 | spin_unlock(&cache->lock); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | ctl = cache->caching_ctl; |
| 226 | atomic_inc(&ctl->count); |
| 227 | spin_unlock(&cache->lock); |
| 228 | return ctl; |
| 229 | } |
| 230 | |
| 231 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 232 | { |
| 233 | if (atomic_dec_and_test(&ctl->count)) |
| 234 | kfree(ctl); |
| 235 | } |
| 236 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 237 | /* |
| 238 | * this is only called by cache_block_group, since we could have freed extents |
| 239 | * we need to check the pinned_extents for any extents that can't be used yet |
| 240 | * since their free space will be released as soon as the transaction commits. |
| 241 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 242 | 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] | 243 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 244 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 245 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 246 | int ret; |
| 247 | |
| 248 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 249 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 250 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 251 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 252 | if (ret) |
| 253 | break; |
| 254 | |
| 255 | if (extent_start == start) { |
| 256 | start = extent_end + 1; |
| 257 | } else if (extent_start > start && extent_start < end) { |
| 258 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 259 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 260 | ret = btrfs_add_free_space(block_group, start, |
| 261 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 262 | BUG_ON(ret); |
| 263 | start = extent_end + 1; |
| 264 | } else { |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (start < end) { |
| 270 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 271 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 272 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 273 | BUG_ON(ret); |
| 274 | } |
| 275 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 276 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 279 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 280 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 281 | struct btrfs_block_group_cache *block_group = data; |
| 282 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 283 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 284 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 285 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 286 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 287 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 288 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 289 | u64 last = 0; |
| 290 | u32 nritems; |
| 291 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 292 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 293 | path = btrfs_alloc_path(); |
| 294 | if (!path) |
| 295 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 296 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 297 | exclude_super_stripes(extent_root, block_group); |
| 298 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 299 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 300 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 301 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 302 | * We don't want to deadlock with somebody trying to allocate a new |
| 303 | * extent for the extent root while also trying to search the extent |
| 304 | * root to add free space. So we skip locking and search the commit |
| 305 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 306 | */ |
| 307 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 308 | path->search_commit_root = 1; |
| 309 | path->reada = 2; |
| 310 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 311 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 312 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 313 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 314 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 315 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 316 | /* need to make sure the commit_root doesn't disappear */ |
| 317 | down_read(&fs_info->extent_commit_sem); |
| 318 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 319 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 320 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 321 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 322 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 323 | leaf = path->nodes[0]; |
| 324 | nritems = btrfs_header_nritems(leaf); |
| 325 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 326 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 327 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 328 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 329 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 330 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 331 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 332 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 333 | if (path->slots[0] < nritems) { |
| 334 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 335 | } else { |
| 336 | ret = find_next_key(path, 0, &key); |
| 337 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 338 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 339 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 340 | caching_ctl->progress = last; |
| 341 | btrfs_release_path(extent_root, path); |
| 342 | up_read(&fs_info->extent_commit_sem); |
| 343 | mutex_unlock(&caching_ctl->mutex); |
| 344 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 345 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 346 | else |
| 347 | cond_resched(); |
| 348 | goto again; |
| 349 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 350 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 351 | if (key.objectid < block_group->key.objectid) { |
| 352 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 353 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 354 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 355 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 356 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 357 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 358 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 359 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 360 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 361 | total_found += add_new_free_space(block_group, |
| 362 | fs_info, last, |
| 363 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 364 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 365 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 366 | if (total_found > (1024 * 1024 * 2)) { |
| 367 | total_found = 0; |
| 368 | wake_up(&caching_ctl->wait); |
| 369 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 370 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 371 | path->slots[0]++; |
| 372 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 373 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 374 | |
| 375 | total_found += add_new_free_space(block_group, fs_info, last, |
| 376 | block_group->key.objectid + |
| 377 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 378 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 379 | |
| 380 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 381 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 382 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 383 | spin_unlock(&block_group->lock); |
| 384 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 385 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 386 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 387 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 388 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 389 | free_excluded_extents(extent_root, block_group); |
| 390 | |
| 391 | mutex_unlock(&caching_ctl->mutex); |
| 392 | wake_up(&caching_ctl->wait); |
| 393 | |
| 394 | put_caching_control(caching_ctl); |
| 395 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int cache_block_group(struct btrfs_block_group_cache *cache) |
| 400 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 401 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 402 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 403 | struct task_struct *tsk; |
| 404 | int ret = 0; |
| 405 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 406 | smp_mb(); |
| 407 | if (cache->cached != BTRFS_CACHE_NO) |
| 408 | return 0; |
| 409 | |
| 410 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); |
| 411 | BUG_ON(!caching_ctl); |
| 412 | |
| 413 | INIT_LIST_HEAD(&caching_ctl->list); |
| 414 | mutex_init(&caching_ctl->mutex); |
| 415 | init_waitqueue_head(&caching_ctl->wait); |
| 416 | caching_ctl->block_group = cache; |
| 417 | caching_ctl->progress = cache->key.objectid; |
| 418 | /* one for caching kthread, one for caching block group list */ |
| 419 | atomic_set(&caching_ctl->count, 2); |
| 420 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 421 | spin_lock(&cache->lock); |
| 422 | if (cache->cached != BTRFS_CACHE_NO) { |
| 423 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 424 | kfree(caching_ctl); |
| 425 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 426 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 427 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 428 | cache->cached = BTRFS_CACHE_STARTED; |
| 429 | spin_unlock(&cache->lock); |
| 430 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | down_write(&fs_info->extent_commit_sem); |
| 432 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 433 | up_write(&fs_info->extent_commit_sem); |
| 434 | |
| 435 | atomic_inc(&cache->space_info->caching_threads); |
| 436 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 437 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 438 | cache->key.objectid); |
| 439 | if (IS_ERR(tsk)) { |
| 440 | ret = PTR_ERR(tsk); |
| 441 | printk(KERN_ERR "error running thread %d\n", ret); |
| 442 | BUG(); |
| 443 | } |
| 444 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 445 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 448 | /* |
| 449 | * return the block group that starts at or after bytenr |
| 450 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 451 | static struct btrfs_block_group_cache * |
| 452 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 453 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 454 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 455 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 456 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 457 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 458 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 461 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 462 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 463 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 464 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 465 | struct btrfs_fs_info *info, |
| 466 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 467 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 468 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 469 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 470 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 471 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 472 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 473 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 474 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 475 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 476 | { |
| 477 | if (atomic_dec_and_test(&cache->count)) |
| 478 | kfree(cache); |
| 479 | } |
| 480 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 481 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 482 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 483 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 484 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 485 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 486 | |
| 487 | rcu_read_lock(); |
| 488 | list_for_each_entry_rcu(found, head, list) { |
| 489 | if (found->flags == flags) { |
| 490 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 491 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 492 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 493 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 494 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 495 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 496 | } |
| 497 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 498 | /* |
| 499 | * after adding space to the filesystem, we need to clear the full flags |
| 500 | * on all the space infos. |
| 501 | */ |
| 502 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 503 | { |
| 504 | struct list_head *head = &info->space_info; |
| 505 | struct btrfs_space_info *found; |
| 506 | |
| 507 | rcu_read_lock(); |
| 508 | list_for_each_entry_rcu(found, head, list) |
| 509 | found->full = 0; |
| 510 | rcu_read_unlock(); |
| 511 | } |
| 512 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 513 | static u64 div_factor(u64 num, int factor) |
| 514 | { |
| 515 | if (factor == 10) |
| 516 | return num; |
| 517 | num *= factor; |
| 518 | do_div(num, 10); |
| 519 | return num; |
| 520 | } |
| 521 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 522 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 523 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 524 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 525 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 526 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 527 | u64 last = max(search_hint, search_start); |
| 528 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 529 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 530 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 531 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 532 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 533 | while (1) { |
| 534 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 535 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 536 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 537 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 538 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 539 | last = cache->key.objectid + cache->key.offset; |
| 540 | used = btrfs_block_group_used(&cache->item); |
| 541 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 542 | if ((full_search || !cache->ro) && |
| 543 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 544 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 545 | div_factor(cache->key.offset, factor)) { |
| 546 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 547 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 548 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 549 | goto found; |
| 550 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 551 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 552 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 553 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 554 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 555 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 556 | if (!wrapped) { |
| 557 | last = search_start; |
| 558 | wrapped = 1; |
| 559 | goto again; |
| 560 | } |
| 561 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 562 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 563 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 564 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 565 | goto again; |
| 566 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 567 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 568 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 569 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 570 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 571 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 572 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 573 | { |
| 574 | int ret; |
| 575 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 576 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 577 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 578 | path = btrfs_alloc_path(); |
| 579 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 580 | key.objectid = start; |
| 581 | key.offset = len; |
| 582 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 583 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 584 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 585 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 586 | return ret; |
| 587 | } |
| 588 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 589 | /* |
| 590 | * Back reference rules. Back refs have three main goals: |
| 591 | * |
| 592 | * 1) differentiate between all holders of references to an extent so that |
| 593 | * when a reference is dropped we can make sure it was a valid reference |
| 594 | * before freeing the extent. |
| 595 | * |
| 596 | * 2) Provide enough information to quickly find the holders of an extent |
| 597 | * if we notice a given block is corrupted or bad. |
| 598 | * |
| 599 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 600 | * maintenance. This is actually the same as #2, but with a slightly |
| 601 | * different use case. |
| 602 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 603 | * There are two kinds of back refs. The implicit back refs is optimized |
| 604 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 605 | * back refs of this kind provide information about the block's owner tree |
| 606 | * and the pointer's key. These information allow us to find the block by |
| 607 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 608 | * referenced by their owner trees. The location of tree block is recorded |
| 609 | * in the back refs. Actually the full back refs is generic, and can be |
| 610 | * used in all cases the implicit back refs is used. The major shortcoming |
| 611 | * of the full back refs is its overhead. Every time a tree block gets |
| 612 | * COWed, we have to update back refs entry for all pointers in it. |
| 613 | * |
| 614 | * For a newly allocated tree block, we use implicit back refs for |
| 615 | * pointers in it. This means most tree related operations only involve |
| 616 | * implicit back refs. For a tree block created in old transaction, the |
| 617 | * only way to drop a reference to it is COW it. So we can detect the |
| 618 | * event that tree block loses its owner tree's reference and do the |
| 619 | * back refs conversion. |
| 620 | * |
| 621 | * When a tree block is COW'd through a tree, there are four cases: |
| 622 | * |
| 623 | * The reference count of the block is one and the tree is the block's |
| 624 | * owner tree. Nothing to do in this case. |
| 625 | * |
| 626 | * The reference count of the block is one and the tree is not the |
| 627 | * block's owner tree. In this case, full back refs is used for pointers |
| 628 | * in the block. Remove these full back refs, add implicit back refs for |
| 629 | * every pointers in the new block. |
| 630 | * |
| 631 | * The reference count of the block is greater than one and the tree is |
| 632 | * the block's owner tree. In this case, implicit back refs is used for |
| 633 | * pointers in the block. Add full back refs for every pointers in the |
| 634 | * block, increase lower level extents' reference counts. The original |
| 635 | * implicit back refs are entailed to the new block. |
| 636 | * |
| 637 | * The reference count of the block is greater than one and the tree is |
| 638 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 639 | * the new block, increase lower level extents' reference count. |
| 640 | * |
| 641 | * Back Reference Key composing: |
| 642 | * |
| 643 | * The key objectid corresponds to the first byte in the extent, |
| 644 | * The key type is used to differentiate between types of back refs. |
| 645 | * There are different meanings of the key offset for different types |
| 646 | * of back refs. |
| 647 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 648 | * File extents can be referenced by: |
| 649 | * |
| 650 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 651 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 652 | * - different offsets inside a file (bookend extents in file.c) |
| 653 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 654 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 655 | * |
| 656 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 657 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 658 | * - original offset in the file |
| 659 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 660 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 661 | * The key offset for the implicit back refs is hash of the first |
| 662 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 663 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 664 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 665 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 666 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 667 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 668 | * The key offset for the implicit back refs is the first byte of |
| 669 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 670 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 671 | * When a file extent is allocated, The implicit back refs is used. |
| 672 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 673 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 674 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 675 | * |
| 676 | * When a file extent is removed file truncation, we find the |
| 677 | * corresponding implicit back refs and check the following fields: |
| 678 | * |
| 679 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 680 | * |
| 681 | * Btree extents can be referenced by: |
| 682 | * |
| 683 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 684 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 685 | * Both the implicit back refs and the full back refs for tree blocks |
| 686 | * only consist of key. The key offset for the implicit back refs is |
| 687 | * objectid of block's owner tree. The key offset for the full back refs |
| 688 | * is the first byte of parent block. |
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 | * When implicit back refs is used, information about the lowest key and |
| 691 | * level of the tree block are required. These information are stored in |
| 692 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 693 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 694 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 695 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 696 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 697 | struct btrfs_root *root, |
| 698 | struct btrfs_path *path, |
| 699 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 700 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 701 | struct btrfs_extent_item *item; |
| 702 | struct btrfs_extent_item_v0 *ei0; |
| 703 | struct btrfs_extent_ref_v0 *ref0; |
| 704 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 705 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 706 | struct btrfs_key key; |
| 707 | struct btrfs_key found_key; |
| 708 | u32 new_size = sizeof(*item); |
| 709 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 710 | int ret; |
| 711 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 712 | leaf = path->nodes[0]; |
| 713 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 714 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 715 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 716 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 717 | struct btrfs_extent_item_v0); |
| 718 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 719 | |
| 720 | if (owner == (u64)-1) { |
| 721 | while (1) { |
| 722 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 723 | ret = btrfs_next_leaf(root, path); |
| 724 | if (ret < 0) |
| 725 | return ret; |
| 726 | BUG_ON(ret > 0); |
| 727 | leaf = path->nodes[0]; |
| 728 | } |
| 729 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 730 | path->slots[0]); |
| 731 | BUG_ON(key.objectid != found_key.objectid); |
| 732 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 733 | path->slots[0]++; |
| 734 | continue; |
| 735 | } |
| 736 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 737 | struct btrfs_extent_ref_v0); |
| 738 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | btrfs_release_path(root, path); |
| 743 | |
| 744 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 745 | new_size += sizeof(*bi); |
| 746 | |
| 747 | new_size -= sizeof(*ei0); |
| 748 | ret = btrfs_search_slot(trans, root, &key, path, |
| 749 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 750 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 751 | return ret; |
| 752 | BUG_ON(ret); |
| 753 | |
| 754 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 755 | BUG_ON(ret); |
| 756 | |
| 757 | leaf = path->nodes[0]; |
| 758 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 759 | btrfs_set_extent_refs(leaf, item, refs); |
| 760 | /* FIXME: get real generation */ |
| 761 | btrfs_set_extent_generation(leaf, item, 0); |
| 762 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 763 | btrfs_set_extent_flags(leaf, item, |
| 764 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 765 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 766 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 767 | /* FIXME: get first key of the block */ |
| 768 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 769 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 770 | } else { |
| 771 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 772 | } |
| 773 | btrfs_mark_buffer_dirty(leaf); |
| 774 | return 0; |
| 775 | } |
| 776 | #endif |
| 777 | |
| 778 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 779 | { |
| 780 | u32 high_crc = ~(u32)0; |
| 781 | u32 low_crc = ~(u32)0; |
| 782 | __le64 lenum; |
| 783 | |
| 784 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 785 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 786 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 787 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 788 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 789 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 790 | |
| 791 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 792 | } |
| 793 | |
| 794 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 795 | struct btrfs_extent_data_ref *ref) |
| 796 | { |
| 797 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 798 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 799 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 800 | } |
| 801 | |
| 802 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 803 | struct btrfs_extent_data_ref *ref, |
| 804 | u64 root_objectid, u64 owner, u64 offset) |
| 805 | { |
| 806 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 807 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 808 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 809 | return 0; |
| 810 | return 1; |
| 811 | } |
| 812 | |
| 813 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 814 | struct btrfs_root *root, |
| 815 | struct btrfs_path *path, |
| 816 | u64 bytenr, u64 parent, |
| 817 | u64 root_objectid, |
| 818 | u64 owner, u64 offset) |
| 819 | { |
| 820 | struct btrfs_key key; |
| 821 | struct btrfs_extent_data_ref *ref; |
| 822 | struct extent_buffer *leaf; |
| 823 | u32 nritems; |
| 824 | int ret; |
| 825 | int recow; |
| 826 | int err = -ENOENT; |
| 827 | |
| 828 | key.objectid = bytenr; |
| 829 | if (parent) { |
| 830 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 831 | key.offset = parent; |
| 832 | } else { |
| 833 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 834 | key.offset = hash_extent_data_ref(root_objectid, |
| 835 | owner, offset); |
| 836 | } |
| 837 | again: |
| 838 | recow = 0; |
| 839 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 840 | if (ret < 0) { |
| 841 | err = ret; |
| 842 | goto fail; |
| 843 | } |
| 844 | |
| 845 | if (parent) { |
| 846 | if (!ret) |
| 847 | return 0; |
| 848 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 849 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 850 | btrfs_release_path(root, path); |
| 851 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 852 | if (ret < 0) { |
| 853 | err = ret; |
| 854 | goto fail; |
| 855 | } |
| 856 | if (!ret) |
| 857 | return 0; |
| 858 | #endif |
| 859 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 863 | nritems = btrfs_header_nritems(leaf); |
| 864 | while (1) { |
| 865 | if (path->slots[0] >= nritems) { |
| 866 | ret = btrfs_next_leaf(root, path); |
| 867 | if (ret < 0) |
| 868 | err = ret; |
| 869 | if (ret) |
| 870 | goto fail; |
| 871 | |
| 872 | leaf = path->nodes[0]; |
| 873 | nritems = btrfs_header_nritems(leaf); |
| 874 | recow = 1; |
| 875 | } |
| 876 | |
| 877 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 878 | if (key.objectid != bytenr || |
| 879 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 880 | goto fail; |
| 881 | |
| 882 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 883 | struct btrfs_extent_data_ref); |
| 884 | |
| 885 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 886 | owner, offset)) { |
| 887 | if (recow) { |
| 888 | btrfs_release_path(root, path); |
| 889 | goto again; |
| 890 | } |
| 891 | err = 0; |
| 892 | break; |
| 893 | } |
| 894 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 895 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 896 | fail: |
| 897 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 898 | } |
| 899 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 900 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 901 | struct btrfs_root *root, |
| 902 | struct btrfs_path *path, |
| 903 | u64 bytenr, u64 parent, |
| 904 | u64 root_objectid, u64 owner, |
| 905 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 906 | { |
| 907 | struct btrfs_key key; |
| 908 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 909 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 910 | u32 num_refs; |
| 911 | int ret; |
| 912 | |
| 913 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 914 | if (parent) { |
| 915 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 916 | key.offset = parent; |
| 917 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 918 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 919 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 920 | key.offset = hash_extent_data_ref(root_objectid, |
| 921 | owner, offset); |
| 922 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 923 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 924 | |
| 925 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 926 | if (ret && ret != -EEXIST) |
| 927 | goto fail; |
| 928 | |
| 929 | leaf = path->nodes[0]; |
| 930 | if (parent) { |
| 931 | struct btrfs_shared_data_ref *ref; |
| 932 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 933 | struct btrfs_shared_data_ref); |
| 934 | if (ret == 0) { |
| 935 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 936 | } else { |
| 937 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 938 | num_refs += refs_to_add; |
| 939 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 940 | } |
| 941 | } else { |
| 942 | struct btrfs_extent_data_ref *ref; |
| 943 | while (ret == -EEXIST) { |
| 944 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 945 | struct btrfs_extent_data_ref); |
| 946 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 947 | owner, offset)) |
| 948 | break; |
| 949 | btrfs_release_path(root, path); |
| 950 | key.offset++; |
| 951 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 952 | size); |
| 953 | if (ret && ret != -EEXIST) |
| 954 | goto fail; |
| 955 | |
| 956 | leaf = path->nodes[0]; |
| 957 | } |
| 958 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 959 | struct btrfs_extent_data_ref); |
| 960 | if (ret == 0) { |
| 961 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 962 | root_objectid); |
| 963 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 964 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 965 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 966 | } else { |
| 967 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 968 | num_refs += refs_to_add; |
| 969 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 970 | } |
| 971 | } |
| 972 | btrfs_mark_buffer_dirty(leaf); |
| 973 | ret = 0; |
| 974 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 975 | btrfs_release_path(root, path); |
| 976 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 977 | } |
| 978 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 979 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 980 | struct btrfs_root *root, |
| 981 | struct btrfs_path *path, |
| 982 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 983 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 984 | struct btrfs_key key; |
| 985 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 986 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 987 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 988 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 989 | int ret = 0; |
| 990 | |
| 991 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 992 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 993 | |
| 994 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 995 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 996 | struct btrfs_extent_data_ref); |
| 997 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 998 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 999 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1000 | struct btrfs_shared_data_ref); |
| 1001 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1002 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1003 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1004 | struct btrfs_extent_ref_v0 *ref0; |
| 1005 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1006 | struct btrfs_extent_ref_v0); |
| 1007 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1008 | #endif |
| 1009 | } else { |
| 1010 | BUG(); |
| 1011 | } |
| 1012 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1013 | BUG_ON(num_refs < refs_to_drop); |
| 1014 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1015 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1016 | if (num_refs == 0) { |
| 1017 | ret = btrfs_del_item(trans, root, path); |
| 1018 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1019 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1020 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1021 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1022 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1023 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1024 | else { |
| 1025 | struct btrfs_extent_ref_v0 *ref0; |
| 1026 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1027 | struct btrfs_extent_ref_v0); |
| 1028 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1029 | } |
| 1030 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1031 | btrfs_mark_buffer_dirty(leaf); |
| 1032 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1033 | return ret; |
| 1034 | } |
| 1035 | |
| 1036 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1037 | struct btrfs_path *path, |
| 1038 | struct btrfs_extent_inline_ref *iref) |
| 1039 | { |
| 1040 | struct btrfs_key key; |
| 1041 | struct extent_buffer *leaf; |
| 1042 | struct btrfs_extent_data_ref *ref1; |
| 1043 | struct btrfs_shared_data_ref *ref2; |
| 1044 | u32 num_refs = 0; |
| 1045 | |
| 1046 | leaf = path->nodes[0]; |
| 1047 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1048 | if (iref) { |
| 1049 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1050 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1051 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1052 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1053 | } else { |
| 1054 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1055 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1056 | } |
| 1057 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1058 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1059 | struct btrfs_extent_data_ref); |
| 1060 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1061 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1062 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1063 | struct btrfs_shared_data_ref); |
| 1064 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1065 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1066 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1067 | struct btrfs_extent_ref_v0 *ref0; |
| 1068 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1069 | struct btrfs_extent_ref_v0); |
| 1070 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1071 | #endif |
| 1072 | } else { |
| 1073 | WARN_ON(1); |
| 1074 | } |
| 1075 | return num_refs; |
| 1076 | } |
| 1077 | |
| 1078 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1079 | struct btrfs_root *root, |
| 1080 | struct btrfs_path *path, |
| 1081 | u64 bytenr, u64 parent, |
| 1082 | u64 root_objectid) |
| 1083 | { |
| 1084 | struct btrfs_key key; |
| 1085 | int ret; |
| 1086 | |
| 1087 | key.objectid = bytenr; |
| 1088 | if (parent) { |
| 1089 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1090 | key.offset = parent; |
| 1091 | } else { |
| 1092 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1093 | key.offset = root_objectid; |
| 1094 | } |
| 1095 | |
| 1096 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1097 | if (ret > 0) |
| 1098 | ret = -ENOENT; |
| 1099 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1100 | if (ret == -ENOENT && parent) { |
| 1101 | btrfs_release_path(root, path); |
| 1102 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1103 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1104 | if (ret > 0) |
| 1105 | ret = -ENOENT; |
| 1106 | } |
| 1107 | #endif |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1112 | struct btrfs_root *root, |
| 1113 | struct btrfs_path *path, |
| 1114 | u64 bytenr, u64 parent, |
| 1115 | u64 root_objectid) |
| 1116 | { |
| 1117 | struct btrfs_key key; |
| 1118 | int ret; |
| 1119 | |
| 1120 | key.objectid = bytenr; |
| 1121 | if (parent) { |
| 1122 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1123 | key.offset = parent; |
| 1124 | } else { |
| 1125 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1126 | key.offset = root_objectid; |
| 1127 | } |
| 1128 | |
| 1129 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1130 | btrfs_release_path(root, path); |
| 1131 | return ret; |
| 1132 | } |
| 1133 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1134 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1135 | { |
| 1136 | int type; |
| 1137 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1138 | if (parent > 0) |
| 1139 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1140 | else |
| 1141 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1142 | } else { |
| 1143 | if (parent > 0) |
| 1144 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1145 | else |
| 1146 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1147 | } |
| 1148 | return type; |
| 1149 | } |
| 1150 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1151 | static int find_next_key(struct btrfs_path *path, int level, |
| 1152 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1153 | |
| 1154 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1155 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1156 | if (!path->nodes[level]) |
| 1157 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1158 | if (path->slots[level] + 1 >= |
| 1159 | btrfs_header_nritems(path->nodes[level])) |
| 1160 | continue; |
| 1161 | if (level == 0) |
| 1162 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1163 | path->slots[level] + 1); |
| 1164 | else |
| 1165 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1166 | path->slots[level] + 1); |
| 1167 | return 0; |
| 1168 | } |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1174 | * to the address of inline back ref, and 0 is returned. |
| 1175 | * |
| 1176 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1177 | * should be inserted, and -ENOENT is returned. |
| 1178 | * |
| 1179 | * if insert is true and there are too many inline back refs, the path |
| 1180 | * points to the extent item, and -EAGAIN is returned. |
| 1181 | * |
| 1182 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1183 | * items in the tree are ordered. |
| 1184 | */ |
| 1185 | static noinline_for_stack |
| 1186 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1187 | struct btrfs_root *root, |
| 1188 | struct btrfs_path *path, |
| 1189 | struct btrfs_extent_inline_ref **ref_ret, |
| 1190 | u64 bytenr, u64 num_bytes, |
| 1191 | u64 parent, u64 root_objectid, |
| 1192 | u64 owner, u64 offset, int insert) |
| 1193 | { |
| 1194 | struct btrfs_key key; |
| 1195 | struct extent_buffer *leaf; |
| 1196 | struct btrfs_extent_item *ei; |
| 1197 | struct btrfs_extent_inline_ref *iref; |
| 1198 | u64 flags; |
| 1199 | u64 item_size; |
| 1200 | unsigned long ptr; |
| 1201 | unsigned long end; |
| 1202 | int extra_size; |
| 1203 | int type; |
| 1204 | int want; |
| 1205 | int ret; |
| 1206 | int err = 0; |
| 1207 | |
| 1208 | key.objectid = bytenr; |
| 1209 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1210 | key.offset = num_bytes; |
| 1211 | |
| 1212 | want = extent_ref_type(parent, owner); |
| 1213 | if (insert) { |
| 1214 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1215 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1216 | } else |
| 1217 | extra_size = -1; |
| 1218 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1219 | if (ret < 0) { |
| 1220 | err = ret; |
| 1221 | goto out; |
| 1222 | } |
| 1223 | BUG_ON(ret); |
| 1224 | |
| 1225 | leaf = path->nodes[0]; |
| 1226 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1227 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1228 | if (item_size < sizeof(*ei)) { |
| 1229 | if (!insert) { |
| 1230 | err = -ENOENT; |
| 1231 | goto out; |
| 1232 | } |
| 1233 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1234 | extra_size); |
| 1235 | if (ret < 0) { |
| 1236 | err = ret; |
| 1237 | goto out; |
| 1238 | } |
| 1239 | leaf = path->nodes[0]; |
| 1240 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1241 | } |
| 1242 | #endif |
| 1243 | BUG_ON(item_size < sizeof(*ei)); |
| 1244 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1245 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1246 | flags = btrfs_extent_flags(leaf, ei); |
| 1247 | |
| 1248 | ptr = (unsigned long)(ei + 1); |
| 1249 | end = (unsigned long)ei + item_size; |
| 1250 | |
| 1251 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1252 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1253 | BUG_ON(ptr > end); |
| 1254 | } else { |
| 1255 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1256 | } |
| 1257 | |
| 1258 | err = -ENOENT; |
| 1259 | while (1) { |
| 1260 | if (ptr >= end) { |
| 1261 | WARN_ON(ptr > end); |
| 1262 | break; |
| 1263 | } |
| 1264 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1265 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1266 | if (want < type) |
| 1267 | break; |
| 1268 | if (want > type) { |
| 1269 | ptr += btrfs_extent_inline_ref_size(type); |
| 1270 | continue; |
| 1271 | } |
| 1272 | |
| 1273 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1274 | struct btrfs_extent_data_ref *dref; |
| 1275 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1276 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1277 | owner, offset)) { |
| 1278 | err = 0; |
| 1279 | break; |
| 1280 | } |
| 1281 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1282 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1283 | break; |
| 1284 | } else { |
| 1285 | u64 ref_offset; |
| 1286 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1287 | if (parent > 0) { |
| 1288 | if (parent == ref_offset) { |
| 1289 | err = 0; |
| 1290 | break; |
| 1291 | } |
| 1292 | if (ref_offset < parent) |
| 1293 | break; |
| 1294 | } else { |
| 1295 | if (root_objectid == ref_offset) { |
| 1296 | err = 0; |
| 1297 | break; |
| 1298 | } |
| 1299 | if (ref_offset < root_objectid) |
| 1300 | break; |
| 1301 | } |
| 1302 | } |
| 1303 | ptr += btrfs_extent_inline_ref_size(type); |
| 1304 | } |
| 1305 | if (err == -ENOENT && insert) { |
| 1306 | if (item_size + extra_size >= |
| 1307 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1308 | err = -EAGAIN; |
| 1309 | goto out; |
| 1310 | } |
| 1311 | /* |
| 1312 | * To add new inline back ref, we have to make sure |
| 1313 | * there is no corresponding back ref item. |
| 1314 | * For simplicity, we just do not add new inline back |
| 1315 | * ref if there is any kind of item for this block |
| 1316 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1317 | if (find_next_key(path, 0, &key) == 0 && |
| 1318 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1319 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1320 | err = -EAGAIN; |
| 1321 | goto out; |
| 1322 | } |
| 1323 | } |
| 1324 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1325 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1326 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1327 | path->keep_locks = 0; |
| 1328 | btrfs_unlock_up_safe(path, 1); |
| 1329 | } |
| 1330 | return err; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * helper to add new inline back ref |
| 1335 | */ |
| 1336 | static noinline_for_stack |
| 1337 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1338 | struct btrfs_root *root, |
| 1339 | struct btrfs_path *path, |
| 1340 | struct btrfs_extent_inline_ref *iref, |
| 1341 | u64 parent, u64 root_objectid, |
| 1342 | u64 owner, u64 offset, int refs_to_add, |
| 1343 | struct btrfs_delayed_extent_op *extent_op) |
| 1344 | { |
| 1345 | struct extent_buffer *leaf; |
| 1346 | struct btrfs_extent_item *ei; |
| 1347 | unsigned long ptr; |
| 1348 | unsigned long end; |
| 1349 | unsigned long item_offset; |
| 1350 | u64 refs; |
| 1351 | int size; |
| 1352 | int type; |
| 1353 | int ret; |
| 1354 | |
| 1355 | leaf = path->nodes[0]; |
| 1356 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1357 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1358 | |
| 1359 | type = extent_ref_type(parent, owner); |
| 1360 | size = btrfs_extent_inline_ref_size(type); |
| 1361 | |
| 1362 | ret = btrfs_extend_item(trans, root, path, size); |
| 1363 | BUG_ON(ret); |
| 1364 | |
| 1365 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1366 | refs = btrfs_extent_refs(leaf, ei); |
| 1367 | refs += refs_to_add; |
| 1368 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1369 | if (extent_op) |
| 1370 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1371 | |
| 1372 | ptr = (unsigned long)ei + item_offset; |
| 1373 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1374 | if (ptr < end - size) |
| 1375 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1376 | end - size - ptr); |
| 1377 | |
| 1378 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1379 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1380 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1381 | struct btrfs_extent_data_ref *dref; |
| 1382 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1383 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1384 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1385 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1386 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1387 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1388 | struct btrfs_shared_data_ref *sref; |
| 1389 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1390 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1391 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1392 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1393 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1394 | } else { |
| 1395 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1396 | } |
| 1397 | btrfs_mark_buffer_dirty(leaf); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1402 | struct btrfs_root *root, |
| 1403 | struct btrfs_path *path, |
| 1404 | struct btrfs_extent_inline_ref **ref_ret, |
| 1405 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1406 | u64 root_objectid, u64 owner, u64 offset) |
| 1407 | { |
| 1408 | int ret; |
| 1409 | |
| 1410 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1411 | bytenr, num_bytes, parent, |
| 1412 | root_objectid, owner, offset, 0); |
| 1413 | if (ret != -ENOENT) |
| 1414 | return ret; |
| 1415 | |
| 1416 | btrfs_release_path(root, path); |
| 1417 | *ref_ret = NULL; |
| 1418 | |
| 1419 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1420 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1421 | root_objectid); |
| 1422 | } else { |
| 1423 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1424 | root_objectid, owner, offset); |
| 1425 | } |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * helper to update/remove inline back ref |
| 1431 | */ |
| 1432 | static noinline_for_stack |
| 1433 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1434 | struct btrfs_root *root, |
| 1435 | struct btrfs_path *path, |
| 1436 | struct btrfs_extent_inline_ref *iref, |
| 1437 | int refs_to_mod, |
| 1438 | struct btrfs_delayed_extent_op *extent_op) |
| 1439 | { |
| 1440 | struct extent_buffer *leaf; |
| 1441 | struct btrfs_extent_item *ei; |
| 1442 | struct btrfs_extent_data_ref *dref = NULL; |
| 1443 | struct btrfs_shared_data_ref *sref = NULL; |
| 1444 | unsigned long ptr; |
| 1445 | unsigned long end; |
| 1446 | u32 item_size; |
| 1447 | int size; |
| 1448 | int type; |
| 1449 | int ret; |
| 1450 | u64 refs; |
| 1451 | |
| 1452 | leaf = path->nodes[0]; |
| 1453 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1454 | refs = btrfs_extent_refs(leaf, ei); |
| 1455 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1456 | refs += refs_to_mod; |
| 1457 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1458 | if (extent_op) |
| 1459 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1460 | |
| 1461 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1462 | |
| 1463 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1464 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1465 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1466 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1467 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1468 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1469 | } else { |
| 1470 | refs = 1; |
| 1471 | BUG_ON(refs_to_mod != -1); |
| 1472 | } |
| 1473 | |
| 1474 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1475 | refs += refs_to_mod; |
| 1476 | |
| 1477 | if (refs > 0) { |
| 1478 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1479 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1480 | else |
| 1481 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1482 | } else { |
| 1483 | size = btrfs_extent_inline_ref_size(type); |
| 1484 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1485 | ptr = (unsigned long)iref; |
| 1486 | end = (unsigned long)ei + item_size; |
| 1487 | if (ptr + size < end) |
| 1488 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1489 | end - ptr - size); |
| 1490 | item_size -= size; |
| 1491 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1492 | BUG_ON(ret); |
| 1493 | } |
| 1494 | btrfs_mark_buffer_dirty(leaf); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | static noinline_for_stack |
| 1499 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1500 | struct btrfs_root *root, |
| 1501 | struct btrfs_path *path, |
| 1502 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1503 | u64 root_objectid, u64 owner, |
| 1504 | u64 offset, int refs_to_add, |
| 1505 | struct btrfs_delayed_extent_op *extent_op) |
| 1506 | { |
| 1507 | struct btrfs_extent_inline_ref *iref; |
| 1508 | int ret; |
| 1509 | |
| 1510 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1511 | bytenr, num_bytes, parent, |
| 1512 | root_objectid, owner, offset, 1); |
| 1513 | if (ret == 0) { |
| 1514 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1515 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1516 | refs_to_add, extent_op); |
| 1517 | } else if (ret == -ENOENT) { |
| 1518 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1519 | parent, root_objectid, |
| 1520 | owner, offset, refs_to_add, |
| 1521 | extent_op); |
| 1522 | } |
| 1523 | return ret; |
| 1524 | } |
| 1525 | |
| 1526 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1527 | struct btrfs_root *root, |
| 1528 | struct btrfs_path *path, |
| 1529 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1530 | u64 owner, u64 offset, int refs_to_add) |
| 1531 | { |
| 1532 | int ret; |
| 1533 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1534 | BUG_ON(refs_to_add != 1); |
| 1535 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1536 | parent, root_objectid); |
| 1537 | } else { |
| 1538 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1539 | parent, root_objectid, |
| 1540 | owner, offset, refs_to_add); |
| 1541 | } |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1546 | struct btrfs_root *root, |
| 1547 | struct btrfs_path *path, |
| 1548 | struct btrfs_extent_inline_ref *iref, |
| 1549 | int refs_to_drop, int is_data) |
| 1550 | { |
| 1551 | int ret; |
| 1552 | |
| 1553 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1554 | if (iref) { |
| 1555 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1556 | -refs_to_drop, NULL); |
| 1557 | } else if (is_data) { |
| 1558 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1559 | } else { |
| 1560 | ret = btrfs_del_item(trans, root, path); |
| 1561 | } |
| 1562 | return ret; |
| 1563 | } |
| 1564 | |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 1565 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1566 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1567 | u64 start, u64 len) |
| 1568 | { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1569 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1570 | } |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 1571 | #endif |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1572 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1573 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1574 | u64 num_bytes) |
| 1575 | { |
| 1576 | #ifdef BIO_RW_DISCARD |
| 1577 | int ret; |
| 1578 | u64 map_length = num_bytes; |
| 1579 | struct btrfs_multi_bio *multi = NULL; |
| 1580 | |
| 1581 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1582 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1583 | bytenr, &map_length, &multi, 0); |
| 1584 | if (!ret) { |
| 1585 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1586 | int i; |
| 1587 | |
| 1588 | if (map_length > num_bytes) |
| 1589 | map_length = num_bytes; |
| 1590 | |
| 1591 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1592 | btrfs_issue_discard(stripe->dev->bdev, |
| 1593 | stripe->physical, |
| 1594 | map_length); |
| 1595 | } |
| 1596 | kfree(multi); |
| 1597 | } |
| 1598 | |
| 1599 | return ret; |
| 1600 | #else |
| 1601 | return 0; |
| 1602 | #endif |
| 1603 | } |
| 1604 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1605 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1606 | struct btrfs_root *root, |
| 1607 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1608 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1609 | { |
| 1610 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1611 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1612 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1613 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1614 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1615 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1616 | parent, root_objectid, (int)owner, |
| 1617 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1618 | } else { |
| 1619 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1620 | parent, root_objectid, owner, offset, |
| 1621 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1622 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1623 | return ret; |
| 1624 | } |
| 1625 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1626 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1627 | struct btrfs_root *root, |
| 1628 | u64 bytenr, u64 num_bytes, |
| 1629 | u64 parent, u64 root_objectid, |
| 1630 | u64 owner, u64 offset, int refs_to_add, |
| 1631 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1632 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1633 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1634 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1635 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1636 | u64 refs; |
| 1637 | int ret; |
| 1638 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1639 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1640 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1641 | if (!path) |
| 1642 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1643 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1644 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1645 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1646 | /* this will setup the path even if it fails to insert the back ref */ |
| 1647 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1648 | path, bytenr, num_bytes, parent, |
| 1649 | root_objectid, owner, offset, |
| 1650 | refs_to_add, extent_op); |
| 1651 | if (ret == 0) |
| 1652 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1653 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1654 | if (ret != -EAGAIN) { |
| 1655 | err = ret; |
| 1656 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1657 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1658 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1659 | leaf = path->nodes[0]; |
| 1660 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1661 | refs = btrfs_extent_refs(leaf, item); |
| 1662 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1663 | if (extent_op) |
| 1664 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1665 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1666 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1667 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1668 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1669 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1670 | path->leave_spinning = 1; |
| 1671 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1672 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1673 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1674 | path, bytenr, parent, root_objectid, |
| 1675 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1676 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1677 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1678 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1679 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1680 | } |
| 1681 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1682 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1683 | struct btrfs_root *root, |
| 1684 | struct btrfs_delayed_ref_node *node, |
| 1685 | struct btrfs_delayed_extent_op *extent_op, |
| 1686 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1687 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1688 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1689 | struct btrfs_delayed_data_ref *ref; |
| 1690 | struct btrfs_key ins; |
| 1691 | u64 parent = 0; |
| 1692 | u64 ref_root = 0; |
| 1693 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1694 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1695 | ins.objectid = node->bytenr; |
| 1696 | ins.offset = node->num_bytes; |
| 1697 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1698 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1699 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1700 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1701 | parent = ref->parent; |
| 1702 | else |
| 1703 | ref_root = ref->root; |
| 1704 | |
| 1705 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1706 | if (extent_op) { |
| 1707 | BUG_ON(extent_op->update_key); |
| 1708 | flags |= extent_op->flags_to_set; |
| 1709 | } |
| 1710 | ret = alloc_reserved_file_extent(trans, root, |
| 1711 | parent, ref_root, flags, |
| 1712 | ref->objectid, ref->offset, |
| 1713 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1714 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1715 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1716 | node->num_bytes, parent, |
| 1717 | ref_root, ref->objectid, |
| 1718 | ref->offset, node->ref_mod, |
| 1719 | extent_op); |
| 1720 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1721 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1722 | node->num_bytes, parent, |
| 1723 | ref_root, ref->objectid, |
| 1724 | ref->offset, node->ref_mod, |
| 1725 | extent_op); |
| 1726 | } else { |
| 1727 | BUG(); |
| 1728 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1729 | return ret; |
| 1730 | } |
| 1731 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1732 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1733 | struct extent_buffer *leaf, |
| 1734 | struct btrfs_extent_item *ei) |
| 1735 | { |
| 1736 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1737 | if (extent_op->update_flags) { |
| 1738 | flags |= extent_op->flags_to_set; |
| 1739 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1740 | } |
| 1741 | |
| 1742 | if (extent_op->update_key) { |
| 1743 | struct btrfs_tree_block_info *bi; |
| 1744 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1745 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1746 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1751 | struct btrfs_root *root, |
| 1752 | struct btrfs_delayed_ref_node *node, |
| 1753 | struct btrfs_delayed_extent_op *extent_op) |
| 1754 | { |
| 1755 | struct btrfs_key key; |
| 1756 | struct btrfs_path *path; |
| 1757 | struct btrfs_extent_item *ei; |
| 1758 | struct extent_buffer *leaf; |
| 1759 | u32 item_size; |
| 1760 | int ret; |
| 1761 | int err = 0; |
| 1762 | |
| 1763 | path = btrfs_alloc_path(); |
| 1764 | if (!path) |
| 1765 | return -ENOMEM; |
| 1766 | |
| 1767 | key.objectid = node->bytenr; |
| 1768 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1769 | key.offset = node->num_bytes; |
| 1770 | |
| 1771 | path->reada = 1; |
| 1772 | path->leave_spinning = 1; |
| 1773 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1774 | path, 0, 1); |
| 1775 | if (ret < 0) { |
| 1776 | err = ret; |
| 1777 | goto out; |
| 1778 | } |
| 1779 | if (ret > 0) { |
| 1780 | err = -EIO; |
| 1781 | goto out; |
| 1782 | } |
| 1783 | |
| 1784 | leaf = path->nodes[0]; |
| 1785 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1786 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1787 | if (item_size < sizeof(*ei)) { |
| 1788 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1789 | path, (u64)-1, 0); |
| 1790 | if (ret < 0) { |
| 1791 | err = ret; |
| 1792 | goto out; |
| 1793 | } |
| 1794 | leaf = path->nodes[0]; |
| 1795 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1796 | } |
| 1797 | #endif |
| 1798 | BUG_ON(item_size < sizeof(*ei)); |
| 1799 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1800 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1801 | |
| 1802 | btrfs_mark_buffer_dirty(leaf); |
| 1803 | out: |
| 1804 | btrfs_free_path(path); |
| 1805 | return err; |
| 1806 | } |
| 1807 | |
| 1808 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1809 | struct btrfs_root *root, |
| 1810 | struct btrfs_delayed_ref_node *node, |
| 1811 | struct btrfs_delayed_extent_op *extent_op, |
| 1812 | int insert_reserved) |
| 1813 | { |
| 1814 | int ret = 0; |
| 1815 | struct btrfs_delayed_tree_ref *ref; |
| 1816 | struct btrfs_key ins; |
| 1817 | u64 parent = 0; |
| 1818 | u64 ref_root = 0; |
| 1819 | |
| 1820 | ins.objectid = node->bytenr; |
| 1821 | ins.offset = node->num_bytes; |
| 1822 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1823 | |
| 1824 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1825 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1826 | parent = ref->parent; |
| 1827 | else |
| 1828 | ref_root = ref->root; |
| 1829 | |
| 1830 | BUG_ON(node->ref_mod != 1); |
| 1831 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1832 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1833 | !extent_op->update_key); |
| 1834 | ret = alloc_reserved_tree_block(trans, root, |
| 1835 | parent, ref_root, |
| 1836 | extent_op->flags_to_set, |
| 1837 | &extent_op->key, |
| 1838 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1839 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1840 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1841 | node->num_bytes, parent, ref_root, |
| 1842 | ref->level, 0, 1, extent_op); |
| 1843 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1844 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1845 | node->num_bytes, parent, ref_root, |
| 1846 | ref->level, 0, 1, extent_op); |
| 1847 | } else { |
| 1848 | BUG(); |
| 1849 | } |
| 1850 | return ret; |
| 1851 | } |
| 1852 | |
| 1853 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1854 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1855 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1856 | struct btrfs_root *root, |
| 1857 | struct btrfs_delayed_ref_node *node, |
| 1858 | struct btrfs_delayed_extent_op *extent_op, |
| 1859 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1860 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1861 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1862 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1863 | struct btrfs_delayed_ref_head *head; |
| 1864 | /* |
| 1865 | * we've hit the end of the chain and we were supposed |
| 1866 | * to insert this extent into the tree. But, it got |
| 1867 | * deleted before we ever needed to insert it, so all |
| 1868 | * we have to do is clean up the accounting |
| 1869 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1870 | BUG_ON(extent_op); |
| 1871 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1872 | if (insert_reserved) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1873 | int mark_free = 0; |
| 1874 | struct extent_buffer *must_clean = NULL; |
| 1875 | |
| 1876 | ret = pin_down_bytes(trans, root, NULL, |
| 1877 | node->bytenr, node->num_bytes, |
| 1878 | head->is_data, 1, &must_clean); |
| 1879 | if (ret > 0) |
| 1880 | mark_free = 1; |
| 1881 | |
| 1882 | if (must_clean) { |
| 1883 | clean_tree_block(NULL, root, must_clean); |
| 1884 | btrfs_tree_unlock(must_clean); |
| 1885 | free_extent_buffer(must_clean); |
| 1886 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1887 | if (head->is_data) { |
| 1888 | ret = btrfs_del_csums(trans, root, |
| 1889 | node->bytenr, |
| 1890 | node->num_bytes); |
| 1891 | BUG_ON(ret); |
| 1892 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1893 | if (mark_free) { |
| 1894 | ret = btrfs_free_reserved_extent(root, |
| 1895 | node->bytenr, |
| 1896 | node->num_bytes); |
| 1897 | BUG_ON(ret); |
| 1898 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1899 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1900 | mutex_unlock(&head->mutex); |
| 1901 | return 0; |
| 1902 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1903 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1904 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1905 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1906 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 1907 | insert_reserved); |
| 1908 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1909 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1910 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 1911 | insert_reserved); |
| 1912 | else |
| 1913 | BUG(); |
| 1914 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1915 | } |
| 1916 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1917 | static noinline struct btrfs_delayed_ref_node * |
| 1918 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1919 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1920 | struct rb_node *node; |
| 1921 | struct btrfs_delayed_ref_node *ref; |
| 1922 | int action = BTRFS_ADD_DELAYED_REF; |
| 1923 | again: |
| 1924 | /* |
| 1925 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1926 | * this prevents ref count from going down to zero when |
| 1927 | * there still are pending delayed ref. |
| 1928 | */ |
| 1929 | node = rb_prev(&head->node.rb_node); |
| 1930 | while (1) { |
| 1931 | if (!node) |
| 1932 | break; |
| 1933 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1934 | rb_node); |
| 1935 | if (ref->bytenr != head->node.bytenr) |
| 1936 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1937 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1938 | return ref; |
| 1939 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1940 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1941 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 1942 | action = BTRFS_DROP_DELAYED_REF; |
| 1943 | goto again; |
| 1944 | } |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1948 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 1949 | struct btrfs_root *root, |
| 1950 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1951 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1952 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1953 | struct btrfs_delayed_ref_node *ref; |
| 1954 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1955 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1956 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1957 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1958 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1959 | |
| 1960 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1961 | while (1) { |
| 1962 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1963 | /* pick a new head ref from the cluster list */ |
| 1964 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1965 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1966 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1967 | locked_ref = list_entry(cluster->next, |
| 1968 | struct btrfs_delayed_ref_head, cluster); |
| 1969 | |
| 1970 | /* grab the lock that says we are going to process |
| 1971 | * all the refs for this head */ |
| 1972 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 1973 | |
| 1974 | /* |
| 1975 | * we may have dropped the spin lock to get the head |
| 1976 | * mutex lock, and that might have given someone else |
| 1977 | * time to free the head. If that's true, it has been |
| 1978 | * removed from our list and we can move on. |
| 1979 | */ |
| 1980 | if (ret == -EAGAIN) { |
| 1981 | locked_ref = NULL; |
| 1982 | count++; |
| 1983 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * record the must insert reserved flag before we |
| 1989 | * drop the spin lock. |
| 1990 | */ |
| 1991 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1992 | locked_ref->must_insert_reserved = 0; |
| 1993 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1994 | extent_op = locked_ref->extent_op; |
| 1995 | locked_ref->extent_op = NULL; |
| 1996 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1997 | /* |
| 1998 | * locked_ref is the head node, so we have to go one |
| 1999 | * node back for any delayed ref updates |
| 2000 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2001 | ref = select_delayed_ref(locked_ref); |
| 2002 | if (!ref) { |
| 2003 | /* All delayed refs have been processed, Go ahead |
| 2004 | * and send the head node to run_one_delayed_ref, |
| 2005 | * so that any accounting fixes can happen |
| 2006 | */ |
| 2007 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2008 | |
| 2009 | if (extent_op && must_insert_reserved) { |
| 2010 | kfree(extent_op); |
| 2011 | extent_op = NULL; |
| 2012 | } |
| 2013 | |
| 2014 | if (extent_op) { |
| 2015 | spin_unlock(&delayed_refs->lock); |
| 2016 | |
| 2017 | ret = run_delayed_extent_op(trans, root, |
| 2018 | ref, extent_op); |
| 2019 | BUG_ON(ret); |
| 2020 | kfree(extent_op); |
| 2021 | |
| 2022 | cond_resched(); |
| 2023 | spin_lock(&delayed_refs->lock); |
| 2024 | continue; |
| 2025 | } |
| 2026 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2027 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2028 | locked_ref = NULL; |
| 2029 | } |
| 2030 | |
| 2031 | ref->in_tree = 0; |
| 2032 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2033 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2034 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2035 | spin_unlock(&delayed_refs->lock); |
| 2036 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2037 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2038 | must_insert_reserved); |
| 2039 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2040 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2041 | btrfs_put_delayed_ref(ref); |
| 2042 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2043 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2044 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2045 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2046 | spin_lock(&delayed_refs->lock); |
| 2047 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2048 | return count; |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * this starts processing the delayed reference count updates and |
| 2053 | * extent insertions we have queued up so far. count can be |
| 2054 | * 0, which means to process everything in the tree at the start |
| 2055 | * of the run (but not newly added entries), or it can be some target |
| 2056 | * number you'd like to process. |
| 2057 | */ |
| 2058 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2059 | struct btrfs_root *root, unsigned long count) |
| 2060 | { |
| 2061 | struct rb_node *node; |
| 2062 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2063 | struct btrfs_delayed_ref_node *ref; |
| 2064 | struct list_head cluster; |
| 2065 | int ret; |
| 2066 | int run_all = count == (unsigned long)-1; |
| 2067 | int run_most = 0; |
| 2068 | |
| 2069 | if (root == root->fs_info->extent_root) |
| 2070 | root = root->fs_info->tree_root; |
| 2071 | |
| 2072 | delayed_refs = &trans->transaction->delayed_refs; |
| 2073 | INIT_LIST_HEAD(&cluster); |
| 2074 | again: |
| 2075 | spin_lock(&delayed_refs->lock); |
| 2076 | if (count == 0) { |
| 2077 | count = delayed_refs->num_entries * 2; |
| 2078 | run_most = 1; |
| 2079 | } |
| 2080 | while (1) { |
| 2081 | if (!(run_all || run_most) && |
| 2082 | delayed_refs->num_heads_ready < 64) |
| 2083 | break; |
| 2084 | |
| 2085 | /* |
| 2086 | * go find something we can process in the rbtree. We start at |
| 2087 | * the beginning of the tree, and then build a cluster |
| 2088 | * of refs to process starting at the first one we are able to |
| 2089 | * lock |
| 2090 | */ |
| 2091 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2092 | delayed_refs->run_delayed_start); |
| 2093 | if (ret) |
| 2094 | break; |
| 2095 | |
| 2096 | ret = run_clustered_refs(trans, root, &cluster); |
| 2097 | BUG_ON(ret < 0); |
| 2098 | |
| 2099 | count -= min_t(unsigned long, ret, count); |
| 2100 | |
| 2101 | if (count == 0) |
| 2102 | break; |
| 2103 | } |
| 2104 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2105 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2106 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2107 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2108 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2109 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2110 | |
| 2111 | while (node) { |
| 2112 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2113 | rb_node); |
| 2114 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2115 | struct btrfs_delayed_ref_head *head; |
| 2116 | |
| 2117 | head = btrfs_delayed_node_to_head(ref); |
| 2118 | atomic_inc(&ref->refs); |
| 2119 | |
| 2120 | spin_unlock(&delayed_refs->lock); |
| 2121 | mutex_lock(&head->mutex); |
| 2122 | mutex_unlock(&head->mutex); |
| 2123 | |
| 2124 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2125 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2126 | goto again; |
| 2127 | } |
| 2128 | node = rb_next(node); |
| 2129 | } |
| 2130 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2131 | schedule_timeout(1); |
| 2132 | goto again; |
| 2133 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2134 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2135 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2136 | return 0; |
| 2137 | } |
| 2138 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2139 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2140 | struct btrfs_root *root, |
| 2141 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2142 | int is_data) |
| 2143 | { |
| 2144 | struct btrfs_delayed_extent_op *extent_op; |
| 2145 | int ret; |
| 2146 | |
| 2147 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2148 | if (!extent_op) |
| 2149 | return -ENOMEM; |
| 2150 | |
| 2151 | extent_op->flags_to_set = flags; |
| 2152 | extent_op->update_flags = 1; |
| 2153 | extent_op->update_key = 0; |
| 2154 | extent_op->is_data = is_data ? 1 : 0; |
| 2155 | |
| 2156 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2157 | if (ret) |
| 2158 | kfree(extent_op); |
| 2159 | return ret; |
| 2160 | } |
| 2161 | |
| 2162 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2163 | struct btrfs_root *root, |
| 2164 | struct btrfs_path *path, |
| 2165 | u64 objectid, u64 offset, u64 bytenr) |
| 2166 | { |
| 2167 | struct btrfs_delayed_ref_head *head; |
| 2168 | struct btrfs_delayed_ref_node *ref; |
| 2169 | struct btrfs_delayed_data_ref *data_ref; |
| 2170 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2171 | struct rb_node *node; |
| 2172 | int ret = 0; |
| 2173 | |
| 2174 | ret = -ENOENT; |
| 2175 | delayed_refs = &trans->transaction->delayed_refs; |
| 2176 | spin_lock(&delayed_refs->lock); |
| 2177 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2178 | if (!head) |
| 2179 | goto out; |
| 2180 | |
| 2181 | if (!mutex_trylock(&head->mutex)) { |
| 2182 | atomic_inc(&head->node.refs); |
| 2183 | spin_unlock(&delayed_refs->lock); |
| 2184 | |
| 2185 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2186 | |
| 2187 | mutex_lock(&head->mutex); |
| 2188 | mutex_unlock(&head->mutex); |
| 2189 | btrfs_put_delayed_ref(&head->node); |
| 2190 | return -EAGAIN; |
| 2191 | } |
| 2192 | |
| 2193 | node = rb_prev(&head->node.rb_node); |
| 2194 | if (!node) |
| 2195 | goto out_unlock; |
| 2196 | |
| 2197 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2198 | |
| 2199 | if (ref->bytenr != bytenr) |
| 2200 | goto out_unlock; |
| 2201 | |
| 2202 | ret = 1; |
| 2203 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2204 | goto out_unlock; |
| 2205 | |
| 2206 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2207 | |
| 2208 | node = rb_prev(node); |
| 2209 | if (node) { |
| 2210 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2211 | if (ref->bytenr == bytenr) |
| 2212 | goto out_unlock; |
| 2213 | } |
| 2214 | |
| 2215 | if (data_ref->root != root->root_key.objectid || |
| 2216 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2217 | goto out_unlock; |
| 2218 | |
| 2219 | ret = 0; |
| 2220 | out_unlock: |
| 2221 | mutex_unlock(&head->mutex); |
| 2222 | out: |
| 2223 | spin_unlock(&delayed_refs->lock); |
| 2224 | return ret; |
| 2225 | } |
| 2226 | |
| 2227 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2228 | struct btrfs_root *root, |
| 2229 | struct btrfs_path *path, |
| 2230 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2231 | { |
| 2232 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2233 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2234 | struct btrfs_extent_data_ref *ref; |
| 2235 | struct btrfs_extent_inline_ref *iref; |
| 2236 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2237 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2238 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2239 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2240 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2241 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2242 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2243 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2244 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2245 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2246 | if (ret < 0) |
| 2247 | goto out; |
| 2248 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2249 | |
| 2250 | ret = -ENOENT; |
| 2251 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2252 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2253 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2254 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2255 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2256 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2257 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2258 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2259 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2260 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2261 | ret = 1; |
| 2262 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2263 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2264 | if (item_size < sizeof(*ei)) { |
| 2265 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2266 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2267 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2268 | #endif |
| 2269 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2270 | |
| 2271 | if (item_size != sizeof(*ei) + |
| 2272 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2273 | goto out; |
| 2274 | |
| 2275 | if (btrfs_extent_generation(leaf, ei) <= |
| 2276 | btrfs_root_last_snapshot(&root->root_item)) |
| 2277 | goto out; |
| 2278 | |
| 2279 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2280 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2281 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2282 | goto out; |
| 2283 | |
| 2284 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2285 | if (btrfs_extent_refs(leaf, ei) != |
| 2286 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2287 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2288 | root->root_key.objectid || |
| 2289 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2290 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2291 | goto out; |
| 2292 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2293 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2294 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2295 | return ret; |
| 2296 | } |
| 2297 | |
| 2298 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2299 | struct btrfs_root *root, |
| 2300 | u64 objectid, u64 offset, u64 bytenr) |
| 2301 | { |
| 2302 | struct btrfs_path *path; |
| 2303 | int ret; |
| 2304 | int ret2; |
| 2305 | |
| 2306 | path = btrfs_alloc_path(); |
| 2307 | if (!path) |
| 2308 | return -ENOENT; |
| 2309 | |
| 2310 | do { |
| 2311 | ret = check_committed_ref(trans, root, path, objectid, |
| 2312 | offset, bytenr); |
| 2313 | if (ret && ret != -ENOENT) |
| 2314 | goto out; |
| 2315 | |
| 2316 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2317 | offset, bytenr); |
| 2318 | } while (ret2 == -EAGAIN); |
| 2319 | |
| 2320 | if (ret2 && ret2 != -ENOENT) { |
| 2321 | ret = ret2; |
| 2322 | goto out; |
| 2323 | } |
| 2324 | |
| 2325 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2326 | ret = 0; |
| 2327 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2328 | btrfs_free_path(path); |
| 2329 | return ret; |
| 2330 | } |
| 2331 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2332 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2333 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2334 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2335 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2336 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2337 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2338 | u64 root_gen; |
| 2339 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2340 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2341 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2342 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2343 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2344 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2345 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2346 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2347 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2348 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2349 | shared = 0; |
| 2350 | root_gen = root->root_key.offset; |
| 2351 | } else { |
| 2352 | shared = 1; |
| 2353 | root_gen = trans->transid - 1; |
| 2354 | } |
| 2355 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2356 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2357 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2358 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2359 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2360 | struct btrfs_leaf_ref *ref; |
| 2361 | struct btrfs_extent_info *info; |
| 2362 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2363 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2364 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2365 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2366 | goto out; |
| 2367 | } |
| 2368 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2369 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2370 | ref->bytenr = buf->start; |
| 2371 | ref->owner = btrfs_header_owner(buf); |
| 2372 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2373 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2374 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2375 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2376 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2377 | u64 disk_bytenr; |
| 2378 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2379 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2380 | continue; |
| 2381 | fi = btrfs_item_ptr(buf, i, |
| 2382 | struct btrfs_file_extent_item); |
| 2383 | if (btrfs_file_extent_type(buf, fi) == |
| 2384 | BTRFS_FILE_EXTENT_INLINE) |
| 2385 | continue; |
| 2386 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2387 | if (disk_bytenr == 0) |
| 2388 | continue; |
| 2389 | |
| 2390 | info->bytenr = disk_bytenr; |
| 2391 | info->num_bytes = |
| 2392 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2393 | info->objectid = key.objectid; |
| 2394 | info->offset = key.offset; |
| 2395 | info++; |
| 2396 | } |
| 2397 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2398 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2399 | if (ret == -EEXIST && shared) { |
| 2400 | struct btrfs_leaf_ref *old; |
| 2401 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2402 | BUG_ON(!old); |
| 2403 | btrfs_remove_leaf_ref(root, old); |
| 2404 | btrfs_free_leaf_ref(root, old); |
| 2405 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2406 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2407 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2408 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2409 | } |
| 2410 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2411 | return ret; |
| 2412 | } |
| 2413 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2414 | /* when a block goes through cow, we update the reference counts of |
| 2415 | * everything that block points to. The internal pointers of the block |
| 2416 | * can be in just about any order, and it is likely to have clusters of |
| 2417 | * things that are close together and clusters of things that are not. |
| 2418 | * |
| 2419 | * To help reduce the seeks that come with updating all of these reference |
| 2420 | * counts, sort them by byte number before actual updates are done. |
| 2421 | * |
| 2422 | * struct refsort is used to match byte number to slot in the btree block. |
| 2423 | * we sort based on the byte number and then use the slot to actually |
| 2424 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2425 | * |
| 2426 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2427 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2428 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2429 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2430 | */ |
| 2431 | struct refsort { |
| 2432 | u64 bytenr; |
| 2433 | u32 slot; |
| 2434 | }; |
| 2435 | |
| 2436 | /* |
| 2437 | * for passing into sort() |
| 2438 | */ |
| 2439 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2440 | { |
| 2441 | const struct refsort *a = a_void; |
| 2442 | const struct refsort *b = b_void; |
| 2443 | |
| 2444 | if (a->bytenr < b->bytenr) |
| 2445 | return -1; |
| 2446 | if (a->bytenr > b->bytenr) |
| 2447 | return 1; |
| 2448 | return 0; |
| 2449 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2450 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2451 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2452 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2453 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2454 | struct extent_buffer *buf, |
| 2455 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2456 | { |
| 2457 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2458 | u64 num_bytes; |
| 2459 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2460 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2461 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2462 | struct btrfs_key key; |
| 2463 | struct btrfs_file_extent_item *fi; |
| 2464 | int i; |
| 2465 | int level; |
| 2466 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2467 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2468 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2469 | |
| 2470 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2471 | nritems = btrfs_header_nritems(buf); |
| 2472 | level = btrfs_header_level(buf); |
| 2473 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2474 | if (!root->ref_cows && level == 0) |
| 2475 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2476 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2477 | if (inc) |
| 2478 | process_func = btrfs_inc_extent_ref; |
| 2479 | else |
| 2480 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2481 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2482 | if (full_backref) |
| 2483 | parent = buf->start; |
| 2484 | else |
| 2485 | parent = 0; |
| 2486 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2487 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2488 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2489 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2490 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2491 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2492 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2493 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2494 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2495 | BTRFS_FILE_EXTENT_INLINE) |
| 2496 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2497 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2498 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2499 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2500 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2501 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2502 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2503 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2504 | parent, ref_root, key.objectid, |
| 2505 | key.offset); |
| 2506 | if (ret) |
| 2507 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2508 | } else { |
| 2509 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2510 | num_bytes = btrfs_level_size(root, level - 1); |
| 2511 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2512 | parent, ref_root, level - 1, 0); |
| 2513 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2514 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2515 | } |
| 2516 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2517 | return 0; |
| 2518 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2519 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2520 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2521 | } |
| 2522 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2523 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2524 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2525 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2526 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2527 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2528 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2529 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2530 | struct extent_buffer *buf, int full_backref) |
| 2531 | { |
| 2532 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2533 | } |
| 2534 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2535 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2536 | struct btrfs_root *root, |
| 2537 | struct btrfs_path *path, |
| 2538 | struct btrfs_block_group_cache *cache) |
| 2539 | { |
| 2540 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2541 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2542 | unsigned long bi; |
| 2543 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2544 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2545 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2546 | if (ret < 0) |
| 2547 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2548 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2549 | |
| 2550 | leaf = path->nodes[0]; |
| 2551 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2552 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2553 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2554 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2555 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2556 | if (ret) |
| 2557 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2558 | return 0; |
| 2559 | |
| 2560 | } |
| 2561 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2562 | static struct btrfs_block_group_cache * |
| 2563 | next_block_group(struct btrfs_root *root, |
| 2564 | struct btrfs_block_group_cache *cache) |
| 2565 | { |
| 2566 | struct rb_node *node; |
| 2567 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2568 | node = rb_next(&cache->cache_node); |
| 2569 | btrfs_put_block_group(cache); |
| 2570 | if (node) { |
| 2571 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2572 | cache_node); |
| 2573 | atomic_inc(&cache->count); |
| 2574 | } else |
| 2575 | cache = NULL; |
| 2576 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2577 | return cache; |
| 2578 | } |
| 2579 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2580 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2581 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2582 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2583 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2584 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2585 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2586 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2587 | |
| 2588 | path = btrfs_alloc_path(); |
| 2589 | if (!path) |
| 2590 | return -ENOMEM; |
| 2591 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2592 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2593 | if (last == 0) { |
| 2594 | err = btrfs_run_delayed_refs(trans, root, |
| 2595 | (unsigned long)-1); |
| 2596 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2597 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2598 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2599 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2600 | while (cache) { |
| 2601 | if (cache->dirty) |
| 2602 | break; |
| 2603 | cache = next_block_group(root, cache); |
| 2604 | } |
| 2605 | if (!cache) { |
| 2606 | if (last == 0) |
| 2607 | break; |
| 2608 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2609 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2610 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2611 | |
| 2612 | cache->dirty = 0; |
| 2613 | last = cache->key.objectid + cache->key.offset; |
| 2614 | |
| 2615 | err = write_one_cache_group(trans, root, path, cache); |
| 2616 | BUG_ON(err); |
| 2617 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2618 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2619 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2620 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2621 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2622 | } |
| 2623 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2624 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2625 | { |
| 2626 | struct btrfs_block_group_cache *block_group; |
| 2627 | int readonly = 0; |
| 2628 | |
| 2629 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2630 | if (!block_group || block_group->ro) |
| 2631 | readonly = 1; |
| 2632 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2633 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2634 | return readonly; |
| 2635 | } |
| 2636 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2637 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2638 | u64 total_bytes, u64 bytes_used, |
| 2639 | struct btrfs_space_info **space_info) |
| 2640 | { |
| 2641 | struct btrfs_space_info *found; |
| 2642 | |
| 2643 | found = __find_space_info(info, flags); |
| 2644 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2645 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2646 | found->total_bytes += total_bytes; |
| 2647 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2648 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2649 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2650 | *space_info = found; |
| 2651 | return 0; |
| 2652 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2653 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2654 | if (!found) |
| 2655 | return -ENOMEM; |
| 2656 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2657 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2658 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2659 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2660 | found->flags = flags; |
| 2661 | found->total_bytes = total_bytes; |
| 2662 | found->bytes_used = bytes_used; |
| 2663 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2664 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2665 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2666 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2667 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2668 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2669 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2670 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2671 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2672 | return 0; |
| 2673 | } |
| 2674 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2675 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2676 | { |
| 2677 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2678 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2679 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2680 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2681 | if (extra_flags) { |
| 2682 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2683 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2684 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2685 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2686 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2687 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2688 | } |
| 2689 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2690 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2691 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 2692 | { |
| 2693 | spin_lock(&cache->space_info->lock); |
| 2694 | spin_lock(&cache->lock); |
| 2695 | if (!cache->ro) { |
| 2696 | cache->space_info->bytes_readonly += cache->key.offset - |
| 2697 | btrfs_block_group_used(&cache->item); |
| 2698 | cache->ro = 1; |
| 2699 | } |
| 2700 | spin_unlock(&cache->lock); |
| 2701 | spin_unlock(&cache->space_info->lock); |
| 2702 | } |
| 2703 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2704 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2705 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2706 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2707 | |
| 2708 | if (num_devices == 1) |
| 2709 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2710 | if (num_devices < 4) |
| 2711 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2712 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2713 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2714 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2715 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2716 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2717 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2718 | |
| 2719 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2720 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2721 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2722 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2723 | |
| 2724 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2725 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2726 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2727 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2728 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2729 | return flags; |
| 2730 | } |
| 2731 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2732 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 2733 | { |
| 2734 | struct btrfs_fs_info *info = root->fs_info; |
| 2735 | u64 alloc_profile; |
| 2736 | |
| 2737 | if (data) { |
| 2738 | alloc_profile = info->avail_data_alloc_bits & |
| 2739 | info->data_alloc_profile; |
| 2740 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 2741 | } else if (root == root->fs_info->chunk_root) { |
| 2742 | alloc_profile = info->avail_system_alloc_bits & |
| 2743 | info->system_alloc_profile; |
| 2744 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 2745 | } else { |
| 2746 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2747 | info->metadata_alloc_profile; |
| 2748 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 2749 | } |
| 2750 | |
| 2751 | return btrfs_reduce_alloc_profile(root, data); |
| 2752 | } |
| 2753 | |
| 2754 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2755 | { |
| 2756 | u64 alloc_target; |
| 2757 | |
| 2758 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2759 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 2760 | alloc_target); |
| 2761 | } |
| 2762 | |
| 2763 | /* |
| 2764 | * for now this just makes sure we have at least 5% of our metadata space free |
| 2765 | * for use. |
| 2766 | */ |
| 2767 | int btrfs_check_metadata_free_space(struct btrfs_root *root) |
| 2768 | { |
| 2769 | struct btrfs_fs_info *info = root->fs_info; |
| 2770 | struct btrfs_space_info *meta_sinfo; |
| 2771 | u64 alloc_target, thresh; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2772 | int committed = 0, ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2773 | |
| 2774 | /* get the space info for where the metadata will live */ |
| 2775 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 2776 | meta_sinfo = __find_space_info(info, alloc_target); |
| 2777 | |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2778 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2779 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2780 | if (!meta_sinfo->full) |
| 2781 | thresh = meta_sinfo->total_bytes * 80; |
| 2782 | else |
| 2783 | thresh = meta_sinfo->total_bytes * 95; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2784 | |
| 2785 | do_div(thresh, 100); |
| 2786 | |
| 2787 | if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 2788 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2789 | struct btrfs_trans_handle *trans; |
| 2790 | if (!meta_sinfo->full) { |
| 2791 | meta_sinfo->force_alloc = 1; |
| 2792 | spin_unlock(&meta_sinfo->lock); |
| 2793 | |
| 2794 | trans = btrfs_start_transaction(root, 1); |
| 2795 | if (!trans) |
| 2796 | return -ENOMEM; |
| 2797 | |
| 2798 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2799 | 2 * 1024 * 1024, alloc_target, 0); |
| 2800 | btrfs_end_transaction(trans, root); |
| 2801 | goto again; |
| 2802 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2803 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2804 | |
| 2805 | if (!committed) { |
| 2806 | committed = 1; |
| 2807 | trans = btrfs_join_transaction(root, 1); |
| 2808 | if (!trans) |
| 2809 | return -ENOMEM; |
| 2810 | ret = btrfs_commit_transaction(trans, root); |
| 2811 | if (ret) |
| 2812 | return ret; |
| 2813 | goto again; |
| 2814 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2815 | return -ENOSPC; |
| 2816 | } |
| 2817 | spin_unlock(&meta_sinfo->lock); |
| 2818 | |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
| 2822 | /* |
| 2823 | * This will check the space that the inode allocates from to make sure we have |
| 2824 | * enough space for bytes. |
| 2825 | */ |
| 2826 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 2827 | u64 bytes) |
| 2828 | { |
| 2829 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2830 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2831 | |
| 2832 | /* make sure bytes are sectorsize aligned */ |
| 2833 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2834 | |
| 2835 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2836 | again: |
| 2837 | /* make sure we have enough space to handle the data first */ |
| 2838 | spin_lock(&data_sinfo->lock); |
| 2839 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 2840 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 2841 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
| 2842 | data_sinfo->bytes_may_use < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2843 | struct btrfs_trans_handle *trans; |
| 2844 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2845 | /* |
| 2846 | * if we don't have enough free bytes in this space then we need |
| 2847 | * to alloc a new chunk. |
| 2848 | */ |
| 2849 | if (!data_sinfo->full) { |
| 2850 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2851 | |
| 2852 | data_sinfo->force_alloc = 1; |
| 2853 | spin_unlock(&data_sinfo->lock); |
| 2854 | |
| 2855 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2856 | trans = btrfs_start_transaction(root, 1); |
| 2857 | if (!trans) |
| 2858 | return -ENOMEM; |
| 2859 | |
| 2860 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2861 | bytes + 2 * 1024 * 1024, |
| 2862 | alloc_target, 0); |
| 2863 | btrfs_end_transaction(trans, root); |
| 2864 | if (ret) |
| 2865 | return ret; |
| 2866 | goto again; |
| 2867 | } |
| 2868 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2869 | |
| 2870 | /* commit the current transaction and try again */ |
| 2871 | if (!committed) { |
| 2872 | committed = 1; |
| 2873 | trans = btrfs_join_transaction(root, 1); |
| 2874 | if (!trans) |
| 2875 | return -ENOMEM; |
| 2876 | ret = btrfs_commit_transaction(trans, root); |
| 2877 | if (ret) |
| 2878 | return ret; |
| 2879 | goto again; |
| 2880 | } |
| 2881 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2882 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 2883 | ", %llu bytes_used, %llu bytes_reserved, " |
Hu Tao | 68f5a38 | 2009-07-02 13:55:45 -0400 | [diff] [blame] | 2884 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use " |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 2885 | "%llu total\n", (unsigned long long)bytes, |
| 2886 | (unsigned long long)data_sinfo->bytes_delalloc, |
| 2887 | (unsigned long long)data_sinfo->bytes_used, |
| 2888 | (unsigned long long)data_sinfo->bytes_reserved, |
| 2889 | (unsigned long long)data_sinfo->bytes_pinned, |
| 2890 | (unsigned long long)data_sinfo->bytes_readonly, |
| 2891 | (unsigned long long)data_sinfo->bytes_may_use, |
| 2892 | (unsigned long long)data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2893 | return -ENOSPC; |
| 2894 | } |
| 2895 | data_sinfo->bytes_may_use += bytes; |
| 2896 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 2897 | spin_unlock(&data_sinfo->lock); |
| 2898 | |
| 2899 | return btrfs_check_metadata_free_space(root); |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * if there was an error for whatever reason after calling |
| 2904 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 2905 | */ |
| 2906 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 2907 | struct inode *inode, u64 bytes) |
| 2908 | { |
| 2909 | struct btrfs_space_info *data_sinfo; |
| 2910 | |
| 2911 | /* make sure bytes are sectorsize aligned */ |
| 2912 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2913 | |
| 2914 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2915 | spin_lock(&data_sinfo->lock); |
| 2916 | data_sinfo->bytes_may_use -= bytes; |
| 2917 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2918 | spin_unlock(&data_sinfo->lock); |
| 2919 | } |
| 2920 | |
| 2921 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 2922 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 2923 | u64 bytes) |
| 2924 | { |
| 2925 | struct btrfs_space_info *data_sinfo; |
| 2926 | |
| 2927 | /* get the space info for where this inode will be storing its data */ |
| 2928 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2929 | |
| 2930 | /* make sure we have enough space to handle the data first */ |
| 2931 | spin_lock(&data_sinfo->lock); |
| 2932 | data_sinfo->bytes_delalloc += bytes; |
| 2933 | |
| 2934 | /* |
| 2935 | * we are adding a delalloc extent without calling |
| 2936 | * btrfs_check_data_free_space first. This happens on a weird |
| 2937 | * writepage condition, but shouldn't hurt our accounting |
| 2938 | */ |
| 2939 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 2940 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 2941 | BTRFS_I(inode)->reserved_bytes = 0; |
| 2942 | } else { |
| 2943 | data_sinfo->bytes_may_use -= bytes; |
| 2944 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2945 | } |
| 2946 | |
| 2947 | spin_unlock(&data_sinfo->lock); |
| 2948 | } |
| 2949 | |
| 2950 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 2951 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 2952 | u64 bytes) |
| 2953 | { |
| 2954 | struct btrfs_space_info *info; |
| 2955 | |
| 2956 | info = BTRFS_I(inode)->space_info; |
| 2957 | |
| 2958 | spin_lock(&info->lock); |
| 2959 | info->bytes_delalloc -= bytes; |
| 2960 | spin_unlock(&info->lock); |
| 2961 | } |
| 2962 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2963 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 2964 | { |
| 2965 | struct list_head *head = &info->space_info; |
| 2966 | struct btrfs_space_info *found; |
| 2967 | |
| 2968 | rcu_read_lock(); |
| 2969 | list_for_each_entry_rcu(found, head, list) { |
| 2970 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2971 | found->force_alloc = 1; |
| 2972 | } |
| 2973 | rcu_read_unlock(); |
| 2974 | } |
| 2975 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2976 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 2977 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2978 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2979 | { |
| 2980 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2981 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2982 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2983 | int ret = 0; |
| 2984 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2985 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2986 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2987 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2988 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2989 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2990 | if (!space_info) { |
| 2991 | ret = update_space_info(extent_root->fs_info, flags, |
| 2992 | 0, 0, &space_info); |
| 2993 | BUG_ON(ret); |
| 2994 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2995 | BUG_ON(!space_info); |
| 2996 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2997 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2998 | if (space_info->force_alloc) { |
| 2999 | force = 1; |
| 3000 | space_info->force_alloc = 0; |
| 3001 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3002 | if (space_info->full) { |
| 3003 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3004 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3005 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3006 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3007 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 3008 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3009 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3010 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3011 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 3012 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3013 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3014 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3015 | spin_unlock(&space_info->lock); |
| 3016 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3017 | /* |
| 3018 | * if we're doing a data chunk, go ahead and make sure that |
| 3019 | * we keep a reasonable number of metadata chunks allocated in the |
| 3020 | * FS as well. |
| 3021 | */ |
| 3022 | if (flags & BTRFS_BLOCK_GROUP_DATA) { |
| 3023 | fs_info->data_chunk_allocations++; |
| 3024 | if (!(fs_info->data_chunk_allocations % |
| 3025 | fs_info->metadata_ratio)) |
| 3026 | force_metadata_allocation(fs_info); |
| 3027 | } |
| 3028 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3029 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3030 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3031 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3032 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3033 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3034 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3035 | } |
| 3036 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3037 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 3038 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3039 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3040 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3041 | { |
| 3042 | struct btrfs_block_group_cache *cache; |
| 3043 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3044 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3045 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3046 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3047 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3048 | /* block accounting for super block */ |
| 3049 | spin_lock(&info->delalloc_lock); |
| 3050 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 3051 | if (alloc) |
| 3052 | old_val += num_bytes; |
| 3053 | else |
| 3054 | old_val -= num_bytes; |
| 3055 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
| 3056 | |
| 3057 | /* block accounting for root item */ |
| 3058 | old_val = btrfs_root_used(&root->root_item); |
| 3059 | if (alloc) |
| 3060 | old_val += num_bytes; |
| 3061 | else |
| 3062 | old_val -= num_bytes; |
| 3063 | btrfs_set_root_used(&root->root_item, old_val); |
| 3064 | spin_unlock(&info->delalloc_lock); |
| 3065 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3066 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3067 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3068 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3069 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3070 | byte_in_group = bytenr - cache->key.objectid; |
| 3071 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3072 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3073 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3074 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3075 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3076 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3077 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3078 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3079 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3080 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3081 | cache->reserved -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3082 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3083 | cache->space_info->bytes_reserved -= num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 3084 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3085 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3086 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3087 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3088 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3089 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3090 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3091 | if (cache->ro) |
| 3092 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3093 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3094 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3095 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3096 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3097 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3098 | |
| 3099 | ret = btrfs_discard_extent(root, bytenr, |
| 3100 | num_bytes); |
| 3101 | WARN_ON(ret); |
| 3102 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3103 | ret = btrfs_add_free_space(cache, bytenr, |
| 3104 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3105 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3106 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3107 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3108 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3109 | total -= num_bytes; |
| 3110 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3111 | } |
| 3112 | return 0; |
| 3113 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3114 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3115 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 3116 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3117 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3118 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3119 | |
| 3120 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 3121 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3122 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3123 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3124 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3125 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3126 | |
| 3127 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3128 | } |
| 3129 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3130 | /* |
| 3131 | * this function must be called within transaction |
| 3132 | */ |
| 3133 | int btrfs_pin_extent(struct btrfs_root *root, |
| 3134 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3135 | { |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3136 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3137 | struct btrfs_block_group_cache *cache; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3138 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3139 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 3140 | BUG_ON(!cache); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3141 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3142 | spin_lock(&cache->space_info->lock); |
| 3143 | spin_lock(&cache->lock); |
| 3144 | cache->pinned += num_bytes; |
| 3145 | cache->space_info->bytes_pinned += num_bytes; |
| 3146 | if (reserved) { |
| 3147 | cache->reserved -= num_bytes; |
| 3148 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3149 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3150 | spin_unlock(&cache->lock); |
| 3151 | spin_unlock(&cache->space_info->lock); |
| 3152 | |
| 3153 | btrfs_put_block_group(cache); |
| 3154 | |
| 3155 | set_extent_dirty(fs_info->pinned_extents, |
| 3156 | bytenr, bytenr + num_bytes - 1, GFP_NOFS); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3157 | return 0; |
| 3158 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3159 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3160 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 3161 | u64 num_bytes, int reserve) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3162 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3163 | spin_lock(&cache->space_info->lock); |
| 3164 | spin_lock(&cache->lock); |
| 3165 | if (reserve) { |
| 3166 | cache->reserved += num_bytes; |
| 3167 | cache->space_info->bytes_reserved += num_bytes; |
| 3168 | } else { |
| 3169 | cache->reserved -= num_bytes; |
| 3170 | cache->space_info->bytes_reserved -= num_bytes; |
| 3171 | } |
| 3172 | spin_unlock(&cache->lock); |
| 3173 | spin_unlock(&cache->space_info->lock); |
| 3174 | return 0; |
| 3175 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3176 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3177 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 3178 | struct btrfs_root *root) |
| 3179 | { |
| 3180 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3181 | struct btrfs_caching_control *next; |
| 3182 | struct btrfs_caching_control *caching_ctl; |
| 3183 | struct btrfs_block_group_cache *cache; |
| 3184 | |
| 3185 | down_write(&fs_info->extent_commit_sem); |
| 3186 | |
| 3187 | list_for_each_entry_safe(caching_ctl, next, |
| 3188 | &fs_info->caching_block_groups, list) { |
| 3189 | cache = caching_ctl->block_group; |
| 3190 | if (block_group_cache_done(cache)) { |
| 3191 | cache->last_byte_to_unpin = (u64)-1; |
| 3192 | list_del_init(&caching_ctl->list); |
| 3193 | put_caching_control(caching_ctl); |
| 3194 | } else { |
| 3195 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3200 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 3201 | else |
| 3202 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 3203 | |
| 3204 | up_write(&fs_info->extent_commit_sem); |
| 3205 | return 0; |
| 3206 | } |
| 3207 | |
| 3208 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 3209 | { |
| 3210 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3211 | struct btrfs_block_group_cache *cache = NULL; |
| 3212 | u64 len; |
| 3213 | |
| 3214 | while (start <= end) { |
| 3215 | if (!cache || |
| 3216 | start >= cache->key.objectid + cache->key.offset) { |
| 3217 | if (cache) |
| 3218 | btrfs_put_block_group(cache); |
| 3219 | cache = btrfs_lookup_block_group(fs_info, start); |
| 3220 | BUG_ON(!cache); |
| 3221 | } |
| 3222 | |
| 3223 | len = cache->key.objectid + cache->key.offset - start; |
| 3224 | len = min(len, end + 1 - start); |
| 3225 | |
| 3226 | if (start < cache->last_byte_to_unpin) { |
| 3227 | len = min(len, cache->last_byte_to_unpin - start); |
| 3228 | btrfs_add_free_space(cache, start, len); |
| 3229 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3230 | |
| 3231 | spin_lock(&cache->space_info->lock); |
| 3232 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3233 | cache->pinned -= len; |
| 3234 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3235 | spin_unlock(&cache->lock); |
| 3236 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3237 | |
| 3238 | start += len; |
| 3239 | } |
| 3240 | |
| 3241 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3242 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3243 | return 0; |
| 3244 | } |
| 3245 | |
| 3246 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3247 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3248 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3249 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3250 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3251 | u64 start; |
| 3252 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3253 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3254 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3255 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3256 | unpin = &fs_info->freed_extents[1]; |
| 3257 | else |
| 3258 | unpin = &fs_info->freed_extents[0]; |
| 3259 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3260 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3261 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 3262 | EXTENT_DIRTY); |
| 3263 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3264 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3265 | |
| 3266 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 3267 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3268 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3269 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3270 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3271 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3272 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3273 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3274 | } |
| 3275 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3276 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 3277 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3278 | struct btrfs_path *path, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3279 | u64 bytenr, u64 num_bytes, |
| 3280 | int is_data, int reserved, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3281 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3282 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3283 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3284 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3285 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3286 | if (is_data) |
| 3287 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3288 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3289 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 3290 | if (!buf) |
| 3291 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3292 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3293 | /* we can reuse a block if it hasn't been written |
| 3294 | * and it is from this transaction. We can't |
| 3295 | * reuse anything from the tree log root because |
| 3296 | * it has tiny sub-transactions. |
| 3297 | */ |
| 3298 | if (btrfs_buffer_uptodate(buf, 0) && |
| 3299 | btrfs_try_tree_lock(buf)) { |
| 3300 | u64 header_owner = btrfs_header_owner(buf); |
| 3301 | u64 header_transid = btrfs_header_generation(buf); |
| 3302 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
| 3303 | header_transid == trans->transid && |
| 3304 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3305 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3306 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 3307 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3308 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 3309 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3310 | free_extent_buffer(buf); |
| 3311 | pinit: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3312 | if (path) |
| 3313 | btrfs_set_path_blocking(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3314 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3315 | btrfs_pin_extent(root, bytenr, num_bytes, reserved); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3316 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3317 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3318 | return 0; |
| 3319 | } |
| 3320 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3321 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 3322 | struct btrfs_root *root, |
| 3323 | u64 bytenr, u64 num_bytes, u64 parent, |
| 3324 | u64 root_objectid, u64 owner_objectid, |
| 3325 | u64 owner_offset, int refs_to_drop, |
| 3326 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3327 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3328 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3329 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3330 | struct btrfs_fs_info *info = root->fs_info; |
| 3331 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3332 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3333 | struct btrfs_extent_item *ei; |
| 3334 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3335 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3336 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3337 | int extent_slot = 0; |
| 3338 | int found_extent = 0; |
| 3339 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3340 | u32 item_size; |
| 3341 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 3342 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3343 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3344 | if (!path) |
| 3345 | return -ENOMEM; |
| 3346 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 3347 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3348 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3349 | |
| 3350 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 3351 | BUG_ON(!is_data && refs_to_drop != 1); |
| 3352 | |
| 3353 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 3354 | bytenr, num_bytes, parent, |
| 3355 | root_objectid, owner_objectid, |
| 3356 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3357 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3358 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3359 | while (extent_slot >= 0) { |
| 3360 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3361 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3362 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3363 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3364 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3365 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3366 | found_extent = 1; |
| 3367 | break; |
| 3368 | } |
| 3369 | if (path->slots[0] - extent_slot > 5) |
| 3370 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3371 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3372 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3373 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3374 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 3375 | if (found_extent && item_size < sizeof(*ei)) |
| 3376 | found_extent = 0; |
| 3377 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3378 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3379 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3380 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3381 | NULL, refs_to_drop, |
| 3382 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3383 | BUG_ON(ret); |
| 3384 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3385 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3386 | |
| 3387 | key.objectid = bytenr; |
| 3388 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3389 | key.offset = num_bytes; |
| 3390 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3391 | ret = btrfs_search_slot(trans, extent_root, |
| 3392 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3393 | if (ret) { |
| 3394 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3395 | ", was looking for %llu\n", ret, |
| 3396 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3397 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3398 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3399 | BUG_ON(ret); |
| 3400 | extent_slot = path->slots[0]; |
| 3401 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3402 | } else { |
| 3403 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3404 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3405 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3406 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3407 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3408 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3409 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3410 | (unsigned long long)owner_objectid, |
| 3411 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3412 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3413 | |
| 3414 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3415 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3416 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3417 | if (item_size < sizeof(*ei)) { |
| 3418 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 3419 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 3420 | owner_objectid, 0); |
| 3421 | BUG_ON(ret < 0); |
| 3422 | |
| 3423 | btrfs_release_path(extent_root, path); |
| 3424 | path->leave_spinning = 1; |
| 3425 | |
| 3426 | key.objectid = bytenr; |
| 3427 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3428 | key.offset = num_bytes; |
| 3429 | |
| 3430 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 3431 | -1, 1); |
| 3432 | if (ret) { |
| 3433 | printk(KERN_ERR "umm, got %d back from search" |
| 3434 | ", was looking for %llu\n", ret, |
| 3435 | (unsigned long long)bytenr); |
| 3436 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3437 | } |
| 3438 | BUG_ON(ret); |
| 3439 | extent_slot = path->slots[0]; |
| 3440 | leaf = path->nodes[0]; |
| 3441 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3442 | } |
| 3443 | #endif |
| 3444 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3445 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3446 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3447 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3448 | struct btrfs_tree_block_info *bi; |
| 3449 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3450 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3451 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3452 | } |
| 3453 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3454 | refs = btrfs_extent_refs(leaf, ei); |
| 3455 | BUG_ON(refs < refs_to_drop); |
| 3456 | refs -= refs_to_drop; |
| 3457 | |
| 3458 | if (refs > 0) { |
| 3459 | if (extent_op) |
| 3460 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3461 | /* |
| 3462 | * In the case of inline back ref, reference count will |
| 3463 | * be updated by remove_extent_backref |
| 3464 | */ |
| 3465 | if (iref) { |
| 3466 | BUG_ON(!found_extent); |
| 3467 | } else { |
| 3468 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3469 | btrfs_mark_buffer_dirty(leaf); |
| 3470 | } |
| 3471 | if (found_extent) { |
| 3472 | ret = remove_extent_backref(trans, extent_root, path, |
| 3473 | iref, refs_to_drop, |
| 3474 | is_data); |
| 3475 | BUG_ON(ret); |
| 3476 | } |
| 3477 | } else { |
| 3478 | int mark_free = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3479 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3480 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3481 | if (found_extent) { |
| 3482 | BUG_ON(is_data && refs_to_drop != |
| 3483 | extent_data_ref_count(root, path, iref)); |
| 3484 | if (iref) { |
| 3485 | BUG_ON(path->slots[0] != extent_slot); |
| 3486 | } else { |
| 3487 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3488 | path->slots[0] = extent_slot; |
| 3489 | num_to_del = 2; |
| 3490 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3491 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3492 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3493 | ret = pin_down_bytes(trans, root, path, bytenr, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3494 | num_bytes, is_data, 0, &must_clean); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3495 | if (ret > 0) |
| 3496 | mark_free = 1; |
| 3497 | BUG_ON(ret < 0); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3498 | /* |
| 3499 | * it is going to be very rare for someone to be waiting |
| 3500 | * on the block we're freeing. del_items might need to |
| 3501 | * schedule, so rather than get fancy, just force it |
| 3502 | * to blocking here |
| 3503 | */ |
| 3504 | if (must_clean) |
| 3505 | btrfs_set_lock_blocking(must_clean); |
| 3506 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3507 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3508 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3509 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3510 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3511 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3512 | if (must_clean) { |
| 3513 | clean_tree_block(NULL, root, must_clean); |
| 3514 | btrfs_tree_unlock(must_clean); |
| 3515 | free_extent_buffer(must_clean); |
| 3516 | } |
| 3517 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3518 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3519 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 3520 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 3521 | } else { |
| 3522 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 3523 | bytenr >> PAGE_CACHE_SHIFT, |
| 3524 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3525 | } |
| 3526 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 3527 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 3528 | mark_free); |
| 3529 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3530 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3531 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3532 | return ret; |
| 3533 | } |
| 3534 | |
| 3535 | /* |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3536 | * when we free an extent, it is possible (and likely) that we free the last |
| 3537 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3538 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3539 | * removes it from the tree. |
| 3540 | */ |
| 3541 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 3542 | struct btrfs_root *root, u64 bytenr) |
| 3543 | { |
| 3544 | struct btrfs_delayed_ref_head *head; |
| 3545 | struct btrfs_delayed_ref_root *delayed_refs; |
| 3546 | struct btrfs_delayed_ref_node *ref; |
| 3547 | struct rb_node *node; |
| 3548 | int ret; |
| 3549 | |
| 3550 | delayed_refs = &trans->transaction->delayed_refs; |
| 3551 | spin_lock(&delayed_refs->lock); |
| 3552 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 3553 | if (!head) |
| 3554 | goto out; |
| 3555 | |
| 3556 | node = rb_prev(&head->node.rb_node); |
| 3557 | if (!node) |
| 3558 | goto out; |
| 3559 | |
| 3560 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 3561 | |
| 3562 | /* there are still entries for this ref, we can't drop it */ |
| 3563 | if (ref->bytenr == bytenr) |
| 3564 | goto out; |
| 3565 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3566 | if (head->extent_op) { |
| 3567 | if (!head->must_insert_reserved) |
| 3568 | goto out; |
| 3569 | kfree(head->extent_op); |
| 3570 | head->extent_op = NULL; |
| 3571 | } |
| 3572 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3573 | /* |
| 3574 | * waiting for the lock here would deadlock. If someone else has it |
| 3575 | * locked they are already in the process of dropping it anyway |
| 3576 | */ |
| 3577 | if (!mutex_trylock(&head->mutex)) |
| 3578 | goto out; |
| 3579 | |
| 3580 | /* |
| 3581 | * at this point we have a head with no other entries. Go |
| 3582 | * ahead and process it. |
| 3583 | */ |
| 3584 | head->node.in_tree = 0; |
| 3585 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3586 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3587 | delayed_refs->num_entries--; |
| 3588 | |
| 3589 | /* |
| 3590 | * we don't take a ref on the node because we're removing it from the |
| 3591 | * tree, so we just steal the ref the tree was holding. |
| 3592 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3593 | delayed_refs->num_heads--; |
| 3594 | if (list_empty(&head->cluster)) |
| 3595 | delayed_refs->num_heads_ready--; |
| 3596 | |
| 3597 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3598 | spin_unlock(&delayed_refs->lock); |
| 3599 | |
| 3600 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3601 | &head->node, head->extent_op, |
| 3602 | head->must_insert_reserved); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3603 | BUG_ON(ret); |
| 3604 | btrfs_put_delayed_ref(&head->node); |
| 3605 | return 0; |
| 3606 | out: |
| 3607 | spin_unlock(&delayed_refs->lock); |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3611 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3612 | struct btrfs_root *root, |
| 3613 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3614 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3615 | { |
| 3616 | int ret; |
| 3617 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3618 | /* |
| 3619 | * tree log blocks never actually go into the extent allocation |
| 3620 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3621 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3622 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3623 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3624 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3625 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3626 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3627 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 3628 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 3629 | parent, root_objectid, (int)owner, |
| 3630 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3631 | BUG_ON(ret); |
| 3632 | ret = check_ref_cleanup(trans, root, bytenr); |
| 3633 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3634 | } else { |
| 3635 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 3636 | parent, root_objectid, owner, |
| 3637 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 3638 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3639 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3640 | return ret; |
| 3641 | } |
| 3642 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 3643 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 3644 | { |
| 3645 | u64 mask = ((u64)root->stripesize - 1); |
| 3646 | u64 ret = (val + mask) & ~mask; |
| 3647 | return ret; |
| 3648 | } |
| 3649 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3650 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3651 | * when we wait for progress in the block group caching, its because |
| 3652 | * our allocation attempt failed at least once. So, we must sleep |
| 3653 | * and let some progress happen before we try again. |
| 3654 | * |
| 3655 | * This function will sleep at least once waiting for new free space to |
| 3656 | * show up, and then it will check the block group free space numbers |
| 3657 | * for our min num_bytes. Another option is to have it go ahead |
| 3658 | * and look in the rbtree for a free extent of a given size, but this |
| 3659 | * is a good start. |
| 3660 | */ |
| 3661 | static noinline int |
| 3662 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 3663 | u64 num_bytes) |
| 3664 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3665 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3666 | DEFINE_WAIT(wait); |
| 3667 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3668 | caching_ctl = get_caching_control(cache); |
| 3669 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3670 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3671 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3672 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3673 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3674 | |
| 3675 | put_caching_control(caching_ctl); |
| 3676 | return 0; |
| 3677 | } |
| 3678 | |
| 3679 | static noinline int |
| 3680 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 3681 | { |
| 3682 | struct btrfs_caching_control *caching_ctl; |
| 3683 | DEFINE_WAIT(wait); |
| 3684 | |
| 3685 | caching_ctl = get_caching_control(cache); |
| 3686 | if (!caching_ctl) |
| 3687 | return 0; |
| 3688 | |
| 3689 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 3690 | |
| 3691 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3692 | return 0; |
| 3693 | } |
| 3694 | |
| 3695 | enum btrfs_loop_type { |
| 3696 | LOOP_CACHED_ONLY = 0, |
| 3697 | LOOP_CACHING_NOWAIT = 1, |
| 3698 | LOOP_CACHING_WAIT = 2, |
| 3699 | LOOP_ALLOC_CHUNK = 3, |
| 3700 | LOOP_NO_EMPTY_SIZE = 4, |
| 3701 | }; |
| 3702 | |
| 3703 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3704 | * walks the btree of allocated extents and find a hole of a given size. |
| 3705 | * The key ins is changed to record the hole: |
| 3706 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3707 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3708 | * ins->offset == number of blocks |
| 3709 | * Any available blocks before search_start are skipped. |
| 3710 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3711 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3712 | struct btrfs_root *orig_root, |
| 3713 | u64 num_bytes, u64 empty_size, |
| 3714 | u64 search_start, u64 search_end, |
| 3715 | u64 hint_byte, struct btrfs_key *ins, |
| 3716 | u64 exclude_start, u64 exclude_nr, |
| 3717 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3718 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3719 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3720 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3721 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3722 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3723 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3724 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3725 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3726 | int last_ptr_loop = 0; |
| 3727 | int loop = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3728 | bool found_uncached_bg = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3729 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3730 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3731 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3732 | ins->objectid = 0; |
| 3733 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3734 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3735 | space_info = __find_space_info(root->fs_info, data); |
| 3736 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3737 | if (orig_root->ref_cows || empty_size) |
| 3738 | allowed_chunk_alloc = 1; |
| 3739 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3740 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3741 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 3742 | if (!btrfs_test_opt(root, SSD)) |
| 3743 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3744 | } |
| 3745 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3746 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 3747 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 3748 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3749 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3750 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3751 | spin_lock(&last_ptr->lock); |
| 3752 | if (last_ptr->block_group) |
| 3753 | hint_byte = last_ptr->window_start; |
| 3754 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3755 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3756 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3757 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3758 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3759 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3760 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3761 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3762 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3763 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3764 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 3765 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3766 | /* |
| 3767 | * we don't want to use the block group if it doesn't match our |
| 3768 | * allocation bits, or if its not cached. |
| 3769 | */ |
| 3770 | if (block_group && block_group_bits(block_group, data) && |
| 3771 | block_group_cache_done(block_group)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3772 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3773 | if (list_empty(&block_group->list) || |
| 3774 | block_group->ro) { |
| 3775 | /* |
| 3776 | * someone is removing this block group, |
| 3777 | * we can't jump into the have_block_group |
| 3778 | * target because our list pointers are not |
| 3779 | * valid |
| 3780 | */ |
| 3781 | btrfs_put_block_group(block_group); |
| 3782 | up_read(&space_info->groups_sem); |
| 3783 | } else |
| 3784 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3785 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3786 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3787 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3788 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3789 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3790 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3791 | down_read(&space_info->groups_sem); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3792 | list_for_each_entry(block_group, &space_info->block_groups, list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3793 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3794 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 3795 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3796 | atomic_inc(&block_group->count); |
| 3797 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3798 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3799 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3800 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
| 3801 | /* |
| 3802 | * we want to start caching kthreads, but not too many |
| 3803 | * right off the bat so we don't overwhelm the system, |
| 3804 | * so only start them if there are less than 2 and we're |
| 3805 | * in the initial allocation phase. |
| 3806 | */ |
| 3807 | if (loop > LOOP_CACHING_NOWAIT || |
| 3808 | atomic_read(&space_info->caching_threads) < 2) { |
| 3809 | ret = cache_block_group(block_group); |
| 3810 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3811 | } |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3812 | } |
| 3813 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3814 | cached = block_group_cache_done(block_group); |
| 3815 | if (unlikely(!cached)) { |
| 3816 | found_uncached_bg = true; |
| 3817 | |
| 3818 | /* if we only want cached bgs, loop */ |
| 3819 | if (loop == LOOP_CACHED_ONLY) |
| 3820 | goto loop; |
| 3821 | } |
| 3822 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3823 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3824 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3825 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3826 | if (last_ptr) { |
| 3827 | /* |
| 3828 | * the refill lock keeps out other |
| 3829 | * people trying to start a new cluster |
| 3830 | */ |
| 3831 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3832 | if (last_ptr->block_group && |
| 3833 | (last_ptr->block_group->ro || |
| 3834 | !block_group_bits(last_ptr->block_group, data))) { |
| 3835 | offset = 0; |
| 3836 | goto refill_cluster; |
| 3837 | } |
| 3838 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3839 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 3840 | num_bytes, search_start); |
| 3841 | if (offset) { |
| 3842 | /* we have a block, we're done */ |
| 3843 | spin_unlock(&last_ptr->refill_lock); |
| 3844 | goto checks; |
| 3845 | } |
| 3846 | |
| 3847 | spin_lock(&last_ptr->lock); |
| 3848 | /* |
| 3849 | * whoops, this cluster doesn't actually point to |
| 3850 | * this block group. Get a ref on the block |
| 3851 | * group is does point to and try again |
| 3852 | */ |
| 3853 | if (!last_ptr_loop && last_ptr->block_group && |
| 3854 | last_ptr->block_group != block_group) { |
| 3855 | |
| 3856 | btrfs_put_block_group(block_group); |
| 3857 | block_group = last_ptr->block_group; |
| 3858 | atomic_inc(&block_group->count); |
| 3859 | spin_unlock(&last_ptr->lock); |
| 3860 | spin_unlock(&last_ptr->refill_lock); |
| 3861 | |
| 3862 | last_ptr_loop = 1; |
| 3863 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3864 | /* |
| 3865 | * we know this block group is properly |
| 3866 | * in the list because |
| 3867 | * btrfs_remove_block_group, drops the |
| 3868 | * cluster before it removes the block |
| 3869 | * group from the list |
| 3870 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3871 | goto have_block_group; |
| 3872 | } |
| 3873 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3874 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3875 | /* |
| 3876 | * this cluster didn't work out, free it and |
| 3877 | * start over |
| 3878 | */ |
| 3879 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3880 | |
| 3881 | last_ptr_loop = 0; |
| 3882 | |
| 3883 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 3884 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3885 | block_group, last_ptr, |
| 3886 | offset, num_bytes, |
| 3887 | empty_cluster + empty_size); |
| 3888 | if (ret == 0) { |
| 3889 | /* |
| 3890 | * now pull our allocation out of this |
| 3891 | * cluster |
| 3892 | */ |
| 3893 | offset = btrfs_alloc_from_cluster(block_group, |
| 3894 | last_ptr, num_bytes, |
| 3895 | search_start); |
| 3896 | if (offset) { |
| 3897 | /* we found one, proceed */ |
| 3898 | spin_unlock(&last_ptr->refill_lock); |
| 3899 | goto checks; |
| 3900 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3901 | } else if (!cached && loop > LOOP_CACHING_NOWAIT) { |
| 3902 | spin_unlock(&last_ptr->refill_lock); |
| 3903 | |
| 3904 | wait_block_group_cache_progress(block_group, |
| 3905 | num_bytes + empty_cluster + empty_size); |
| 3906 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3907 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3908 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3909 | /* |
| 3910 | * at this point we either didn't find a cluster |
| 3911 | * or we weren't able to allocate a block from our |
| 3912 | * cluster. Free the cluster we've been trying |
| 3913 | * to use, and go to the next block group |
| 3914 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3915 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3916 | btrfs_return_cluster_to_free_space(NULL, |
| 3917 | last_ptr); |
| 3918 | spin_unlock(&last_ptr->refill_lock); |
| 3919 | goto loop; |
| 3920 | } |
| 3921 | spin_unlock(&last_ptr->refill_lock); |
| 3922 | } |
| 3923 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3924 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 3925 | num_bytes, empty_size); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3926 | if (!offset && (cached || (!cached && |
| 3927 | loop == LOOP_CACHING_NOWAIT))) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3928 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3929 | } else if (!offset && (!cached && |
| 3930 | loop > LOOP_CACHING_NOWAIT)) { |
| 3931 | wait_block_group_cache_progress(block_group, |
| 3932 | num_bytes + empty_size); |
| 3933 | goto have_block_group; |
| 3934 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3935 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3936 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3937 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3938 | if (search_start + num_bytes >= search_end) { |
| 3939 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3940 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3941 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3942 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3943 | /* move on to the next group */ |
| 3944 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3945 | block_group->key.objectid + block_group->key.offset) { |
| 3946 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3947 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3948 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3949 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3950 | if (exclude_nr > 0 && |
| 3951 | (search_start + num_bytes > exclude_start && |
| 3952 | search_start < exclude_start + exclude_nr)) { |
| 3953 | search_start = exclude_start + exclude_nr; |
| 3954 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3955 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3956 | /* |
| 3957 | * if search_start is still in this block group |
| 3958 | * then we just re-search this block group |
| 3959 | */ |
| 3960 | if (search_start >= block_group->key.objectid && |
| 3961 | search_start < (block_group->key.objectid + |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3962 | block_group->key.offset)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3963 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3964 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3965 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3966 | |
| 3967 | ins->objectid = search_start; |
| 3968 | ins->offset = num_bytes; |
| 3969 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3970 | if (offset < search_start) |
| 3971 | btrfs_add_free_space(block_group, offset, |
| 3972 | search_start - offset); |
| 3973 | BUG_ON(offset > search_start); |
| 3974 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3975 | update_reserved_extents(block_group, num_bytes, 1); |
| 3976 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3977 | /* we are all good, lets return */ |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3978 | break; |
| 3979 | loop: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3980 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3981 | } |
| 3982 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3983 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3984 | /* LOOP_CACHED_ONLY, only search fully cached block groups |
| 3985 | * LOOP_CACHING_NOWAIT, search partially cached block groups, but |
| 3986 | * dont wait foR them to finish caching |
| 3987 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 3988 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 3989 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 3990 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3991 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3992 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 3993 | (found_uncached_bg || empty_size || empty_cluster || |
| 3994 | allowed_chunk_alloc)) { |
| 3995 | if (found_uncached_bg) { |
| 3996 | found_uncached_bg = false; |
| 3997 | if (loop < LOOP_CACHING_WAIT) { |
| 3998 | loop++; |
| 3999 | goto search; |
| 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4004 | empty_size = 0; |
| 4005 | empty_cluster = 0; |
| 4006 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4007 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4008 | if (allowed_chunk_alloc) { |
| 4009 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 4010 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4011 | allowed_chunk_alloc = 0; |
| 4012 | } else { |
| 4013 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4014 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4015 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4016 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4017 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4018 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4019 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4020 | ret = -ENOSPC; |
| 4021 | } else if (!ins->objectid) { |
| 4022 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 4023 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4024 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4025 | /* we found what we needed */ |
| 4026 | if (ins->objectid) { |
| 4027 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4028 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4029 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4030 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4031 | ret = 0; |
| 4032 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4033 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4034 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4035 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4036 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4037 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 4038 | { |
| 4039 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4040 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4041 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 4042 | (unsigned long long)(info->total_bytes - info->bytes_used - |
| 4043 | info->bytes_pinned - info->bytes_reserved), |
| 4044 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4045 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 4046 | " may_use=%llu, used=%llu\n", |
| 4047 | (unsigned long long)info->total_bytes, |
| 4048 | (unsigned long long)info->bytes_pinned, |
| 4049 | (unsigned long long)info->bytes_delalloc, |
| 4050 | (unsigned long long)info->bytes_may_use, |
| 4051 | (unsigned long long)info->bytes_used); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4052 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4053 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 4054 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4055 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4056 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 4057 | "%llu pinned %llu reserved\n", |
| 4058 | (unsigned long long)cache->key.objectid, |
| 4059 | (unsigned long long)cache->key.offset, |
| 4060 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 4061 | (unsigned long long)cache->pinned, |
| 4062 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4063 | btrfs_dump_free_space(cache, bytes); |
| 4064 | spin_unlock(&cache->lock); |
| 4065 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4066 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4067 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4068 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4069 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 4070 | struct btrfs_root *root, |
| 4071 | u64 num_bytes, u64 min_alloc_size, |
| 4072 | u64 empty_size, u64 hint_byte, |
| 4073 | u64 search_end, struct btrfs_key *ins, |
| 4074 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4075 | { |
| 4076 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 4077 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4078 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4079 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4080 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4081 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4082 | /* |
| 4083 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 4084 | * is not called recursively on allocations |
| 4085 | */ |
| 4086 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4087 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4088 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4089 | 2 * 1024 * 1024, |
| 4090 | BTRFS_BLOCK_GROUP_METADATA | |
| 4091 | (info->metadata_alloc_profile & |
| 4092 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4093 | } |
| 4094 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4095 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4096 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4097 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4098 | WARN_ON(num_bytes < root->sectorsize); |
| 4099 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 4100 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 4101 | trans->alloc_exclude_start, |
| 4102 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4103 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4104 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 4105 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4106 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4107 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4108 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 4109 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4110 | goto again; |
| 4111 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4112 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4113 | struct btrfs_space_info *sinfo; |
| 4114 | |
| 4115 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4116 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 4117 | "wanted %llu\n", (unsigned long long)data, |
| 4118 | (unsigned long long)num_bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4119 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4120 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4121 | |
| 4122 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4123 | } |
| 4124 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4125 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 4126 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4127 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4128 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4129 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4130 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 4131 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4132 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 4133 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4134 | return -ENOSPC; |
| 4135 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4136 | |
| 4137 | ret = btrfs_discard_extent(root, start, len); |
| 4138 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4139 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4140 | update_reserved_extents(cache, len, 0); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4141 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4142 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4143 | return ret; |
| 4144 | } |
| 4145 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4146 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4147 | struct btrfs_root *root, |
| 4148 | u64 parent, u64 root_objectid, |
| 4149 | u64 flags, u64 owner, u64 offset, |
| 4150 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4151 | { |
| 4152 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4153 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4154 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4155 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4156 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4157 | struct extent_buffer *leaf; |
| 4158 | int type; |
| 4159 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4160 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4161 | if (parent > 0) |
| 4162 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4163 | else |
| 4164 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4165 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4166 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4167 | |
| 4168 | path = btrfs_alloc_path(); |
| 4169 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4170 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4171 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4172 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4173 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4174 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4175 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4176 | leaf = path->nodes[0]; |
| 4177 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4178 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4179 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4180 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4181 | btrfs_set_extent_flags(leaf, extent_item, |
| 4182 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4183 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4184 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4185 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4186 | if (parent > 0) { |
| 4187 | struct btrfs_shared_data_ref *ref; |
| 4188 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4189 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4190 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4191 | } else { |
| 4192 | struct btrfs_extent_data_ref *ref; |
| 4193 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4194 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4195 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4196 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4197 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4198 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4199 | |
| 4200 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4201 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4202 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4203 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4204 | 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4205 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4206 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4207 | "%llu\n", (unsigned long long)ins->objectid, |
| 4208 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4209 | BUG(); |
| 4210 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4211 | return ret; |
| 4212 | } |
| 4213 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4214 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 4215 | struct btrfs_root *root, |
| 4216 | u64 parent, u64 root_objectid, |
| 4217 | u64 flags, struct btrfs_disk_key *key, |
| 4218 | int level, struct btrfs_key *ins) |
| 4219 | { |
| 4220 | int ret; |
| 4221 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4222 | struct btrfs_extent_item *extent_item; |
| 4223 | struct btrfs_tree_block_info *block_info; |
| 4224 | struct btrfs_extent_inline_ref *iref; |
| 4225 | struct btrfs_path *path; |
| 4226 | struct extent_buffer *leaf; |
| 4227 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 4228 | |
| 4229 | path = btrfs_alloc_path(); |
| 4230 | BUG_ON(!path); |
| 4231 | |
| 4232 | path->leave_spinning = 1; |
| 4233 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4234 | ins, size); |
| 4235 | BUG_ON(ret); |
| 4236 | |
| 4237 | leaf = path->nodes[0]; |
| 4238 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4239 | struct btrfs_extent_item); |
| 4240 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4241 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4242 | btrfs_set_extent_flags(leaf, extent_item, |
| 4243 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 4244 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 4245 | |
| 4246 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 4247 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 4248 | |
| 4249 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4250 | if (parent > 0) { |
| 4251 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4252 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4253 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 4254 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4255 | } else { |
| 4256 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4257 | BTRFS_TREE_BLOCK_REF_KEY); |
| 4258 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 4259 | } |
| 4260 | |
| 4261 | btrfs_mark_buffer_dirty(leaf); |
| 4262 | btrfs_free_path(path); |
| 4263 | |
| 4264 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4265 | 1, 0); |
| 4266 | if (ret) { |
| 4267 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4268 | "%llu\n", (unsigned long long)ins->objectid, |
| 4269 | (unsigned long long)ins->offset); |
| 4270 | BUG(); |
| 4271 | } |
| 4272 | return ret; |
| 4273 | } |
| 4274 | |
| 4275 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4276 | struct btrfs_root *root, |
| 4277 | u64 root_objectid, u64 owner, |
| 4278 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4279 | { |
| 4280 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4281 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4282 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4283 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4284 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 4285 | 0, root_objectid, owner, offset, |
| 4286 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4287 | return ret; |
| 4288 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4289 | |
| 4290 | /* |
| 4291 | * this is used by the tree logging recovery code. It records that |
| 4292 | * an extent has been allocated and makes sure to clear the free |
| 4293 | * space cache bits as well |
| 4294 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4295 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 4296 | struct btrfs_root *root, |
| 4297 | u64 root_objectid, u64 owner, u64 offset, |
| 4298 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4299 | { |
| 4300 | int ret; |
| 4301 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4302 | struct btrfs_caching_control *caching_ctl; |
| 4303 | u64 start = ins->objectid; |
| 4304 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4305 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4306 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4307 | cache_block_group(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4308 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4309 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4310 | if (!caching_ctl) { |
| 4311 | BUG_ON(!block_group_cache_done(block_group)); |
| 4312 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 4313 | BUG_ON(ret); |
| 4314 | } else { |
| 4315 | mutex_lock(&caching_ctl->mutex); |
| 4316 | |
| 4317 | if (start >= caching_ctl->progress) { |
| 4318 | ret = add_excluded_extent(root, start, num_bytes); |
| 4319 | BUG_ON(ret); |
| 4320 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 4321 | ret = btrfs_remove_free_space(block_group, |
| 4322 | start, num_bytes); |
| 4323 | BUG_ON(ret); |
| 4324 | } else { |
| 4325 | num_bytes = caching_ctl->progress - start; |
| 4326 | ret = btrfs_remove_free_space(block_group, |
| 4327 | start, num_bytes); |
| 4328 | BUG_ON(ret); |
| 4329 | |
| 4330 | start = caching_ctl->progress; |
| 4331 | num_bytes = ins->objectid + ins->offset - |
| 4332 | caching_ctl->progress; |
| 4333 | ret = add_excluded_extent(root, start, num_bytes); |
| 4334 | BUG_ON(ret); |
| 4335 | } |
| 4336 | |
| 4337 | mutex_unlock(&caching_ctl->mutex); |
| 4338 | put_caching_control(caching_ctl); |
| 4339 | } |
| 4340 | |
| 4341 | update_reserved_extents(block_group, ins->offset, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4342 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4343 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 4344 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4345 | return ret; |
| 4346 | } |
| 4347 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4348 | /* |
| 4349 | * finds a free extent and does all the dirty work required for allocation |
| 4350 | * returns the key for the extent through ins, and a tree buffer for |
| 4351 | * the first block of the extent through buf. |
| 4352 | * |
| 4353 | * returns 0 if everything worked, non-zero otherwise. |
| 4354 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4355 | static int alloc_tree_block(struct btrfs_trans_handle *trans, |
| 4356 | struct btrfs_root *root, |
| 4357 | u64 num_bytes, u64 parent, u64 root_objectid, |
| 4358 | struct btrfs_disk_key *key, int level, |
| 4359 | u64 empty_size, u64 hint_byte, u64 search_end, |
| 4360 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4361 | { |
| 4362 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4363 | u64 flags = 0; |
| 4364 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4365 | ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes, |
| 4366 | empty_size, hint_byte, search_end, |
| 4367 | ins, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4368 | if (ret) |
| 4369 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4370 | |
| 4371 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4372 | if (parent == 0) |
| 4373 | parent = ins->objectid; |
| 4374 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4375 | } else |
| 4376 | BUG_ON(parent > 0); |
| 4377 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4378 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4379 | struct btrfs_delayed_extent_op *extent_op; |
| 4380 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 4381 | BUG_ON(!extent_op); |
| 4382 | if (key) |
| 4383 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4384 | else |
| 4385 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4386 | extent_op->flags_to_set = flags; |
| 4387 | extent_op->update_key = 1; |
| 4388 | extent_op->update_flags = 1; |
| 4389 | extent_op->is_data = 0; |
| 4390 | |
| 4391 | ret = btrfs_add_delayed_tree_ref(trans, ins->objectid, |
| 4392 | ins->offset, parent, root_objectid, |
| 4393 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 4394 | extent_op); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4395 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4396 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4397 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4398 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4399 | |
| 4400 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 4401 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4402 | u64 bytenr, u32 blocksize, |
| 4403 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4404 | { |
| 4405 | struct extent_buffer *buf; |
| 4406 | |
| 4407 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 4408 | if (!buf) |
| 4409 | return ERR_PTR(-ENOMEM); |
| 4410 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4411 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4412 | btrfs_tree_lock(buf); |
| 4413 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4414 | |
| 4415 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4416 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4417 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4418 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4419 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4420 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4421 | } else { |
| 4422 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4423 | buf->start + buf->len - 1, GFP_NOFS); |
| 4424 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4425 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4426 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4427 | return buf; |
| 4428 | } |
| 4429 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4430 | /* |
| 4431 | * helper function to allocate a block for a given tree |
| 4432 | * returns the tree buffer or NULL. |
| 4433 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4434 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4435 | struct btrfs_root *root, u32 blocksize, |
| 4436 | u64 parent, u64 root_objectid, |
| 4437 | struct btrfs_disk_key *key, int level, |
| 4438 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4439 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4440 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4441 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4442 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4443 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4444 | ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid, |
| 4445 | key, level, empty_size, hint, (u64)-1, &ins); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4446 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4447 | BUG_ON(ret > 0); |
| 4448 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4449 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4450 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4451 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 4452 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4453 | return buf; |
| 4454 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4455 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4456 | #if 0 |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4457 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 4458 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4459 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4460 | u64 disk_bytenr; |
| 4461 | u64 num_bytes; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4462 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4463 | struct btrfs_file_extent_item *fi; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4464 | u32 nritems; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4465 | int i; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4466 | int ret; |
| 4467 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4468 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 4469 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4470 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4471 | for (i = 0; i < nritems; i++) { |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 4472 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4473 | btrfs_item_key_to_cpu(leaf, &key, i); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4474 | |
| 4475 | /* only extents have references, skip everything else */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4476 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4477 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4478 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4479 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4480 | |
| 4481 | /* inline extents live in the btree, they don't have refs */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4482 | if (btrfs_file_extent_type(leaf, fi) == |
| 4483 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 4484 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4485 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4486 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4487 | |
| 4488 | /* holes don't have refs */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4489 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 4490 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 4491 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4492 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4493 | ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes, |
| 4494 | leaf->start, 0, key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4495 | BUG_ON(ret); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4496 | } |
| 4497 | return 0; |
| 4498 | } |
| 4499 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4500 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4501 | struct btrfs_root *root, |
| 4502 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4503 | { |
| 4504 | int i; |
| 4505 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4506 | struct btrfs_extent_info *info; |
| 4507 | struct refsort *sorted; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4508 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4509 | if (ref->nritems == 0) |
| 4510 | return 0; |
| 4511 | |
| 4512 | sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4513 | for (i = 0; i < ref->nritems; i++) { |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4514 | sorted[i].bytenr = ref->extents[i].bytenr; |
| 4515 | sorted[i].slot = i; |
| 4516 | } |
| 4517 | sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL); |
| 4518 | |
| 4519 | /* |
| 4520 | * the items in the ref were sorted when the ref was inserted |
| 4521 | * into the ref cache, so this is already in order |
| 4522 | */ |
| 4523 | for (i = 0; i < ref->nritems; i++) { |
| 4524 | info = ref->extents + sorted[i].slot; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4525 | ret = btrfs_free_extent(trans, root, info->bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4526 | info->num_bytes, ref->bytenr, |
| 4527 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4528 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4529 | |
| 4530 | atomic_inc(&root->fs_info->throttle_gen); |
| 4531 | wake_up(&root->fs_info->transaction_throttle); |
| 4532 | cond_resched(); |
| 4533 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4534 | BUG_ON(ret); |
| 4535 | info++; |
| 4536 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4537 | |
Chris Mason | 806638b | 2009-02-05 09:08:14 -0500 | [diff] [blame] | 4538 | kfree(sorted); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4539 | return 0; |
| 4540 | } |
| 4541 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4542 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4543 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, |
| 4544 | struct btrfs_root *root, u64 start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4545 | u64 len, u32 *refs) |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4546 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 4547 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4549 | ret = btrfs_lookup_extent_refs(trans, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4550 | BUG_ON(ret); |
| 4551 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4552 | #if 0 /* some debugging code in case we see problems here */ |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4553 | /* if the refs count is one, it won't get increased again. But |
| 4554 | * if the ref count is > 1, someone may be decreasing it at |
| 4555 | * the same time we are. |
| 4556 | */ |
| 4557 | if (*refs != 1) { |
| 4558 | struct extent_buffer *eb = NULL; |
| 4559 | eb = btrfs_find_create_tree_block(root, start, len); |
| 4560 | if (eb) |
| 4561 | btrfs_tree_lock(eb); |
| 4562 | |
| 4563 | mutex_lock(&root->fs_info->alloc_mutex); |
| 4564 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 4565 | BUG_ON(ret); |
| 4566 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 4567 | |
| 4568 | if (eb) { |
| 4569 | btrfs_tree_unlock(eb); |
| 4570 | free_extent_buffer(eb); |
| 4571 | } |
| 4572 | if (*refs == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4573 | printk(KERN_ERR "btrfs block %llu went down to one " |
| 4574 | "during drop_snap\n", (unsigned long long)start); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | } |
| 4578 | #endif |
| 4579 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4580 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 4581 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4582 | } |
| 4583 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4584 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4585 | /* |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4586 | * this is used while deleting old snapshots, and it drops the refs |
| 4587 | * on a whole subtree starting from a level 1 node. |
| 4588 | * |
| 4589 | * The idea is to sort all the leaf pointers, and then drop the |
| 4590 | * ref on all the leaves in order. Most of the time the leaves |
| 4591 | * will have ref cache entries, so no leaf IOs will be required to |
| 4592 | * find the extents they have references on. |
| 4593 | * |
| 4594 | * For each leaf, any references it has are also dropped in order |
| 4595 | * |
| 4596 | * This ends up dropping the references in something close to optimal |
| 4597 | * order for reading and modifying the extent allocation tree. |
| 4598 | */ |
| 4599 | static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, |
| 4600 | struct btrfs_root *root, |
| 4601 | struct btrfs_path *path) |
| 4602 | { |
| 4603 | u64 bytenr; |
| 4604 | u64 root_owner; |
| 4605 | u64 root_gen; |
| 4606 | struct extent_buffer *eb = path->nodes[1]; |
| 4607 | struct extent_buffer *leaf; |
| 4608 | struct btrfs_leaf_ref *ref; |
| 4609 | struct refsort *sorted = NULL; |
| 4610 | int nritems = btrfs_header_nritems(eb); |
| 4611 | int ret; |
| 4612 | int i; |
| 4613 | int refi = 0; |
| 4614 | int slot = path->slots[1]; |
| 4615 | u32 blocksize = btrfs_level_size(root, 0); |
| 4616 | u32 refs; |
| 4617 | |
| 4618 | if (nritems == 0) |
| 4619 | goto out; |
| 4620 | |
| 4621 | root_owner = btrfs_header_owner(eb); |
| 4622 | root_gen = btrfs_header_generation(eb); |
| 4623 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 4624 | |
| 4625 | /* |
| 4626 | * step one, sort all the leaf pointers so we don't scribble |
| 4627 | * randomly into the extent allocation tree |
| 4628 | */ |
| 4629 | for (i = slot; i < nritems; i++) { |
| 4630 | sorted[refi].bytenr = btrfs_node_blockptr(eb, i); |
| 4631 | sorted[refi].slot = i; |
| 4632 | refi++; |
| 4633 | } |
| 4634 | |
| 4635 | /* |
| 4636 | * nritems won't be zero, but if we're picking up drop_snapshot |
| 4637 | * after a crash, slot might be > 0, so double check things |
| 4638 | * just in case. |
| 4639 | */ |
| 4640 | if (refi == 0) |
| 4641 | goto out; |
| 4642 | |
| 4643 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 4644 | |
| 4645 | /* |
| 4646 | * the first loop frees everything the leaves point to |
| 4647 | */ |
| 4648 | for (i = 0; i < refi; i++) { |
| 4649 | u64 ptr_gen; |
| 4650 | |
| 4651 | bytenr = sorted[i].bytenr; |
| 4652 | |
| 4653 | /* |
| 4654 | * check the reference count on this leaf. If it is > 1 |
| 4655 | * we just decrement it below and don't update any |
| 4656 | * of the refs the leaf points to. |
| 4657 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4658 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 4659 | blocksize, &refs); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4660 | BUG_ON(ret); |
| 4661 | if (refs != 1) |
| 4662 | continue; |
| 4663 | |
| 4664 | ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot); |
| 4665 | |
| 4666 | /* |
| 4667 | * the leaf only had one reference, which means the |
| 4668 | * only thing pointing to this leaf is the snapshot |
| 4669 | * we're deleting. It isn't possible for the reference |
| 4670 | * count to increase again later |
| 4671 | * |
| 4672 | * The reference cache is checked for the leaf, |
| 4673 | * and if found we'll be able to drop any refs held by |
| 4674 | * the leaf without needing to read it in. |
| 4675 | */ |
| 4676 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
| 4677 | if (ref && ref->generation != ptr_gen) { |
| 4678 | btrfs_free_leaf_ref(root, ref); |
| 4679 | ref = NULL; |
| 4680 | } |
| 4681 | if (ref) { |
| 4682 | ret = cache_drop_leaf_ref(trans, root, ref); |
| 4683 | BUG_ON(ret); |
| 4684 | btrfs_remove_leaf_ref(root, ref); |
| 4685 | btrfs_free_leaf_ref(root, ref); |
| 4686 | } else { |
| 4687 | /* |
| 4688 | * the leaf wasn't in the reference cache, so |
| 4689 | * we have to read it. |
| 4690 | */ |
| 4691 | leaf = read_tree_block(root, bytenr, blocksize, |
| 4692 | ptr_gen); |
| 4693 | ret = btrfs_drop_leaf_ref(trans, root, leaf); |
| 4694 | BUG_ON(ret); |
| 4695 | free_extent_buffer(leaf); |
| 4696 | } |
| 4697 | atomic_inc(&root->fs_info->throttle_gen); |
| 4698 | wake_up(&root->fs_info->transaction_throttle); |
| 4699 | cond_resched(); |
| 4700 | } |
| 4701 | |
| 4702 | /* |
| 4703 | * run through the loop again to free the refs on the leaves. |
| 4704 | * This is faster than doing it in the loop above because |
| 4705 | * the leaves are likely to be clustered together. We end up |
| 4706 | * working in nice chunks on the extent allocation tree. |
| 4707 | */ |
| 4708 | for (i = 0; i < refi; i++) { |
| 4709 | bytenr = sorted[i].bytenr; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4710 | ret = btrfs_free_extent(trans, root, bytenr, |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4711 | blocksize, eb->start, |
| 4712 | root_owner, root_gen, 0, 1); |
| 4713 | BUG_ON(ret); |
| 4714 | |
| 4715 | atomic_inc(&root->fs_info->throttle_gen); |
| 4716 | wake_up(&root->fs_info->transaction_throttle); |
| 4717 | cond_resched(); |
| 4718 | } |
| 4719 | out: |
| 4720 | kfree(sorted); |
| 4721 | |
| 4722 | /* |
| 4723 | * update the path to show we've processed the entire level 1 |
| 4724 | * node. This will get saved into the root's drop_snapshot_progress |
| 4725 | * field so these drops are not repeated again if this transaction |
| 4726 | * commits. |
| 4727 | */ |
| 4728 | path->slots[1] = nritems; |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
| 4732 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4733 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 4734 | * counts as it goes. |
| 4735 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4736 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4737 | struct btrfs_root *root, |
| 4738 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4739 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4740 | u64 root_owner; |
| 4741 | u64 root_gen; |
| 4742 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 4743 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4744 | struct extent_buffer *next; |
| 4745 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4746 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4747 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4748 | int ret; |
| 4749 | u32 refs; |
| 4750 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4751 | WARN_ON(*level < 0); |
| 4752 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4753 | ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4754 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4755 | BUG_ON(ret); |
| 4756 | if (refs > 1) |
| 4757 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4758 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4759 | /* |
| 4760 | * walk down to the last node level and free all the leaves |
| 4761 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4762 | while (*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4763 | WARN_ON(*level < 0); |
| 4764 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4765 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4766 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4767 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4768 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4769 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4770 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4771 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4772 | break; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4773 | |
| 4774 | /* the new code goes down to level 1 and does all the |
| 4775 | * leaves pointed to that node in bulk. So, this check |
| 4776 | * for level 0 will always be false. |
| 4777 | * |
| 4778 | * But, the disk format allows the drop_snapshot_progress |
| 4779 | * field in the root to leave things in a state where |
| 4780 | * a leaf will need cleaning up here. If someone crashes |
| 4781 | * with the old code and then boots with the new code, |
| 4782 | * we might find a leaf here. |
| 4783 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4784 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4785 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4786 | BUG_ON(ret); |
| 4787 | break; |
| 4788 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4789 | |
| 4790 | /* |
| 4791 | * once we get to level one, process the whole node |
| 4792 | * at once, including everything below it. |
| 4793 | */ |
| 4794 | if (*level == 1) { |
| 4795 | ret = drop_level_one_refs(trans, root, path); |
| 4796 | BUG_ON(ret); |
| 4797 | break; |
| 4798 | } |
| 4799 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4800 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 4801 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4802 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4803 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4804 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 4805 | blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4806 | BUG_ON(ret); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4807 | |
| 4808 | /* |
| 4809 | * if there is more than one reference, we don't need |
| 4810 | * to read that node to drop any references it has. We |
| 4811 | * just drop the ref we hold on that node and move on to the |
| 4812 | * next slot in this level. |
| 4813 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4814 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4815 | parent = path->nodes[*level]; |
| 4816 | root_owner = btrfs_header_owner(parent); |
| 4817 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4818 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4819 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4820 | ret = btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4821 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4822 | root_owner, root_gen, |
| 4823 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4824 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4825 | |
| 4826 | atomic_inc(&root->fs_info->throttle_gen); |
| 4827 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4828 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4829 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4830 | continue; |
| 4831 | } |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4832 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4833 | /* |
| 4834 | * we need to keep freeing things in the next level down. |
| 4835 | * read the block and loop around to process it |
| 4836 | */ |
| 4837 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4838 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 4839 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4840 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4841 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4842 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4843 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4844 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4845 | } |
| 4846 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4847 | WARN_ON(*level < 0); |
| 4848 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4849 | |
| 4850 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4851 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4852 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4853 | } else { |
| 4854 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4855 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4856 | } |
| 4857 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4858 | blocksize = btrfs_level_size(root, *level); |
| 4859 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4860 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4861 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4862 | /* |
| 4863 | * cleanup and free the reference on the last node |
| 4864 | * we processed |
| 4865 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4866 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4867 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4868 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4869 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4870 | path->nodes[*level] = NULL; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4871 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4872 | *level += 1; |
| 4873 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4874 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4875 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4876 | return 0; |
| 4877 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4878 | #endif |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4879 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4880 | struct walk_control { |
| 4881 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4882 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4883 | struct btrfs_key update_progress; |
| 4884 | int stage; |
| 4885 | int level; |
| 4886 | int shared_level; |
| 4887 | int update_ref; |
| 4888 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4889 | int reada_slot; |
| 4890 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4891 | }; |
| 4892 | |
| 4893 | #define DROP_REFERENCE 1 |
| 4894 | #define UPDATE_BACKREF 2 |
| 4895 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4896 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4897 | struct btrfs_root *root, |
| 4898 | struct walk_control *wc, |
| 4899 | struct btrfs_path *path) |
| 4900 | { |
| 4901 | u64 bytenr; |
| 4902 | u64 generation; |
| 4903 | u64 refs; |
| 4904 | u64 last = 0; |
| 4905 | u32 nritems; |
| 4906 | u32 blocksize; |
| 4907 | struct btrfs_key key; |
| 4908 | struct extent_buffer *eb; |
| 4909 | int ret; |
| 4910 | int slot; |
| 4911 | int nread = 0; |
| 4912 | |
| 4913 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4914 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4915 | wc->reada_count = max(wc->reada_count, 2); |
| 4916 | } else { |
| 4917 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4918 | wc->reada_count = min_t(int, wc->reada_count, |
| 4919 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 4920 | } |
| 4921 | |
| 4922 | eb = path->nodes[wc->level]; |
| 4923 | nritems = btrfs_header_nritems(eb); |
| 4924 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 4925 | |
| 4926 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 4927 | if (nread >= wc->reada_count) |
| 4928 | break; |
| 4929 | |
| 4930 | cond_resched(); |
| 4931 | bytenr = btrfs_node_blockptr(eb, slot); |
| 4932 | generation = btrfs_node_ptr_generation(eb, slot); |
| 4933 | |
| 4934 | if (slot == path->slots[wc->level]) |
| 4935 | goto reada; |
| 4936 | |
| 4937 | if (wc->stage == UPDATE_BACKREF && |
| 4938 | generation <= root->root_key.offset) |
| 4939 | continue; |
| 4940 | |
| 4941 | if (wc->stage == DROP_REFERENCE) { |
| 4942 | ret = btrfs_lookup_extent_info(trans, root, |
| 4943 | bytenr, blocksize, |
| 4944 | &refs, NULL); |
| 4945 | BUG_ON(ret); |
| 4946 | BUG_ON(refs == 0); |
| 4947 | if (refs == 1) |
| 4948 | goto reada; |
| 4949 | |
| 4950 | if (!wc->update_ref || |
| 4951 | generation <= root->root_key.offset) |
| 4952 | continue; |
| 4953 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 4954 | ret = btrfs_comp_cpu_keys(&key, |
| 4955 | &wc->update_progress); |
| 4956 | if (ret < 0) |
| 4957 | continue; |
| 4958 | } |
| 4959 | reada: |
| 4960 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 4961 | generation); |
| 4962 | if (ret) |
| 4963 | break; |
| 4964 | last = bytenr + blocksize; |
| 4965 | nread++; |
| 4966 | } |
| 4967 | wc->reada_slot = slot; |
| 4968 | } |
| 4969 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4970 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4971 | * hepler to process tree block while walking down the tree. |
| 4972 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4973 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 4974 | * back refs for pointers in the block. |
| 4975 | * |
| 4976 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4977 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4978 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 4979 | struct btrfs_root *root, |
| 4980 | struct btrfs_path *path, |
| 4981 | struct walk_control *wc) |
| 4982 | { |
| 4983 | int level = wc->level; |
| 4984 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4985 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4986 | int ret; |
| 4987 | |
| 4988 | if (wc->stage == UPDATE_BACKREF && |
| 4989 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 4990 | return 1; |
| 4991 | |
| 4992 | /* |
| 4993 | * when reference count of tree block is 1, it won't increase |
| 4994 | * again. once full backref flag is set, we never clear it. |
| 4995 | */ |
| 4996 | if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 4997 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) { |
| 4998 | BUG_ON(!path->locks[level]); |
| 4999 | ret = btrfs_lookup_extent_info(trans, root, |
| 5000 | eb->start, eb->len, |
| 5001 | &wc->refs[level], |
| 5002 | &wc->flags[level]); |
| 5003 | BUG_ON(ret); |
| 5004 | BUG_ON(wc->refs[level] == 0); |
| 5005 | } |
| 5006 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5007 | if (wc->stage == DROP_REFERENCE) { |
| 5008 | if (wc->refs[level] > 1) |
| 5009 | return 1; |
| 5010 | |
| 5011 | if (path->locks[level] && !wc->keep_locks) { |
| 5012 | btrfs_tree_unlock(eb); |
| 5013 | path->locks[level] = 0; |
| 5014 | } |
| 5015 | return 0; |
| 5016 | } |
| 5017 | |
| 5018 | /* wc->stage == UPDATE_BACKREF */ |
| 5019 | if (!(wc->flags[level] & flag)) { |
| 5020 | BUG_ON(!path->locks[level]); |
| 5021 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5022 | BUG_ON(ret); |
| 5023 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5024 | BUG_ON(ret); |
| 5025 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5026 | eb->len, flag, 0); |
| 5027 | BUG_ON(ret); |
| 5028 | wc->flags[level] |= flag; |
| 5029 | } |
| 5030 | |
| 5031 | /* |
| 5032 | * the block is shared by multiple trees, so it's not good to |
| 5033 | * keep the tree lock |
| 5034 | */ |
| 5035 | if (path->locks[level] && level > 0) { |
| 5036 | btrfs_tree_unlock(eb); |
| 5037 | path->locks[level] = 0; |
| 5038 | } |
| 5039 | return 0; |
| 5040 | } |
| 5041 | |
| 5042 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5043 | * hepler to process tree block pointer. |
| 5044 | * |
| 5045 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5046 | * reference count of the block pointed to. if the block |
| 5047 | * is shared and we need update back refs for the subtree |
| 5048 | * rooted at the block, this function changes wc->stage to |
| 5049 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5050 | * need to update back, this function drops the reference |
| 5051 | * to the block. |
| 5052 | * |
| 5053 | * NOTE: return value 1 means we should stop walking down. |
| 5054 | */ |
| 5055 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5056 | struct btrfs_root *root, |
| 5057 | struct btrfs_path *path, |
| 5058 | struct walk_control *wc) |
| 5059 | { |
| 5060 | u64 bytenr; |
| 5061 | u64 generation; |
| 5062 | u64 parent; |
| 5063 | u32 blocksize; |
| 5064 | struct btrfs_key key; |
| 5065 | struct extent_buffer *next; |
| 5066 | int level = wc->level; |
| 5067 | int reada = 0; |
| 5068 | int ret = 0; |
| 5069 | |
| 5070 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5071 | path->slots[level]); |
| 5072 | /* |
| 5073 | * if the lower level block was created before the snapshot |
| 5074 | * was created, we know there is no need to update back refs |
| 5075 | * for the subtree |
| 5076 | */ |
| 5077 | if (wc->stage == UPDATE_BACKREF && |
| 5078 | generation <= root->root_key.offset) |
| 5079 | return 1; |
| 5080 | |
| 5081 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5082 | blocksize = btrfs_level_size(root, level - 1); |
| 5083 | |
| 5084 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5085 | if (!next) { |
| 5086 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5087 | reada = 1; |
| 5088 | } |
| 5089 | btrfs_tree_lock(next); |
| 5090 | btrfs_set_lock_blocking(next); |
| 5091 | |
| 5092 | if (wc->stage == DROP_REFERENCE) { |
| 5093 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5094 | &wc->refs[level - 1], |
| 5095 | &wc->flags[level - 1]); |
| 5096 | BUG_ON(ret); |
| 5097 | BUG_ON(wc->refs[level - 1] == 0); |
| 5098 | |
| 5099 | if (wc->refs[level - 1] > 1) { |
| 5100 | if (!wc->update_ref || |
| 5101 | generation <= root->root_key.offset) |
| 5102 | goto skip; |
| 5103 | |
| 5104 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5105 | path->slots[level]); |
| 5106 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5107 | if (ret < 0) |
| 5108 | goto skip; |
| 5109 | |
| 5110 | wc->stage = UPDATE_BACKREF; |
| 5111 | wc->shared_level = level - 1; |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5116 | btrfs_tree_unlock(next); |
| 5117 | free_extent_buffer(next); |
| 5118 | next = NULL; |
| 5119 | } |
| 5120 | |
| 5121 | if (!next) { |
| 5122 | if (reada && level == 1) |
| 5123 | reada_walk_down(trans, root, wc, path); |
| 5124 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5125 | btrfs_tree_lock(next); |
| 5126 | btrfs_set_lock_blocking(next); |
| 5127 | } |
| 5128 | |
| 5129 | level--; |
| 5130 | BUG_ON(level != btrfs_header_level(next)); |
| 5131 | path->nodes[level] = next; |
| 5132 | path->slots[level] = 0; |
| 5133 | path->locks[level] = 1; |
| 5134 | wc->level = level; |
| 5135 | if (wc->level == 1) |
| 5136 | wc->reada_slot = 0; |
| 5137 | return 0; |
| 5138 | skip: |
| 5139 | wc->refs[level - 1] = 0; |
| 5140 | wc->flags[level - 1] = 0; |
| 5141 | |
| 5142 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5143 | parent = path->nodes[level]->start; |
| 5144 | } else { |
| 5145 | BUG_ON(root->root_key.objectid != |
| 5146 | btrfs_header_owner(path->nodes[level])); |
| 5147 | parent = 0; |
| 5148 | } |
| 5149 | |
| 5150 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 5151 | root->root_key.objectid, level - 1, 0); |
| 5152 | BUG_ON(ret); |
| 5153 | |
| 5154 | btrfs_tree_unlock(next); |
| 5155 | free_extent_buffer(next); |
| 5156 | return 1; |
| 5157 | } |
| 5158 | |
| 5159 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5160 | * hepler to process tree block while walking up the tree. |
| 5161 | * |
| 5162 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5163 | * reference count on the block. |
| 5164 | * |
| 5165 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5166 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5167 | * to UPDATE_BACKREF previously while processing the block. |
| 5168 | * |
| 5169 | * NOTE: return value 1 means we should stop walking up. |
| 5170 | */ |
| 5171 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5172 | struct btrfs_root *root, |
| 5173 | struct btrfs_path *path, |
| 5174 | struct walk_control *wc) |
| 5175 | { |
| 5176 | int ret = 0; |
| 5177 | int level = wc->level; |
| 5178 | struct extent_buffer *eb = path->nodes[level]; |
| 5179 | u64 parent = 0; |
| 5180 | |
| 5181 | if (wc->stage == UPDATE_BACKREF) { |
| 5182 | BUG_ON(wc->shared_level < level); |
| 5183 | if (level < wc->shared_level) |
| 5184 | goto out; |
| 5185 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5186 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5187 | if (ret > 0) |
| 5188 | wc->update_ref = 0; |
| 5189 | |
| 5190 | wc->stage = DROP_REFERENCE; |
| 5191 | wc->shared_level = -1; |
| 5192 | path->slots[level] = 0; |
| 5193 | |
| 5194 | /* |
| 5195 | * check reference count again if the block isn't locked. |
| 5196 | * we should start walking down the tree again if reference |
| 5197 | * count is one. |
| 5198 | */ |
| 5199 | if (!path->locks[level]) { |
| 5200 | BUG_ON(level == 0); |
| 5201 | btrfs_tree_lock(eb); |
| 5202 | btrfs_set_lock_blocking(eb); |
| 5203 | path->locks[level] = 1; |
| 5204 | |
| 5205 | ret = btrfs_lookup_extent_info(trans, root, |
| 5206 | eb->start, eb->len, |
| 5207 | &wc->refs[level], |
| 5208 | &wc->flags[level]); |
| 5209 | BUG_ON(ret); |
| 5210 | BUG_ON(wc->refs[level] == 0); |
| 5211 | if (wc->refs[level] == 1) { |
| 5212 | btrfs_tree_unlock(eb); |
| 5213 | path->locks[level] = 0; |
| 5214 | return 1; |
| 5215 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | |
| 5219 | /* wc->stage == DROP_REFERENCE */ |
| 5220 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5221 | |
| 5222 | if (wc->refs[level] == 1) { |
| 5223 | if (level == 0) { |
| 5224 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5225 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 5226 | else |
| 5227 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5228 | BUG_ON(ret); |
| 5229 | } |
| 5230 | /* make block locked assertion in clean_tree_block happy */ |
| 5231 | if (!path->locks[level] && |
| 5232 | btrfs_header_generation(eb) == trans->transid) { |
| 5233 | btrfs_tree_lock(eb); |
| 5234 | btrfs_set_lock_blocking(eb); |
| 5235 | path->locks[level] = 1; |
| 5236 | } |
| 5237 | clean_tree_block(trans, root, eb); |
| 5238 | } |
| 5239 | |
| 5240 | if (eb == root->node) { |
| 5241 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5242 | parent = eb->start; |
| 5243 | else |
| 5244 | BUG_ON(root->root_key.objectid != |
| 5245 | btrfs_header_owner(eb)); |
| 5246 | } else { |
| 5247 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5248 | parent = path->nodes[level + 1]->start; |
| 5249 | else |
| 5250 | BUG_ON(root->root_key.objectid != |
| 5251 | btrfs_header_owner(path->nodes[level + 1])); |
| 5252 | } |
| 5253 | |
| 5254 | ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent, |
| 5255 | root->root_key.objectid, level, 0); |
| 5256 | BUG_ON(ret); |
| 5257 | out: |
| 5258 | wc->refs[level] = 0; |
| 5259 | wc->flags[level] = 0; |
| 5260 | return ret; |
| 5261 | } |
| 5262 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5263 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5264 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5265 | struct btrfs_path *path, |
| 5266 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5267 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5268 | int level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5269 | int ret; |
| 5270 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5271 | while (level >= 0) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5272 | if (path->slots[level] >= |
| 5273 | btrfs_header_nritems(path->nodes[level])) |
| 5274 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5275 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5276 | ret = walk_down_proc(trans, root, path, wc); |
| 5277 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5278 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5279 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5280 | if (level == 0) |
| 5281 | break; |
| 5282 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5283 | ret = do_walk_down(trans, root, path, wc); |
| 5284 | if (ret > 0) { |
| 5285 | path->slots[level]++; |
| 5286 | continue; |
| 5287 | } |
| 5288 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5289 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5290 | return 0; |
| 5291 | } |
| 5292 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5293 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5294 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5295 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5296 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5297 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5298 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5299 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5300 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5301 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5302 | while (level < max_level && path->nodes[level]) { |
| 5303 | wc->level = level; |
| 5304 | if (path->slots[level] + 1 < |
| 5305 | btrfs_header_nritems(path->nodes[level])) { |
| 5306 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5307 | return 0; |
| 5308 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5309 | ret = walk_up_proc(trans, root, path, wc); |
| 5310 | if (ret > 0) |
| 5311 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5312 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5313 | if (path->locks[level]) { |
| 5314 | btrfs_tree_unlock(path->nodes[level]); |
| 5315 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5316 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5317 | free_extent_buffer(path->nodes[level]); |
| 5318 | path->nodes[level] = NULL; |
| 5319 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5320 | } |
| 5321 | } |
| 5322 | return 1; |
| 5323 | } |
| 5324 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5325 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5326 | * drop a subvolume tree. |
| 5327 | * |
| 5328 | * this function traverses the tree freeing any blocks that only |
| 5329 | * referenced by the tree. |
| 5330 | * |
| 5331 | * when a shared tree block is found. this function decreases its |
| 5332 | * reference count by one. if update_ref is true, this function |
| 5333 | * also make sure backrefs for the shared block and all lower level |
| 5334 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5335 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5336 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5337 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5338 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5339 | struct btrfs_trans_handle *trans; |
| 5340 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5341 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5342 | struct walk_control *wc; |
| 5343 | struct btrfs_key key; |
| 5344 | int err = 0; |
| 5345 | int ret; |
| 5346 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5347 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5348 | path = btrfs_alloc_path(); |
| 5349 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5350 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5351 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5352 | BUG_ON(!wc); |
| 5353 | |
| 5354 | trans = btrfs_start_transaction(tree_root, 1); |
| 5355 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5356 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5357 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5358 | path->nodes[level] = btrfs_lock_root_node(root); |
| 5359 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5360 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5361 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5362 | memset(&wc->update_progress, 0, |
| 5363 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5364 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5365 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5366 | memcpy(&wc->update_progress, &key, |
| 5367 | sizeof(wc->update_progress)); |
| 5368 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5369 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5370 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5371 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5372 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5373 | path->lowest_level = 0; |
| 5374 | if (ret < 0) { |
| 5375 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5376 | goto out; |
| 5377 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5378 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5379 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5380 | /* |
| 5381 | * unlock our path, this is safe because only this |
| 5382 | * function is allowed to delete this snapshot |
| 5383 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5384 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5385 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5386 | level = btrfs_header_level(root->node); |
| 5387 | while (1) { |
| 5388 | btrfs_tree_lock(path->nodes[level]); |
| 5389 | btrfs_set_lock_blocking(path->nodes[level]); |
| 5390 | |
| 5391 | ret = btrfs_lookup_extent_info(trans, root, |
| 5392 | path->nodes[level]->start, |
| 5393 | path->nodes[level]->len, |
| 5394 | &wc->refs[level], |
| 5395 | &wc->flags[level]); |
| 5396 | BUG_ON(ret); |
| 5397 | BUG_ON(wc->refs[level] == 0); |
| 5398 | |
| 5399 | if (level == root_item->drop_level) |
| 5400 | break; |
| 5401 | |
| 5402 | btrfs_tree_unlock(path->nodes[level]); |
| 5403 | WARN_ON(wc->refs[level] != 1); |
| 5404 | level--; |
| 5405 | } |
| 5406 | } |
| 5407 | |
| 5408 | wc->level = level; |
| 5409 | wc->shared_level = -1; |
| 5410 | wc->stage = DROP_REFERENCE; |
| 5411 | wc->update_ref = update_ref; |
| 5412 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5413 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5414 | |
| 5415 | while (1) { |
| 5416 | ret = walk_down_tree(trans, root, path, wc); |
| 5417 | if (ret < 0) { |
| 5418 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5419 | break; |
| 5420 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5421 | |
| 5422 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5423 | if (ret < 0) { |
| 5424 | err = ret; |
| 5425 | break; |
| 5426 | } |
| 5427 | |
| 5428 | if (ret > 0) { |
| 5429 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5430 | break; |
| 5431 | } |
| 5432 | |
| 5433 | if (wc->stage == DROP_REFERENCE) { |
| 5434 | level = wc->level; |
| 5435 | btrfs_node_key(path->nodes[level], |
| 5436 | &root_item->drop_progress, |
| 5437 | path->slots[level]); |
| 5438 | root_item->drop_level = level; |
| 5439 | } |
| 5440 | |
| 5441 | BUG_ON(wc->level == 0); |
| 5442 | if (trans->transaction->in_commit || |
| 5443 | trans->transaction->delayed_refs.flushing) { |
| 5444 | ret = btrfs_update_root(trans, tree_root, |
| 5445 | &root->root_key, |
| 5446 | root_item); |
| 5447 | BUG_ON(ret); |
| 5448 | |
| 5449 | btrfs_end_transaction(trans, tree_root); |
| 5450 | trans = btrfs_start_transaction(tree_root, 1); |
| 5451 | } else { |
| 5452 | unsigned long update; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5453 | update = trans->delayed_ref_updates; |
| 5454 | trans->delayed_ref_updates = 0; |
| 5455 | if (update) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5456 | btrfs_run_delayed_refs(trans, tree_root, |
| 5457 | update); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5458 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5459 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5460 | btrfs_release_path(root, path); |
| 5461 | BUG_ON(err); |
| 5462 | |
| 5463 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 5464 | BUG_ON(ret); |
| 5465 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame^] | 5466 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 5467 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 5468 | NULL, NULL); |
| 5469 | BUG_ON(ret < 0); |
| 5470 | if (ret > 0) { |
| 5471 | ret = btrfs_del_orphan_item(trans, tree_root, |
| 5472 | root->root_key.objectid); |
| 5473 | BUG_ON(ret); |
| 5474 | } |
| 5475 | } |
| 5476 | |
| 5477 | if (root->in_radix) { |
| 5478 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 5479 | } else { |
| 5480 | free_extent_buffer(root->node); |
| 5481 | free_extent_buffer(root->commit_root); |
| 5482 | kfree(root); |
| 5483 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5484 | out: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5485 | btrfs_end_transaction(trans, tree_root); |
| 5486 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5487 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5488 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5489 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5490 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5491 | /* |
| 5492 | * drop subtree rooted at tree block 'node'. |
| 5493 | * |
| 5494 | * NOTE: this function will unlock and release tree block 'node' |
| 5495 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5496 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5497 | struct btrfs_root *root, |
| 5498 | struct extent_buffer *node, |
| 5499 | struct extent_buffer *parent) |
| 5500 | { |
| 5501 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5502 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5503 | int level; |
| 5504 | int parent_level; |
| 5505 | int ret = 0; |
| 5506 | int wret; |
| 5507 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5508 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5509 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5510 | path = btrfs_alloc_path(); |
| 5511 | BUG_ON(!path); |
| 5512 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5513 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5514 | BUG_ON(!wc); |
| 5515 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5516 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5517 | parent_level = btrfs_header_level(parent); |
| 5518 | extent_buffer_get(parent); |
| 5519 | path->nodes[parent_level] = parent; |
| 5520 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5521 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5522 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5523 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5524 | path->nodes[level] = node; |
| 5525 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5526 | path->locks[level] = 1; |
| 5527 | |
| 5528 | wc->refs[parent_level] = 1; |
| 5529 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5530 | wc->level = level; |
| 5531 | wc->shared_level = -1; |
| 5532 | wc->stage = DROP_REFERENCE; |
| 5533 | wc->update_ref = 0; |
| 5534 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5535 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5536 | |
| 5537 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5538 | wret = walk_down_tree(trans, root, path, wc); |
| 5539 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5540 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5541 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5542 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5543 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5544 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5545 | if (wret < 0) |
| 5546 | ret = wret; |
| 5547 | if (wret != 0) |
| 5548 | break; |
| 5549 | } |
| 5550 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5551 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5552 | btrfs_free_path(path); |
| 5553 | return ret; |
| 5554 | } |
| 5555 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5556 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5557 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 5558 | unsigned long nr) |
| 5559 | { |
| 5560 | return min(last, start + nr - 1); |
| 5561 | } |
| 5562 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5563 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5564 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5565 | { |
| 5566 | u64 page_start; |
| 5567 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5568 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5569 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5570 | unsigned long i; |
| 5571 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5572 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5573 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5574 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5575 | unsigned int total_read = 0; |
| 5576 | unsigned int total_dirty = 0; |
| 5577 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5578 | |
| 5579 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5580 | |
| 5581 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5582 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5583 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 5584 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5585 | /* make sure the dirty trick played by the caller work */ |
| 5586 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5587 | first_index, last_index); |
| 5588 | if (ret) |
| 5589 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5590 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5591 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5592 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5593 | for (i = first_index ; i <= last_index; i++) { |
| 5594 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5595 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5596 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5597 | } |
| 5598 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5599 | again: |
| 5600 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5601 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5602 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5603 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5604 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5605 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5606 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5607 | if (!PageUptodate(page)) { |
| 5608 | btrfs_readpage(NULL, page); |
| 5609 | lock_page(page); |
| 5610 | if (!PageUptodate(page)) { |
| 5611 | unlock_page(page); |
| 5612 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5613 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5614 | goto out_unlock; |
| 5615 | } |
| 5616 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5617 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5618 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5619 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 5620 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5621 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5622 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5623 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5624 | if (ordered) { |
| 5625 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5626 | unlock_page(page); |
| 5627 | page_cache_release(page); |
| 5628 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5629 | btrfs_put_ordered_extent(ordered); |
| 5630 | goto again; |
| 5631 | } |
| 5632 | set_page_extent_mapped(page); |
| 5633 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5634 | if (i == first_index) |
| 5635 | set_extent_bits(io_tree, page_start, page_end, |
| 5636 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 5637 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5638 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5639 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5640 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5641 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5642 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5643 | unlock_page(page); |
| 5644 | page_cache_release(page); |
| 5645 | } |
| 5646 | |
| 5647 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5648 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5649 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5650 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5651 | return ret; |
| 5652 | } |
| 5653 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5654 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5655 | struct btrfs_key *extent_key, |
| 5656 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5657 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5658 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5659 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 5660 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5661 | u64 start = extent_key->objectid - offset; |
| 5662 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5663 | |
| 5664 | em = alloc_extent_map(GFP_NOFS); |
| 5665 | BUG_ON(!em || IS_ERR(em)); |
| 5666 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5667 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5668 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5669 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5670 | em->block_start = extent_key->objectid; |
| 5671 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5672 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5673 | |
| 5674 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5675 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5676 | while (1) { |
| 5677 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5678 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5679 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5680 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5681 | if (ret != -EEXIST) { |
| 5682 | free_extent_map(em); |
| 5683 | break; |
| 5684 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5685 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5686 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5687 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5688 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5689 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | struct btrfs_ref_path { |
| 5693 | u64 extent_start; |
| 5694 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 5695 | u64 root_objectid; |
| 5696 | u64 root_generation; |
| 5697 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5698 | u32 num_refs; |
| 5699 | int lowest_level; |
| 5700 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5701 | int shared_level; |
| 5702 | |
| 5703 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 5704 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5705 | }; |
| 5706 | |
| 5707 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5708 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5709 | u64 disk_bytenr; |
| 5710 | u64 disk_num_bytes; |
| 5711 | u64 offset; |
| 5712 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5713 | u8 compression; |
| 5714 | u8 encryption; |
| 5715 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5716 | }; |
| 5717 | |
| 5718 | static int is_cowonly_root(u64 root_objectid) |
| 5719 | { |
| 5720 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 5721 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 5722 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 5723 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5724 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5725 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5726 | return 1; |
| 5727 | return 0; |
| 5728 | } |
| 5729 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5730 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5731 | struct btrfs_root *extent_root, |
| 5732 | struct btrfs_ref_path *ref_path, |
| 5733 | int first_time) |
| 5734 | { |
| 5735 | struct extent_buffer *leaf; |
| 5736 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5737 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5738 | struct btrfs_key key; |
| 5739 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5740 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5741 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5742 | int level; |
| 5743 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5744 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5745 | path = btrfs_alloc_path(); |
| 5746 | if (!path) |
| 5747 | return -ENOMEM; |
| 5748 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5749 | if (first_time) { |
| 5750 | ref_path->lowest_level = -1; |
| 5751 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5752 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5753 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5754 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5755 | walk_down: |
| 5756 | level = ref_path->current_level - 1; |
| 5757 | while (level >= -1) { |
| 5758 | u64 parent; |
| 5759 | if (level < ref_path->lowest_level) |
| 5760 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5761 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5762 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5763 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5764 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5765 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5766 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5767 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5768 | parent = ref_path->nodes[level + 1]; |
| 5769 | ref_path->nodes[level + 1] = 0; |
| 5770 | ref_path->current_level = level; |
| 5771 | BUG_ON(parent == 0); |
| 5772 | |
| 5773 | key.objectid = bytenr; |
| 5774 | key.offset = parent + 1; |
| 5775 | key.type = BTRFS_EXTENT_REF_KEY; |
| 5776 | |
| 5777 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5778 | if (ret < 0) |
| 5779 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5780 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5781 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5782 | leaf = path->nodes[0]; |
| 5783 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5784 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5785 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5786 | if (ret < 0) |
| 5787 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5788 | if (ret > 0) |
| 5789 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5790 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5791 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5792 | |
| 5793 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5794 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5795 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 5796 | if (level < ref_path->shared_level) |
| 5797 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5798 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5799 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5800 | next: |
| 5801 | level--; |
| 5802 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5803 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5804 | } |
| 5805 | /* reached lowest level */ |
| 5806 | ret = 1; |
| 5807 | goto out; |
| 5808 | walk_up: |
| 5809 | level = ref_path->current_level; |
| 5810 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 5811 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5812 | |
| 5813 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5814 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5815 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5816 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5817 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5818 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5819 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5820 | key.objectid = bytenr; |
| 5821 | key.offset = 0; |
| 5822 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5823 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5824 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 5825 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5826 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5827 | |
| 5828 | leaf = path->nodes[0]; |
| 5829 | nritems = btrfs_header_nritems(leaf); |
| 5830 | if (path->slots[0] >= nritems) { |
| 5831 | ret = btrfs_next_leaf(extent_root, path); |
| 5832 | if (ret < 0) |
| 5833 | goto out; |
| 5834 | if (ret > 0) { |
| 5835 | /* the extent was freed by someone */ |
| 5836 | if (ref_path->lowest_level == level) |
| 5837 | goto out; |
| 5838 | btrfs_release_path(extent_root, path); |
| 5839 | goto walk_down; |
| 5840 | } |
| 5841 | leaf = path->nodes[0]; |
| 5842 | } |
| 5843 | |
| 5844 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5845 | if (found_key.objectid != bytenr || |
| 5846 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 5847 | /* the extent was freed by someone */ |
| 5848 | if (ref_path->lowest_level == level) { |
| 5849 | ret = 1; |
| 5850 | goto out; |
| 5851 | } |
| 5852 | btrfs_release_path(extent_root, path); |
| 5853 | goto walk_down; |
| 5854 | } |
| 5855 | found: |
| 5856 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 5857 | struct btrfs_extent_ref); |
| 5858 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 5859 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5860 | if (first_time) { |
| 5861 | level = (int)ref_objectid; |
| 5862 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 5863 | ref_path->lowest_level = level; |
| 5864 | ref_path->current_level = level; |
| 5865 | ref_path->nodes[level] = bytenr; |
| 5866 | } else { |
| 5867 | WARN_ON(ref_objectid != level); |
| 5868 | } |
| 5869 | } else { |
| 5870 | WARN_ON(level != -1); |
| 5871 | } |
| 5872 | first_time = 0; |
| 5873 | |
| 5874 | if (ref_path->lowest_level == level) { |
| 5875 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5876 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 5877 | } |
| 5878 | |
| 5879 | /* |
| 5880 | * the block is tree root or the block isn't in reference |
| 5881 | * counted tree. |
| 5882 | */ |
| 5883 | if (found_key.objectid == found_key.offset || |
| 5884 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 5885 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 5886 | ref_path->root_generation = |
| 5887 | btrfs_ref_generation(leaf, ref); |
| 5888 | if (level < 0) { |
| 5889 | /* special reference from the tree log */ |
| 5890 | ref_path->nodes[0] = found_key.offset; |
| 5891 | ref_path->current_level = 0; |
| 5892 | } |
| 5893 | ret = 0; |
| 5894 | goto out; |
| 5895 | } |
| 5896 | |
| 5897 | level++; |
| 5898 | BUG_ON(ref_path->nodes[level] != 0); |
| 5899 | ref_path->nodes[level] = found_key.offset; |
| 5900 | ref_path->current_level = level; |
| 5901 | |
| 5902 | /* |
| 5903 | * the reference was created in the running transaction, |
| 5904 | * no need to continue walking up. |
| 5905 | */ |
| 5906 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 5907 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 5908 | ref_path->root_generation = |
| 5909 | btrfs_ref_generation(leaf, ref); |
| 5910 | ret = 0; |
| 5911 | goto out; |
| 5912 | } |
| 5913 | |
| 5914 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5915 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5916 | } |
| 5917 | /* reached max tree level, but no tree root found. */ |
| 5918 | BUG(); |
| 5919 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5920 | btrfs_free_path(path); |
| 5921 | return ret; |
| 5922 | } |
| 5923 | |
| 5924 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 5925 | struct btrfs_root *extent_root, |
| 5926 | struct btrfs_ref_path *ref_path, |
| 5927 | u64 extent_start) |
| 5928 | { |
| 5929 | memset(ref_path, 0, sizeof(*ref_path)); |
| 5930 | ref_path->extent_start = extent_start; |
| 5931 | |
| 5932 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 5933 | } |
| 5934 | |
| 5935 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 5936 | struct btrfs_root *extent_root, |
| 5937 | struct btrfs_ref_path *ref_path) |
| 5938 | { |
| 5939 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 5940 | } |
| 5941 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5942 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5943 | struct btrfs_key *extent_key, |
| 5944 | u64 offset, int no_fragment, |
| 5945 | struct disk_extent **extents, |
| 5946 | int *nr_extents) |
| 5947 | { |
| 5948 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5949 | struct btrfs_path *path; |
| 5950 | struct btrfs_file_extent_item *fi; |
| 5951 | struct extent_buffer *leaf; |
| 5952 | struct disk_extent *exts = *extents; |
| 5953 | struct btrfs_key found_key; |
| 5954 | u64 cur_pos; |
| 5955 | u64 last_byte; |
| 5956 | u32 nritems; |
| 5957 | int nr = 0; |
| 5958 | int max = *nr_extents; |
| 5959 | int ret; |
| 5960 | |
| 5961 | WARN_ON(!no_fragment && *extents); |
| 5962 | if (!exts) { |
| 5963 | max = 1; |
| 5964 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 5965 | if (!exts) |
| 5966 | return -ENOMEM; |
| 5967 | } |
| 5968 | |
| 5969 | path = btrfs_alloc_path(); |
| 5970 | BUG_ON(!path); |
| 5971 | |
| 5972 | cur_pos = extent_key->objectid - offset; |
| 5973 | last_byte = extent_key->objectid + extent_key->offset; |
| 5974 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 5975 | cur_pos, 0); |
| 5976 | if (ret < 0) |
| 5977 | goto out; |
| 5978 | if (ret > 0) { |
| 5979 | ret = -ENOENT; |
| 5980 | goto out; |
| 5981 | } |
| 5982 | |
| 5983 | while (1) { |
| 5984 | leaf = path->nodes[0]; |
| 5985 | nritems = btrfs_header_nritems(leaf); |
| 5986 | if (path->slots[0] >= nritems) { |
| 5987 | ret = btrfs_next_leaf(root, path); |
| 5988 | if (ret < 0) |
| 5989 | goto out; |
| 5990 | if (ret > 0) |
| 5991 | break; |
| 5992 | leaf = path->nodes[0]; |
| 5993 | } |
| 5994 | |
| 5995 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5996 | if (found_key.offset != cur_pos || |
| 5997 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 5998 | found_key.objectid != reloc_inode->i_ino) |
| 5999 | break; |
| 6000 | |
| 6001 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6002 | struct btrfs_file_extent_item); |
| 6003 | if (btrfs_file_extent_type(leaf, fi) != |
| 6004 | BTRFS_FILE_EXTENT_REG || |
| 6005 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6006 | break; |
| 6007 | |
| 6008 | if (nr == max) { |
| 6009 | struct disk_extent *old = exts; |
| 6010 | max *= 2; |
| 6011 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6012 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6013 | if (old != *extents) |
| 6014 | kfree(old); |
| 6015 | } |
| 6016 | |
| 6017 | exts[nr].disk_bytenr = |
| 6018 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6019 | exts[nr].disk_num_bytes = |
| 6020 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6021 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6022 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6023 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6024 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6025 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6026 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6027 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6028 | BUG_ON(exts[nr].offset > 0); |
| 6029 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6030 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6031 | |
| 6032 | cur_pos += exts[nr].num_bytes; |
| 6033 | nr++; |
| 6034 | |
| 6035 | if (cur_pos + offset >= last_byte) |
| 6036 | break; |
| 6037 | |
| 6038 | if (no_fragment) { |
| 6039 | ret = 1; |
| 6040 | goto out; |
| 6041 | } |
| 6042 | path->slots[0]++; |
| 6043 | } |
| 6044 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6045 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6046 | if (cur_pos + offset < last_byte) { |
| 6047 | ret = -ENOENT; |
| 6048 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6049 | } |
| 6050 | ret = 0; |
| 6051 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6052 | btrfs_free_path(path); |
| 6053 | if (ret) { |
| 6054 | if (exts != *extents) |
| 6055 | kfree(exts); |
| 6056 | } else { |
| 6057 | *extents = exts; |
| 6058 | *nr_extents = nr; |
| 6059 | } |
| 6060 | return ret; |
| 6061 | } |
| 6062 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6063 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6064 | struct btrfs_root *root, |
| 6065 | struct btrfs_path *path, |
| 6066 | struct btrfs_key *extent_key, |
| 6067 | struct btrfs_key *leaf_key, |
| 6068 | struct btrfs_ref_path *ref_path, |
| 6069 | struct disk_extent *new_extents, |
| 6070 | int nr_extents) |
| 6071 | { |
| 6072 | struct extent_buffer *leaf; |
| 6073 | struct btrfs_file_extent_item *fi; |
| 6074 | struct inode *inode = NULL; |
| 6075 | struct btrfs_key key; |
| 6076 | u64 lock_start = 0; |
| 6077 | u64 lock_end = 0; |
| 6078 | u64 num_bytes; |
| 6079 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6080 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6081 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6082 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6083 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6084 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6085 | int ret; |
| 6086 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6087 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6088 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6089 | if (key.objectid < ref_path->owner_objectid || |
| 6090 | (key.objectid == ref_path->owner_objectid && |
| 6091 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6092 | key.objectid = ref_path->owner_objectid; |
| 6093 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6094 | key.offset = 0; |
| 6095 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6096 | } |
| 6097 | |
| 6098 | while (1) { |
| 6099 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6100 | if (ret < 0) |
| 6101 | goto out; |
| 6102 | |
| 6103 | leaf = path->nodes[0]; |
| 6104 | nritems = btrfs_header_nritems(leaf); |
| 6105 | next: |
| 6106 | if (extent_locked && ret > 0) { |
| 6107 | /* |
| 6108 | * the file extent item was modified by someone |
| 6109 | * before the extent got locked. |
| 6110 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6111 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6112 | lock_end, GFP_NOFS); |
| 6113 | extent_locked = 0; |
| 6114 | } |
| 6115 | |
| 6116 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6117 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6118 | break; |
| 6119 | |
| 6120 | BUG_ON(extent_locked); |
| 6121 | ret = btrfs_next_leaf(root, path); |
| 6122 | if (ret < 0) |
| 6123 | goto out; |
| 6124 | if (ret > 0) |
| 6125 | break; |
| 6126 | leaf = path->nodes[0]; |
| 6127 | nritems = btrfs_header_nritems(leaf); |
| 6128 | } |
| 6129 | |
| 6130 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 6131 | |
| 6132 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 6133 | if ((key.objectid > ref_path->owner_objectid) || |
| 6134 | (key.objectid == ref_path->owner_objectid && |
| 6135 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6136 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6137 | break; |
| 6138 | } |
| 6139 | |
| 6140 | if (inode && key.objectid != inode->i_ino) { |
| 6141 | BUG_ON(extent_locked); |
| 6142 | btrfs_release_path(root, path); |
| 6143 | mutex_unlock(&inode->i_mutex); |
| 6144 | iput(inode); |
| 6145 | inode = NULL; |
| 6146 | continue; |
| 6147 | } |
| 6148 | |
| 6149 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6150 | path->slots[0]++; |
| 6151 | ret = 1; |
| 6152 | goto next; |
| 6153 | } |
| 6154 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6155 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6156 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 6157 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 6158 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6159 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 6160 | extent_key->objectid)) { |
| 6161 | path->slots[0]++; |
| 6162 | ret = 1; |
| 6163 | goto next; |
| 6164 | } |
| 6165 | |
| 6166 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6167 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 6168 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6169 | if (search_end == (u64)-1) { |
| 6170 | search_end = key.offset - ext_offset + |
| 6171 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 6172 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6173 | |
| 6174 | if (!extent_locked) { |
| 6175 | lock_start = key.offset; |
| 6176 | lock_end = lock_start + num_bytes - 1; |
| 6177 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6178 | if (lock_start > key.offset || |
| 6179 | lock_end + 1 < key.offset + num_bytes) { |
| 6180 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6181 | lock_start, lock_end, GFP_NOFS); |
| 6182 | extent_locked = 0; |
| 6183 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | if (!inode) { |
| 6187 | btrfs_release_path(root, path); |
| 6188 | |
| 6189 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 6190 | key.objectid, root); |
| 6191 | if (inode->i_state & I_NEW) { |
| 6192 | BTRFS_I(inode)->root = root; |
| 6193 | BTRFS_I(inode)->location.objectid = |
| 6194 | key.objectid; |
| 6195 | BTRFS_I(inode)->location.type = |
| 6196 | BTRFS_INODE_ITEM_KEY; |
| 6197 | BTRFS_I(inode)->location.offset = 0; |
| 6198 | btrfs_read_locked_inode(inode); |
| 6199 | unlock_new_inode(inode); |
| 6200 | } |
| 6201 | /* |
| 6202 | * some code call btrfs_commit_transaction while |
| 6203 | * holding the i_mutex, so we can't use mutex_lock |
| 6204 | * here. |
| 6205 | */ |
| 6206 | if (is_bad_inode(inode) || |
| 6207 | !mutex_trylock(&inode->i_mutex)) { |
| 6208 | iput(inode); |
| 6209 | inode = NULL; |
| 6210 | key.offset = (u64)-1; |
| 6211 | goto skip; |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | if (!extent_locked) { |
| 6216 | struct btrfs_ordered_extent *ordered; |
| 6217 | |
| 6218 | btrfs_release_path(root, path); |
| 6219 | |
| 6220 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6221 | lock_end, GFP_NOFS); |
| 6222 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6223 | lock_end); |
| 6224 | if (ordered && |
| 6225 | ordered->file_offset <= lock_end && |
| 6226 | ordered->file_offset + ordered->len > lock_start) { |
| 6227 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6228 | lock_start, lock_end, GFP_NOFS); |
| 6229 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6230 | btrfs_put_ordered_extent(ordered); |
| 6231 | key.offset += num_bytes; |
| 6232 | goto skip; |
| 6233 | } |
| 6234 | if (ordered) |
| 6235 | btrfs_put_ordered_extent(ordered); |
| 6236 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6237 | extent_locked = 1; |
| 6238 | continue; |
| 6239 | } |
| 6240 | |
| 6241 | if (nr_extents == 1) { |
| 6242 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6243 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6244 | new_extents[0].disk_bytenr); |
| 6245 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6246 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6247 | btrfs_mark_buffer_dirty(leaf); |
| 6248 | |
| 6249 | btrfs_drop_extent_cache(inode, key.offset, |
| 6250 | key.offset + num_bytes - 1, 0); |
| 6251 | |
| 6252 | ret = btrfs_inc_extent_ref(trans, root, |
| 6253 | new_extents[0].disk_bytenr, |
| 6254 | new_extents[0].disk_num_bytes, |
| 6255 | leaf->start, |
| 6256 | root->root_key.objectid, |
| 6257 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6258 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6259 | BUG_ON(ret); |
| 6260 | |
| 6261 | ret = btrfs_free_extent(trans, root, |
| 6262 | extent_key->objectid, |
| 6263 | extent_key->offset, |
| 6264 | leaf->start, |
| 6265 | btrfs_header_owner(leaf), |
| 6266 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6267 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6268 | BUG_ON(ret); |
| 6269 | |
| 6270 | btrfs_release_path(root, path); |
| 6271 | key.offset += num_bytes; |
| 6272 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6273 | BUG_ON(1); |
| 6274 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6275 | u64 alloc_hint; |
| 6276 | u64 extent_len; |
| 6277 | int i; |
| 6278 | /* |
| 6279 | * drop old extent pointer at first, then insert the |
| 6280 | * new pointers one bye one |
| 6281 | */ |
| 6282 | btrfs_release_path(root, path); |
| 6283 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 6284 | key.offset + num_bytes, |
| 6285 | key.offset, &alloc_hint); |
| 6286 | BUG_ON(ret); |
| 6287 | |
| 6288 | for (i = 0; i < nr_extents; i++) { |
| 6289 | if (ext_offset >= new_extents[i].num_bytes) { |
| 6290 | ext_offset -= new_extents[i].num_bytes; |
| 6291 | continue; |
| 6292 | } |
| 6293 | extent_len = min(new_extents[i].num_bytes - |
| 6294 | ext_offset, num_bytes); |
| 6295 | |
| 6296 | ret = btrfs_insert_empty_item(trans, root, |
| 6297 | path, &key, |
| 6298 | sizeof(*fi)); |
| 6299 | BUG_ON(ret); |
| 6300 | |
| 6301 | leaf = path->nodes[0]; |
| 6302 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6303 | struct btrfs_file_extent_item); |
| 6304 | btrfs_set_file_extent_generation(leaf, fi, |
| 6305 | trans->transid); |
| 6306 | btrfs_set_file_extent_type(leaf, fi, |
| 6307 | BTRFS_FILE_EXTENT_REG); |
| 6308 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6309 | new_extents[i].disk_bytenr); |
| 6310 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6311 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6312 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 6313 | new_extents[i].ram_bytes); |
| 6314 | |
| 6315 | btrfs_set_file_extent_compression(leaf, fi, |
| 6316 | new_extents[i].compression); |
| 6317 | btrfs_set_file_extent_encryption(leaf, fi, |
| 6318 | new_extents[i].encryption); |
| 6319 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 6320 | new_extents[i].other_encoding); |
| 6321 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6322 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 6323 | extent_len); |
| 6324 | ext_offset += new_extents[i].offset; |
| 6325 | btrfs_set_file_extent_offset(leaf, fi, |
| 6326 | ext_offset); |
| 6327 | btrfs_mark_buffer_dirty(leaf); |
| 6328 | |
| 6329 | btrfs_drop_extent_cache(inode, key.offset, |
| 6330 | key.offset + extent_len - 1, 0); |
| 6331 | |
| 6332 | ret = btrfs_inc_extent_ref(trans, root, |
| 6333 | new_extents[i].disk_bytenr, |
| 6334 | new_extents[i].disk_num_bytes, |
| 6335 | leaf->start, |
| 6336 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6337 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6338 | BUG_ON(ret); |
| 6339 | btrfs_release_path(root, path); |
| 6340 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6341 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6342 | |
| 6343 | ext_offset = 0; |
| 6344 | num_bytes -= extent_len; |
| 6345 | key.offset += extent_len; |
| 6346 | |
| 6347 | if (num_bytes == 0) |
| 6348 | break; |
| 6349 | } |
| 6350 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6351 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6355 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6356 | lock_end, GFP_NOFS); |
| 6357 | extent_locked = 0; |
| 6358 | } |
| 6359 | skip: |
| 6360 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6361 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6362 | break; |
| 6363 | |
| 6364 | cond_resched(); |
| 6365 | } |
| 6366 | ret = 0; |
| 6367 | out: |
| 6368 | btrfs_release_path(root, path); |
| 6369 | if (inode) { |
| 6370 | mutex_unlock(&inode->i_mutex); |
| 6371 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6372 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6373 | lock_end, GFP_NOFS); |
| 6374 | } |
| 6375 | iput(inode); |
| 6376 | } |
| 6377 | return ret; |
| 6378 | } |
| 6379 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6380 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 6381 | struct btrfs_root *root, |
| 6382 | struct extent_buffer *buf, u64 orig_start) |
| 6383 | { |
| 6384 | int level; |
| 6385 | int ret; |
| 6386 | |
| 6387 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 6388 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6389 | |
| 6390 | level = btrfs_header_level(buf); |
| 6391 | if (level == 0) { |
| 6392 | struct btrfs_leaf_ref *ref; |
| 6393 | struct btrfs_leaf_ref *orig_ref; |
| 6394 | |
| 6395 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 6396 | if (!orig_ref) |
| 6397 | return -ENOENT; |
| 6398 | |
| 6399 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 6400 | if (!ref) { |
| 6401 | btrfs_free_leaf_ref(root, orig_ref); |
| 6402 | return -ENOMEM; |
| 6403 | } |
| 6404 | |
| 6405 | ref->nritems = orig_ref->nritems; |
| 6406 | memcpy(ref->extents, orig_ref->extents, |
| 6407 | sizeof(ref->extents[0]) * ref->nritems); |
| 6408 | |
| 6409 | btrfs_free_leaf_ref(root, orig_ref); |
| 6410 | |
| 6411 | ref->root_gen = trans->transid; |
| 6412 | ref->bytenr = buf->start; |
| 6413 | ref->owner = btrfs_header_owner(buf); |
| 6414 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6415 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6416 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 6417 | WARN_ON(ret); |
| 6418 | btrfs_free_leaf_ref(root, ref); |
| 6419 | } |
| 6420 | return 0; |
| 6421 | } |
| 6422 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6423 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6424 | struct extent_buffer *leaf, |
| 6425 | struct btrfs_block_group_cache *group, |
| 6426 | struct btrfs_root *target_root) |
| 6427 | { |
| 6428 | struct btrfs_key key; |
| 6429 | struct inode *inode = NULL; |
| 6430 | struct btrfs_file_extent_item *fi; |
| 6431 | u64 num_bytes; |
| 6432 | u64 skip_objectid = 0; |
| 6433 | u32 nritems; |
| 6434 | u32 i; |
| 6435 | |
| 6436 | nritems = btrfs_header_nritems(leaf); |
| 6437 | for (i = 0; i < nritems; i++) { |
| 6438 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6439 | if (key.objectid == skip_objectid || |
| 6440 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 6441 | continue; |
| 6442 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6443 | if (btrfs_file_extent_type(leaf, fi) == |
| 6444 | BTRFS_FILE_EXTENT_INLINE) |
| 6445 | continue; |
| 6446 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6447 | continue; |
| 6448 | if (!inode || inode->i_ino != key.objectid) { |
| 6449 | iput(inode); |
| 6450 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 6451 | key.objectid, target_root, 1); |
| 6452 | } |
| 6453 | if (!inode) { |
| 6454 | skip_objectid = key.objectid; |
| 6455 | continue; |
| 6456 | } |
| 6457 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6458 | |
| 6459 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 6460 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6461 | btrfs_drop_extent_cache(inode, key.offset, |
| 6462 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6463 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 6464 | key.offset + num_bytes - 1, GFP_NOFS); |
| 6465 | cond_resched(); |
| 6466 | } |
| 6467 | iput(inode); |
| 6468 | return 0; |
| 6469 | } |
| 6470 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6471 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6472 | struct btrfs_root *root, |
| 6473 | struct extent_buffer *leaf, |
| 6474 | struct btrfs_block_group_cache *group, |
| 6475 | struct inode *reloc_inode) |
| 6476 | { |
| 6477 | struct btrfs_key key; |
| 6478 | struct btrfs_key extent_key; |
| 6479 | struct btrfs_file_extent_item *fi; |
| 6480 | struct btrfs_leaf_ref *ref; |
| 6481 | struct disk_extent *new_extent; |
| 6482 | u64 bytenr; |
| 6483 | u64 num_bytes; |
| 6484 | u32 nritems; |
| 6485 | u32 i; |
| 6486 | int ext_index; |
| 6487 | int nr_extent; |
| 6488 | int ret; |
| 6489 | |
| 6490 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 6491 | BUG_ON(!new_extent); |
| 6492 | |
| 6493 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 6494 | BUG_ON(!ref); |
| 6495 | |
| 6496 | ext_index = -1; |
| 6497 | nritems = btrfs_header_nritems(leaf); |
| 6498 | for (i = 0; i < nritems; i++) { |
| 6499 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6500 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 6501 | continue; |
| 6502 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6503 | if (btrfs_file_extent_type(leaf, fi) == |
| 6504 | BTRFS_FILE_EXTENT_INLINE) |
| 6505 | continue; |
| 6506 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6507 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6508 | if (bytenr == 0) |
| 6509 | continue; |
| 6510 | |
| 6511 | ext_index++; |
| 6512 | if (bytenr >= group->key.objectid + group->key.offset || |
| 6513 | bytenr + num_bytes <= group->key.objectid) |
| 6514 | continue; |
| 6515 | |
| 6516 | extent_key.objectid = bytenr; |
| 6517 | extent_key.offset = num_bytes; |
| 6518 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6519 | nr_extent = 1; |
| 6520 | ret = get_new_locations(reloc_inode, &extent_key, |
| 6521 | group->key.objectid, 1, |
| 6522 | &new_extent, &nr_extent); |
| 6523 | if (ret > 0) |
| 6524 | continue; |
| 6525 | BUG_ON(ret < 0); |
| 6526 | |
| 6527 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 6528 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 6529 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 6530 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 6531 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6532 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6533 | new_extent->disk_bytenr); |
| 6534 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6535 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6536 | btrfs_mark_buffer_dirty(leaf); |
| 6537 | |
| 6538 | ret = btrfs_inc_extent_ref(trans, root, |
| 6539 | new_extent->disk_bytenr, |
| 6540 | new_extent->disk_num_bytes, |
| 6541 | leaf->start, |
| 6542 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6543 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6544 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6545 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6546 | ret = btrfs_free_extent(trans, root, |
| 6547 | bytenr, num_bytes, leaf->start, |
| 6548 | btrfs_header_owner(leaf), |
| 6549 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6550 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6551 | BUG_ON(ret); |
| 6552 | cond_resched(); |
| 6553 | } |
| 6554 | kfree(new_extent); |
| 6555 | BUG_ON(ext_index + 1 != ref->nritems); |
| 6556 | btrfs_free_leaf_ref(root, ref); |
| 6557 | return 0; |
| 6558 | } |
| 6559 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6560 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 6561 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6562 | { |
| 6563 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6564 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6565 | |
| 6566 | if (root->reloc_root) { |
| 6567 | reloc_root = root->reloc_root; |
| 6568 | root->reloc_root = NULL; |
| 6569 | list_add(&reloc_root->dead_list, |
| 6570 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6571 | |
| 6572 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 6573 | reloc_root->node->start); |
| 6574 | btrfs_set_root_level(&root->root_item, |
| 6575 | btrfs_header_level(reloc_root->node)); |
| 6576 | memset(&reloc_root->root_item.drop_progress, 0, |
| 6577 | sizeof(struct btrfs_disk_key)); |
| 6578 | reloc_root->root_item.drop_level = 0; |
| 6579 | |
| 6580 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 6581 | &reloc_root->root_key, |
| 6582 | &reloc_root->root_item); |
| 6583 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6584 | } |
| 6585 | return 0; |
| 6586 | } |
| 6587 | |
| 6588 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 6589 | { |
| 6590 | struct btrfs_trans_handle *trans; |
| 6591 | struct btrfs_root *reloc_root; |
| 6592 | struct btrfs_root *prev_root = NULL; |
| 6593 | struct list_head dead_roots; |
| 6594 | int ret; |
| 6595 | unsigned long nr; |
| 6596 | |
| 6597 | INIT_LIST_HEAD(&dead_roots); |
| 6598 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 6599 | |
| 6600 | while (!list_empty(&dead_roots)) { |
| 6601 | reloc_root = list_entry(dead_roots.prev, |
| 6602 | struct btrfs_root, dead_list); |
| 6603 | list_del_init(&reloc_root->dead_list); |
| 6604 | |
| 6605 | BUG_ON(reloc_root->commit_root != NULL); |
| 6606 | while (1) { |
| 6607 | trans = btrfs_join_transaction(root, 1); |
| 6608 | BUG_ON(!trans); |
| 6609 | |
| 6610 | mutex_lock(&root->fs_info->drop_mutex); |
| 6611 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 6612 | if (ret != -EAGAIN) |
| 6613 | break; |
| 6614 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6615 | |
| 6616 | nr = trans->blocks_used; |
| 6617 | ret = btrfs_end_transaction(trans, root); |
| 6618 | BUG_ON(ret); |
| 6619 | btrfs_btree_balance_dirty(root, nr); |
| 6620 | } |
| 6621 | |
| 6622 | free_extent_buffer(reloc_root->node); |
| 6623 | |
| 6624 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 6625 | &reloc_root->root_key); |
| 6626 | BUG_ON(ret); |
| 6627 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6628 | |
| 6629 | nr = trans->blocks_used; |
| 6630 | ret = btrfs_end_transaction(trans, root); |
| 6631 | BUG_ON(ret); |
| 6632 | btrfs_btree_balance_dirty(root, nr); |
| 6633 | |
| 6634 | kfree(prev_root); |
| 6635 | prev_root = reloc_root; |
| 6636 | } |
| 6637 | if (prev_root) { |
| 6638 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 6639 | kfree(prev_root); |
| 6640 | } |
| 6641 | return 0; |
| 6642 | } |
| 6643 | |
| 6644 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 6645 | { |
| 6646 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 6647 | return 0; |
| 6648 | } |
| 6649 | |
| 6650 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 6651 | { |
| 6652 | struct btrfs_root *reloc_root; |
| 6653 | struct btrfs_trans_handle *trans; |
| 6654 | struct btrfs_key location; |
| 6655 | int found; |
| 6656 | int ret; |
| 6657 | |
| 6658 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6659 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 6660 | BUG_ON(ret); |
| 6661 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 6662 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 6663 | |
| 6664 | if (found) { |
| 6665 | trans = btrfs_start_transaction(root, 1); |
| 6666 | BUG_ON(!trans); |
| 6667 | ret = btrfs_commit_transaction(trans, root); |
| 6668 | BUG_ON(ret); |
| 6669 | } |
| 6670 | |
| 6671 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6672 | location.offset = (u64)-1; |
| 6673 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 6674 | |
| 6675 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 6676 | BUG_ON(!reloc_root); |
| 6677 | btrfs_orphan_cleanup(reloc_root); |
| 6678 | return 0; |
| 6679 | } |
| 6680 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6681 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6682 | struct btrfs_root *root) |
| 6683 | { |
| 6684 | struct btrfs_root *reloc_root; |
| 6685 | struct extent_buffer *eb; |
| 6686 | struct btrfs_root_item *root_item; |
| 6687 | struct btrfs_key root_key; |
| 6688 | int ret; |
| 6689 | |
| 6690 | BUG_ON(!root->ref_cows); |
| 6691 | if (root->reloc_root) |
| 6692 | return 0; |
| 6693 | |
| 6694 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 6695 | BUG_ON(!root_item); |
| 6696 | |
| 6697 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 6698 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 6699 | BUG_ON(ret); |
| 6700 | |
| 6701 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 6702 | root_key.offset = root->root_key.objectid; |
| 6703 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6704 | |
| 6705 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 6706 | btrfs_set_root_refs(root_item, 0); |
| 6707 | btrfs_set_root_bytenr(root_item, eb->start); |
| 6708 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6709 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6710 | |
| 6711 | btrfs_tree_unlock(eb); |
| 6712 | free_extent_buffer(eb); |
| 6713 | |
| 6714 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 6715 | &root_key, root_item); |
| 6716 | BUG_ON(ret); |
| 6717 | kfree(root_item); |
| 6718 | |
| 6719 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 6720 | &root_key); |
| 6721 | BUG_ON(!reloc_root); |
| 6722 | reloc_root->last_trans = trans->transid; |
| 6723 | reloc_root->commit_root = NULL; |
| 6724 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 6725 | |
| 6726 | root->reloc_root = reloc_root; |
| 6727 | return 0; |
| 6728 | } |
| 6729 | |
| 6730 | /* |
| 6731 | * Core function of space balance. |
| 6732 | * |
| 6733 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6734 | * counted roots. There is one reloc tree for each subvol, and all |
| 6735 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 6736 | * of the latest committed roots of subvols (root->commit_root). |
| 6737 | * |
| 6738 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 6739 | * COW the block through subvol's reloc tree, then update block pointer |
| 6740 | * in the subvol to point to the new block. Since all reloc trees share |
| 6741 | * same root key objectid, doing special handing for tree blocks owned |
| 6742 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 6743 | * we can use the resulting new block directly when the same block is |
| 6744 | * required to COW again through other reloc trees. By this way, relocated |
| 6745 | * tree blocks are shared between reloc trees, so they are also shared |
| 6746 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6747 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6748 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6749 | struct btrfs_root *root, |
| 6750 | struct btrfs_path *path, |
| 6751 | struct btrfs_key *first_key, |
| 6752 | struct btrfs_ref_path *ref_path, |
| 6753 | struct btrfs_block_group_cache *group, |
| 6754 | struct inode *reloc_inode) |
| 6755 | { |
| 6756 | struct btrfs_root *reloc_root; |
| 6757 | struct extent_buffer *eb = NULL; |
| 6758 | struct btrfs_key *keys; |
| 6759 | u64 *nodes; |
| 6760 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6761 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6762 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6763 | int ret; |
| 6764 | |
| 6765 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 6766 | lowest_level = ref_path->owner_objectid; |
| 6767 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6768 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6769 | path->lowest_level = lowest_level; |
| 6770 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 6771 | BUG_ON(ret < 0); |
| 6772 | path->lowest_level = 0; |
| 6773 | btrfs_release_path(root, path); |
| 6774 | return 0; |
| 6775 | } |
| 6776 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6777 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6778 | ret = init_reloc_tree(trans, root); |
| 6779 | BUG_ON(ret); |
| 6780 | reloc_root = root->reloc_root; |
| 6781 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6782 | shared_level = ref_path->shared_level; |
| 6783 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6784 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6785 | keys = ref_path->node_keys; |
| 6786 | nodes = ref_path->new_nodes; |
| 6787 | memset(&keys[shared_level + 1], 0, |
| 6788 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 6789 | memset(&nodes[shared_level + 1], 0, |
| 6790 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6791 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6792 | if (nodes[lowest_level] == 0) { |
| 6793 | path->lowest_level = lowest_level; |
| 6794 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6795 | 0, 1); |
| 6796 | BUG_ON(ret); |
| 6797 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 6798 | eb = path->nodes[level]; |
| 6799 | if (!eb || eb == reloc_root->node) |
| 6800 | break; |
| 6801 | nodes[level] = eb->start; |
| 6802 | if (level == 0) |
| 6803 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 6804 | else |
| 6805 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 6806 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6807 | if (nodes[0] && |
| 6808 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6809 | eb = path->nodes[0]; |
| 6810 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 6811 | group, reloc_inode); |
| 6812 | BUG_ON(ret); |
| 6813 | } |
| 6814 | btrfs_release_path(reloc_root, path); |
| 6815 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6816 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6817 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6818 | BUG_ON(ret); |
| 6819 | } |
| 6820 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6821 | /* |
| 6822 | * replace tree blocks in the fs tree with tree blocks in |
| 6823 | * the reloc tree. |
| 6824 | */ |
| 6825 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 6826 | BUG_ON(ret < 0); |
| 6827 | |
| 6828 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6829 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6830 | 0, 0); |
| 6831 | BUG_ON(ret); |
| 6832 | extent_buffer_get(path->nodes[0]); |
| 6833 | eb = path->nodes[0]; |
| 6834 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6835 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 6836 | BUG_ON(ret); |
| 6837 | free_extent_buffer(eb); |
| 6838 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6839 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6840 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6841 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6842 | return 0; |
| 6843 | } |
| 6844 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6845 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6846 | struct btrfs_root *root, |
| 6847 | struct btrfs_path *path, |
| 6848 | struct btrfs_key *first_key, |
| 6849 | struct btrfs_ref_path *ref_path) |
| 6850 | { |
| 6851 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6852 | |
| 6853 | ret = relocate_one_path(trans, root, path, first_key, |
| 6854 | ref_path, NULL, NULL); |
| 6855 | BUG_ON(ret); |
| 6856 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6857 | return 0; |
| 6858 | } |
| 6859 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6860 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6861 | struct btrfs_root *extent_root, |
| 6862 | struct btrfs_path *path, |
| 6863 | struct btrfs_key *extent_key) |
| 6864 | { |
| 6865 | int ret; |
| 6866 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6867 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 6868 | if (ret) |
| 6869 | goto out; |
| 6870 | ret = btrfs_del_item(trans, extent_root, path); |
| 6871 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6872 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6873 | return ret; |
| 6874 | } |
| 6875 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6876 | 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] | 6877 | struct btrfs_ref_path *ref_path) |
| 6878 | { |
| 6879 | struct btrfs_key root_key; |
| 6880 | |
| 6881 | root_key.objectid = ref_path->root_objectid; |
| 6882 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6883 | if (is_cowonly_root(ref_path->root_objectid)) |
| 6884 | root_key.offset = 0; |
| 6885 | else |
| 6886 | root_key.offset = (u64)-1; |
| 6887 | |
| 6888 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 6889 | } |
| 6890 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6891 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6892 | struct btrfs_path *path, |
| 6893 | struct btrfs_key *extent_key, |
| 6894 | struct btrfs_block_group_cache *group, |
| 6895 | struct inode *reloc_inode, int pass) |
| 6896 | { |
| 6897 | struct btrfs_trans_handle *trans; |
| 6898 | struct btrfs_root *found_root; |
| 6899 | struct btrfs_ref_path *ref_path = NULL; |
| 6900 | struct disk_extent *new_extents = NULL; |
| 6901 | int nr_extents = 0; |
| 6902 | int loops; |
| 6903 | int ret; |
| 6904 | int level; |
| 6905 | struct btrfs_key first_key; |
| 6906 | u64 prev_block = 0; |
| 6907 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6908 | |
| 6909 | trans = btrfs_start_transaction(extent_root, 1); |
| 6910 | BUG_ON(!trans); |
| 6911 | |
| 6912 | if (extent_key->objectid == 0) { |
| 6913 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 6914 | goto out; |
| 6915 | } |
| 6916 | |
| 6917 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 6918 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6919 | ret = -ENOMEM; |
| 6920 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | for (loops = 0; ; loops++) { |
| 6924 | if (loops == 0) { |
| 6925 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 6926 | extent_key->objectid); |
| 6927 | } else { |
| 6928 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 6929 | } |
| 6930 | if (ret < 0) |
| 6931 | goto out; |
| 6932 | if (ret > 0) |
| 6933 | break; |
| 6934 | |
| 6935 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6936 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 6937 | continue; |
| 6938 | |
| 6939 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 6940 | BUG_ON(!found_root); |
| 6941 | /* |
| 6942 | * for reference counted tree, only process reference paths |
| 6943 | * rooted at the latest committed root. |
| 6944 | */ |
| 6945 | if (found_root->ref_cows && |
| 6946 | ref_path->root_generation != found_root->root_key.offset) |
| 6947 | continue; |
| 6948 | |
| 6949 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 6950 | if (pass == 0) { |
| 6951 | /* |
| 6952 | * copy data extents to new locations |
| 6953 | */ |
| 6954 | u64 group_start = group->key.objectid; |
| 6955 | ret = relocate_data_extent(reloc_inode, |
| 6956 | extent_key, |
| 6957 | group_start); |
| 6958 | if (ret < 0) |
| 6959 | goto out; |
| 6960 | break; |
| 6961 | } |
| 6962 | level = 0; |
| 6963 | } else { |
| 6964 | level = ref_path->owner_objectid; |
| 6965 | } |
| 6966 | |
| 6967 | if (prev_block != ref_path->nodes[level]) { |
| 6968 | struct extent_buffer *eb; |
| 6969 | u64 block_start = ref_path->nodes[level]; |
| 6970 | u64 block_size = btrfs_level_size(found_root, level); |
| 6971 | |
| 6972 | eb = read_tree_block(found_root, block_start, |
| 6973 | block_size, 0); |
| 6974 | btrfs_tree_lock(eb); |
| 6975 | BUG_ON(level != btrfs_header_level(eb)); |
| 6976 | |
| 6977 | if (level == 0) |
| 6978 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 6979 | else |
| 6980 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 6981 | |
| 6982 | btrfs_tree_unlock(eb); |
| 6983 | free_extent_buffer(eb); |
| 6984 | prev_block = block_start; |
| 6985 | } |
| 6986 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 6987 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6988 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 6989 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6990 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 6991 | /* |
| 6992 | * try to update data extent references while |
| 6993 | * keeping metadata shared between snapshots. |
| 6994 | */ |
| 6995 | if (pass == 1) { |
| 6996 | ret = relocate_one_path(trans, found_root, |
| 6997 | path, &first_key, ref_path, |
| 6998 | group, reloc_inode); |
| 6999 | if (ret < 0) |
| 7000 | goto out; |
| 7001 | continue; |
| 7002 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7003 | /* |
| 7004 | * use fallback method to process the remaining |
| 7005 | * references. |
| 7006 | */ |
| 7007 | if (!new_extents) { |
| 7008 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7009 | new_extents = kmalloc(sizeof(*new_extents), |
| 7010 | GFP_NOFS); |
| 7011 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7012 | ret = get_new_locations(reloc_inode, |
| 7013 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7014 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7015 | &new_extents, |
| 7016 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7017 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7018 | goto out; |
| 7019 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7020 | ret = replace_one_extent(trans, found_root, |
| 7021 | path, extent_key, |
| 7022 | &first_key, ref_path, |
| 7023 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7024 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7025 | ret = relocate_tree_block(trans, found_root, path, |
| 7026 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7027 | } |
| 7028 | if (ret < 0) |
| 7029 | goto out; |
| 7030 | } |
| 7031 | ret = 0; |
| 7032 | out: |
| 7033 | btrfs_end_transaction(trans, extent_root); |
| 7034 | kfree(new_extents); |
| 7035 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7036 | return ret; |
| 7037 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7038 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7039 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7040 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7041 | { |
| 7042 | u64 num_devices; |
| 7043 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7044 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7045 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7046 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7047 | if (num_devices == 1) { |
| 7048 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7049 | stripped = flags & ~stripped; |
| 7050 | |
| 7051 | /* turn raid0 into single device chunks */ |
| 7052 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7053 | return stripped; |
| 7054 | |
| 7055 | /* turn mirroring into duplication */ |
| 7056 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7057 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7058 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7059 | return flags; |
| 7060 | } else { |
| 7061 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7062 | if (flags & stripped) |
| 7063 | return flags; |
| 7064 | |
| 7065 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7066 | stripped = flags & ~stripped; |
| 7067 | |
| 7068 | /* switch duplicated blocks with raid1 */ |
| 7069 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7070 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7071 | |
| 7072 | /* turn single device chunks into raid0 */ |
| 7073 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7074 | } |
| 7075 | return flags; |
| 7076 | } |
| 7077 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7078 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7079 | struct btrfs_block_group_cache *shrink_block_group, |
| 7080 | int force) |
| 7081 | { |
| 7082 | struct btrfs_trans_handle *trans; |
| 7083 | u64 new_alloc_flags; |
| 7084 | u64 calc; |
| 7085 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7086 | spin_lock(&shrink_block_group->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7087 | if (btrfs_block_group_used(&shrink_block_group->item) + |
| 7088 | shrink_block_group->reserved > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7089 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7090 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7091 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7092 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7093 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7094 | new_alloc_flags = update_block_group_flags(root, |
| 7095 | shrink_block_group->flags); |
| 7096 | if (new_alloc_flags != shrink_block_group->flags) { |
| 7097 | calc = |
| 7098 | btrfs_block_group_used(&shrink_block_group->item); |
| 7099 | } else { |
| 7100 | calc = shrink_block_group->key.offset; |
| 7101 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7102 | spin_unlock(&shrink_block_group->lock); |
| 7103 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7104 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 7105 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7106 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7107 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7108 | } else |
| 7109 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7110 | return 0; |
| 7111 | } |
| 7112 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7113 | |
| 7114 | int btrfs_prepare_block_group_relocation(struct btrfs_root *root, |
| 7115 | struct btrfs_block_group_cache *group) |
| 7116 | |
| 7117 | { |
| 7118 | __alloc_chunk_for_shrink(root, group, 1); |
| 7119 | set_block_group_readonly(group); |
| 7120 | return 0; |
| 7121 | } |
| 7122 | |
| 7123 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7124 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 7125 | struct btrfs_root *root, |
| 7126 | u64 objectid, u64 size) |
| 7127 | { |
| 7128 | struct btrfs_path *path; |
| 7129 | struct btrfs_inode_item *item; |
| 7130 | struct extent_buffer *leaf; |
| 7131 | int ret; |
| 7132 | |
| 7133 | path = btrfs_alloc_path(); |
| 7134 | if (!path) |
| 7135 | return -ENOMEM; |
| 7136 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 7137 | path->leave_spinning = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7138 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 7139 | if (ret) |
| 7140 | goto out; |
| 7141 | |
| 7142 | leaf = path->nodes[0]; |
| 7143 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 7144 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 7145 | btrfs_set_inode_generation(leaf, item, 1); |
| 7146 | btrfs_set_inode_size(leaf, item, size); |
| 7147 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 7148 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7149 | btrfs_mark_buffer_dirty(leaf); |
| 7150 | btrfs_release_path(root, path); |
| 7151 | out: |
| 7152 | btrfs_free_path(path); |
| 7153 | return ret; |
| 7154 | } |
| 7155 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7156 | static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7157 | struct btrfs_block_group_cache *group) |
| 7158 | { |
| 7159 | struct inode *inode = NULL; |
| 7160 | struct btrfs_trans_handle *trans; |
| 7161 | struct btrfs_root *root; |
| 7162 | struct btrfs_key root_key; |
| 7163 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 7164 | int err = 0; |
| 7165 | |
| 7166 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 7167 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7168 | root_key.offset = (u64)-1; |
| 7169 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7170 | if (IS_ERR(root)) |
| 7171 | return ERR_CAST(root); |
| 7172 | |
| 7173 | trans = btrfs_start_transaction(root, 1); |
| 7174 | BUG_ON(!trans); |
| 7175 | |
| 7176 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 7177 | if (err) |
| 7178 | goto out; |
| 7179 | |
| 7180 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 7181 | BUG_ON(err); |
| 7182 | |
| 7183 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7184 | group->key.offset, 0, group->key.offset, |
| 7185 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7186 | BUG_ON(err); |
| 7187 | |
| 7188 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 7189 | if (inode->i_state & I_NEW) { |
| 7190 | BTRFS_I(inode)->root = root; |
| 7191 | BTRFS_I(inode)->location.objectid = objectid; |
| 7192 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 7193 | BTRFS_I(inode)->location.offset = 0; |
| 7194 | btrfs_read_locked_inode(inode); |
| 7195 | unlock_new_inode(inode); |
| 7196 | BUG_ON(is_bad_inode(inode)); |
| 7197 | } else { |
| 7198 | BUG_ON(1); |
| 7199 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 7200 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7201 | |
| 7202 | err = btrfs_orphan_add(trans, inode); |
| 7203 | out: |
| 7204 | btrfs_end_transaction(trans, root); |
| 7205 | if (err) { |
| 7206 | if (inode) |
| 7207 | iput(inode); |
| 7208 | inode = ERR_PTR(err); |
| 7209 | } |
| 7210 | return inode; |
| 7211 | } |
| 7212 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 7213 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
| 7214 | { |
| 7215 | |
| 7216 | struct btrfs_ordered_sum *sums; |
| 7217 | struct btrfs_sector_sum *sector_sum; |
| 7218 | struct btrfs_ordered_extent *ordered; |
| 7219 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 7220 | struct list_head list; |
| 7221 | size_t offset; |
| 7222 | int ret; |
| 7223 | u64 disk_bytenr; |
| 7224 | |
| 7225 | INIT_LIST_HEAD(&list); |
| 7226 | |
| 7227 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); |
| 7228 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); |
| 7229 | |
| 7230 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 7231 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 7232 | disk_bytenr + len - 1, &list); |
| 7233 | |
| 7234 | while (!list_empty(&list)) { |
| 7235 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 7236 | list_del_init(&sums->list); |
| 7237 | |
| 7238 | sector_sum = sums->sums; |
| 7239 | sums->bytenr = ordered->start; |
| 7240 | |
| 7241 | offset = 0; |
| 7242 | while (offset < sums->len) { |
| 7243 | sector_sum->bytenr += ordered->start - disk_bytenr; |
| 7244 | sector_sum++; |
| 7245 | offset += root->sectorsize; |
| 7246 | } |
| 7247 | |
| 7248 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 7249 | } |
| 7250 | btrfs_put_ordered_extent(ordered); |
| 7251 | return 0; |
| 7252 | } |
| 7253 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7254 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7255 | { |
| 7256 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7257 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7258 | struct btrfs_fs_info *info = root->fs_info; |
| 7259 | struct extent_buffer *leaf; |
| 7260 | struct inode *reloc_inode; |
| 7261 | struct btrfs_block_group_cache *block_group; |
| 7262 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7263 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7264 | u64 cur_byte; |
| 7265 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7266 | u32 nritems; |
| 7267 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7268 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7269 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7270 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7271 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7272 | |
| 7273 | block_group = btrfs_lookup_block_group(info, group_start); |
| 7274 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7275 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7276 | printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7277 | (unsigned long long)block_group->key.objectid, |
| 7278 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7279 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7280 | path = btrfs_alloc_path(); |
| 7281 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7282 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7283 | reloc_inode = create_reloc_inode(info, block_group); |
| 7284 | BUG_ON(IS_ERR(reloc_inode)); |
| 7285 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7286 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7287 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7288 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7289 | btrfs_start_delalloc_inodes(info->tree_root); |
| 7290 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7291 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7292 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7293 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 7294 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7295 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7296 | key.offset = 0; |
| 7297 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7298 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 7299 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7300 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 7301 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7302 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7303 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 7304 | btrfs_clean_old_snapshots(info->tree_root); |
| 7305 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 7306 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7307 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7308 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 7309 | btrfs_commit_transaction(trans, info->tree_root); |
| 7310 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7311 | while (1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7312 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7313 | if (ret < 0) |
| 7314 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7315 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7316 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7317 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7318 | if (path->slots[0] >= nritems) { |
| 7319 | ret = btrfs_next_leaf(root, path); |
| 7320 | if (ret < 0) |
| 7321 | goto out; |
| 7322 | if (ret == 1) { |
| 7323 | ret = 0; |
| 7324 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7325 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7326 | leaf = path->nodes[0]; |
| 7327 | nritems = btrfs_header_nritems(leaf); |
| 7328 | } |
| 7329 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7330 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 7331 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7332 | if (key.objectid >= block_group->key.objectid + |
| 7333 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7334 | break; |
| 7335 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 7336 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 7337 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7338 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 7339 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7340 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 7341 | } |
| 7342 | progress = 1; |
| 7343 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7344 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 7345 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7346 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7347 | goto next; |
| 7348 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7349 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7350 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7351 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7352 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7353 | |
| 7354 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 7355 | ret = relocate_one_extent(root, path, &key, block_group, |
| 7356 | reloc_inode, pass); |
| 7357 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7358 | if (ret > 0) |
| 7359 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7360 | |
| 7361 | key.objectid = cur_byte; |
| 7362 | key.type = 0; |
| 7363 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7367 | |
| 7368 | if (pass == 0) { |
| 7369 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 7370 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7371 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7372 | |
| 7373 | if (total_found > 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7374 | printk(KERN_INFO "btrfs found %llu extents in pass %d\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7375 | (unsigned long long)total_found, pass); |
| 7376 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7377 | if (total_found == skipped && pass > 2) { |
| 7378 | iput(reloc_inode); |
| 7379 | reloc_inode = create_reloc_inode(info, block_group); |
| 7380 | pass = 0; |
| 7381 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7382 | goto again; |
| 7383 | } |
| 7384 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7385 | /* delete reloc_inode */ |
| 7386 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7387 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7388 | /* unpin extents in this range */ |
| 7389 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 7390 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7391 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7392 | spin_lock(&block_group->lock); |
| 7393 | WARN_ON(block_group->pinned > 0); |
| 7394 | WARN_ON(block_group->reserved > 0); |
| 7395 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 7396 | spin_unlock(&block_group->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7397 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7398 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7399 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7400 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7401 | return ret; |
| 7402 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7403 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7404 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7405 | static int find_first_block_group(struct btrfs_root *root, |
| 7406 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7407 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7408 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7409 | struct btrfs_key found_key; |
| 7410 | struct extent_buffer *leaf; |
| 7411 | int slot; |
| 7412 | |
| 7413 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7414 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7415 | goto out; |
| 7416 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7417 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7418 | slot = path->slots[0]; |
| 7419 | leaf = path->nodes[0]; |
| 7420 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7421 | ret = btrfs_next_leaf(root, path); |
| 7422 | if (ret == 0) |
| 7423 | continue; |
| 7424 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7425 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7426 | break; |
| 7427 | } |
| 7428 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7429 | |
| 7430 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7431 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7432 | ret = 0; |
| 7433 | goto out; |
| 7434 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7435 | path->slots[0]++; |
| 7436 | } |
| 7437 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7438 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7439 | return ret; |
| 7440 | } |
| 7441 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7442 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7443 | { |
| 7444 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7445 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7446 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7447 | struct rb_node *n; |
| 7448 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7449 | down_write(&info->extent_commit_sem); |
| 7450 | while (!list_empty(&info->caching_block_groups)) { |
| 7451 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7452 | struct btrfs_caching_control, list); |
| 7453 | list_del(&caching_ctl->list); |
| 7454 | put_caching_control(caching_ctl); |
| 7455 | } |
| 7456 | up_write(&info->extent_commit_sem); |
| 7457 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7458 | spin_lock(&info->block_group_cache_lock); |
| 7459 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7460 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7461 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7462 | rb_erase(&block_group->cache_node, |
| 7463 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7464 | spin_unlock(&info->block_group_cache_lock); |
| 7465 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7466 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7467 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7468 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7469 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7470 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7471 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7472 | |
| 7473 | btrfs_remove_free_space_cache(block_group); |
| 7474 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7475 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7476 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7477 | |
| 7478 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7479 | } |
| 7480 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7481 | |
| 7482 | /* now that all the block groups are freed, go through and |
| 7483 | * free all the space_info structs. This is only called during |
| 7484 | * the final stages of unmount, and so we know nobody is |
| 7485 | * using them. We call synchronize_rcu() once before we start, |
| 7486 | * just to be on the safe side. |
| 7487 | */ |
| 7488 | synchronize_rcu(); |
| 7489 | |
| 7490 | while(!list_empty(&info->space_info)) { |
| 7491 | space_info = list_entry(info->space_info.next, |
| 7492 | struct btrfs_space_info, |
| 7493 | list); |
| 7494 | |
| 7495 | list_del(&space_info->list); |
| 7496 | kfree(space_info); |
| 7497 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7498 | return 0; |
| 7499 | } |
| 7500 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7501 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7502 | { |
| 7503 | struct btrfs_path *path; |
| 7504 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7505 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7506 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7507 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7508 | struct btrfs_key key; |
| 7509 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7510 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7511 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7512 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7513 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7514 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7515 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7516 | path = btrfs_alloc_path(); |
| 7517 | if (!path) |
| 7518 | return -ENOMEM; |
| 7519 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7520 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7521 | ret = find_first_block_group(root, path, &key); |
| 7522 | if (ret > 0) { |
| 7523 | ret = 0; |
| 7524 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7525 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7526 | if (ret != 0) |
| 7527 | goto error; |
| 7528 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7529 | leaf = path->nodes[0]; |
| 7530 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7531 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7532 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7533 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7534 | break; |
| 7535 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7536 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7537 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7538 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7539 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7540 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7541 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7542 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7543 | |
| 7544 | /* |
| 7545 | * we only want to have 32k of ram per block group for keeping |
| 7546 | * track of free space, and if we pass 1/2 of that we want to |
| 7547 | * start converting things over to using bitmaps |
| 7548 | */ |
| 7549 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7550 | sizeof(struct btrfs_free_space); |
| 7551 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7552 | read_extent_buffer(leaf, &cache->item, |
| 7553 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7554 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7555 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7556 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7557 | key.objectid = found_key.objectid + found_key.offset; |
| 7558 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7559 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7560 | cache->sectorsize = root->sectorsize; |
| 7561 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7562 | /* |
| 7563 | * check for two cases, either we are full, and therefore |
| 7564 | * don't need to bother with the caching work since we won't |
| 7565 | * find any space, or we are empty, and we can just add all |
| 7566 | * the space in and be done with it. This saves us _alot_ of |
| 7567 | * time, particularly in the full case. |
| 7568 | */ |
| 7569 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7570 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7571 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7572 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7573 | exclude_super_stripes(root, cache); |
| 7574 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7575 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7576 | add_new_free_space(cache, root->fs_info, |
| 7577 | found_key.objectid, |
| 7578 | found_key.objectid + |
| 7579 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7580 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7581 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7582 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7583 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7584 | btrfs_block_group_used(&cache->item), |
| 7585 | &space_info); |
| 7586 | BUG_ON(ret); |
| 7587 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7588 | down_write(&space_info->groups_sem); |
| 7589 | list_add_tail(&cache->list, &space_info->block_groups); |
| 7590 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7591 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7592 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7593 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7594 | |
| 7595 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7596 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 7597 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7598 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7599 | ret = 0; |
| 7600 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7601 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7602 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7603 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7604 | |
| 7605 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7606 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7607 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7608 | u64 size) |
| 7609 | { |
| 7610 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7611 | struct btrfs_root *extent_root; |
| 7612 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7613 | |
| 7614 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7615 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7616 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7617 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7618 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7619 | if (!cache) |
| 7620 | return -ENOMEM; |
| 7621 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7622 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7623 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7624 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7625 | cache->sectorsize = root->sectorsize; |
| 7626 | |
| 7627 | /* |
| 7628 | * we only want to have 32k of ram per block group for keeping track |
| 7629 | * of free space, and if we pass 1/2 of that we want to start |
| 7630 | * converting things over to using bitmaps |
| 7631 | */ |
| 7632 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7633 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7634 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7635 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7636 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7637 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7638 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7639 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7640 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7641 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7642 | cache->flags = type; |
| 7643 | btrfs_set_block_group_flags(&cache->item, type); |
| 7644 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7645 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7646 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7647 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7648 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7649 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7650 | chunk_offset + size); |
| 7651 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7652 | free_excluded_extents(root, cache); |
| 7653 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7654 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7655 | &cache->space_info); |
| 7656 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7657 | down_write(&cache->space_info->groups_sem); |
| 7658 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 7659 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7660 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7661 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7662 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7663 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7664 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7665 | sizeof(cache->item)); |
| 7666 | BUG_ON(ret); |
| 7667 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7668 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7669 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7670 | return 0; |
| 7671 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7672 | |
| 7673 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7674 | struct btrfs_root *root, u64 group_start) |
| 7675 | { |
| 7676 | struct btrfs_path *path; |
| 7677 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7678 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7679 | struct btrfs_key key; |
| 7680 | int ret; |
| 7681 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7682 | root = root->fs_info->extent_root; |
| 7683 | |
| 7684 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7685 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7686 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7687 | |
| 7688 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7689 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7690 | /* make sure this block group isn't part of an allocation cluster */ |
| 7691 | cluster = &root->fs_info->data_alloc_cluster; |
| 7692 | spin_lock(&cluster->refill_lock); |
| 7693 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7694 | spin_unlock(&cluster->refill_lock); |
| 7695 | |
| 7696 | /* |
| 7697 | * make sure this block group isn't part of a metadata |
| 7698 | * allocation cluster |
| 7699 | */ |
| 7700 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7701 | spin_lock(&cluster->refill_lock); |
| 7702 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7703 | spin_unlock(&cluster->refill_lock); |
| 7704 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7705 | path = btrfs_alloc_path(); |
| 7706 | BUG_ON(!path); |
| 7707 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7708 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7709 | rb_erase(&block_group->cache_node, |
| 7710 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7711 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7712 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7713 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7714 | /* |
| 7715 | * we must use list_del_init so people can check to see if they |
| 7716 | * are still on the list after taking the semaphore |
| 7717 | */ |
| 7718 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7719 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7720 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7721 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7722 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7723 | |
| 7724 | btrfs_remove_free_space_cache(block_group); |
| 7725 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7726 | spin_lock(&block_group->space_info->lock); |
| 7727 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7728 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 7729 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7730 | |
| 7731 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7732 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7733 | btrfs_put_block_group(block_group); |
| 7734 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7735 | |
| 7736 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7737 | if (ret > 0) |
| 7738 | ret = -EIO; |
| 7739 | if (ret < 0) |
| 7740 | goto out; |
| 7741 | |
| 7742 | ret = btrfs_del_item(trans, root, path); |
| 7743 | out: |
| 7744 | btrfs_free_path(path); |
| 7745 | return ret; |
| 7746 | } |