Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 20 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 22 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 23 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 25 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 26 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 27 | #include "ctree.h" |
| 28 | #include "disk-io.h" |
| 29 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 30 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 32 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 33 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 34 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 35 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 36 | struct btrfs_root *root, |
| 37 | u64 bytenr, u64 num_bytes, int alloc, |
| 38 | int mark_free); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 39 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 40 | u64 num_bytes, int reserve); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 41 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 42 | struct btrfs_root *root, |
| 43 | u64 bytenr, u64 num_bytes, u64 parent, |
| 44 | u64 root_objectid, u64 owner_objectid, |
| 45 | u64 owner_offset, int refs_to_drop, |
| 46 | struct btrfs_delayed_extent_op *extra_op); |
| 47 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 48 | struct extent_buffer *leaf, |
| 49 | struct btrfs_extent_item *ei); |
| 50 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 51 | struct btrfs_root *root, |
| 52 | u64 parent, u64 root_objectid, |
| 53 | u64 flags, u64 owner, u64 offset, |
| 54 | struct btrfs_key *ins, int ref_mod); |
| 55 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
| 57 | u64 parent, u64 root_objectid, |
| 58 | u64 flags, struct btrfs_disk_key *key, |
| 59 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 60 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 62 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 63 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 64 | struct btrfs_root *root, |
| 65 | struct btrfs_path *path, |
| 66 | u64 bytenr, u64 num_bytes, |
| 67 | int is_data, int reserved, |
| 68 | struct extent_buffer **must_clean); |
| 69 | static int find_next_key(struct btrfs_path *path, int level, |
| 70 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 71 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 72 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 73 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 74 | static noinline int |
| 75 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 76 | { |
| 77 | smp_mb(); |
| 78 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 79 | } |
| 80 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 81 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 82 | { |
| 83 | return (cache->flags & bits) == bits; |
| 84 | } |
| 85 | |
| 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 | fa9c0d79 | 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 | fa9c0d79 | 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 | fa9c0d79 | 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 | { |
Christoph Hellwig | 746cd1e | 2009-09-12 07:35:43 +0200 | [diff] [blame] | 1574 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, |
| 1575 | DISCARD_FL_BARRIER); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1576 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1577 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1578 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1579 | u64 num_bytes) |
| 1580 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1581 | int ret; |
| 1582 | u64 map_length = num_bytes; |
| 1583 | struct btrfs_multi_bio *multi = NULL; |
| 1584 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1585 | if (!btrfs_test_opt(root, DISCARD)) |
| 1586 | return 0; |
| 1587 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1588 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1589 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1590 | bytenr, &map_length, &multi, 0); |
| 1591 | if (!ret) { |
| 1592 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1593 | int i; |
| 1594 | |
| 1595 | if (map_length > num_bytes) |
| 1596 | map_length = num_bytes; |
| 1597 | |
| 1598 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1599 | btrfs_issue_discard(stripe->dev->bdev, |
| 1600 | stripe->physical, |
| 1601 | map_length); |
| 1602 | } |
| 1603 | kfree(multi); |
| 1604 | } |
| 1605 | |
| 1606 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1607 | } |
| 1608 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1609 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1610 | struct btrfs_root *root, |
| 1611 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1612 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1613 | { |
| 1614 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1615 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1616 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1617 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1618 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1619 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1620 | parent, root_objectid, (int)owner, |
| 1621 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1622 | } else { |
| 1623 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1624 | parent, root_objectid, owner, offset, |
| 1625 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1626 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1627 | return ret; |
| 1628 | } |
| 1629 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1630 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1631 | struct btrfs_root *root, |
| 1632 | u64 bytenr, u64 num_bytes, |
| 1633 | u64 parent, u64 root_objectid, |
| 1634 | u64 owner, u64 offset, int refs_to_add, |
| 1635 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1636 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1637 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1638 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1639 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1640 | u64 refs; |
| 1641 | int ret; |
| 1642 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1643 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1644 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1645 | if (!path) |
| 1646 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1647 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1648 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1649 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1650 | /* this will setup the path even if it fails to insert the back ref */ |
| 1651 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1652 | path, bytenr, num_bytes, parent, |
| 1653 | root_objectid, owner, offset, |
| 1654 | refs_to_add, extent_op); |
| 1655 | if (ret == 0) |
| 1656 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1657 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1658 | if (ret != -EAGAIN) { |
| 1659 | err = ret; |
| 1660 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1661 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1662 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1663 | leaf = path->nodes[0]; |
| 1664 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1665 | refs = btrfs_extent_refs(leaf, item); |
| 1666 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1667 | if (extent_op) |
| 1668 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1669 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1670 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1671 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1672 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1673 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1674 | path->leave_spinning = 1; |
| 1675 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1676 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1677 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1678 | path, bytenr, parent, root_objectid, |
| 1679 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1680 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1681 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1682 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1683 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1684 | } |
| 1685 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1686 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1687 | struct btrfs_root *root, |
| 1688 | struct btrfs_delayed_ref_node *node, |
| 1689 | struct btrfs_delayed_extent_op *extent_op, |
| 1690 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1691 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1692 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1693 | struct btrfs_delayed_data_ref *ref; |
| 1694 | struct btrfs_key ins; |
| 1695 | u64 parent = 0; |
| 1696 | u64 ref_root = 0; |
| 1697 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1698 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1699 | ins.objectid = node->bytenr; |
| 1700 | ins.offset = node->num_bytes; |
| 1701 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1702 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1703 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1704 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1705 | parent = ref->parent; |
| 1706 | else |
| 1707 | ref_root = ref->root; |
| 1708 | |
| 1709 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1710 | if (extent_op) { |
| 1711 | BUG_ON(extent_op->update_key); |
| 1712 | flags |= extent_op->flags_to_set; |
| 1713 | } |
| 1714 | ret = alloc_reserved_file_extent(trans, root, |
| 1715 | parent, ref_root, flags, |
| 1716 | ref->objectid, ref->offset, |
| 1717 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1718 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1719 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1720 | node->num_bytes, parent, |
| 1721 | ref_root, ref->objectid, |
| 1722 | ref->offset, node->ref_mod, |
| 1723 | extent_op); |
| 1724 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1725 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1726 | node->num_bytes, parent, |
| 1727 | ref_root, ref->objectid, |
| 1728 | ref->offset, node->ref_mod, |
| 1729 | extent_op); |
| 1730 | } else { |
| 1731 | BUG(); |
| 1732 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1733 | return ret; |
| 1734 | } |
| 1735 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1736 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1737 | struct extent_buffer *leaf, |
| 1738 | struct btrfs_extent_item *ei) |
| 1739 | { |
| 1740 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1741 | if (extent_op->update_flags) { |
| 1742 | flags |= extent_op->flags_to_set; |
| 1743 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1744 | } |
| 1745 | |
| 1746 | if (extent_op->update_key) { |
| 1747 | struct btrfs_tree_block_info *bi; |
| 1748 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1749 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1750 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1755 | struct btrfs_root *root, |
| 1756 | struct btrfs_delayed_ref_node *node, |
| 1757 | struct btrfs_delayed_extent_op *extent_op) |
| 1758 | { |
| 1759 | struct btrfs_key key; |
| 1760 | struct btrfs_path *path; |
| 1761 | struct btrfs_extent_item *ei; |
| 1762 | struct extent_buffer *leaf; |
| 1763 | u32 item_size; |
| 1764 | int ret; |
| 1765 | int err = 0; |
| 1766 | |
| 1767 | path = btrfs_alloc_path(); |
| 1768 | if (!path) |
| 1769 | return -ENOMEM; |
| 1770 | |
| 1771 | key.objectid = node->bytenr; |
| 1772 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1773 | key.offset = node->num_bytes; |
| 1774 | |
| 1775 | path->reada = 1; |
| 1776 | path->leave_spinning = 1; |
| 1777 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1778 | path, 0, 1); |
| 1779 | if (ret < 0) { |
| 1780 | err = ret; |
| 1781 | goto out; |
| 1782 | } |
| 1783 | if (ret > 0) { |
| 1784 | err = -EIO; |
| 1785 | goto out; |
| 1786 | } |
| 1787 | |
| 1788 | leaf = path->nodes[0]; |
| 1789 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1790 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1791 | if (item_size < sizeof(*ei)) { |
| 1792 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1793 | path, (u64)-1, 0); |
| 1794 | if (ret < 0) { |
| 1795 | err = ret; |
| 1796 | goto out; |
| 1797 | } |
| 1798 | leaf = path->nodes[0]; |
| 1799 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1800 | } |
| 1801 | #endif |
| 1802 | BUG_ON(item_size < sizeof(*ei)); |
| 1803 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1804 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1805 | |
| 1806 | btrfs_mark_buffer_dirty(leaf); |
| 1807 | out: |
| 1808 | btrfs_free_path(path); |
| 1809 | return err; |
| 1810 | } |
| 1811 | |
| 1812 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1813 | struct btrfs_root *root, |
| 1814 | struct btrfs_delayed_ref_node *node, |
| 1815 | struct btrfs_delayed_extent_op *extent_op, |
| 1816 | int insert_reserved) |
| 1817 | { |
| 1818 | int ret = 0; |
| 1819 | struct btrfs_delayed_tree_ref *ref; |
| 1820 | struct btrfs_key ins; |
| 1821 | u64 parent = 0; |
| 1822 | u64 ref_root = 0; |
| 1823 | |
| 1824 | ins.objectid = node->bytenr; |
| 1825 | ins.offset = node->num_bytes; |
| 1826 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1827 | |
| 1828 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1829 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1830 | parent = ref->parent; |
| 1831 | else |
| 1832 | ref_root = ref->root; |
| 1833 | |
| 1834 | BUG_ON(node->ref_mod != 1); |
| 1835 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1836 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1837 | !extent_op->update_key); |
| 1838 | ret = alloc_reserved_tree_block(trans, root, |
| 1839 | parent, ref_root, |
| 1840 | extent_op->flags_to_set, |
| 1841 | &extent_op->key, |
| 1842 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1843 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1844 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1845 | node->num_bytes, parent, ref_root, |
| 1846 | ref->level, 0, 1, extent_op); |
| 1847 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1848 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1849 | node->num_bytes, parent, ref_root, |
| 1850 | ref->level, 0, 1, extent_op); |
| 1851 | } else { |
| 1852 | BUG(); |
| 1853 | } |
| 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1858 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1859 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1860 | struct btrfs_root *root, |
| 1861 | struct btrfs_delayed_ref_node *node, |
| 1862 | struct btrfs_delayed_extent_op *extent_op, |
| 1863 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1864 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1865 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1866 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1867 | struct btrfs_delayed_ref_head *head; |
| 1868 | /* |
| 1869 | * we've hit the end of the chain and we were supposed |
| 1870 | * to insert this extent into the tree. But, it got |
| 1871 | * deleted before we ever needed to insert it, so all |
| 1872 | * we have to do is clean up the accounting |
| 1873 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1874 | BUG_ON(extent_op); |
| 1875 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1876 | if (insert_reserved) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1877 | int mark_free = 0; |
| 1878 | struct extent_buffer *must_clean = NULL; |
| 1879 | |
| 1880 | ret = pin_down_bytes(trans, root, NULL, |
| 1881 | node->bytenr, node->num_bytes, |
| 1882 | head->is_data, 1, &must_clean); |
| 1883 | if (ret > 0) |
| 1884 | mark_free = 1; |
| 1885 | |
| 1886 | if (must_clean) { |
| 1887 | clean_tree_block(NULL, root, must_clean); |
| 1888 | btrfs_tree_unlock(must_clean); |
| 1889 | free_extent_buffer(must_clean); |
| 1890 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1891 | if (head->is_data) { |
| 1892 | ret = btrfs_del_csums(trans, root, |
| 1893 | node->bytenr, |
| 1894 | node->num_bytes); |
| 1895 | BUG_ON(ret); |
| 1896 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 1897 | if (mark_free) { |
| 1898 | ret = btrfs_free_reserved_extent(root, |
| 1899 | node->bytenr, |
| 1900 | node->num_bytes); |
| 1901 | BUG_ON(ret); |
| 1902 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1903 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1904 | mutex_unlock(&head->mutex); |
| 1905 | return 0; |
| 1906 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1907 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1908 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1909 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1910 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 1911 | insert_reserved); |
| 1912 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1913 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1914 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 1915 | insert_reserved); |
| 1916 | else |
| 1917 | BUG(); |
| 1918 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1919 | } |
| 1920 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1921 | static noinline struct btrfs_delayed_ref_node * |
| 1922 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1923 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1924 | struct rb_node *node; |
| 1925 | struct btrfs_delayed_ref_node *ref; |
| 1926 | int action = BTRFS_ADD_DELAYED_REF; |
| 1927 | again: |
| 1928 | /* |
| 1929 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1930 | * this prevents ref count from going down to zero when |
| 1931 | * there still are pending delayed ref. |
| 1932 | */ |
| 1933 | node = rb_prev(&head->node.rb_node); |
| 1934 | while (1) { |
| 1935 | if (!node) |
| 1936 | break; |
| 1937 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1938 | rb_node); |
| 1939 | if (ref->bytenr != head->node.bytenr) |
| 1940 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1941 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1942 | return ref; |
| 1943 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1944 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1945 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 1946 | action = BTRFS_DROP_DELAYED_REF; |
| 1947 | goto again; |
| 1948 | } |
| 1949 | return NULL; |
| 1950 | } |
| 1951 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1952 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 1953 | struct btrfs_root *root, |
| 1954 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1955 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1956 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1957 | struct btrfs_delayed_ref_node *ref; |
| 1958 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1959 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1960 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1961 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1962 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1963 | |
| 1964 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1965 | while (1) { |
| 1966 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1967 | /* pick a new head ref from the cluster list */ |
| 1968 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1969 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1970 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1971 | locked_ref = list_entry(cluster->next, |
| 1972 | struct btrfs_delayed_ref_head, cluster); |
| 1973 | |
| 1974 | /* grab the lock that says we are going to process |
| 1975 | * all the refs for this head */ |
| 1976 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 1977 | |
| 1978 | /* |
| 1979 | * we may have dropped the spin lock to get the head |
| 1980 | * mutex lock, and that might have given someone else |
| 1981 | * time to free the head. If that's true, it has been |
| 1982 | * removed from our list and we can move on. |
| 1983 | */ |
| 1984 | if (ret == -EAGAIN) { |
| 1985 | locked_ref = NULL; |
| 1986 | count++; |
| 1987 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * record the must insert reserved flag before we |
| 1993 | * drop the spin lock. |
| 1994 | */ |
| 1995 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1996 | locked_ref->must_insert_reserved = 0; |
| 1997 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1998 | extent_op = locked_ref->extent_op; |
| 1999 | locked_ref->extent_op = NULL; |
| 2000 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2001 | /* |
| 2002 | * locked_ref is the head node, so we have to go one |
| 2003 | * node back for any delayed ref updates |
| 2004 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2005 | ref = select_delayed_ref(locked_ref); |
| 2006 | if (!ref) { |
| 2007 | /* All delayed refs have been processed, Go ahead |
| 2008 | * and send the head node to run_one_delayed_ref, |
| 2009 | * so that any accounting fixes can happen |
| 2010 | */ |
| 2011 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2012 | |
| 2013 | if (extent_op && must_insert_reserved) { |
| 2014 | kfree(extent_op); |
| 2015 | extent_op = NULL; |
| 2016 | } |
| 2017 | |
| 2018 | if (extent_op) { |
| 2019 | spin_unlock(&delayed_refs->lock); |
| 2020 | |
| 2021 | ret = run_delayed_extent_op(trans, root, |
| 2022 | ref, extent_op); |
| 2023 | BUG_ON(ret); |
| 2024 | kfree(extent_op); |
| 2025 | |
| 2026 | cond_resched(); |
| 2027 | spin_lock(&delayed_refs->lock); |
| 2028 | continue; |
| 2029 | } |
| 2030 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2031 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2032 | locked_ref = NULL; |
| 2033 | } |
| 2034 | |
| 2035 | ref->in_tree = 0; |
| 2036 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2037 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2038 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2039 | spin_unlock(&delayed_refs->lock); |
| 2040 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2041 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2042 | must_insert_reserved); |
| 2043 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2044 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2045 | btrfs_put_delayed_ref(ref); |
| 2046 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2047 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2048 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2049 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2050 | spin_lock(&delayed_refs->lock); |
| 2051 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2052 | return count; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * this starts processing the delayed reference count updates and |
| 2057 | * extent insertions we have queued up so far. count can be |
| 2058 | * 0, which means to process everything in the tree at the start |
| 2059 | * of the run (but not newly added entries), or it can be some target |
| 2060 | * number you'd like to process. |
| 2061 | */ |
| 2062 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2063 | struct btrfs_root *root, unsigned long count) |
| 2064 | { |
| 2065 | struct rb_node *node; |
| 2066 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2067 | struct btrfs_delayed_ref_node *ref; |
| 2068 | struct list_head cluster; |
| 2069 | int ret; |
| 2070 | int run_all = count == (unsigned long)-1; |
| 2071 | int run_most = 0; |
| 2072 | |
| 2073 | if (root == root->fs_info->extent_root) |
| 2074 | root = root->fs_info->tree_root; |
| 2075 | |
| 2076 | delayed_refs = &trans->transaction->delayed_refs; |
| 2077 | INIT_LIST_HEAD(&cluster); |
| 2078 | again: |
| 2079 | spin_lock(&delayed_refs->lock); |
| 2080 | if (count == 0) { |
| 2081 | count = delayed_refs->num_entries * 2; |
| 2082 | run_most = 1; |
| 2083 | } |
| 2084 | while (1) { |
| 2085 | if (!(run_all || run_most) && |
| 2086 | delayed_refs->num_heads_ready < 64) |
| 2087 | break; |
| 2088 | |
| 2089 | /* |
| 2090 | * go find something we can process in the rbtree. We start at |
| 2091 | * the beginning of the tree, and then build a cluster |
| 2092 | * of refs to process starting at the first one we are able to |
| 2093 | * lock |
| 2094 | */ |
| 2095 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2096 | delayed_refs->run_delayed_start); |
| 2097 | if (ret) |
| 2098 | break; |
| 2099 | |
| 2100 | ret = run_clustered_refs(trans, root, &cluster); |
| 2101 | BUG_ON(ret < 0); |
| 2102 | |
| 2103 | count -= min_t(unsigned long, ret, count); |
| 2104 | |
| 2105 | if (count == 0) |
| 2106 | break; |
| 2107 | } |
| 2108 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2109 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2110 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2111 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2112 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2113 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2114 | |
| 2115 | while (node) { |
| 2116 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2117 | rb_node); |
| 2118 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2119 | struct btrfs_delayed_ref_head *head; |
| 2120 | |
| 2121 | head = btrfs_delayed_node_to_head(ref); |
| 2122 | atomic_inc(&ref->refs); |
| 2123 | |
| 2124 | spin_unlock(&delayed_refs->lock); |
| 2125 | mutex_lock(&head->mutex); |
| 2126 | mutex_unlock(&head->mutex); |
| 2127 | |
| 2128 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2129 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2130 | goto again; |
| 2131 | } |
| 2132 | node = rb_next(node); |
| 2133 | } |
| 2134 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2135 | schedule_timeout(1); |
| 2136 | goto again; |
| 2137 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2138 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2139 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2140 | return 0; |
| 2141 | } |
| 2142 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2143 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2144 | struct btrfs_root *root, |
| 2145 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2146 | int is_data) |
| 2147 | { |
| 2148 | struct btrfs_delayed_extent_op *extent_op; |
| 2149 | int ret; |
| 2150 | |
| 2151 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2152 | if (!extent_op) |
| 2153 | return -ENOMEM; |
| 2154 | |
| 2155 | extent_op->flags_to_set = flags; |
| 2156 | extent_op->update_flags = 1; |
| 2157 | extent_op->update_key = 0; |
| 2158 | extent_op->is_data = is_data ? 1 : 0; |
| 2159 | |
| 2160 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2161 | if (ret) |
| 2162 | kfree(extent_op); |
| 2163 | return ret; |
| 2164 | } |
| 2165 | |
| 2166 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2167 | struct btrfs_root *root, |
| 2168 | struct btrfs_path *path, |
| 2169 | u64 objectid, u64 offset, u64 bytenr) |
| 2170 | { |
| 2171 | struct btrfs_delayed_ref_head *head; |
| 2172 | struct btrfs_delayed_ref_node *ref; |
| 2173 | struct btrfs_delayed_data_ref *data_ref; |
| 2174 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2175 | struct rb_node *node; |
| 2176 | int ret = 0; |
| 2177 | |
| 2178 | ret = -ENOENT; |
| 2179 | delayed_refs = &trans->transaction->delayed_refs; |
| 2180 | spin_lock(&delayed_refs->lock); |
| 2181 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2182 | if (!head) |
| 2183 | goto out; |
| 2184 | |
| 2185 | if (!mutex_trylock(&head->mutex)) { |
| 2186 | atomic_inc(&head->node.refs); |
| 2187 | spin_unlock(&delayed_refs->lock); |
| 2188 | |
| 2189 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2190 | |
| 2191 | mutex_lock(&head->mutex); |
| 2192 | mutex_unlock(&head->mutex); |
| 2193 | btrfs_put_delayed_ref(&head->node); |
| 2194 | return -EAGAIN; |
| 2195 | } |
| 2196 | |
| 2197 | node = rb_prev(&head->node.rb_node); |
| 2198 | if (!node) |
| 2199 | goto out_unlock; |
| 2200 | |
| 2201 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2202 | |
| 2203 | if (ref->bytenr != bytenr) |
| 2204 | goto out_unlock; |
| 2205 | |
| 2206 | ret = 1; |
| 2207 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2208 | goto out_unlock; |
| 2209 | |
| 2210 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2211 | |
| 2212 | node = rb_prev(node); |
| 2213 | if (node) { |
| 2214 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2215 | if (ref->bytenr == bytenr) |
| 2216 | goto out_unlock; |
| 2217 | } |
| 2218 | |
| 2219 | if (data_ref->root != root->root_key.objectid || |
| 2220 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2221 | goto out_unlock; |
| 2222 | |
| 2223 | ret = 0; |
| 2224 | out_unlock: |
| 2225 | mutex_unlock(&head->mutex); |
| 2226 | out: |
| 2227 | spin_unlock(&delayed_refs->lock); |
| 2228 | return ret; |
| 2229 | } |
| 2230 | |
| 2231 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2232 | struct btrfs_root *root, |
| 2233 | struct btrfs_path *path, |
| 2234 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2235 | { |
| 2236 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2237 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2238 | struct btrfs_extent_data_ref *ref; |
| 2239 | struct btrfs_extent_inline_ref *iref; |
| 2240 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2241 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2242 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2243 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2244 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2245 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2246 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2247 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2248 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2249 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2250 | if (ret < 0) |
| 2251 | goto out; |
| 2252 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2253 | |
| 2254 | ret = -ENOENT; |
| 2255 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2256 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2257 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2258 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2259 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2260 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2261 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2262 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2263 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2264 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2265 | ret = 1; |
| 2266 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2267 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2268 | if (item_size < sizeof(*ei)) { |
| 2269 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2270 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2271 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2272 | #endif |
| 2273 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2274 | |
| 2275 | if (item_size != sizeof(*ei) + |
| 2276 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2277 | goto out; |
| 2278 | |
| 2279 | if (btrfs_extent_generation(leaf, ei) <= |
| 2280 | btrfs_root_last_snapshot(&root->root_item)) |
| 2281 | goto out; |
| 2282 | |
| 2283 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2284 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2285 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2286 | goto out; |
| 2287 | |
| 2288 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2289 | if (btrfs_extent_refs(leaf, ei) != |
| 2290 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2291 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2292 | root->root_key.objectid || |
| 2293 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2294 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2295 | goto out; |
| 2296 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2297 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2298 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2299 | return ret; |
| 2300 | } |
| 2301 | |
| 2302 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2303 | struct btrfs_root *root, |
| 2304 | u64 objectid, u64 offset, u64 bytenr) |
| 2305 | { |
| 2306 | struct btrfs_path *path; |
| 2307 | int ret; |
| 2308 | int ret2; |
| 2309 | |
| 2310 | path = btrfs_alloc_path(); |
| 2311 | if (!path) |
| 2312 | return -ENOENT; |
| 2313 | |
| 2314 | do { |
| 2315 | ret = check_committed_ref(trans, root, path, objectid, |
| 2316 | offset, bytenr); |
| 2317 | if (ret && ret != -ENOENT) |
| 2318 | goto out; |
| 2319 | |
| 2320 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2321 | offset, bytenr); |
| 2322 | } while (ret2 == -EAGAIN); |
| 2323 | |
| 2324 | if (ret2 && ret2 != -ENOENT) { |
| 2325 | ret = ret2; |
| 2326 | goto out; |
| 2327 | } |
| 2328 | |
| 2329 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2330 | ret = 0; |
| 2331 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2332 | btrfs_free_path(path); |
| 2333 | return ret; |
| 2334 | } |
| 2335 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2336 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2337 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2338 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2339 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2340 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2341 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2342 | u64 root_gen; |
| 2343 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2344 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2345 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2346 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2347 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2348 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2349 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2350 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2351 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2352 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2353 | shared = 0; |
| 2354 | root_gen = root->root_key.offset; |
| 2355 | } else { |
| 2356 | shared = 1; |
| 2357 | root_gen = trans->transid - 1; |
| 2358 | } |
| 2359 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2360 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2361 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2362 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2363 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2364 | struct btrfs_leaf_ref *ref; |
| 2365 | struct btrfs_extent_info *info; |
| 2366 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2367 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2368 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2369 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2370 | goto out; |
| 2371 | } |
| 2372 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2373 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2374 | ref->bytenr = buf->start; |
| 2375 | ref->owner = btrfs_header_owner(buf); |
| 2376 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2377 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2378 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2379 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2380 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2381 | u64 disk_bytenr; |
| 2382 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2383 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2384 | continue; |
| 2385 | fi = btrfs_item_ptr(buf, i, |
| 2386 | struct btrfs_file_extent_item); |
| 2387 | if (btrfs_file_extent_type(buf, fi) == |
| 2388 | BTRFS_FILE_EXTENT_INLINE) |
| 2389 | continue; |
| 2390 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2391 | if (disk_bytenr == 0) |
| 2392 | continue; |
| 2393 | |
| 2394 | info->bytenr = disk_bytenr; |
| 2395 | info->num_bytes = |
| 2396 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2397 | info->objectid = key.objectid; |
| 2398 | info->offset = key.offset; |
| 2399 | info++; |
| 2400 | } |
| 2401 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2402 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2403 | if (ret == -EEXIST && shared) { |
| 2404 | struct btrfs_leaf_ref *old; |
| 2405 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2406 | BUG_ON(!old); |
| 2407 | btrfs_remove_leaf_ref(root, old); |
| 2408 | btrfs_free_leaf_ref(root, old); |
| 2409 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2410 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2411 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2412 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2413 | } |
| 2414 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2415 | return ret; |
| 2416 | } |
| 2417 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2418 | /* when a block goes through cow, we update the reference counts of |
| 2419 | * everything that block points to. The internal pointers of the block |
| 2420 | * can be in just about any order, and it is likely to have clusters of |
| 2421 | * things that are close together and clusters of things that are not. |
| 2422 | * |
| 2423 | * To help reduce the seeks that come with updating all of these reference |
| 2424 | * counts, sort them by byte number before actual updates are done. |
| 2425 | * |
| 2426 | * struct refsort is used to match byte number to slot in the btree block. |
| 2427 | * we sort based on the byte number and then use the slot to actually |
| 2428 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2429 | * |
| 2430 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2431 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2432 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2433 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2434 | */ |
| 2435 | struct refsort { |
| 2436 | u64 bytenr; |
| 2437 | u32 slot; |
| 2438 | }; |
| 2439 | |
| 2440 | /* |
| 2441 | * for passing into sort() |
| 2442 | */ |
| 2443 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2444 | { |
| 2445 | const struct refsort *a = a_void; |
| 2446 | const struct refsort *b = b_void; |
| 2447 | |
| 2448 | if (a->bytenr < b->bytenr) |
| 2449 | return -1; |
| 2450 | if (a->bytenr > b->bytenr) |
| 2451 | return 1; |
| 2452 | return 0; |
| 2453 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2454 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2455 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2456 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2457 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2458 | struct extent_buffer *buf, |
| 2459 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2460 | { |
| 2461 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2462 | u64 num_bytes; |
| 2463 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2464 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2465 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2466 | struct btrfs_key key; |
| 2467 | struct btrfs_file_extent_item *fi; |
| 2468 | int i; |
| 2469 | int level; |
| 2470 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2471 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2472 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2473 | |
| 2474 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2475 | nritems = btrfs_header_nritems(buf); |
| 2476 | level = btrfs_header_level(buf); |
| 2477 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2478 | if (!root->ref_cows && level == 0) |
| 2479 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2480 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2481 | if (inc) |
| 2482 | process_func = btrfs_inc_extent_ref; |
| 2483 | else |
| 2484 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2485 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2486 | if (full_backref) |
| 2487 | parent = buf->start; |
| 2488 | else |
| 2489 | parent = 0; |
| 2490 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2491 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2492 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2493 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2494 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2495 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2496 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2497 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2498 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2499 | BTRFS_FILE_EXTENT_INLINE) |
| 2500 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2501 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2502 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2503 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2504 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2505 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2506 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2507 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2508 | parent, ref_root, key.objectid, |
| 2509 | key.offset); |
| 2510 | if (ret) |
| 2511 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2512 | } else { |
| 2513 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2514 | num_bytes = btrfs_level_size(root, level - 1); |
| 2515 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2516 | parent, ref_root, level - 1, 0); |
| 2517 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2518 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2519 | } |
| 2520 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2521 | return 0; |
| 2522 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2523 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2524 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2525 | } |
| 2526 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2527 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2528 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2529 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2530 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2531 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2532 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2533 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2534 | struct extent_buffer *buf, int full_backref) |
| 2535 | { |
| 2536 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2537 | } |
| 2538 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2539 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2540 | struct btrfs_root *root, |
| 2541 | struct btrfs_path *path, |
| 2542 | struct btrfs_block_group_cache *cache) |
| 2543 | { |
| 2544 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2545 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2546 | unsigned long bi; |
| 2547 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2548 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2549 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2550 | if (ret < 0) |
| 2551 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2552 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2553 | |
| 2554 | leaf = path->nodes[0]; |
| 2555 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2556 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2557 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2558 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2559 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2560 | if (ret) |
| 2561 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2562 | return 0; |
| 2563 | |
| 2564 | } |
| 2565 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2566 | static struct btrfs_block_group_cache * |
| 2567 | next_block_group(struct btrfs_root *root, |
| 2568 | struct btrfs_block_group_cache *cache) |
| 2569 | { |
| 2570 | struct rb_node *node; |
| 2571 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2572 | node = rb_next(&cache->cache_node); |
| 2573 | btrfs_put_block_group(cache); |
| 2574 | if (node) { |
| 2575 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2576 | cache_node); |
| 2577 | atomic_inc(&cache->count); |
| 2578 | } else |
| 2579 | cache = NULL; |
| 2580 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2581 | return cache; |
| 2582 | } |
| 2583 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2584 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2585 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2586 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2587 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2588 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2589 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2590 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2591 | |
| 2592 | path = btrfs_alloc_path(); |
| 2593 | if (!path) |
| 2594 | return -ENOMEM; |
| 2595 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2596 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2597 | if (last == 0) { |
| 2598 | err = btrfs_run_delayed_refs(trans, root, |
| 2599 | (unsigned long)-1); |
| 2600 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2601 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2602 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2603 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2604 | while (cache) { |
| 2605 | if (cache->dirty) |
| 2606 | break; |
| 2607 | cache = next_block_group(root, cache); |
| 2608 | } |
| 2609 | if (!cache) { |
| 2610 | if (last == 0) |
| 2611 | break; |
| 2612 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2613 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2614 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2615 | |
| 2616 | cache->dirty = 0; |
| 2617 | last = cache->key.objectid + cache->key.offset; |
| 2618 | |
| 2619 | err = write_one_cache_group(trans, root, path, cache); |
| 2620 | BUG_ON(err); |
| 2621 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2622 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2623 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2624 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2625 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2626 | } |
| 2627 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2628 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2629 | { |
| 2630 | struct btrfs_block_group_cache *block_group; |
| 2631 | int readonly = 0; |
| 2632 | |
| 2633 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2634 | if (!block_group || block_group->ro) |
| 2635 | readonly = 1; |
| 2636 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2637 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2638 | return readonly; |
| 2639 | } |
| 2640 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2641 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2642 | u64 total_bytes, u64 bytes_used, |
| 2643 | struct btrfs_space_info **space_info) |
| 2644 | { |
| 2645 | struct btrfs_space_info *found; |
| 2646 | |
| 2647 | found = __find_space_info(info, flags); |
| 2648 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2649 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2650 | found->total_bytes += total_bytes; |
| 2651 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2652 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2653 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2654 | *space_info = found; |
| 2655 | return 0; |
| 2656 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2657 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2658 | if (!found) |
| 2659 | return -ENOMEM; |
| 2660 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2661 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2662 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2663 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2664 | found->flags = flags; |
| 2665 | found->total_bytes = total_bytes; |
| 2666 | found->bytes_used = bytes_used; |
| 2667 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2668 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2669 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2670 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2671 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2672 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2673 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2674 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2675 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2676 | return 0; |
| 2677 | } |
| 2678 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2679 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2680 | { |
| 2681 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2682 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2683 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2684 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2685 | if (extra_flags) { |
| 2686 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2687 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2688 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2689 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2690 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2691 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2692 | } |
| 2693 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2694 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2695 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 2696 | { |
| 2697 | spin_lock(&cache->space_info->lock); |
| 2698 | spin_lock(&cache->lock); |
| 2699 | if (!cache->ro) { |
| 2700 | cache->space_info->bytes_readonly += cache->key.offset - |
| 2701 | btrfs_block_group_used(&cache->item); |
| 2702 | cache->ro = 1; |
| 2703 | } |
| 2704 | spin_unlock(&cache->lock); |
| 2705 | spin_unlock(&cache->space_info->lock); |
| 2706 | } |
| 2707 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2708 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2709 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2710 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2711 | |
| 2712 | if (num_devices == 1) |
| 2713 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2714 | if (num_devices < 4) |
| 2715 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2716 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2717 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2718 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2719 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2720 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2721 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2722 | |
| 2723 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2724 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2725 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2726 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2727 | |
| 2728 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2729 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2730 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2731 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2732 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2733 | return flags; |
| 2734 | } |
| 2735 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2736 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 2737 | { |
| 2738 | struct btrfs_fs_info *info = root->fs_info; |
| 2739 | u64 alloc_profile; |
| 2740 | |
| 2741 | if (data) { |
| 2742 | alloc_profile = info->avail_data_alloc_bits & |
| 2743 | info->data_alloc_profile; |
| 2744 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 2745 | } else if (root == root->fs_info->chunk_root) { |
| 2746 | alloc_profile = info->avail_system_alloc_bits & |
| 2747 | info->system_alloc_profile; |
| 2748 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 2749 | } else { |
| 2750 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2751 | info->metadata_alloc_profile; |
| 2752 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 2753 | } |
| 2754 | |
| 2755 | return btrfs_reduce_alloc_profile(root, data); |
| 2756 | } |
| 2757 | |
| 2758 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2759 | { |
| 2760 | u64 alloc_target; |
| 2761 | |
| 2762 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2763 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 2764 | alloc_target); |
| 2765 | } |
| 2766 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2767 | static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items) |
| 2768 | { |
| 2769 | u64 num_bytes; |
| 2770 | int level; |
| 2771 | |
| 2772 | level = BTRFS_MAX_LEVEL - 2; |
| 2773 | /* |
| 2774 | * NOTE: these calculations are absolutely the worst possible case. |
| 2775 | * This assumes that _every_ item we insert will require a new leaf, and |
| 2776 | * that the tree has grown to its maximum level size. |
| 2777 | */ |
| 2778 | |
| 2779 | /* |
| 2780 | * for every item we insert we could insert both an extent item and a |
| 2781 | * extent ref item. Then for ever item we insert, we will need to cow |
| 2782 | * both the original leaf, plus the leaf to the left and right of it. |
| 2783 | * |
| 2784 | * Unless we are talking about the extent root, then we just want the |
| 2785 | * number of items * 2, since we just need the extent item plus its ref. |
| 2786 | */ |
| 2787 | if (root == root->fs_info->extent_root) |
| 2788 | num_bytes = num_items * 2; |
| 2789 | else |
| 2790 | num_bytes = (num_items + (2 * num_items)) * 3; |
| 2791 | |
| 2792 | /* |
| 2793 | * num_bytes is total number of leaves we could need times the leaf |
| 2794 | * size, and then for every leaf we could end up cow'ing 2 nodes per |
| 2795 | * level, down to the leaf level. |
| 2796 | */ |
| 2797 | num_bytes = (num_bytes * root->leafsize) + |
| 2798 | (num_bytes * (level * 2)) * root->nodesize; |
| 2799 | |
| 2800 | return num_bytes; |
| 2801 | } |
| 2802 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2803 | /* |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2804 | * Unreserve metadata space for delalloc. If we have less reserved credits than |
| 2805 | * we have extents, this function does nothing. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2806 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2807 | int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root, |
| 2808 | struct inode *inode, int num_items) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2809 | { |
| 2810 | struct btrfs_fs_info *info = root->fs_info; |
| 2811 | struct btrfs_space_info *meta_sinfo; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2812 | u64 num_bytes; |
| 2813 | u64 alloc_target; |
| 2814 | bool bug = false; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2815 | |
| 2816 | /* get the space info for where the metadata will live */ |
| 2817 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 2818 | meta_sinfo = __find_space_info(info, alloc_target); |
| 2819 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2820 | num_bytes = calculate_bytes_needed(root->fs_info->extent_root, |
| 2821 | num_items); |
| 2822 | |
| 2823 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2824 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 2825 | if (BTRFS_I(inode)->reserved_extents <= |
| 2826 | BTRFS_I(inode)->outstanding_extents) { |
| 2827 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2828 | spin_unlock(&meta_sinfo->lock); |
| 2829 | return 0; |
| 2830 | } |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2831 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2832 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2833 | BTRFS_I(inode)->reserved_extents--; |
| 2834 | BUG_ON(BTRFS_I(inode)->reserved_extents < 0); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2835 | |
| 2836 | if (meta_sinfo->bytes_delalloc < num_bytes) { |
| 2837 | bug = true; |
| 2838 | meta_sinfo->bytes_delalloc = 0; |
| 2839 | } else { |
| 2840 | meta_sinfo->bytes_delalloc -= num_bytes; |
| 2841 | } |
| 2842 | spin_unlock(&meta_sinfo->lock); |
| 2843 | |
| 2844 | BUG_ON(bug); |
| 2845 | |
| 2846 | return 0; |
| 2847 | } |
| 2848 | |
| 2849 | static void check_force_delalloc(struct btrfs_space_info *meta_sinfo) |
| 2850 | { |
| 2851 | u64 thresh; |
| 2852 | |
| 2853 | thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 2854 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 2855 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 2856 | meta_sinfo->bytes_may_use; |
| 2857 | |
| 2858 | thresh = meta_sinfo->total_bytes - thresh; |
| 2859 | thresh *= 80; |
| 2860 | do_div(thresh, 100); |
| 2861 | if (thresh <= meta_sinfo->bytes_delalloc) |
| 2862 | meta_sinfo->force_delalloc = 1; |
| 2863 | else |
| 2864 | meta_sinfo->force_delalloc = 0; |
| 2865 | } |
| 2866 | |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2867 | struct async_flush { |
| 2868 | struct btrfs_root *root; |
| 2869 | struct btrfs_space_info *info; |
| 2870 | struct btrfs_work work; |
| 2871 | }; |
| 2872 | |
| 2873 | static noinline void flush_delalloc_async(struct btrfs_work *work) |
| 2874 | { |
| 2875 | struct async_flush *async; |
| 2876 | struct btrfs_root *root; |
| 2877 | struct btrfs_space_info *info; |
| 2878 | |
| 2879 | async = container_of(work, struct async_flush, work); |
| 2880 | root = async->root; |
| 2881 | info = async->info; |
| 2882 | |
| 2883 | btrfs_start_delalloc_inodes(root); |
| 2884 | wake_up(&info->flush_wait); |
| 2885 | btrfs_wait_ordered_extents(root, 0); |
| 2886 | |
| 2887 | spin_lock(&info->lock); |
| 2888 | info->flushing = 0; |
| 2889 | spin_unlock(&info->lock); |
| 2890 | wake_up(&info->flush_wait); |
| 2891 | |
| 2892 | kfree(async); |
| 2893 | } |
| 2894 | |
| 2895 | static void wait_on_flush(struct btrfs_space_info *info) |
| 2896 | { |
| 2897 | DEFINE_WAIT(wait); |
| 2898 | u64 used; |
| 2899 | |
| 2900 | while (1) { |
| 2901 | prepare_to_wait(&info->flush_wait, &wait, |
| 2902 | TASK_UNINTERRUPTIBLE); |
| 2903 | spin_lock(&info->lock); |
| 2904 | if (!info->flushing) { |
| 2905 | spin_unlock(&info->lock); |
| 2906 | break; |
| 2907 | } |
| 2908 | |
| 2909 | used = info->bytes_used + info->bytes_reserved + |
| 2910 | info->bytes_pinned + info->bytes_readonly + |
| 2911 | info->bytes_super + info->bytes_root + |
| 2912 | info->bytes_may_use + info->bytes_delalloc; |
| 2913 | if (used < info->total_bytes) { |
| 2914 | spin_unlock(&info->lock); |
| 2915 | break; |
| 2916 | } |
| 2917 | spin_unlock(&info->lock); |
| 2918 | schedule(); |
| 2919 | } |
| 2920 | finish_wait(&info->flush_wait, &wait); |
| 2921 | } |
| 2922 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2923 | static void flush_delalloc(struct btrfs_root *root, |
| 2924 | struct btrfs_space_info *info) |
| 2925 | { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2926 | struct async_flush *async; |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2927 | bool wait = false; |
| 2928 | |
| 2929 | spin_lock(&info->lock); |
| 2930 | |
| 2931 | if (!info->flushing) { |
| 2932 | info->flushing = 1; |
| 2933 | init_waitqueue_head(&info->flush_wait); |
| 2934 | } else { |
| 2935 | wait = true; |
| 2936 | } |
| 2937 | |
| 2938 | spin_unlock(&info->lock); |
| 2939 | |
| 2940 | if (wait) { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2941 | wait_on_flush(info); |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2942 | return; |
| 2943 | } |
| 2944 | |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2945 | async = kzalloc(sizeof(*async), GFP_NOFS); |
| 2946 | if (!async) |
| 2947 | goto flush; |
| 2948 | |
| 2949 | async->root = root; |
| 2950 | async->info = info; |
| 2951 | async->work.func = flush_delalloc_async; |
| 2952 | |
| 2953 | btrfs_queue_worker(&root->fs_info->enospc_workers, |
| 2954 | &async->work); |
| 2955 | wait_on_flush(info); |
| 2956 | return; |
| 2957 | |
| 2958 | flush: |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 2959 | btrfs_start_delalloc_inodes(root); |
| 2960 | btrfs_wait_ordered_extents(root, 0); |
| 2961 | |
| 2962 | spin_lock(&info->lock); |
| 2963 | info->flushing = 0; |
| 2964 | spin_unlock(&info->lock); |
| 2965 | wake_up(&info->flush_wait); |
| 2966 | } |
| 2967 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2968 | static int maybe_allocate_chunk(struct btrfs_root *root, |
| 2969 | struct btrfs_space_info *info) |
| 2970 | { |
| 2971 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
| 2972 | struct btrfs_trans_handle *trans; |
| 2973 | bool wait = false; |
| 2974 | int ret = 0; |
| 2975 | u64 min_metadata; |
| 2976 | u64 free_space; |
| 2977 | |
| 2978 | free_space = btrfs_super_total_bytes(disk_super); |
| 2979 | /* |
Chris Mason | 33b2580 | 2009-11-11 10:16:57 -0500 | [diff] [blame] | 2980 | * we allow the metadata to grow to a max of either 10gb or 5% of the |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2981 | * space in the volume. |
| 2982 | */ |
Chris Mason | 33b2580 | 2009-11-11 10:16:57 -0500 | [diff] [blame] | 2983 | min_metadata = min((u64)10 * 1024 * 1024 * 1024, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2984 | div64_u64(free_space * 5, 100)); |
| 2985 | if (info->total_bytes >= min_metadata) { |
| 2986 | spin_unlock(&info->lock); |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | if (info->full) { |
| 2991 | spin_unlock(&info->lock); |
| 2992 | return 0; |
| 2993 | } |
| 2994 | |
| 2995 | if (!info->allocating_chunk) { |
| 2996 | info->force_alloc = 1; |
| 2997 | info->allocating_chunk = 1; |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 2998 | init_waitqueue_head(&info->allocate_wait); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2999 | } else { |
| 3000 | wait = true; |
| 3001 | } |
| 3002 | |
| 3003 | spin_unlock(&info->lock); |
| 3004 | |
| 3005 | if (wait) { |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3006 | wait_event(info->allocate_wait, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3007 | !info->allocating_chunk); |
| 3008 | return 1; |
| 3009 | } |
| 3010 | |
| 3011 | trans = btrfs_start_transaction(root, 1); |
| 3012 | if (!trans) { |
| 3013 | ret = -ENOMEM; |
| 3014 | goto out; |
| 3015 | } |
| 3016 | |
| 3017 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3018 | 4096 + 2 * 1024 * 1024, |
| 3019 | info->flags, 0); |
| 3020 | btrfs_end_transaction(trans, root); |
| 3021 | if (ret) |
| 3022 | goto out; |
| 3023 | out: |
| 3024 | spin_lock(&info->lock); |
| 3025 | info->allocating_chunk = 0; |
| 3026 | spin_unlock(&info->lock); |
Josef Bacik | e3ccfa9 | 2009-10-07 20:44:34 -0400 | [diff] [blame] | 3027 | wake_up(&info->allocate_wait); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3028 | |
| 3029 | if (ret) |
| 3030 | return 0; |
| 3031 | return 1; |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Reserve metadata space for delalloc. |
| 3036 | */ |
| 3037 | int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root, |
| 3038 | struct inode *inode, int num_items) |
| 3039 | { |
| 3040 | struct btrfs_fs_info *info = root->fs_info; |
| 3041 | struct btrfs_space_info *meta_sinfo; |
| 3042 | u64 num_bytes; |
| 3043 | u64 used; |
| 3044 | u64 alloc_target; |
| 3045 | int flushed = 0; |
| 3046 | int force_delalloc; |
| 3047 | |
| 3048 | /* get the space info for where the metadata will live */ |
| 3049 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3050 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3051 | |
| 3052 | num_bytes = calculate_bytes_needed(root->fs_info->extent_root, |
| 3053 | num_items); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3054 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3055 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3056 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3057 | force_delalloc = meta_sinfo->force_delalloc; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3058 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3059 | if (unlikely(!meta_sinfo->bytes_root)) |
| 3060 | meta_sinfo->bytes_root = calculate_bytes_needed(root, 6); |
| 3061 | |
| 3062 | if (!flushed) |
| 3063 | meta_sinfo->bytes_delalloc += num_bytes; |
| 3064 | |
| 3065 | used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 3066 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 3067 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 3068 | meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc; |
| 3069 | |
| 3070 | if (used > meta_sinfo->total_bytes) { |
| 3071 | flushed++; |
| 3072 | |
| 3073 | if (flushed == 1) { |
| 3074 | if (maybe_allocate_chunk(root, meta_sinfo)) |
| 3075 | goto again; |
| 3076 | flushed++; |
| 3077 | } else { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3078 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3079 | } |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3080 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3081 | if (flushed == 2) { |
| 3082 | filemap_flush(inode->i_mapping); |
| 3083 | goto again; |
| 3084 | } else if (flushed == 3) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3085 | flush_delalloc(root, meta_sinfo); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3086 | goto again; |
| 3087 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3088 | spin_lock(&meta_sinfo->lock); |
| 3089 | meta_sinfo->bytes_delalloc -= num_bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3090 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3091 | printk(KERN_ERR "enospc, has %d, reserved %d\n", |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3092 | BTRFS_I(inode)->outstanding_extents, |
| 3093 | BTRFS_I(inode)->reserved_extents); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3094 | dump_space_info(meta_sinfo, 0, 0); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3095 | return -ENOSPC; |
| 3096 | } |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3097 | |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3098 | BTRFS_I(inode)->reserved_extents++; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3099 | check_force_delalloc(meta_sinfo); |
| 3100 | spin_unlock(&meta_sinfo->lock); |
| 3101 | |
| 3102 | if (!flushed && force_delalloc) |
| 3103 | filemap_flush(inode->i_mapping); |
| 3104 | |
| 3105 | return 0; |
| 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * unreserve num_items number of items worth of metadata space. This needs to |
| 3110 | * be paired with btrfs_reserve_metadata_space. |
| 3111 | * |
| 3112 | * NOTE: if you have the option, run this _AFTER_ you do a |
| 3113 | * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref |
| 3114 | * oprations which will result in more used metadata, so we want to make sure we |
| 3115 | * can do that without issue. |
| 3116 | */ |
| 3117 | int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items) |
| 3118 | { |
| 3119 | struct btrfs_fs_info *info = root->fs_info; |
| 3120 | struct btrfs_space_info *meta_sinfo; |
| 3121 | u64 num_bytes; |
| 3122 | u64 alloc_target; |
| 3123 | bool bug = false; |
| 3124 | |
| 3125 | /* get the space info for where the metadata will live */ |
| 3126 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3127 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3128 | |
| 3129 | num_bytes = calculate_bytes_needed(root, num_items); |
| 3130 | |
| 3131 | spin_lock(&meta_sinfo->lock); |
| 3132 | if (meta_sinfo->bytes_may_use < num_bytes) { |
| 3133 | bug = true; |
| 3134 | meta_sinfo->bytes_may_use = 0; |
| 3135 | } else { |
| 3136 | meta_sinfo->bytes_may_use -= num_bytes; |
| 3137 | } |
| 3138 | spin_unlock(&meta_sinfo->lock); |
| 3139 | |
| 3140 | BUG_ON(bug); |
| 3141 | |
| 3142 | return 0; |
| 3143 | } |
| 3144 | |
| 3145 | /* |
| 3146 | * Reserve some metadata space for use. We'll calculate the worste case number |
| 3147 | * of bytes that would be needed to modify num_items number of items. If we |
| 3148 | * have space, fantastic, if not, you get -ENOSPC. Please call |
| 3149 | * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of |
| 3150 | * items you reserved, since whatever metadata you needed should have already |
| 3151 | * been allocated. |
| 3152 | * |
| 3153 | * This will commit the transaction to make more space if we don't have enough |
| 3154 | * metadata space. THe only time we don't do this is if we're reserving space |
| 3155 | * inside of a transaction, then we will just return -ENOSPC and it is the |
| 3156 | * callers responsibility to handle it properly. |
| 3157 | */ |
| 3158 | int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items) |
| 3159 | { |
| 3160 | struct btrfs_fs_info *info = root->fs_info; |
| 3161 | struct btrfs_space_info *meta_sinfo; |
| 3162 | u64 num_bytes; |
| 3163 | u64 used; |
| 3164 | u64 alloc_target; |
| 3165 | int retries = 0; |
| 3166 | |
| 3167 | /* get the space info for where the metadata will live */ |
| 3168 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3169 | meta_sinfo = __find_space_info(info, alloc_target); |
| 3170 | |
| 3171 | num_bytes = calculate_bytes_needed(root, num_items); |
| 3172 | again: |
| 3173 | spin_lock(&meta_sinfo->lock); |
| 3174 | |
| 3175 | if (unlikely(!meta_sinfo->bytes_root)) |
| 3176 | meta_sinfo->bytes_root = calculate_bytes_needed(root, 6); |
| 3177 | |
| 3178 | if (!retries) |
| 3179 | meta_sinfo->bytes_may_use += num_bytes; |
| 3180 | |
| 3181 | used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 3182 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly + |
| 3183 | meta_sinfo->bytes_super + meta_sinfo->bytes_root + |
| 3184 | meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc; |
| 3185 | |
| 3186 | if (used > meta_sinfo->total_bytes) { |
| 3187 | retries++; |
| 3188 | if (retries == 1) { |
| 3189 | if (maybe_allocate_chunk(root, meta_sinfo)) |
| 3190 | goto again; |
| 3191 | retries++; |
| 3192 | } else { |
| 3193 | spin_unlock(&meta_sinfo->lock); |
| 3194 | } |
| 3195 | |
| 3196 | if (retries == 2) { |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 3197 | flush_delalloc(root, meta_sinfo); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3198 | goto again; |
| 3199 | } |
| 3200 | spin_lock(&meta_sinfo->lock); |
| 3201 | meta_sinfo->bytes_may_use -= num_bytes; |
| 3202 | spin_unlock(&meta_sinfo->lock); |
| 3203 | |
| 3204 | dump_space_info(meta_sinfo, 0, 0); |
| 3205 | return -ENOSPC; |
| 3206 | } |
| 3207 | |
| 3208 | check_force_delalloc(meta_sinfo); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3209 | spin_unlock(&meta_sinfo->lock); |
| 3210 | |
| 3211 | return 0; |
| 3212 | } |
| 3213 | |
| 3214 | /* |
| 3215 | * This will check the space that the inode allocates from to make sure we have |
| 3216 | * enough space for bytes. |
| 3217 | */ |
| 3218 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 3219 | u64 bytes) |
| 3220 | { |
| 3221 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3222 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3223 | |
| 3224 | /* make sure bytes are sectorsize aligned */ |
| 3225 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3226 | |
| 3227 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3228 | if (!data_sinfo) |
| 3229 | goto alloc; |
| 3230 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3231 | again: |
| 3232 | /* make sure we have enough space to handle the data first */ |
| 3233 | spin_lock(&data_sinfo->lock); |
| 3234 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 3235 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 3236 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 3237 | data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3238 | struct btrfs_trans_handle *trans; |
| 3239 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3240 | /* |
| 3241 | * if we don't have enough free bytes in this space then we need |
| 3242 | * to alloc a new chunk. |
| 3243 | */ |
| 3244 | if (!data_sinfo->full) { |
| 3245 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3246 | |
| 3247 | data_sinfo->force_alloc = 1; |
| 3248 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3249 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3250 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 3251 | trans = btrfs_start_transaction(root, 1); |
| 3252 | if (!trans) |
| 3253 | return -ENOMEM; |
| 3254 | |
| 3255 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3256 | bytes + 2 * 1024 * 1024, |
| 3257 | alloc_target, 0); |
| 3258 | btrfs_end_transaction(trans, root); |
| 3259 | if (ret) |
| 3260 | return ret; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3261 | |
| 3262 | if (!data_sinfo) { |
| 3263 | btrfs_set_inode_space_info(root, inode); |
| 3264 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3265 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3266 | goto again; |
| 3267 | } |
| 3268 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3269 | |
| 3270 | /* commit the current transaction and try again */ |
Sage Weil | dd7e0b7 | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3271 | if (!committed && !root->fs_info->open_ioctl_trans) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3272 | committed = 1; |
| 3273 | trans = btrfs_join_transaction(root, 1); |
| 3274 | if (!trans) |
| 3275 | return -ENOMEM; |
| 3276 | ret = btrfs_commit_transaction(trans, root); |
| 3277 | if (ret) |
| 3278 | return ret; |
| 3279 | goto again; |
| 3280 | } |
| 3281 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3282 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 3283 | ", %llu bytes_used, %llu bytes_reserved, " |
Hu Tao | 68f5a38 | 2009-07-02 13:55:45 -0400 | [diff] [blame] | 3284 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use " |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 3285 | "%llu total\n", (unsigned long long)bytes, |
| 3286 | (unsigned long long)data_sinfo->bytes_delalloc, |
| 3287 | (unsigned long long)data_sinfo->bytes_used, |
| 3288 | (unsigned long long)data_sinfo->bytes_reserved, |
| 3289 | (unsigned long long)data_sinfo->bytes_pinned, |
| 3290 | (unsigned long long)data_sinfo->bytes_readonly, |
| 3291 | (unsigned long long)data_sinfo->bytes_may_use, |
| 3292 | (unsigned long long)data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3293 | return -ENOSPC; |
| 3294 | } |
| 3295 | data_sinfo->bytes_may_use += bytes; |
| 3296 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3297 | spin_unlock(&data_sinfo->lock); |
| 3298 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3299 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | /* |
| 3303 | * if there was an error for whatever reason after calling |
| 3304 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 3305 | */ |
| 3306 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 3307 | struct inode *inode, u64 bytes) |
| 3308 | { |
| 3309 | struct btrfs_space_info *data_sinfo; |
| 3310 | |
| 3311 | /* make sure bytes are sectorsize aligned */ |
| 3312 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3313 | |
| 3314 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3315 | spin_lock(&data_sinfo->lock); |
| 3316 | data_sinfo->bytes_may_use -= bytes; |
| 3317 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3318 | spin_unlock(&data_sinfo->lock); |
| 3319 | } |
| 3320 | |
| 3321 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 3322 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 3323 | u64 bytes) |
| 3324 | { |
| 3325 | struct btrfs_space_info *data_sinfo; |
| 3326 | |
| 3327 | /* get the space info for where this inode will be storing its data */ |
| 3328 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3329 | |
| 3330 | /* make sure we have enough space to handle the data first */ |
| 3331 | spin_lock(&data_sinfo->lock); |
| 3332 | data_sinfo->bytes_delalloc += bytes; |
| 3333 | |
| 3334 | /* |
| 3335 | * we are adding a delalloc extent without calling |
| 3336 | * btrfs_check_data_free_space first. This happens on a weird |
| 3337 | * writepage condition, but shouldn't hurt our accounting |
| 3338 | */ |
| 3339 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 3340 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 3341 | BTRFS_I(inode)->reserved_bytes = 0; |
| 3342 | } else { |
| 3343 | data_sinfo->bytes_may_use -= bytes; |
| 3344 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3345 | } |
| 3346 | |
| 3347 | spin_unlock(&data_sinfo->lock); |
| 3348 | } |
| 3349 | |
| 3350 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 3351 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 3352 | u64 bytes) |
| 3353 | { |
| 3354 | struct btrfs_space_info *info; |
| 3355 | |
| 3356 | info = BTRFS_I(inode)->space_info; |
| 3357 | |
| 3358 | spin_lock(&info->lock); |
| 3359 | info->bytes_delalloc -= bytes; |
| 3360 | spin_unlock(&info->lock); |
| 3361 | } |
| 3362 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3363 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3364 | { |
| 3365 | struct list_head *head = &info->space_info; |
| 3366 | struct btrfs_space_info *found; |
| 3367 | |
| 3368 | rcu_read_lock(); |
| 3369 | list_for_each_entry_rcu(found, head, list) { |
| 3370 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3371 | found->force_alloc = 1; |
| 3372 | } |
| 3373 | rcu_read_unlock(); |
| 3374 | } |
| 3375 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3376 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3377 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3378 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3379 | { |
| 3380 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3381 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3382 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3383 | int ret = 0; |
| 3384 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3385 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3386 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3387 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3388 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3389 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3390 | if (!space_info) { |
| 3391 | ret = update_space_info(extent_root->fs_info, flags, |
| 3392 | 0, 0, &space_info); |
| 3393 | BUG_ON(ret); |
| 3394 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3395 | BUG_ON(!space_info); |
| 3396 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3397 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3398 | if (space_info->force_alloc) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3399 | force = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3400 | if (space_info->full) { |
| 3401 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3402 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3403 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3404 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3405 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3406 | thresh = div_factor(thresh, 8); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3407 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3408 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3409 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 3410 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3411 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3412 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3413 | spin_unlock(&space_info->lock); |
| 3414 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3415 | /* |
| 3416 | * if we're doing a data chunk, go ahead and make sure that |
| 3417 | * we keep a reasonable number of metadata chunks allocated in the |
| 3418 | * FS as well. |
| 3419 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3420 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3421 | fs_info->data_chunk_allocations++; |
| 3422 | if (!(fs_info->data_chunk_allocations % |
| 3423 | fs_info->metadata_ratio)) |
| 3424 | force_metadata_allocation(fs_info); |
| 3425 | } |
| 3426 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3427 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3428 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3429 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3430 | space_info->full = 1; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3431 | space_info->force_alloc = 0; |
| 3432 | spin_unlock(&space_info->lock); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3433 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3434 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3435 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3436 | } |
| 3437 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3438 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 3439 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3440 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3441 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3442 | { |
| 3443 | struct btrfs_block_group_cache *cache; |
| 3444 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3445 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3446 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3447 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3448 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3449 | /* block accounting for super block */ |
| 3450 | spin_lock(&info->delalloc_lock); |
| 3451 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 3452 | if (alloc) |
| 3453 | old_val += num_bytes; |
| 3454 | else |
| 3455 | old_val -= num_bytes; |
| 3456 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
| 3457 | |
| 3458 | /* block accounting for root item */ |
| 3459 | old_val = btrfs_root_used(&root->root_item); |
| 3460 | if (alloc) |
| 3461 | old_val += num_bytes; |
| 3462 | else |
| 3463 | old_val -= num_bytes; |
| 3464 | btrfs_set_root_used(&root->root_item, old_val); |
| 3465 | spin_unlock(&info->delalloc_lock); |
| 3466 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3467 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3468 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3469 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3470 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3471 | byte_in_group = bytenr - cache->key.objectid; |
| 3472 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3473 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3474 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3475 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3476 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3477 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3478 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3479 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3480 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3481 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3482 | cache->reserved -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3483 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3484 | cache->space_info->bytes_reserved -= num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 3485 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3486 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3487 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3488 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3489 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3490 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3491 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3492 | if (cache->ro) |
| 3493 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3494 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3495 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3496 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3497 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3498 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3499 | |
| 3500 | ret = btrfs_discard_extent(root, bytenr, |
| 3501 | num_bytes); |
| 3502 | WARN_ON(ret); |
| 3503 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3504 | ret = btrfs_add_free_space(cache, bytenr, |
| 3505 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3506 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3507 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3508 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3509 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3510 | total -= num_bytes; |
| 3511 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3512 | } |
| 3513 | return 0; |
| 3514 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3515 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3516 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 3517 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3518 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3519 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3520 | |
| 3521 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 3522 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3523 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3524 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3525 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3526 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3527 | |
| 3528 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3529 | } |
| 3530 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3531 | /* |
| 3532 | * this function must be called within transaction |
| 3533 | */ |
| 3534 | int btrfs_pin_extent(struct btrfs_root *root, |
| 3535 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3536 | { |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3537 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3538 | struct btrfs_block_group_cache *cache; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3539 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3540 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 3541 | BUG_ON(!cache); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3542 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3543 | spin_lock(&cache->space_info->lock); |
| 3544 | spin_lock(&cache->lock); |
| 3545 | cache->pinned += num_bytes; |
| 3546 | cache->space_info->bytes_pinned += num_bytes; |
| 3547 | if (reserved) { |
| 3548 | cache->reserved -= num_bytes; |
| 3549 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3550 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3551 | spin_unlock(&cache->lock); |
| 3552 | spin_unlock(&cache->space_info->lock); |
| 3553 | |
| 3554 | btrfs_put_block_group(cache); |
| 3555 | |
| 3556 | set_extent_dirty(fs_info->pinned_extents, |
| 3557 | bytenr, bytenr + num_bytes - 1, GFP_NOFS); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3558 | return 0; |
| 3559 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3560 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3561 | static int update_reserved_extents(struct btrfs_block_group_cache *cache, |
| 3562 | u64 num_bytes, int reserve) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3563 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3564 | spin_lock(&cache->space_info->lock); |
| 3565 | spin_lock(&cache->lock); |
| 3566 | if (reserve) { |
| 3567 | cache->reserved += num_bytes; |
| 3568 | cache->space_info->bytes_reserved += num_bytes; |
| 3569 | } else { |
| 3570 | cache->reserved -= num_bytes; |
| 3571 | cache->space_info->bytes_reserved -= num_bytes; |
| 3572 | } |
| 3573 | spin_unlock(&cache->lock); |
| 3574 | spin_unlock(&cache->space_info->lock); |
| 3575 | return 0; |
| 3576 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3577 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3578 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 3579 | struct btrfs_root *root) |
| 3580 | { |
| 3581 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3582 | struct btrfs_caching_control *next; |
| 3583 | struct btrfs_caching_control *caching_ctl; |
| 3584 | struct btrfs_block_group_cache *cache; |
| 3585 | |
| 3586 | down_write(&fs_info->extent_commit_sem); |
| 3587 | |
| 3588 | list_for_each_entry_safe(caching_ctl, next, |
| 3589 | &fs_info->caching_block_groups, list) { |
| 3590 | cache = caching_ctl->block_group; |
| 3591 | if (block_group_cache_done(cache)) { |
| 3592 | cache->last_byte_to_unpin = (u64)-1; |
| 3593 | list_del_init(&caching_ctl->list); |
| 3594 | put_caching_control(caching_ctl); |
| 3595 | } else { |
| 3596 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3601 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 3602 | else |
| 3603 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 3604 | |
| 3605 | up_write(&fs_info->extent_commit_sem); |
| 3606 | return 0; |
| 3607 | } |
| 3608 | |
| 3609 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 3610 | { |
| 3611 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3612 | struct btrfs_block_group_cache *cache = NULL; |
| 3613 | u64 len; |
| 3614 | |
| 3615 | while (start <= end) { |
| 3616 | if (!cache || |
| 3617 | start >= cache->key.objectid + cache->key.offset) { |
| 3618 | if (cache) |
| 3619 | btrfs_put_block_group(cache); |
| 3620 | cache = btrfs_lookup_block_group(fs_info, start); |
| 3621 | BUG_ON(!cache); |
| 3622 | } |
| 3623 | |
| 3624 | len = cache->key.objectid + cache->key.offset - start; |
| 3625 | len = min(len, end + 1 - start); |
| 3626 | |
| 3627 | if (start < cache->last_byte_to_unpin) { |
| 3628 | len = min(len, cache->last_byte_to_unpin - start); |
| 3629 | btrfs_add_free_space(cache, start, len); |
| 3630 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3631 | |
| 3632 | spin_lock(&cache->space_info->lock); |
| 3633 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3634 | cache->pinned -= len; |
| 3635 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3636 | spin_unlock(&cache->lock); |
| 3637 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3638 | |
| 3639 | start += len; |
| 3640 | } |
| 3641 | |
| 3642 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3643 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3644 | return 0; |
| 3645 | } |
| 3646 | |
| 3647 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3648 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3649 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3650 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3651 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3652 | u64 start; |
| 3653 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3654 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3655 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3656 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3657 | unpin = &fs_info->freed_extents[1]; |
| 3658 | else |
| 3659 | unpin = &fs_info->freed_extents[0]; |
| 3660 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3661 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3662 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 3663 | EXTENT_DIRTY); |
| 3664 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3665 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3666 | |
| 3667 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 3668 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3669 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3670 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3671 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3672 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3673 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3674 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3675 | } |
| 3676 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3677 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 3678 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3679 | struct btrfs_path *path, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3680 | u64 bytenr, u64 num_bytes, |
| 3681 | int is_data, int reserved, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3682 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3683 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3684 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3685 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3686 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3687 | if (is_data) |
| 3688 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3689 | |
Chris Mason | 444528b | 2009-10-14 09:38:28 -0400 | [diff] [blame] | 3690 | /* |
| 3691 | * discard is sloooow, and so triggering discards on |
| 3692 | * individual btree blocks isn't a good plan. Just |
| 3693 | * pin everything in discard mode. |
| 3694 | */ |
| 3695 | if (btrfs_test_opt(root, DISCARD)) |
| 3696 | goto pinit; |
| 3697 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3698 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 3699 | if (!buf) |
| 3700 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3701 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3702 | /* we can reuse a block if it hasn't been written |
| 3703 | * and it is from this transaction. We can't |
| 3704 | * reuse anything from the tree log root because |
| 3705 | * it has tiny sub-transactions. |
| 3706 | */ |
| 3707 | if (btrfs_buffer_uptodate(buf, 0) && |
| 3708 | btrfs_try_tree_lock(buf)) { |
| 3709 | u64 header_owner = btrfs_header_owner(buf); |
| 3710 | u64 header_transid = btrfs_header_generation(buf); |
| 3711 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
| 3712 | header_transid == trans->transid && |
| 3713 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3714 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3715 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 3716 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3717 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 3718 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3719 | free_extent_buffer(buf); |
| 3720 | pinit: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3721 | if (path) |
| 3722 | btrfs_set_path_blocking(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3723 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3724 | btrfs_pin_extent(root, bytenr, num_bytes, reserved); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3725 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3726 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3727 | return 0; |
| 3728 | } |
| 3729 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3730 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 3731 | struct btrfs_root *root, |
| 3732 | u64 bytenr, u64 num_bytes, u64 parent, |
| 3733 | u64 root_objectid, u64 owner_objectid, |
| 3734 | u64 owner_offset, int refs_to_drop, |
| 3735 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3736 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3737 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3738 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3739 | struct btrfs_fs_info *info = root->fs_info; |
| 3740 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3741 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3742 | struct btrfs_extent_item *ei; |
| 3743 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3744 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3745 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3746 | int extent_slot = 0; |
| 3747 | int found_extent = 0; |
| 3748 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3749 | u32 item_size; |
| 3750 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 3751 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3752 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3753 | if (!path) |
| 3754 | return -ENOMEM; |
| 3755 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 3756 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3757 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3758 | |
| 3759 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 3760 | BUG_ON(!is_data && refs_to_drop != 1); |
| 3761 | |
| 3762 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 3763 | bytenr, num_bytes, parent, |
| 3764 | root_objectid, owner_objectid, |
| 3765 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3766 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3767 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3768 | while (extent_slot >= 0) { |
| 3769 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3770 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3771 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3772 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3773 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3774 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3775 | found_extent = 1; |
| 3776 | break; |
| 3777 | } |
| 3778 | if (path->slots[0] - extent_slot > 5) |
| 3779 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3780 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3781 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3782 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3783 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 3784 | if (found_extent && item_size < sizeof(*ei)) |
| 3785 | found_extent = 0; |
| 3786 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3787 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3788 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3789 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3790 | NULL, refs_to_drop, |
| 3791 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3792 | BUG_ON(ret); |
| 3793 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3794 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3795 | |
| 3796 | key.objectid = bytenr; |
| 3797 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3798 | key.offset = num_bytes; |
| 3799 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3800 | ret = btrfs_search_slot(trans, extent_root, |
| 3801 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3802 | if (ret) { |
| 3803 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3804 | ", was looking for %llu\n", ret, |
| 3805 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3806 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3807 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3808 | BUG_ON(ret); |
| 3809 | extent_slot = path->slots[0]; |
| 3810 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3811 | } else { |
| 3812 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3813 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3814 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3815 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3816 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3817 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3818 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3819 | (unsigned long long)owner_objectid, |
| 3820 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3821 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3822 | |
| 3823 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3824 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3825 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3826 | if (item_size < sizeof(*ei)) { |
| 3827 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 3828 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 3829 | owner_objectid, 0); |
| 3830 | BUG_ON(ret < 0); |
| 3831 | |
| 3832 | btrfs_release_path(extent_root, path); |
| 3833 | path->leave_spinning = 1; |
| 3834 | |
| 3835 | key.objectid = bytenr; |
| 3836 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3837 | key.offset = num_bytes; |
| 3838 | |
| 3839 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 3840 | -1, 1); |
| 3841 | if (ret) { |
| 3842 | printk(KERN_ERR "umm, got %d back from search" |
| 3843 | ", was looking for %llu\n", ret, |
| 3844 | (unsigned long long)bytenr); |
| 3845 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3846 | } |
| 3847 | BUG_ON(ret); |
| 3848 | extent_slot = path->slots[0]; |
| 3849 | leaf = path->nodes[0]; |
| 3850 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3851 | } |
| 3852 | #endif |
| 3853 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3854 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3855 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3856 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3857 | struct btrfs_tree_block_info *bi; |
| 3858 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3859 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3860 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3861 | } |
| 3862 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3863 | refs = btrfs_extent_refs(leaf, ei); |
| 3864 | BUG_ON(refs < refs_to_drop); |
| 3865 | refs -= refs_to_drop; |
| 3866 | |
| 3867 | if (refs > 0) { |
| 3868 | if (extent_op) |
| 3869 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3870 | /* |
| 3871 | * In the case of inline back ref, reference count will |
| 3872 | * be updated by remove_extent_backref |
| 3873 | */ |
| 3874 | if (iref) { |
| 3875 | BUG_ON(!found_extent); |
| 3876 | } else { |
| 3877 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3878 | btrfs_mark_buffer_dirty(leaf); |
| 3879 | } |
| 3880 | if (found_extent) { |
| 3881 | ret = remove_extent_backref(trans, extent_root, path, |
| 3882 | iref, refs_to_drop, |
| 3883 | is_data); |
| 3884 | BUG_ON(ret); |
| 3885 | } |
| 3886 | } else { |
| 3887 | int mark_free = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3888 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3889 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3890 | if (found_extent) { |
| 3891 | BUG_ON(is_data && refs_to_drop != |
| 3892 | extent_data_ref_count(root, path, iref)); |
| 3893 | if (iref) { |
| 3894 | BUG_ON(path->slots[0] != extent_slot); |
| 3895 | } else { |
| 3896 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3897 | path->slots[0] = extent_slot; |
| 3898 | num_to_del = 2; |
| 3899 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3900 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3901 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3902 | ret = pin_down_bytes(trans, root, path, bytenr, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3903 | num_bytes, is_data, 0, &must_clean); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3904 | if (ret > 0) |
| 3905 | mark_free = 1; |
| 3906 | BUG_ON(ret < 0); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3907 | /* |
| 3908 | * it is going to be very rare for someone to be waiting |
| 3909 | * on the block we're freeing. del_items might need to |
| 3910 | * schedule, so rather than get fancy, just force it |
| 3911 | * to blocking here |
| 3912 | */ |
| 3913 | if (must_clean) |
| 3914 | btrfs_set_lock_blocking(must_clean); |
| 3915 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3916 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3917 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3918 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3919 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3920 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3921 | if (must_clean) { |
| 3922 | clean_tree_block(NULL, root, must_clean); |
| 3923 | btrfs_tree_unlock(must_clean); |
| 3924 | free_extent_buffer(must_clean); |
| 3925 | } |
| 3926 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3927 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3928 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 3929 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 3930 | } else { |
| 3931 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 3932 | bytenr >> PAGE_CACHE_SHIFT, |
| 3933 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3934 | } |
| 3935 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 3936 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 3937 | mark_free); |
| 3938 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3939 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3940 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3941 | return ret; |
| 3942 | } |
| 3943 | |
| 3944 | /* |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3945 | * when we free an extent, it is possible (and likely) that we free the last |
| 3946 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3947 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3948 | * removes it from the tree. |
| 3949 | */ |
| 3950 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 3951 | struct btrfs_root *root, u64 bytenr) |
| 3952 | { |
| 3953 | struct btrfs_delayed_ref_head *head; |
| 3954 | struct btrfs_delayed_ref_root *delayed_refs; |
| 3955 | struct btrfs_delayed_ref_node *ref; |
| 3956 | struct rb_node *node; |
| 3957 | int ret; |
| 3958 | |
| 3959 | delayed_refs = &trans->transaction->delayed_refs; |
| 3960 | spin_lock(&delayed_refs->lock); |
| 3961 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 3962 | if (!head) |
| 3963 | goto out; |
| 3964 | |
| 3965 | node = rb_prev(&head->node.rb_node); |
| 3966 | if (!node) |
| 3967 | goto out; |
| 3968 | |
| 3969 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 3970 | |
| 3971 | /* there are still entries for this ref, we can't drop it */ |
| 3972 | if (ref->bytenr == bytenr) |
| 3973 | goto out; |
| 3974 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3975 | if (head->extent_op) { |
| 3976 | if (!head->must_insert_reserved) |
| 3977 | goto out; |
| 3978 | kfree(head->extent_op); |
| 3979 | head->extent_op = NULL; |
| 3980 | } |
| 3981 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3982 | /* |
| 3983 | * waiting for the lock here would deadlock. If someone else has it |
| 3984 | * locked they are already in the process of dropping it anyway |
| 3985 | */ |
| 3986 | if (!mutex_trylock(&head->mutex)) |
| 3987 | goto out; |
| 3988 | |
| 3989 | /* |
| 3990 | * at this point we have a head with no other entries. Go |
| 3991 | * ahead and process it. |
| 3992 | */ |
| 3993 | head->node.in_tree = 0; |
| 3994 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3995 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3996 | delayed_refs->num_entries--; |
| 3997 | |
| 3998 | /* |
| 3999 | * we don't take a ref on the node because we're removing it from the |
| 4000 | * tree, so we just steal the ref the tree was holding. |
| 4001 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4002 | delayed_refs->num_heads--; |
| 4003 | if (list_empty(&head->cluster)) |
| 4004 | delayed_refs->num_heads_ready--; |
| 4005 | |
| 4006 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4007 | spin_unlock(&delayed_refs->lock); |
| 4008 | |
| 4009 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4010 | &head->node, head->extent_op, |
| 4011 | head->must_insert_reserved); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4012 | BUG_ON(ret); |
| 4013 | btrfs_put_delayed_ref(&head->node); |
| 4014 | return 0; |
| 4015 | out: |
| 4016 | spin_unlock(&delayed_refs->lock); |
| 4017 | return 0; |
| 4018 | } |
| 4019 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4020 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4021 | struct btrfs_root *root, |
| 4022 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4023 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4024 | { |
| 4025 | int ret; |
| 4026 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4027 | /* |
| 4028 | * tree log blocks never actually go into the extent allocation |
| 4029 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4030 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4031 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4032 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4033 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4034 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4035 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4036 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4037 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4038 | parent, root_objectid, (int)owner, |
| 4039 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4040 | BUG_ON(ret); |
| 4041 | ret = check_ref_cleanup(trans, root, bytenr); |
| 4042 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4043 | } else { |
| 4044 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4045 | parent, root_objectid, owner, |
| 4046 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4047 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4048 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4049 | return ret; |
| 4050 | } |
| 4051 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4052 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4053 | { |
| 4054 | u64 mask = ((u64)root->stripesize - 1); |
| 4055 | u64 ret = (val + mask) & ~mask; |
| 4056 | return ret; |
| 4057 | } |
| 4058 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4059 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4060 | * when we wait for progress in the block group caching, its because |
| 4061 | * our allocation attempt failed at least once. So, we must sleep |
| 4062 | * and let some progress happen before we try again. |
| 4063 | * |
| 4064 | * This function will sleep at least once waiting for new free space to |
| 4065 | * show up, and then it will check the block group free space numbers |
| 4066 | * for our min num_bytes. Another option is to have it go ahead |
| 4067 | * and look in the rbtree for a free extent of a given size, but this |
| 4068 | * is a good start. |
| 4069 | */ |
| 4070 | static noinline int |
| 4071 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4072 | u64 num_bytes) |
| 4073 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4074 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4075 | DEFINE_WAIT(wait); |
| 4076 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4077 | caching_ctl = get_caching_control(cache); |
| 4078 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4079 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4080 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4081 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4082 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4083 | |
| 4084 | put_caching_control(caching_ctl); |
| 4085 | return 0; |
| 4086 | } |
| 4087 | |
| 4088 | static noinline int |
| 4089 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4090 | { |
| 4091 | struct btrfs_caching_control *caching_ctl; |
| 4092 | DEFINE_WAIT(wait); |
| 4093 | |
| 4094 | caching_ctl = get_caching_control(cache); |
| 4095 | if (!caching_ctl) |
| 4096 | return 0; |
| 4097 | |
| 4098 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4099 | |
| 4100 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4101 | return 0; |
| 4102 | } |
| 4103 | |
| 4104 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4105 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4106 | LOOP_CACHING_NOWAIT = 1, |
| 4107 | LOOP_CACHING_WAIT = 2, |
| 4108 | LOOP_ALLOC_CHUNK = 3, |
| 4109 | LOOP_NO_EMPTY_SIZE = 4, |
| 4110 | }; |
| 4111 | |
| 4112 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4113 | * walks the btree of allocated extents and find a hole of a given size. |
| 4114 | * The key ins is changed to record the hole: |
| 4115 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4116 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4117 | * ins->offset == number of blocks |
| 4118 | * Any available blocks before search_start are skipped. |
| 4119 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4120 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4121 | struct btrfs_root *orig_root, |
| 4122 | u64 num_bytes, u64 empty_size, |
| 4123 | u64 search_start, u64 search_end, |
| 4124 | u64 hint_byte, struct btrfs_key *ins, |
| 4125 | u64 exclude_start, u64 exclude_nr, |
| 4126 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4127 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4128 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4129 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4130 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4131 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4132 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4133 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4134 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4135 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4136 | int last_ptr_loop = 0; |
| 4137 | int loop = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4138 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4139 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4140 | bool failed_alloc = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4141 | u64 ideal_cache_percent = 0; |
| 4142 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4143 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4144 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4145 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4146 | ins->objectid = 0; |
| 4147 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4148 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4149 | space_info = __find_space_info(root->fs_info, data); |
| 4150 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4151 | if (orig_root->ref_cows || empty_size) |
| 4152 | allowed_chunk_alloc = 1; |
| 4153 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4154 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4155 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4156 | if (!btrfs_test_opt(root, SSD)) |
| 4157 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4158 | } |
| 4159 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4160 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 4161 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4162 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4163 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4164 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4165 | spin_lock(&last_ptr->lock); |
| 4166 | if (last_ptr->block_group) |
| 4167 | hint_byte = last_ptr->window_start; |
| 4168 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4169 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4170 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4171 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4172 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4173 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4174 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4175 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4176 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4177 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4178 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4179 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4180 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4181 | /* |
| 4182 | * we don't want to use the block group if it doesn't match our |
| 4183 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4184 | * |
| 4185 | * However if we are re-searching with an ideal block group |
| 4186 | * 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] | 4187 | */ |
| 4188 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4189 | (block_group->cached != BTRFS_CACHE_NO || |
| 4190 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4191 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4192 | if (list_empty(&block_group->list) || |
| 4193 | block_group->ro) { |
| 4194 | /* |
| 4195 | * someone is removing this block group, |
| 4196 | * we can't jump into the have_block_group |
| 4197 | * target because our list pointers are not |
| 4198 | * valid |
| 4199 | */ |
| 4200 | btrfs_put_block_group(block_group); |
| 4201 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4202 | } else { |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4203 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4204 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4205 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4206 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4207 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4208 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4209 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4210 | down_read(&space_info->groups_sem); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4211 | list_for_each_entry(block_group, &space_info->block_groups, list) { |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4212 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4213 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4214 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4215 | atomic_inc(&block_group->count); |
| 4216 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4217 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4218 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4219 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4220 | u64 free_percent; |
| 4221 | |
| 4222 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4223 | free_percent *= 100; |
| 4224 | free_percent = div64_u64(free_percent, |
| 4225 | block_group->key.offset); |
| 4226 | free_percent = 100 - free_percent; |
| 4227 | if (free_percent > ideal_cache_percent && |
| 4228 | likely(!block_group->ro)) { |
| 4229 | ideal_cache_offset = block_group->key.objectid; |
| 4230 | ideal_cache_percent = free_percent; |
| 4231 | } |
| 4232 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4233 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4234 | * We only want to start kthread caching if we are at |
| 4235 | * the point where we will wait for caching to make |
| 4236 | * progress, or if our ideal search is over and we've |
| 4237 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4238 | */ |
| 4239 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4240 | (loop > LOOP_FIND_IDEAL && |
| 4241 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4242 | ret = cache_block_group(block_group); |
| 4243 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4244 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4245 | found_uncached_bg = true; |
| 4246 | |
| 4247 | /* |
| 4248 | * If loop is set for cached only, try the next block |
| 4249 | * group. |
| 4250 | */ |
| 4251 | if (loop == LOOP_FIND_IDEAL) |
| 4252 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4253 | } |
| 4254 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4255 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4256 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4257 | found_uncached_bg = true; |
| 4258 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4259 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4260 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4261 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4262 | /* |
| 4263 | * Ok we want to try and use the cluster allocator, so lets look |
| 4264 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 4265 | * have tried the cluster allocator plenty of times at this |
| 4266 | * point and not have found anything, so we are likely way too |
| 4267 | * fragmented for the clustering stuff to find anything, so lets |
| 4268 | * just skip it and let the allocator find whatever block it can |
| 4269 | * find |
| 4270 | */ |
| 4271 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4272 | /* |
| 4273 | * the refill lock keeps out other |
| 4274 | * people trying to start a new cluster |
| 4275 | */ |
| 4276 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4277 | if (last_ptr->block_group && |
| 4278 | (last_ptr->block_group->ro || |
| 4279 | !block_group_bits(last_ptr->block_group, data))) { |
| 4280 | offset = 0; |
| 4281 | goto refill_cluster; |
| 4282 | } |
| 4283 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4284 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 4285 | num_bytes, search_start); |
| 4286 | if (offset) { |
| 4287 | /* we have a block, we're done */ |
| 4288 | spin_unlock(&last_ptr->refill_lock); |
| 4289 | goto checks; |
| 4290 | } |
| 4291 | |
| 4292 | spin_lock(&last_ptr->lock); |
| 4293 | /* |
| 4294 | * whoops, this cluster doesn't actually point to |
| 4295 | * this block group. Get a ref on the block |
| 4296 | * group is does point to and try again |
| 4297 | */ |
| 4298 | if (!last_ptr_loop && last_ptr->block_group && |
| 4299 | last_ptr->block_group != block_group) { |
| 4300 | |
| 4301 | btrfs_put_block_group(block_group); |
| 4302 | block_group = last_ptr->block_group; |
| 4303 | atomic_inc(&block_group->count); |
| 4304 | spin_unlock(&last_ptr->lock); |
| 4305 | spin_unlock(&last_ptr->refill_lock); |
| 4306 | |
| 4307 | last_ptr_loop = 1; |
| 4308 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4309 | /* |
| 4310 | * we know this block group is properly |
| 4311 | * in the list because |
| 4312 | * btrfs_remove_block_group, drops the |
| 4313 | * cluster before it removes the block |
| 4314 | * group from the list |
| 4315 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4316 | goto have_block_group; |
| 4317 | } |
| 4318 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4319 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4320 | /* |
| 4321 | * this cluster didn't work out, free it and |
| 4322 | * start over |
| 4323 | */ |
| 4324 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 4325 | |
| 4326 | last_ptr_loop = 0; |
| 4327 | |
| 4328 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 4329 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4330 | block_group, last_ptr, |
| 4331 | offset, num_bytes, |
| 4332 | empty_cluster + empty_size); |
| 4333 | if (ret == 0) { |
| 4334 | /* |
| 4335 | * now pull our allocation out of this |
| 4336 | * cluster |
| 4337 | */ |
| 4338 | offset = btrfs_alloc_from_cluster(block_group, |
| 4339 | last_ptr, num_bytes, |
| 4340 | search_start); |
| 4341 | if (offset) { |
| 4342 | /* we found one, proceed */ |
| 4343 | spin_unlock(&last_ptr->refill_lock); |
| 4344 | goto checks; |
| 4345 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4346 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 4347 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4348 | spin_unlock(&last_ptr->refill_lock); |
| 4349 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4350 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4351 | wait_block_group_cache_progress(block_group, |
| 4352 | num_bytes + empty_cluster + empty_size); |
| 4353 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4354 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4355 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4356 | /* |
| 4357 | * at this point we either didn't find a cluster |
| 4358 | * or we weren't able to allocate a block from our |
| 4359 | * cluster. Free the cluster we've been trying |
| 4360 | * to use, and go to the next block group |
| 4361 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4362 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4363 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4364 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4365 | } |
| 4366 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4367 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 4368 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4369 | /* |
| 4370 | * If we didn't find a chunk, and we haven't failed on this |
| 4371 | * block group before, and this block group is in the middle of |
| 4372 | * caching and we are ok with waiting, then go ahead and wait |
| 4373 | * for progress to be made, and set failed_alloc to true. |
| 4374 | * |
| 4375 | * If failed_alloc is true then we've already waited on this |
| 4376 | * block group once and should move on to the next block group. |
| 4377 | */ |
| 4378 | if (!offset && !failed_alloc && !cached && |
| 4379 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4380 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4381 | num_bytes + empty_size); |
| 4382 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4383 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4384 | } else if (!offset) { |
| 4385 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4386 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4387 | checks: |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4388 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4389 | /* move on to the next group */ |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4390 | if (search_start + num_bytes >= search_end) { |
| 4391 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4392 | goto loop; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4393 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4394 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4395 | /* move on to the next group */ |
| 4396 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4397 | block_group->key.objectid + block_group->key.offset) { |
| 4398 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4399 | goto loop; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4400 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4401 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4402 | if (exclude_nr > 0 && |
| 4403 | (search_start + num_bytes > exclude_start && |
| 4404 | search_start < exclude_start + exclude_nr)) { |
| 4405 | search_start = exclude_start + exclude_nr; |
| 4406 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4407 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4408 | /* |
| 4409 | * if search_start is still in this block group |
| 4410 | * then we just re-search this block group |
| 4411 | */ |
| 4412 | if (search_start >= block_group->key.objectid && |
| 4413 | search_start < (block_group->key.objectid + |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4414 | block_group->key.offset)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4415 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4416 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4417 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4418 | |
| 4419 | ins->objectid = search_start; |
| 4420 | ins->offset = num_bytes; |
| 4421 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4422 | if (offset < search_start) |
| 4423 | btrfs_add_free_space(block_group, offset, |
| 4424 | search_start - offset); |
| 4425 | BUG_ON(offset > search_start); |
| 4426 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4427 | update_reserved_extents(block_group, num_bytes, 1); |
| 4428 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4429 | /* we are all good, lets return */ |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4430 | break; |
| 4431 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4432 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4433 | failed_alloc = false; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4434 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4435 | } |
| 4436 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4437 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4438 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 4439 | * for them to make caching progress. Also |
| 4440 | * determine the best possible bg to cache |
| 4441 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 4442 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4443 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 4444 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 4445 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 4446 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4447 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4448 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 4449 | (found_uncached_bg || empty_size || empty_cluster || |
| 4450 | allowed_chunk_alloc)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4451 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4452 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4453 | loop++; |
| 4454 | if (!ideal_cache_percent && |
| 4455 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4456 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4457 | |
| 4458 | /* |
| 4459 | * 1 of the following 2 things have happened so far |
| 4460 | * |
| 4461 | * 1) We found an ideal block group for caching that |
| 4462 | * is mostly full and will cache quickly, so we might |
| 4463 | * as well wait for it. |
| 4464 | * |
| 4465 | * 2) We searched for cached only and we didn't find |
| 4466 | * anything, and we didn't start any caching kthreads |
| 4467 | * either, so chances are we will loop through and |
| 4468 | * start a couple caching kthreads, and then come back |
| 4469 | * around and just wait for them. This will be slower |
| 4470 | * because we will have 2 caching kthreads reading at |
| 4471 | * the same time when we could have just started one |
| 4472 | * and waited for it to get far enough to give us an |
| 4473 | * allocation, so go ahead and go to the wait caching |
| 4474 | * loop. |
| 4475 | */ |
| 4476 | loop = LOOP_CACHING_WAIT; |
| 4477 | search_start = ideal_cache_offset; |
| 4478 | ideal_cache_percent = 0; |
| 4479 | goto ideal_cache; |
| 4480 | } else if (loop == LOOP_FIND_IDEAL) { |
| 4481 | /* |
| 4482 | * Didn't find a uncached bg, wait on anything we find |
| 4483 | * next. |
| 4484 | */ |
| 4485 | loop = LOOP_CACHING_WAIT; |
| 4486 | goto search; |
| 4487 | } |
| 4488 | |
| 4489 | if (loop < LOOP_CACHING_WAIT) { |
| 4490 | loop++; |
| 4491 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4495 | empty_size = 0; |
| 4496 | empty_cluster = 0; |
| 4497 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4498 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4499 | if (allowed_chunk_alloc) { |
| 4500 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 4501 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4502 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4503 | done_chunk_alloc = 1; |
| 4504 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4505 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4506 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4507 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4508 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4509 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4510 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4511 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4512 | ret = -ENOSPC; |
| 4513 | } else if (!ins->objectid) { |
| 4514 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 4515 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4516 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4517 | /* we found what we needed */ |
| 4518 | if (ins->objectid) { |
| 4519 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4520 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4521 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4522 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4523 | ret = 0; |
| 4524 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4525 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4526 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4527 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4528 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4529 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 4530 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4531 | { |
| 4532 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4533 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4534 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4535 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 4536 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4537 | info->bytes_pinned - info->bytes_reserved - |
| 4538 | info->bytes_super), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4539 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4540 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4541 | " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu" |
| 4542 | "\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 4543 | (unsigned long long)info->total_bytes, |
| 4544 | (unsigned long long)info->bytes_pinned, |
| 4545 | (unsigned long long)info->bytes_delalloc, |
| 4546 | (unsigned long long)info->bytes_may_use, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4547 | (unsigned long long)info->bytes_used, |
| 4548 | (unsigned long long)info->bytes_root, |
| 4549 | (unsigned long long)info->bytes_super, |
| 4550 | (unsigned long long)info->bytes_reserved); |
| 4551 | spin_unlock(&info->lock); |
| 4552 | |
| 4553 | if (!dump_block_groups) |
| 4554 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4555 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4556 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 4557 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4558 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4559 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 4560 | "%llu pinned %llu reserved\n", |
| 4561 | (unsigned long long)cache->key.objectid, |
| 4562 | (unsigned long long)cache->key.offset, |
| 4563 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 4564 | (unsigned long long)cache->pinned, |
| 4565 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4566 | btrfs_dump_free_space(cache, bytes); |
| 4567 | spin_unlock(&cache->lock); |
| 4568 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4569 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4570 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4571 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4572 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 4573 | struct btrfs_root *root, |
| 4574 | u64 num_bytes, u64 min_alloc_size, |
| 4575 | u64 empty_size, u64 hint_byte, |
| 4576 | u64 search_end, struct btrfs_key *ins, |
| 4577 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4578 | { |
| 4579 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 4580 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4581 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4582 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 4583 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4584 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4585 | /* |
| 4586 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 4587 | * is not called recursively on allocations |
| 4588 | */ |
| 4589 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4590 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4591 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4592 | 2 * 1024 * 1024, |
| 4593 | BTRFS_BLOCK_GROUP_METADATA | |
| 4594 | (info->metadata_alloc_profile & |
| 4595 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4596 | } |
| 4597 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4598 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4599 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4600 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4601 | WARN_ON(num_bytes < root->sectorsize); |
| 4602 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 4603 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 4604 | trans->alloc_exclude_start, |
| 4605 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4606 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4607 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 4608 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4609 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4610 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4611 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 4612 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 4613 | goto again; |
| 4614 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4615 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4616 | struct btrfs_space_info *sinfo; |
| 4617 | |
| 4618 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4619 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 4620 | "wanted %llu\n", (unsigned long long)data, |
| 4621 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4622 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4623 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4624 | |
| 4625 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4626 | } |
| 4627 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4628 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 4629 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4630 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4631 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4632 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4633 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 4634 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4635 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 4636 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4637 | return -ENOSPC; |
| 4638 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4639 | |
| 4640 | ret = btrfs_discard_extent(root, start, len); |
| 4641 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4642 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4643 | update_reserved_extents(cache, len, 0); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4644 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4645 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4646 | return ret; |
| 4647 | } |
| 4648 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4649 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4650 | struct btrfs_root *root, |
| 4651 | u64 parent, u64 root_objectid, |
| 4652 | u64 flags, u64 owner, u64 offset, |
| 4653 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4654 | { |
| 4655 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4656 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4657 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4658 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4659 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4660 | struct extent_buffer *leaf; |
| 4661 | int type; |
| 4662 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 4663 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4664 | if (parent > 0) |
| 4665 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 4666 | else |
| 4667 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4668 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4669 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4670 | |
| 4671 | path = btrfs_alloc_path(); |
| 4672 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4673 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4674 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4675 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4676 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4677 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4678 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4679 | leaf = path->nodes[0]; |
| 4680 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4681 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4682 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 4683 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4684 | btrfs_set_extent_flags(leaf, extent_item, |
| 4685 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4686 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4687 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 4688 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 4689 | if (parent > 0) { |
| 4690 | struct btrfs_shared_data_ref *ref; |
| 4691 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 4692 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4693 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 4694 | } else { |
| 4695 | struct btrfs_extent_data_ref *ref; |
| 4696 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 4697 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 4698 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 4699 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 4700 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 4701 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 4702 | |
| 4703 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4704 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4705 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4706 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4707 | 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4708 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4709 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4710 | "%llu\n", (unsigned long long)ins->objectid, |
| 4711 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 4712 | BUG(); |
| 4713 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4714 | return ret; |
| 4715 | } |
| 4716 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4717 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 4718 | struct btrfs_root *root, |
| 4719 | u64 parent, u64 root_objectid, |
| 4720 | u64 flags, struct btrfs_disk_key *key, |
| 4721 | int level, struct btrfs_key *ins) |
| 4722 | { |
| 4723 | int ret; |
| 4724 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4725 | struct btrfs_extent_item *extent_item; |
| 4726 | struct btrfs_tree_block_info *block_info; |
| 4727 | struct btrfs_extent_inline_ref *iref; |
| 4728 | struct btrfs_path *path; |
| 4729 | struct extent_buffer *leaf; |
| 4730 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 4731 | |
| 4732 | path = btrfs_alloc_path(); |
| 4733 | BUG_ON(!path); |
| 4734 | |
| 4735 | path->leave_spinning = 1; |
| 4736 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 4737 | ins, size); |
| 4738 | BUG_ON(ret); |
| 4739 | |
| 4740 | leaf = path->nodes[0]; |
| 4741 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 4742 | struct btrfs_extent_item); |
| 4743 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 4744 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 4745 | btrfs_set_extent_flags(leaf, extent_item, |
| 4746 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 4747 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 4748 | |
| 4749 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 4750 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 4751 | |
| 4752 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 4753 | if (parent > 0) { |
| 4754 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 4755 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4756 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 4757 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 4758 | } else { |
| 4759 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 4760 | BTRFS_TREE_BLOCK_REF_KEY); |
| 4761 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 4762 | } |
| 4763 | |
| 4764 | btrfs_mark_buffer_dirty(leaf); |
| 4765 | btrfs_free_path(path); |
| 4766 | |
| 4767 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 4768 | 1, 0); |
| 4769 | if (ret) { |
| 4770 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 4771 | "%llu\n", (unsigned long long)ins->objectid, |
| 4772 | (unsigned long long)ins->offset); |
| 4773 | BUG(); |
| 4774 | } |
| 4775 | return ret; |
| 4776 | } |
| 4777 | |
| 4778 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 4779 | struct btrfs_root *root, |
| 4780 | u64 root_objectid, u64 owner, |
| 4781 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4782 | { |
| 4783 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 4784 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4785 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4786 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4787 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 4788 | 0, root_objectid, owner, offset, |
| 4789 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4790 | return ret; |
| 4791 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4792 | |
| 4793 | /* |
| 4794 | * this is used by the tree logging recovery code. It records that |
| 4795 | * an extent has been allocated and makes sure to clear the free |
| 4796 | * space cache bits as well |
| 4797 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4798 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 4799 | struct btrfs_root *root, |
| 4800 | u64 root_objectid, u64 owner, u64 offset, |
| 4801 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4802 | { |
| 4803 | int ret; |
| 4804 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4805 | struct btrfs_caching_control *caching_ctl; |
| 4806 | u64 start = ins->objectid; |
| 4807 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4808 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4809 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4810 | cache_block_group(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4811 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4812 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4813 | if (!caching_ctl) { |
| 4814 | BUG_ON(!block_group_cache_done(block_group)); |
| 4815 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 4816 | BUG_ON(ret); |
| 4817 | } else { |
| 4818 | mutex_lock(&caching_ctl->mutex); |
| 4819 | |
| 4820 | if (start >= caching_ctl->progress) { |
| 4821 | ret = add_excluded_extent(root, start, num_bytes); |
| 4822 | BUG_ON(ret); |
| 4823 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 4824 | ret = btrfs_remove_free_space(block_group, |
| 4825 | start, num_bytes); |
| 4826 | BUG_ON(ret); |
| 4827 | } else { |
| 4828 | num_bytes = caching_ctl->progress - start; |
| 4829 | ret = btrfs_remove_free_space(block_group, |
| 4830 | start, num_bytes); |
| 4831 | BUG_ON(ret); |
| 4832 | |
| 4833 | start = caching_ctl->progress; |
| 4834 | num_bytes = ins->objectid + ins->offset - |
| 4835 | caching_ctl->progress; |
| 4836 | ret = add_excluded_extent(root, start, num_bytes); |
| 4837 | BUG_ON(ret); |
| 4838 | } |
| 4839 | |
| 4840 | mutex_unlock(&caching_ctl->mutex); |
| 4841 | put_caching_control(caching_ctl); |
| 4842 | } |
| 4843 | |
| 4844 | update_reserved_extents(block_group, ins->offset, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4845 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4846 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 4847 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4848 | return ret; |
| 4849 | } |
| 4850 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4851 | /* |
| 4852 | * finds a free extent and does all the dirty work required for allocation |
| 4853 | * returns the key for the extent through ins, and a tree buffer for |
| 4854 | * the first block of the extent through buf. |
| 4855 | * |
| 4856 | * returns 0 if everything worked, non-zero otherwise. |
| 4857 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4858 | static int alloc_tree_block(struct btrfs_trans_handle *trans, |
| 4859 | struct btrfs_root *root, |
| 4860 | u64 num_bytes, u64 parent, u64 root_objectid, |
| 4861 | struct btrfs_disk_key *key, int level, |
| 4862 | u64 empty_size, u64 hint_byte, u64 search_end, |
| 4863 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4864 | { |
| 4865 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4866 | u64 flags = 0; |
| 4867 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4868 | ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes, |
| 4869 | empty_size, hint_byte, search_end, |
| 4870 | ins, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4871 | if (ret) |
| 4872 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4873 | |
| 4874 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4875 | if (parent == 0) |
| 4876 | parent = ins->objectid; |
| 4877 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4878 | } else |
| 4879 | BUG_ON(parent > 0); |
| 4880 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4881 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4882 | struct btrfs_delayed_extent_op *extent_op; |
| 4883 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 4884 | BUG_ON(!extent_op); |
| 4885 | if (key) |
| 4886 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4887 | else |
| 4888 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4889 | extent_op->flags_to_set = flags; |
| 4890 | extent_op->update_key = 1; |
| 4891 | extent_op->update_flags = 1; |
| 4892 | extent_op->is_data = 0; |
| 4893 | |
| 4894 | ret = btrfs_add_delayed_tree_ref(trans, ins->objectid, |
| 4895 | ins->offset, parent, root_objectid, |
| 4896 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 4897 | extent_op); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4898 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4899 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4900 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4901 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4902 | |
| 4903 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 4904 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4905 | u64 bytenr, u32 blocksize, |
| 4906 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4907 | { |
| 4908 | struct extent_buffer *buf; |
| 4909 | |
| 4910 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 4911 | if (!buf) |
| 4912 | return ERR_PTR(-ENOMEM); |
| 4913 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4914 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4915 | btrfs_tree_lock(buf); |
| 4916 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4917 | |
| 4918 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4919 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4920 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4921 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4922 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4923 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4924 | } else { |
| 4925 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4926 | buf->start + buf->len - 1, GFP_NOFS); |
| 4927 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4928 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4929 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4930 | return buf; |
| 4931 | } |
| 4932 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4933 | /* |
| 4934 | * helper function to allocate a block for a given tree |
| 4935 | * returns the tree buffer or NULL. |
| 4936 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4937 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4938 | struct btrfs_root *root, u32 blocksize, |
| 4939 | u64 parent, u64 root_objectid, |
| 4940 | struct btrfs_disk_key *key, int level, |
| 4941 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4942 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4943 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4944 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4945 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4946 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4947 | ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid, |
| 4948 | key, level, empty_size, hint, (u64)-1, &ins); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4949 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4950 | BUG_ON(ret > 0); |
| 4951 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4952 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4953 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4954 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 4955 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4956 | return buf; |
| 4957 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4958 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4959 | struct walk_control { |
| 4960 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4961 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4962 | struct btrfs_key update_progress; |
| 4963 | int stage; |
| 4964 | int level; |
| 4965 | int shared_level; |
| 4966 | int update_ref; |
| 4967 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4968 | int reada_slot; |
| 4969 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4970 | }; |
| 4971 | |
| 4972 | #define DROP_REFERENCE 1 |
| 4973 | #define UPDATE_BACKREF 2 |
| 4974 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4975 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 4976 | struct btrfs_root *root, |
| 4977 | struct walk_control *wc, |
| 4978 | struct btrfs_path *path) |
| 4979 | { |
| 4980 | u64 bytenr; |
| 4981 | u64 generation; |
| 4982 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 4983 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 4984 | u64 last = 0; |
| 4985 | u32 nritems; |
| 4986 | u32 blocksize; |
| 4987 | struct btrfs_key key; |
| 4988 | struct extent_buffer *eb; |
| 4989 | int ret; |
| 4990 | int slot; |
| 4991 | int nread = 0; |
| 4992 | |
| 4993 | if (path->slots[wc->level] < wc->reada_slot) { |
| 4994 | wc->reada_count = wc->reada_count * 2 / 3; |
| 4995 | wc->reada_count = max(wc->reada_count, 2); |
| 4996 | } else { |
| 4997 | wc->reada_count = wc->reada_count * 3 / 2; |
| 4998 | wc->reada_count = min_t(int, wc->reada_count, |
| 4999 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5000 | } |
| 5001 | |
| 5002 | eb = path->nodes[wc->level]; |
| 5003 | nritems = btrfs_header_nritems(eb); |
| 5004 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5005 | |
| 5006 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5007 | if (nread >= wc->reada_count) |
| 5008 | break; |
| 5009 | |
| 5010 | cond_resched(); |
| 5011 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5012 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5013 | |
| 5014 | if (slot == path->slots[wc->level]) |
| 5015 | goto reada; |
| 5016 | |
| 5017 | if (wc->stage == UPDATE_BACKREF && |
| 5018 | generation <= root->root_key.offset) |
| 5019 | continue; |
| 5020 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5021 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5022 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5023 | &refs, &flags); |
| 5024 | BUG_ON(ret); |
| 5025 | BUG_ON(refs == 0); |
| 5026 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5027 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5028 | if (refs == 1) |
| 5029 | goto reada; |
| 5030 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5031 | if (wc->level == 1 && |
| 5032 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5033 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5034 | if (!wc->update_ref || |
| 5035 | generation <= root->root_key.offset) |
| 5036 | continue; |
| 5037 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5038 | ret = btrfs_comp_cpu_keys(&key, |
| 5039 | &wc->update_progress); |
| 5040 | if (ret < 0) |
| 5041 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5042 | } else { |
| 5043 | if (wc->level == 1 && |
| 5044 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5045 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5046 | } |
| 5047 | reada: |
| 5048 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5049 | generation); |
| 5050 | if (ret) |
| 5051 | break; |
| 5052 | last = bytenr + blocksize; |
| 5053 | nread++; |
| 5054 | } |
| 5055 | wc->reada_slot = slot; |
| 5056 | } |
| 5057 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5058 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5059 | * hepler to process tree block while walking down the tree. |
| 5060 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5061 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5062 | * back refs for pointers in the block. |
| 5063 | * |
| 5064 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5065 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5066 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5067 | struct btrfs_root *root, |
| 5068 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5069 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5070 | { |
| 5071 | int level = wc->level; |
| 5072 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5073 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5074 | int ret; |
| 5075 | |
| 5076 | if (wc->stage == UPDATE_BACKREF && |
| 5077 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5078 | return 1; |
| 5079 | |
| 5080 | /* |
| 5081 | * when reference count of tree block is 1, it won't increase |
| 5082 | * again. once full backref flag is set, we never clear it. |
| 5083 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5084 | if (lookup_info && |
| 5085 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5086 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5087 | BUG_ON(!path->locks[level]); |
| 5088 | ret = btrfs_lookup_extent_info(trans, root, |
| 5089 | eb->start, eb->len, |
| 5090 | &wc->refs[level], |
| 5091 | &wc->flags[level]); |
| 5092 | BUG_ON(ret); |
| 5093 | BUG_ON(wc->refs[level] == 0); |
| 5094 | } |
| 5095 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5096 | if (wc->stage == DROP_REFERENCE) { |
| 5097 | if (wc->refs[level] > 1) |
| 5098 | return 1; |
| 5099 | |
| 5100 | if (path->locks[level] && !wc->keep_locks) { |
| 5101 | btrfs_tree_unlock(eb); |
| 5102 | path->locks[level] = 0; |
| 5103 | } |
| 5104 | return 0; |
| 5105 | } |
| 5106 | |
| 5107 | /* wc->stage == UPDATE_BACKREF */ |
| 5108 | if (!(wc->flags[level] & flag)) { |
| 5109 | BUG_ON(!path->locks[level]); |
| 5110 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5111 | BUG_ON(ret); |
| 5112 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5113 | BUG_ON(ret); |
| 5114 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5115 | eb->len, flag, 0); |
| 5116 | BUG_ON(ret); |
| 5117 | wc->flags[level] |= flag; |
| 5118 | } |
| 5119 | |
| 5120 | /* |
| 5121 | * the block is shared by multiple trees, so it's not good to |
| 5122 | * keep the tree lock |
| 5123 | */ |
| 5124 | if (path->locks[level] && level > 0) { |
| 5125 | btrfs_tree_unlock(eb); |
| 5126 | path->locks[level] = 0; |
| 5127 | } |
| 5128 | return 0; |
| 5129 | } |
| 5130 | |
| 5131 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5132 | * hepler to process tree block pointer. |
| 5133 | * |
| 5134 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5135 | * reference count of the block pointed to. if the block |
| 5136 | * is shared and we need update back refs for the subtree |
| 5137 | * rooted at the block, this function changes wc->stage to |
| 5138 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5139 | * need to update back, this function drops the reference |
| 5140 | * to the block. |
| 5141 | * |
| 5142 | * NOTE: return value 1 means we should stop walking down. |
| 5143 | */ |
| 5144 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5145 | struct btrfs_root *root, |
| 5146 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5147 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5148 | { |
| 5149 | u64 bytenr; |
| 5150 | u64 generation; |
| 5151 | u64 parent; |
| 5152 | u32 blocksize; |
| 5153 | struct btrfs_key key; |
| 5154 | struct extent_buffer *next; |
| 5155 | int level = wc->level; |
| 5156 | int reada = 0; |
| 5157 | int ret = 0; |
| 5158 | |
| 5159 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5160 | path->slots[level]); |
| 5161 | /* |
| 5162 | * if the lower level block was created before the snapshot |
| 5163 | * was created, we know there is no need to update back refs |
| 5164 | * for the subtree |
| 5165 | */ |
| 5166 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5167 | generation <= root->root_key.offset) { |
| 5168 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5169 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5170 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5171 | |
| 5172 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5173 | blocksize = btrfs_level_size(root, level - 1); |
| 5174 | |
| 5175 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5176 | if (!next) { |
| 5177 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5178 | reada = 1; |
| 5179 | } |
| 5180 | btrfs_tree_lock(next); |
| 5181 | btrfs_set_lock_blocking(next); |
| 5182 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5183 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5184 | &wc->refs[level - 1], |
| 5185 | &wc->flags[level - 1]); |
| 5186 | BUG_ON(ret); |
| 5187 | BUG_ON(wc->refs[level - 1] == 0); |
| 5188 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5189 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5190 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5191 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5192 | if (level == 1 && |
| 5193 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5194 | goto skip; |
| 5195 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5196 | if (!wc->update_ref || |
| 5197 | generation <= root->root_key.offset) |
| 5198 | goto skip; |
| 5199 | |
| 5200 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5201 | path->slots[level]); |
| 5202 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5203 | if (ret < 0) |
| 5204 | goto skip; |
| 5205 | |
| 5206 | wc->stage = UPDATE_BACKREF; |
| 5207 | wc->shared_level = level - 1; |
| 5208 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5209 | } else { |
| 5210 | if (level == 1 && |
| 5211 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5212 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5216 | btrfs_tree_unlock(next); |
| 5217 | free_extent_buffer(next); |
| 5218 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5219 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | if (!next) { |
| 5223 | if (reada && level == 1) |
| 5224 | reada_walk_down(trans, root, wc, path); |
| 5225 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5226 | btrfs_tree_lock(next); |
| 5227 | btrfs_set_lock_blocking(next); |
| 5228 | } |
| 5229 | |
| 5230 | level--; |
| 5231 | BUG_ON(level != btrfs_header_level(next)); |
| 5232 | path->nodes[level] = next; |
| 5233 | path->slots[level] = 0; |
| 5234 | path->locks[level] = 1; |
| 5235 | wc->level = level; |
| 5236 | if (wc->level == 1) |
| 5237 | wc->reada_slot = 0; |
| 5238 | return 0; |
| 5239 | skip: |
| 5240 | wc->refs[level - 1] = 0; |
| 5241 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5242 | if (wc->stage == DROP_REFERENCE) { |
| 5243 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5244 | parent = path->nodes[level]->start; |
| 5245 | } else { |
| 5246 | BUG_ON(root->root_key.objectid != |
| 5247 | btrfs_header_owner(path->nodes[level])); |
| 5248 | parent = 0; |
| 5249 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5250 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5251 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 5252 | root->root_key.objectid, level - 1, 0); |
| 5253 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5254 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5255 | btrfs_tree_unlock(next); |
| 5256 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5257 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5258 | return 1; |
| 5259 | } |
| 5260 | |
| 5261 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5262 | * hepler to process tree block while walking up the tree. |
| 5263 | * |
| 5264 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5265 | * reference count on the block. |
| 5266 | * |
| 5267 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5268 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5269 | * to UPDATE_BACKREF previously while processing the block. |
| 5270 | * |
| 5271 | * NOTE: return value 1 means we should stop walking up. |
| 5272 | */ |
| 5273 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5274 | struct btrfs_root *root, |
| 5275 | struct btrfs_path *path, |
| 5276 | struct walk_control *wc) |
| 5277 | { |
| 5278 | int ret = 0; |
| 5279 | int level = wc->level; |
| 5280 | struct extent_buffer *eb = path->nodes[level]; |
| 5281 | u64 parent = 0; |
| 5282 | |
| 5283 | if (wc->stage == UPDATE_BACKREF) { |
| 5284 | BUG_ON(wc->shared_level < level); |
| 5285 | if (level < wc->shared_level) |
| 5286 | goto out; |
| 5287 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5288 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5289 | if (ret > 0) |
| 5290 | wc->update_ref = 0; |
| 5291 | |
| 5292 | wc->stage = DROP_REFERENCE; |
| 5293 | wc->shared_level = -1; |
| 5294 | path->slots[level] = 0; |
| 5295 | |
| 5296 | /* |
| 5297 | * check reference count again if the block isn't locked. |
| 5298 | * we should start walking down the tree again if reference |
| 5299 | * count is one. |
| 5300 | */ |
| 5301 | if (!path->locks[level]) { |
| 5302 | BUG_ON(level == 0); |
| 5303 | btrfs_tree_lock(eb); |
| 5304 | btrfs_set_lock_blocking(eb); |
| 5305 | path->locks[level] = 1; |
| 5306 | |
| 5307 | ret = btrfs_lookup_extent_info(trans, root, |
| 5308 | eb->start, eb->len, |
| 5309 | &wc->refs[level], |
| 5310 | &wc->flags[level]); |
| 5311 | BUG_ON(ret); |
| 5312 | BUG_ON(wc->refs[level] == 0); |
| 5313 | if (wc->refs[level] == 1) { |
| 5314 | btrfs_tree_unlock(eb); |
| 5315 | path->locks[level] = 0; |
| 5316 | return 1; |
| 5317 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5318 | } |
| 5319 | } |
| 5320 | |
| 5321 | /* wc->stage == DROP_REFERENCE */ |
| 5322 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5323 | |
| 5324 | if (wc->refs[level] == 1) { |
| 5325 | if (level == 0) { |
| 5326 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5327 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 5328 | else |
| 5329 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5330 | BUG_ON(ret); |
| 5331 | } |
| 5332 | /* make block locked assertion in clean_tree_block happy */ |
| 5333 | if (!path->locks[level] && |
| 5334 | btrfs_header_generation(eb) == trans->transid) { |
| 5335 | btrfs_tree_lock(eb); |
| 5336 | btrfs_set_lock_blocking(eb); |
| 5337 | path->locks[level] = 1; |
| 5338 | } |
| 5339 | clean_tree_block(trans, root, eb); |
| 5340 | } |
| 5341 | |
| 5342 | if (eb == root->node) { |
| 5343 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5344 | parent = eb->start; |
| 5345 | else |
| 5346 | BUG_ON(root->root_key.objectid != |
| 5347 | btrfs_header_owner(eb)); |
| 5348 | } else { |
| 5349 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5350 | parent = path->nodes[level + 1]->start; |
| 5351 | else |
| 5352 | BUG_ON(root->root_key.objectid != |
| 5353 | btrfs_header_owner(path->nodes[level + 1])); |
| 5354 | } |
| 5355 | |
| 5356 | ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent, |
| 5357 | root->root_key.objectid, level, 0); |
| 5358 | BUG_ON(ret); |
| 5359 | out: |
| 5360 | wc->refs[level] = 0; |
| 5361 | wc->flags[level] = 0; |
| 5362 | return ret; |
| 5363 | } |
| 5364 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5365 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5366 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5367 | struct btrfs_path *path, |
| 5368 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5369 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5370 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5371 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5372 | int ret; |
| 5373 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5374 | while (level >= 0) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5375 | if (path->slots[level] >= |
| 5376 | btrfs_header_nritems(path->nodes[level])) |
| 5377 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5378 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5379 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5380 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5381 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5382 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5383 | if (level == 0) |
| 5384 | break; |
| 5385 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5386 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5387 | if (ret > 0) { |
| 5388 | path->slots[level]++; |
| 5389 | continue; |
| 5390 | } |
| 5391 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5392 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5393 | return 0; |
| 5394 | } |
| 5395 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5396 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5397 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5398 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5399 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5400 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5401 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5402 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5403 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5404 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5405 | while (level < max_level && path->nodes[level]) { |
| 5406 | wc->level = level; |
| 5407 | if (path->slots[level] + 1 < |
| 5408 | btrfs_header_nritems(path->nodes[level])) { |
| 5409 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5410 | return 0; |
| 5411 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5412 | ret = walk_up_proc(trans, root, path, wc); |
| 5413 | if (ret > 0) |
| 5414 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5415 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5416 | if (path->locks[level]) { |
| 5417 | btrfs_tree_unlock(path->nodes[level]); |
| 5418 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5419 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5420 | free_extent_buffer(path->nodes[level]); |
| 5421 | path->nodes[level] = NULL; |
| 5422 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5423 | } |
| 5424 | } |
| 5425 | return 1; |
| 5426 | } |
| 5427 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5428 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5429 | * drop a subvolume tree. |
| 5430 | * |
| 5431 | * this function traverses the tree freeing any blocks that only |
| 5432 | * referenced by the tree. |
| 5433 | * |
| 5434 | * when a shared tree block is found. this function decreases its |
| 5435 | * reference count by one. if update_ref is true, this function |
| 5436 | * also make sure backrefs for the shared block and all lower level |
| 5437 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5438 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5439 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5440 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5441 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5442 | struct btrfs_trans_handle *trans; |
| 5443 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5444 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5445 | struct walk_control *wc; |
| 5446 | struct btrfs_key key; |
| 5447 | int err = 0; |
| 5448 | int ret; |
| 5449 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5450 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5451 | path = btrfs_alloc_path(); |
| 5452 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5453 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5454 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5455 | BUG_ON(!wc); |
| 5456 | |
| 5457 | trans = btrfs_start_transaction(tree_root, 1); |
| 5458 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5459 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5460 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5461 | path->nodes[level] = btrfs_lock_root_node(root); |
| 5462 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5463 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5464 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5465 | memset(&wc->update_progress, 0, |
| 5466 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5467 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5468 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5469 | memcpy(&wc->update_progress, &key, |
| 5470 | sizeof(wc->update_progress)); |
| 5471 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5472 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5473 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5474 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5475 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5476 | path->lowest_level = 0; |
| 5477 | if (ret < 0) { |
| 5478 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5479 | goto out; |
| 5480 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5481 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5482 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5483 | /* |
| 5484 | * unlock our path, this is safe because only this |
| 5485 | * function is allowed to delete this snapshot |
| 5486 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5487 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5488 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5489 | level = btrfs_header_level(root->node); |
| 5490 | while (1) { |
| 5491 | btrfs_tree_lock(path->nodes[level]); |
| 5492 | btrfs_set_lock_blocking(path->nodes[level]); |
| 5493 | |
| 5494 | ret = btrfs_lookup_extent_info(trans, root, |
| 5495 | path->nodes[level]->start, |
| 5496 | path->nodes[level]->len, |
| 5497 | &wc->refs[level], |
| 5498 | &wc->flags[level]); |
| 5499 | BUG_ON(ret); |
| 5500 | BUG_ON(wc->refs[level] == 0); |
| 5501 | |
| 5502 | if (level == root_item->drop_level) |
| 5503 | break; |
| 5504 | |
| 5505 | btrfs_tree_unlock(path->nodes[level]); |
| 5506 | WARN_ON(wc->refs[level] != 1); |
| 5507 | level--; |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | wc->level = level; |
| 5512 | wc->shared_level = -1; |
| 5513 | wc->stage = DROP_REFERENCE; |
| 5514 | wc->update_ref = update_ref; |
| 5515 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5516 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5517 | |
| 5518 | while (1) { |
| 5519 | ret = walk_down_tree(trans, root, path, wc); |
| 5520 | if (ret < 0) { |
| 5521 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5522 | break; |
| 5523 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5524 | |
| 5525 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5526 | if (ret < 0) { |
| 5527 | err = ret; |
| 5528 | break; |
| 5529 | } |
| 5530 | |
| 5531 | if (ret > 0) { |
| 5532 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5533 | break; |
| 5534 | } |
| 5535 | |
| 5536 | if (wc->stage == DROP_REFERENCE) { |
| 5537 | level = wc->level; |
| 5538 | btrfs_node_key(path->nodes[level], |
| 5539 | &root_item->drop_progress, |
| 5540 | path->slots[level]); |
| 5541 | root_item->drop_level = level; |
| 5542 | } |
| 5543 | |
| 5544 | BUG_ON(wc->level == 0); |
| 5545 | if (trans->transaction->in_commit || |
| 5546 | trans->transaction->delayed_refs.flushing) { |
| 5547 | ret = btrfs_update_root(trans, tree_root, |
| 5548 | &root->root_key, |
| 5549 | root_item); |
| 5550 | BUG_ON(ret); |
| 5551 | |
| 5552 | btrfs_end_transaction(trans, tree_root); |
| 5553 | trans = btrfs_start_transaction(tree_root, 1); |
| 5554 | } else { |
| 5555 | unsigned long update; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5556 | update = trans->delayed_ref_updates; |
| 5557 | trans->delayed_ref_updates = 0; |
| 5558 | if (update) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5559 | btrfs_run_delayed_refs(trans, tree_root, |
| 5560 | update); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5561 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5562 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5563 | btrfs_release_path(root, path); |
| 5564 | BUG_ON(err); |
| 5565 | |
| 5566 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 5567 | BUG_ON(ret); |
| 5568 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 5569 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 5570 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 5571 | NULL, NULL); |
| 5572 | BUG_ON(ret < 0); |
| 5573 | if (ret > 0) { |
| 5574 | ret = btrfs_del_orphan_item(trans, tree_root, |
| 5575 | root->root_key.objectid); |
| 5576 | BUG_ON(ret); |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | if (root->in_radix) { |
| 5581 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 5582 | } else { |
| 5583 | free_extent_buffer(root->node); |
| 5584 | free_extent_buffer(root->commit_root); |
| 5585 | kfree(root); |
| 5586 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5587 | out: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5588 | btrfs_end_transaction(trans, tree_root); |
| 5589 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5590 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5591 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5592 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5593 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5594 | /* |
| 5595 | * drop subtree rooted at tree block 'node'. |
| 5596 | * |
| 5597 | * NOTE: this function will unlock and release tree block 'node' |
| 5598 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5599 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5600 | struct btrfs_root *root, |
| 5601 | struct extent_buffer *node, |
| 5602 | struct extent_buffer *parent) |
| 5603 | { |
| 5604 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5605 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5606 | int level; |
| 5607 | int parent_level; |
| 5608 | int ret = 0; |
| 5609 | int wret; |
| 5610 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5611 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5612 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5613 | path = btrfs_alloc_path(); |
| 5614 | BUG_ON(!path); |
| 5615 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5616 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5617 | BUG_ON(!wc); |
| 5618 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5619 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5620 | parent_level = btrfs_header_level(parent); |
| 5621 | extent_buffer_get(parent); |
| 5622 | path->nodes[parent_level] = parent; |
| 5623 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5624 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5625 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5626 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5627 | path->nodes[level] = node; |
| 5628 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5629 | path->locks[level] = 1; |
| 5630 | |
| 5631 | wc->refs[parent_level] = 1; |
| 5632 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5633 | wc->level = level; |
| 5634 | wc->shared_level = -1; |
| 5635 | wc->stage = DROP_REFERENCE; |
| 5636 | wc->update_ref = 0; |
| 5637 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5638 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5639 | |
| 5640 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5641 | wret = walk_down_tree(trans, root, path, wc); |
| 5642 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5643 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5644 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5645 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5646 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5647 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5648 | if (wret < 0) |
| 5649 | ret = wret; |
| 5650 | if (wret != 0) |
| 5651 | break; |
| 5652 | } |
| 5653 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5654 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5655 | btrfs_free_path(path); |
| 5656 | return ret; |
| 5657 | } |
| 5658 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5659 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5660 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 5661 | unsigned long nr) |
| 5662 | { |
| 5663 | return min(last, start + nr - 1); |
| 5664 | } |
| 5665 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5666 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5667 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5668 | { |
| 5669 | u64 page_start; |
| 5670 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5671 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5672 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5673 | unsigned long i; |
| 5674 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5675 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5676 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5677 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5678 | unsigned int total_read = 0; |
| 5679 | unsigned int total_dirty = 0; |
| 5680 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5681 | |
| 5682 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5683 | |
| 5684 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5685 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5686 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 5687 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5688 | /* make sure the dirty trick played by the caller work */ |
| 5689 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5690 | first_index, last_index); |
| 5691 | if (ret) |
| 5692 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5693 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5694 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5695 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5696 | for (i = first_index ; i <= last_index; i++) { |
| 5697 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5698 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5699 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5700 | } |
| 5701 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5702 | again: |
| 5703 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5704 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5705 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5706 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5707 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5708 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5709 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5710 | if (!PageUptodate(page)) { |
| 5711 | btrfs_readpage(NULL, page); |
| 5712 | lock_page(page); |
| 5713 | if (!PageUptodate(page)) { |
| 5714 | unlock_page(page); |
| 5715 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5716 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5717 | goto out_unlock; |
| 5718 | } |
| 5719 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5720 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5721 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5722 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 5723 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5724 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5725 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5726 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5727 | if (ordered) { |
| 5728 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5729 | unlock_page(page); |
| 5730 | page_cache_release(page); |
| 5731 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5732 | btrfs_put_ordered_extent(ordered); |
| 5733 | goto again; |
| 5734 | } |
| 5735 | set_page_extent_mapped(page); |
| 5736 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5737 | if (i == first_index) |
| 5738 | set_extent_bits(io_tree, page_start, page_end, |
| 5739 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 5740 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5741 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5742 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5743 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5744 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5745 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5746 | unlock_page(page); |
| 5747 | page_cache_release(page); |
| 5748 | } |
| 5749 | |
| 5750 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5751 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5752 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5753 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5754 | return ret; |
| 5755 | } |
| 5756 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5757 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5758 | struct btrfs_key *extent_key, |
| 5759 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5760 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5761 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5762 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 5763 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5764 | u64 start = extent_key->objectid - offset; |
| 5765 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5766 | |
| 5767 | em = alloc_extent_map(GFP_NOFS); |
| 5768 | BUG_ON(!em || IS_ERR(em)); |
| 5769 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5770 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5771 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5772 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5773 | em->block_start = extent_key->objectid; |
| 5774 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5775 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5776 | |
| 5777 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5778 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5779 | while (1) { |
| 5780 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5781 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5782 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5783 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5784 | if (ret != -EEXIST) { |
| 5785 | free_extent_map(em); |
| 5786 | break; |
| 5787 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5788 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5789 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5790 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5791 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5792 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | struct btrfs_ref_path { |
| 5796 | u64 extent_start; |
| 5797 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 5798 | u64 root_objectid; |
| 5799 | u64 root_generation; |
| 5800 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5801 | u32 num_refs; |
| 5802 | int lowest_level; |
| 5803 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5804 | int shared_level; |
| 5805 | |
| 5806 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 5807 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5808 | }; |
| 5809 | |
| 5810 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5811 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5812 | u64 disk_bytenr; |
| 5813 | u64 disk_num_bytes; |
| 5814 | u64 offset; |
| 5815 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5816 | u8 compression; |
| 5817 | u8 encryption; |
| 5818 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5819 | }; |
| 5820 | |
| 5821 | static int is_cowonly_root(u64 root_objectid) |
| 5822 | { |
| 5823 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 5824 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 5825 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 5826 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5827 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5828 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5829 | return 1; |
| 5830 | return 0; |
| 5831 | } |
| 5832 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5833 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5834 | struct btrfs_root *extent_root, |
| 5835 | struct btrfs_ref_path *ref_path, |
| 5836 | int first_time) |
| 5837 | { |
| 5838 | struct extent_buffer *leaf; |
| 5839 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5840 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5841 | struct btrfs_key key; |
| 5842 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5843 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5844 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5845 | int level; |
| 5846 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5847 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5848 | path = btrfs_alloc_path(); |
| 5849 | if (!path) |
| 5850 | return -ENOMEM; |
| 5851 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5852 | if (first_time) { |
| 5853 | ref_path->lowest_level = -1; |
| 5854 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5855 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5856 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5857 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5858 | walk_down: |
| 5859 | level = ref_path->current_level - 1; |
| 5860 | while (level >= -1) { |
| 5861 | u64 parent; |
| 5862 | if (level < ref_path->lowest_level) |
| 5863 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5864 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5865 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5866 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5867 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5868 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5869 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5870 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5871 | parent = ref_path->nodes[level + 1]; |
| 5872 | ref_path->nodes[level + 1] = 0; |
| 5873 | ref_path->current_level = level; |
| 5874 | BUG_ON(parent == 0); |
| 5875 | |
| 5876 | key.objectid = bytenr; |
| 5877 | key.offset = parent + 1; |
| 5878 | key.type = BTRFS_EXTENT_REF_KEY; |
| 5879 | |
| 5880 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5881 | if (ret < 0) |
| 5882 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5883 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5884 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5885 | leaf = path->nodes[0]; |
| 5886 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5887 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5888 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5889 | if (ret < 0) |
| 5890 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5891 | if (ret > 0) |
| 5892 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5893 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5894 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5895 | |
| 5896 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5897 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5898 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 5899 | if (level < ref_path->shared_level) |
| 5900 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5901 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5902 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5903 | next: |
| 5904 | level--; |
| 5905 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5906 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5907 | } |
| 5908 | /* reached lowest level */ |
| 5909 | ret = 1; |
| 5910 | goto out; |
| 5911 | walk_up: |
| 5912 | level = ref_path->current_level; |
| 5913 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 5914 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5915 | |
| 5916 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5917 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5918 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5919 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5920 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5921 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5922 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5923 | key.objectid = bytenr; |
| 5924 | key.offset = 0; |
| 5925 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5926 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5927 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 5928 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5929 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5930 | |
| 5931 | leaf = path->nodes[0]; |
| 5932 | nritems = btrfs_header_nritems(leaf); |
| 5933 | if (path->slots[0] >= nritems) { |
| 5934 | ret = btrfs_next_leaf(extent_root, path); |
| 5935 | if (ret < 0) |
| 5936 | goto out; |
| 5937 | if (ret > 0) { |
| 5938 | /* the extent was freed by someone */ |
| 5939 | if (ref_path->lowest_level == level) |
| 5940 | goto out; |
| 5941 | btrfs_release_path(extent_root, path); |
| 5942 | goto walk_down; |
| 5943 | } |
| 5944 | leaf = path->nodes[0]; |
| 5945 | } |
| 5946 | |
| 5947 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5948 | if (found_key.objectid != bytenr || |
| 5949 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 5950 | /* the extent was freed by someone */ |
| 5951 | if (ref_path->lowest_level == level) { |
| 5952 | ret = 1; |
| 5953 | goto out; |
| 5954 | } |
| 5955 | btrfs_release_path(extent_root, path); |
| 5956 | goto walk_down; |
| 5957 | } |
| 5958 | found: |
| 5959 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 5960 | struct btrfs_extent_ref); |
| 5961 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 5962 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5963 | if (first_time) { |
| 5964 | level = (int)ref_objectid; |
| 5965 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 5966 | ref_path->lowest_level = level; |
| 5967 | ref_path->current_level = level; |
| 5968 | ref_path->nodes[level] = bytenr; |
| 5969 | } else { |
| 5970 | WARN_ON(ref_objectid != level); |
| 5971 | } |
| 5972 | } else { |
| 5973 | WARN_ON(level != -1); |
| 5974 | } |
| 5975 | first_time = 0; |
| 5976 | |
| 5977 | if (ref_path->lowest_level == level) { |
| 5978 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5979 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 5980 | } |
| 5981 | |
| 5982 | /* |
| 5983 | * the block is tree root or the block isn't in reference |
| 5984 | * counted tree. |
| 5985 | */ |
| 5986 | if (found_key.objectid == found_key.offset || |
| 5987 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 5988 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 5989 | ref_path->root_generation = |
| 5990 | btrfs_ref_generation(leaf, ref); |
| 5991 | if (level < 0) { |
| 5992 | /* special reference from the tree log */ |
| 5993 | ref_path->nodes[0] = found_key.offset; |
| 5994 | ref_path->current_level = 0; |
| 5995 | } |
| 5996 | ret = 0; |
| 5997 | goto out; |
| 5998 | } |
| 5999 | |
| 6000 | level++; |
| 6001 | BUG_ON(ref_path->nodes[level] != 0); |
| 6002 | ref_path->nodes[level] = found_key.offset; |
| 6003 | ref_path->current_level = level; |
| 6004 | |
| 6005 | /* |
| 6006 | * the reference was created in the running transaction, |
| 6007 | * no need to continue walking up. |
| 6008 | */ |
| 6009 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6010 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6011 | ref_path->root_generation = |
| 6012 | btrfs_ref_generation(leaf, ref); |
| 6013 | ret = 0; |
| 6014 | goto out; |
| 6015 | } |
| 6016 | |
| 6017 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6018 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6019 | } |
| 6020 | /* reached max tree level, but no tree root found. */ |
| 6021 | BUG(); |
| 6022 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6023 | btrfs_free_path(path); |
| 6024 | return ret; |
| 6025 | } |
| 6026 | |
| 6027 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6028 | struct btrfs_root *extent_root, |
| 6029 | struct btrfs_ref_path *ref_path, |
| 6030 | u64 extent_start) |
| 6031 | { |
| 6032 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6033 | ref_path->extent_start = extent_start; |
| 6034 | |
| 6035 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6036 | } |
| 6037 | |
| 6038 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6039 | struct btrfs_root *extent_root, |
| 6040 | struct btrfs_ref_path *ref_path) |
| 6041 | { |
| 6042 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6043 | } |
| 6044 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6045 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6046 | struct btrfs_key *extent_key, |
| 6047 | u64 offset, int no_fragment, |
| 6048 | struct disk_extent **extents, |
| 6049 | int *nr_extents) |
| 6050 | { |
| 6051 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6052 | struct btrfs_path *path; |
| 6053 | struct btrfs_file_extent_item *fi; |
| 6054 | struct extent_buffer *leaf; |
| 6055 | struct disk_extent *exts = *extents; |
| 6056 | struct btrfs_key found_key; |
| 6057 | u64 cur_pos; |
| 6058 | u64 last_byte; |
| 6059 | u32 nritems; |
| 6060 | int nr = 0; |
| 6061 | int max = *nr_extents; |
| 6062 | int ret; |
| 6063 | |
| 6064 | WARN_ON(!no_fragment && *extents); |
| 6065 | if (!exts) { |
| 6066 | max = 1; |
| 6067 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6068 | if (!exts) |
| 6069 | return -ENOMEM; |
| 6070 | } |
| 6071 | |
| 6072 | path = btrfs_alloc_path(); |
| 6073 | BUG_ON(!path); |
| 6074 | |
| 6075 | cur_pos = extent_key->objectid - offset; |
| 6076 | last_byte = extent_key->objectid + extent_key->offset; |
| 6077 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6078 | cur_pos, 0); |
| 6079 | if (ret < 0) |
| 6080 | goto out; |
| 6081 | if (ret > 0) { |
| 6082 | ret = -ENOENT; |
| 6083 | goto out; |
| 6084 | } |
| 6085 | |
| 6086 | while (1) { |
| 6087 | leaf = path->nodes[0]; |
| 6088 | nritems = btrfs_header_nritems(leaf); |
| 6089 | if (path->slots[0] >= nritems) { |
| 6090 | ret = btrfs_next_leaf(root, path); |
| 6091 | if (ret < 0) |
| 6092 | goto out; |
| 6093 | if (ret > 0) |
| 6094 | break; |
| 6095 | leaf = path->nodes[0]; |
| 6096 | } |
| 6097 | |
| 6098 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6099 | if (found_key.offset != cur_pos || |
| 6100 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6101 | found_key.objectid != reloc_inode->i_ino) |
| 6102 | break; |
| 6103 | |
| 6104 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6105 | struct btrfs_file_extent_item); |
| 6106 | if (btrfs_file_extent_type(leaf, fi) != |
| 6107 | BTRFS_FILE_EXTENT_REG || |
| 6108 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6109 | break; |
| 6110 | |
| 6111 | if (nr == max) { |
| 6112 | struct disk_extent *old = exts; |
| 6113 | max *= 2; |
| 6114 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6115 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6116 | if (old != *extents) |
| 6117 | kfree(old); |
| 6118 | } |
| 6119 | |
| 6120 | exts[nr].disk_bytenr = |
| 6121 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6122 | exts[nr].disk_num_bytes = |
| 6123 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6124 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6125 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6126 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6127 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6128 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6129 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6130 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6131 | BUG_ON(exts[nr].offset > 0); |
| 6132 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6133 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6134 | |
| 6135 | cur_pos += exts[nr].num_bytes; |
| 6136 | nr++; |
| 6137 | |
| 6138 | if (cur_pos + offset >= last_byte) |
| 6139 | break; |
| 6140 | |
| 6141 | if (no_fragment) { |
| 6142 | ret = 1; |
| 6143 | goto out; |
| 6144 | } |
| 6145 | path->slots[0]++; |
| 6146 | } |
| 6147 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6148 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6149 | if (cur_pos + offset < last_byte) { |
| 6150 | ret = -ENOENT; |
| 6151 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6152 | } |
| 6153 | ret = 0; |
| 6154 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6155 | btrfs_free_path(path); |
| 6156 | if (ret) { |
| 6157 | if (exts != *extents) |
| 6158 | kfree(exts); |
| 6159 | } else { |
| 6160 | *extents = exts; |
| 6161 | *nr_extents = nr; |
| 6162 | } |
| 6163 | return ret; |
| 6164 | } |
| 6165 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6166 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6167 | struct btrfs_root *root, |
| 6168 | struct btrfs_path *path, |
| 6169 | struct btrfs_key *extent_key, |
| 6170 | struct btrfs_key *leaf_key, |
| 6171 | struct btrfs_ref_path *ref_path, |
| 6172 | struct disk_extent *new_extents, |
| 6173 | int nr_extents) |
| 6174 | { |
| 6175 | struct extent_buffer *leaf; |
| 6176 | struct btrfs_file_extent_item *fi; |
| 6177 | struct inode *inode = NULL; |
| 6178 | struct btrfs_key key; |
| 6179 | u64 lock_start = 0; |
| 6180 | u64 lock_end = 0; |
| 6181 | u64 num_bytes; |
| 6182 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6183 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6184 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6185 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6186 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6187 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6188 | int ret; |
| 6189 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6190 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6191 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6192 | if (key.objectid < ref_path->owner_objectid || |
| 6193 | (key.objectid == ref_path->owner_objectid && |
| 6194 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6195 | key.objectid = ref_path->owner_objectid; |
| 6196 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6197 | key.offset = 0; |
| 6198 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | while (1) { |
| 6202 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6203 | if (ret < 0) |
| 6204 | goto out; |
| 6205 | |
| 6206 | leaf = path->nodes[0]; |
| 6207 | nritems = btrfs_header_nritems(leaf); |
| 6208 | next: |
| 6209 | if (extent_locked && ret > 0) { |
| 6210 | /* |
| 6211 | * the file extent item was modified by someone |
| 6212 | * before the extent got locked. |
| 6213 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6214 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6215 | lock_end, GFP_NOFS); |
| 6216 | extent_locked = 0; |
| 6217 | } |
| 6218 | |
| 6219 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6220 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6221 | break; |
| 6222 | |
| 6223 | BUG_ON(extent_locked); |
| 6224 | ret = btrfs_next_leaf(root, path); |
| 6225 | if (ret < 0) |
| 6226 | goto out; |
| 6227 | if (ret > 0) |
| 6228 | break; |
| 6229 | leaf = path->nodes[0]; |
| 6230 | nritems = btrfs_header_nritems(leaf); |
| 6231 | } |
| 6232 | |
| 6233 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 6234 | |
| 6235 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 6236 | if ((key.objectid > ref_path->owner_objectid) || |
| 6237 | (key.objectid == ref_path->owner_objectid && |
| 6238 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6239 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6240 | break; |
| 6241 | } |
| 6242 | |
| 6243 | if (inode && key.objectid != inode->i_ino) { |
| 6244 | BUG_ON(extent_locked); |
| 6245 | btrfs_release_path(root, path); |
| 6246 | mutex_unlock(&inode->i_mutex); |
| 6247 | iput(inode); |
| 6248 | inode = NULL; |
| 6249 | continue; |
| 6250 | } |
| 6251 | |
| 6252 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6253 | path->slots[0]++; |
| 6254 | ret = 1; |
| 6255 | goto next; |
| 6256 | } |
| 6257 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6258 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6259 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 6260 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 6261 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6262 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 6263 | extent_key->objectid)) { |
| 6264 | path->slots[0]++; |
| 6265 | ret = 1; |
| 6266 | goto next; |
| 6267 | } |
| 6268 | |
| 6269 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6270 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 6271 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6272 | if (search_end == (u64)-1) { |
| 6273 | search_end = key.offset - ext_offset + |
| 6274 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 6275 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6276 | |
| 6277 | if (!extent_locked) { |
| 6278 | lock_start = key.offset; |
| 6279 | lock_end = lock_start + num_bytes - 1; |
| 6280 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6281 | if (lock_start > key.offset || |
| 6282 | lock_end + 1 < key.offset + num_bytes) { |
| 6283 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6284 | lock_start, lock_end, GFP_NOFS); |
| 6285 | extent_locked = 0; |
| 6286 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6287 | } |
| 6288 | |
| 6289 | if (!inode) { |
| 6290 | btrfs_release_path(root, path); |
| 6291 | |
| 6292 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 6293 | key.objectid, root); |
| 6294 | if (inode->i_state & I_NEW) { |
| 6295 | BTRFS_I(inode)->root = root; |
| 6296 | BTRFS_I(inode)->location.objectid = |
| 6297 | key.objectid; |
| 6298 | BTRFS_I(inode)->location.type = |
| 6299 | BTRFS_INODE_ITEM_KEY; |
| 6300 | BTRFS_I(inode)->location.offset = 0; |
| 6301 | btrfs_read_locked_inode(inode); |
| 6302 | unlock_new_inode(inode); |
| 6303 | } |
| 6304 | /* |
| 6305 | * some code call btrfs_commit_transaction while |
| 6306 | * holding the i_mutex, so we can't use mutex_lock |
| 6307 | * here. |
| 6308 | */ |
| 6309 | if (is_bad_inode(inode) || |
| 6310 | !mutex_trylock(&inode->i_mutex)) { |
| 6311 | iput(inode); |
| 6312 | inode = NULL; |
| 6313 | key.offset = (u64)-1; |
| 6314 | goto skip; |
| 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | if (!extent_locked) { |
| 6319 | struct btrfs_ordered_extent *ordered; |
| 6320 | |
| 6321 | btrfs_release_path(root, path); |
| 6322 | |
| 6323 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6324 | lock_end, GFP_NOFS); |
| 6325 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6326 | lock_end); |
| 6327 | if (ordered && |
| 6328 | ordered->file_offset <= lock_end && |
| 6329 | ordered->file_offset + ordered->len > lock_start) { |
| 6330 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6331 | lock_start, lock_end, GFP_NOFS); |
| 6332 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6333 | btrfs_put_ordered_extent(ordered); |
| 6334 | key.offset += num_bytes; |
| 6335 | goto skip; |
| 6336 | } |
| 6337 | if (ordered) |
| 6338 | btrfs_put_ordered_extent(ordered); |
| 6339 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6340 | extent_locked = 1; |
| 6341 | continue; |
| 6342 | } |
| 6343 | |
| 6344 | if (nr_extents == 1) { |
| 6345 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6346 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6347 | new_extents[0].disk_bytenr); |
| 6348 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6349 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6350 | btrfs_mark_buffer_dirty(leaf); |
| 6351 | |
| 6352 | btrfs_drop_extent_cache(inode, key.offset, |
| 6353 | key.offset + num_bytes - 1, 0); |
| 6354 | |
| 6355 | ret = btrfs_inc_extent_ref(trans, root, |
| 6356 | new_extents[0].disk_bytenr, |
| 6357 | new_extents[0].disk_num_bytes, |
| 6358 | leaf->start, |
| 6359 | root->root_key.objectid, |
| 6360 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6361 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6362 | BUG_ON(ret); |
| 6363 | |
| 6364 | ret = btrfs_free_extent(trans, root, |
| 6365 | extent_key->objectid, |
| 6366 | extent_key->offset, |
| 6367 | leaf->start, |
| 6368 | btrfs_header_owner(leaf), |
| 6369 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6370 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6371 | BUG_ON(ret); |
| 6372 | |
| 6373 | btrfs_release_path(root, path); |
| 6374 | key.offset += num_bytes; |
| 6375 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6376 | BUG_ON(1); |
| 6377 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6378 | u64 alloc_hint; |
| 6379 | u64 extent_len; |
| 6380 | int i; |
| 6381 | /* |
| 6382 | * drop old extent pointer at first, then insert the |
| 6383 | * new pointers one bye one |
| 6384 | */ |
| 6385 | btrfs_release_path(root, path); |
| 6386 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 6387 | key.offset + num_bytes, |
| 6388 | key.offset, &alloc_hint); |
| 6389 | BUG_ON(ret); |
| 6390 | |
| 6391 | for (i = 0; i < nr_extents; i++) { |
| 6392 | if (ext_offset >= new_extents[i].num_bytes) { |
| 6393 | ext_offset -= new_extents[i].num_bytes; |
| 6394 | continue; |
| 6395 | } |
| 6396 | extent_len = min(new_extents[i].num_bytes - |
| 6397 | ext_offset, num_bytes); |
| 6398 | |
| 6399 | ret = btrfs_insert_empty_item(trans, root, |
| 6400 | path, &key, |
| 6401 | sizeof(*fi)); |
| 6402 | BUG_ON(ret); |
| 6403 | |
| 6404 | leaf = path->nodes[0]; |
| 6405 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6406 | struct btrfs_file_extent_item); |
| 6407 | btrfs_set_file_extent_generation(leaf, fi, |
| 6408 | trans->transid); |
| 6409 | btrfs_set_file_extent_type(leaf, fi, |
| 6410 | BTRFS_FILE_EXTENT_REG); |
| 6411 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6412 | new_extents[i].disk_bytenr); |
| 6413 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6414 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6415 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 6416 | new_extents[i].ram_bytes); |
| 6417 | |
| 6418 | btrfs_set_file_extent_compression(leaf, fi, |
| 6419 | new_extents[i].compression); |
| 6420 | btrfs_set_file_extent_encryption(leaf, fi, |
| 6421 | new_extents[i].encryption); |
| 6422 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 6423 | new_extents[i].other_encoding); |
| 6424 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6425 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 6426 | extent_len); |
| 6427 | ext_offset += new_extents[i].offset; |
| 6428 | btrfs_set_file_extent_offset(leaf, fi, |
| 6429 | ext_offset); |
| 6430 | btrfs_mark_buffer_dirty(leaf); |
| 6431 | |
| 6432 | btrfs_drop_extent_cache(inode, key.offset, |
| 6433 | key.offset + extent_len - 1, 0); |
| 6434 | |
| 6435 | ret = btrfs_inc_extent_ref(trans, root, |
| 6436 | new_extents[i].disk_bytenr, |
| 6437 | new_extents[i].disk_num_bytes, |
| 6438 | leaf->start, |
| 6439 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6440 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6441 | BUG_ON(ret); |
| 6442 | btrfs_release_path(root, path); |
| 6443 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6444 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6445 | |
| 6446 | ext_offset = 0; |
| 6447 | num_bytes -= extent_len; |
| 6448 | key.offset += extent_len; |
| 6449 | |
| 6450 | if (num_bytes == 0) |
| 6451 | break; |
| 6452 | } |
| 6453 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6454 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6455 | } |
| 6456 | |
| 6457 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6458 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6459 | lock_end, GFP_NOFS); |
| 6460 | extent_locked = 0; |
| 6461 | } |
| 6462 | skip: |
| 6463 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6464 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6465 | break; |
| 6466 | |
| 6467 | cond_resched(); |
| 6468 | } |
| 6469 | ret = 0; |
| 6470 | out: |
| 6471 | btrfs_release_path(root, path); |
| 6472 | if (inode) { |
| 6473 | mutex_unlock(&inode->i_mutex); |
| 6474 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6475 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6476 | lock_end, GFP_NOFS); |
| 6477 | } |
| 6478 | iput(inode); |
| 6479 | } |
| 6480 | return ret; |
| 6481 | } |
| 6482 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6483 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 6484 | struct btrfs_root *root, |
| 6485 | struct extent_buffer *buf, u64 orig_start) |
| 6486 | { |
| 6487 | int level; |
| 6488 | int ret; |
| 6489 | |
| 6490 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 6491 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6492 | |
| 6493 | level = btrfs_header_level(buf); |
| 6494 | if (level == 0) { |
| 6495 | struct btrfs_leaf_ref *ref; |
| 6496 | struct btrfs_leaf_ref *orig_ref; |
| 6497 | |
| 6498 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 6499 | if (!orig_ref) |
| 6500 | return -ENOENT; |
| 6501 | |
| 6502 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 6503 | if (!ref) { |
| 6504 | btrfs_free_leaf_ref(root, orig_ref); |
| 6505 | return -ENOMEM; |
| 6506 | } |
| 6507 | |
| 6508 | ref->nritems = orig_ref->nritems; |
| 6509 | memcpy(ref->extents, orig_ref->extents, |
| 6510 | sizeof(ref->extents[0]) * ref->nritems); |
| 6511 | |
| 6512 | btrfs_free_leaf_ref(root, orig_ref); |
| 6513 | |
| 6514 | ref->root_gen = trans->transid; |
| 6515 | ref->bytenr = buf->start; |
| 6516 | ref->owner = btrfs_header_owner(buf); |
| 6517 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6518 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6519 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 6520 | WARN_ON(ret); |
| 6521 | btrfs_free_leaf_ref(root, ref); |
| 6522 | } |
| 6523 | return 0; |
| 6524 | } |
| 6525 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6526 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6527 | struct extent_buffer *leaf, |
| 6528 | struct btrfs_block_group_cache *group, |
| 6529 | struct btrfs_root *target_root) |
| 6530 | { |
| 6531 | struct btrfs_key key; |
| 6532 | struct inode *inode = NULL; |
| 6533 | struct btrfs_file_extent_item *fi; |
| 6534 | u64 num_bytes; |
| 6535 | u64 skip_objectid = 0; |
| 6536 | u32 nritems; |
| 6537 | u32 i; |
| 6538 | |
| 6539 | nritems = btrfs_header_nritems(leaf); |
| 6540 | for (i = 0; i < nritems; i++) { |
| 6541 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6542 | if (key.objectid == skip_objectid || |
| 6543 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 6544 | continue; |
| 6545 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6546 | if (btrfs_file_extent_type(leaf, fi) == |
| 6547 | BTRFS_FILE_EXTENT_INLINE) |
| 6548 | continue; |
| 6549 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6550 | continue; |
| 6551 | if (!inode || inode->i_ino != key.objectid) { |
| 6552 | iput(inode); |
| 6553 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 6554 | key.objectid, target_root, 1); |
| 6555 | } |
| 6556 | if (!inode) { |
| 6557 | skip_objectid = key.objectid; |
| 6558 | continue; |
| 6559 | } |
| 6560 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6561 | |
| 6562 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 6563 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6564 | btrfs_drop_extent_cache(inode, key.offset, |
| 6565 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6566 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 6567 | key.offset + num_bytes - 1, GFP_NOFS); |
| 6568 | cond_resched(); |
| 6569 | } |
| 6570 | iput(inode); |
| 6571 | return 0; |
| 6572 | } |
| 6573 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6574 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6575 | struct btrfs_root *root, |
| 6576 | struct extent_buffer *leaf, |
| 6577 | struct btrfs_block_group_cache *group, |
| 6578 | struct inode *reloc_inode) |
| 6579 | { |
| 6580 | struct btrfs_key key; |
| 6581 | struct btrfs_key extent_key; |
| 6582 | struct btrfs_file_extent_item *fi; |
| 6583 | struct btrfs_leaf_ref *ref; |
| 6584 | struct disk_extent *new_extent; |
| 6585 | u64 bytenr; |
| 6586 | u64 num_bytes; |
| 6587 | u32 nritems; |
| 6588 | u32 i; |
| 6589 | int ext_index; |
| 6590 | int nr_extent; |
| 6591 | int ret; |
| 6592 | |
| 6593 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 6594 | BUG_ON(!new_extent); |
| 6595 | |
| 6596 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 6597 | BUG_ON(!ref); |
| 6598 | |
| 6599 | ext_index = -1; |
| 6600 | nritems = btrfs_header_nritems(leaf); |
| 6601 | for (i = 0; i < nritems; i++) { |
| 6602 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6603 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 6604 | continue; |
| 6605 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6606 | if (btrfs_file_extent_type(leaf, fi) == |
| 6607 | BTRFS_FILE_EXTENT_INLINE) |
| 6608 | continue; |
| 6609 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6610 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6611 | if (bytenr == 0) |
| 6612 | continue; |
| 6613 | |
| 6614 | ext_index++; |
| 6615 | if (bytenr >= group->key.objectid + group->key.offset || |
| 6616 | bytenr + num_bytes <= group->key.objectid) |
| 6617 | continue; |
| 6618 | |
| 6619 | extent_key.objectid = bytenr; |
| 6620 | extent_key.offset = num_bytes; |
| 6621 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6622 | nr_extent = 1; |
| 6623 | ret = get_new_locations(reloc_inode, &extent_key, |
| 6624 | group->key.objectid, 1, |
| 6625 | &new_extent, &nr_extent); |
| 6626 | if (ret > 0) |
| 6627 | continue; |
| 6628 | BUG_ON(ret < 0); |
| 6629 | |
| 6630 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 6631 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 6632 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 6633 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 6634 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6635 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6636 | new_extent->disk_bytenr); |
| 6637 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6638 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6639 | btrfs_mark_buffer_dirty(leaf); |
| 6640 | |
| 6641 | ret = btrfs_inc_extent_ref(trans, root, |
| 6642 | new_extent->disk_bytenr, |
| 6643 | new_extent->disk_num_bytes, |
| 6644 | leaf->start, |
| 6645 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6646 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6647 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6648 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6649 | ret = btrfs_free_extent(trans, root, |
| 6650 | bytenr, num_bytes, leaf->start, |
| 6651 | btrfs_header_owner(leaf), |
| 6652 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6653 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6654 | BUG_ON(ret); |
| 6655 | cond_resched(); |
| 6656 | } |
| 6657 | kfree(new_extent); |
| 6658 | BUG_ON(ext_index + 1 != ref->nritems); |
| 6659 | btrfs_free_leaf_ref(root, ref); |
| 6660 | return 0; |
| 6661 | } |
| 6662 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6663 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 6664 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6665 | { |
| 6666 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6667 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6668 | |
| 6669 | if (root->reloc_root) { |
| 6670 | reloc_root = root->reloc_root; |
| 6671 | root->reloc_root = NULL; |
| 6672 | list_add(&reloc_root->dead_list, |
| 6673 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6674 | |
| 6675 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 6676 | reloc_root->node->start); |
| 6677 | btrfs_set_root_level(&root->root_item, |
| 6678 | btrfs_header_level(reloc_root->node)); |
| 6679 | memset(&reloc_root->root_item.drop_progress, 0, |
| 6680 | sizeof(struct btrfs_disk_key)); |
| 6681 | reloc_root->root_item.drop_level = 0; |
| 6682 | |
| 6683 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 6684 | &reloc_root->root_key, |
| 6685 | &reloc_root->root_item); |
| 6686 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6687 | } |
| 6688 | return 0; |
| 6689 | } |
| 6690 | |
| 6691 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 6692 | { |
| 6693 | struct btrfs_trans_handle *trans; |
| 6694 | struct btrfs_root *reloc_root; |
| 6695 | struct btrfs_root *prev_root = NULL; |
| 6696 | struct list_head dead_roots; |
| 6697 | int ret; |
| 6698 | unsigned long nr; |
| 6699 | |
| 6700 | INIT_LIST_HEAD(&dead_roots); |
| 6701 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 6702 | |
| 6703 | while (!list_empty(&dead_roots)) { |
| 6704 | reloc_root = list_entry(dead_roots.prev, |
| 6705 | struct btrfs_root, dead_list); |
| 6706 | list_del_init(&reloc_root->dead_list); |
| 6707 | |
| 6708 | BUG_ON(reloc_root->commit_root != NULL); |
| 6709 | while (1) { |
| 6710 | trans = btrfs_join_transaction(root, 1); |
| 6711 | BUG_ON(!trans); |
| 6712 | |
| 6713 | mutex_lock(&root->fs_info->drop_mutex); |
| 6714 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 6715 | if (ret != -EAGAIN) |
| 6716 | break; |
| 6717 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6718 | |
| 6719 | nr = trans->blocks_used; |
| 6720 | ret = btrfs_end_transaction(trans, root); |
| 6721 | BUG_ON(ret); |
| 6722 | btrfs_btree_balance_dirty(root, nr); |
| 6723 | } |
| 6724 | |
| 6725 | free_extent_buffer(reloc_root->node); |
| 6726 | |
| 6727 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 6728 | &reloc_root->root_key); |
| 6729 | BUG_ON(ret); |
| 6730 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6731 | |
| 6732 | nr = trans->blocks_used; |
| 6733 | ret = btrfs_end_transaction(trans, root); |
| 6734 | BUG_ON(ret); |
| 6735 | btrfs_btree_balance_dirty(root, nr); |
| 6736 | |
| 6737 | kfree(prev_root); |
| 6738 | prev_root = reloc_root; |
| 6739 | } |
| 6740 | if (prev_root) { |
| 6741 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 6742 | kfree(prev_root); |
| 6743 | } |
| 6744 | return 0; |
| 6745 | } |
| 6746 | |
| 6747 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 6748 | { |
| 6749 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 6750 | return 0; |
| 6751 | } |
| 6752 | |
| 6753 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 6754 | { |
| 6755 | struct btrfs_root *reloc_root; |
| 6756 | struct btrfs_trans_handle *trans; |
| 6757 | struct btrfs_key location; |
| 6758 | int found; |
| 6759 | int ret; |
| 6760 | |
| 6761 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6762 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 6763 | BUG_ON(ret); |
| 6764 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 6765 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 6766 | |
| 6767 | if (found) { |
| 6768 | trans = btrfs_start_transaction(root, 1); |
| 6769 | BUG_ON(!trans); |
| 6770 | ret = btrfs_commit_transaction(trans, root); |
| 6771 | BUG_ON(ret); |
| 6772 | } |
| 6773 | |
| 6774 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6775 | location.offset = (u64)-1; |
| 6776 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 6777 | |
| 6778 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 6779 | BUG_ON(!reloc_root); |
| 6780 | btrfs_orphan_cleanup(reloc_root); |
| 6781 | return 0; |
| 6782 | } |
| 6783 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6784 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6785 | struct btrfs_root *root) |
| 6786 | { |
| 6787 | struct btrfs_root *reloc_root; |
| 6788 | struct extent_buffer *eb; |
| 6789 | struct btrfs_root_item *root_item; |
| 6790 | struct btrfs_key root_key; |
| 6791 | int ret; |
| 6792 | |
| 6793 | BUG_ON(!root->ref_cows); |
| 6794 | if (root->reloc_root) |
| 6795 | return 0; |
| 6796 | |
| 6797 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 6798 | BUG_ON(!root_item); |
| 6799 | |
| 6800 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 6801 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 6802 | BUG_ON(ret); |
| 6803 | |
| 6804 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 6805 | root_key.offset = root->root_key.objectid; |
| 6806 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6807 | |
| 6808 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 6809 | btrfs_set_root_refs(root_item, 0); |
| 6810 | btrfs_set_root_bytenr(root_item, eb->start); |
| 6811 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6812 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6813 | |
| 6814 | btrfs_tree_unlock(eb); |
| 6815 | free_extent_buffer(eb); |
| 6816 | |
| 6817 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 6818 | &root_key, root_item); |
| 6819 | BUG_ON(ret); |
| 6820 | kfree(root_item); |
| 6821 | |
| 6822 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 6823 | &root_key); |
| 6824 | BUG_ON(!reloc_root); |
| 6825 | reloc_root->last_trans = trans->transid; |
| 6826 | reloc_root->commit_root = NULL; |
| 6827 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 6828 | |
| 6829 | root->reloc_root = reloc_root; |
| 6830 | return 0; |
| 6831 | } |
| 6832 | |
| 6833 | /* |
| 6834 | * Core function of space balance. |
| 6835 | * |
| 6836 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6837 | * counted roots. There is one reloc tree for each subvol, and all |
| 6838 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 6839 | * of the latest committed roots of subvols (root->commit_root). |
| 6840 | * |
| 6841 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 6842 | * COW the block through subvol's reloc tree, then update block pointer |
| 6843 | * in the subvol to point to the new block. Since all reloc trees share |
| 6844 | * same root key objectid, doing special handing for tree blocks owned |
| 6845 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 6846 | * we can use the resulting new block directly when the same block is |
| 6847 | * required to COW again through other reloc trees. By this way, relocated |
| 6848 | * tree blocks are shared between reloc trees, so they are also shared |
| 6849 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6850 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6851 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6852 | struct btrfs_root *root, |
| 6853 | struct btrfs_path *path, |
| 6854 | struct btrfs_key *first_key, |
| 6855 | struct btrfs_ref_path *ref_path, |
| 6856 | struct btrfs_block_group_cache *group, |
| 6857 | struct inode *reloc_inode) |
| 6858 | { |
| 6859 | struct btrfs_root *reloc_root; |
| 6860 | struct extent_buffer *eb = NULL; |
| 6861 | struct btrfs_key *keys; |
| 6862 | u64 *nodes; |
| 6863 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6864 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6865 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6866 | int ret; |
| 6867 | |
| 6868 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 6869 | lowest_level = ref_path->owner_objectid; |
| 6870 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6871 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6872 | path->lowest_level = lowest_level; |
| 6873 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 6874 | BUG_ON(ret < 0); |
| 6875 | path->lowest_level = 0; |
| 6876 | btrfs_release_path(root, path); |
| 6877 | return 0; |
| 6878 | } |
| 6879 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6880 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6881 | ret = init_reloc_tree(trans, root); |
| 6882 | BUG_ON(ret); |
| 6883 | reloc_root = root->reloc_root; |
| 6884 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6885 | shared_level = ref_path->shared_level; |
| 6886 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6887 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6888 | keys = ref_path->node_keys; |
| 6889 | nodes = ref_path->new_nodes; |
| 6890 | memset(&keys[shared_level + 1], 0, |
| 6891 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 6892 | memset(&nodes[shared_level + 1], 0, |
| 6893 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6894 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6895 | if (nodes[lowest_level] == 0) { |
| 6896 | path->lowest_level = lowest_level; |
| 6897 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6898 | 0, 1); |
| 6899 | BUG_ON(ret); |
| 6900 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 6901 | eb = path->nodes[level]; |
| 6902 | if (!eb || eb == reloc_root->node) |
| 6903 | break; |
| 6904 | nodes[level] = eb->start; |
| 6905 | if (level == 0) |
| 6906 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 6907 | else |
| 6908 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 6909 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6910 | if (nodes[0] && |
| 6911 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6912 | eb = path->nodes[0]; |
| 6913 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 6914 | group, reloc_inode); |
| 6915 | BUG_ON(ret); |
| 6916 | } |
| 6917 | btrfs_release_path(reloc_root, path); |
| 6918 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6919 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6920 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6921 | BUG_ON(ret); |
| 6922 | } |
| 6923 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6924 | /* |
| 6925 | * replace tree blocks in the fs tree with tree blocks in |
| 6926 | * the reloc tree. |
| 6927 | */ |
| 6928 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 6929 | BUG_ON(ret < 0); |
| 6930 | |
| 6931 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6932 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6933 | 0, 0); |
| 6934 | BUG_ON(ret); |
| 6935 | extent_buffer_get(path->nodes[0]); |
| 6936 | eb = path->nodes[0]; |
| 6937 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6938 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 6939 | BUG_ON(ret); |
| 6940 | free_extent_buffer(eb); |
| 6941 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6942 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6943 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6944 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6945 | return 0; |
| 6946 | } |
| 6947 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6948 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6949 | struct btrfs_root *root, |
| 6950 | struct btrfs_path *path, |
| 6951 | struct btrfs_key *first_key, |
| 6952 | struct btrfs_ref_path *ref_path) |
| 6953 | { |
| 6954 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6955 | |
| 6956 | ret = relocate_one_path(trans, root, path, first_key, |
| 6957 | ref_path, NULL, NULL); |
| 6958 | BUG_ON(ret); |
| 6959 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6960 | return 0; |
| 6961 | } |
| 6962 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6963 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6964 | struct btrfs_root *extent_root, |
| 6965 | struct btrfs_path *path, |
| 6966 | struct btrfs_key *extent_key) |
| 6967 | { |
| 6968 | int ret; |
| 6969 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6970 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 6971 | if (ret) |
| 6972 | goto out; |
| 6973 | ret = btrfs_del_item(trans, extent_root, path); |
| 6974 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6975 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6976 | return ret; |
| 6977 | } |
| 6978 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6979 | 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] | 6980 | struct btrfs_ref_path *ref_path) |
| 6981 | { |
| 6982 | struct btrfs_key root_key; |
| 6983 | |
| 6984 | root_key.objectid = ref_path->root_objectid; |
| 6985 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6986 | if (is_cowonly_root(ref_path->root_objectid)) |
| 6987 | root_key.offset = 0; |
| 6988 | else |
| 6989 | root_key.offset = (u64)-1; |
| 6990 | |
| 6991 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 6992 | } |
| 6993 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6994 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6995 | struct btrfs_path *path, |
| 6996 | struct btrfs_key *extent_key, |
| 6997 | struct btrfs_block_group_cache *group, |
| 6998 | struct inode *reloc_inode, int pass) |
| 6999 | { |
| 7000 | struct btrfs_trans_handle *trans; |
| 7001 | struct btrfs_root *found_root; |
| 7002 | struct btrfs_ref_path *ref_path = NULL; |
| 7003 | struct disk_extent *new_extents = NULL; |
| 7004 | int nr_extents = 0; |
| 7005 | int loops; |
| 7006 | int ret; |
| 7007 | int level; |
| 7008 | struct btrfs_key first_key; |
| 7009 | u64 prev_block = 0; |
| 7010 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7011 | |
| 7012 | trans = btrfs_start_transaction(extent_root, 1); |
| 7013 | BUG_ON(!trans); |
| 7014 | |
| 7015 | if (extent_key->objectid == 0) { |
| 7016 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7017 | goto out; |
| 7018 | } |
| 7019 | |
| 7020 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7021 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7022 | ret = -ENOMEM; |
| 7023 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7024 | } |
| 7025 | |
| 7026 | for (loops = 0; ; loops++) { |
| 7027 | if (loops == 0) { |
| 7028 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7029 | extent_key->objectid); |
| 7030 | } else { |
| 7031 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7032 | } |
| 7033 | if (ret < 0) |
| 7034 | goto out; |
| 7035 | if (ret > 0) |
| 7036 | break; |
| 7037 | |
| 7038 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7039 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7040 | continue; |
| 7041 | |
| 7042 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7043 | BUG_ON(!found_root); |
| 7044 | /* |
| 7045 | * for reference counted tree, only process reference paths |
| 7046 | * rooted at the latest committed root. |
| 7047 | */ |
| 7048 | if (found_root->ref_cows && |
| 7049 | ref_path->root_generation != found_root->root_key.offset) |
| 7050 | continue; |
| 7051 | |
| 7052 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7053 | if (pass == 0) { |
| 7054 | /* |
| 7055 | * copy data extents to new locations |
| 7056 | */ |
| 7057 | u64 group_start = group->key.objectid; |
| 7058 | ret = relocate_data_extent(reloc_inode, |
| 7059 | extent_key, |
| 7060 | group_start); |
| 7061 | if (ret < 0) |
| 7062 | goto out; |
| 7063 | break; |
| 7064 | } |
| 7065 | level = 0; |
| 7066 | } else { |
| 7067 | level = ref_path->owner_objectid; |
| 7068 | } |
| 7069 | |
| 7070 | if (prev_block != ref_path->nodes[level]) { |
| 7071 | struct extent_buffer *eb; |
| 7072 | u64 block_start = ref_path->nodes[level]; |
| 7073 | u64 block_size = btrfs_level_size(found_root, level); |
| 7074 | |
| 7075 | eb = read_tree_block(found_root, block_start, |
| 7076 | block_size, 0); |
| 7077 | btrfs_tree_lock(eb); |
| 7078 | BUG_ON(level != btrfs_header_level(eb)); |
| 7079 | |
| 7080 | if (level == 0) |
| 7081 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7082 | else |
| 7083 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7084 | |
| 7085 | btrfs_tree_unlock(eb); |
| 7086 | free_extent_buffer(eb); |
| 7087 | prev_block = block_start; |
| 7088 | } |
| 7089 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7090 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7091 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7092 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7093 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7094 | /* |
| 7095 | * try to update data extent references while |
| 7096 | * keeping metadata shared between snapshots. |
| 7097 | */ |
| 7098 | if (pass == 1) { |
| 7099 | ret = relocate_one_path(trans, found_root, |
| 7100 | path, &first_key, ref_path, |
| 7101 | group, reloc_inode); |
| 7102 | if (ret < 0) |
| 7103 | goto out; |
| 7104 | continue; |
| 7105 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7106 | /* |
| 7107 | * use fallback method to process the remaining |
| 7108 | * references. |
| 7109 | */ |
| 7110 | if (!new_extents) { |
| 7111 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7112 | new_extents = kmalloc(sizeof(*new_extents), |
| 7113 | GFP_NOFS); |
| 7114 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7115 | ret = get_new_locations(reloc_inode, |
| 7116 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7117 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7118 | &new_extents, |
| 7119 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7120 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7121 | goto out; |
| 7122 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7123 | ret = replace_one_extent(trans, found_root, |
| 7124 | path, extent_key, |
| 7125 | &first_key, ref_path, |
| 7126 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7127 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7128 | ret = relocate_tree_block(trans, found_root, path, |
| 7129 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7130 | } |
| 7131 | if (ret < 0) |
| 7132 | goto out; |
| 7133 | } |
| 7134 | ret = 0; |
| 7135 | out: |
| 7136 | btrfs_end_transaction(trans, extent_root); |
| 7137 | kfree(new_extents); |
| 7138 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7139 | return ret; |
| 7140 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7141 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7142 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7143 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7144 | { |
| 7145 | u64 num_devices; |
| 7146 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7147 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7148 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7149 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7150 | if (num_devices == 1) { |
| 7151 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7152 | stripped = flags & ~stripped; |
| 7153 | |
| 7154 | /* turn raid0 into single device chunks */ |
| 7155 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7156 | return stripped; |
| 7157 | |
| 7158 | /* turn mirroring into duplication */ |
| 7159 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7160 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7161 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7162 | return flags; |
| 7163 | } else { |
| 7164 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7165 | if (flags & stripped) |
| 7166 | return flags; |
| 7167 | |
| 7168 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7169 | stripped = flags & ~stripped; |
| 7170 | |
| 7171 | /* switch duplicated blocks with raid1 */ |
| 7172 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7173 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7174 | |
| 7175 | /* turn single device chunks into raid0 */ |
| 7176 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7177 | } |
| 7178 | return flags; |
| 7179 | } |
| 7180 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7181 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7182 | struct btrfs_block_group_cache *shrink_block_group, |
| 7183 | int force) |
| 7184 | { |
| 7185 | struct btrfs_trans_handle *trans; |
| 7186 | u64 new_alloc_flags; |
| 7187 | u64 calc; |
| 7188 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7189 | spin_lock(&shrink_block_group->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7190 | if (btrfs_block_group_used(&shrink_block_group->item) + |
| 7191 | shrink_block_group->reserved > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7192 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7193 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7194 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7195 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7196 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7197 | new_alloc_flags = update_block_group_flags(root, |
| 7198 | shrink_block_group->flags); |
| 7199 | if (new_alloc_flags != shrink_block_group->flags) { |
| 7200 | calc = |
| 7201 | btrfs_block_group_used(&shrink_block_group->item); |
| 7202 | } else { |
| 7203 | calc = shrink_block_group->key.offset; |
| 7204 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7205 | spin_unlock(&shrink_block_group->lock); |
| 7206 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7207 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 7208 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7209 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7210 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7211 | } else |
| 7212 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7213 | return 0; |
| 7214 | } |
| 7215 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7216 | |
| 7217 | int btrfs_prepare_block_group_relocation(struct btrfs_root *root, |
| 7218 | struct btrfs_block_group_cache *group) |
| 7219 | |
| 7220 | { |
| 7221 | __alloc_chunk_for_shrink(root, group, 1); |
| 7222 | set_block_group_readonly(group); |
| 7223 | return 0; |
| 7224 | } |
| 7225 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7226 | /* |
| 7227 | * checks to see if its even possible to relocate this block group. |
| 7228 | * |
| 7229 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7230 | * ok to go ahead and try. |
| 7231 | */ |
| 7232 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7233 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7234 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7235 | struct btrfs_space_info *space_info; |
| 7236 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7237 | struct btrfs_device *device; |
| 7238 | int full = 0; |
| 7239 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7240 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7241 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7242 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7243 | /* odd, couldn't find the block group, leave it alone */ |
| 7244 | if (!block_group) |
| 7245 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7246 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7247 | /* no bytes used, we're good */ |
| 7248 | if (!btrfs_block_group_used(&block_group->item)) |
| 7249 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7250 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7251 | space_info = block_group->space_info; |
| 7252 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7253 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7254 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7255 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7256 | /* |
| 7257 | * 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] | 7258 | * relocate it unless we're able to allocate a new chunk below. |
| 7259 | * |
| 7260 | * Otherwise, we need to make sure we have room in the space to handle |
| 7261 | * 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] | 7262 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7263 | if ((space_info->total_bytes != block_group->key.offset) && |
| 7264 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7265 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7266 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7267 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7268 | spin_unlock(&space_info->lock); |
| 7269 | goto out; |
| 7270 | } |
| 7271 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7272 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7273 | /* |
| 7274 | * ok we don't have enough space, but maybe we have free space on our |
| 7275 | * devices to allocate new chunks for relocation, so loop through our |
| 7276 | * alloc devices and guess if we have enough space. However, if we |
| 7277 | * were marked as full, then we know there aren't enough chunks, and we |
| 7278 | * can just return. |
| 7279 | */ |
| 7280 | ret = -1; |
| 7281 | if (full) |
| 7282 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 7283 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7284 | mutex_lock(&root->fs_info->chunk_mutex); |
| 7285 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 7286 | u64 min_free = btrfs_block_group_used(&block_group->item); |
| 7287 | u64 dev_offset, max_avail; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7288 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7289 | /* |
| 7290 | * check to make sure we can actually find a chunk with enough |
| 7291 | * space to fit our block group in. |
| 7292 | */ |
| 7293 | if (device->total_bytes > device->bytes_used + min_free) { |
| 7294 | ret = find_free_dev_extent(NULL, device, min_free, |
| 7295 | &dev_offset, &max_avail); |
| 7296 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7297 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7298 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7299 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7300 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7301 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7302 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7303 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7304 | return ret; |
| 7305 | } |
| 7306 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7307 | static int find_first_block_group(struct btrfs_root *root, |
| 7308 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7309 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7310 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7311 | struct btrfs_key found_key; |
| 7312 | struct extent_buffer *leaf; |
| 7313 | int slot; |
| 7314 | |
| 7315 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7316 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7317 | goto out; |
| 7318 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7319 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7320 | slot = path->slots[0]; |
| 7321 | leaf = path->nodes[0]; |
| 7322 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7323 | ret = btrfs_next_leaf(root, path); |
| 7324 | if (ret == 0) |
| 7325 | continue; |
| 7326 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7327 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7328 | break; |
| 7329 | } |
| 7330 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7331 | |
| 7332 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7333 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7334 | ret = 0; |
| 7335 | goto out; |
| 7336 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7337 | path->slots[0]++; |
| 7338 | } |
| 7339 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7340 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7341 | return ret; |
| 7342 | } |
| 7343 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7344 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7345 | { |
| 7346 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7347 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7348 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7349 | struct rb_node *n; |
| 7350 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7351 | down_write(&info->extent_commit_sem); |
| 7352 | while (!list_empty(&info->caching_block_groups)) { |
| 7353 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7354 | struct btrfs_caching_control, list); |
| 7355 | list_del(&caching_ctl->list); |
| 7356 | put_caching_control(caching_ctl); |
| 7357 | } |
| 7358 | up_write(&info->extent_commit_sem); |
| 7359 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7360 | spin_lock(&info->block_group_cache_lock); |
| 7361 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7362 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7363 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7364 | rb_erase(&block_group->cache_node, |
| 7365 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7366 | spin_unlock(&info->block_group_cache_lock); |
| 7367 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7368 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7369 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7370 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7371 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7372 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7373 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7374 | |
| 7375 | btrfs_remove_free_space_cache(block_group); |
| 7376 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7377 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7378 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7379 | |
| 7380 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7381 | } |
| 7382 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7383 | |
| 7384 | /* now that all the block groups are freed, go through and |
| 7385 | * free all the space_info structs. This is only called during |
| 7386 | * the final stages of unmount, and so we know nobody is |
| 7387 | * using them. We call synchronize_rcu() once before we start, |
| 7388 | * just to be on the safe side. |
| 7389 | */ |
| 7390 | synchronize_rcu(); |
| 7391 | |
| 7392 | while(!list_empty(&info->space_info)) { |
| 7393 | space_info = list_entry(info->space_info.next, |
| 7394 | struct btrfs_space_info, |
| 7395 | list); |
| 7396 | |
| 7397 | list_del(&space_info->list); |
| 7398 | kfree(space_info); |
| 7399 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7400 | return 0; |
| 7401 | } |
| 7402 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7403 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7404 | { |
| 7405 | struct btrfs_path *path; |
| 7406 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7407 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7408 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7409 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7410 | struct btrfs_key key; |
| 7411 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7412 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7413 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7414 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7415 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7416 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7417 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7418 | path = btrfs_alloc_path(); |
| 7419 | if (!path) |
| 7420 | return -ENOMEM; |
| 7421 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7422 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7423 | ret = find_first_block_group(root, path, &key); |
| 7424 | if (ret > 0) { |
| 7425 | ret = 0; |
| 7426 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7427 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7428 | if (ret != 0) |
| 7429 | goto error; |
| 7430 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7431 | leaf = path->nodes[0]; |
| 7432 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7433 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7434 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7435 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7436 | break; |
| 7437 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7438 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7439 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7440 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7441 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7442 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7443 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7444 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7445 | |
| 7446 | /* |
| 7447 | * we only want to have 32k of ram per block group for keeping |
| 7448 | * track of free space, and if we pass 1/2 of that we want to |
| 7449 | * start converting things over to using bitmaps |
| 7450 | */ |
| 7451 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7452 | sizeof(struct btrfs_free_space); |
| 7453 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7454 | read_extent_buffer(leaf, &cache->item, |
| 7455 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7456 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7457 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7458 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7459 | key.objectid = found_key.objectid + found_key.offset; |
| 7460 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7461 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7462 | cache->sectorsize = root->sectorsize; |
| 7463 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7464 | /* |
| 7465 | * check for two cases, either we are full, and therefore |
| 7466 | * don't need to bother with the caching work since we won't |
| 7467 | * find any space, or we are empty, and we can just add all |
| 7468 | * the space in and be done with it. This saves us _alot_ of |
| 7469 | * time, particularly in the full case. |
| 7470 | */ |
| 7471 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7472 | exclude_super_stripes(root, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7473 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7474 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7475 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7476 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7477 | exclude_super_stripes(root, cache); |
| 7478 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7479 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7480 | add_new_free_space(cache, root->fs_info, |
| 7481 | found_key.objectid, |
| 7482 | found_key.objectid + |
| 7483 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7484 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7485 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7486 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7487 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7488 | btrfs_block_group_used(&cache->item), |
| 7489 | &space_info); |
| 7490 | BUG_ON(ret); |
| 7491 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7492 | spin_lock(&cache->space_info->lock); |
| 7493 | cache->space_info->bytes_super += cache->bytes_super; |
| 7494 | spin_unlock(&cache->space_info->lock); |
| 7495 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7496 | down_write(&space_info->groups_sem); |
| 7497 | list_add_tail(&cache->list, &space_info->block_groups); |
| 7498 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7499 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7500 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7501 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7502 | |
| 7503 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7504 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 7505 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7506 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7507 | ret = 0; |
| 7508 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7509 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7510 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7511 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7512 | |
| 7513 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7514 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7515 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7516 | u64 size) |
| 7517 | { |
| 7518 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7519 | struct btrfs_root *extent_root; |
| 7520 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7521 | |
| 7522 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7523 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7524 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7525 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7526 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7527 | if (!cache) |
| 7528 | return -ENOMEM; |
| 7529 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7530 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7531 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7532 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7533 | cache->sectorsize = root->sectorsize; |
| 7534 | |
| 7535 | /* |
| 7536 | * we only want to have 32k of ram per block group for keeping track |
| 7537 | * of free space, and if we pass 1/2 of that we want to start |
| 7538 | * converting things over to using bitmaps |
| 7539 | */ |
| 7540 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7541 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7542 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7543 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7544 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7545 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7546 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7547 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7548 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7549 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7550 | cache->flags = type; |
| 7551 | btrfs_set_block_group_flags(&cache->item, type); |
| 7552 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7553 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7554 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7555 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7556 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7557 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7558 | chunk_offset + size); |
| 7559 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7560 | free_excluded_extents(root, cache); |
| 7561 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7562 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7563 | &cache->space_info); |
| 7564 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7565 | |
| 7566 | spin_lock(&cache->space_info->lock); |
| 7567 | cache->space_info->bytes_super += cache->bytes_super; |
| 7568 | spin_unlock(&cache->space_info->lock); |
| 7569 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7570 | down_write(&cache->space_info->groups_sem); |
| 7571 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 7572 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7573 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7574 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7575 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7576 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7577 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7578 | sizeof(cache->item)); |
| 7579 | BUG_ON(ret); |
| 7580 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7581 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7582 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7583 | return 0; |
| 7584 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7585 | |
| 7586 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7587 | struct btrfs_root *root, u64 group_start) |
| 7588 | { |
| 7589 | struct btrfs_path *path; |
| 7590 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7591 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7592 | struct btrfs_key key; |
| 7593 | int ret; |
| 7594 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7595 | root = root->fs_info->extent_root; |
| 7596 | |
| 7597 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7598 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7599 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7600 | |
| 7601 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7602 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7603 | /* make sure this block group isn't part of an allocation cluster */ |
| 7604 | cluster = &root->fs_info->data_alloc_cluster; |
| 7605 | spin_lock(&cluster->refill_lock); |
| 7606 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7607 | spin_unlock(&cluster->refill_lock); |
| 7608 | |
| 7609 | /* |
| 7610 | * make sure this block group isn't part of a metadata |
| 7611 | * allocation cluster |
| 7612 | */ |
| 7613 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7614 | spin_lock(&cluster->refill_lock); |
| 7615 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7616 | spin_unlock(&cluster->refill_lock); |
| 7617 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7618 | path = btrfs_alloc_path(); |
| 7619 | BUG_ON(!path); |
| 7620 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7621 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7622 | rb_erase(&block_group->cache_node, |
| 7623 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7624 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7625 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7626 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7627 | /* |
| 7628 | * we must use list_del_init so people can check to see if they |
| 7629 | * are still on the list after taking the semaphore |
| 7630 | */ |
| 7631 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7632 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7633 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7634 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7635 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7636 | |
| 7637 | btrfs_remove_free_space_cache(block_group); |
| 7638 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7639 | spin_lock(&block_group->space_info->lock); |
| 7640 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7641 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 7642 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7643 | |
| 7644 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7645 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7646 | btrfs_put_block_group(block_group); |
| 7647 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7648 | |
| 7649 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7650 | if (ret > 0) |
| 7651 | ret = -EIO; |
| 7652 | if (ret < 0) |
| 7653 | goto out; |
| 7654 | |
| 7655 | ret = btrfs_del_item(trans, root, path); |
| 7656 | out: |
| 7657 | btrfs_free_path(path); |
| 7658 | return ret; |
| 7659 | } |