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 | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 33 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 34 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 35 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 36 | struct btrfs_root *root, |
| 37 | u64 bytenr, u64 num_bytes, int alloc, |
| 38 | int mark_free); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 39 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 40 | u64 num_bytes, int reserve); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 41 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 42 | struct btrfs_root *root, |
| 43 | u64 bytenr, u64 num_bytes, u64 parent, |
| 44 | u64 root_objectid, u64 owner_objectid, |
| 45 | u64 owner_offset, int refs_to_drop, |
| 46 | struct btrfs_delayed_extent_op *extra_op); |
| 47 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 48 | struct extent_buffer *leaf, |
| 49 | struct btrfs_extent_item *ei); |
| 50 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 51 | struct btrfs_root *root, |
| 52 | u64 parent, u64 root_objectid, |
| 53 | u64 flags, u64 owner, u64 offset, |
| 54 | struct btrfs_key *ins, int ref_mod); |
| 55 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
| 57 | u64 parent, u64 root_objectid, |
| 58 | u64 flags, struct btrfs_disk_key *key, |
| 59 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 60 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 62 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 63 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 64 | struct btrfs_root *root, |
| 65 | struct btrfs_path *path, |
| 66 | u64 bytenr, u64 num_bytes, |
| 67 | int is_data, int reserved, |
| 68 | struct extent_buffer **must_clean); |
| 69 | static int find_next_key(struct btrfs_path *path, int level, |
| 70 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 71 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 72 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 73 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 74 | static noinline int |
| 75 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 76 | { |
| 77 | smp_mb(); |
| 78 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 79 | } |
| 80 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 81 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 82 | { |
| 83 | return (cache->flags & bits) == bits; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * this adds the block group to the fs_info rb tree for the block group |
| 88 | * cache |
| 89 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 90 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 91 | struct btrfs_block_group_cache *block_group) |
| 92 | { |
| 93 | struct rb_node **p; |
| 94 | struct rb_node *parent = NULL; |
| 95 | struct btrfs_block_group_cache *cache; |
| 96 | |
| 97 | spin_lock(&info->block_group_cache_lock); |
| 98 | p = &info->block_group_cache_tree.rb_node; |
| 99 | |
| 100 | while (*p) { |
| 101 | parent = *p; |
| 102 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 103 | cache_node); |
| 104 | if (block_group->key.objectid < cache->key.objectid) { |
| 105 | p = &(*p)->rb_left; |
| 106 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 107 | p = &(*p)->rb_right; |
| 108 | } else { |
| 109 | spin_unlock(&info->block_group_cache_lock); |
| 110 | return -EEXIST; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | rb_link_node(&block_group->cache_node, parent, p); |
| 115 | rb_insert_color(&block_group->cache_node, |
| 116 | &info->block_group_cache_tree); |
| 117 | spin_unlock(&info->block_group_cache_lock); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * This will return the block group at or after bytenr if contains is 0, else |
| 124 | * it will return the block group that contains the bytenr |
| 125 | */ |
| 126 | static struct btrfs_block_group_cache * |
| 127 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 128 | int contains) |
| 129 | { |
| 130 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 131 | struct rb_node *n; |
| 132 | u64 end, start; |
| 133 | |
| 134 | spin_lock(&info->block_group_cache_lock); |
| 135 | n = info->block_group_cache_tree.rb_node; |
| 136 | |
| 137 | while (n) { |
| 138 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 139 | cache_node); |
| 140 | end = cache->key.objectid + cache->key.offset - 1; |
| 141 | start = cache->key.objectid; |
| 142 | |
| 143 | if (bytenr < start) { |
| 144 | if (!contains && (!ret || start < ret->key.objectid)) |
| 145 | ret = cache; |
| 146 | n = n->rb_left; |
| 147 | } else if (bytenr > start) { |
| 148 | if (contains && bytenr <= end) { |
| 149 | ret = cache; |
| 150 | break; |
| 151 | } |
| 152 | n = n->rb_right; |
| 153 | } else { |
| 154 | ret = cache; |
| 155 | break; |
| 156 | } |
| 157 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 158 | if (ret) |
| 159 | atomic_inc(&ret->count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 160 | spin_unlock(&info->block_group_cache_lock); |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 165 | static int add_excluded_extent(struct btrfs_root *root, |
| 166 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 167 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 168 | u64 end = start + num_bytes - 1; |
| 169 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 170 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 171 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 172 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 173 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 176 | static void free_excluded_extents(struct btrfs_root *root, |
| 177 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 178 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 179 | u64 start, end; |
| 180 | |
| 181 | start = cache->key.objectid; |
| 182 | end = start + cache->key.offset - 1; |
| 183 | |
| 184 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 185 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 186 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 187 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 188 | } |
| 189 | |
| 190 | static int exclude_super_stripes(struct btrfs_root *root, |
| 191 | struct btrfs_block_group_cache *cache) |
| 192 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 193 | u64 bytenr; |
| 194 | u64 *logical; |
| 195 | int stripe_len; |
| 196 | int i, nr, ret; |
| 197 | |
| 198 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 199 | bytenr = btrfs_sb_offset(i); |
| 200 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 201 | cache->key.objectid, bytenr, |
| 202 | 0, &logical, &nr, &stripe_len); |
| 203 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 204 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 205 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 206 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 207 | ret = add_excluded_extent(root, logical[nr], |
| 208 | stripe_len); |
| 209 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 210 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 211 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 212 | kfree(logical); |
| 213 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 217 | static struct btrfs_caching_control * |
| 218 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 219 | { |
| 220 | struct btrfs_caching_control *ctl; |
| 221 | |
| 222 | spin_lock(&cache->lock); |
| 223 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 224 | spin_unlock(&cache->lock); |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | ctl = cache->caching_ctl; |
| 229 | atomic_inc(&ctl->count); |
| 230 | spin_unlock(&cache->lock); |
| 231 | return ctl; |
| 232 | } |
| 233 | |
| 234 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 235 | { |
| 236 | if (atomic_dec_and_test(&ctl->count)) |
| 237 | kfree(ctl); |
| 238 | } |
| 239 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 240 | /* |
| 241 | * this is only called by cache_block_group, since we could have freed extents |
| 242 | * we need to check the pinned_extents for any extents that can't be used yet |
| 243 | * since their free space will be released as soon as the transaction commits. |
| 244 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 245 | 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] | 246 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 247 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 248 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 249 | int ret; |
| 250 | |
| 251 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 252 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 253 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 254 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 255 | if (ret) |
| 256 | break; |
| 257 | |
| 258 | if (extent_start == start) { |
| 259 | start = extent_end + 1; |
| 260 | } else if (extent_start > start && extent_start < end) { |
| 261 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 262 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 263 | ret = btrfs_add_free_space(block_group, start, |
| 264 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 265 | BUG_ON(ret); |
| 266 | start = extent_end + 1; |
| 267 | } else { |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (start < end) { |
| 273 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 274 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 275 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 276 | BUG_ON(ret); |
| 277 | } |
| 278 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 279 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 282 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 283 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 284 | struct btrfs_block_group_cache *block_group = data; |
| 285 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 286 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 287 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 288 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 289 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 290 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 291 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 292 | u64 last = 0; |
| 293 | u32 nritems; |
| 294 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 295 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 296 | path = btrfs_alloc_path(); |
| 297 | if (!path) |
| 298 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 299 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 300 | exclude_super_stripes(extent_root, block_group); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 301 | spin_lock(&block_group->space_info->lock); |
| 302 | block_group->space_info->bytes_super += block_group->bytes_super; |
| 303 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 304 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 305 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 306 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 307 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 308 | * We don't want to deadlock with somebody trying to allocate a new |
| 309 | * extent for the extent root while also trying to search the extent |
| 310 | * root to add free space. So we skip locking and search the commit |
| 311 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 312 | */ |
| 313 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 314 | path->search_commit_root = 1; |
| 315 | path->reada = 2; |
| 316 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 317 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 318 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 319 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 320 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 321 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 322 | /* need to make sure the commit_root doesn't disappear */ |
| 323 | down_read(&fs_info->extent_commit_sem); |
| 324 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 325 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 326 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 327 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 328 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 329 | leaf = path->nodes[0]; |
| 330 | nritems = btrfs_header_nritems(leaf); |
| 331 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 332 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 333 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 334 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 335 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 336 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 337 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 338 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 339 | if (path->slots[0] < nritems) { |
| 340 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 341 | } else { |
| 342 | ret = find_next_key(path, 0, &key); |
| 343 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 344 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 345 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 346 | caching_ctl->progress = last; |
| 347 | btrfs_release_path(extent_root, path); |
| 348 | up_read(&fs_info->extent_commit_sem); |
| 349 | mutex_unlock(&caching_ctl->mutex); |
| 350 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 351 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 352 | else |
| 353 | cond_resched(); |
| 354 | goto again; |
| 355 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 356 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 357 | if (key.objectid < block_group->key.objectid) { |
| 358 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 359 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 360 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 361 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 362 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 363 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 364 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 365 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 366 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 367 | total_found += add_new_free_space(block_group, |
| 368 | fs_info, last, |
| 369 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 370 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 371 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 372 | if (total_found > (1024 * 1024 * 2)) { |
| 373 | total_found = 0; |
| 374 | wake_up(&caching_ctl->wait); |
| 375 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 376 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 377 | path->slots[0]++; |
| 378 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 379 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 380 | |
| 381 | total_found += add_new_free_space(block_group, fs_info, last, |
| 382 | block_group->key.objectid + |
| 383 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 384 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 385 | |
| 386 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 387 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 388 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 389 | spin_unlock(&block_group->lock); |
| 390 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 391 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 392 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 393 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 394 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 395 | free_excluded_extents(extent_root, block_group); |
| 396 | |
| 397 | mutex_unlock(&caching_ctl->mutex); |
| 398 | wake_up(&caching_ctl->wait); |
| 399 | |
| 400 | put_caching_control(caching_ctl); |
| 401 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int cache_block_group(struct btrfs_block_group_cache *cache) |
| 406 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 407 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 408 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 409 | struct task_struct *tsk; |
| 410 | int ret = 0; |
| 411 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 412 | smp_mb(); |
| 413 | if (cache->cached != BTRFS_CACHE_NO) |
| 414 | return 0; |
| 415 | |
| 416 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); |
| 417 | BUG_ON(!caching_ctl); |
| 418 | |
| 419 | INIT_LIST_HEAD(&caching_ctl->list); |
| 420 | mutex_init(&caching_ctl->mutex); |
| 421 | init_waitqueue_head(&caching_ctl->wait); |
| 422 | caching_ctl->block_group = cache; |
| 423 | caching_ctl->progress = cache->key.objectid; |
| 424 | /* one for caching kthread, one for caching block group list */ |
| 425 | atomic_set(&caching_ctl->count, 2); |
| 426 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 427 | spin_lock(&cache->lock); |
| 428 | if (cache->cached != BTRFS_CACHE_NO) { |
| 429 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 430 | kfree(caching_ctl); |
| 431 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 432 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 433 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 434 | cache->cached = BTRFS_CACHE_STARTED; |
| 435 | spin_unlock(&cache->lock); |
| 436 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 437 | down_write(&fs_info->extent_commit_sem); |
| 438 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 439 | up_write(&fs_info->extent_commit_sem); |
| 440 | |
| 441 | atomic_inc(&cache->space_info->caching_threads); |
| 442 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 443 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 444 | cache->key.objectid); |
| 445 | if (IS_ERR(tsk)) { |
| 446 | ret = PTR_ERR(tsk); |
| 447 | printk(KERN_ERR "error running thread %d\n", ret); |
| 448 | BUG(); |
| 449 | } |
| 450 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 451 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 454 | /* |
| 455 | * return the block group that starts at or after bytenr |
| 456 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 457 | static struct btrfs_block_group_cache * |
| 458 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 459 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 460 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 461 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 462 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 463 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 464 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 465 | } |
| 466 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 467 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 468 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 469 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 470 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 471 | struct btrfs_fs_info *info, |
| 472 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 473 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 474 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 475 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 476 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 477 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 478 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 479 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 480 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 481 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 482 | { |
| 483 | if (atomic_dec_and_test(&cache->count)) |
| 484 | kfree(cache); |
| 485 | } |
| 486 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 487 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 488 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 489 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 490 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 491 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 492 | |
| 493 | rcu_read_lock(); |
| 494 | list_for_each_entry_rcu(found, head, list) { |
| 495 | if (found->flags == flags) { |
| 496 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 497 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 498 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 499 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 500 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 501 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 502 | } |
| 503 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 504 | /* |
| 505 | * after adding space to the filesystem, we need to clear the full flags |
| 506 | * on all the space infos. |
| 507 | */ |
| 508 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 509 | { |
| 510 | struct list_head *head = &info->space_info; |
| 511 | struct btrfs_space_info *found; |
| 512 | |
| 513 | rcu_read_lock(); |
| 514 | list_for_each_entry_rcu(found, head, list) |
| 515 | found->full = 0; |
| 516 | rcu_read_unlock(); |
| 517 | } |
| 518 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 519 | static u64 div_factor(u64 num, int factor) |
| 520 | { |
| 521 | if (factor == 10) |
| 522 | return num; |
| 523 | num *= factor; |
| 524 | do_div(num, 10); |
| 525 | return num; |
| 526 | } |
| 527 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 528 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 529 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 530 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 531 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 532 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 533 | u64 last = max(search_hint, search_start); |
| 534 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 535 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 536 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 537 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 538 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 539 | while (1) { |
| 540 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 541 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 542 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 543 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 544 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 545 | last = cache->key.objectid + cache->key.offset; |
| 546 | used = btrfs_block_group_used(&cache->item); |
| 547 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 548 | if ((full_search || !cache->ro) && |
| 549 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 550 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 551 | div_factor(cache->key.offset, factor)) { |
| 552 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 553 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 554 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 555 | goto found; |
| 556 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 557 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 558 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 559 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 560 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 561 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 562 | if (!wrapped) { |
| 563 | last = search_start; |
| 564 | wrapped = 1; |
| 565 | goto again; |
| 566 | } |
| 567 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 568 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 569 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 570 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 571 | goto again; |
| 572 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 573 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 574 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 575 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 576 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 577 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 578 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 579 | { |
| 580 | int ret; |
| 581 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 582 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 583 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 584 | path = btrfs_alloc_path(); |
| 585 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 586 | key.objectid = start; |
| 587 | key.offset = len; |
| 588 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 589 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 590 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 591 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 592 | return ret; |
| 593 | } |
| 594 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 595 | /* |
| 596 | * Back reference rules. Back refs have three main goals: |
| 597 | * |
| 598 | * 1) differentiate between all holders of references to an extent so that |
| 599 | * when a reference is dropped we can make sure it was a valid reference |
| 600 | * before freeing the extent. |
| 601 | * |
| 602 | * 2) Provide enough information to quickly find the holders of an extent |
| 603 | * if we notice a given block is corrupted or bad. |
| 604 | * |
| 605 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 606 | * maintenance. This is actually the same as #2, but with a slightly |
| 607 | * different use case. |
| 608 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 609 | * There are two kinds of back refs. The implicit back refs is optimized |
| 610 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 611 | * back refs of this kind provide information about the block's owner tree |
| 612 | * and the pointer's key. These information allow us to find the block by |
| 613 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 614 | * referenced by their owner trees. The location of tree block is recorded |
| 615 | * in the back refs. Actually the full back refs is generic, and can be |
| 616 | * used in all cases the implicit back refs is used. The major shortcoming |
| 617 | * of the full back refs is its overhead. Every time a tree block gets |
| 618 | * COWed, we have to update back refs entry for all pointers in it. |
| 619 | * |
| 620 | * For a newly allocated tree block, we use implicit back refs for |
| 621 | * pointers in it. This means most tree related operations only involve |
| 622 | * implicit back refs. For a tree block created in old transaction, the |
| 623 | * only way to drop a reference to it is COW it. So we can detect the |
| 624 | * event that tree block loses its owner tree's reference and do the |
| 625 | * back refs conversion. |
| 626 | * |
| 627 | * When a tree block is COW'd through a tree, there are four cases: |
| 628 | * |
| 629 | * The reference count of the block is one and the tree is the block's |
| 630 | * owner tree. Nothing to do in this case. |
| 631 | * |
| 632 | * The reference count of the block is one and the tree is not the |
| 633 | * block's owner tree. In this case, full back refs is used for pointers |
| 634 | * in the block. Remove these full back refs, add implicit back refs for |
| 635 | * every pointers in the new block. |
| 636 | * |
| 637 | * The reference count of the block is greater than one and the tree is |
| 638 | * the block's owner tree. In this case, implicit back refs is used for |
| 639 | * pointers in the block. Add full back refs for every pointers in the |
| 640 | * block, increase lower level extents' reference counts. The original |
| 641 | * implicit back refs are entailed to the new block. |
| 642 | * |
| 643 | * The reference count of the block is greater than one and the tree is |
| 644 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 645 | * the new block, increase lower level extents' reference count. |
| 646 | * |
| 647 | * Back Reference Key composing: |
| 648 | * |
| 649 | * The key objectid corresponds to the first byte in the extent, |
| 650 | * The key type is used to differentiate between types of back refs. |
| 651 | * There are different meanings of the key offset for different types |
| 652 | * of back refs. |
| 653 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 654 | * File extents can be referenced by: |
| 655 | * |
| 656 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 657 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 658 | * - different offsets inside a file (bookend extents in file.c) |
| 659 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 660 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 661 | * |
| 662 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 663 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 664 | * - original offset in the file |
| 665 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 666 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 667 | * The key offset for the implicit back refs is hash of the first |
| 668 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 669 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 670 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 671 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 672 | * - number of pointers in the tree leaf |
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 | * The key offset for the implicit back refs is the first byte of |
| 675 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 676 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 677 | * When a file extent is allocated, The implicit back refs is used. |
| 678 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 679 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 680 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 681 | * |
| 682 | * When a file extent is removed file truncation, we find the |
| 683 | * corresponding implicit back refs and check the following fields: |
| 684 | * |
| 685 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 686 | * |
| 687 | * Btree extents can be referenced by: |
| 688 | * |
| 689 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 690 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 691 | * Both the implicit back refs and the full back refs for tree blocks |
| 692 | * only consist of key. The key offset for the implicit back refs is |
| 693 | * objectid of block's owner tree. The key offset for the full back refs |
| 694 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 695 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 696 | * When implicit back refs is used, information about the lowest key and |
| 697 | * level of the tree block are required. These information are stored in |
| 698 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 699 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 700 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 701 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 702 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 703 | struct btrfs_root *root, |
| 704 | struct btrfs_path *path, |
| 705 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 706 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 707 | struct btrfs_extent_item *item; |
| 708 | struct btrfs_extent_item_v0 *ei0; |
| 709 | struct btrfs_extent_ref_v0 *ref0; |
| 710 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 711 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 712 | struct btrfs_key key; |
| 713 | struct btrfs_key found_key; |
| 714 | u32 new_size = sizeof(*item); |
| 715 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 716 | int ret; |
| 717 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 718 | leaf = path->nodes[0]; |
| 719 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 720 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 721 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 722 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 723 | struct btrfs_extent_item_v0); |
| 724 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 725 | |
| 726 | if (owner == (u64)-1) { |
| 727 | while (1) { |
| 728 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 729 | ret = btrfs_next_leaf(root, path); |
| 730 | if (ret < 0) |
| 731 | return ret; |
| 732 | BUG_ON(ret > 0); |
| 733 | leaf = path->nodes[0]; |
| 734 | } |
| 735 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 736 | path->slots[0]); |
| 737 | BUG_ON(key.objectid != found_key.objectid); |
| 738 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 739 | path->slots[0]++; |
| 740 | continue; |
| 741 | } |
| 742 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 743 | struct btrfs_extent_ref_v0); |
| 744 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | btrfs_release_path(root, path); |
| 749 | |
| 750 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 751 | new_size += sizeof(*bi); |
| 752 | |
| 753 | new_size -= sizeof(*ei0); |
| 754 | ret = btrfs_search_slot(trans, root, &key, path, |
| 755 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 756 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 757 | return ret; |
| 758 | BUG_ON(ret); |
| 759 | |
| 760 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 761 | BUG_ON(ret); |
| 762 | |
| 763 | leaf = path->nodes[0]; |
| 764 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 765 | btrfs_set_extent_refs(leaf, item, refs); |
| 766 | /* FIXME: get real generation */ |
| 767 | btrfs_set_extent_generation(leaf, item, 0); |
| 768 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 769 | btrfs_set_extent_flags(leaf, item, |
| 770 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 771 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 772 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 773 | /* FIXME: get first key of the block */ |
| 774 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 775 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 776 | } else { |
| 777 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 778 | } |
| 779 | btrfs_mark_buffer_dirty(leaf); |
| 780 | return 0; |
| 781 | } |
| 782 | #endif |
| 783 | |
| 784 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 785 | { |
| 786 | u32 high_crc = ~(u32)0; |
| 787 | u32 low_crc = ~(u32)0; |
| 788 | __le64 lenum; |
| 789 | |
| 790 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 791 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 792 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 793 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 794 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 795 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 796 | |
| 797 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 798 | } |
| 799 | |
| 800 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 801 | struct btrfs_extent_data_ref *ref) |
| 802 | { |
| 803 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 804 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 805 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 806 | } |
| 807 | |
| 808 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 809 | struct btrfs_extent_data_ref *ref, |
| 810 | u64 root_objectid, u64 owner, u64 offset) |
| 811 | { |
| 812 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 813 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 814 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 815 | return 0; |
| 816 | return 1; |
| 817 | } |
| 818 | |
| 819 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 820 | struct btrfs_root *root, |
| 821 | struct btrfs_path *path, |
| 822 | u64 bytenr, u64 parent, |
| 823 | u64 root_objectid, |
| 824 | u64 owner, u64 offset) |
| 825 | { |
| 826 | struct btrfs_key key; |
| 827 | struct btrfs_extent_data_ref *ref; |
| 828 | struct extent_buffer *leaf; |
| 829 | u32 nritems; |
| 830 | int ret; |
| 831 | int recow; |
| 832 | int err = -ENOENT; |
| 833 | |
| 834 | key.objectid = bytenr; |
| 835 | if (parent) { |
| 836 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 837 | key.offset = parent; |
| 838 | } else { |
| 839 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 840 | key.offset = hash_extent_data_ref(root_objectid, |
| 841 | owner, offset); |
| 842 | } |
| 843 | again: |
| 844 | recow = 0; |
| 845 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 846 | if (ret < 0) { |
| 847 | err = ret; |
| 848 | goto fail; |
| 849 | } |
| 850 | |
| 851 | if (parent) { |
| 852 | if (!ret) |
| 853 | return 0; |
| 854 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 855 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 856 | btrfs_release_path(root, path); |
| 857 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 858 | if (ret < 0) { |
| 859 | err = ret; |
| 860 | goto fail; |
| 861 | } |
| 862 | if (!ret) |
| 863 | return 0; |
| 864 | #endif |
| 865 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 869 | nritems = btrfs_header_nritems(leaf); |
| 870 | while (1) { |
| 871 | if (path->slots[0] >= nritems) { |
| 872 | ret = btrfs_next_leaf(root, path); |
| 873 | if (ret < 0) |
| 874 | err = ret; |
| 875 | if (ret) |
| 876 | goto fail; |
| 877 | |
| 878 | leaf = path->nodes[0]; |
| 879 | nritems = btrfs_header_nritems(leaf); |
| 880 | recow = 1; |
| 881 | } |
| 882 | |
| 883 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 884 | if (key.objectid != bytenr || |
| 885 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 886 | goto fail; |
| 887 | |
| 888 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 889 | struct btrfs_extent_data_ref); |
| 890 | |
| 891 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 892 | owner, offset)) { |
| 893 | if (recow) { |
| 894 | btrfs_release_path(root, path); |
| 895 | goto again; |
| 896 | } |
| 897 | err = 0; |
| 898 | break; |
| 899 | } |
| 900 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 901 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 902 | fail: |
| 903 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 904 | } |
| 905 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 906 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 907 | struct btrfs_root *root, |
| 908 | struct btrfs_path *path, |
| 909 | u64 bytenr, u64 parent, |
| 910 | u64 root_objectid, u64 owner, |
| 911 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 912 | { |
| 913 | struct btrfs_key key; |
| 914 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 915 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 916 | u32 num_refs; |
| 917 | int ret; |
| 918 | |
| 919 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 920 | if (parent) { |
| 921 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 922 | key.offset = parent; |
| 923 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 924 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 925 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 926 | key.offset = hash_extent_data_ref(root_objectid, |
| 927 | owner, offset); |
| 928 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 929 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 930 | |
| 931 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 932 | if (ret && ret != -EEXIST) |
| 933 | goto fail; |
| 934 | |
| 935 | leaf = path->nodes[0]; |
| 936 | if (parent) { |
| 937 | struct btrfs_shared_data_ref *ref; |
| 938 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 939 | struct btrfs_shared_data_ref); |
| 940 | if (ret == 0) { |
| 941 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 942 | } else { |
| 943 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 944 | num_refs += refs_to_add; |
| 945 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 946 | } |
| 947 | } else { |
| 948 | struct btrfs_extent_data_ref *ref; |
| 949 | while (ret == -EEXIST) { |
| 950 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 951 | struct btrfs_extent_data_ref); |
| 952 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 953 | owner, offset)) |
| 954 | break; |
| 955 | btrfs_release_path(root, path); |
| 956 | key.offset++; |
| 957 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 958 | size); |
| 959 | if (ret && ret != -EEXIST) |
| 960 | goto fail; |
| 961 | |
| 962 | leaf = path->nodes[0]; |
| 963 | } |
| 964 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 965 | struct btrfs_extent_data_ref); |
| 966 | if (ret == 0) { |
| 967 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 968 | root_objectid); |
| 969 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 970 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 971 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 972 | } else { |
| 973 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 974 | num_refs += refs_to_add; |
| 975 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 976 | } |
| 977 | } |
| 978 | btrfs_mark_buffer_dirty(leaf); |
| 979 | ret = 0; |
| 980 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 981 | btrfs_release_path(root, path); |
| 982 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 983 | } |
| 984 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 985 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 986 | struct btrfs_root *root, |
| 987 | struct btrfs_path *path, |
| 988 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 989 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 990 | struct btrfs_key key; |
| 991 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 992 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 993 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 994 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 995 | int ret = 0; |
| 996 | |
| 997 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 998 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 999 | |
| 1000 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1001 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1002 | struct btrfs_extent_data_ref); |
| 1003 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1004 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1005 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1006 | struct btrfs_shared_data_ref); |
| 1007 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1008 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1009 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1010 | struct btrfs_extent_ref_v0 *ref0; |
| 1011 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1012 | struct btrfs_extent_ref_v0); |
| 1013 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1014 | #endif |
| 1015 | } else { |
| 1016 | BUG(); |
| 1017 | } |
| 1018 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1019 | BUG_ON(num_refs < refs_to_drop); |
| 1020 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1021 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1022 | if (num_refs == 0) { |
| 1023 | ret = btrfs_del_item(trans, root, path); |
| 1024 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1025 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1026 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1027 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1028 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1029 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1030 | else { |
| 1031 | struct btrfs_extent_ref_v0 *ref0; |
| 1032 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1033 | struct btrfs_extent_ref_v0); |
| 1034 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1035 | } |
| 1036 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1037 | btrfs_mark_buffer_dirty(leaf); |
| 1038 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1043 | struct btrfs_path *path, |
| 1044 | struct btrfs_extent_inline_ref *iref) |
| 1045 | { |
| 1046 | struct btrfs_key key; |
| 1047 | struct extent_buffer *leaf; |
| 1048 | struct btrfs_extent_data_ref *ref1; |
| 1049 | struct btrfs_shared_data_ref *ref2; |
| 1050 | u32 num_refs = 0; |
| 1051 | |
| 1052 | leaf = path->nodes[0]; |
| 1053 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1054 | if (iref) { |
| 1055 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1056 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1057 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1058 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1059 | } else { |
| 1060 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1061 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1062 | } |
| 1063 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1064 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1065 | struct btrfs_extent_data_ref); |
| 1066 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1067 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1068 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1069 | struct btrfs_shared_data_ref); |
| 1070 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1071 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1072 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1073 | struct btrfs_extent_ref_v0 *ref0; |
| 1074 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1075 | struct btrfs_extent_ref_v0); |
| 1076 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1077 | #endif |
| 1078 | } else { |
| 1079 | WARN_ON(1); |
| 1080 | } |
| 1081 | return num_refs; |
| 1082 | } |
| 1083 | |
| 1084 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1085 | struct btrfs_root *root, |
| 1086 | struct btrfs_path *path, |
| 1087 | u64 bytenr, u64 parent, |
| 1088 | u64 root_objectid) |
| 1089 | { |
| 1090 | struct btrfs_key key; |
| 1091 | int ret; |
| 1092 | |
| 1093 | key.objectid = bytenr; |
| 1094 | if (parent) { |
| 1095 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1096 | key.offset = parent; |
| 1097 | } else { |
| 1098 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1099 | key.offset = root_objectid; |
| 1100 | } |
| 1101 | |
| 1102 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1103 | if (ret > 0) |
| 1104 | ret = -ENOENT; |
| 1105 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1106 | if (ret == -ENOENT && parent) { |
| 1107 | btrfs_release_path(root, path); |
| 1108 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1109 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1110 | if (ret > 0) |
| 1111 | ret = -ENOENT; |
| 1112 | } |
| 1113 | #endif |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1118 | struct btrfs_root *root, |
| 1119 | struct btrfs_path *path, |
| 1120 | u64 bytenr, u64 parent, |
| 1121 | u64 root_objectid) |
| 1122 | { |
| 1123 | struct btrfs_key key; |
| 1124 | int ret; |
| 1125 | |
| 1126 | key.objectid = bytenr; |
| 1127 | if (parent) { |
| 1128 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1129 | key.offset = parent; |
| 1130 | } else { |
| 1131 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1132 | key.offset = root_objectid; |
| 1133 | } |
| 1134 | |
| 1135 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1136 | btrfs_release_path(root, path); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1140 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1141 | { |
| 1142 | int type; |
| 1143 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1144 | if (parent > 0) |
| 1145 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1146 | else |
| 1147 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1148 | } else { |
| 1149 | if (parent > 0) |
| 1150 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1151 | else |
| 1152 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1153 | } |
| 1154 | return type; |
| 1155 | } |
| 1156 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1157 | static int find_next_key(struct btrfs_path *path, int level, |
| 1158 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1159 | |
| 1160 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1161 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1162 | if (!path->nodes[level]) |
| 1163 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1164 | if (path->slots[level] + 1 >= |
| 1165 | btrfs_header_nritems(path->nodes[level])) |
| 1166 | continue; |
| 1167 | if (level == 0) |
| 1168 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1169 | path->slots[level] + 1); |
| 1170 | else |
| 1171 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1172 | path->slots[level] + 1); |
| 1173 | return 0; |
| 1174 | } |
| 1175 | return 1; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1180 | * to the address of inline back ref, and 0 is returned. |
| 1181 | * |
| 1182 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1183 | * should be inserted, and -ENOENT is returned. |
| 1184 | * |
| 1185 | * if insert is true and there are too many inline back refs, the path |
| 1186 | * points to the extent item, and -EAGAIN is returned. |
| 1187 | * |
| 1188 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1189 | * items in the tree are ordered. |
| 1190 | */ |
| 1191 | static noinline_for_stack |
| 1192 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1193 | struct btrfs_root *root, |
| 1194 | struct btrfs_path *path, |
| 1195 | struct btrfs_extent_inline_ref **ref_ret, |
| 1196 | u64 bytenr, u64 num_bytes, |
| 1197 | u64 parent, u64 root_objectid, |
| 1198 | u64 owner, u64 offset, int insert) |
| 1199 | { |
| 1200 | struct btrfs_key key; |
| 1201 | struct extent_buffer *leaf; |
| 1202 | struct btrfs_extent_item *ei; |
| 1203 | struct btrfs_extent_inline_ref *iref; |
| 1204 | u64 flags; |
| 1205 | u64 item_size; |
| 1206 | unsigned long ptr; |
| 1207 | unsigned long end; |
| 1208 | int extra_size; |
| 1209 | int type; |
| 1210 | int want; |
| 1211 | int ret; |
| 1212 | int err = 0; |
| 1213 | |
| 1214 | key.objectid = bytenr; |
| 1215 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1216 | key.offset = num_bytes; |
| 1217 | |
| 1218 | want = extent_ref_type(parent, owner); |
| 1219 | if (insert) { |
| 1220 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1221 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1222 | } else |
| 1223 | extra_size = -1; |
| 1224 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1225 | if (ret < 0) { |
| 1226 | err = ret; |
| 1227 | goto out; |
| 1228 | } |
| 1229 | BUG_ON(ret); |
| 1230 | |
| 1231 | leaf = path->nodes[0]; |
| 1232 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1233 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1234 | if (item_size < sizeof(*ei)) { |
| 1235 | if (!insert) { |
| 1236 | err = -ENOENT; |
| 1237 | goto out; |
| 1238 | } |
| 1239 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1240 | extra_size); |
| 1241 | if (ret < 0) { |
| 1242 | err = ret; |
| 1243 | goto out; |
| 1244 | } |
| 1245 | leaf = path->nodes[0]; |
| 1246 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1247 | } |
| 1248 | #endif |
| 1249 | BUG_ON(item_size < sizeof(*ei)); |
| 1250 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1251 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1252 | flags = btrfs_extent_flags(leaf, ei); |
| 1253 | |
| 1254 | ptr = (unsigned long)(ei + 1); |
| 1255 | end = (unsigned long)ei + item_size; |
| 1256 | |
| 1257 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1258 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1259 | BUG_ON(ptr > end); |
| 1260 | } else { |
| 1261 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1262 | } |
| 1263 | |
| 1264 | err = -ENOENT; |
| 1265 | while (1) { |
| 1266 | if (ptr >= end) { |
| 1267 | WARN_ON(ptr > end); |
| 1268 | break; |
| 1269 | } |
| 1270 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1271 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1272 | if (want < type) |
| 1273 | break; |
| 1274 | if (want > type) { |
| 1275 | ptr += btrfs_extent_inline_ref_size(type); |
| 1276 | continue; |
| 1277 | } |
| 1278 | |
| 1279 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1280 | struct btrfs_extent_data_ref *dref; |
| 1281 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1282 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1283 | owner, offset)) { |
| 1284 | err = 0; |
| 1285 | break; |
| 1286 | } |
| 1287 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1288 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1289 | break; |
| 1290 | } else { |
| 1291 | u64 ref_offset; |
| 1292 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1293 | if (parent > 0) { |
| 1294 | if (parent == ref_offset) { |
| 1295 | err = 0; |
| 1296 | break; |
| 1297 | } |
| 1298 | if (ref_offset < parent) |
| 1299 | break; |
| 1300 | } else { |
| 1301 | if (root_objectid == ref_offset) { |
| 1302 | err = 0; |
| 1303 | break; |
| 1304 | } |
| 1305 | if (ref_offset < root_objectid) |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
| 1309 | ptr += btrfs_extent_inline_ref_size(type); |
| 1310 | } |
| 1311 | if (err == -ENOENT && insert) { |
| 1312 | if (item_size + extra_size >= |
| 1313 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1314 | err = -EAGAIN; |
| 1315 | goto out; |
| 1316 | } |
| 1317 | /* |
| 1318 | * To add new inline back ref, we have to make sure |
| 1319 | * there is no corresponding back ref item. |
| 1320 | * For simplicity, we just do not add new inline back |
| 1321 | * ref if there is any kind of item for this block |
| 1322 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1323 | if (find_next_key(path, 0, &key) == 0 && |
| 1324 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1325 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1326 | err = -EAGAIN; |
| 1327 | goto out; |
| 1328 | } |
| 1329 | } |
| 1330 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1331 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1332 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1333 | path->keep_locks = 0; |
| 1334 | btrfs_unlock_up_safe(path, 1); |
| 1335 | } |
| 1336 | return err; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | * helper to add new inline back ref |
| 1341 | */ |
| 1342 | static noinline_for_stack |
| 1343 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1344 | struct btrfs_root *root, |
| 1345 | struct btrfs_path *path, |
| 1346 | struct btrfs_extent_inline_ref *iref, |
| 1347 | u64 parent, u64 root_objectid, |
| 1348 | u64 owner, u64 offset, int refs_to_add, |
| 1349 | struct btrfs_delayed_extent_op *extent_op) |
| 1350 | { |
| 1351 | struct extent_buffer *leaf; |
| 1352 | struct btrfs_extent_item *ei; |
| 1353 | unsigned long ptr; |
| 1354 | unsigned long end; |
| 1355 | unsigned long item_offset; |
| 1356 | u64 refs; |
| 1357 | int size; |
| 1358 | int type; |
| 1359 | int ret; |
| 1360 | |
| 1361 | leaf = path->nodes[0]; |
| 1362 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1363 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1364 | |
| 1365 | type = extent_ref_type(parent, owner); |
| 1366 | size = btrfs_extent_inline_ref_size(type); |
| 1367 | |
| 1368 | ret = btrfs_extend_item(trans, root, path, size); |
| 1369 | BUG_ON(ret); |
| 1370 | |
| 1371 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1372 | refs = btrfs_extent_refs(leaf, ei); |
| 1373 | refs += refs_to_add; |
| 1374 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1375 | if (extent_op) |
| 1376 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1377 | |
| 1378 | ptr = (unsigned long)ei + item_offset; |
| 1379 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1380 | if (ptr < end - size) |
| 1381 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1382 | end - size - ptr); |
| 1383 | |
| 1384 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1385 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1386 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1387 | struct btrfs_extent_data_ref *dref; |
| 1388 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1389 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1390 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1391 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1392 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1393 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1394 | struct btrfs_shared_data_ref *sref; |
| 1395 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1396 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1397 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1398 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1399 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1400 | } else { |
| 1401 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1402 | } |
| 1403 | btrfs_mark_buffer_dirty(leaf); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1408 | struct btrfs_root *root, |
| 1409 | struct btrfs_path *path, |
| 1410 | struct btrfs_extent_inline_ref **ref_ret, |
| 1411 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1412 | u64 root_objectid, u64 owner, u64 offset) |
| 1413 | { |
| 1414 | int ret; |
| 1415 | |
| 1416 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1417 | bytenr, num_bytes, parent, |
| 1418 | root_objectid, owner, offset, 0); |
| 1419 | if (ret != -ENOENT) |
| 1420 | return ret; |
| 1421 | |
| 1422 | btrfs_release_path(root, path); |
| 1423 | *ref_ret = NULL; |
| 1424 | |
| 1425 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1426 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1427 | root_objectid); |
| 1428 | } else { |
| 1429 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1430 | root_objectid, owner, offset); |
| 1431 | } |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * helper to update/remove inline back ref |
| 1437 | */ |
| 1438 | static noinline_for_stack |
| 1439 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1440 | struct btrfs_root *root, |
| 1441 | struct btrfs_path *path, |
| 1442 | struct btrfs_extent_inline_ref *iref, |
| 1443 | int refs_to_mod, |
| 1444 | struct btrfs_delayed_extent_op *extent_op) |
| 1445 | { |
| 1446 | struct extent_buffer *leaf; |
| 1447 | struct btrfs_extent_item *ei; |
| 1448 | struct btrfs_extent_data_ref *dref = NULL; |
| 1449 | struct btrfs_shared_data_ref *sref = NULL; |
| 1450 | unsigned long ptr; |
| 1451 | unsigned long end; |
| 1452 | u32 item_size; |
| 1453 | int size; |
| 1454 | int type; |
| 1455 | int ret; |
| 1456 | u64 refs; |
| 1457 | |
| 1458 | leaf = path->nodes[0]; |
| 1459 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1460 | refs = btrfs_extent_refs(leaf, ei); |
| 1461 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1462 | refs += refs_to_mod; |
| 1463 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1464 | if (extent_op) |
| 1465 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1466 | |
| 1467 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1468 | |
| 1469 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1470 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1471 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1472 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1473 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1474 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1475 | } else { |
| 1476 | refs = 1; |
| 1477 | BUG_ON(refs_to_mod != -1); |
| 1478 | } |
| 1479 | |
| 1480 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1481 | refs += refs_to_mod; |
| 1482 | |
| 1483 | if (refs > 0) { |
| 1484 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1485 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1486 | else |
| 1487 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1488 | } else { |
| 1489 | size = btrfs_extent_inline_ref_size(type); |
| 1490 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1491 | ptr = (unsigned long)iref; |
| 1492 | end = (unsigned long)ei + item_size; |
| 1493 | if (ptr + size < end) |
| 1494 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1495 | end - ptr - size); |
| 1496 | item_size -= size; |
| 1497 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1498 | BUG_ON(ret); |
| 1499 | } |
| 1500 | btrfs_mark_buffer_dirty(leaf); |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
| 1504 | static noinline_for_stack |
| 1505 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1506 | struct btrfs_root *root, |
| 1507 | struct btrfs_path *path, |
| 1508 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1509 | u64 root_objectid, u64 owner, |
| 1510 | u64 offset, int refs_to_add, |
| 1511 | struct btrfs_delayed_extent_op *extent_op) |
| 1512 | { |
| 1513 | struct btrfs_extent_inline_ref *iref; |
| 1514 | int ret; |
| 1515 | |
| 1516 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1517 | bytenr, num_bytes, parent, |
| 1518 | root_objectid, owner, offset, 1); |
| 1519 | if (ret == 0) { |
| 1520 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1521 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1522 | refs_to_add, extent_op); |
| 1523 | } else if (ret == -ENOENT) { |
| 1524 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1525 | parent, root_objectid, |
| 1526 | owner, offset, refs_to_add, |
| 1527 | extent_op); |
| 1528 | } |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1533 | struct btrfs_root *root, |
| 1534 | struct btrfs_path *path, |
| 1535 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1536 | u64 owner, u64 offset, int refs_to_add) |
| 1537 | { |
| 1538 | int ret; |
| 1539 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1540 | BUG_ON(refs_to_add != 1); |
| 1541 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1542 | parent, root_objectid); |
| 1543 | } else { |
| 1544 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1545 | parent, root_objectid, |
| 1546 | owner, offset, refs_to_add); |
| 1547 | } |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
| 1551 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1552 | struct btrfs_root *root, |
| 1553 | struct btrfs_path *path, |
| 1554 | struct btrfs_extent_inline_ref *iref, |
| 1555 | int refs_to_drop, int is_data) |
| 1556 | { |
| 1557 | int ret; |
| 1558 | |
| 1559 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1560 | if (iref) { |
| 1561 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1562 | -refs_to_drop, NULL); |
| 1563 | } else if (is_data) { |
| 1564 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1565 | } else { |
| 1566 | ret = btrfs_del_item(trans, root, path); |
| 1567 | } |
| 1568 | return ret; |
| 1569 | } |
| 1570 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1571 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1572 | u64 start, u64 len) |
| 1573 | { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1574 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1575 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1576 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1577 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1578 | u64 num_bytes) |
| 1579 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1580 | int ret; |
| 1581 | u64 map_length = num_bytes; |
| 1582 | struct btrfs_multi_bio *multi = NULL; |
| 1583 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1584 | if (!btrfs_test_opt(root, DISCARD)) |
| 1585 | return 0; |
| 1586 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1587 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1588 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1589 | bytenr, &map_length, &multi, 0); |
| 1590 | if (!ret) { |
| 1591 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1592 | int i; |
| 1593 | |
| 1594 | if (map_length > num_bytes) |
| 1595 | map_length = num_bytes; |
| 1596 | |
| 1597 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1598 | btrfs_issue_discard(stripe->dev->bdev, |
| 1599 | stripe->physical, |
| 1600 | map_length); |
| 1601 | } |
| 1602 | kfree(multi); |
| 1603 | } |
| 1604 | |
| 1605 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1606 | } |
| 1607 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1608 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1609 | struct btrfs_root *root, |
| 1610 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1611 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1612 | { |
| 1613 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1614 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1615 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1616 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1617 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1618 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1619 | parent, root_objectid, (int)owner, |
| 1620 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1621 | } else { |
| 1622 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1623 | parent, root_objectid, owner, offset, |
| 1624 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1625 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1626 | return ret; |
| 1627 | } |
| 1628 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1629 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1630 | struct btrfs_root *root, |
| 1631 | u64 bytenr, u64 num_bytes, |
| 1632 | u64 parent, u64 root_objectid, |
| 1633 | u64 owner, u64 offset, int refs_to_add, |
| 1634 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1635 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1636 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1637 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1638 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1639 | u64 refs; |
| 1640 | int ret; |
| 1641 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1642 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1643 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1644 | if (!path) |
| 1645 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1646 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1647 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1648 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1649 | /* this will setup the path even if it fails to insert the back ref */ |
| 1650 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1651 | path, bytenr, num_bytes, parent, |
| 1652 | root_objectid, owner, offset, |
| 1653 | refs_to_add, extent_op); |
| 1654 | if (ret == 0) |
| 1655 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1656 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1657 | if (ret != -EAGAIN) { |
| 1658 | err = ret; |
| 1659 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1660 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1661 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1662 | leaf = path->nodes[0]; |
| 1663 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1664 | refs = btrfs_extent_refs(leaf, item); |
| 1665 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1666 | if (extent_op) |
| 1667 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1668 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1669 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1670 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1671 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1672 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1673 | path->leave_spinning = 1; |
| 1674 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1675 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1676 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1677 | path, bytenr, parent, root_objectid, |
| 1678 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1679 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1680 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1681 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1682 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1683 | } |
| 1684 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1685 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1686 | struct btrfs_root *root, |
| 1687 | struct btrfs_delayed_ref_node *node, |
| 1688 | struct btrfs_delayed_extent_op *extent_op, |
| 1689 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1690 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1691 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1692 | struct btrfs_delayed_data_ref *ref; |
| 1693 | struct btrfs_key ins; |
| 1694 | u64 parent = 0; |
| 1695 | u64 ref_root = 0; |
| 1696 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1697 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1698 | ins.objectid = node->bytenr; |
| 1699 | ins.offset = node->num_bytes; |
| 1700 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1701 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1702 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1703 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1704 | parent = ref->parent; |
| 1705 | else |
| 1706 | ref_root = ref->root; |
| 1707 | |
| 1708 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1709 | if (extent_op) { |
| 1710 | BUG_ON(extent_op->update_key); |
| 1711 | flags |= extent_op->flags_to_set; |
| 1712 | } |
| 1713 | ret = alloc_reserved_file_extent(trans, root, |
| 1714 | parent, ref_root, flags, |
| 1715 | ref->objectid, ref->offset, |
| 1716 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1717 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1718 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1719 | node->num_bytes, parent, |
| 1720 | ref_root, ref->objectid, |
| 1721 | ref->offset, node->ref_mod, |
| 1722 | extent_op); |
| 1723 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1724 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1725 | node->num_bytes, parent, |
| 1726 | ref_root, ref->objectid, |
| 1727 | ref->offset, node->ref_mod, |
| 1728 | extent_op); |
| 1729 | } else { |
| 1730 | BUG(); |
| 1731 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1732 | return ret; |
| 1733 | } |
| 1734 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1735 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1736 | struct extent_buffer *leaf, |
| 1737 | struct btrfs_extent_item *ei) |
| 1738 | { |
| 1739 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1740 | if (extent_op->update_flags) { |
| 1741 | flags |= extent_op->flags_to_set; |
| 1742 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1743 | } |
| 1744 | |
| 1745 | if (extent_op->update_key) { |
| 1746 | struct btrfs_tree_block_info *bi; |
| 1747 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1748 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1749 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1754 | struct btrfs_root *root, |
| 1755 | struct btrfs_delayed_ref_node *node, |
| 1756 | struct btrfs_delayed_extent_op *extent_op) |
| 1757 | { |
| 1758 | struct btrfs_key key; |
| 1759 | struct btrfs_path *path; |
| 1760 | struct btrfs_extent_item *ei; |
| 1761 | struct extent_buffer *leaf; |
| 1762 | u32 item_size; |
| 1763 | int ret; |
| 1764 | int err = 0; |
| 1765 | |
| 1766 | path = btrfs_alloc_path(); |
| 1767 | if (!path) |
| 1768 | return -ENOMEM; |
| 1769 | |
| 1770 | key.objectid = node->bytenr; |
| 1771 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1772 | key.offset = node->num_bytes; |
| 1773 | |
| 1774 | path->reada = 1; |
| 1775 | path->leave_spinning = 1; |
| 1776 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1777 | path, 0, 1); |
| 1778 | if (ret < 0) { |
| 1779 | err = ret; |
| 1780 | goto out; |
| 1781 | } |
| 1782 | if (ret > 0) { |
| 1783 | err = -EIO; |
| 1784 | goto out; |
| 1785 | } |
| 1786 | |
| 1787 | leaf = path->nodes[0]; |
| 1788 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1789 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1790 | if (item_size < sizeof(*ei)) { |
| 1791 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1792 | path, (u64)-1, 0); |
| 1793 | if (ret < 0) { |
| 1794 | err = ret; |
| 1795 | goto out; |
| 1796 | } |
| 1797 | leaf = path->nodes[0]; |
| 1798 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1799 | } |
| 1800 | #endif |
| 1801 | BUG_ON(item_size < sizeof(*ei)); |
| 1802 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1803 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1804 | |
| 1805 | btrfs_mark_buffer_dirty(leaf); |
| 1806 | out: |
| 1807 | btrfs_free_path(path); |
| 1808 | return err; |
| 1809 | } |
| 1810 | |
| 1811 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1812 | struct btrfs_root *root, |
| 1813 | struct btrfs_delayed_ref_node *node, |
| 1814 | struct btrfs_delayed_extent_op *extent_op, |
| 1815 | int insert_reserved) |
| 1816 | { |
| 1817 | int ret = 0; |
| 1818 | struct btrfs_delayed_tree_ref *ref; |
| 1819 | struct btrfs_key ins; |
| 1820 | u64 parent = 0; |
| 1821 | u64 ref_root = 0; |
| 1822 | |
| 1823 | ins.objectid = node->bytenr; |
| 1824 | ins.offset = node->num_bytes; |
| 1825 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1826 | |
| 1827 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1828 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1829 | parent = ref->parent; |
| 1830 | else |
| 1831 | ref_root = ref->root; |
| 1832 | |
| 1833 | BUG_ON(node->ref_mod != 1); |
| 1834 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1835 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1836 | !extent_op->update_key); |
| 1837 | ret = alloc_reserved_tree_block(trans, root, |
| 1838 | parent, ref_root, |
| 1839 | extent_op->flags_to_set, |
| 1840 | &extent_op->key, |
| 1841 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1842 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1843 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1844 | node->num_bytes, parent, ref_root, |
| 1845 | ref->level, 0, 1, extent_op); |
| 1846 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1847 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1848 | node->num_bytes, parent, ref_root, |
| 1849 | ref->level, 0, 1, extent_op); |
| 1850 | } else { |
| 1851 | BUG(); |
| 1852 | } |
| 1853 | return ret; |
| 1854 | } |
| 1855 | |
| 1856 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1857 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1858 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1859 | struct btrfs_root *root, |
| 1860 | struct btrfs_delayed_ref_node *node, |
| 1861 | struct btrfs_delayed_extent_op *extent_op, |
| 1862 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1863 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1864 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1865 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1866 | struct btrfs_delayed_ref_head *head; |
| 1867 | /* |
| 1868 | * we've hit the end of the chain and we were supposed |
| 1869 | * to insert this extent into the tree. But, it got |
| 1870 | * deleted before we ever needed to insert it, so all |
| 1871 | * we have to do is clean up the accounting |
| 1872 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1873 | BUG_ON(extent_op); |
| 1874 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1875 | if (insert_reserved) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1876 | int mark_free = 0; |
| 1877 | struct extent_buffer *must_clean = NULL; |
| 1878 | |
| 1879 | ret = pin_down_bytes(trans, root, NULL, |
| 1880 | node->bytenr, node->num_bytes, |
| 1881 | head->is_data, 1, &must_clean); |
| 1882 | if (ret > 0) |
| 1883 | mark_free = 1; |
| 1884 | |
| 1885 | if (must_clean) { |
| 1886 | clean_tree_block(NULL, root, must_clean); |
| 1887 | btrfs_tree_unlock(must_clean); |
| 1888 | free_extent_buffer(must_clean); |
| 1889 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1890 | if (head->is_data) { |
| 1891 | ret = btrfs_del_csums(trans, root, |
| 1892 | node->bytenr, |
| 1893 | node->num_bytes); |
| 1894 | BUG_ON(ret); |
| 1895 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1896 | if (mark_free) { |
| 1897 | ret = btrfs_free_reserved_extent(root, |
| 1898 | node->bytenr, |
| 1899 | node->num_bytes); |
| 1900 | BUG_ON(ret); |
| 1901 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1902 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1903 | mutex_unlock(&head->mutex); |
| 1904 | return 0; |
| 1905 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1906 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1907 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1908 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1909 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 1910 | insert_reserved); |
| 1911 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1912 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1913 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 1914 | insert_reserved); |
| 1915 | else |
| 1916 | BUG(); |
| 1917 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1918 | } |
| 1919 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1920 | static noinline struct btrfs_delayed_ref_node * |
| 1921 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1922 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1923 | struct rb_node *node; |
| 1924 | struct btrfs_delayed_ref_node *ref; |
| 1925 | int action = BTRFS_ADD_DELAYED_REF; |
| 1926 | again: |
| 1927 | /* |
| 1928 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1929 | * this prevents ref count from going down to zero when |
| 1930 | * there still are pending delayed ref. |
| 1931 | */ |
| 1932 | node = rb_prev(&head->node.rb_node); |
| 1933 | while (1) { |
| 1934 | if (!node) |
| 1935 | break; |
| 1936 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1937 | rb_node); |
| 1938 | if (ref->bytenr != head->node.bytenr) |
| 1939 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1940 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1941 | return ref; |
| 1942 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1943 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1944 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 1945 | action = BTRFS_DROP_DELAYED_REF; |
| 1946 | goto again; |
| 1947 | } |
| 1948 | return NULL; |
| 1949 | } |
| 1950 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1951 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 1952 | struct btrfs_root *root, |
| 1953 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1954 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1955 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1956 | struct btrfs_delayed_ref_node *ref; |
| 1957 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1958 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1959 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1960 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1961 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1962 | |
| 1963 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1964 | while (1) { |
| 1965 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1966 | /* pick a new head ref from the cluster list */ |
| 1967 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1968 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1969 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1970 | locked_ref = list_entry(cluster->next, |
| 1971 | struct btrfs_delayed_ref_head, cluster); |
| 1972 | |
| 1973 | /* grab the lock that says we are going to process |
| 1974 | * all the refs for this head */ |
| 1975 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 1976 | |
| 1977 | /* |
| 1978 | * we may have dropped the spin lock to get the head |
| 1979 | * mutex lock, and that might have given someone else |
| 1980 | * time to free the head. If that's true, it has been |
| 1981 | * removed from our list and we can move on. |
| 1982 | */ |
| 1983 | if (ret == -EAGAIN) { |
| 1984 | locked_ref = NULL; |
| 1985 | count++; |
| 1986 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * record the must insert reserved flag before we |
| 1992 | * drop the spin lock. |
| 1993 | */ |
| 1994 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1995 | locked_ref->must_insert_reserved = 0; |
| 1996 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1997 | extent_op = locked_ref->extent_op; |
| 1998 | locked_ref->extent_op = NULL; |
| 1999 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2000 | /* |
| 2001 | * locked_ref is the head node, so we have to go one |
| 2002 | * node back for any delayed ref updates |
| 2003 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2004 | ref = select_delayed_ref(locked_ref); |
| 2005 | if (!ref) { |
| 2006 | /* All delayed refs have been processed, Go ahead |
| 2007 | * and send the head node to run_one_delayed_ref, |
| 2008 | * so that any accounting fixes can happen |
| 2009 | */ |
| 2010 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2011 | |
| 2012 | if (extent_op && must_insert_reserved) { |
| 2013 | kfree(extent_op); |
| 2014 | extent_op = NULL; |
| 2015 | } |
| 2016 | |
| 2017 | if (extent_op) { |
| 2018 | spin_unlock(&delayed_refs->lock); |
| 2019 | |
| 2020 | ret = run_delayed_extent_op(trans, root, |
| 2021 | ref, extent_op); |
| 2022 | BUG_ON(ret); |
| 2023 | kfree(extent_op); |
| 2024 | |
| 2025 | cond_resched(); |
| 2026 | spin_lock(&delayed_refs->lock); |
| 2027 | continue; |
| 2028 | } |
| 2029 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2030 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2031 | locked_ref = NULL; |
| 2032 | } |
| 2033 | |
| 2034 | ref->in_tree = 0; |
| 2035 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2036 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2037 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2038 | spin_unlock(&delayed_refs->lock); |
| 2039 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2040 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2041 | must_insert_reserved); |
| 2042 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2043 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2044 | btrfs_put_delayed_ref(ref); |
| 2045 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2046 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2047 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2048 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2049 | spin_lock(&delayed_refs->lock); |
| 2050 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2051 | return count; |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * this starts processing the delayed reference count updates and |
| 2056 | * extent insertions we have queued up so far. count can be |
| 2057 | * 0, which means to process everything in the tree at the start |
| 2058 | * of the run (but not newly added entries), or it can be some target |
| 2059 | * number you'd like to process. |
| 2060 | */ |
| 2061 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2062 | struct btrfs_root *root, unsigned long count) |
| 2063 | { |
| 2064 | struct rb_node *node; |
| 2065 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2066 | struct btrfs_delayed_ref_node *ref; |
| 2067 | struct list_head cluster; |
| 2068 | int ret; |
| 2069 | int run_all = count == (unsigned long)-1; |
| 2070 | int run_most = 0; |
| 2071 | |
| 2072 | if (root == root->fs_info->extent_root) |
| 2073 | root = root->fs_info->tree_root; |
| 2074 | |
| 2075 | delayed_refs = &trans->transaction->delayed_refs; |
| 2076 | INIT_LIST_HEAD(&cluster); |
| 2077 | again: |
| 2078 | spin_lock(&delayed_refs->lock); |
| 2079 | if (count == 0) { |
| 2080 | count = delayed_refs->num_entries * 2; |
| 2081 | run_most = 1; |
| 2082 | } |
| 2083 | while (1) { |
| 2084 | if (!(run_all || run_most) && |
| 2085 | delayed_refs->num_heads_ready < 64) |
| 2086 | break; |
| 2087 | |
| 2088 | /* |
| 2089 | * go find something we can process in the rbtree. We start at |
| 2090 | * the beginning of the tree, and then build a cluster |
| 2091 | * of refs to process starting at the first one we are able to |
| 2092 | * lock |
| 2093 | */ |
| 2094 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2095 | delayed_refs->run_delayed_start); |
| 2096 | if (ret) |
| 2097 | break; |
| 2098 | |
| 2099 | ret = run_clustered_refs(trans, root, &cluster); |
| 2100 | BUG_ON(ret < 0); |
| 2101 | |
| 2102 | count -= min_t(unsigned long, ret, count); |
| 2103 | |
| 2104 | if (count == 0) |
| 2105 | break; |
| 2106 | } |
| 2107 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2108 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2109 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2110 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2111 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2112 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2113 | |
| 2114 | while (node) { |
| 2115 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2116 | rb_node); |
| 2117 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2118 | struct btrfs_delayed_ref_head *head; |
| 2119 | |
| 2120 | head = btrfs_delayed_node_to_head(ref); |
| 2121 | atomic_inc(&ref->refs); |
| 2122 | |
| 2123 | spin_unlock(&delayed_refs->lock); |
| 2124 | mutex_lock(&head->mutex); |
| 2125 | mutex_unlock(&head->mutex); |
| 2126 | |
| 2127 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2128 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2129 | goto again; |
| 2130 | } |
| 2131 | node = rb_next(node); |
| 2132 | } |
| 2133 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2134 | schedule_timeout(1); |
| 2135 | goto again; |
| 2136 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2137 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2138 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2139 | return 0; |
| 2140 | } |
| 2141 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2142 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2143 | struct btrfs_root *root, |
| 2144 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2145 | int is_data) |
| 2146 | { |
| 2147 | struct btrfs_delayed_extent_op *extent_op; |
| 2148 | int ret; |
| 2149 | |
| 2150 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2151 | if (!extent_op) |
| 2152 | return -ENOMEM; |
| 2153 | |
| 2154 | extent_op->flags_to_set = flags; |
| 2155 | extent_op->update_flags = 1; |
| 2156 | extent_op->update_key = 0; |
| 2157 | extent_op->is_data = is_data ? 1 : 0; |
| 2158 | |
| 2159 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2160 | if (ret) |
| 2161 | kfree(extent_op); |
| 2162 | return ret; |
| 2163 | } |
| 2164 | |
| 2165 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2166 | struct btrfs_root *root, |
| 2167 | struct btrfs_path *path, |
| 2168 | u64 objectid, u64 offset, u64 bytenr) |
| 2169 | { |
| 2170 | struct btrfs_delayed_ref_head *head; |
| 2171 | struct btrfs_delayed_ref_node *ref; |
| 2172 | struct btrfs_delayed_data_ref *data_ref; |
| 2173 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2174 | struct rb_node *node; |
| 2175 | int ret = 0; |
| 2176 | |
| 2177 | ret = -ENOENT; |
| 2178 | delayed_refs = &trans->transaction->delayed_refs; |
| 2179 | spin_lock(&delayed_refs->lock); |
| 2180 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2181 | if (!head) |
| 2182 | goto out; |
| 2183 | |
| 2184 | if (!mutex_trylock(&head->mutex)) { |
| 2185 | atomic_inc(&head->node.refs); |
| 2186 | spin_unlock(&delayed_refs->lock); |
| 2187 | |
| 2188 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2189 | |
| 2190 | mutex_lock(&head->mutex); |
| 2191 | mutex_unlock(&head->mutex); |
| 2192 | btrfs_put_delayed_ref(&head->node); |
| 2193 | return -EAGAIN; |
| 2194 | } |
| 2195 | |
| 2196 | node = rb_prev(&head->node.rb_node); |
| 2197 | if (!node) |
| 2198 | goto out_unlock; |
| 2199 | |
| 2200 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2201 | |
| 2202 | if (ref->bytenr != bytenr) |
| 2203 | goto out_unlock; |
| 2204 | |
| 2205 | ret = 1; |
| 2206 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2207 | goto out_unlock; |
| 2208 | |
| 2209 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2210 | |
| 2211 | node = rb_prev(node); |
| 2212 | if (node) { |
| 2213 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2214 | if (ref->bytenr == bytenr) |
| 2215 | goto out_unlock; |
| 2216 | } |
| 2217 | |
| 2218 | if (data_ref->root != root->root_key.objectid || |
| 2219 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2220 | goto out_unlock; |
| 2221 | |
| 2222 | ret = 0; |
| 2223 | out_unlock: |
| 2224 | mutex_unlock(&head->mutex); |
| 2225 | out: |
| 2226 | spin_unlock(&delayed_refs->lock); |
| 2227 | return ret; |
| 2228 | } |
| 2229 | |
| 2230 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2231 | struct btrfs_root *root, |
| 2232 | struct btrfs_path *path, |
| 2233 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2234 | { |
| 2235 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2236 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2237 | struct btrfs_extent_data_ref *ref; |
| 2238 | struct btrfs_extent_inline_ref *iref; |
| 2239 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2240 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2241 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2242 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2243 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2244 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2245 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2246 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2247 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2248 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2249 | if (ret < 0) |
| 2250 | goto out; |
| 2251 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2252 | |
| 2253 | ret = -ENOENT; |
| 2254 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2255 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2256 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2257 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2258 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2259 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
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 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2262 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2263 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2264 | ret = 1; |
| 2265 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2266 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2267 | if (item_size < sizeof(*ei)) { |
| 2268 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2269 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2270 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2271 | #endif |
| 2272 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2273 | |
| 2274 | if (item_size != sizeof(*ei) + |
| 2275 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2276 | goto out; |
| 2277 | |
| 2278 | if (btrfs_extent_generation(leaf, ei) <= |
| 2279 | btrfs_root_last_snapshot(&root->root_item)) |
| 2280 | goto out; |
| 2281 | |
| 2282 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2283 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2284 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2285 | goto out; |
| 2286 | |
| 2287 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2288 | if (btrfs_extent_refs(leaf, ei) != |
| 2289 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2290 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2291 | root->root_key.objectid || |
| 2292 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2293 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2294 | goto out; |
| 2295 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2296 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2297 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2298 | return ret; |
| 2299 | } |
| 2300 | |
| 2301 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2302 | struct btrfs_root *root, |
| 2303 | u64 objectid, u64 offset, u64 bytenr) |
| 2304 | { |
| 2305 | struct btrfs_path *path; |
| 2306 | int ret; |
| 2307 | int ret2; |
| 2308 | |
| 2309 | path = btrfs_alloc_path(); |
| 2310 | if (!path) |
| 2311 | return -ENOENT; |
| 2312 | |
| 2313 | do { |
| 2314 | ret = check_committed_ref(trans, root, path, objectid, |
| 2315 | offset, bytenr); |
| 2316 | if (ret && ret != -ENOENT) |
| 2317 | goto out; |
| 2318 | |
| 2319 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2320 | offset, bytenr); |
| 2321 | } while (ret2 == -EAGAIN); |
| 2322 | |
| 2323 | if (ret2 && ret2 != -ENOENT) { |
| 2324 | ret = ret2; |
| 2325 | goto out; |
| 2326 | } |
| 2327 | |
| 2328 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2329 | ret = 0; |
| 2330 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2331 | btrfs_free_path(path); |
| 2332 | return ret; |
| 2333 | } |
| 2334 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2335 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2336 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2337 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2338 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2339 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2340 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2341 | u64 root_gen; |
| 2342 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2343 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2344 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2345 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2346 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2347 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2348 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2349 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2350 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2351 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2352 | shared = 0; |
| 2353 | root_gen = root->root_key.offset; |
| 2354 | } else { |
| 2355 | shared = 1; |
| 2356 | root_gen = trans->transid - 1; |
| 2357 | } |
| 2358 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2359 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2360 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2361 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2362 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2363 | struct btrfs_leaf_ref *ref; |
| 2364 | struct btrfs_extent_info *info; |
| 2365 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2366 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2367 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2368 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2369 | goto out; |
| 2370 | } |
| 2371 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2372 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2373 | ref->bytenr = buf->start; |
| 2374 | ref->owner = btrfs_header_owner(buf); |
| 2375 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2376 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2377 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2378 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2379 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2380 | u64 disk_bytenr; |
| 2381 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2382 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2383 | continue; |
| 2384 | fi = btrfs_item_ptr(buf, i, |
| 2385 | struct btrfs_file_extent_item); |
| 2386 | if (btrfs_file_extent_type(buf, fi) == |
| 2387 | BTRFS_FILE_EXTENT_INLINE) |
| 2388 | continue; |
| 2389 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2390 | if (disk_bytenr == 0) |
| 2391 | continue; |
| 2392 | |
| 2393 | info->bytenr = disk_bytenr; |
| 2394 | info->num_bytes = |
| 2395 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2396 | info->objectid = key.objectid; |
| 2397 | info->offset = key.offset; |
| 2398 | info++; |
| 2399 | } |
| 2400 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2401 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2402 | if (ret == -EEXIST && shared) { |
| 2403 | struct btrfs_leaf_ref *old; |
| 2404 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2405 | BUG_ON(!old); |
| 2406 | btrfs_remove_leaf_ref(root, old); |
| 2407 | btrfs_free_leaf_ref(root, old); |
| 2408 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2409 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2410 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2411 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2412 | } |
| 2413 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2414 | return ret; |
| 2415 | } |
| 2416 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2417 | /* when a block goes through cow, we update the reference counts of |
| 2418 | * everything that block points to. The internal pointers of the block |
| 2419 | * can be in just about any order, and it is likely to have clusters of |
| 2420 | * things that are close together and clusters of things that are not. |
| 2421 | * |
| 2422 | * To help reduce the seeks that come with updating all of these reference |
| 2423 | * counts, sort them by byte number before actual updates are done. |
| 2424 | * |
| 2425 | * struct refsort is used to match byte number to slot in the btree block. |
| 2426 | * we sort based on the byte number and then use the slot to actually |
| 2427 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2428 | * |
| 2429 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2430 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2431 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2432 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2433 | */ |
| 2434 | struct refsort { |
| 2435 | u64 bytenr; |
| 2436 | u32 slot; |
| 2437 | }; |
| 2438 | |
| 2439 | /* |
| 2440 | * for passing into sort() |
| 2441 | */ |
| 2442 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2443 | { |
| 2444 | const struct refsort *a = a_void; |
| 2445 | const struct refsort *b = b_void; |
| 2446 | |
| 2447 | if (a->bytenr < b->bytenr) |
| 2448 | return -1; |
| 2449 | if (a->bytenr > b->bytenr) |
| 2450 | return 1; |
| 2451 | return 0; |
| 2452 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2453 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2454 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2455 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2456 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2457 | struct extent_buffer *buf, |
| 2458 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2459 | { |
| 2460 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2461 | u64 num_bytes; |
| 2462 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2463 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2464 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2465 | struct btrfs_key key; |
| 2466 | struct btrfs_file_extent_item *fi; |
| 2467 | int i; |
| 2468 | int level; |
| 2469 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2470 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2471 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2472 | |
| 2473 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2474 | nritems = btrfs_header_nritems(buf); |
| 2475 | level = btrfs_header_level(buf); |
| 2476 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2477 | if (!root->ref_cows && level == 0) |
| 2478 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2479 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2480 | if (inc) |
| 2481 | process_func = btrfs_inc_extent_ref; |
| 2482 | else |
| 2483 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2484 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2485 | if (full_backref) |
| 2486 | parent = buf->start; |
| 2487 | else |
| 2488 | parent = 0; |
| 2489 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2490 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2491 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2492 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2493 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2494 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2495 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2496 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2497 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2498 | BTRFS_FILE_EXTENT_INLINE) |
| 2499 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2500 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2501 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2502 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2503 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2504 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2505 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2506 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2507 | parent, ref_root, key.objectid, |
| 2508 | key.offset); |
| 2509 | if (ret) |
| 2510 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2511 | } else { |
| 2512 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2513 | num_bytes = btrfs_level_size(root, level - 1); |
| 2514 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2515 | parent, ref_root, level - 1, 0); |
| 2516 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2517 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2518 | } |
| 2519 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2520 | return 0; |
| 2521 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2522 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2523 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2524 | } |
| 2525 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2526 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2527 | struct extent_buffer *buf, int full_backref) |
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 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2530 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2531 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2532 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2533 | struct extent_buffer *buf, int full_backref) |
| 2534 | { |
| 2535 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2536 | } |
| 2537 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2538 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2539 | struct btrfs_root *root, |
| 2540 | struct btrfs_path *path, |
| 2541 | struct btrfs_block_group_cache *cache) |
| 2542 | { |
| 2543 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2544 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2545 | unsigned long bi; |
| 2546 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2547 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2548 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2549 | if (ret < 0) |
| 2550 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2551 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2552 | |
| 2553 | leaf = path->nodes[0]; |
| 2554 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2555 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2556 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2557 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2558 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2559 | if (ret) |
| 2560 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2561 | return 0; |
| 2562 | |
| 2563 | } |
| 2564 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2565 | static struct btrfs_block_group_cache * |
| 2566 | next_block_group(struct btrfs_root *root, |
| 2567 | struct btrfs_block_group_cache *cache) |
| 2568 | { |
| 2569 | struct rb_node *node; |
| 2570 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2571 | node = rb_next(&cache->cache_node); |
| 2572 | btrfs_put_block_group(cache); |
| 2573 | if (node) { |
| 2574 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2575 | cache_node); |
| 2576 | atomic_inc(&cache->count); |
| 2577 | } else |
| 2578 | cache = NULL; |
| 2579 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2580 | return cache; |
| 2581 | } |
| 2582 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2583 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2584 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2585 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2586 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2587 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2588 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2589 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2590 | |
| 2591 | path = btrfs_alloc_path(); |
| 2592 | if (!path) |
| 2593 | return -ENOMEM; |
| 2594 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2595 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2596 | if (last == 0) { |
| 2597 | err = btrfs_run_delayed_refs(trans, root, |
| 2598 | (unsigned long)-1); |
| 2599 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2600 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2601 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2602 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2603 | while (cache) { |
| 2604 | if (cache->dirty) |
| 2605 | break; |
| 2606 | cache = next_block_group(root, cache); |
| 2607 | } |
| 2608 | if (!cache) { |
| 2609 | if (last == 0) |
| 2610 | break; |
| 2611 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2612 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2613 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2614 | |
| 2615 | cache->dirty = 0; |
| 2616 | last = cache->key.objectid + cache->key.offset; |
| 2617 | |
| 2618 | err = write_one_cache_group(trans, root, path, cache); |
| 2619 | BUG_ON(err); |
| 2620 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2621 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2622 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2623 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2624 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2625 | } |
| 2626 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2627 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2628 | { |
| 2629 | struct btrfs_block_group_cache *block_group; |
| 2630 | int readonly = 0; |
| 2631 | |
| 2632 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2633 | if (!block_group || block_group->ro) |
| 2634 | readonly = 1; |
| 2635 | if (block_group) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2636 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2637 | return readonly; |
| 2638 | } |
| 2639 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2640 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2641 | u64 total_bytes, u64 bytes_used, |
| 2642 | struct btrfs_space_info **space_info) |
| 2643 | { |
| 2644 | struct btrfs_space_info *found; |
| 2645 | |
| 2646 | found = __find_space_info(info, flags); |
| 2647 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2648 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2649 | found->total_bytes += total_bytes; |
| 2650 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2651 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2652 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2653 | *space_info = found; |
| 2654 | return 0; |
| 2655 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2656 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2657 | if (!found) |
| 2658 | return -ENOMEM; |
| 2659 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2660 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2661 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2662 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2663 | found->flags = flags; |
| 2664 | found->total_bytes = total_bytes; |
| 2665 | found->bytes_used = bytes_used; |
| 2666 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2667 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2668 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2669 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2670 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2671 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2672 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2673 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2674 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2675 | return 0; |
| 2676 | } |
| 2677 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2678 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2679 | { |
| 2680 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2681 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2682 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2683 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2684 | if (extra_flags) { |
| 2685 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2686 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2687 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2688 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2689 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2690 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2691 | } |
| 2692 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2693 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2694 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 2695 | { |
| 2696 | spin_lock(&cache->space_info->lock); |
| 2697 | spin_lock(&cache->lock); |
| 2698 | if (!cache->ro) { |
| 2699 | cache->space_info->bytes_readonly += cache->key.offset - |
| 2700 | btrfs_block_group_used(&cache->item); |
| 2701 | cache->ro = 1; |
| 2702 | } |
| 2703 | spin_unlock(&cache->lock); |
| 2704 | spin_unlock(&cache->space_info->lock); |
| 2705 | } |
| 2706 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2707 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2708 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2709 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2710 | |
| 2711 | if (num_devices == 1) |
| 2712 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2713 | if (num_devices < 4) |
| 2714 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2715 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2716 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2717 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2718 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2719 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2720 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2721 | |
| 2722 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2723 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2724 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2725 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2726 | |
| 2727 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2728 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2729 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2730 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2731 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2732 | return flags; |
| 2733 | } |
| 2734 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2735 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 2736 | { |
| 2737 | struct btrfs_fs_info *info = root->fs_info; |
| 2738 | u64 alloc_profile; |
| 2739 | |
| 2740 | if (data) { |
| 2741 | alloc_profile = info->avail_data_alloc_bits & |
| 2742 | info->data_alloc_profile; |
| 2743 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 2744 | } else if (root == root->fs_info->chunk_root) { |
| 2745 | alloc_profile = info->avail_system_alloc_bits & |
| 2746 | info->system_alloc_profile; |
| 2747 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 2748 | } else { |
| 2749 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2750 | info->metadata_alloc_profile; |
| 2751 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 2752 | } |
| 2753 | |
| 2754 | return btrfs_reduce_alloc_profile(root, data); |
| 2755 | } |
| 2756 | |
| 2757 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2758 | { |
| 2759 | u64 alloc_target; |
| 2760 | |
| 2761 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2762 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 2763 | alloc_target); |
| 2764 | } |
| 2765 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2766 | static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items) |
| 2767 | { |
| 2768 | u64 num_bytes; |
| 2769 | int level; |
| 2770 | |
| 2771 | level = BTRFS_MAX_LEVEL - 2; |
| 2772 | /* |
| 2773 | * NOTE: these calculations are absolutely the worst possible case. |
| 2774 | * This assumes that _every_ item we insert will require a new leaf, and |
| 2775 | * that the tree has grown to its maximum level size. |
| 2776 | */ |
| 2777 | |
| 2778 | /* |
| 2779 | * for every item we insert we could insert both an extent item and a |
| 2780 | * extent ref item. Then for ever item we insert, we will need to cow |
| 2781 | * both the original leaf, plus the leaf to the left and right of it. |
| 2782 | * |
| 2783 | * Unless we are talking about the extent root, then we just want the |
| 2784 | * number of items * 2, since we just need the extent item plus its ref. |
| 2785 | */ |
| 2786 | if (root == root->fs_info->extent_root) |
| 2787 | num_bytes = num_items * 2; |
| 2788 | else |
| 2789 | num_bytes = (num_items + (2 * num_items)) * 3; |
| 2790 | |
| 2791 | /* |
| 2792 | * num_bytes is total number of leaves we could need times the leaf |
| 2793 | * size, and then for every leaf we could end up cow'ing 2 nodes per |
| 2794 | * level, down to the leaf level. |
| 2795 | */ |
| 2796 | num_bytes = (num_bytes * root->leafsize) + |
| 2797 | (num_bytes * (level * 2)) * root->nodesize; |
| 2798 | |
| 2799 | return num_bytes; |
| 2800 | } |
| 2801 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2802 | /* |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2803 | * Unreserve metadata space for delalloc. If we have less reserved credits than |
| 2804 | * we have extents, this function does nothing. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2805 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2806 | int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root, |
| 2807 | struct inode *inode, int num_items) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2808 | { |
| 2809 | struct btrfs_fs_info *info = root->fs_info; |
| 2810 | struct btrfs_space_info *meta_sinfo; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2811 | u64 num_bytes; |
| 2812 | u64 alloc_target; |
| 2813 | bool bug = false; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2814 | |
| 2815 | /* get the space info for where the metadata will live */ |
| 2816 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 2817 | meta_sinfo = __find_space_info(info, alloc_target); |
| 2818 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2819 | num_bytes = calculate_bytes_needed(root->fs_info->extent_root, |
| 2820 | num_items); |
| 2821 | |
| 2822 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2823 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 2824 | if (BTRFS_I(inode)->reserved_extents <= |
| 2825 | BTRFS_I(inode)->outstanding_extents) { |
| 2826 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2827 | spin_unlock(&meta_sinfo->lock); |
| 2828 | return 0; |
| 2829 | } |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2830 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2831 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2832 | BTRFS_I(inode)->reserved_extents--; |
| 2833 | BUG_ON(BTRFS_I(inode)->reserved_extents < 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2834 | |
| 2835 | if (meta_sinfo->bytes_delalloc < num_bytes) { |
| 2836 | bug = true; |
| 2837 | meta_sinfo->bytes_delalloc = 0; |
| 2838 | } else { |
| 2839 | meta_sinfo->bytes_delalloc -= num_bytes; |
| 2840 | } |
| 2841 | spin_unlock(&meta_sinfo->lock); |
| 2842 | |
| 2843 | BUG_ON(bug); |
| 2844 | |
| 2845 | return 0; |
| 2846 | } |
| 2847 | |
| 2848 | static void check_force_delalloc(struct btrfs_space_info *meta_sinfo) |
| 2849 | { |
| 2850 | u64 thresh; |
| 2851 | |
| 2852 | thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 2853 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 2854 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 2855 | meta_sinfo->bytes_may_use; |
| 2856 | |
| 2857 | thresh = meta_sinfo->total_bytes - thresh; |
| 2858 | thresh *= 80; |
| 2859 | do_div(thresh, 100); |
| 2860 | if (thresh <= meta_sinfo->bytes_delalloc) |
| 2861 | meta_sinfo->force_delalloc = 1; |
| 2862 | else |
| 2863 | meta_sinfo->force_delalloc = 0; |
| 2864 | } |
| 2865 | |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2866 | struct async_flush { |
| 2867 | struct btrfs_root *root; |
| 2868 | struct btrfs_space_info *info; |
| 2869 | struct btrfs_work work; |
| 2870 | }; |
| 2871 | |
| 2872 | static noinline void flush_delalloc_async(struct btrfs_work *work) |
| 2873 | { |
| 2874 | struct async_flush *async; |
| 2875 | struct btrfs_root *root; |
| 2876 | struct btrfs_space_info *info; |
| 2877 | |
| 2878 | async = container_of(work, struct async_flush, work); |
| 2879 | root = async->root; |
| 2880 | info = async->info; |
| 2881 | |
| 2882 | btrfs_start_delalloc_inodes(root); |
| 2883 | wake_up(&info->flush_wait); |
| 2884 | btrfs_wait_ordered_extents(root, 0); |
| 2885 | |
| 2886 | spin_lock(&info->lock); |
| 2887 | info->flushing = 0; |
| 2888 | spin_unlock(&info->lock); |
| 2889 | wake_up(&info->flush_wait); |
| 2890 | |
| 2891 | kfree(async); |
| 2892 | } |
| 2893 | |
| 2894 | static void wait_on_flush(struct btrfs_space_info *info) |
| 2895 | { |
| 2896 | DEFINE_WAIT(wait); |
| 2897 | u64 used; |
| 2898 | |
| 2899 | while (1) { |
| 2900 | prepare_to_wait(&info->flush_wait, &wait, |
| 2901 | TASK_UNINTERRUPTIBLE); |
| 2902 | spin_lock(&info->lock); |
| 2903 | if (!info->flushing) { |
| 2904 | spin_unlock(&info->lock); |
| 2905 | break; |
| 2906 | } |
| 2907 | |
| 2908 | used = info->bytes_used + info->bytes_reserved + |
| 2909 | info->bytes_pinned + info->bytes_readonly + |
| 2910 | info->bytes_super + info->bytes_root + |
| 2911 | info->bytes_may_use + info->bytes_delalloc; |
| 2912 | if (used < info->total_bytes) { |
| 2913 | spin_unlock(&info->lock); |
| 2914 | break; |
| 2915 | } |
| 2916 | spin_unlock(&info->lock); |
| 2917 | schedule(); |
| 2918 | } |
| 2919 | finish_wait(&info->flush_wait, &wait); |
| 2920 | } |
| 2921 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2922 | static void flush_delalloc(struct btrfs_root *root, |
| 2923 | struct btrfs_space_info *info) |
| 2924 | { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2925 | struct async_flush *async; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2926 | bool wait = false; |
| 2927 | |
| 2928 | spin_lock(&info->lock); |
| 2929 | |
| 2930 | if (!info->flushing) { |
| 2931 | info->flushing = 1; |
| 2932 | init_waitqueue_head(&info->flush_wait); |
| 2933 | } else { |
| 2934 | wait = true; |
| 2935 | } |
| 2936 | |
| 2937 | spin_unlock(&info->lock); |
| 2938 | |
| 2939 | if (wait) { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2940 | wait_on_flush(info); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2941 | return; |
| 2942 | } |
| 2943 | |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2944 | async = kzalloc(sizeof(*async), GFP_NOFS); |
| 2945 | if (!async) |
| 2946 | goto flush; |
| 2947 | |
| 2948 | async->root = root; |
| 2949 | async->info = info; |
| 2950 | async->work.func = flush_delalloc_async; |
| 2951 | |
| 2952 | btrfs_queue_worker(&root->fs_info->enospc_workers, |
| 2953 | &async->work); |
| 2954 | wait_on_flush(info); |
| 2955 | return; |
| 2956 | |
| 2957 | flush: |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2958 | btrfs_start_delalloc_inodes(root); |
| 2959 | btrfs_wait_ordered_extents(root, 0); |
| 2960 | |
| 2961 | spin_lock(&info->lock); |
| 2962 | info->flushing = 0; |
| 2963 | spin_unlock(&info->lock); |
| 2964 | wake_up(&info->flush_wait); |
| 2965 | } |
| 2966 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2967 | static int maybe_allocate_chunk(struct btrfs_root *root, |
| 2968 | struct btrfs_space_info *info) |
| 2969 | { |
| 2970 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
| 2971 | struct btrfs_trans_handle *trans; |
| 2972 | bool wait = false; |
| 2973 | int ret = 0; |
| 2974 | u64 min_metadata; |
| 2975 | u64 free_space; |
| 2976 | |
| 2977 | free_space = btrfs_super_total_bytes(disk_super); |
| 2978 | /* |
| 2979 | * we allow the metadata to grow to a max of either 5gb or 5% of the |
| 2980 | * space in the volume. |
| 2981 | */ |
| 2982 | min_metadata = min((u64)5 * 1024 * 1024 * 1024, |
| 2983 | div64_u64(free_space * 5, 100)); |
| 2984 | if (info->total_bytes >= min_metadata) { |
| 2985 | spin_unlock(&info->lock); |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | if (info->full) { |
| 2990 | spin_unlock(&info->lock); |
| 2991 | return 0; |
| 2992 | } |
| 2993 | |
| 2994 | if (!info->allocating_chunk) { |
| 2995 | info->force_alloc = 1; |
| 2996 | info->allocating_chunk = 1; |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2997 | init_waitqueue_head(&info->allocate_wait); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2998 | } else { |
| 2999 | wait = true; |
| 3000 | } |
| 3001 | |
| 3002 | spin_unlock(&info->lock); |
| 3003 | |
| 3004 | if (wait) { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3005 | wait_event(info->allocate_wait, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3006 | !info->allocating_chunk); |
| 3007 | return 1; |
| 3008 | } |
| 3009 | |
| 3010 | trans = btrfs_start_transaction(root, 1); |
| 3011 | if (!trans) { |
| 3012 | ret = -ENOMEM; |
| 3013 | goto out; |
| 3014 | } |
| 3015 | |
| 3016 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3017 | 4096 + 2 * 1024 * 1024, |
| 3018 | info->flags, 0); |
| 3019 | btrfs_end_transaction(trans, root); |
| 3020 | if (ret) |
| 3021 | goto out; |
| 3022 | out: |
| 3023 | spin_lock(&info->lock); |
| 3024 | info->allocating_chunk = 0; |
| 3025 | spin_unlock(&info->lock); |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3026 | wake_up(&info->allocate_wait); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3027 | |
| 3028 | if (ret) |
| 3029 | return 0; |
| 3030 | return 1; |
| 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * Reserve metadata space for delalloc. |
| 3035 | */ |
| 3036 | int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root, |
| 3037 | struct inode *inode, int num_items) |
| 3038 | { |
| 3039 | struct btrfs_fs_info *info = root->fs_info; |
| 3040 | struct btrfs_space_info *meta_sinfo; |
| 3041 | u64 num_bytes; |
| 3042 | u64 used; |
| 3043 | u64 alloc_target; |
| 3044 | int flushed = 0; |
| 3045 | int force_delalloc; |
| 3046 | |
| 3047 | /* get the space info for where the metadata will live */ |
| 3048 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3049 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3050 | |
| 3051 | num_bytes = calculate_bytes_needed(root->fs_info->extent_root, |
| 3052 | num_items); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3053 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3054 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3055 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3056 | force_delalloc = meta_sinfo->force_delalloc; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3057 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3058 | if (unlikely(!meta_sinfo->bytes_root)) |
| 3059 | meta_sinfo->bytes_root = calculate_bytes_needed(root, 6); |
| 3060 | |
| 3061 | if (!flushed) |
| 3062 | meta_sinfo->bytes_delalloc += num_bytes; |
| 3063 | |
| 3064 | used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 3065 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 3066 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 3067 | meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc; |
| 3068 | |
| 3069 | if (used > meta_sinfo->total_bytes) { |
| 3070 | flushed++; |
| 3071 | |
| 3072 | if (flushed == 1) { |
| 3073 | if (maybe_allocate_chunk(root, meta_sinfo)) |
| 3074 | goto again; |
| 3075 | flushed++; |
| 3076 | } else { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3077 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3078 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3079 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3080 | if (flushed == 2) { |
| 3081 | filemap_flush(inode->i_mapping); |
| 3082 | goto again; |
| 3083 | } else if (flushed == 3) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3084 | flush_delalloc(root, meta_sinfo); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3085 | goto again; |
| 3086 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3087 | spin_lock(&meta_sinfo->lock); |
| 3088 | meta_sinfo->bytes_delalloc -= num_bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3089 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3090 | printk(KERN_ERR "enospc, has %d, reserved %d\n", |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3091 | BTRFS_I(inode)->outstanding_extents, |
| 3092 | BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3093 | dump_space_info(meta_sinfo, 0, 0); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3094 | return -ENOSPC; |
| 3095 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3096 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3097 | BTRFS_I(inode)->reserved_extents++; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3098 | check_force_delalloc(meta_sinfo); |
| 3099 | spin_unlock(&meta_sinfo->lock); |
| 3100 | |
| 3101 | if (!flushed && force_delalloc) |
| 3102 | filemap_flush(inode->i_mapping); |
| 3103 | |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
| 3107 | /* |
| 3108 | * unreserve num_items number of items worth of metadata space. This needs to |
| 3109 | * be paired with btrfs_reserve_metadata_space. |
| 3110 | * |
| 3111 | * NOTE: if you have the option, run this _AFTER_ you do a |
| 3112 | * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref |
| 3113 | * oprations which will result in more used metadata, so we want to make sure we |
| 3114 | * can do that without issue. |
| 3115 | */ |
| 3116 | int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items) |
| 3117 | { |
| 3118 | struct btrfs_fs_info *info = root->fs_info; |
| 3119 | struct btrfs_space_info *meta_sinfo; |
| 3120 | u64 num_bytes; |
| 3121 | u64 alloc_target; |
| 3122 | bool bug = false; |
| 3123 | |
| 3124 | /* get the space info for where the metadata will live */ |
| 3125 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3126 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3127 | |
| 3128 | num_bytes = calculate_bytes_needed(root, num_items); |
| 3129 | |
| 3130 | spin_lock(&meta_sinfo->lock); |
| 3131 | if (meta_sinfo->bytes_may_use < num_bytes) { |
| 3132 | bug = true; |
| 3133 | meta_sinfo->bytes_may_use = 0; |
| 3134 | } else { |
| 3135 | meta_sinfo->bytes_may_use -= num_bytes; |
| 3136 | } |
| 3137 | spin_unlock(&meta_sinfo->lock); |
| 3138 | |
| 3139 | BUG_ON(bug); |
| 3140 | |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
| 3144 | /* |
| 3145 | * Reserve some metadata space for use. We'll calculate the worste case number |
| 3146 | * of bytes that would be needed to modify num_items number of items. If we |
| 3147 | * have space, fantastic, if not, you get -ENOSPC. Please call |
| 3148 | * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of |
| 3149 | * items you reserved, since whatever metadata you needed should have already |
| 3150 | * been allocated. |
| 3151 | * |
| 3152 | * This will commit the transaction to make more space if we don't have enough |
| 3153 | * metadata space. THe only time we don't do this is if we're reserving space |
| 3154 | * inside of a transaction, then we will just return -ENOSPC and it is the |
| 3155 | * callers responsibility to handle it properly. |
| 3156 | */ |
| 3157 | int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items) |
| 3158 | { |
| 3159 | struct btrfs_fs_info *info = root->fs_info; |
| 3160 | struct btrfs_space_info *meta_sinfo; |
| 3161 | u64 num_bytes; |
| 3162 | u64 used; |
| 3163 | u64 alloc_target; |
| 3164 | int retries = 0; |
| 3165 | |
| 3166 | /* get the space info for where the metadata will live */ |
| 3167 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3168 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3169 | |
| 3170 | num_bytes = calculate_bytes_needed(root, num_items); |
| 3171 | again: |
| 3172 | spin_lock(&meta_sinfo->lock); |
| 3173 | |
| 3174 | if (unlikely(!meta_sinfo->bytes_root)) |
| 3175 | meta_sinfo->bytes_root = calculate_bytes_needed(root, 6); |
| 3176 | |
| 3177 | if (!retries) |
| 3178 | meta_sinfo->bytes_may_use += num_bytes; |
| 3179 | |
| 3180 | used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 3181 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 3182 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 3183 | meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc; |
| 3184 | |
| 3185 | if (used > meta_sinfo->total_bytes) { |
| 3186 | retries++; |
| 3187 | if (retries == 1) { |
| 3188 | if (maybe_allocate_chunk(root, meta_sinfo)) |
| 3189 | goto again; |
| 3190 | retries++; |
| 3191 | } else { |
| 3192 | spin_unlock(&meta_sinfo->lock); |
| 3193 | } |
| 3194 | |
| 3195 | if (retries == 2) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3196 | flush_delalloc(root, meta_sinfo); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3197 | goto again; |
| 3198 | } |
| 3199 | spin_lock(&meta_sinfo->lock); |
| 3200 | meta_sinfo->bytes_may_use -= num_bytes; |
| 3201 | spin_unlock(&meta_sinfo->lock); |
| 3202 | |
| 3203 | dump_space_info(meta_sinfo, 0, 0); |
| 3204 | return -ENOSPC; |
| 3205 | } |
| 3206 | |
| 3207 | check_force_delalloc(meta_sinfo); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3208 | spin_unlock(&meta_sinfo->lock); |
| 3209 | |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * This will check the space that the inode allocates from to make sure we have |
| 3215 | * enough space for bytes. |
| 3216 | */ |
| 3217 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 3218 | u64 bytes) |
| 3219 | { |
| 3220 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3221 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3222 | |
| 3223 | /* make sure bytes are sectorsize aligned */ |
| 3224 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3225 | |
| 3226 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3227 | if (!data_sinfo) |
| 3228 | goto alloc; |
| 3229 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3230 | again: |
| 3231 | /* make sure we have enough space to handle the data first */ |
| 3232 | spin_lock(&data_sinfo->lock); |
| 3233 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 3234 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 3235 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3236 | data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3237 | struct btrfs_trans_handle *trans; |
| 3238 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3239 | /* |
| 3240 | * if we don't have enough free bytes in this space then we need |
| 3241 | * to alloc a new chunk. |
| 3242 | */ |
| 3243 | if (!data_sinfo->full) { |
| 3244 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3245 | |
| 3246 | data_sinfo->force_alloc = 1; |
| 3247 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3248 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3249 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 3250 | trans = btrfs_start_transaction(root, 1); |
| 3251 | if (!trans) |
| 3252 | return -ENOMEM; |
| 3253 | |
| 3254 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3255 | bytes + 2 * 1024 * 1024, |
| 3256 | alloc_target, 0); |
| 3257 | btrfs_end_transaction(trans, root); |
| 3258 | if (ret) |
| 3259 | return ret; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3260 | |
| 3261 | if (!data_sinfo) { |
| 3262 | btrfs_set_inode_space_info(root, inode); |
| 3263 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3264 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3265 | goto again; |
| 3266 | } |
| 3267 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3268 | |
| 3269 | /* commit the current transaction and try again */ |
Sage Weil | dd7e0b7 | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3270 | if (!committed && !root->fs_info->open_ioctl_trans) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3271 | committed = 1; |
| 3272 | trans = btrfs_join_transaction(root, 1); |
| 3273 | if (!trans) |
| 3274 | return -ENOMEM; |
| 3275 | ret = btrfs_commit_transaction(trans, root); |
| 3276 | if (ret) |
| 3277 | return ret; |
| 3278 | goto again; |
| 3279 | } |
| 3280 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3281 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 3282 | ", %llu bytes_used, %llu bytes_reserved, " |
Hu Tao | 68f5a38 | 2009-07-02 13:55:45 -0400 | [diff] [blame] | 3283 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use " |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 3284 | "%llu total\n", (unsigned long long)bytes, |
| 3285 | (unsigned long long)data_sinfo->bytes_delalloc, |
| 3286 | (unsigned long long)data_sinfo->bytes_used, |
| 3287 | (unsigned long long)data_sinfo->bytes_reserved, |
| 3288 | (unsigned long long)data_sinfo->bytes_pinned, |
| 3289 | (unsigned long long)data_sinfo->bytes_readonly, |
| 3290 | (unsigned long long)data_sinfo->bytes_may_use, |
| 3291 | (unsigned long long)data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3292 | return -ENOSPC; |
| 3293 | } |
| 3294 | data_sinfo->bytes_may_use += bytes; |
| 3295 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3296 | spin_unlock(&data_sinfo->lock); |
| 3297 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3298 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * if there was an error for whatever reason after calling |
| 3303 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 3304 | */ |
| 3305 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 3306 | struct inode *inode, u64 bytes) |
| 3307 | { |
| 3308 | struct btrfs_space_info *data_sinfo; |
| 3309 | |
| 3310 | /* make sure bytes are sectorsize aligned */ |
| 3311 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3312 | |
| 3313 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3314 | spin_lock(&data_sinfo->lock); |
| 3315 | data_sinfo->bytes_may_use -= bytes; |
| 3316 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3317 | spin_unlock(&data_sinfo->lock); |
| 3318 | } |
| 3319 | |
| 3320 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 3321 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 3322 | u64 bytes) |
| 3323 | { |
| 3324 | struct btrfs_space_info *data_sinfo; |
| 3325 | |
| 3326 | /* get the space info for where this inode will be storing its data */ |
| 3327 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3328 | |
| 3329 | /* make sure we have enough space to handle the data first */ |
| 3330 | spin_lock(&data_sinfo->lock); |
| 3331 | data_sinfo->bytes_delalloc += bytes; |
| 3332 | |
| 3333 | /* |
| 3334 | * we are adding a delalloc extent without calling |
| 3335 | * btrfs_check_data_free_space first. This happens on a weird |
| 3336 | * writepage condition, but shouldn't hurt our accounting |
| 3337 | */ |
| 3338 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 3339 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 3340 | BTRFS_I(inode)->reserved_bytes = 0; |
| 3341 | } else { |
| 3342 | data_sinfo->bytes_may_use -= bytes; |
| 3343 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3344 | } |
| 3345 | |
| 3346 | spin_unlock(&data_sinfo->lock); |
| 3347 | } |
| 3348 | |
| 3349 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 3350 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 3351 | u64 bytes) |
| 3352 | { |
| 3353 | struct btrfs_space_info *info; |
| 3354 | |
| 3355 | info = BTRFS_I(inode)->space_info; |
| 3356 | |
| 3357 | spin_lock(&info->lock); |
| 3358 | info->bytes_delalloc -= bytes; |
| 3359 | spin_unlock(&info->lock); |
| 3360 | } |
| 3361 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3362 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3363 | { |
| 3364 | struct list_head *head = &info->space_info; |
| 3365 | struct btrfs_space_info *found; |
| 3366 | |
| 3367 | rcu_read_lock(); |
| 3368 | list_for_each_entry_rcu(found, head, list) { |
| 3369 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3370 | found->force_alloc = 1; |
| 3371 | } |
| 3372 | rcu_read_unlock(); |
| 3373 | } |
| 3374 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3375 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3376 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3377 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3378 | { |
| 3379 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3380 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3381 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3382 | int ret = 0; |
| 3383 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3384 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3385 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3386 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3387 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3388 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3389 | if (!space_info) { |
| 3390 | ret = update_space_info(extent_root->fs_info, flags, |
| 3391 | 0, 0, &space_info); |
| 3392 | BUG_ON(ret); |
| 3393 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3394 | BUG_ON(!space_info); |
| 3395 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3396 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3397 | if (space_info->force_alloc) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3398 | force = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3399 | if (space_info->full) { |
| 3400 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3401 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3402 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3403 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3404 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3405 | thresh = div_factor(thresh, 8); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3406 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3407 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3408 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 3409 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3410 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3411 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3412 | spin_unlock(&space_info->lock); |
| 3413 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3414 | /* |
| 3415 | * if we're doing a data chunk, go ahead and make sure that |
| 3416 | * we keep a reasonable number of metadata chunks allocated in the |
| 3417 | * FS as well. |
| 3418 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3419 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3420 | fs_info->data_chunk_allocations++; |
| 3421 | if (!(fs_info->data_chunk_allocations % |
| 3422 | fs_info->metadata_ratio)) |
| 3423 | force_metadata_allocation(fs_info); |
| 3424 | } |
| 3425 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3426 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3427 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3428 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3429 | space_info->full = 1; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3430 | space_info->force_alloc = 0; |
| 3431 | spin_unlock(&space_info->lock); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3432 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3433 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3434 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3435 | } |
| 3436 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3437 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 3438 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3439 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3440 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3441 | { |
| 3442 | struct btrfs_block_group_cache *cache; |
| 3443 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3444 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3445 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3446 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3447 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3448 | /* block accounting for super block */ |
| 3449 | spin_lock(&info->delalloc_lock); |
| 3450 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 3451 | if (alloc) |
| 3452 | old_val += num_bytes; |
| 3453 | else |
| 3454 | old_val -= num_bytes; |
| 3455 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
| 3456 | |
| 3457 | /* block accounting for root item */ |
| 3458 | old_val = btrfs_root_used(&root->root_item); |
| 3459 | if (alloc) |
| 3460 | old_val += num_bytes; |
| 3461 | else |
| 3462 | old_val -= num_bytes; |
| 3463 | btrfs_set_root_used(&root->root_item, old_val); |
| 3464 | spin_unlock(&info->delalloc_lock); |
| 3465 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3466 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3467 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3468 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3469 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3470 | byte_in_group = bytenr - cache->key.objectid; |
| 3471 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3472 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3473 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3474 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3475 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3476 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3477 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3478 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3479 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3480 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3481 | cache->reserved -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3482 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3483 | cache->space_info->bytes_reserved -= num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 3484 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3485 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3486 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3487 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3488 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3489 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3490 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3491 | if (cache->ro) |
| 3492 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3493 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3494 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3495 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3496 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3497 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3498 | |
| 3499 | ret = btrfs_discard_extent(root, bytenr, |
| 3500 | num_bytes); |
| 3501 | WARN_ON(ret); |
| 3502 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3503 | ret = btrfs_add_free_space(cache, bytenr, |
| 3504 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3505 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3506 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3507 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3508 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3509 | total -= num_bytes; |
| 3510 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3511 | } |
| 3512 | return 0; |
| 3513 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3514 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3515 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 3516 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3517 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3518 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3519 | |
| 3520 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 3521 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3522 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3523 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3524 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3525 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3526 | |
| 3527 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3528 | } |
| 3529 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3530 | /* |
| 3531 | * this function must be called within transaction |
| 3532 | */ |
| 3533 | int btrfs_pin_extent(struct btrfs_root *root, |
| 3534 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3535 | { |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3536 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3537 | struct btrfs_block_group_cache *cache; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3538 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3539 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 3540 | BUG_ON(!cache); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3541 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3542 | spin_lock(&cache->space_info->lock); |
| 3543 | spin_lock(&cache->lock); |
| 3544 | cache->pinned += num_bytes; |
| 3545 | cache->space_info->bytes_pinned += num_bytes; |
| 3546 | if (reserved) { |
| 3547 | cache->reserved -= num_bytes; |
| 3548 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3549 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3550 | spin_unlock(&cache->lock); |
| 3551 | spin_unlock(&cache->space_info->lock); |
| 3552 | |
| 3553 | btrfs_put_block_group(cache); |
| 3554 | |
| 3555 | set_extent_dirty(fs_info->pinned_extents, |
| 3556 | bytenr, bytenr + num_bytes - 1, GFP_NOFS); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3557 | return 0; |
| 3558 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3559 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3560 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 3561 | u64 num_bytes, int reserve) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3562 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3563 | spin_lock(&cache->space_info->lock); |
| 3564 | spin_lock(&cache->lock); |
| 3565 | if (reserve) { |
| 3566 | cache->reserved += num_bytes; |
| 3567 | cache->space_info->bytes_reserved += num_bytes; |
| 3568 | } else { |
| 3569 | cache->reserved -= num_bytes; |
| 3570 | cache->space_info->bytes_reserved -= num_bytes; |
| 3571 | } |
| 3572 | spin_unlock(&cache->lock); |
| 3573 | spin_unlock(&cache->space_info->lock); |
| 3574 | return 0; |
| 3575 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3576 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3577 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 3578 | struct btrfs_root *root) |
| 3579 | { |
| 3580 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3581 | struct btrfs_caching_control *next; |
| 3582 | struct btrfs_caching_control *caching_ctl; |
| 3583 | struct btrfs_block_group_cache *cache; |
| 3584 | |
| 3585 | down_write(&fs_info->extent_commit_sem); |
| 3586 | |
| 3587 | list_for_each_entry_safe(caching_ctl, next, |
| 3588 | &fs_info->caching_block_groups, list) { |
| 3589 | cache = caching_ctl->block_group; |
| 3590 | if (block_group_cache_done(cache)) { |
| 3591 | cache->last_byte_to_unpin = (u64)-1; |
| 3592 | list_del_init(&caching_ctl->list); |
| 3593 | put_caching_control(caching_ctl); |
| 3594 | } else { |
| 3595 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3600 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 3601 | else |
| 3602 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 3603 | |
| 3604 | up_write(&fs_info->extent_commit_sem); |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
| 3608 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 3609 | { |
| 3610 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3611 | struct btrfs_block_group_cache *cache = NULL; |
| 3612 | u64 len; |
| 3613 | |
| 3614 | while (start <= end) { |
| 3615 | if (!cache || |
| 3616 | start >= cache->key.objectid + cache->key.offset) { |
| 3617 | if (cache) |
| 3618 | btrfs_put_block_group(cache); |
| 3619 | cache = btrfs_lookup_block_group(fs_info, start); |
| 3620 | BUG_ON(!cache); |
| 3621 | } |
| 3622 | |
| 3623 | len = cache->key.objectid + cache->key.offset - start; |
| 3624 | len = min(len, end + 1 - start); |
| 3625 | |
| 3626 | if (start < cache->last_byte_to_unpin) { |
| 3627 | len = min(len, cache->last_byte_to_unpin - start); |
| 3628 | btrfs_add_free_space(cache, start, len); |
| 3629 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3630 | |
| 3631 | spin_lock(&cache->space_info->lock); |
| 3632 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3633 | cache->pinned -= len; |
| 3634 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3635 | spin_unlock(&cache->lock); |
| 3636 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3637 | |
| 3638 | start += len; |
| 3639 | } |
| 3640 | |
| 3641 | if (cache) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3642 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3643 | return 0; |
| 3644 | } |
| 3645 | |
| 3646 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3647 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3648 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3649 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3650 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3651 | u64 start; |
| 3652 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3653 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3654 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3655 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3656 | unpin = &fs_info->freed_extents[1]; |
| 3657 | else |
| 3658 | unpin = &fs_info->freed_extents[0]; |
| 3659 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3660 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3661 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 3662 | EXTENT_DIRTY); |
| 3663 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3664 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3665 | |
| 3666 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 3667 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3668 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3669 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3670 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3671 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3672 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3673 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3674 | } |
| 3675 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3676 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 3677 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3678 | struct btrfs_path *path, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3679 | u64 bytenr, u64 num_bytes, |
| 3680 | int is_data, int reserved, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3681 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3682 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3683 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3684 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3685 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3686 | if (is_data) |
| 3687 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3688 | |
Chris Mason | 444528b | 2009-10-14 09:38:28 -0400 | [diff] [blame] | 3689 | /* |
| 3690 | * discard is sloooow, and so triggering discards on |
| 3691 | * individual btree blocks isn't a good plan. Just |
| 3692 | * pin everything in discard mode. |
| 3693 | */ |
| 3694 | if (btrfs_test_opt(root, DISCARD)) |
| 3695 | goto pinit; |
| 3696 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3697 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 3698 | if (!buf) |
| 3699 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3700 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3701 | /* we can reuse a block if it hasn't been written |
| 3702 | * and it is from this transaction. We can't |
| 3703 | * reuse anything from the tree log root because |
| 3704 | * it has tiny sub-transactions. |
| 3705 | */ |
| 3706 | if (btrfs_buffer_uptodate(buf, 0) && |
| 3707 | btrfs_try_tree_lock(buf)) { |
| 3708 | u64 header_owner = btrfs_header_owner(buf); |
| 3709 | u64 header_transid = btrfs_header_generation(buf); |
| 3710 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
| 3711 | header_transid == trans->transid && |
| 3712 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3713 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3714 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 3715 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3716 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 3717 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3718 | free_extent_buffer(buf); |
| 3719 | pinit: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3720 | if (path) |
| 3721 | btrfs_set_path_blocking(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3722 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3723 | btrfs_pin_extent(root, bytenr, num_bytes, reserved); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3724 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3725 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3726 | return 0; |
| 3727 | } |
| 3728 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3729 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 3730 | struct btrfs_root *root, |
| 3731 | u64 bytenr, u64 num_bytes, u64 parent, |
| 3732 | u64 root_objectid, u64 owner_objectid, |
| 3733 | u64 owner_offset, int refs_to_drop, |
| 3734 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3735 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3736 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3737 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3738 | struct btrfs_fs_info *info = root->fs_info; |
| 3739 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3740 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3741 | struct btrfs_extent_item *ei; |
| 3742 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3743 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3744 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3745 | int extent_slot = 0; |
| 3746 | int found_extent = 0; |
| 3747 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3748 | u32 item_size; |
| 3749 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 3750 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3751 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3752 | if (!path) |
| 3753 | return -ENOMEM; |
| 3754 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 3755 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3756 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3757 | |
| 3758 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 3759 | BUG_ON(!is_data && refs_to_drop != 1); |
| 3760 | |
| 3761 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 3762 | bytenr, num_bytes, parent, |
| 3763 | root_objectid, owner_objectid, |
| 3764 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3765 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3766 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3767 | while (extent_slot >= 0) { |
| 3768 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3769 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3770 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3771 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3772 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3773 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3774 | found_extent = 1; |
| 3775 | break; |
| 3776 | } |
| 3777 | if (path->slots[0] - extent_slot > 5) |
| 3778 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3779 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3780 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3781 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3782 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 3783 | if (found_extent && item_size < sizeof(*ei)) |
| 3784 | found_extent = 0; |
| 3785 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3786 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3787 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3788 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3789 | NULL, refs_to_drop, |
| 3790 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3791 | BUG_ON(ret); |
| 3792 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3793 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3794 | |
| 3795 | key.objectid = bytenr; |
| 3796 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3797 | key.offset = num_bytes; |
| 3798 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3799 | ret = btrfs_search_slot(trans, extent_root, |
| 3800 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3801 | if (ret) { |
| 3802 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3803 | ", was looking for %llu\n", ret, |
| 3804 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3805 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3806 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3807 | BUG_ON(ret); |
| 3808 | extent_slot = path->slots[0]; |
| 3809 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3810 | } else { |
| 3811 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3812 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3813 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3814 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3815 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3816 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3817 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3818 | (unsigned long long)owner_objectid, |
| 3819 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3820 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3821 | |
| 3822 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3823 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3824 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3825 | if (item_size < sizeof(*ei)) { |
| 3826 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 3827 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 3828 | owner_objectid, 0); |
| 3829 | BUG_ON(ret < 0); |
| 3830 | |
| 3831 | btrfs_release_path(extent_root, path); |
| 3832 | path->leave_spinning = 1; |
| 3833 | |
| 3834 | key.objectid = bytenr; |
| 3835 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3836 | key.offset = num_bytes; |
| 3837 | |
| 3838 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 3839 | -1, 1); |
| 3840 | if (ret) { |
| 3841 | printk(KERN_ERR "umm, got %d back from search" |
| 3842 | ", was looking for %llu\n", ret, |
| 3843 | (unsigned long long)bytenr); |
| 3844 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3845 | } |
| 3846 | BUG_ON(ret); |
| 3847 | extent_slot = path->slots[0]; |
| 3848 | leaf = path->nodes[0]; |
| 3849 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3850 | } |
| 3851 | #endif |
| 3852 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3853 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3854 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3855 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3856 | struct btrfs_tree_block_info *bi; |
| 3857 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3858 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3859 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3860 | } |
| 3861 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3862 | refs = btrfs_extent_refs(leaf, ei); |
| 3863 | BUG_ON(refs < refs_to_drop); |
| 3864 | refs -= refs_to_drop; |
| 3865 | |
| 3866 | if (refs > 0) { |
| 3867 | if (extent_op) |
| 3868 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3869 | /* |
| 3870 | * In the case of inline back ref, reference count will |
| 3871 | * be updated by remove_extent_backref |
| 3872 | */ |
| 3873 | if (iref) { |
| 3874 | BUG_ON(!found_extent); |
| 3875 | } else { |
| 3876 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3877 | btrfs_mark_buffer_dirty(leaf); |
| 3878 | } |
| 3879 | if (found_extent) { |
| 3880 | ret = remove_extent_backref(trans, extent_root, path, |
| 3881 | iref, refs_to_drop, |
| 3882 | is_data); |
| 3883 | BUG_ON(ret); |
| 3884 | } |
| 3885 | } else { |
| 3886 | int mark_free = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3887 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3888 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3889 | if (found_extent) { |
| 3890 | BUG_ON(is_data && refs_to_drop != |
| 3891 | extent_data_ref_count(root, path, iref)); |
| 3892 | if (iref) { |
| 3893 | BUG_ON(path->slots[0] != extent_slot); |
| 3894 | } else { |
| 3895 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3896 | path->slots[0] = extent_slot; |
| 3897 | num_to_del = 2; |
| 3898 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3899 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3900 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3901 | ret = pin_down_bytes(trans, root, path, bytenr, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3902 | num_bytes, is_data, 0, &must_clean); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3903 | if (ret > 0) |
| 3904 | mark_free = 1; |
| 3905 | BUG_ON(ret < 0); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3906 | /* |
| 3907 | * it is going to be very rare for someone to be waiting |
| 3908 | * on the block we're freeing. del_items might need to |
| 3909 | * schedule, so rather than get fancy, just force it |
| 3910 | * to blocking here |
| 3911 | */ |
| 3912 | if (must_clean) |
| 3913 | btrfs_set_lock_blocking(must_clean); |
| 3914 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3915 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3916 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3917 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3918 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3919 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3920 | if (must_clean) { |
| 3921 | clean_tree_block(NULL, root, must_clean); |
| 3922 | btrfs_tree_unlock(must_clean); |
| 3923 | free_extent_buffer(must_clean); |
| 3924 | } |
| 3925 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3926 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3927 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 3928 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 3929 | } else { |
| 3930 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 3931 | bytenr >> PAGE_CACHE_SHIFT, |
| 3932 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3933 | } |
| 3934 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 3935 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 3936 | mark_free); |
| 3937 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3938 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3939 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3940 | return ret; |
| 3941 | } |
| 3942 | |
| 3943 | /* |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3944 | * when we free an extent, it is possible (and likely) that we free the last |
| 3945 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3946 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3947 | * removes it from the tree. |
| 3948 | */ |
| 3949 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 3950 | struct btrfs_root *root, u64 bytenr) |
| 3951 | { |
| 3952 | struct btrfs_delayed_ref_head *head; |
| 3953 | struct btrfs_delayed_ref_root *delayed_refs; |
| 3954 | struct btrfs_delayed_ref_node *ref; |
| 3955 | struct rb_node *node; |
| 3956 | int ret; |
| 3957 | |
| 3958 | delayed_refs = &trans->transaction->delayed_refs; |
| 3959 | spin_lock(&delayed_refs->lock); |
| 3960 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 3961 | if (!head) |
| 3962 | goto out; |
| 3963 | |
| 3964 | node = rb_prev(&head->node.rb_node); |
| 3965 | if (!node) |
| 3966 | goto out; |
| 3967 | |
| 3968 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 3969 | |
| 3970 | /* there are still entries for this ref, we can't drop it */ |
| 3971 | if (ref->bytenr == bytenr) |
| 3972 | goto out; |
| 3973 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3974 | if (head->extent_op) { |
| 3975 | if (!head->must_insert_reserved) |
| 3976 | goto out; |
| 3977 | kfree(head->extent_op); |
| 3978 | head->extent_op = NULL; |
| 3979 | } |
| 3980 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3981 | /* |
| 3982 | * waiting for the lock here would deadlock. If someone else has it |
| 3983 | * locked they are already in the process of dropping it anyway |
| 3984 | */ |
| 3985 | if (!mutex_trylock(&head->mutex)) |
| 3986 | goto out; |
| 3987 | |
| 3988 | /* |
| 3989 | * at this point we have a head with no other entries. Go |
| 3990 | * ahead and process it. |
| 3991 | */ |
| 3992 | head->node.in_tree = 0; |
| 3993 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3994 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3995 | delayed_refs->num_entries--; |
| 3996 | |
| 3997 | /* |
| 3998 | * we don't take a ref on the node because we're removing it from the |
| 3999 | * tree, so we just steal the ref the tree was holding. |
| 4000 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4001 | delayed_refs->num_heads--; |
| 4002 | if (list_empty(&head->cluster)) |
| 4003 | delayed_refs->num_heads_ready--; |
| 4004 | |
| 4005 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4006 | spin_unlock(&delayed_refs->lock); |
| 4007 | |
| 4008 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4009 | &head->node, head->extent_op, |
| 4010 | head->must_insert_reserved); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4011 | BUG_ON(ret); |
| 4012 | btrfs_put_delayed_ref(&head->node); |
| 4013 | return 0; |
| 4014 | out: |
| 4015 | spin_unlock(&delayed_refs->lock); |
| 4016 | return 0; |
| 4017 | } |
| 4018 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4019 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4020 | struct btrfs_root *root, |
| 4021 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4022 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4023 | { |
| 4024 | int ret; |
| 4025 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4026 | /* |
| 4027 | * tree log blocks never actually go into the extent allocation |
| 4028 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4029 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4030 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4031 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4032 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4033 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4034 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4035 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4036 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4037 | parent, root_objectid, (int)owner, |
| 4038 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4039 | BUG_ON(ret); |
| 4040 | ret = check_ref_cleanup(trans, root, bytenr); |
| 4041 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4042 | } else { |
| 4043 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4044 | parent, root_objectid, owner, |
| 4045 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4046 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4047 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4048 | return ret; |
| 4049 | } |
| 4050 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4051 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4052 | { |
| 4053 | u64 mask = ((u64)root->stripesize - 1); |
| 4054 | u64 ret = (val + mask) & ~mask; |
| 4055 | return ret; |
| 4056 | } |
| 4057 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4058 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4059 | * when we wait for progress in the block group caching, its because |
| 4060 | * our allocation attempt failed at least once. So, we must sleep |
| 4061 | * and let some progress happen before we try again. |
| 4062 | * |
| 4063 | * This function will sleep at least once waiting for new free space to |
| 4064 | * show up, and then it will check the block group free space numbers |
| 4065 | * for our min num_bytes. Another option is to have it go ahead |
| 4066 | * and look in the rbtree for a free extent of a given size, but this |
| 4067 | * is a good start. |
| 4068 | */ |
| 4069 | static noinline int |
| 4070 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4071 | u64 num_bytes) |
| 4072 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4073 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4074 | DEFINE_WAIT(wait); |
| 4075 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4076 | caching_ctl = get_caching_control(cache); |
| 4077 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4078 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4079 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4080 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4081 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4082 | |
| 4083 | put_caching_control(caching_ctl); |
| 4084 | return 0; |
| 4085 | } |
| 4086 | |
| 4087 | static noinline int |
| 4088 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4089 | { |
| 4090 | struct btrfs_caching_control *caching_ctl; |
| 4091 | DEFINE_WAIT(wait); |
| 4092 | |
| 4093 | caching_ctl = get_caching_control(cache); |
| 4094 | if (!caching_ctl) |
| 4095 | return 0; |
| 4096 | |
| 4097 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4098 | |
| 4099 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4100 | return 0; |
| 4101 | } |
| 4102 | |
| 4103 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4104 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4105 | LOOP_CACHING_NOWAIT = 1, |
| 4106 | LOOP_CACHING_WAIT = 2, |
| 4107 | LOOP_ALLOC_CHUNK = 3, |
| 4108 | LOOP_NO_EMPTY_SIZE = 4, |
| 4109 | }; |
| 4110 | |
| 4111 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4112 | * walks the btree of allocated extents and find a hole of a given size. |
| 4113 | * The key ins is changed to record the hole: |
| 4114 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4115 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4116 | * ins->offset == number of blocks |
| 4117 | * Any available blocks before search_start are skipped. |
| 4118 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4119 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4120 | struct btrfs_root *orig_root, |
| 4121 | u64 num_bytes, u64 empty_size, |
| 4122 | u64 search_start, u64 search_end, |
| 4123 | u64 hint_byte, struct btrfs_key *ins, |
| 4124 | u64 exclude_start, u64 exclude_nr, |
| 4125 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4126 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4127 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4128 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4129 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4130 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4131 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4132 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4133 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4134 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4135 | int last_ptr_loop = 0; |
| 4136 | int loop = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4137 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4138 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4139 | bool failed_alloc = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4140 | u64 ideal_cache_percent = 0; |
| 4141 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4142 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4143 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4144 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4145 | ins->objectid = 0; |
| 4146 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4147 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4148 | space_info = __find_space_info(root->fs_info, data); |
| 4149 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4150 | if (orig_root->ref_cows || empty_size) |
| 4151 | allowed_chunk_alloc = 1; |
| 4152 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4153 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4154 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4155 | if (!btrfs_test_opt(root, SSD)) |
| 4156 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4157 | } |
| 4158 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4159 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 4160 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4161 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4162 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4163 | if (last_ptr) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4164 | spin_lock(&last_ptr->lock); |
| 4165 | if (last_ptr->block_group) |
| 4166 | hint_byte = last_ptr->window_start; |
| 4167 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4168 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4169 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4170 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4171 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4172 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4173 | if (!last_ptr) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4174 | empty_cluster = 0; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4175 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4176 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4177 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4178 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4179 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4180 | /* |
| 4181 | * we don't want to use the block group if it doesn't match our |
| 4182 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4183 | * |
| 4184 | * However if we are re-searching with an ideal block group |
| 4185 | * picked out then we don't care that the block group is cached. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4186 | */ |
| 4187 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4188 | (block_group->cached != BTRFS_CACHE_NO || |
| 4189 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4190 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4191 | if (list_empty(&block_group->list) || |
| 4192 | block_group->ro) { |
| 4193 | /* |
| 4194 | * someone is removing this block group, |
| 4195 | * we can't jump into the have_block_group |
| 4196 | * target because our list pointers are not |
| 4197 | * valid |
| 4198 | */ |
| 4199 | btrfs_put_block_group(block_group); |
| 4200 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4201 | } else { |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4202 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4203 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4204 | } else if (block_group) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4205 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4206 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4207 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4208 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4209 | down_read(&space_info->groups_sem); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4210 | list_for_each_entry(block_group, &space_info->block_groups, list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4211 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4212 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4213 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4214 | atomic_inc(&block_group->count); |
| 4215 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4216 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4217 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4218 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4219 | u64 free_percent; |
| 4220 | |
| 4221 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4222 | free_percent *= 100; |
| 4223 | free_percent = div64_u64(free_percent, |
| 4224 | block_group->key.offset); |
| 4225 | free_percent = 100 - free_percent; |
| 4226 | if (free_percent > ideal_cache_percent && |
| 4227 | likely(!block_group->ro)) { |
| 4228 | ideal_cache_offset = block_group->key.objectid; |
| 4229 | ideal_cache_percent = free_percent; |
| 4230 | } |
| 4231 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4232 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4233 | * We only want to start kthread caching if we are at |
| 4234 | * the point where we will wait for caching to make |
| 4235 | * progress, or if our ideal search is over and we've |
| 4236 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4237 | */ |
| 4238 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4239 | (loop > LOOP_FIND_IDEAL && |
| 4240 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4241 | ret = cache_block_group(block_group); |
| 4242 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4243 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4244 | found_uncached_bg = true; |
| 4245 | |
| 4246 | /* |
| 4247 | * If loop is set for cached only, try the next block |
| 4248 | * group. |
| 4249 | */ |
| 4250 | if (loop == LOOP_FIND_IDEAL) |
| 4251 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4252 | } |
| 4253 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4254 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4255 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4256 | found_uncached_bg = true; |
| 4257 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4258 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4259 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4260 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4261 | /* |
| 4262 | * Ok we want to try and use the cluster allocator, so lets look |
| 4263 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 4264 | * have tried the cluster allocator plenty of times at this |
| 4265 | * point and not have found anything, so we are likely way too |
| 4266 | * fragmented for the clustering stuff to find anything, so lets |
| 4267 | * just skip it and let the allocator find whatever block it can |
| 4268 | * find |
| 4269 | */ |
| 4270 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4271 | /* |
| 4272 | * the refill lock keeps out other |
| 4273 | * people trying to start a new cluster |
| 4274 | */ |
| 4275 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4276 | if (last_ptr->block_group && |
| 4277 | (last_ptr->block_group->ro || |
| 4278 | !block_group_bits(last_ptr->block_group, data))) { |
| 4279 | offset = 0; |
| 4280 | goto refill_cluster; |
| 4281 | } |
| 4282 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4283 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 4284 | num_bytes, search_start); |
| 4285 | if (offset) { |
| 4286 | /* we have a block, we're done */ |
| 4287 | spin_unlock(&last_ptr->refill_lock); |
| 4288 | goto checks; |
| 4289 | } |
| 4290 | |
| 4291 | spin_lock(&last_ptr->lock); |
| 4292 | /* |
| 4293 | * whoops, this cluster doesn't actually point to |
| 4294 | * this block group. Get a ref on the block |
| 4295 | * group is does point to and try again |
| 4296 | */ |
| 4297 | if (!last_ptr_loop && last_ptr->block_group && |
| 4298 | last_ptr->block_group != block_group) { |
| 4299 | |
| 4300 | btrfs_put_block_group(block_group); |
| 4301 | block_group = last_ptr->block_group; |
| 4302 | atomic_inc(&block_group->count); |
| 4303 | spin_unlock(&last_ptr->lock); |
| 4304 | spin_unlock(&last_ptr->refill_lock); |
| 4305 | |
| 4306 | last_ptr_loop = 1; |
| 4307 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4308 | /* |
| 4309 | * we know this block group is properly |
| 4310 | * in the list because |
| 4311 | * btrfs_remove_block_group, drops the |
| 4312 | * cluster before it removes the block |
| 4313 | * group from the list |
| 4314 | */ |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4315 | goto have_block_group; |
| 4316 | } |
| 4317 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4318 | refill_cluster: |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4319 | /* |
| 4320 | * this cluster didn't work out, free it and |
| 4321 | * start over |
| 4322 | */ |
| 4323 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 4324 | |
| 4325 | last_ptr_loop = 0; |
| 4326 | |
| 4327 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 4328 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4329 | block_group, last_ptr, |
| 4330 | offset, num_bytes, |
| 4331 | empty_cluster + empty_size); |
| 4332 | if (ret == 0) { |
| 4333 | /* |
| 4334 | * now pull our allocation out of this |
| 4335 | * cluster |
| 4336 | */ |
| 4337 | offset = btrfs_alloc_from_cluster(block_group, |
| 4338 | last_ptr, num_bytes, |
| 4339 | search_start); |
| 4340 | if (offset) { |
| 4341 | /* we found one, proceed */ |
| 4342 | spin_unlock(&last_ptr->refill_lock); |
| 4343 | goto checks; |
| 4344 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4345 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 4346 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4347 | spin_unlock(&last_ptr->refill_lock); |
| 4348 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4349 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4350 | wait_block_group_cache_progress(block_group, |
| 4351 | num_bytes + empty_cluster + empty_size); |
| 4352 | goto have_block_group; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4353 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4354 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4355 | /* |
| 4356 | * at this point we either didn't find a cluster |
| 4357 | * or we weren't able to allocate a block from our |
| 4358 | * cluster. Free the cluster we've been trying |
| 4359 | * to use, and go to the next block group |
| 4360 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4361 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4362 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4363 | goto loop; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4364 | } |
| 4365 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4366 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 4367 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4368 | /* |
| 4369 | * If we didn't find a chunk, and we haven't failed on this |
| 4370 | * block group before, and this block group is in the middle of |
| 4371 | * caching and we are ok with waiting, then go ahead and wait |
| 4372 | * for progress to be made, and set failed_alloc to true. |
| 4373 | * |
| 4374 | * If failed_alloc is true then we've already waited on this |
| 4375 | * block group once and should move on to the next block group. |
| 4376 | */ |
| 4377 | if (!offset && !failed_alloc && !cached && |
| 4378 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4379 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4380 | num_bytes + empty_size); |
| 4381 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4382 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4383 | } else if (!offset) { |
| 4384 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4385 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4386 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4387 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4388 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4389 | if (search_start + num_bytes >= search_end) { |
| 4390 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4391 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4392 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4393 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4394 | /* move on to the next group */ |
| 4395 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4396 | block_group->key.objectid + block_group->key.offset) { |
| 4397 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4398 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4399 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4400 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4401 | if (exclude_nr > 0 && |
| 4402 | (search_start + num_bytes > exclude_start && |
| 4403 | search_start < exclude_start + exclude_nr)) { |
| 4404 | search_start = exclude_start + exclude_nr; |
| 4405 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4406 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4407 | /* |
| 4408 | * if search_start is still in this block group |
| 4409 | * then we just re-search this block group |
| 4410 | */ |
| 4411 | if (search_start >= block_group->key.objectid && |
| 4412 | search_start < (block_group->key.objectid + |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4413 | block_group->key.offset)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4414 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4415 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4416 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4417 | |
| 4418 | ins->objectid = search_start; |
| 4419 | ins->offset = num_bytes; |
| 4420 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4421 | if (offset < search_start) |
| 4422 | btrfs_add_free_space(block_group, offset, |
| 4423 | search_start - offset); |
| 4424 | BUG_ON(offset > search_start); |
| 4425 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4426 | update_reserved_extents(block_group, num_bytes, 1); |
| 4427 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4428 | /* we are all good, lets return */ |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4429 | break; |
| 4430 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4431 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4432 | failed_alloc = false; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4433 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4434 | } |
| 4435 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4436 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4437 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 4438 | * for them to make caching progress. Also |
| 4439 | * determine the best possible bg to cache |
| 4440 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 4441 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4442 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 4443 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 4444 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 4445 | * again |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4446 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4447 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 4448 | (found_uncached_bg || empty_size || empty_cluster || |
| 4449 | allowed_chunk_alloc)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4450 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4451 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4452 | loop++; |
| 4453 | if (!ideal_cache_percent && |
| 4454 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4455 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4456 | |
| 4457 | /* |
| 4458 | * 1 of the following 2 things have happened so far |
| 4459 | * |
| 4460 | * 1) We found an ideal block group for caching that |
| 4461 | * is mostly full and will cache quickly, so we might |
| 4462 | * as well wait for it. |
| 4463 | * |
| 4464 | * 2) We searched for cached only and we didn't find |
| 4465 | * anything, and we didn't start any caching kthreads |
| 4466 | * either, so chances are we will loop through and |
| 4467 | * start a couple caching kthreads, and then come back |
| 4468 | * around and just wait for them. This will be slower |
| 4469 | * because we will have 2 caching kthreads reading at |
| 4470 | * the same time when we could have just started one |
| 4471 | * and waited for it to get far enough to give us an |
| 4472 | * allocation, so go ahead and go to the wait caching |
| 4473 | * loop. |
| 4474 | */ |
| 4475 | loop = LOOP_CACHING_WAIT; |
| 4476 | search_start = ideal_cache_offset; |
| 4477 | ideal_cache_percent = 0; |
| 4478 | goto ideal_cache; |
| 4479 | } else if (loop == LOOP_FIND_IDEAL) { |
| 4480 | /* |
| 4481 | * Didn't find a uncached bg, wait on anything we find |
| 4482 | * next. |
| 4483 | */ |
| 4484 | loop = LOOP_CACHING_WAIT; |
| 4485 | goto search; |
| 4486 | } |
| 4487 | |
| 4488 | if (loop < LOOP_CACHING_WAIT) { |
| 4489 | loop++; |
| 4490 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4494 | empty_size = 0; |
| 4495 | empty_cluster = 0; |
| 4496 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4497 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4498 | if (allowed_chunk_alloc) { |
| 4499 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 4500 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4501 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4502 | done_chunk_alloc = 1; |
| 4503 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4504 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4505 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4506 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4507 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4508 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4509 | goto search; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4510 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4511 | ret = -ENOSPC; |
| 4512 | } else if (!ins->objectid) { |
| 4513 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 4514 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4515 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4516 | /* we found what we needed */ |
| 4517 | if (ins->objectid) { |
| 4518 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4519 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4520 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4521 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4522 | ret = 0; |
| 4523 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4524 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4525 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4526 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4527 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4528 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 4529 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4530 | { |
| 4531 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4532 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4533 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4534 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 4535 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4536 | info->bytes_pinned - info->bytes_reserved - |
| 4537 | info->bytes_super), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4538 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4539 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4540 | " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu" |
| 4541 | "\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 4542 | (unsigned long long)info->total_bytes, |
| 4543 | (unsigned long long)info->bytes_pinned, |
| 4544 | (unsigned long long)info->bytes_delalloc, |
| 4545 | (unsigned long long)info->bytes_may_use, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4546 | (unsigned long long)info->bytes_used, |
| 4547 | (unsigned long long)info->bytes_root, |
| 4548 | (unsigned long long)info->bytes_super, |
| 4549 | (unsigned long long)info->bytes_reserved); |
| 4550 | spin_unlock(&info->lock); |
| 4551 | |
| 4552 | if (!dump_block_groups) |
| 4553 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4554 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4555 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 4556 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4557 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4558 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 4559 | "%llu pinned %llu reserved\n", |
| 4560 | (unsigned long long)cache->key.objectid, |
| 4561 | (unsigned long long)cache->key.offset, |
| 4562 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 4563 | (unsigned long long)cache->pinned, |
| 4564 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4565 | btrfs_dump_free_space(cache, bytes); |
| 4566 | spin_unlock(&cache->lock); |
| 4567 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4568 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4569 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4570 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4571 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 4572 | struct btrfs_root *root, |
| 4573 | u64 num_bytes, u64 min_alloc_size, |
| 4574 | u64 empty_size, u64 hint_byte, |
| 4575 | u64 search_end, struct btrfs_key *ins, |
| 4576 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4577 | { |
| 4578 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 4579 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4580 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4581 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4582 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4583 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4584 | /* |
| 4585 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 4586 | * is not called recursively on allocations |
| 4587 | */ |
| 4588 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4589 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4590 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4591 | 2 * 1024 * 1024, |
| 4592 | BTRFS_BLOCK_GROUP_METADATA | |
| 4593 | (info->metadata_alloc_profile & |
| 4594 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4595 | } |
| 4596 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4597 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4598 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4599 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4600 | WARN_ON(num_bytes < root->sectorsize); |
| 4601 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 4602 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 4603 | trans->alloc_exclude_start, |
| 4604 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4605 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4606 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 4607 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4608 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4609 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4610 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 4611 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4612 | goto again; |
| 4613 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4614 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4615 | struct btrfs_space_info *sinfo; |
| 4616 | |
| 4617 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4618 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 4619 | "wanted %llu\n", (unsigned long long)data, |
| 4620 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4621 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4622 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4623 | |
| 4624 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4625 | } |
| 4626 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4627 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 4628 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4629 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4630 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4631 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4632 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 4633 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4634 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 4635 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4636 | return -ENOSPC; |
| 4637 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4638 | |
| 4639 | ret = btrfs_discard_extent(root, start, len); |
| 4640 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4641 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4642 | update_reserved_extents(cache, len, 0); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4643 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4644 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4645 | return ret; |
| 4646 | } |
| 4647 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4648 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4649 | struct btrfs_root *root, |
| 4650 | u64 parent, u64 root_objectid, |
| 4651 | u64 flags, u64 owner, u64 offset, |
| 4652 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4653 | { |
| 4654 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4655 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4656 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4657 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4658 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4659 | struct extent_buffer *leaf; |
| 4660 | int type; |
| 4661 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4662 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4663 | if (parent > 0) |
| 4664 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4665 | else |
| 4666 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4667 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4668 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4669 | |
| 4670 | path = btrfs_alloc_path(); |
| 4671 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4672 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4673 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4674 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4675 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4676 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4677 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4678 | leaf = path->nodes[0]; |
| 4679 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4680 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4681 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4682 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4683 | btrfs_set_extent_flags(leaf, extent_item, |
| 4684 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4685 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4686 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4687 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4688 | if (parent > 0) { |
| 4689 | struct btrfs_shared_data_ref *ref; |
| 4690 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4691 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4692 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4693 | } else { |
| 4694 | struct btrfs_extent_data_ref *ref; |
| 4695 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4696 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4697 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4698 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4699 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4700 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4701 | |
| 4702 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4703 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4704 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4705 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4706 | 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4707 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4708 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4709 | "%llu\n", (unsigned long long)ins->objectid, |
| 4710 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4711 | BUG(); |
| 4712 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4713 | return ret; |
| 4714 | } |
| 4715 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4716 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 4717 | struct btrfs_root *root, |
| 4718 | u64 parent, u64 root_objectid, |
| 4719 | u64 flags, struct btrfs_disk_key *key, |
| 4720 | int level, struct btrfs_key *ins) |
| 4721 | { |
| 4722 | int ret; |
| 4723 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4724 | struct btrfs_extent_item *extent_item; |
| 4725 | struct btrfs_tree_block_info *block_info; |
| 4726 | struct btrfs_extent_inline_ref *iref; |
| 4727 | struct btrfs_path *path; |
| 4728 | struct extent_buffer *leaf; |
| 4729 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 4730 | |
| 4731 | path = btrfs_alloc_path(); |
| 4732 | BUG_ON(!path); |
| 4733 | |
| 4734 | path->leave_spinning = 1; |
| 4735 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4736 | ins, size); |
| 4737 | BUG_ON(ret); |
| 4738 | |
| 4739 | leaf = path->nodes[0]; |
| 4740 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4741 | struct btrfs_extent_item); |
| 4742 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4743 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4744 | btrfs_set_extent_flags(leaf, extent_item, |
| 4745 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 4746 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 4747 | |
| 4748 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 4749 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 4750 | |
| 4751 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4752 | if (parent > 0) { |
| 4753 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4754 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4755 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 4756 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4757 | } else { |
| 4758 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4759 | BTRFS_TREE_BLOCK_REF_KEY); |
| 4760 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 4761 | } |
| 4762 | |
| 4763 | btrfs_mark_buffer_dirty(leaf); |
| 4764 | btrfs_free_path(path); |
| 4765 | |
| 4766 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4767 | 1, 0); |
| 4768 | if (ret) { |
| 4769 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4770 | "%llu\n", (unsigned long long)ins->objectid, |
| 4771 | (unsigned long long)ins->offset); |
| 4772 | BUG(); |
| 4773 | } |
| 4774 | return ret; |
| 4775 | } |
| 4776 | |
| 4777 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4778 | struct btrfs_root *root, |
| 4779 | u64 root_objectid, u64 owner, |
| 4780 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4781 | { |
| 4782 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4783 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4784 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4785 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4786 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 4787 | 0, root_objectid, owner, offset, |
| 4788 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4789 | return ret; |
| 4790 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4791 | |
| 4792 | /* |
| 4793 | * this is used by the tree logging recovery code. It records that |
| 4794 | * an extent has been allocated and makes sure to clear the free |
| 4795 | * space cache bits as well |
| 4796 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4797 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 4798 | struct btrfs_root *root, |
| 4799 | u64 root_objectid, u64 owner, u64 offset, |
| 4800 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4801 | { |
| 4802 | int ret; |
| 4803 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4804 | struct btrfs_caching_control *caching_ctl; |
| 4805 | u64 start = ins->objectid; |
| 4806 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4807 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4808 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4809 | cache_block_group(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4810 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4811 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4812 | if (!caching_ctl) { |
| 4813 | BUG_ON(!block_group_cache_done(block_group)); |
| 4814 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 4815 | BUG_ON(ret); |
| 4816 | } else { |
| 4817 | mutex_lock(&caching_ctl->mutex); |
| 4818 | |
| 4819 | if (start >= caching_ctl->progress) { |
| 4820 | ret = add_excluded_extent(root, start, num_bytes); |
| 4821 | BUG_ON(ret); |
| 4822 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 4823 | ret = btrfs_remove_free_space(block_group, |
| 4824 | start, num_bytes); |
| 4825 | BUG_ON(ret); |
| 4826 | } else { |
| 4827 | num_bytes = caching_ctl->progress - start; |
| 4828 | ret = btrfs_remove_free_space(block_group, |
| 4829 | start, num_bytes); |
| 4830 | BUG_ON(ret); |
| 4831 | |
| 4832 | start = caching_ctl->progress; |
| 4833 | num_bytes = ins->objectid + ins->offset - |
| 4834 | caching_ctl->progress; |
| 4835 | ret = add_excluded_extent(root, start, num_bytes); |
| 4836 | BUG_ON(ret); |
| 4837 | } |
| 4838 | |
| 4839 | mutex_unlock(&caching_ctl->mutex); |
| 4840 | put_caching_control(caching_ctl); |
| 4841 | } |
| 4842 | |
| 4843 | update_reserved_extents(block_group, ins->offset, 1); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4844 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4845 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 4846 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4847 | return ret; |
| 4848 | } |
| 4849 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4850 | /* |
| 4851 | * finds a free extent and does all the dirty work required for allocation |
| 4852 | * returns the key for the extent through ins, and a tree buffer for |
| 4853 | * the first block of the extent through buf. |
| 4854 | * |
| 4855 | * returns 0 if everything worked, non-zero otherwise. |
| 4856 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4857 | static int alloc_tree_block(struct btrfs_trans_handle *trans, |
| 4858 | struct btrfs_root *root, |
| 4859 | u64 num_bytes, u64 parent, u64 root_objectid, |
| 4860 | struct btrfs_disk_key *key, int level, |
| 4861 | u64 empty_size, u64 hint_byte, u64 search_end, |
| 4862 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4863 | { |
| 4864 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4865 | u64 flags = 0; |
| 4866 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4867 | ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes, |
| 4868 | empty_size, hint_byte, search_end, |
| 4869 | ins, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4870 | if (ret) |
| 4871 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4872 | |
| 4873 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4874 | if (parent == 0) |
| 4875 | parent = ins->objectid; |
| 4876 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4877 | } else |
| 4878 | BUG_ON(parent > 0); |
| 4879 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4880 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4881 | struct btrfs_delayed_extent_op *extent_op; |
| 4882 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 4883 | BUG_ON(!extent_op); |
| 4884 | if (key) |
| 4885 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4886 | else |
| 4887 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4888 | extent_op->flags_to_set = flags; |
| 4889 | extent_op->update_key = 1; |
| 4890 | extent_op->update_flags = 1; |
| 4891 | extent_op->is_data = 0; |
| 4892 | |
| 4893 | ret = btrfs_add_delayed_tree_ref(trans, ins->objectid, |
| 4894 | ins->offset, parent, root_objectid, |
| 4895 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 4896 | extent_op); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4897 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4898 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4899 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4900 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4901 | |
| 4902 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 4903 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4904 | u64 bytenr, u32 blocksize, |
| 4905 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4906 | { |
| 4907 | struct extent_buffer *buf; |
| 4908 | |
| 4909 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 4910 | if (!buf) |
| 4911 | return ERR_PTR(-ENOMEM); |
| 4912 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4913 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4914 | btrfs_tree_lock(buf); |
| 4915 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4916 | |
| 4917 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4918 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4919 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4920 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4921 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4922 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4923 | } else { |
| 4924 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4925 | buf->start + buf->len - 1, GFP_NOFS); |
| 4926 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4927 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4928 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4929 | return buf; |
| 4930 | } |
| 4931 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4932 | /* |
| 4933 | * helper function to allocate a block for a given tree |
| 4934 | * returns the tree buffer or NULL. |
| 4935 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4936 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4937 | struct btrfs_root *root, u32 blocksize, |
| 4938 | u64 parent, u64 root_objectid, |
| 4939 | struct btrfs_disk_key *key, int level, |
| 4940 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4941 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4942 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4943 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4944 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4945 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4946 | ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid, |
| 4947 | key, level, empty_size, hint, (u64)-1, &ins); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4948 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4949 | BUG_ON(ret > 0); |
| 4950 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4951 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4952 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4953 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 4954 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4955 | return buf; |
| 4956 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4957 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4958 | struct walk_control { |
| 4959 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4960 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4961 | struct btrfs_key update_progress; |
| 4962 | int stage; |
| 4963 | int level; |
| 4964 | int shared_level; |
| 4965 | int update_ref; |
| 4966 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4967 | int reada_slot; |
| 4968 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4969 | }; |
| 4970 | |
| 4971 | #define DROP_REFERENCE 1 |
| 4972 | #define UPDATE_BACKREF 2 |
| 4973 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4974 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4975 | struct btrfs_root *root, |
| 4976 | struct walk_control *wc, |
| 4977 | struct btrfs_path *path) |
| 4978 | { |
| 4979 | u64 bytenr; |
| 4980 | u64 generation; |
| 4981 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4982 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4983 | u64 last = 0; |
| 4984 | u32 nritems; |
| 4985 | u32 blocksize; |
| 4986 | struct btrfs_key key; |
| 4987 | struct extent_buffer *eb; |
| 4988 | int ret; |
| 4989 | int slot; |
| 4990 | int nread = 0; |
| 4991 | |
| 4992 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4993 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4994 | wc->reada_count = max(wc->reada_count, 2); |
| 4995 | } else { |
| 4996 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4997 | wc->reada_count = min_t(int, wc->reada_count, |
| 4998 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 4999 | } |
| 5000 | |
| 5001 | eb = path->nodes[wc->level]; |
| 5002 | nritems = btrfs_header_nritems(eb); |
| 5003 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5004 | |
| 5005 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5006 | if (nread >= wc->reada_count) |
| 5007 | break; |
| 5008 | |
| 5009 | cond_resched(); |
| 5010 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5011 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5012 | |
| 5013 | if (slot == path->slots[wc->level]) |
| 5014 | goto reada; |
| 5015 | |
| 5016 | if (wc->stage == UPDATE_BACKREF && |
| 5017 | generation <= root->root_key.offset) |
| 5018 | continue; |
| 5019 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5020 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5021 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5022 | &refs, &flags); |
| 5023 | BUG_ON(ret); |
| 5024 | BUG_ON(refs == 0); |
| 5025 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5026 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5027 | if (refs == 1) |
| 5028 | goto reada; |
| 5029 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5030 | if (wc->level == 1 && |
| 5031 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5032 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5033 | if (!wc->update_ref || |
| 5034 | generation <= root->root_key.offset) |
| 5035 | continue; |
| 5036 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5037 | ret = btrfs_comp_cpu_keys(&key, |
| 5038 | &wc->update_progress); |
| 5039 | if (ret < 0) |
| 5040 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5041 | } else { |
| 5042 | if (wc->level == 1 && |
| 5043 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5044 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5045 | } |
| 5046 | reada: |
| 5047 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5048 | generation); |
| 5049 | if (ret) |
| 5050 | break; |
| 5051 | last = bytenr + blocksize; |
| 5052 | nread++; |
| 5053 | } |
| 5054 | wc->reada_slot = slot; |
| 5055 | } |
| 5056 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5057 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5058 | * hepler to process tree block while walking down the tree. |
| 5059 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5060 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5061 | * back refs for pointers in the block. |
| 5062 | * |
| 5063 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5064 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5065 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5066 | struct btrfs_root *root, |
| 5067 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5068 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5069 | { |
| 5070 | int level = wc->level; |
| 5071 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5072 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5073 | int ret; |
| 5074 | |
| 5075 | if (wc->stage == UPDATE_BACKREF && |
| 5076 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5077 | return 1; |
| 5078 | |
| 5079 | /* |
| 5080 | * when reference count of tree block is 1, it won't increase |
| 5081 | * again. once full backref flag is set, we never clear it. |
| 5082 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5083 | if (lookup_info && |
| 5084 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5085 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5086 | BUG_ON(!path->locks[level]); |
| 5087 | ret = btrfs_lookup_extent_info(trans, root, |
| 5088 | eb->start, eb->len, |
| 5089 | &wc->refs[level], |
| 5090 | &wc->flags[level]); |
| 5091 | BUG_ON(ret); |
| 5092 | BUG_ON(wc->refs[level] == 0); |
| 5093 | } |
| 5094 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5095 | if (wc->stage == DROP_REFERENCE) { |
| 5096 | if (wc->refs[level] > 1) |
| 5097 | return 1; |
| 5098 | |
| 5099 | if (path->locks[level] && !wc->keep_locks) { |
| 5100 | btrfs_tree_unlock(eb); |
| 5101 | path->locks[level] = 0; |
| 5102 | } |
| 5103 | return 0; |
| 5104 | } |
| 5105 | |
| 5106 | /* wc->stage == UPDATE_BACKREF */ |
| 5107 | if (!(wc->flags[level] & flag)) { |
| 5108 | BUG_ON(!path->locks[level]); |
| 5109 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5110 | BUG_ON(ret); |
| 5111 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5112 | BUG_ON(ret); |
| 5113 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5114 | eb->len, flag, 0); |
| 5115 | BUG_ON(ret); |
| 5116 | wc->flags[level] |= flag; |
| 5117 | } |
| 5118 | |
| 5119 | /* |
| 5120 | * the block is shared by multiple trees, so it's not good to |
| 5121 | * keep the tree lock |
| 5122 | */ |
| 5123 | if (path->locks[level] && level > 0) { |
| 5124 | btrfs_tree_unlock(eb); |
| 5125 | path->locks[level] = 0; |
| 5126 | } |
| 5127 | return 0; |
| 5128 | } |
| 5129 | |
| 5130 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5131 | * hepler to process tree block pointer. |
| 5132 | * |
| 5133 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5134 | * reference count of the block pointed to. if the block |
| 5135 | * is shared and we need update back refs for the subtree |
| 5136 | * rooted at the block, this function changes wc->stage to |
| 5137 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5138 | * need to update back, this function drops the reference |
| 5139 | * to the block. |
| 5140 | * |
| 5141 | * NOTE: return value 1 means we should stop walking down. |
| 5142 | */ |
| 5143 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5144 | struct btrfs_root *root, |
| 5145 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5146 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5147 | { |
| 5148 | u64 bytenr; |
| 5149 | u64 generation; |
| 5150 | u64 parent; |
| 5151 | u32 blocksize; |
| 5152 | struct btrfs_key key; |
| 5153 | struct extent_buffer *next; |
| 5154 | int level = wc->level; |
| 5155 | int reada = 0; |
| 5156 | int ret = 0; |
| 5157 | |
| 5158 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5159 | path->slots[level]); |
| 5160 | /* |
| 5161 | * if the lower level block was created before the snapshot |
| 5162 | * was created, we know there is no need to update back refs |
| 5163 | * for the subtree |
| 5164 | */ |
| 5165 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5166 | generation <= root->root_key.offset) { |
| 5167 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5168 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5169 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5170 | |
| 5171 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5172 | blocksize = btrfs_level_size(root, level - 1); |
| 5173 | |
| 5174 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5175 | if (!next) { |
| 5176 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5177 | reada = 1; |
| 5178 | } |
| 5179 | btrfs_tree_lock(next); |
| 5180 | btrfs_set_lock_blocking(next); |
| 5181 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5182 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5183 | &wc->refs[level - 1], |
| 5184 | &wc->flags[level - 1]); |
| 5185 | BUG_ON(ret); |
| 5186 | BUG_ON(wc->refs[level - 1] == 0); |
| 5187 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5188 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5189 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5190 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5191 | if (level == 1 && |
| 5192 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5193 | goto skip; |
| 5194 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5195 | if (!wc->update_ref || |
| 5196 | generation <= root->root_key.offset) |
| 5197 | goto skip; |
| 5198 | |
| 5199 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5200 | path->slots[level]); |
| 5201 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5202 | if (ret < 0) |
| 5203 | goto skip; |
| 5204 | |
| 5205 | wc->stage = UPDATE_BACKREF; |
| 5206 | wc->shared_level = level - 1; |
| 5207 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5208 | } else { |
| 5209 | if (level == 1 && |
| 5210 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5211 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5215 | btrfs_tree_unlock(next); |
| 5216 | free_extent_buffer(next); |
| 5217 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5218 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | if (!next) { |
| 5222 | if (reada && level == 1) |
| 5223 | reada_walk_down(trans, root, wc, path); |
| 5224 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5225 | btrfs_tree_lock(next); |
| 5226 | btrfs_set_lock_blocking(next); |
| 5227 | } |
| 5228 | |
| 5229 | level--; |
| 5230 | BUG_ON(level != btrfs_header_level(next)); |
| 5231 | path->nodes[level] = next; |
| 5232 | path->slots[level] = 0; |
| 5233 | path->locks[level] = 1; |
| 5234 | wc->level = level; |
| 5235 | if (wc->level == 1) |
| 5236 | wc->reada_slot = 0; |
| 5237 | return 0; |
| 5238 | skip: |
| 5239 | wc->refs[level - 1] = 0; |
| 5240 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5241 | if (wc->stage == DROP_REFERENCE) { |
| 5242 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5243 | parent = path->nodes[level]->start; |
| 5244 | } else { |
| 5245 | BUG_ON(root->root_key.objectid != |
| 5246 | btrfs_header_owner(path->nodes[level])); |
| 5247 | parent = 0; |
| 5248 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5249 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5250 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 5251 | root->root_key.objectid, level - 1, 0); |
| 5252 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5253 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5254 | btrfs_tree_unlock(next); |
| 5255 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5256 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5257 | return 1; |
| 5258 | } |
| 5259 | |
| 5260 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5261 | * hepler to process tree block while walking up the tree. |
| 5262 | * |
| 5263 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5264 | * reference count on the block. |
| 5265 | * |
| 5266 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5267 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5268 | * to UPDATE_BACKREF previously while processing the block. |
| 5269 | * |
| 5270 | * NOTE: return value 1 means we should stop walking up. |
| 5271 | */ |
| 5272 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5273 | struct btrfs_root *root, |
| 5274 | struct btrfs_path *path, |
| 5275 | struct walk_control *wc) |
| 5276 | { |
| 5277 | int ret = 0; |
| 5278 | int level = wc->level; |
| 5279 | struct extent_buffer *eb = path->nodes[level]; |
| 5280 | u64 parent = 0; |
| 5281 | |
| 5282 | if (wc->stage == UPDATE_BACKREF) { |
| 5283 | BUG_ON(wc->shared_level < level); |
| 5284 | if (level < wc->shared_level) |
| 5285 | goto out; |
| 5286 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5287 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5288 | if (ret > 0) |
| 5289 | wc->update_ref = 0; |
| 5290 | |
| 5291 | wc->stage = DROP_REFERENCE; |
| 5292 | wc->shared_level = -1; |
| 5293 | path->slots[level] = 0; |
| 5294 | |
| 5295 | /* |
| 5296 | * check reference count again if the block isn't locked. |
| 5297 | * we should start walking down the tree again if reference |
| 5298 | * count is one. |
| 5299 | */ |
| 5300 | if (!path->locks[level]) { |
| 5301 | BUG_ON(level == 0); |
| 5302 | btrfs_tree_lock(eb); |
| 5303 | btrfs_set_lock_blocking(eb); |
| 5304 | path->locks[level] = 1; |
| 5305 | |
| 5306 | ret = btrfs_lookup_extent_info(trans, root, |
| 5307 | eb->start, eb->len, |
| 5308 | &wc->refs[level], |
| 5309 | &wc->flags[level]); |
| 5310 | BUG_ON(ret); |
| 5311 | BUG_ON(wc->refs[level] == 0); |
| 5312 | if (wc->refs[level] == 1) { |
| 5313 | btrfs_tree_unlock(eb); |
| 5314 | path->locks[level] = 0; |
| 5315 | return 1; |
| 5316 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5317 | } |
| 5318 | } |
| 5319 | |
| 5320 | /* wc->stage == DROP_REFERENCE */ |
| 5321 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5322 | |
| 5323 | if (wc->refs[level] == 1) { |
| 5324 | if (level == 0) { |
| 5325 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5326 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 5327 | else |
| 5328 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5329 | BUG_ON(ret); |
| 5330 | } |
| 5331 | /* make block locked assertion in clean_tree_block happy */ |
| 5332 | if (!path->locks[level] && |
| 5333 | btrfs_header_generation(eb) == trans->transid) { |
| 5334 | btrfs_tree_lock(eb); |
| 5335 | btrfs_set_lock_blocking(eb); |
| 5336 | path->locks[level] = 1; |
| 5337 | } |
| 5338 | clean_tree_block(trans, root, eb); |
| 5339 | } |
| 5340 | |
| 5341 | if (eb == root->node) { |
| 5342 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5343 | parent = eb->start; |
| 5344 | else |
| 5345 | BUG_ON(root->root_key.objectid != |
| 5346 | btrfs_header_owner(eb)); |
| 5347 | } else { |
| 5348 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5349 | parent = path->nodes[level + 1]->start; |
| 5350 | else |
| 5351 | BUG_ON(root->root_key.objectid != |
| 5352 | btrfs_header_owner(path->nodes[level + 1])); |
| 5353 | } |
| 5354 | |
| 5355 | ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent, |
| 5356 | root->root_key.objectid, level, 0); |
| 5357 | BUG_ON(ret); |
| 5358 | out: |
| 5359 | wc->refs[level] = 0; |
| 5360 | wc->flags[level] = 0; |
| 5361 | return ret; |
| 5362 | } |
| 5363 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5364 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5365 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5366 | struct btrfs_path *path, |
| 5367 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5368 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5369 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5370 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5371 | int ret; |
| 5372 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5373 | while (level >= 0) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5374 | if (path->slots[level] >= |
| 5375 | btrfs_header_nritems(path->nodes[level])) |
| 5376 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5377 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5378 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5379 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5380 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5381 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5382 | if (level == 0) |
| 5383 | break; |
| 5384 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5385 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5386 | if (ret > 0) { |
| 5387 | path->slots[level]++; |
| 5388 | continue; |
| 5389 | } |
| 5390 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5391 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5392 | return 0; |
| 5393 | } |
| 5394 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5395 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5396 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5397 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5398 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5399 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5400 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5401 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5402 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5403 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5404 | while (level < max_level && path->nodes[level]) { |
| 5405 | wc->level = level; |
| 5406 | if (path->slots[level] + 1 < |
| 5407 | btrfs_header_nritems(path->nodes[level])) { |
| 5408 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5409 | return 0; |
| 5410 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5411 | ret = walk_up_proc(trans, root, path, wc); |
| 5412 | if (ret > 0) |
| 5413 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5414 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5415 | if (path->locks[level]) { |
| 5416 | btrfs_tree_unlock(path->nodes[level]); |
| 5417 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5418 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5419 | free_extent_buffer(path->nodes[level]); |
| 5420 | path->nodes[level] = NULL; |
| 5421 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5422 | } |
| 5423 | } |
| 5424 | return 1; |
| 5425 | } |
| 5426 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5427 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5428 | * drop a subvolume tree. |
| 5429 | * |
| 5430 | * this function traverses the tree freeing any blocks that only |
| 5431 | * referenced by the tree. |
| 5432 | * |
| 5433 | * when a shared tree block is found. this function decreases its |
| 5434 | * reference count by one. if update_ref is true, this function |
| 5435 | * also make sure backrefs for the shared block and all lower level |
| 5436 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5437 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5438 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5439 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5440 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5441 | struct btrfs_trans_handle *trans; |
| 5442 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5443 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5444 | struct walk_control *wc; |
| 5445 | struct btrfs_key key; |
| 5446 | int err = 0; |
| 5447 | int ret; |
| 5448 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5449 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5450 | path = btrfs_alloc_path(); |
| 5451 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5452 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5453 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5454 | BUG_ON(!wc); |
| 5455 | |
| 5456 | trans = btrfs_start_transaction(tree_root, 1); |
| 5457 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5458 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5459 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5460 | path->nodes[level] = btrfs_lock_root_node(root); |
| 5461 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5462 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5463 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5464 | memset(&wc->update_progress, 0, |
| 5465 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5466 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5467 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5468 | memcpy(&wc->update_progress, &key, |
| 5469 | sizeof(wc->update_progress)); |
| 5470 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5471 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5472 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5473 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5474 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5475 | path->lowest_level = 0; |
| 5476 | if (ret < 0) { |
| 5477 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5478 | goto out; |
| 5479 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5480 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5481 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5482 | /* |
| 5483 | * unlock our path, this is safe because only this |
| 5484 | * function is allowed to delete this snapshot |
| 5485 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5486 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5487 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5488 | level = btrfs_header_level(root->node); |
| 5489 | while (1) { |
| 5490 | btrfs_tree_lock(path->nodes[level]); |
| 5491 | btrfs_set_lock_blocking(path->nodes[level]); |
| 5492 | |
| 5493 | ret = btrfs_lookup_extent_info(trans, root, |
| 5494 | path->nodes[level]->start, |
| 5495 | path->nodes[level]->len, |
| 5496 | &wc->refs[level], |
| 5497 | &wc->flags[level]); |
| 5498 | BUG_ON(ret); |
| 5499 | BUG_ON(wc->refs[level] == 0); |
| 5500 | |
| 5501 | if (level == root_item->drop_level) |
| 5502 | break; |
| 5503 | |
| 5504 | btrfs_tree_unlock(path->nodes[level]); |
| 5505 | WARN_ON(wc->refs[level] != 1); |
| 5506 | level--; |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | wc->level = level; |
| 5511 | wc->shared_level = -1; |
| 5512 | wc->stage = DROP_REFERENCE; |
| 5513 | wc->update_ref = update_ref; |
| 5514 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5515 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5516 | |
| 5517 | while (1) { |
| 5518 | ret = walk_down_tree(trans, root, path, wc); |
| 5519 | if (ret < 0) { |
| 5520 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5521 | break; |
| 5522 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5523 | |
| 5524 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5525 | if (ret < 0) { |
| 5526 | err = ret; |
| 5527 | break; |
| 5528 | } |
| 5529 | |
| 5530 | if (ret > 0) { |
| 5531 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5532 | break; |
| 5533 | } |
| 5534 | |
| 5535 | if (wc->stage == DROP_REFERENCE) { |
| 5536 | level = wc->level; |
| 5537 | btrfs_node_key(path->nodes[level], |
| 5538 | &root_item->drop_progress, |
| 5539 | path->slots[level]); |
| 5540 | root_item->drop_level = level; |
| 5541 | } |
| 5542 | |
| 5543 | BUG_ON(wc->level == 0); |
| 5544 | if (trans->transaction->in_commit || |
| 5545 | trans->transaction->delayed_refs.flushing) { |
| 5546 | ret = btrfs_update_root(trans, tree_root, |
| 5547 | &root->root_key, |
| 5548 | root_item); |
| 5549 | BUG_ON(ret); |
| 5550 | |
| 5551 | btrfs_end_transaction(trans, tree_root); |
| 5552 | trans = btrfs_start_transaction(tree_root, 1); |
| 5553 | } else { |
| 5554 | unsigned long update; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5555 | update = trans->delayed_ref_updates; |
| 5556 | trans->delayed_ref_updates = 0; |
| 5557 | if (update) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5558 | btrfs_run_delayed_refs(trans, tree_root, |
| 5559 | update); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5560 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5561 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5562 | btrfs_release_path(root, path); |
| 5563 | BUG_ON(err); |
| 5564 | |
| 5565 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 5566 | BUG_ON(ret); |
| 5567 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5568 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 5569 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 5570 | NULL, NULL); |
| 5571 | BUG_ON(ret < 0); |
| 5572 | if (ret > 0) { |
| 5573 | ret = btrfs_del_orphan_item(trans, tree_root, |
| 5574 | root->root_key.objectid); |
| 5575 | BUG_ON(ret); |
| 5576 | } |
| 5577 | } |
| 5578 | |
| 5579 | if (root->in_radix) { |
| 5580 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 5581 | } else { |
| 5582 | free_extent_buffer(root->node); |
| 5583 | free_extent_buffer(root->commit_root); |
| 5584 | kfree(root); |
| 5585 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5586 | out: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5587 | btrfs_end_transaction(trans, tree_root); |
| 5588 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5589 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5590 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5591 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5592 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5593 | /* |
| 5594 | * drop subtree rooted at tree block 'node'. |
| 5595 | * |
| 5596 | * NOTE: this function will unlock and release tree block 'node' |
| 5597 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5598 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5599 | struct btrfs_root *root, |
| 5600 | struct extent_buffer *node, |
| 5601 | struct extent_buffer *parent) |
| 5602 | { |
| 5603 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5604 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5605 | int level; |
| 5606 | int parent_level; |
| 5607 | int ret = 0; |
| 5608 | int wret; |
| 5609 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5610 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5611 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5612 | path = btrfs_alloc_path(); |
| 5613 | BUG_ON(!path); |
| 5614 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5615 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5616 | BUG_ON(!wc); |
| 5617 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5618 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5619 | parent_level = btrfs_header_level(parent); |
| 5620 | extent_buffer_get(parent); |
| 5621 | path->nodes[parent_level] = parent; |
| 5622 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5623 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5624 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5625 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5626 | path->nodes[level] = node; |
| 5627 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5628 | path->locks[level] = 1; |
| 5629 | |
| 5630 | wc->refs[parent_level] = 1; |
| 5631 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5632 | wc->level = level; |
| 5633 | wc->shared_level = -1; |
| 5634 | wc->stage = DROP_REFERENCE; |
| 5635 | wc->update_ref = 0; |
| 5636 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5637 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5638 | |
| 5639 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5640 | wret = walk_down_tree(trans, root, path, wc); |
| 5641 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5642 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5643 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5644 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5645 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5646 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5647 | if (wret < 0) |
| 5648 | ret = wret; |
| 5649 | if (wret != 0) |
| 5650 | break; |
| 5651 | } |
| 5652 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5653 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5654 | btrfs_free_path(path); |
| 5655 | return ret; |
| 5656 | } |
| 5657 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5658 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5659 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 5660 | unsigned long nr) |
| 5661 | { |
| 5662 | return min(last, start + nr - 1); |
| 5663 | } |
| 5664 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5665 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5666 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5667 | { |
| 5668 | u64 page_start; |
| 5669 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5670 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5671 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5672 | unsigned long i; |
| 5673 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5674 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5675 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5676 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5677 | unsigned int total_read = 0; |
| 5678 | unsigned int total_dirty = 0; |
| 5679 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5680 | |
| 5681 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5682 | |
| 5683 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5684 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5685 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 5686 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5687 | /* make sure the dirty trick played by the caller work */ |
| 5688 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5689 | first_index, last_index); |
| 5690 | if (ret) |
| 5691 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5692 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5693 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5694 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5695 | for (i = first_index ; i <= last_index; i++) { |
| 5696 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5697 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5698 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5699 | } |
| 5700 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5701 | again: |
| 5702 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5703 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5704 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5705 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5706 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5707 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5708 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5709 | if (!PageUptodate(page)) { |
| 5710 | btrfs_readpage(NULL, page); |
| 5711 | lock_page(page); |
| 5712 | if (!PageUptodate(page)) { |
| 5713 | unlock_page(page); |
| 5714 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5715 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5716 | goto out_unlock; |
| 5717 | } |
| 5718 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5719 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5720 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5721 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 5722 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5723 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5724 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5725 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5726 | if (ordered) { |
| 5727 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5728 | unlock_page(page); |
| 5729 | page_cache_release(page); |
| 5730 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5731 | btrfs_put_ordered_extent(ordered); |
| 5732 | goto again; |
| 5733 | } |
| 5734 | set_page_extent_mapped(page); |
| 5735 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5736 | if (i == first_index) |
| 5737 | set_extent_bits(io_tree, page_start, page_end, |
| 5738 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 5739 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5740 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5741 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5742 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5743 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5744 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5745 | unlock_page(page); |
| 5746 | page_cache_release(page); |
| 5747 | } |
| 5748 | |
| 5749 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5750 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5751 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5752 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5753 | return ret; |
| 5754 | } |
| 5755 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5756 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5757 | struct btrfs_key *extent_key, |
| 5758 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5759 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5760 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5761 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 5762 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5763 | u64 start = extent_key->objectid - offset; |
| 5764 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5765 | |
| 5766 | em = alloc_extent_map(GFP_NOFS); |
| 5767 | BUG_ON(!em || IS_ERR(em)); |
| 5768 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5769 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5770 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5771 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5772 | em->block_start = extent_key->objectid; |
| 5773 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5774 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5775 | |
| 5776 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5777 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5778 | while (1) { |
| 5779 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5780 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5781 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5782 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5783 | if (ret != -EEXIST) { |
| 5784 | free_extent_map(em); |
| 5785 | break; |
| 5786 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5787 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5788 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5789 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5790 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5791 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | struct btrfs_ref_path { |
| 5795 | u64 extent_start; |
| 5796 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 5797 | u64 root_objectid; |
| 5798 | u64 root_generation; |
| 5799 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5800 | u32 num_refs; |
| 5801 | int lowest_level; |
| 5802 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5803 | int shared_level; |
| 5804 | |
| 5805 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 5806 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5807 | }; |
| 5808 | |
| 5809 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5810 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5811 | u64 disk_bytenr; |
| 5812 | u64 disk_num_bytes; |
| 5813 | u64 offset; |
| 5814 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5815 | u8 compression; |
| 5816 | u8 encryption; |
| 5817 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5818 | }; |
| 5819 | |
| 5820 | static int is_cowonly_root(u64 root_objectid) |
| 5821 | { |
| 5822 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 5823 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 5824 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 5825 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5826 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5827 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5828 | return 1; |
| 5829 | return 0; |
| 5830 | } |
| 5831 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5832 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5833 | struct btrfs_root *extent_root, |
| 5834 | struct btrfs_ref_path *ref_path, |
| 5835 | int first_time) |
| 5836 | { |
| 5837 | struct extent_buffer *leaf; |
| 5838 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5839 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5840 | struct btrfs_key key; |
| 5841 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5842 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5843 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5844 | int level; |
| 5845 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5846 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5847 | path = btrfs_alloc_path(); |
| 5848 | if (!path) |
| 5849 | return -ENOMEM; |
| 5850 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5851 | if (first_time) { |
| 5852 | ref_path->lowest_level = -1; |
| 5853 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5854 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5855 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5856 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5857 | walk_down: |
| 5858 | level = ref_path->current_level - 1; |
| 5859 | while (level >= -1) { |
| 5860 | u64 parent; |
| 5861 | if (level < ref_path->lowest_level) |
| 5862 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5863 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5864 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5865 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5866 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5867 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5868 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5869 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5870 | parent = ref_path->nodes[level + 1]; |
| 5871 | ref_path->nodes[level + 1] = 0; |
| 5872 | ref_path->current_level = level; |
| 5873 | BUG_ON(parent == 0); |
| 5874 | |
| 5875 | key.objectid = bytenr; |
| 5876 | key.offset = parent + 1; |
| 5877 | key.type = BTRFS_EXTENT_REF_KEY; |
| 5878 | |
| 5879 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5880 | if (ret < 0) |
| 5881 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5882 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5883 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5884 | leaf = path->nodes[0]; |
| 5885 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5886 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5887 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5888 | if (ret < 0) |
| 5889 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5890 | if (ret > 0) |
| 5891 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5892 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5893 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5894 | |
| 5895 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5896 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5897 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 5898 | if (level < ref_path->shared_level) |
| 5899 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5900 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5901 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5902 | next: |
| 5903 | level--; |
| 5904 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5905 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5906 | } |
| 5907 | /* reached lowest level */ |
| 5908 | ret = 1; |
| 5909 | goto out; |
| 5910 | walk_up: |
| 5911 | level = ref_path->current_level; |
| 5912 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 5913 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5914 | |
| 5915 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5916 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5917 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5918 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5919 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5920 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5921 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5922 | key.objectid = bytenr; |
| 5923 | key.offset = 0; |
| 5924 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5925 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5926 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 5927 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5928 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5929 | |
| 5930 | leaf = path->nodes[0]; |
| 5931 | nritems = btrfs_header_nritems(leaf); |
| 5932 | if (path->slots[0] >= nritems) { |
| 5933 | ret = btrfs_next_leaf(extent_root, path); |
| 5934 | if (ret < 0) |
| 5935 | goto out; |
| 5936 | if (ret > 0) { |
| 5937 | /* the extent was freed by someone */ |
| 5938 | if (ref_path->lowest_level == level) |
| 5939 | goto out; |
| 5940 | btrfs_release_path(extent_root, path); |
| 5941 | goto walk_down; |
| 5942 | } |
| 5943 | leaf = path->nodes[0]; |
| 5944 | } |
| 5945 | |
| 5946 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5947 | if (found_key.objectid != bytenr || |
| 5948 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 5949 | /* the extent was freed by someone */ |
| 5950 | if (ref_path->lowest_level == level) { |
| 5951 | ret = 1; |
| 5952 | goto out; |
| 5953 | } |
| 5954 | btrfs_release_path(extent_root, path); |
| 5955 | goto walk_down; |
| 5956 | } |
| 5957 | found: |
| 5958 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 5959 | struct btrfs_extent_ref); |
| 5960 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 5961 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5962 | if (first_time) { |
| 5963 | level = (int)ref_objectid; |
| 5964 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 5965 | ref_path->lowest_level = level; |
| 5966 | ref_path->current_level = level; |
| 5967 | ref_path->nodes[level] = bytenr; |
| 5968 | } else { |
| 5969 | WARN_ON(ref_objectid != level); |
| 5970 | } |
| 5971 | } else { |
| 5972 | WARN_ON(level != -1); |
| 5973 | } |
| 5974 | first_time = 0; |
| 5975 | |
| 5976 | if (ref_path->lowest_level == level) { |
| 5977 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5978 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 5979 | } |
| 5980 | |
| 5981 | /* |
| 5982 | * the block is tree root or the block isn't in reference |
| 5983 | * counted tree. |
| 5984 | */ |
| 5985 | if (found_key.objectid == found_key.offset || |
| 5986 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 5987 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 5988 | ref_path->root_generation = |
| 5989 | btrfs_ref_generation(leaf, ref); |
| 5990 | if (level < 0) { |
| 5991 | /* special reference from the tree log */ |
| 5992 | ref_path->nodes[0] = found_key.offset; |
| 5993 | ref_path->current_level = 0; |
| 5994 | } |
| 5995 | ret = 0; |
| 5996 | goto out; |
| 5997 | } |
| 5998 | |
| 5999 | level++; |
| 6000 | BUG_ON(ref_path->nodes[level] != 0); |
| 6001 | ref_path->nodes[level] = found_key.offset; |
| 6002 | ref_path->current_level = level; |
| 6003 | |
| 6004 | /* |
| 6005 | * the reference was created in the running transaction, |
| 6006 | * no need to continue walking up. |
| 6007 | */ |
| 6008 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6009 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6010 | ref_path->root_generation = |
| 6011 | btrfs_ref_generation(leaf, ref); |
| 6012 | ret = 0; |
| 6013 | goto out; |
| 6014 | } |
| 6015 | |
| 6016 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6017 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6018 | } |
| 6019 | /* reached max tree level, but no tree root found. */ |
| 6020 | BUG(); |
| 6021 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6022 | btrfs_free_path(path); |
| 6023 | return ret; |
| 6024 | } |
| 6025 | |
| 6026 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6027 | struct btrfs_root *extent_root, |
| 6028 | struct btrfs_ref_path *ref_path, |
| 6029 | u64 extent_start) |
| 6030 | { |
| 6031 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6032 | ref_path->extent_start = extent_start; |
| 6033 | |
| 6034 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6035 | } |
| 6036 | |
| 6037 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6038 | struct btrfs_root *extent_root, |
| 6039 | struct btrfs_ref_path *ref_path) |
| 6040 | { |
| 6041 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6042 | } |
| 6043 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6044 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6045 | struct btrfs_key *extent_key, |
| 6046 | u64 offset, int no_fragment, |
| 6047 | struct disk_extent **extents, |
| 6048 | int *nr_extents) |
| 6049 | { |
| 6050 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6051 | struct btrfs_path *path; |
| 6052 | struct btrfs_file_extent_item *fi; |
| 6053 | struct extent_buffer *leaf; |
| 6054 | struct disk_extent *exts = *extents; |
| 6055 | struct btrfs_key found_key; |
| 6056 | u64 cur_pos; |
| 6057 | u64 last_byte; |
| 6058 | u32 nritems; |
| 6059 | int nr = 0; |
| 6060 | int max = *nr_extents; |
| 6061 | int ret; |
| 6062 | |
| 6063 | WARN_ON(!no_fragment && *extents); |
| 6064 | if (!exts) { |
| 6065 | max = 1; |
| 6066 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6067 | if (!exts) |
| 6068 | return -ENOMEM; |
| 6069 | } |
| 6070 | |
| 6071 | path = btrfs_alloc_path(); |
| 6072 | BUG_ON(!path); |
| 6073 | |
| 6074 | cur_pos = extent_key->objectid - offset; |
| 6075 | last_byte = extent_key->objectid + extent_key->offset; |
| 6076 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6077 | cur_pos, 0); |
| 6078 | if (ret < 0) |
| 6079 | goto out; |
| 6080 | if (ret > 0) { |
| 6081 | ret = -ENOENT; |
| 6082 | goto out; |
| 6083 | } |
| 6084 | |
| 6085 | while (1) { |
| 6086 | leaf = path->nodes[0]; |
| 6087 | nritems = btrfs_header_nritems(leaf); |
| 6088 | if (path->slots[0] >= nritems) { |
| 6089 | ret = btrfs_next_leaf(root, path); |
| 6090 | if (ret < 0) |
| 6091 | goto out; |
| 6092 | if (ret > 0) |
| 6093 | break; |
| 6094 | leaf = path->nodes[0]; |
| 6095 | } |
| 6096 | |
| 6097 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6098 | if (found_key.offset != cur_pos || |
| 6099 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6100 | found_key.objectid != reloc_inode->i_ino) |
| 6101 | break; |
| 6102 | |
| 6103 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6104 | struct btrfs_file_extent_item); |
| 6105 | if (btrfs_file_extent_type(leaf, fi) != |
| 6106 | BTRFS_FILE_EXTENT_REG || |
| 6107 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6108 | break; |
| 6109 | |
| 6110 | if (nr == max) { |
| 6111 | struct disk_extent *old = exts; |
| 6112 | max *= 2; |
| 6113 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6114 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6115 | if (old != *extents) |
| 6116 | kfree(old); |
| 6117 | } |
| 6118 | |
| 6119 | exts[nr].disk_bytenr = |
| 6120 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6121 | exts[nr].disk_num_bytes = |
| 6122 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6123 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6124 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6125 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6126 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6127 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6128 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6129 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6130 | BUG_ON(exts[nr].offset > 0); |
| 6131 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6132 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6133 | |
| 6134 | cur_pos += exts[nr].num_bytes; |
| 6135 | nr++; |
| 6136 | |
| 6137 | if (cur_pos + offset >= last_byte) |
| 6138 | break; |
| 6139 | |
| 6140 | if (no_fragment) { |
| 6141 | ret = 1; |
| 6142 | goto out; |
| 6143 | } |
| 6144 | path->slots[0]++; |
| 6145 | } |
| 6146 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6147 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6148 | if (cur_pos + offset < last_byte) { |
| 6149 | ret = -ENOENT; |
| 6150 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6151 | } |
| 6152 | ret = 0; |
| 6153 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6154 | btrfs_free_path(path); |
| 6155 | if (ret) { |
| 6156 | if (exts != *extents) |
| 6157 | kfree(exts); |
| 6158 | } else { |
| 6159 | *extents = exts; |
| 6160 | *nr_extents = nr; |
| 6161 | } |
| 6162 | return ret; |
| 6163 | } |
| 6164 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6165 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6166 | struct btrfs_root *root, |
| 6167 | struct btrfs_path *path, |
| 6168 | struct btrfs_key *extent_key, |
| 6169 | struct btrfs_key *leaf_key, |
| 6170 | struct btrfs_ref_path *ref_path, |
| 6171 | struct disk_extent *new_extents, |
| 6172 | int nr_extents) |
| 6173 | { |
| 6174 | struct extent_buffer *leaf; |
| 6175 | struct btrfs_file_extent_item *fi; |
| 6176 | struct inode *inode = NULL; |
| 6177 | struct btrfs_key key; |
| 6178 | u64 lock_start = 0; |
| 6179 | u64 lock_end = 0; |
| 6180 | u64 num_bytes; |
| 6181 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6182 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6183 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6184 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6185 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6186 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6187 | int ret; |
| 6188 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6189 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6190 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6191 | if (key.objectid < ref_path->owner_objectid || |
| 6192 | (key.objectid == ref_path->owner_objectid && |
| 6193 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6194 | key.objectid = ref_path->owner_objectid; |
| 6195 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6196 | key.offset = 0; |
| 6197 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | while (1) { |
| 6201 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6202 | if (ret < 0) |
| 6203 | goto out; |
| 6204 | |
| 6205 | leaf = path->nodes[0]; |
| 6206 | nritems = btrfs_header_nritems(leaf); |
| 6207 | next: |
| 6208 | if (extent_locked && ret > 0) { |
| 6209 | /* |
| 6210 | * the file extent item was modified by someone |
| 6211 | * before the extent got locked. |
| 6212 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6213 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6214 | lock_end, GFP_NOFS); |
| 6215 | extent_locked = 0; |
| 6216 | } |
| 6217 | |
| 6218 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6219 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6220 | break; |
| 6221 | |
| 6222 | BUG_ON(extent_locked); |
| 6223 | ret = btrfs_next_leaf(root, path); |
| 6224 | if (ret < 0) |
| 6225 | goto out; |
| 6226 | if (ret > 0) |
| 6227 | break; |
| 6228 | leaf = path->nodes[0]; |
| 6229 | nritems = btrfs_header_nritems(leaf); |
| 6230 | } |
| 6231 | |
| 6232 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 6233 | |
| 6234 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 6235 | if ((key.objectid > ref_path->owner_objectid) || |
| 6236 | (key.objectid == ref_path->owner_objectid && |
| 6237 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6238 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6239 | break; |
| 6240 | } |
| 6241 | |
| 6242 | if (inode && key.objectid != inode->i_ino) { |
| 6243 | BUG_ON(extent_locked); |
| 6244 | btrfs_release_path(root, path); |
| 6245 | mutex_unlock(&inode->i_mutex); |
| 6246 | iput(inode); |
| 6247 | inode = NULL; |
| 6248 | continue; |
| 6249 | } |
| 6250 | |
| 6251 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6252 | path->slots[0]++; |
| 6253 | ret = 1; |
| 6254 | goto next; |
| 6255 | } |
| 6256 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6257 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6258 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 6259 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 6260 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6261 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 6262 | extent_key->objectid)) { |
| 6263 | path->slots[0]++; |
| 6264 | ret = 1; |
| 6265 | goto next; |
| 6266 | } |
| 6267 | |
| 6268 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6269 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 6270 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6271 | if (search_end == (u64)-1) { |
| 6272 | search_end = key.offset - ext_offset + |
| 6273 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 6274 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6275 | |
| 6276 | if (!extent_locked) { |
| 6277 | lock_start = key.offset; |
| 6278 | lock_end = lock_start + num_bytes - 1; |
| 6279 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6280 | if (lock_start > key.offset || |
| 6281 | lock_end + 1 < key.offset + num_bytes) { |
| 6282 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6283 | lock_start, lock_end, GFP_NOFS); |
| 6284 | extent_locked = 0; |
| 6285 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | if (!inode) { |
| 6289 | btrfs_release_path(root, path); |
| 6290 | |
| 6291 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 6292 | key.objectid, root); |
| 6293 | if (inode->i_state & I_NEW) { |
| 6294 | BTRFS_I(inode)->root = root; |
| 6295 | BTRFS_I(inode)->location.objectid = |
| 6296 | key.objectid; |
| 6297 | BTRFS_I(inode)->location.type = |
| 6298 | BTRFS_INODE_ITEM_KEY; |
| 6299 | BTRFS_I(inode)->location.offset = 0; |
| 6300 | btrfs_read_locked_inode(inode); |
| 6301 | unlock_new_inode(inode); |
| 6302 | } |
| 6303 | /* |
| 6304 | * some code call btrfs_commit_transaction while |
| 6305 | * holding the i_mutex, so we can't use mutex_lock |
| 6306 | * here. |
| 6307 | */ |
| 6308 | if (is_bad_inode(inode) || |
| 6309 | !mutex_trylock(&inode->i_mutex)) { |
| 6310 | iput(inode); |
| 6311 | inode = NULL; |
| 6312 | key.offset = (u64)-1; |
| 6313 | goto skip; |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | if (!extent_locked) { |
| 6318 | struct btrfs_ordered_extent *ordered; |
| 6319 | |
| 6320 | btrfs_release_path(root, path); |
| 6321 | |
| 6322 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6323 | lock_end, GFP_NOFS); |
| 6324 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6325 | lock_end); |
| 6326 | if (ordered && |
| 6327 | ordered->file_offset <= lock_end && |
| 6328 | ordered->file_offset + ordered->len > lock_start) { |
| 6329 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6330 | lock_start, lock_end, GFP_NOFS); |
| 6331 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6332 | btrfs_put_ordered_extent(ordered); |
| 6333 | key.offset += num_bytes; |
| 6334 | goto skip; |
| 6335 | } |
| 6336 | if (ordered) |
| 6337 | btrfs_put_ordered_extent(ordered); |
| 6338 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6339 | extent_locked = 1; |
| 6340 | continue; |
| 6341 | } |
| 6342 | |
| 6343 | if (nr_extents == 1) { |
| 6344 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6345 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6346 | new_extents[0].disk_bytenr); |
| 6347 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6348 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6349 | btrfs_mark_buffer_dirty(leaf); |
| 6350 | |
| 6351 | btrfs_drop_extent_cache(inode, key.offset, |
| 6352 | key.offset + num_bytes - 1, 0); |
| 6353 | |
| 6354 | ret = btrfs_inc_extent_ref(trans, root, |
| 6355 | new_extents[0].disk_bytenr, |
| 6356 | new_extents[0].disk_num_bytes, |
| 6357 | leaf->start, |
| 6358 | root->root_key.objectid, |
| 6359 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6360 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6361 | BUG_ON(ret); |
| 6362 | |
| 6363 | ret = btrfs_free_extent(trans, root, |
| 6364 | extent_key->objectid, |
| 6365 | extent_key->offset, |
| 6366 | leaf->start, |
| 6367 | btrfs_header_owner(leaf), |
| 6368 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6369 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6370 | BUG_ON(ret); |
| 6371 | |
| 6372 | btrfs_release_path(root, path); |
| 6373 | key.offset += num_bytes; |
| 6374 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6375 | BUG_ON(1); |
| 6376 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6377 | u64 alloc_hint; |
| 6378 | u64 extent_len; |
| 6379 | int i; |
| 6380 | /* |
| 6381 | * drop old extent pointer at first, then insert the |
| 6382 | * new pointers one bye one |
| 6383 | */ |
| 6384 | btrfs_release_path(root, path); |
| 6385 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 6386 | key.offset + num_bytes, |
| 6387 | key.offset, &alloc_hint); |
| 6388 | BUG_ON(ret); |
| 6389 | |
| 6390 | for (i = 0; i < nr_extents; i++) { |
| 6391 | if (ext_offset >= new_extents[i].num_bytes) { |
| 6392 | ext_offset -= new_extents[i].num_bytes; |
| 6393 | continue; |
| 6394 | } |
| 6395 | extent_len = min(new_extents[i].num_bytes - |
| 6396 | ext_offset, num_bytes); |
| 6397 | |
| 6398 | ret = btrfs_insert_empty_item(trans, root, |
| 6399 | path, &key, |
| 6400 | sizeof(*fi)); |
| 6401 | BUG_ON(ret); |
| 6402 | |
| 6403 | leaf = path->nodes[0]; |
| 6404 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6405 | struct btrfs_file_extent_item); |
| 6406 | btrfs_set_file_extent_generation(leaf, fi, |
| 6407 | trans->transid); |
| 6408 | btrfs_set_file_extent_type(leaf, fi, |
| 6409 | BTRFS_FILE_EXTENT_REG); |
| 6410 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6411 | new_extents[i].disk_bytenr); |
| 6412 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6413 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6414 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 6415 | new_extents[i].ram_bytes); |
| 6416 | |
| 6417 | btrfs_set_file_extent_compression(leaf, fi, |
| 6418 | new_extents[i].compression); |
| 6419 | btrfs_set_file_extent_encryption(leaf, fi, |
| 6420 | new_extents[i].encryption); |
| 6421 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 6422 | new_extents[i].other_encoding); |
| 6423 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6424 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 6425 | extent_len); |
| 6426 | ext_offset += new_extents[i].offset; |
| 6427 | btrfs_set_file_extent_offset(leaf, fi, |
| 6428 | ext_offset); |
| 6429 | btrfs_mark_buffer_dirty(leaf); |
| 6430 | |
| 6431 | btrfs_drop_extent_cache(inode, key.offset, |
| 6432 | key.offset + extent_len - 1, 0); |
| 6433 | |
| 6434 | ret = btrfs_inc_extent_ref(trans, root, |
| 6435 | new_extents[i].disk_bytenr, |
| 6436 | new_extents[i].disk_num_bytes, |
| 6437 | leaf->start, |
| 6438 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6439 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6440 | BUG_ON(ret); |
| 6441 | btrfs_release_path(root, path); |
| 6442 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6443 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6444 | |
| 6445 | ext_offset = 0; |
| 6446 | num_bytes -= extent_len; |
| 6447 | key.offset += extent_len; |
| 6448 | |
| 6449 | if (num_bytes == 0) |
| 6450 | break; |
| 6451 | } |
| 6452 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6453 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6454 | } |
| 6455 | |
| 6456 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6457 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6458 | lock_end, GFP_NOFS); |
| 6459 | extent_locked = 0; |
| 6460 | } |
| 6461 | skip: |
| 6462 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6463 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6464 | break; |
| 6465 | |
| 6466 | cond_resched(); |
| 6467 | } |
| 6468 | ret = 0; |
| 6469 | out: |
| 6470 | btrfs_release_path(root, path); |
| 6471 | if (inode) { |
| 6472 | mutex_unlock(&inode->i_mutex); |
| 6473 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6474 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6475 | lock_end, GFP_NOFS); |
| 6476 | } |
| 6477 | iput(inode); |
| 6478 | } |
| 6479 | return ret; |
| 6480 | } |
| 6481 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6482 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 6483 | struct btrfs_root *root, |
| 6484 | struct extent_buffer *buf, u64 orig_start) |
| 6485 | { |
| 6486 | int level; |
| 6487 | int ret; |
| 6488 | |
| 6489 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 6490 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6491 | |
| 6492 | level = btrfs_header_level(buf); |
| 6493 | if (level == 0) { |
| 6494 | struct btrfs_leaf_ref *ref; |
| 6495 | struct btrfs_leaf_ref *orig_ref; |
| 6496 | |
| 6497 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 6498 | if (!orig_ref) |
| 6499 | return -ENOENT; |
| 6500 | |
| 6501 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 6502 | if (!ref) { |
| 6503 | btrfs_free_leaf_ref(root, orig_ref); |
| 6504 | return -ENOMEM; |
| 6505 | } |
| 6506 | |
| 6507 | ref->nritems = orig_ref->nritems; |
| 6508 | memcpy(ref->extents, orig_ref->extents, |
| 6509 | sizeof(ref->extents[0]) * ref->nritems); |
| 6510 | |
| 6511 | btrfs_free_leaf_ref(root, orig_ref); |
| 6512 | |
| 6513 | ref->root_gen = trans->transid; |
| 6514 | ref->bytenr = buf->start; |
| 6515 | ref->owner = btrfs_header_owner(buf); |
| 6516 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6517 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6518 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 6519 | WARN_ON(ret); |
| 6520 | btrfs_free_leaf_ref(root, ref); |
| 6521 | } |
| 6522 | return 0; |
| 6523 | } |
| 6524 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6525 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6526 | struct extent_buffer *leaf, |
| 6527 | struct btrfs_block_group_cache *group, |
| 6528 | struct btrfs_root *target_root) |
| 6529 | { |
| 6530 | struct btrfs_key key; |
| 6531 | struct inode *inode = NULL; |
| 6532 | struct btrfs_file_extent_item *fi; |
| 6533 | u64 num_bytes; |
| 6534 | u64 skip_objectid = 0; |
| 6535 | u32 nritems; |
| 6536 | u32 i; |
| 6537 | |
| 6538 | nritems = btrfs_header_nritems(leaf); |
| 6539 | for (i = 0; i < nritems; i++) { |
| 6540 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6541 | if (key.objectid == skip_objectid || |
| 6542 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 6543 | continue; |
| 6544 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6545 | if (btrfs_file_extent_type(leaf, fi) == |
| 6546 | BTRFS_FILE_EXTENT_INLINE) |
| 6547 | continue; |
| 6548 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6549 | continue; |
| 6550 | if (!inode || inode->i_ino != key.objectid) { |
| 6551 | iput(inode); |
| 6552 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 6553 | key.objectid, target_root, 1); |
| 6554 | } |
| 6555 | if (!inode) { |
| 6556 | skip_objectid = key.objectid; |
| 6557 | continue; |
| 6558 | } |
| 6559 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6560 | |
| 6561 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 6562 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6563 | btrfs_drop_extent_cache(inode, key.offset, |
| 6564 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6565 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 6566 | key.offset + num_bytes - 1, GFP_NOFS); |
| 6567 | cond_resched(); |
| 6568 | } |
| 6569 | iput(inode); |
| 6570 | return 0; |
| 6571 | } |
| 6572 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6573 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6574 | struct btrfs_root *root, |
| 6575 | struct extent_buffer *leaf, |
| 6576 | struct btrfs_block_group_cache *group, |
| 6577 | struct inode *reloc_inode) |
| 6578 | { |
| 6579 | struct btrfs_key key; |
| 6580 | struct btrfs_key extent_key; |
| 6581 | struct btrfs_file_extent_item *fi; |
| 6582 | struct btrfs_leaf_ref *ref; |
| 6583 | struct disk_extent *new_extent; |
| 6584 | u64 bytenr; |
| 6585 | u64 num_bytes; |
| 6586 | u32 nritems; |
| 6587 | u32 i; |
| 6588 | int ext_index; |
| 6589 | int nr_extent; |
| 6590 | int ret; |
| 6591 | |
| 6592 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 6593 | BUG_ON(!new_extent); |
| 6594 | |
| 6595 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 6596 | BUG_ON(!ref); |
| 6597 | |
| 6598 | ext_index = -1; |
| 6599 | nritems = btrfs_header_nritems(leaf); |
| 6600 | for (i = 0; i < nritems; i++) { |
| 6601 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6602 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 6603 | continue; |
| 6604 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6605 | if (btrfs_file_extent_type(leaf, fi) == |
| 6606 | BTRFS_FILE_EXTENT_INLINE) |
| 6607 | continue; |
| 6608 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6609 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6610 | if (bytenr == 0) |
| 6611 | continue; |
| 6612 | |
| 6613 | ext_index++; |
| 6614 | if (bytenr >= group->key.objectid + group->key.offset || |
| 6615 | bytenr + num_bytes <= group->key.objectid) |
| 6616 | continue; |
| 6617 | |
| 6618 | extent_key.objectid = bytenr; |
| 6619 | extent_key.offset = num_bytes; |
| 6620 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6621 | nr_extent = 1; |
| 6622 | ret = get_new_locations(reloc_inode, &extent_key, |
| 6623 | group->key.objectid, 1, |
| 6624 | &new_extent, &nr_extent); |
| 6625 | if (ret > 0) |
| 6626 | continue; |
| 6627 | BUG_ON(ret < 0); |
| 6628 | |
| 6629 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 6630 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 6631 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 6632 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 6633 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6634 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6635 | new_extent->disk_bytenr); |
| 6636 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6637 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6638 | btrfs_mark_buffer_dirty(leaf); |
| 6639 | |
| 6640 | ret = btrfs_inc_extent_ref(trans, root, |
| 6641 | new_extent->disk_bytenr, |
| 6642 | new_extent->disk_num_bytes, |
| 6643 | leaf->start, |
| 6644 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6645 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6646 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6647 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6648 | ret = btrfs_free_extent(trans, root, |
| 6649 | bytenr, num_bytes, leaf->start, |
| 6650 | btrfs_header_owner(leaf), |
| 6651 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6652 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6653 | BUG_ON(ret); |
| 6654 | cond_resched(); |
| 6655 | } |
| 6656 | kfree(new_extent); |
| 6657 | BUG_ON(ext_index + 1 != ref->nritems); |
| 6658 | btrfs_free_leaf_ref(root, ref); |
| 6659 | return 0; |
| 6660 | } |
| 6661 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6662 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 6663 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6664 | { |
| 6665 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6666 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6667 | |
| 6668 | if (root->reloc_root) { |
| 6669 | reloc_root = root->reloc_root; |
| 6670 | root->reloc_root = NULL; |
| 6671 | list_add(&reloc_root->dead_list, |
| 6672 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6673 | |
| 6674 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 6675 | reloc_root->node->start); |
| 6676 | btrfs_set_root_level(&root->root_item, |
| 6677 | btrfs_header_level(reloc_root->node)); |
| 6678 | memset(&reloc_root->root_item.drop_progress, 0, |
| 6679 | sizeof(struct btrfs_disk_key)); |
| 6680 | reloc_root->root_item.drop_level = 0; |
| 6681 | |
| 6682 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 6683 | &reloc_root->root_key, |
| 6684 | &reloc_root->root_item); |
| 6685 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6686 | } |
| 6687 | return 0; |
| 6688 | } |
| 6689 | |
| 6690 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 6691 | { |
| 6692 | struct btrfs_trans_handle *trans; |
| 6693 | struct btrfs_root *reloc_root; |
| 6694 | struct btrfs_root *prev_root = NULL; |
| 6695 | struct list_head dead_roots; |
| 6696 | int ret; |
| 6697 | unsigned long nr; |
| 6698 | |
| 6699 | INIT_LIST_HEAD(&dead_roots); |
| 6700 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 6701 | |
| 6702 | while (!list_empty(&dead_roots)) { |
| 6703 | reloc_root = list_entry(dead_roots.prev, |
| 6704 | struct btrfs_root, dead_list); |
| 6705 | list_del_init(&reloc_root->dead_list); |
| 6706 | |
| 6707 | BUG_ON(reloc_root->commit_root != NULL); |
| 6708 | while (1) { |
| 6709 | trans = btrfs_join_transaction(root, 1); |
| 6710 | BUG_ON(!trans); |
| 6711 | |
| 6712 | mutex_lock(&root->fs_info->drop_mutex); |
| 6713 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 6714 | if (ret != -EAGAIN) |
| 6715 | break; |
| 6716 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6717 | |
| 6718 | nr = trans->blocks_used; |
| 6719 | ret = btrfs_end_transaction(trans, root); |
| 6720 | BUG_ON(ret); |
| 6721 | btrfs_btree_balance_dirty(root, nr); |
| 6722 | } |
| 6723 | |
| 6724 | free_extent_buffer(reloc_root->node); |
| 6725 | |
| 6726 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 6727 | &reloc_root->root_key); |
| 6728 | BUG_ON(ret); |
| 6729 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6730 | |
| 6731 | nr = trans->blocks_used; |
| 6732 | ret = btrfs_end_transaction(trans, root); |
| 6733 | BUG_ON(ret); |
| 6734 | btrfs_btree_balance_dirty(root, nr); |
| 6735 | |
| 6736 | kfree(prev_root); |
| 6737 | prev_root = reloc_root; |
| 6738 | } |
| 6739 | if (prev_root) { |
| 6740 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 6741 | kfree(prev_root); |
| 6742 | } |
| 6743 | return 0; |
| 6744 | } |
| 6745 | |
| 6746 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 6747 | { |
| 6748 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 6749 | return 0; |
| 6750 | } |
| 6751 | |
| 6752 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 6753 | { |
| 6754 | struct btrfs_root *reloc_root; |
| 6755 | struct btrfs_trans_handle *trans; |
| 6756 | struct btrfs_key location; |
| 6757 | int found; |
| 6758 | int ret; |
| 6759 | |
| 6760 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6761 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 6762 | BUG_ON(ret); |
| 6763 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 6764 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 6765 | |
| 6766 | if (found) { |
| 6767 | trans = btrfs_start_transaction(root, 1); |
| 6768 | BUG_ON(!trans); |
| 6769 | ret = btrfs_commit_transaction(trans, root); |
| 6770 | BUG_ON(ret); |
| 6771 | } |
| 6772 | |
| 6773 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6774 | location.offset = (u64)-1; |
| 6775 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 6776 | |
| 6777 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 6778 | BUG_ON(!reloc_root); |
| 6779 | btrfs_orphan_cleanup(reloc_root); |
| 6780 | return 0; |
| 6781 | } |
| 6782 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6783 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6784 | struct btrfs_root *root) |
| 6785 | { |
| 6786 | struct btrfs_root *reloc_root; |
| 6787 | struct extent_buffer *eb; |
| 6788 | struct btrfs_root_item *root_item; |
| 6789 | struct btrfs_key root_key; |
| 6790 | int ret; |
| 6791 | |
| 6792 | BUG_ON(!root->ref_cows); |
| 6793 | if (root->reloc_root) |
| 6794 | return 0; |
| 6795 | |
| 6796 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 6797 | BUG_ON(!root_item); |
| 6798 | |
| 6799 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 6800 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 6801 | BUG_ON(ret); |
| 6802 | |
| 6803 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 6804 | root_key.offset = root->root_key.objectid; |
| 6805 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6806 | |
| 6807 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 6808 | btrfs_set_root_refs(root_item, 0); |
| 6809 | btrfs_set_root_bytenr(root_item, eb->start); |
| 6810 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6811 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6812 | |
| 6813 | btrfs_tree_unlock(eb); |
| 6814 | free_extent_buffer(eb); |
| 6815 | |
| 6816 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 6817 | &root_key, root_item); |
| 6818 | BUG_ON(ret); |
| 6819 | kfree(root_item); |
| 6820 | |
| 6821 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 6822 | &root_key); |
| 6823 | BUG_ON(!reloc_root); |
| 6824 | reloc_root->last_trans = trans->transid; |
| 6825 | reloc_root->commit_root = NULL; |
| 6826 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 6827 | |
| 6828 | root->reloc_root = reloc_root; |
| 6829 | return 0; |
| 6830 | } |
| 6831 | |
| 6832 | /* |
| 6833 | * Core function of space balance. |
| 6834 | * |
| 6835 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6836 | * counted roots. There is one reloc tree for each subvol, and all |
| 6837 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 6838 | * of the latest committed roots of subvols (root->commit_root). |
| 6839 | * |
| 6840 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 6841 | * COW the block through subvol's reloc tree, then update block pointer |
| 6842 | * in the subvol to point to the new block. Since all reloc trees share |
| 6843 | * same root key objectid, doing special handing for tree blocks owned |
| 6844 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 6845 | * we can use the resulting new block directly when the same block is |
| 6846 | * required to COW again through other reloc trees. By this way, relocated |
| 6847 | * tree blocks are shared between reloc trees, so they are also shared |
| 6848 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6849 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6850 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6851 | struct btrfs_root *root, |
| 6852 | struct btrfs_path *path, |
| 6853 | struct btrfs_key *first_key, |
| 6854 | struct btrfs_ref_path *ref_path, |
| 6855 | struct btrfs_block_group_cache *group, |
| 6856 | struct inode *reloc_inode) |
| 6857 | { |
| 6858 | struct btrfs_root *reloc_root; |
| 6859 | struct extent_buffer *eb = NULL; |
| 6860 | struct btrfs_key *keys; |
| 6861 | u64 *nodes; |
| 6862 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6863 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6864 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6865 | int ret; |
| 6866 | |
| 6867 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 6868 | lowest_level = ref_path->owner_objectid; |
| 6869 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6870 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6871 | path->lowest_level = lowest_level; |
| 6872 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 6873 | BUG_ON(ret < 0); |
| 6874 | path->lowest_level = 0; |
| 6875 | btrfs_release_path(root, path); |
| 6876 | return 0; |
| 6877 | } |
| 6878 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6879 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6880 | ret = init_reloc_tree(trans, root); |
| 6881 | BUG_ON(ret); |
| 6882 | reloc_root = root->reloc_root; |
| 6883 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6884 | shared_level = ref_path->shared_level; |
| 6885 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6886 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6887 | keys = ref_path->node_keys; |
| 6888 | nodes = ref_path->new_nodes; |
| 6889 | memset(&keys[shared_level + 1], 0, |
| 6890 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 6891 | memset(&nodes[shared_level + 1], 0, |
| 6892 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6893 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6894 | if (nodes[lowest_level] == 0) { |
| 6895 | path->lowest_level = lowest_level; |
| 6896 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6897 | 0, 1); |
| 6898 | BUG_ON(ret); |
| 6899 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 6900 | eb = path->nodes[level]; |
| 6901 | if (!eb || eb == reloc_root->node) |
| 6902 | break; |
| 6903 | nodes[level] = eb->start; |
| 6904 | if (level == 0) |
| 6905 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 6906 | else |
| 6907 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 6908 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6909 | if (nodes[0] && |
| 6910 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6911 | eb = path->nodes[0]; |
| 6912 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 6913 | group, reloc_inode); |
| 6914 | BUG_ON(ret); |
| 6915 | } |
| 6916 | btrfs_release_path(reloc_root, path); |
| 6917 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6918 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6919 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6920 | BUG_ON(ret); |
| 6921 | } |
| 6922 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6923 | /* |
| 6924 | * replace tree blocks in the fs tree with tree blocks in |
| 6925 | * the reloc tree. |
| 6926 | */ |
| 6927 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 6928 | BUG_ON(ret < 0); |
| 6929 | |
| 6930 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6931 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6932 | 0, 0); |
| 6933 | BUG_ON(ret); |
| 6934 | extent_buffer_get(path->nodes[0]); |
| 6935 | eb = path->nodes[0]; |
| 6936 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6937 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 6938 | BUG_ON(ret); |
| 6939 | free_extent_buffer(eb); |
| 6940 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6941 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6942 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6943 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6944 | return 0; |
| 6945 | } |
| 6946 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6947 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6948 | struct btrfs_root *root, |
| 6949 | struct btrfs_path *path, |
| 6950 | struct btrfs_key *first_key, |
| 6951 | struct btrfs_ref_path *ref_path) |
| 6952 | { |
| 6953 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6954 | |
| 6955 | ret = relocate_one_path(trans, root, path, first_key, |
| 6956 | ref_path, NULL, NULL); |
| 6957 | BUG_ON(ret); |
| 6958 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6959 | return 0; |
| 6960 | } |
| 6961 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6962 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6963 | struct btrfs_root *extent_root, |
| 6964 | struct btrfs_path *path, |
| 6965 | struct btrfs_key *extent_key) |
| 6966 | { |
| 6967 | int ret; |
| 6968 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6969 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 6970 | if (ret) |
| 6971 | goto out; |
| 6972 | ret = btrfs_del_item(trans, extent_root, path); |
| 6973 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6974 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6975 | return ret; |
| 6976 | } |
| 6977 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6978 | 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] | 6979 | struct btrfs_ref_path *ref_path) |
| 6980 | { |
| 6981 | struct btrfs_key root_key; |
| 6982 | |
| 6983 | root_key.objectid = ref_path->root_objectid; |
| 6984 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6985 | if (is_cowonly_root(ref_path->root_objectid)) |
| 6986 | root_key.offset = 0; |
| 6987 | else |
| 6988 | root_key.offset = (u64)-1; |
| 6989 | |
| 6990 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 6991 | } |
| 6992 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6993 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6994 | struct btrfs_path *path, |
| 6995 | struct btrfs_key *extent_key, |
| 6996 | struct btrfs_block_group_cache *group, |
| 6997 | struct inode *reloc_inode, int pass) |
| 6998 | { |
| 6999 | struct btrfs_trans_handle *trans; |
| 7000 | struct btrfs_root *found_root; |
| 7001 | struct btrfs_ref_path *ref_path = NULL; |
| 7002 | struct disk_extent *new_extents = NULL; |
| 7003 | int nr_extents = 0; |
| 7004 | int loops; |
| 7005 | int ret; |
| 7006 | int level; |
| 7007 | struct btrfs_key first_key; |
| 7008 | u64 prev_block = 0; |
| 7009 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7010 | |
| 7011 | trans = btrfs_start_transaction(extent_root, 1); |
| 7012 | BUG_ON(!trans); |
| 7013 | |
| 7014 | if (extent_key->objectid == 0) { |
| 7015 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7016 | goto out; |
| 7017 | } |
| 7018 | |
| 7019 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7020 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7021 | ret = -ENOMEM; |
| 7022 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7023 | } |
| 7024 | |
| 7025 | for (loops = 0; ; loops++) { |
| 7026 | if (loops == 0) { |
| 7027 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7028 | extent_key->objectid); |
| 7029 | } else { |
| 7030 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7031 | } |
| 7032 | if (ret < 0) |
| 7033 | goto out; |
| 7034 | if (ret > 0) |
| 7035 | break; |
| 7036 | |
| 7037 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7038 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7039 | continue; |
| 7040 | |
| 7041 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7042 | BUG_ON(!found_root); |
| 7043 | /* |
| 7044 | * for reference counted tree, only process reference paths |
| 7045 | * rooted at the latest committed root. |
| 7046 | */ |
| 7047 | if (found_root->ref_cows && |
| 7048 | ref_path->root_generation != found_root->root_key.offset) |
| 7049 | continue; |
| 7050 | |
| 7051 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7052 | if (pass == 0) { |
| 7053 | /* |
| 7054 | * copy data extents to new locations |
| 7055 | */ |
| 7056 | u64 group_start = group->key.objectid; |
| 7057 | ret = relocate_data_extent(reloc_inode, |
| 7058 | extent_key, |
| 7059 | group_start); |
| 7060 | if (ret < 0) |
| 7061 | goto out; |
| 7062 | break; |
| 7063 | } |
| 7064 | level = 0; |
| 7065 | } else { |
| 7066 | level = ref_path->owner_objectid; |
| 7067 | } |
| 7068 | |
| 7069 | if (prev_block != ref_path->nodes[level]) { |
| 7070 | struct extent_buffer *eb; |
| 7071 | u64 block_start = ref_path->nodes[level]; |
| 7072 | u64 block_size = btrfs_level_size(found_root, level); |
| 7073 | |
| 7074 | eb = read_tree_block(found_root, block_start, |
| 7075 | block_size, 0); |
| 7076 | btrfs_tree_lock(eb); |
| 7077 | BUG_ON(level != btrfs_header_level(eb)); |
| 7078 | |
| 7079 | if (level == 0) |
| 7080 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7081 | else |
| 7082 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7083 | |
| 7084 | btrfs_tree_unlock(eb); |
| 7085 | free_extent_buffer(eb); |
| 7086 | prev_block = block_start; |
| 7087 | } |
| 7088 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7089 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7090 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7091 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7092 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7093 | /* |
| 7094 | * try to update data extent references while |
| 7095 | * keeping metadata shared between snapshots. |
| 7096 | */ |
| 7097 | if (pass == 1) { |
| 7098 | ret = relocate_one_path(trans, found_root, |
| 7099 | path, &first_key, ref_path, |
| 7100 | group, reloc_inode); |
| 7101 | if (ret < 0) |
| 7102 | goto out; |
| 7103 | continue; |
| 7104 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7105 | /* |
| 7106 | * use fallback method to process the remaining |
| 7107 | * references. |
| 7108 | */ |
| 7109 | if (!new_extents) { |
| 7110 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7111 | new_extents = kmalloc(sizeof(*new_extents), |
| 7112 | GFP_NOFS); |
| 7113 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7114 | ret = get_new_locations(reloc_inode, |
| 7115 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7116 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7117 | &new_extents, |
| 7118 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7119 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7120 | goto out; |
| 7121 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7122 | ret = replace_one_extent(trans, found_root, |
| 7123 | path, extent_key, |
| 7124 | &first_key, ref_path, |
| 7125 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7126 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7127 | ret = relocate_tree_block(trans, found_root, path, |
| 7128 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7129 | } |
| 7130 | if (ret < 0) |
| 7131 | goto out; |
| 7132 | } |
| 7133 | ret = 0; |
| 7134 | out: |
| 7135 | btrfs_end_transaction(trans, extent_root); |
| 7136 | kfree(new_extents); |
| 7137 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7138 | return ret; |
| 7139 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7140 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7141 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7142 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7143 | { |
| 7144 | u64 num_devices; |
| 7145 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7146 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7147 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7148 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7149 | if (num_devices == 1) { |
| 7150 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7151 | stripped = flags & ~stripped; |
| 7152 | |
| 7153 | /* turn raid0 into single device chunks */ |
| 7154 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7155 | return stripped; |
| 7156 | |
| 7157 | /* turn mirroring into duplication */ |
| 7158 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7159 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7160 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7161 | return flags; |
| 7162 | } else { |
| 7163 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7164 | if (flags & stripped) |
| 7165 | return flags; |
| 7166 | |
| 7167 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7168 | stripped = flags & ~stripped; |
| 7169 | |
| 7170 | /* switch duplicated blocks with raid1 */ |
| 7171 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7172 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7173 | |
| 7174 | /* turn single device chunks into raid0 */ |
| 7175 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7176 | } |
| 7177 | return flags; |
| 7178 | } |
| 7179 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7180 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7181 | struct btrfs_block_group_cache *shrink_block_group, |
| 7182 | int force) |
| 7183 | { |
| 7184 | struct btrfs_trans_handle *trans; |
| 7185 | u64 new_alloc_flags; |
| 7186 | u64 calc; |
| 7187 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7188 | spin_lock(&shrink_block_group->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7189 | if (btrfs_block_group_used(&shrink_block_group->item) + |
| 7190 | shrink_block_group->reserved > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7191 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7192 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7193 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7194 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7195 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7196 | new_alloc_flags = update_block_group_flags(root, |
| 7197 | shrink_block_group->flags); |
| 7198 | if (new_alloc_flags != shrink_block_group->flags) { |
| 7199 | calc = |
| 7200 | btrfs_block_group_used(&shrink_block_group->item); |
| 7201 | } else { |
| 7202 | calc = shrink_block_group->key.offset; |
| 7203 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7204 | spin_unlock(&shrink_block_group->lock); |
| 7205 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7206 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 7207 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7208 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7209 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7210 | } else |
| 7211 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7212 | return 0; |
| 7213 | } |
| 7214 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7215 | |
| 7216 | int btrfs_prepare_block_group_relocation(struct btrfs_root *root, |
| 7217 | struct btrfs_block_group_cache *group) |
| 7218 | |
| 7219 | { |
| 7220 | __alloc_chunk_for_shrink(root, group, 1); |
| 7221 | set_block_group_readonly(group); |
| 7222 | return 0; |
| 7223 | } |
| 7224 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7225 | /* |
| 7226 | * checks to see if its even possible to relocate this block group. |
| 7227 | * |
| 7228 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7229 | * ok to go ahead and try. |
| 7230 | */ |
| 7231 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
| 7232 | { |
| 7233 | struct btrfs_block_group_cache *block_group; |
| 7234 | struct btrfs_space_info *space_info; |
| 7235 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7236 | struct btrfs_device *device; |
| 7237 | int full = 0; |
| 7238 | int ret = 0; |
| 7239 | |
| 7240 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 7241 | |
| 7242 | /* odd, couldn't find the block group, leave it alone */ |
| 7243 | if (!block_group) |
| 7244 | return -1; |
| 7245 | |
| 7246 | /* no bytes used, we're good */ |
| 7247 | if (!btrfs_block_group_used(&block_group->item)) |
| 7248 | goto out; |
| 7249 | |
| 7250 | space_info = block_group->space_info; |
| 7251 | spin_lock(&space_info->lock); |
| 7252 | |
| 7253 | full = space_info->full; |
| 7254 | |
| 7255 | /* |
| 7256 | * if this is the last block group we have in this space, we can't |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7257 | * relocate it unless we're able to allocate a new chunk below. |
| 7258 | * |
| 7259 | * Otherwise, we need to make sure we have room in the space to handle |
| 7260 | * all of the extents from this block group. If we can, we're good |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7261 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7262 | if ((space_info->total_bytes != block_group->key.offset) && |
| 7263 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7264 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7265 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7266 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7267 | spin_unlock(&space_info->lock); |
| 7268 | goto out; |
| 7269 | } |
| 7270 | spin_unlock(&space_info->lock); |
| 7271 | |
| 7272 | /* |
| 7273 | * ok we don't have enough space, but maybe we have free space on our |
| 7274 | * devices to allocate new chunks for relocation, so loop through our |
| 7275 | * alloc devices and guess if we have enough space. However, if we |
| 7276 | * were marked as full, then we know there aren't enough chunks, and we |
| 7277 | * can just return. |
| 7278 | */ |
| 7279 | ret = -1; |
| 7280 | if (full) |
| 7281 | goto out; |
| 7282 | |
| 7283 | mutex_lock(&root->fs_info->chunk_mutex); |
| 7284 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 7285 | u64 min_free = btrfs_block_group_used(&block_group->item); |
| 7286 | u64 dev_offset, max_avail; |
| 7287 | |
| 7288 | /* |
| 7289 | * check to make sure we can actually find a chunk with enough |
| 7290 | * space to fit our block group in. |
| 7291 | */ |
| 7292 | if (device->total_bytes > device->bytes_used + min_free) { |
| 7293 | ret = find_free_dev_extent(NULL, device, min_free, |
| 7294 | &dev_offset, &max_avail); |
| 7295 | if (!ret) |
| 7296 | break; |
| 7297 | ret = -1; |
| 7298 | } |
| 7299 | } |
| 7300 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 7301 | out: |
| 7302 | btrfs_put_block_group(block_group); |
| 7303 | return ret; |
| 7304 | } |
| 7305 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7306 | static int find_first_block_group(struct btrfs_root *root, |
| 7307 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7308 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7309 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7310 | struct btrfs_key found_key; |
| 7311 | struct extent_buffer *leaf; |
| 7312 | int slot; |
| 7313 | |
| 7314 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7315 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7316 | goto out; |
| 7317 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7318 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7319 | slot = path->slots[0]; |
| 7320 | leaf = path->nodes[0]; |
| 7321 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7322 | ret = btrfs_next_leaf(root, path); |
| 7323 | if (ret == 0) |
| 7324 | continue; |
| 7325 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7326 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7327 | break; |
| 7328 | } |
| 7329 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7330 | |
| 7331 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7332 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7333 | ret = 0; |
| 7334 | goto out; |
| 7335 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7336 | path->slots[0]++; |
| 7337 | } |
| 7338 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7339 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7340 | return ret; |
| 7341 | } |
| 7342 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7343 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7344 | { |
| 7345 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7346 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7347 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7348 | struct rb_node *n; |
| 7349 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7350 | down_write(&info->extent_commit_sem); |
| 7351 | while (!list_empty(&info->caching_block_groups)) { |
| 7352 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7353 | struct btrfs_caching_control, list); |
| 7354 | list_del(&caching_ctl->list); |
| 7355 | put_caching_control(caching_ctl); |
| 7356 | } |
| 7357 | up_write(&info->extent_commit_sem); |
| 7358 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7359 | spin_lock(&info->block_group_cache_lock); |
| 7360 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7361 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7362 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7363 | rb_erase(&block_group->cache_node, |
| 7364 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7365 | spin_unlock(&info->block_group_cache_lock); |
| 7366 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7367 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7368 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7369 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7370 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7371 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7372 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7373 | |
| 7374 | btrfs_remove_free_space_cache(block_group); |
| 7375 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7376 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7377 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7378 | |
| 7379 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7380 | } |
| 7381 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7382 | |
| 7383 | /* now that all the block groups are freed, go through and |
| 7384 | * free all the space_info structs. This is only called during |
| 7385 | * the final stages of unmount, and so we know nobody is |
| 7386 | * using them. We call synchronize_rcu() once before we start, |
| 7387 | * just to be on the safe side. |
| 7388 | */ |
| 7389 | synchronize_rcu(); |
| 7390 | |
| 7391 | while(!list_empty(&info->space_info)) { |
| 7392 | space_info = list_entry(info->space_info.next, |
| 7393 | struct btrfs_space_info, |
| 7394 | list); |
| 7395 | |
| 7396 | list_del(&space_info->list); |
| 7397 | kfree(space_info); |
| 7398 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7399 | return 0; |
| 7400 | } |
| 7401 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7402 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7403 | { |
| 7404 | struct btrfs_path *path; |
| 7405 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7406 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7407 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7408 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7409 | struct btrfs_key key; |
| 7410 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7411 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7412 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7413 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7414 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7415 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7416 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7417 | path = btrfs_alloc_path(); |
| 7418 | if (!path) |
| 7419 | return -ENOMEM; |
| 7420 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7421 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7422 | ret = find_first_block_group(root, path, &key); |
| 7423 | if (ret > 0) { |
| 7424 | ret = 0; |
| 7425 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7426 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7427 | if (ret != 0) |
| 7428 | goto error; |
| 7429 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7430 | leaf = path->nodes[0]; |
| 7431 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7432 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7433 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7434 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7435 | break; |
| 7436 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7437 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7438 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7439 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7440 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7441 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7442 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7443 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7444 | |
| 7445 | /* |
| 7446 | * we only want to have 32k of ram per block group for keeping |
| 7447 | * track of free space, and if we pass 1/2 of that we want to |
| 7448 | * start converting things over to using bitmaps |
| 7449 | */ |
| 7450 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7451 | sizeof(struct btrfs_free_space); |
| 7452 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7453 | read_extent_buffer(leaf, &cache->item, |
| 7454 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7455 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7456 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7457 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7458 | key.objectid = found_key.objectid + found_key.offset; |
| 7459 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7460 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7461 | cache->sectorsize = root->sectorsize; |
| 7462 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7463 | /* |
| 7464 | * check for two cases, either we are full, and therefore |
| 7465 | * don't need to bother with the caching work since we won't |
| 7466 | * find any space, or we are empty, and we can just add all |
| 7467 | * the space in and be done with it. This saves us _alot_ of |
| 7468 | * time, particularly in the full case. |
| 7469 | */ |
| 7470 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7471 | exclude_super_stripes(root, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7472 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7473 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7474 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7475 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7476 | exclude_super_stripes(root, cache); |
| 7477 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7478 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7479 | add_new_free_space(cache, root->fs_info, |
| 7480 | found_key.objectid, |
| 7481 | found_key.objectid + |
| 7482 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7483 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7484 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7485 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7486 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7487 | btrfs_block_group_used(&cache->item), |
| 7488 | &space_info); |
| 7489 | BUG_ON(ret); |
| 7490 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7491 | spin_lock(&cache->space_info->lock); |
| 7492 | cache->space_info->bytes_super += cache->bytes_super; |
| 7493 | spin_unlock(&cache->space_info->lock); |
| 7494 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7495 | down_write(&space_info->groups_sem); |
| 7496 | list_add_tail(&cache->list, &space_info->block_groups); |
| 7497 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7498 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7499 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7500 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7501 | |
| 7502 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7503 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 7504 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7505 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7506 | ret = 0; |
| 7507 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7508 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7509 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7510 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7511 | |
| 7512 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7513 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7514 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7515 | u64 size) |
| 7516 | { |
| 7517 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7518 | struct btrfs_root *extent_root; |
| 7519 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7520 | |
| 7521 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7522 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7523 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7524 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7525 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7526 | if (!cache) |
| 7527 | return -ENOMEM; |
| 7528 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7529 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7530 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7531 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7532 | cache->sectorsize = root->sectorsize; |
| 7533 | |
| 7534 | /* |
| 7535 | * we only want to have 32k of ram per block group for keeping track |
| 7536 | * of free space, and if we pass 1/2 of that we want to start |
| 7537 | * converting things over to using bitmaps |
| 7538 | */ |
| 7539 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7540 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7541 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7542 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7543 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7544 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7545 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7546 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7547 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7548 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7549 | cache->flags = type; |
| 7550 | btrfs_set_block_group_flags(&cache->item, type); |
| 7551 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7552 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7553 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7554 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7555 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7556 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7557 | chunk_offset + size); |
| 7558 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7559 | free_excluded_extents(root, cache); |
| 7560 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7561 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7562 | &cache->space_info); |
| 7563 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7564 | |
| 7565 | spin_lock(&cache->space_info->lock); |
| 7566 | cache->space_info->bytes_super += cache->bytes_super; |
| 7567 | spin_unlock(&cache->space_info->lock); |
| 7568 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7569 | down_write(&cache->space_info->groups_sem); |
| 7570 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 7571 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7572 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7573 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7574 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7575 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7576 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7577 | sizeof(cache->item)); |
| 7578 | BUG_ON(ret); |
| 7579 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7580 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7581 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7582 | return 0; |
| 7583 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7584 | |
| 7585 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7586 | struct btrfs_root *root, u64 group_start) |
| 7587 | { |
| 7588 | struct btrfs_path *path; |
| 7589 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7590 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7591 | struct btrfs_key key; |
| 7592 | int ret; |
| 7593 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7594 | root = root->fs_info->extent_root; |
| 7595 | |
| 7596 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7597 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7598 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7599 | |
| 7600 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7601 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7602 | /* make sure this block group isn't part of an allocation cluster */ |
| 7603 | cluster = &root->fs_info->data_alloc_cluster; |
| 7604 | spin_lock(&cluster->refill_lock); |
| 7605 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7606 | spin_unlock(&cluster->refill_lock); |
| 7607 | |
| 7608 | /* |
| 7609 | * make sure this block group isn't part of a metadata |
| 7610 | * allocation cluster |
| 7611 | */ |
| 7612 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7613 | spin_lock(&cluster->refill_lock); |
| 7614 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7615 | spin_unlock(&cluster->refill_lock); |
| 7616 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7617 | path = btrfs_alloc_path(); |
| 7618 | BUG_ON(!path); |
| 7619 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7620 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7621 | rb_erase(&block_group->cache_node, |
| 7622 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7623 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7624 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7625 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7626 | /* |
| 7627 | * we must use list_del_init so people can check to see if they |
| 7628 | * are still on the list after taking the semaphore |
| 7629 | */ |
| 7630 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7631 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7632 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7633 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7634 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7635 | |
| 7636 | btrfs_remove_free_space_cache(block_group); |
| 7637 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7638 | spin_lock(&block_group->space_info->lock); |
| 7639 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7640 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 7641 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7642 | |
| 7643 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7644 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7645 | btrfs_put_block_group(block_group); |
| 7646 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7647 | |
| 7648 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7649 | if (ret > 0) |
| 7650 | ret = -EIO; |
| 7651 | if (ret < 0) |
| 7652 | goto out; |
| 7653 | |
| 7654 | ret = btrfs_del_item(trans, root, path); |
| 7655 | out: |
| 7656 | btrfs_free_path(path); |
| 7657 | return ret; |
| 7658 | } |