Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 20 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 22 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 23 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 26 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 27 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 33 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 34 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 36 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 37 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 38 | u64 bytenr, u64 num_bytes, int alloc); |
| 39 | static int update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 40 | u64 num_bytes, int reserve, int sinfo); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 41 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 42 | struct btrfs_root *root, |
| 43 | u64 bytenr, u64 num_bytes, u64 parent, |
| 44 | u64 root_objectid, u64 owner_objectid, |
| 45 | u64 owner_offset, int refs_to_drop, |
| 46 | struct btrfs_delayed_extent_op *extra_op); |
| 47 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 48 | struct extent_buffer *leaf, |
| 49 | struct btrfs_extent_item *ei); |
| 50 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 51 | struct btrfs_root *root, |
| 52 | u64 parent, u64 root_objectid, |
| 53 | u64 flags, u64 owner, u64 offset, |
| 54 | struct btrfs_key *ins, int ref_mod); |
| 55 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
| 57 | u64 parent, u64 root_objectid, |
| 58 | u64 flags, struct btrfs_disk_key *key, |
| 59 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 60 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 62 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 63 | static int find_next_key(struct btrfs_path *path, int level, |
| 64 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 65 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 66 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 67 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 68 | static noinline int |
| 69 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 70 | { |
| 71 | smp_mb(); |
| 72 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 73 | } |
| 74 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 75 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 76 | { |
| 77 | return (cache->flags & bits) == bits; |
| 78 | } |
| 79 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 80 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 81 | { |
| 82 | atomic_inc(&cache->count); |
| 83 | } |
| 84 | |
| 85 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 86 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 87 | if (atomic_dec_and_test(&cache->count)) { |
| 88 | WARN_ON(cache->pinned > 0); |
| 89 | WARN_ON(cache->reserved > 0); |
| 90 | WARN_ON(cache->reserved_pinned > 0); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 91 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 92 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 95 | /* |
| 96 | * this adds the block group to the fs_info rb tree for the block group |
| 97 | * cache |
| 98 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 99 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 100 | struct btrfs_block_group_cache *block_group) |
| 101 | { |
| 102 | struct rb_node **p; |
| 103 | struct rb_node *parent = NULL; |
| 104 | struct btrfs_block_group_cache *cache; |
| 105 | |
| 106 | spin_lock(&info->block_group_cache_lock); |
| 107 | p = &info->block_group_cache_tree.rb_node; |
| 108 | |
| 109 | while (*p) { |
| 110 | parent = *p; |
| 111 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 112 | cache_node); |
| 113 | if (block_group->key.objectid < cache->key.objectid) { |
| 114 | p = &(*p)->rb_left; |
| 115 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 116 | p = &(*p)->rb_right; |
| 117 | } else { |
| 118 | spin_unlock(&info->block_group_cache_lock); |
| 119 | return -EEXIST; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | rb_link_node(&block_group->cache_node, parent, p); |
| 124 | rb_insert_color(&block_group->cache_node, |
| 125 | &info->block_group_cache_tree); |
| 126 | spin_unlock(&info->block_group_cache_lock); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * This will return the block group at or after bytenr if contains is 0, else |
| 133 | * it will return the block group that contains the bytenr |
| 134 | */ |
| 135 | static struct btrfs_block_group_cache * |
| 136 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 137 | int contains) |
| 138 | { |
| 139 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 140 | struct rb_node *n; |
| 141 | u64 end, start; |
| 142 | |
| 143 | spin_lock(&info->block_group_cache_lock); |
| 144 | n = info->block_group_cache_tree.rb_node; |
| 145 | |
| 146 | while (n) { |
| 147 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 148 | cache_node); |
| 149 | end = cache->key.objectid + cache->key.offset - 1; |
| 150 | start = cache->key.objectid; |
| 151 | |
| 152 | if (bytenr < start) { |
| 153 | if (!contains && (!ret || start < ret->key.objectid)) |
| 154 | ret = cache; |
| 155 | n = n->rb_left; |
| 156 | } else if (bytenr > start) { |
| 157 | if (contains && bytenr <= end) { |
| 158 | ret = cache; |
| 159 | break; |
| 160 | } |
| 161 | n = n->rb_right; |
| 162 | } else { |
| 163 | ret = cache; |
| 164 | break; |
| 165 | } |
| 166 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 167 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 168 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 169 | spin_unlock(&info->block_group_cache_lock); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 174 | static int add_excluded_extent(struct btrfs_root *root, |
| 175 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 176 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 177 | u64 end = start + num_bytes - 1; |
| 178 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 179 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 180 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 181 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 182 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 185 | static void free_excluded_extents(struct btrfs_root *root, |
| 186 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 187 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 188 | u64 start, end; |
| 189 | |
| 190 | start = cache->key.objectid; |
| 191 | end = start + cache->key.offset - 1; |
| 192 | |
| 193 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 194 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 195 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 196 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 197 | } |
| 198 | |
| 199 | static int exclude_super_stripes(struct btrfs_root *root, |
| 200 | struct btrfs_block_group_cache *cache) |
| 201 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 202 | u64 bytenr; |
| 203 | u64 *logical; |
| 204 | int stripe_len; |
| 205 | int i, nr, ret; |
| 206 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 207 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 208 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 209 | cache->bytes_super += stripe_len; |
| 210 | ret = add_excluded_extent(root, cache->key.objectid, |
| 211 | stripe_len); |
| 212 | BUG_ON(ret); |
| 213 | } |
| 214 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 215 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 216 | bytenr = btrfs_sb_offset(i); |
| 217 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 218 | cache->key.objectid, bytenr, |
| 219 | 0, &logical, &nr, &stripe_len); |
| 220 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 221 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 222 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 223 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 224 | ret = add_excluded_extent(root, logical[nr], |
| 225 | stripe_len); |
| 226 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 227 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 229 | kfree(logical); |
| 230 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 234 | static struct btrfs_caching_control * |
| 235 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 236 | { |
| 237 | struct btrfs_caching_control *ctl; |
| 238 | |
| 239 | spin_lock(&cache->lock); |
| 240 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 241 | spin_unlock(&cache->lock); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 245 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 246 | if (!cache->caching_ctl) { |
| 247 | spin_unlock(&cache->lock); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 251 | ctl = cache->caching_ctl; |
| 252 | atomic_inc(&ctl->count); |
| 253 | spin_unlock(&cache->lock); |
| 254 | return ctl; |
| 255 | } |
| 256 | |
| 257 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 258 | { |
| 259 | if (atomic_dec_and_test(&ctl->count)) |
| 260 | kfree(ctl); |
| 261 | } |
| 262 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 263 | /* |
| 264 | * this is only called by cache_block_group, since we could have freed extents |
| 265 | * we need to check the pinned_extents for any extents that can't be used yet |
| 266 | * since their free space will be released as soon as the transaction commits. |
| 267 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 268 | 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] | 269 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 270 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 271 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | int ret; |
| 273 | |
| 274 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 275 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 276 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 277 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 278 | if (ret) |
| 279 | break; |
| 280 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 281 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 282 | start = extent_end + 1; |
| 283 | } else if (extent_start > start && extent_start < end) { |
| 284 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 285 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 286 | ret = btrfs_add_free_space(block_group, start, |
| 287 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 288 | BUG_ON(ret); |
| 289 | start = extent_end + 1; |
| 290 | } else { |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (start < end) { |
| 296 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 297 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 298 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 299 | BUG_ON(ret); |
| 300 | } |
| 301 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 302 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 305 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 306 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 307 | struct btrfs_block_group_cache *block_group = data; |
| 308 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 309 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 310 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 311 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 312 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 313 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 314 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 315 | u64 last = 0; |
| 316 | u32 nritems; |
| 317 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 318 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 319 | path = btrfs_alloc_path(); |
| 320 | if (!path) |
| 321 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 322 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 323 | exclude_super_stripes(extent_root, block_group); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 324 | spin_lock(&block_group->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 325 | block_group->space_info->bytes_readonly += block_group->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 326 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 327 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 328 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 329 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 330 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 331 | * We don't want to deadlock with somebody trying to allocate a new |
| 332 | * extent for the extent root while also trying to search the extent |
| 333 | * root to add free space. So we skip locking and search the commit |
| 334 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 335 | */ |
| 336 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 337 | path->search_commit_root = 1; |
| 338 | path->reada = 2; |
| 339 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 340 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 341 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 342 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 343 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 344 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 345 | /* need to make sure the commit_root doesn't disappear */ |
| 346 | down_read(&fs_info->extent_commit_sem); |
| 347 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 348 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 349 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 350 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 351 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 352 | leaf = path->nodes[0]; |
| 353 | nritems = btrfs_header_nritems(leaf); |
| 354 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 355 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 356 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 357 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 358 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 359 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 360 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 361 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 362 | if (path->slots[0] < nritems) { |
| 363 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 364 | } else { |
| 365 | ret = find_next_key(path, 0, &key); |
| 366 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 367 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 368 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 369 | caching_ctl->progress = last; |
| 370 | btrfs_release_path(extent_root, path); |
| 371 | up_read(&fs_info->extent_commit_sem); |
| 372 | mutex_unlock(&caching_ctl->mutex); |
| 373 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 374 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 375 | else |
| 376 | cond_resched(); |
| 377 | goto again; |
| 378 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 379 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 380 | if (key.objectid < block_group->key.objectid) { |
| 381 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 382 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 383 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 384 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 385 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 386 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 387 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 388 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 389 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 390 | total_found += add_new_free_space(block_group, |
| 391 | fs_info, last, |
| 392 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 393 | last = key.objectid + key.offset; |
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 | if (total_found > (1024 * 1024 * 2)) { |
| 396 | total_found = 0; |
| 397 | wake_up(&caching_ctl->wait); |
| 398 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 399 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 400 | path->slots[0]++; |
| 401 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 402 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 403 | |
| 404 | total_found += add_new_free_space(block_group, fs_info, last, |
| 405 | block_group->key.objectid + |
| 406 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 407 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 408 | |
| 409 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 410 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 411 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 412 | spin_unlock(&block_group->lock); |
| 413 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 414 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 415 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 416 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 417 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 418 | free_excluded_extents(extent_root, block_group); |
| 419 | |
| 420 | mutex_unlock(&caching_ctl->mutex); |
| 421 | wake_up(&caching_ctl->wait); |
| 422 | |
| 423 | put_caching_control(caching_ctl); |
| 424 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 425 | btrfs_put_block_group(block_group); |
| 426 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 430 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 431 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 432 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 433 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 434 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 435 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 436 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 437 | struct task_struct *tsk; |
| 438 | int ret = 0; |
| 439 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 440 | smp_mb(); |
| 441 | if (cache->cached != BTRFS_CACHE_NO) |
| 442 | return 0; |
| 443 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 444 | /* |
| 445 | * We can't do the read from on-disk cache during a commit since we need |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 446 | * to have the normal tree locking. Also if we are currently trying to |
| 447 | * allocate blocks for the tree root we can't do the fast caching since |
| 448 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 449 | */ |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 450 | if (!trans->transaction->in_commit && |
| 451 | (root && root != root->fs_info->tree_root)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 452 | spin_lock(&cache->lock); |
| 453 | if (cache->cached != BTRFS_CACHE_NO) { |
| 454 | spin_unlock(&cache->lock); |
| 455 | return 0; |
| 456 | } |
| 457 | cache->cached = BTRFS_CACHE_STARTED; |
| 458 | spin_unlock(&cache->lock); |
| 459 | |
| 460 | ret = load_free_space_cache(fs_info, cache); |
| 461 | |
| 462 | spin_lock(&cache->lock); |
| 463 | if (ret == 1) { |
| 464 | cache->cached = BTRFS_CACHE_FINISHED; |
| 465 | cache->last_byte_to_unpin = (u64)-1; |
| 466 | } else { |
| 467 | cache->cached = BTRFS_CACHE_NO; |
| 468 | } |
| 469 | spin_unlock(&cache->lock); |
| 470 | if (ret == 1) |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | if (load_cache_only) |
| 475 | return 0; |
| 476 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 477 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); |
| 478 | BUG_ON(!caching_ctl); |
| 479 | |
| 480 | INIT_LIST_HEAD(&caching_ctl->list); |
| 481 | mutex_init(&caching_ctl->mutex); |
| 482 | init_waitqueue_head(&caching_ctl->wait); |
| 483 | caching_ctl->block_group = cache; |
| 484 | caching_ctl->progress = cache->key.objectid; |
| 485 | /* one for caching kthread, one for caching block group list */ |
| 486 | atomic_set(&caching_ctl->count, 2); |
| 487 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 488 | spin_lock(&cache->lock); |
| 489 | if (cache->cached != BTRFS_CACHE_NO) { |
| 490 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 491 | kfree(caching_ctl); |
| 492 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 493 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 494 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 495 | cache->cached = BTRFS_CACHE_STARTED; |
| 496 | spin_unlock(&cache->lock); |
| 497 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 498 | down_write(&fs_info->extent_commit_sem); |
| 499 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 500 | up_write(&fs_info->extent_commit_sem); |
| 501 | |
| 502 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 503 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 504 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 505 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 506 | cache->key.objectid); |
| 507 | if (IS_ERR(tsk)) { |
| 508 | ret = PTR_ERR(tsk); |
| 509 | printk(KERN_ERR "error running thread %d\n", ret); |
| 510 | BUG(); |
| 511 | } |
| 512 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 513 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 516 | /* |
| 517 | * return the block group that starts at or after bytenr |
| 518 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 519 | static struct btrfs_block_group_cache * |
| 520 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 521 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 522 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 523 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 524 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 525 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 526 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 529 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 530 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 531 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 532 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 533 | struct btrfs_fs_info *info, |
| 534 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 535 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 536 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 537 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 538 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 539 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 540 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 541 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 542 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 543 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 544 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 545 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 546 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 547 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 548 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 549 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 550 | BTRFS_BLOCK_GROUP_METADATA; |
| 551 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 552 | rcu_read_lock(); |
| 553 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 554 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 555 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 556 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 557 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 558 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 559 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 560 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 561 | } |
| 562 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 563 | /* |
| 564 | * after adding space to the filesystem, we need to clear the full flags |
| 565 | * on all the space infos. |
| 566 | */ |
| 567 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 568 | { |
| 569 | struct list_head *head = &info->space_info; |
| 570 | struct btrfs_space_info *found; |
| 571 | |
| 572 | rcu_read_lock(); |
| 573 | list_for_each_entry_rcu(found, head, list) |
| 574 | found->full = 0; |
| 575 | rcu_read_unlock(); |
| 576 | } |
| 577 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 578 | static u64 div_factor(u64 num, int factor) |
| 579 | { |
| 580 | if (factor == 10) |
| 581 | return num; |
| 582 | num *= factor; |
| 583 | do_div(num, 10); |
| 584 | return num; |
| 585 | } |
| 586 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 587 | static u64 div_factor_fine(u64 num, int factor) |
| 588 | { |
| 589 | if (factor == 100) |
| 590 | return num; |
| 591 | num *= factor; |
| 592 | do_div(num, 100); |
| 593 | return num; |
| 594 | } |
| 595 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 596 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 597 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 598 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 599 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 600 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 601 | u64 last = max(search_hint, search_start); |
| 602 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 603 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 604 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 605 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 606 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 607 | while (1) { |
| 608 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 609 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 610 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 611 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 612 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 613 | last = cache->key.objectid + cache->key.offset; |
| 614 | used = btrfs_block_group_used(&cache->item); |
| 615 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 616 | if ((full_search || !cache->ro) && |
| 617 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 618 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 619 | div_factor(cache->key.offset, factor)) { |
| 620 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 621 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 622 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 623 | goto found; |
| 624 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 625 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 626 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 627 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 628 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 629 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 630 | if (!wrapped) { |
| 631 | last = search_start; |
| 632 | wrapped = 1; |
| 633 | goto again; |
| 634 | } |
| 635 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 636 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 637 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 638 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 639 | goto again; |
| 640 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 641 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 642 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 643 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 644 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 645 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 646 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 647 | { |
| 648 | int ret; |
| 649 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 650 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 651 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 652 | path = btrfs_alloc_path(); |
| 653 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 654 | key.objectid = start; |
| 655 | key.offset = len; |
| 656 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 657 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 658 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 659 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 660 | return ret; |
| 661 | } |
| 662 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 663 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 664 | * helper function to lookup reference count and flags of extent. |
| 665 | * |
| 666 | * the head node for delayed ref is used to store the sum of all the |
| 667 | * reference count modifications queued up in the rbtree. the head |
| 668 | * node may also store the extent flags to set. This way you can check |
| 669 | * to see what the reference count and extent flags would be if all of |
| 670 | * the delayed refs are not processed. |
| 671 | */ |
| 672 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 673 | struct btrfs_root *root, u64 bytenr, |
| 674 | u64 num_bytes, u64 *refs, u64 *flags) |
| 675 | { |
| 676 | struct btrfs_delayed_ref_head *head; |
| 677 | struct btrfs_delayed_ref_root *delayed_refs; |
| 678 | struct btrfs_path *path; |
| 679 | struct btrfs_extent_item *ei; |
| 680 | struct extent_buffer *leaf; |
| 681 | struct btrfs_key key; |
| 682 | u32 item_size; |
| 683 | u64 num_refs; |
| 684 | u64 extent_flags; |
| 685 | int ret; |
| 686 | |
| 687 | path = btrfs_alloc_path(); |
| 688 | if (!path) |
| 689 | return -ENOMEM; |
| 690 | |
| 691 | key.objectid = bytenr; |
| 692 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 693 | key.offset = num_bytes; |
| 694 | if (!trans) { |
| 695 | path->skip_locking = 1; |
| 696 | path->search_commit_root = 1; |
| 697 | } |
| 698 | again: |
| 699 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 700 | &key, path, 0, 0); |
| 701 | if (ret < 0) |
| 702 | goto out_free; |
| 703 | |
| 704 | if (ret == 0) { |
| 705 | leaf = path->nodes[0]; |
| 706 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 707 | if (item_size >= sizeof(*ei)) { |
| 708 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 709 | struct btrfs_extent_item); |
| 710 | num_refs = btrfs_extent_refs(leaf, ei); |
| 711 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 712 | } else { |
| 713 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 714 | struct btrfs_extent_item_v0 *ei0; |
| 715 | BUG_ON(item_size != sizeof(*ei0)); |
| 716 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 717 | struct btrfs_extent_item_v0); |
| 718 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 719 | /* FIXME: this isn't correct for data */ |
| 720 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 721 | #else |
| 722 | BUG(); |
| 723 | #endif |
| 724 | } |
| 725 | BUG_ON(num_refs == 0); |
| 726 | } else { |
| 727 | num_refs = 0; |
| 728 | extent_flags = 0; |
| 729 | ret = 0; |
| 730 | } |
| 731 | |
| 732 | if (!trans) |
| 733 | goto out; |
| 734 | |
| 735 | delayed_refs = &trans->transaction->delayed_refs; |
| 736 | spin_lock(&delayed_refs->lock); |
| 737 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 738 | if (head) { |
| 739 | if (!mutex_trylock(&head->mutex)) { |
| 740 | atomic_inc(&head->node.refs); |
| 741 | spin_unlock(&delayed_refs->lock); |
| 742 | |
| 743 | btrfs_release_path(root->fs_info->extent_root, path); |
| 744 | |
| 745 | mutex_lock(&head->mutex); |
| 746 | mutex_unlock(&head->mutex); |
| 747 | btrfs_put_delayed_ref(&head->node); |
| 748 | goto again; |
| 749 | } |
| 750 | if (head->extent_op && head->extent_op->update_flags) |
| 751 | extent_flags |= head->extent_op->flags_to_set; |
| 752 | else |
| 753 | BUG_ON(num_refs == 0); |
| 754 | |
| 755 | num_refs += head->node.ref_mod; |
| 756 | mutex_unlock(&head->mutex); |
| 757 | } |
| 758 | spin_unlock(&delayed_refs->lock); |
| 759 | out: |
| 760 | WARN_ON(num_refs == 0); |
| 761 | if (refs) |
| 762 | *refs = num_refs; |
| 763 | if (flags) |
| 764 | *flags = extent_flags; |
| 765 | out_free: |
| 766 | btrfs_free_path(path); |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 771 | * Back reference rules. Back refs have three main goals: |
| 772 | * |
| 773 | * 1) differentiate between all holders of references to an extent so that |
| 774 | * when a reference is dropped we can make sure it was a valid reference |
| 775 | * before freeing the extent. |
| 776 | * |
| 777 | * 2) Provide enough information to quickly find the holders of an extent |
| 778 | * if we notice a given block is corrupted or bad. |
| 779 | * |
| 780 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 781 | * maintenance. This is actually the same as #2, but with a slightly |
| 782 | * different use case. |
| 783 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 784 | * There are two kinds of back refs. The implicit back refs is optimized |
| 785 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 786 | * back refs of this kind provide information about the block's owner tree |
| 787 | * and the pointer's key. These information allow us to find the block by |
| 788 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 789 | * referenced by their owner trees. The location of tree block is recorded |
| 790 | * in the back refs. Actually the full back refs is generic, and can be |
| 791 | * used in all cases the implicit back refs is used. The major shortcoming |
| 792 | * of the full back refs is its overhead. Every time a tree block gets |
| 793 | * COWed, we have to update back refs entry for all pointers in it. |
| 794 | * |
| 795 | * For a newly allocated tree block, we use implicit back refs for |
| 796 | * pointers in it. This means most tree related operations only involve |
| 797 | * implicit back refs. For a tree block created in old transaction, the |
| 798 | * only way to drop a reference to it is COW it. So we can detect the |
| 799 | * event that tree block loses its owner tree's reference and do the |
| 800 | * back refs conversion. |
| 801 | * |
| 802 | * When a tree block is COW'd through a tree, there are four cases: |
| 803 | * |
| 804 | * The reference count of the block is one and the tree is the block's |
| 805 | * owner tree. Nothing to do in this case. |
| 806 | * |
| 807 | * The reference count of the block is one and the tree is not the |
| 808 | * block's owner tree. In this case, full back refs is used for pointers |
| 809 | * in the block. Remove these full back refs, add implicit back refs for |
| 810 | * every pointers in the new block. |
| 811 | * |
| 812 | * The reference count of the block is greater than one and the tree is |
| 813 | * the block's owner tree. In this case, implicit back refs is used for |
| 814 | * pointers in the block. Add full back refs for every pointers in the |
| 815 | * block, increase lower level extents' reference counts. The original |
| 816 | * implicit back refs are entailed to the new block. |
| 817 | * |
| 818 | * The reference count of the block is greater than one and the tree is |
| 819 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 820 | * the new block, increase lower level extents' reference count. |
| 821 | * |
| 822 | * Back Reference Key composing: |
| 823 | * |
| 824 | * The key objectid corresponds to the first byte in the extent, |
| 825 | * The key type is used to differentiate between types of back refs. |
| 826 | * There are different meanings of the key offset for different types |
| 827 | * of back refs. |
| 828 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 829 | * File extents can be referenced by: |
| 830 | * |
| 831 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 832 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 833 | * - different offsets inside a file (bookend extents in file.c) |
| 834 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 835 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 836 | * |
| 837 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 838 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 839 | * - original offset in the file |
| 840 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 841 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 842 | * The key offset for the implicit back refs is hash of the first |
| 843 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 844 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 845 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 846 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 847 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 848 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 849 | * The key offset for the implicit back refs is the first byte of |
| 850 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 851 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 852 | * When a file extent is allocated, The implicit back refs is used. |
| 853 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 854 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 855 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 856 | * |
| 857 | * When a file extent is removed file truncation, we find the |
| 858 | * corresponding implicit back refs and check the following fields: |
| 859 | * |
| 860 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 861 | * |
| 862 | * Btree extents can be referenced by: |
| 863 | * |
| 864 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 865 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 866 | * Both the implicit back refs and the full back refs for tree blocks |
| 867 | * only consist of key. The key offset for the implicit back refs is |
| 868 | * objectid of block's owner tree. The key offset for the full back refs |
| 869 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 870 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 871 | * When implicit back refs is used, information about the lowest key and |
| 872 | * level of the tree block are required. These information are stored in |
| 873 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 874 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 875 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 876 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 877 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 878 | struct btrfs_root *root, |
| 879 | struct btrfs_path *path, |
| 880 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 881 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 882 | struct btrfs_extent_item *item; |
| 883 | struct btrfs_extent_item_v0 *ei0; |
| 884 | struct btrfs_extent_ref_v0 *ref0; |
| 885 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 886 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 887 | struct btrfs_key key; |
| 888 | struct btrfs_key found_key; |
| 889 | u32 new_size = sizeof(*item); |
| 890 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 891 | int ret; |
| 892 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 893 | leaf = path->nodes[0]; |
| 894 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 895 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 896 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 897 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 898 | struct btrfs_extent_item_v0); |
| 899 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 900 | |
| 901 | if (owner == (u64)-1) { |
| 902 | while (1) { |
| 903 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 904 | ret = btrfs_next_leaf(root, path); |
| 905 | if (ret < 0) |
| 906 | return ret; |
| 907 | BUG_ON(ret > 0); |
| 908 | leaf = path->nodes[0]; |
| 909 | } |
| 910 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 911 | path->slots[0]); |
| 912 | BUG_ON(key.objectid != found_key.objectid); |
| 913 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 914 | path->slots[0]++; |
| 915 | continue; |
| 916 | } |
| 917 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 918 | struct btrfs_extent_ref_v0); |
| 919 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | btrfs_release_path(root, path); |
| 924 | |
| 925 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 926 | new_size += sizeof(*bi); |
| 927 | |
| 928 | new_size -= sizeof(*ei0); |
| 929 | ret = btrfs_search_slot(trans, root, &key, path, |
| 930 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 931 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 932 | return ret; |
| 933 | BUG_ON(ret); |
| 934 | |
| 935 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 936 | BUG_ON(ret); |
| 937 | |
| 938 | leaf = path->nodes[0]; |
| 939 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 940 | btrfs_set_extent_refs(leaf, item, refs); |
| 941 | /* FIXME: get real generation */ |
| 942 | btrfs_set_extent_generation(leaf, item, 0); |
| 943 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 944 | btrfs_set_extent_flags(leaf, item, |
| 945 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 946 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 947 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 948 | /* FIXME: get first key of the block */ |
| 949 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 950 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 951 | } else { |
| 952 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 953 | } |
| 954 | btrfs_mark_buffer_dirty(leaf); |
| 955 | return 0; |
| 956 | } |
| 957 | #endif |
| 958 | |
| 959 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 960 | { |
| 961 | u32 high_crc = ~(u32)0; |
| 962 | u32 low_crc = ~(u32)0; |
| 963 | __le64 lenum; |
| 964 | |
| 965 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 966 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 967 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 968 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 969 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 970 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 971 | |
| 972 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 973 | } |
| 974 | |
| 975 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 976 | struct btrfs_extent_data_ref *ref) |
| 977 | { |
| 978 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 979 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 980 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 981 | } |
| 982 | |
| 983 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 984 | struct btrfs_extent_data_ref *ref, |
| 985 | u64 root_objectid, u64 owner, u64 offset) |
| 986 | { |
| 987 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 988 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 989 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 990 | return 0; |
| 991 | return 1; |
| 992 | } |
| 993 | |
| 994 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 995 | struct btrfs_root *root, |
| 996 | struct btrfs_path *path, |
| 997 | u64 bytenr, u64 parent, |
| 998 | u64 root_objectid, |
| 999 | u64 owner, u64 offset) |
| 1000 | { |
| 1001 | struct btrfs_key key; |
| 1002 | struct btrfs_extent_data_ref *ref; |
| 1003 | struct extent_buffer *leaf; |
| 1004 | u32 nritems; |
| 1005 | int ret; |
| 1006 | int recow; |
| 1007 | int err = -ENOENT; |
| 1008 | |
| 1009 | key.objectid = bytenr; |
| 1010 | if (parent) { |
| 1011 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1012 | key.offset = parent; |
| 1013 | } else { |
| 1014 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1015 | key.offset = hash_extent_data_ref(root_objectid, |
| 1016 | owner, offset); |
| 1017 | } |
| 1018 | again: |
| 1019 | recow = 0; |
| 1020 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1021 | if (ret < 0) { |
| 1022 | err = ret; |
| 1023 | goto fail; |
| 1024 | } |
| 1025 | |
| 1026 | if (parent) { |
| 1027 | if (!ret) |
| 1028 | return 0; |
| 1029 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1030 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1031 | btrfs_release_path(root, path); |
| 1032 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1033 | if (ret < 0) { |
| 1034 | err = ret; |
| 1035 | goto fail; |
| 1036 | } |
| 1037 | if (!ret) |
| 1038 | return 0; |
| 1039 | #endif |
| 1040 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1044 | nritems = btrfs_header_nritems(leaf); |
| 1045 | while (1) { |
| 1046 | if (path->slots[0] >= nritems) { |
| 1047 | ret = btrfs_next_leaf(root, path); |
| 1048 | if (ret < 0) |
| 1049 | err = ret; |
| 1050 | if (ret) |
| 1051 | goto fail; |
| 1052 | |
| 1053 | leaf = path->nodes[0]; |
| 1054 | nritems = btrfs_header_nritems(leaf); |
| 1055 | recow = 1; |
| 1056 | } |
| 1057 | |
| 1058 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1059 | if (key.objectid != bytenr || |
| 1060 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1061 | goto fail; |
| 1062 | |
| 1063 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1064 | struct btrfs_extent_data_ref); |
| 1065 | |
| 1066 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1067 | owner, offset)) { |
| 1068 | if (recow) { |
| 1069 | btrfs_release_path(root, path); |
| 1070 | goto again; |
| 1071 | } |
| 1072 | err = 0; |
| 1073 | break; |
| 1074 | } |
| 1075 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1076 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1077 | fail: |
| 1078 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1079 | } |
| 1080 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1081 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1082 | struct btrfs_root *root, |
| 1083 | struct btrfs_path *path, |
| 1084 | u64 bytenr, u64 parent, |
| 1085 | u64 root_objectid, u64 owner, |
| 1086 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1087 | { |
| 1088 | struct btrfs_key key; |
| 1089 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1090 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1091 | u32 num_refs; |
| 1092 | int ret; |
| 1093 | |
| 1094 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1095 | if (parent) { |
| 1096 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1097 | key.offset = parent; |
| 1098 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1099 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1100 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1101 | key.offset = hash_extent_data_ref(root_objectid, |
| 1102 | owner, offset); |
| 1103 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1104 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1105 | |
| 1106 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1107 | if (ret && ret != -EEXIST) |
| 1108 | goto fail; |
| 1109 | |
| 1110 | leaf = path->nodes[0]; |
| 1111 | if (parent) { |
| 1112 | struct btrfs_shared_data_ref *ref; |
| 1113 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1114 | struct btrfs_shared_data_ref); |
| 1115 | if (ret == 0) { |
| 1116 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1117 | } else { |
| 1118 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1119 | num_refs += refs_to_add; |
| 1120 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1121 | } |
| 1122 | } else { |
| 1123 | struct btrfs_extent_data_ref *ref; |
| 1124 | while (ret == -EEXIST) { |
| 1125 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1126 | struct btrfs_extent_data_ref); |
| 1127 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1128 | owner, offset)) |
| 1129 | break; |
| 1130 | btrfs_release_path(root, path); |
| 1131 | key.offset++; |
| 1132 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1133 | size); |
| 1134 | if (ret && ret != -EEXIST) |
| 1135 | goto fail; |
| 1136 | |
| 1137 | leaf = path->nodes[0]; |
| 1138 | } |
| 1139 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1140 | struct btrfs_extent_data_ref); |
| 1141 | if (ret == 0) { |
| 1142 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1143 | root_objectid); |
| 1144 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1145 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1146 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1147 | } else { |
| 1148 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1149 | num_refs += refs_to_add; |
| 1150 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1151 | } |
| 1152 | } |
| 1153 | btrfs_mark_buffer_dirty(leaf); |
| 1154 | ret = 0; |
| 1155 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1156 | btrfs_release_path(root, path); |
| 1157 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1158 | } |
| 1159 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1160 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1161 | struct btrfs_root *root, |
| 1162 | struct btrfs_path *path, |
| 1163 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1164 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1165 | struct btrfs_key key; |
| 1166 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1167 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1168 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1169 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1170 | int ret = 0; |
| 1171 | |
| 1172 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1173 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1174 | |
| 1175 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1176 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1177 | struct btrfs_extent_data_ref); |
| 1178 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1179 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1180 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1181 | struct btrfs_shared_data_ref); |
| 1182 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1183 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1184 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1185 | struct btrfs_extent_ref_v0 *ref0; |
| 1186 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1187 | struct btrfs_extent_ref_v0); |
| 1188 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1189 | #endif |
| 1190 | } else { |
| 1191 | BUG(); |
| 1192 | } |
| 1193 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1194 | BUG_ON(num_refs < refs_to_drop); |
| 1195 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1196 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1197 | if (num_refs == 0) { |
| 1198 | ret = btrfs_del_item(trans, root, path); |
| 1199 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1200 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1201 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1202 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1203 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1204 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1205 | else { |
| 1206 | struct btrfs_extent_ref_v0 *ref0; |
| 1207 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1208 | struct btrfs_extent_ref_v0); |
| 1209 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1210 | } |
| 1211 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1212 | btrfs_mark_buffer_dirty(leaf); |
| 1213 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1214 | return ret; |
| 1215 | } |
| 1216 | |
| 1217 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1218 | struct btrfs_path *path, |
| 1219 | struct btrfs_extent_inline_ref *iref) |
| 1220 | { |
| 1221 | struct btrfs_key key; |
| 1222 | struct extent_buffer *leaf; |
| 1223 | struct btrfs_extent_data_ref *ref1; |
| 1224 | struct btrfs_shared_data_ref *ref2; |
| 1225 | u32 num_refs = 0; |
| 1226 | |
| 1227 | leaf = path->nodes[0]; |
| 1228 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1229 | if (iref) { |
| 1230 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1231 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1232 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1233 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1234 | } else { |
| 1235 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1236 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1237 | } |
| 1238 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1239 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1240 | struct btrfs_extent_data_ref); |
| 1241 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1242 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1243 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1244 | struct btrfs_shared_data_ref); |
| 1245 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1246 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1247 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1248 | struct btrfs_extent_ref_v0 *ref0; |
| 1249 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1250 | struct btrfs_extent_ref_v0); |
| 1251 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1252 | #endif |
| 1253 | } else { |
| 1254 | WARN_ON(1); |
| 1255 | } |
| 1256 | return num_refs; |
| 1257 | } |
| 1258 | |
| 1259 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1260 | struct btrfs_root *root, |
| 1261 | struct btrfs_path *path, |
| 1262 | u64 bytenr, u64 parent, |
| 1263 | u64 root_objectid) |
| 1264 | { |
| 1265 | struct btrfs_key key; |
| 1266 | int ret; |
| 1267 | |
| 1268 | key.objectid = bytenr; |
| 1269 | if (parent) { |
| 1270 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1271 | key.offset = parent; |
| 1272 | } else { |
| 1273 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1274 | key.offset = root_objectid; |
| 1275 | } |
| 1276 | |
| 1277 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1278 | if (ret > 0) |
| 1279 | ret = -ENOENT; |
| 1280 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1281 | if (ret == -ENOENT && parent) { |
| 1282 | btrfs_release_path(root, path); |
| 1283 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1284 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1285 | if (ret > 0) |
| 1286 | ret = -ENOENT; |
| 1287 | } |
| 1288 | #endif |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1293 | struct btrfs_root *root, |
| 1294 | struct btrfs_path *path, |
| 1295 | u64 bytenr, u64 parent, |
| 1296 | u64 root_objectid) |
| 1297 | { |
| 1298 | struct btrfs_key key; |
| 1299 | int ret; |
| 1300 | |
| 1301 | key.objectid = bytenr; |
| 1302 | if (parent) { |
| 1303 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1304 | key.offset = parent; |
| 1305 | } else { |
| 1306 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1307 | key.offset = root_objectid; |
| 1308 | } |
| 1309 | |
| 1310 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1311 | btrfs_release_path(root, path); |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1315 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1316 | { |
| 1317 | int type; |
| 1318 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1319 | if (parent > 0) |
| 1320 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1321 | else |
| 1322 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1323 | } else { |
| 1324 | if (parent > 0) |
| 1325 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1326 | else |
| 1327 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1328 | } |
| 1329 | return type; |
| 1330 | } |
| 1331 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1332 | static int find_next_key(struct btrfs_path *path, int level, |
| 1333 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1334 | |
| 1335 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1336 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1337 | if (!path->nodes[level]) |
| 1338 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1339 | if (path->slots[level] + 1 >= |
| 1340 | btrfs_header_nritems(path->nodes[level])) |
| 1341 | continue; |
| 1342 | if (level == 0) |
| 1343 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1344 | path->slots[level] + 1); |
| 1345 | else |
| 1346 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1347 | path->slots[level] + 1); |
| 1348 | return 0; |
| 1349 | } |
| 1350 | return 1; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1355 | * to the address of inline back ref, and 0 is returned. |
| 1356 | * |
| 1357 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1358 | * should be inserted, and -ENOENT is returned. |
| 1359 | * |
| 1360 | * if insert is true and there are too many inline back refs, the path |
| 1361 | * points to the extent item, and -EAGAIN is returned. |
| 1362 | * |
| 1363 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1364 | * items in the tree are ordered. |
| 1365 | */ |
| 1366 | static noinline_for_stack |
| 1367 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1368 | struct btrfs_root *root, |
| 1369 | struct btrfs_path *path, |
| 1370 | struct btrfs_extent_inline_ref **ref_ret, |
| 1371 | u64 bytenr, u64 num_bytes, |
| 1372 | u64 parent, u64 root_objectid, |
| 1373 | u64 owner, u64 offset, int insert) |
| 1374 | { |
| 1375 | struct btrfs_key key; |
| 1376 | struct extent_buffer *leaf; |
| 1377 | struct btrfs_extent_item *ei; |
| 1378 | struct btrfs_extent_inline_ref *iref; |
| 1379 | u64 flags; |
| 1380 | u64 item_size; |
| 1381 | unsigned long ptr; |
| 1382 | unsigned long end; |
| 1383 | int extra_size; |
| 1384 | int type; |
| 1385 | int want; |
| 1386 | int ret; |
| 1387 | int err = 0; |
| 1388 | |
| 1389 | key.objectid = bytenr; |
| 1390 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1391 | key.offset = num_bytes; |
| 1392 | |
| 1393 | want = extent_ref_type(parent, owner); |
| 1394 | if (insert) { |
| 1395 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1396 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1397 | } else |
| 1398 | extra_size = -1; |
| 1399 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1400 | if (ret < 0) { |
| 1401 | err = ret; |
| 1402 | goto out; |
| 1403 | } |
| 1404 | BUG_ON(ret); |
| 1405 | |
| 1406 | leaf = path->nodes[0]; |
| 1407 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1408 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1409 | if (item_size < sizeof(*ei)) { |
| 1410 | if (!insert) { |
| 1411 | err = -ENOENT; |
| 1412 | goto out; |
| 1413 | } |
| 1414 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1415 | extra_size); |
| 1416 | if (ret < 0) { |
| 1417 | err = ret; |
| 1418 | goto out; |
| 1419 | } |
| 1420 | leaf = path->nodes[0]; |
| 1421 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1422 | } |
| 1423 | #endif |
| 1424 | BUG_ON(item_size < sizeof(*ei)); |
| 1425 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1426 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1427 | flags = btrfs_extent_flags(leaf, ei); |
| 1428 | |
| 1429 | ptr = (unsigned long)(ei + 1); |
| 1430 | end = (unsigned long)ei + item_size; |
| 1431 | |
| 1432 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1433 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1434 | BUG_ON(ptr > end); |
| 1435 | } else { |
| 1436 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1437 | } |
| 1438 | |
| 1439 | err = -ENOENT; |
| 1440 | while (1) { |
| 1441 | if (ptr >= end) { |
| 1442 | WARN_ON(ptr > end); |
| 1443 | break; |
| 1444 | } |
| 1445 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1446 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1447 | if (want < type) |
| 1448 | break; |
| 1449 | if (want > type) { |
| 1450 | ptr += btrfs_extent_inline_ref_size(type); |
| 1451 | continue; |
| 1452 | } |
| 1453 | |
| 1454 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1455 | struct btrfs_extent_data_ref *dref; |
| 1456 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1457 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1458 | owner, offset)) { |
| 1459 | err = 0; |
| 1460 | break; |
| 1461 | } |
| 1462 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1463 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1464 | break; |
| 1465 | } else { |
| 1466 | u64 ref_offset; |
| 1467 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1468 | if (parent > 0) { |
| 1469 | if (parent == ref_offset) { |
| 1470 | err = 0; |
| 1471 | break; |
| 1472 | } |
| 1473 | if (ref_offset < parent) |
| 1474 | break; |
| 1475 | } else { |
| 1476 | if (root_objectid == ref_offset) { |
| 1477 | err = 0; |
| 1478 | break; |
| 1479 | } |
| 1480 | if (ref_offset < root_objectid) |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
| 1484 | ptr += btrfs_extent_inline_ref_size(type); |
| 1485 | } |
| 1486 | if (err == -ENOENT && insert) { |
| 1487 | if (item_size + extra_size >= |
| 1488 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1489 | err = -EAGAIN; |
| 1490 | goto out; |
| 1491 | } |
| 1492 | /* |
| 1493 | * To add new inline back ref, we have to make sure |
| 1494 | * there is no corresponding back ref item. |
| 1495 | * For simplicity, we just do not add new inline back |
| 1496 | * ref if there is any kind of item for this block |
| 1497 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1498 | if (find_next_key(path, 0, &key) == 0 && |
| 1499 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1500 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1501 | err = -EAGAIN; |
| 1502 | goto out; |
| 1503 | } |
| 1504 | } |
| 1505 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1506 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1507 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1508 | path->keep_locks = 0; |
| 1509 | btrfs_unlock_up_safe(path, 1); |
| 1510 | } |
| 1511 | return err; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * helper to add new inline back ref |
| 1516 | */ |
| 1517 | static noinline_for_stack |
| 1518 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1519 | struct btrfs_root *root, |
| 1520 | struct btrfs_path *path, |
| 1521 | struct btrfs_extent_inline_ref *iref, |
| 1522 | u64 parent, u64 root_objectid, |
| 1523 | u64 owner, u64 offset, int refs_to_add, |
| 1524 | struct btrfs_delayed_extent_op *extent_op) |
| 1525 | { |
| 1526 | struct extent_buffer *leaf; |
| 1527 | struct btrfs_extent_item *ei; |
| 1528 | unsigned long ptr; |
| 1529 | unsigned long end; |
| 1530 | unsigned long item_offset; |
| 1531 | u64 refs; |
| 1532 | int size; |
| 1533 | int type; |
| 1534 | int ret; |
| 1535 | |
| 1536 | leaf = path->nodes[0]; |
| 1537 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1538 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1539 | |
| 1540 | type = extent_ref_type(parent, owner); |
| 1541 | size = btrfs_extent_inline_ref_size(type); |
| 1542 | |
| 1543 | ret = btrfs_extend_item(trans, root, path, size); |
| 1544 | BUG_ON(ret); |
| 1545 | |
| 1546 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1547 | refs = btrfs_extent_refs(leaf, ei); |
| 1548 | refs += refs_to_add; |
| 1549 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1550 | if (extent_op) |
| 1551 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1552 | |
| 1553 | ptr = (unsigned long)ei + item_offset; |
| 1554 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1555 | if (ptr < end - size) |
| 1556 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1557 | end - size - ptr); |
| 1558 | |
| 1559 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1560 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1561 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1562 | struct btrfs_extent_data_ref *dref; |
| 1563 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1564 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1565 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1566 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1567 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1568 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1569 | struct btrfs_shared_data_ref *sref; |
| 1570 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1571 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1572 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1573 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1574 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1575 | } else { |
| 1576 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1577 | } |
| 1578 | btrfs_mark_buffer_dirty(leaf); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1583 | struct btrfs_root *root, |
| 1584 | struct btrfs_path *path, |
| 1585 | struct btrfs_extent_inline_ref **ref_ret, |
| 1586 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1587 | u64 root_objectid, u64 owner, u64 offset) |
| 1588 | { |
| 1589 | int ret; |
| 1590 | |
| 1591 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1592 | bytenr, num_bytes, parent, |
| 1593 | root_objectid, owner, offset, 0); |
| 1594 | if (ret != -ENOENT) |
| 1595 | return ret; |
| 1596 | |
| 1597 | btrfs_release_path(root, path); |
| 1598 | *ref_ret = NULL; |
| 1599 | |
| 1600 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1601 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1602 | root_objectid); |
| 1603 | } else { |
| 1604 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1605 | root_objectid, owner, offset); |
| 1606 | } |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * helper to update/remove inline back ref |
| 1612 | */ |
| 1613 | static noinline_for_stack |
| 1614 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1615 | struct btrfs_root *root, |
| 1616 | struct btrfs_path *path, |
| 1617 | struct btrfs_extent_inline_ref *iref, |
| 1618 | int refs_to_mod, |
| 1619 | struct btrfs_delayed_extent_op *extent_op) |
| 1620 | { |
| 1621 | struct extent_buffer *leaf; |
| 1622 | struct btrfs_extent_item *ei; |
| 1623 | struct btrfs_extent_data_ref *dref = NULL; |
| 1624 | struct btrfs_shared_data_ref *sref = NULL; |
| 1625 | unsigned long ptr; |
| 1626 | unsigned long end; |
| 1627 | u32 item_size; |
| 1628 | int size; |
| 1629 | int type; |
| 1630 | int ret; |
| 1631 | u64 refs; |
| 1632 | |
| 1633 | leaf = path->nodes[0]; |
| 1634 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1635 | refs = btrfs_extent_refs(leaf, ei); |
| 1636 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1637 | refs += refs_to_mod; |
| 1638 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1639 | if (extent_op) |
| 1640 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1641 | |
| 1642 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1643 | |
| 1644 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1645 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1646 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1647 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1648 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1649 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1650 | } else { |
| 1651 | refs = 1; |
| 1652 | BUG_ON(refs_to_mod != -1); |
| 1653 | } |
| 1654 | |
| 1655 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1656 | refs += refs_to_mod; |
| 1657 | |
| 1658 | if (refs > 0) { |
| 1659 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1660 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1661 | else |
| 1662 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1663 | } else { |
| 1664 | size = btrfs_extent_inline_ref_size(type); |
| 1665 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1666 | ptr = (unsigned long)iref; |
| 1667 | end = (unsigned long)ei + item_size; |
| 1668 | if (ptr + size < end) |
| 1669 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1670 | end - ptr - size); |
| 1671 | item_size -= size; |
| 1672 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1673 | BUG_ON(ret); |
| 1674 | } |
| 1675 | btrfs_mark_buffer_dirty(leaf); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | static noinline_for_stack |
| 1680 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1681 | struct btrfs_root *root, |
| 1682 | struct btrfs_path *path, |
| 1683 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1684 | u64 root_objectid, u64 owner, |
| 1685 | u64 offset, int refs_to_add, |
| 1686 | struct btrfs_delayed_extent_op *extent_op) |
| 1687 | { |
| 1688 | struct btrfs_extent_inline_ref *iref; |
| 1689 | int ret; |
| 1690 | |
| 1691 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1692 | bytenr, num_bytes, parent, |
| 1693 | root_objectid, owner, offset, 1); |
| 1694 | if (ret == 0) { |
| 1695 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1696 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1697 | refs_to_add, extent_op); |
| 1698 | } else if (ret == -ENOENT) { |
| 1699 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1700 | parent, root_objectid, |
| 1701 | owner, offset, refs_to_add, |
| 1702 | extent_op); |
| 1703 | } |
| 1704 | return ret; |
| 1705 | } |
| 1706 | |
| 1707 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1708 | struct btrfs_root *root, |
| 1709 | struct btrfs_path *path, |
| 1710 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1711 | u64 owner, u64 offset, int refs_to_add) |
| 1712 | { |
| 1713 | int ret; |
| 1714 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1715 | BUG_ON(refs_to_add != 1); |
| 1716 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1717 | parent, root_objectid); |
| 1718 | } else { |
| 1719 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1720 | parent, root_objectid, |
| 1721 | owner, offset, refs_to_add); |
| 1722 | } |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1727 | struct btrfs_root *root, |
| 1728 | struct btrfs_path *path, |
| 1729 | struct btrfs_extent_inline_ref *iref, |
| 1730 | int refs_to_drop, int is_data) |
| 1731 | { |
| 1732 | int ret; |
| 1733 | |
| 1734 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1735 | if (iref) { |
| 1736 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1737 | -refs_to_drop, NULL); |
| 1738 | } else if (is_data) { |
| 1739 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1740 | } else { |
| 1741 | ret = btrfs_del_item(trans, root, path); |
| 1742 | } |
| 1743 | return ret; |
| 1744 | } |
| 1745 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1746 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1747 | u64 start, u64 len) |
| 1748 | { |
Christoph Hellwig | 746cd1e | 2009-09-12 07:35:43 +0200 | [diff] [blame] | 1749 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, |
Dmitry Monakhov | fbd9b09 | 2010-04-28 17:55:06 +0400 | [diff] [blame] | 1750 | BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1751 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1752 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1753 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1754 | u64 num_bytes) |
| 1755 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1756 | int ret; |
| 1757 | u64 map_length = num_bytes; |
| 1758 | struct btrfs_multi_bio *multi = NULL; |
| 1759 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1760 | if (!btrfs_test_opt(root, DISCARD)) |
| 1761 | return 0; |
| 1762 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1763 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1764 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1765 | bytenr, &map_length, &multi, 0); |
| 1766 | if (!ret) { |
| 1767 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1768 | int i; |
| 1769 | |
| 1770 | if (map_length > num_bytes) |
| 1771 | map_length = num_bytes; |
| 1772 | |
| 1773 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1774 | btrfs_issue_discard(stripe->dev->bdev, |
| 1775 | stripe->physical, |
| 1776 | map_length); |
| 1777 | } |
| 1778 | kfree(multi); |
| 1779 | } |
| 1780 | |
| 1781 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1782 | } |
| 1783 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1784 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1785 | struct btrfs_root *root, |
| 1786 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1787 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1788 | { |
| 1789 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1790 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1791 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1792 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1793 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1794 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1795 | parent, root_objectid, (int)owner, |
| 1796 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1797 | } else { |
| 1798 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1799 | parent, root_objectid, owner, offset, |
| 1800 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1801 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1802 | return ret; |
| 1803 | } |
| 1804 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1805 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1806 | struct btrfs_root *root, |
| 1807 | u64 bytenr, u64 num_bytes, |
| 1808 | u64 parent, u64 root_objectid, |
| 1809 | u64 owner, u64 offset, int refs_to_add, |
| 1810 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1811 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1812 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1813 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1814 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1815 | u64 refs; |
| 1816 | int ret; |
| 1817 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1818 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1819 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1820 | if (!path) |
| 1821 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1822 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1823 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1824 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1825 | /* this will setup the path even if it fails to insert the back ref */ |
| 1826 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1827 | path, bytenr, num_bytes, parent, |
| 1828 | root_objectid, owner, offset, |
| 1829 | refs_to_add, extent_op); |
| 1830 | if (ret == 0) |
| 1831 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1832 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1833 | if (ret != -EAGAIN) { |
| 1834 | err = ret; |
| 1835 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1836 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1837 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1838 | leaf = path->nodes[0]; |
| 1839 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1840 | refs = btrfs_extent_refs(leaf, item); |
| 1841 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1842 | if (extent_op) |
| 1843 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1844 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1845 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1846 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1847 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1848 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1849 | path->leave_spinning = 1; |
| 1850 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1851 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1852 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1853 | path, bytenr, parent, root_objectid, |
| 1854 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1855 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1856 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1857 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1858 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1859 | } |
| 1860 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1861 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1862 | struct btrfs_root *root, |
| 1863 | struct btrfs_delayed_ref_node *node, |
| 1864 | struct btrfs_delayed_extent_op *extent_op, |
| 1865 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1866 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1867 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1868 | struct btrfs_delayed_data_ref *ref; |
| 1869 | struct btrfs_key ins; |
| 1870 | u64 parent = 0; |
| 1871 | u64 ref_root = 0; |
| 1872 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1873 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1874 | ins.objectid = node->bytenr; |
| 1875 | ins.offset = node->num_bytes; |
| 1876 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1877 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1878 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1879 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1880 | parent = ref->parent; |
| 1881 | else |
| 1882 | ref_root = ref->root; |
| 1883 | |
| 1884 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1885 | if (extent_op) { |
| 1886 | BUG_ON(extent_op->update_key); |
| 1887 | flags |= extent_op->flags_to_set; |
| 1888 | } |
| 1889 | ret = alloc_reserved_file_extent(trans, root, |
| 1890 | parent, ref_root, flags, |
| 1891 | ref->objectid, ref->offset, |
| 1892 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1893 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1894 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1895 | node->num_bytes, parent, |
| 1896 | ref_root, ref->objectid, |
| 1897 | ref->offset, node->ref_mod, |
| 1898 | extent_op); |
| 1899 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1900 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1901 | node->num_bytes, parent, |
| 1902 | ref_root, ref->objectid, |
| 1903 | ref->offset, node->ref_mod, |
| 1904 | extent_op); |
| 1905 | } else { |
| 1906 | BUG(); |
| 1907 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1908 | return ret; |
| 1909 | } |
| 1910 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1911 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1912 | struct extent_buffer *leaf, |
| 1913 | struct btrfs_extent_item *ei) |
| 1914 | { |
| 1915 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1916 | if (extent_op->update_flags) { |
| 1917 | flags |= extent_op->flags_to_set; |
| 1918 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1919 | } |
| 1920 | |
| 1921 | if (extent_op->update_key) { |
| 1922 | struct btrfs_tree_block_info *bi; |
| 1923 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1924 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1925 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1930 | struct btrfs_root *root, |
| 1931 | struct btrfs_delayed_ref_node *node, |
| 1932 | struct btrfs_delayed_extent_op *extent_op) |
| 1933 | { |
| 1934 | struct btrfs_key key; |
| 1935 | struct btrfs_path *path; |
| 1936 | struct btrfs_extent_item *ei; |
| 1937 | struct extent_buffer *leaf; |
| 1938 | u32 item_size; |
| 1939 | int ret; |
| 1940 | int err = 0; |
| 1941 | |
| 1942 | path = btrfs_alloc_path(); |
| 1943 | if (!path) |
| 1944 | return -ENOMEM; |
| 1945 | |
| 1946 | key.objectid = node->bytenr; |
| 1947 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1948 | key.offset = node->num_bytes; |
| 1949 | |
| 1950 | path->reada = 1; |
| 1951 | path->leave_spinning = 1; |
| 1952 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1953 | path, 0, 1); |
| 1954 | if (ret < 0) { |
| 1955 | err = ret; |
| 1956 | goto out; |
| 1957 | } |
| 1958 | if (ret > 0) { |
| 1959 | err = -EIO; |
| 1960 | goto out; |
| 1961 | } |
| 1962 | |
| 1963 | leaf = path->nodes[0]; |
| 1964 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1965 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1966 | if (item_size < sizeof(*ei)) { |
| 1967 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1968 | path, (u64)-1, 0); |
| 1969 | if (ret < 0) { |
| 1970 | err = ret; |
| 1971 | goto out; |
| 1972 | } |
| 1973 | leaf = path->nodes[0]; |
| 1974 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1975 | } |
| 1976 | #endif |
| 1977 | BUG_ON(item_size < sizeof(*ei)); |
| 1978 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1979 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1980 | |
| 1981 | btrfs_mark_buffer_dirty(leaf); |
| 1982 | out: |
| 1983 | btrfs_free_path(path); |
| 1984 | return err; |
| 1985 | } |
| 1986 | |
| 1987 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1988 | struct btrfs_root *root, |
| 1989 | struct btrfs_delayed_ref_node *node, |
| 1990 | struct btrfs_delayed_extent_op *extent_op, |
| 1991 | int insert_reserved) |
| 1992 | { |
| 1993 | int ret = 0; |
| 1994 | struct btrfs_delayed_tree_ref *ref; |
| 1995 | struct btrfs_key ins; |
| 1996 | u64 parent = 0; |
| 1997 | u64 ref_root = 0; |
| 1998 | |
| 1999 | ins.objectid = node->bytenr; |
| 2000 | ins.offset = node->num_bytes; |
| 2001 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2002 | |
| 2003 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2004 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2005 | parent = ref->parent; |
| 2006 | else |
| 2007 | ref_root = ref->root; |
| 2008 | |
| 2009 | BUG_ON(node->ref_mod != 1); |
| 2010 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2011 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2012 | !extent_op->update_key); |
| 2013 | ret = alloc_reserved_tree_block(trans, root, |
| 2014 | parent, ref_root, |
| 2015 | extent_op->flags_to_set, |
| 2016 | &extent_op->key, |
| 2017 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2018 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2019 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2020 | node->num_bytes, parent, ref_root, |
| 2021 | ref->level, 0, 1, extent_op); |
| 2022 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2023 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2024 | node->num_bytes, parent, ref_root, |
| 2025 | ref->level, 0, 1, extent_op); |
| 2026 | } else { |
| 2027 | BUG(); |
| 2028 | } |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2032 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2033 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2034 | struct btrfs_root *root, |
| 2035 | struct btrfs_delayed_ref_node *node, |
| 2036 | struct btrfs_delayed_extent_op *extent_op, |
| 2037 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2038 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2039 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2040 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2041 | struct btrfs_delayed_ref_head *head; |
| 2042 | /* |
| 2043 | * we've hit the end of the chain and we were supposed |
| 2044 | * to insert this extent into the tree. But, it got |
| 2045 | * deleted before we ever needed to insert it, so all |
| 2046 | * we have to do is clean up the accounting |
| 2047 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2048 | BUG_ON(extent_op); |
| 2049 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2050 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2051 | btrfs_pin_extent(root, node->bytenr, |
| 2052 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2053 | if (head->is_data) { |
| 2054 | ret = btrfs_del_csums(trans, root, |
| 2055 | node->bytenr, |
| 2056 | node->num_bytes); |
| 2057 | BUG_ON(ret); |
| 2058 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2059 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2060 | mutex_unlock(&head->mutex); |
| 2061 | return 0; |
| 2062 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2063 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2064 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2065 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2066 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2067 | insert_reserved); |
| 2068 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2069 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2070 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2071 | insert_reserved); |
| 2072 | else |
| 2073 | BUG(); |
| 2074 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2075 | } |
| 2076 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2077 | static noinline struct btrfs_delayed_ref_node * |
| 2078 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2079 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2080 | struct rb_node *node; |
| 2081 | struct btrfs_delayed_ref_node *ref; |
| 2082 | int action = BTRFS_ADD_DELAYED_REF; |
| 2083 | again: |
| 2084 | /* |
| 2085 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2086 | * this prevents ref count from going down to zero when |
| 2087 | * there still are pending delayed ref. |
| 2088 | */ |
| 2089 | node = rb_prev(&head->node.rb_node); |
| 2090 | while (1) { |
| 2091 | if (!node) |
| 2092 | break; |
| 2093 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2094 | rb_node); |
| 2095 | if (ref->bytenr != head->node.bytenr) |
| 2096 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2097 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2098 | return ref; |
| 2099 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2100 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2101 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2102 | action = BTRFS_DROP_DELAYED_REF; |
| 2103 | goto again; |
| 2104 | } |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2108 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2109 | struct btrfs_root *root, |
| 2110 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2111 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2112 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2113 | struct btrfs_delayed_ref_node *ref; |
| 2114 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2115 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2116 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2117 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2118 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2119 | |
| 2120 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2121 | while (1) { |
| 2122 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2123 | /* pick a new head ref from the cluster list */ |
| 2124 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2125 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2126 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2127 | locked_ref = list_entry(cluster->next, |
| 2128 | struct btrfs_delayed_ref_head, cluster); |
| 2129 | |
| 2130 | /* grab the lock that says we are going to process |
| 2131 | * all the refs for this head */ |
| 2132 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2133 | |
| 2134 | /* |
| 2135 | * we may have dropped the spin lock to get the head |
| 2136 | * mutex lock, and that might have given someone else |
| 2137 | * time to free the head. If that's true, it has been |
| 2138 | * removed from our list and we can move on. |
| 2139 | */ |
| 2140 | if (ret == -EAGAIN) { |
| 2141 | locked_ref = NULL; |
| 2142 | count++; |
| 2143 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | /* |
| 2148 | * record the must insert reserved flag before we |
| 2149 | * drop the spin lock. |
| 2150 | */ |
| 2151 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2152 | locked_ref->must_insert_reserved = 0; |
| 2153 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2154 | extent_op = locked_ref->extent_op; |
| 2155 | locked_ref->extent_op = NULL; |
| 2156 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2157 | /* |
| 2158 | * locked_ref is the head node, so we have to go one |
| 2159 | * node back for any delayed ref updates |
| 2160 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2161 | ref = select_delayed_ref(locked_ref); |
| 2162 | if (!ref) { |
| 2163 | /* All delayed refs have been processed, Go ahead |
| 2164 | * and send the head node to run_one_delayed_ref, |
| 2165 | * so that any accounting fixes can happen |
| 2166 | */ |
| 2167 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2168 | |
| 2169 | if (extent_op && must_insert_reserved) { |
| 2170 | kfree(extent_op); |
| 2171 | extent_op = NULL; |
| 2172 | } |
| 2173 | |
| 2174 | if (extent_op) { |
| 2175 | spin_unlock(&delayed_refs->lock); |
| 2176 | |
| 2177 | ret = run_delayed_extent_op(trans, root, |
| 2178 | ref, extent_op); |
| 2179 | BUG_ON(ret); |
| 2180 | kfree(extent_op); |
| 2181 | |
| 2182 | cond_resched(); |
| 2183 | spin_lock(&delayed_refs->lock); |
| 2184 | continue; |
| 2185 | } |
| 2186 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2187 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2188 | locked_ref = NULL; |
| 2189 | } |
| 2190 | |
| 2191 | ref->in_tree = 0; |
| 2192 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2193 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2194 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2195 | spin_unlock(&delayed_refs->lock); |
| 2196 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2197 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2198 | must_insert_reserved); |
| 2199 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2200 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2201 | btrfs_put_delayed_ref(ref); |
| 2202 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2203 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2204 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2205 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2206 | spin_lock(&delayed_refs->lock); |
| 2207 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2208 | return count; |
| 2209 | } |
| 2210 | |
| 2211 | /* |
| 2212 | * this starts processing the delayed reference count updates and |
| 2213 | * extent insertions we have queued up so far. count can be |
| 2214 | * 0, which means to process everything in the tree at the start |
| 2215 | * of the run (but not newly added entries), or it can be some target |
| 2216 | * number you'd like to process. |
| 2217 | */ |
| 2218 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2219 | struct btrfs_root *root, unsigned long count) |
| 2220 | { |
| 2221 | struct rb_node *node; |
| 2222 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2223 | struct btrfs_delayed_ref_node *ref; |
| 2224 | struct list_head cluster; |
| 2225 | int ret; |
| 2226 | int run_all = count == (unsigned long)-1; |
| 2227 | int run_most = 0; |
| 2228 | |
| 2229 | if (root == root->fs_info->extent_root) |
| 2230 | root = root->fs_info->tree_root; |
| 2231 | |
| 2232 | delayed_refs = &trans->transaction->delayed_refs; |
| 2233 | INIT_LIST_HEAD(&cluster); |
| 2234 | again: |
| 2235 | spin_lock(&delayed_refs->lock); |
| 2236 | if (count == 0) { |
| 2237 | count = delayed_refs->num_entries * 2; |
| 2238 | run_most = 1; |
| 2239 | } |
| 2240 | while (1) { |
| 2241 | if (!(run_all || run_most) && |
| 2242 | delayed_refs->num_heads_ready < 64) |
| 2243 | break; |
| 2244 | |
| 2245 | /* |
| 2246 | * go find something we can process in the rbtree. We start at |
| 2247 | * the beginning of the tree, and then build a cluster |
| 2248 | * of refs to process starting at the first one we are able to |
| 2249 | * lock |
| 2250 | */ |
| 2251 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2252 | delayed_refs->run_delayed_start); |
| 2253 | if (ret) |
| 2254 | break; |
| 2255 | |
| 2256 | ret = run_clustered_refs(trans, root, &cluster); |
| 2257 | BUG_ON(ret < 0); |
| 2258 | |
| 2259 | count -= min_t(unsigned long, ret, count); |
| 2260 | |
| 2261 | if (count == 0) |
| 2262 | break; |
| 2263 | } |
| 2264 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2265 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2266 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2267 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2268 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2269 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2270 | |
| 2271 | while (node) { |
| 2272 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2273 | rb_node); |
| 2274 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2275 | struct btrfs_delayed_ref_head *head; |
| 2276 | |
| 2277 | head = btrfs_delayed_node_to_head(ref); |
| 2278 | atomic_inc(&ref->refs); |
| 2279 | |
| 2280 | spin_unlock(&delayed_refs->lock); |
| 2281 | mutex_lock(&head->mutex); |
| 2282 | mutex_unlock(&head->mutex); |
| 2283 | |
| 2284 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2285 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2286 | goto again; |
| 2287 | } |
| 2288 | node = rb_next(node); |
| 2289 | } |
| 2290 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2291 | schedule_timeout(1); |
| 2292 | goto again; |
| 2293 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2294 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2295 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2299 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2300 | struct btrfs_root *root, |
| 2301 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2302 | int is_data) |
| 2303 | { |
| 2304 | struct btrfs_delayed_extent_op *extent_op; |
| 2305 | int ret; |
| 2306 | |
| 2307 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2308 | if (!extent_op) |
| 2309 | return -ENOMEM; |
| 2310 | |
| 2311 | extent_op->flags_to_set = flags; |
| 2312 | extent_op->update_flags = 1; |
| 2313 | extent_op->update_key = 0; |
| 2314 | extent_op->is_data = is_data ? 1 : 0; |
| 2315 | |
| 2316 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2317 | if (ret) |
| 2318 | kfree(extent_op); |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
| 2322 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2323 | struct btrfs_root *root, |
| 2324 | struct btrfs_path *path, |
| 2325 | u64 objectid, u64 offset, u64 bytenr) |
| 2326 | { |
| 2327 | struct btrfs_delayed_ref_head *head; |
| 2328 | struct btrfs_delayed_ref_node *ref; |
| 2329 | struct btrfs_delayed_data_ref *data_ref; |
| 2330 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2331 | struct rb_node *node; |
| 2332 | int ret = 0; |
| 2333 | |
| 2334 | ret = -ENOENT; |
| 2335 | delayed_refs = &trans->transaction->delayed_refs; |
| 2336 | spin_lock(&delayed_refs->lock); |
| 2337 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2338 | if (!head) |
| 2339 | goto out; |
| 2340 | |
| 2341 | if (!mutex_trylock(&head->mutex)) { |
| 2342 | atomic_inc(&head->node.refs); |
| 2343 | spin_unlock(&delayed_refs->lock); |
| 2344 | |
| 2345 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2346 | |
| 2347 | mutex_lock(&head->mutex); |
| 2348 | mutex_unlock(&head->mutex); |
| 2349 | btrfs_put_delayed_ref(&head->node); |
| 2350 | return -EAGAIN; |
| 2351 | } |
| 2352 | |
| 2353 | node = rb_prev(&head->node.rb_node); |
| 2354 | if (!node) |
| 2355 | goto out_unlock; |
| 2356 | |
| 2357 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2358 | |
| 2359 | if (ref->bytenr != bytenr) |
| 2360 | goto out_unlock; |
| 2361 | |
| 2362 | ret = 1; |
| 2363 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2364 | goto out_unlock; |
| 2365 | |
| 2366 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2367 | |
| 2368 | node = rb_prev(node); |
| 2369 | if (node) { |
| 2370 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2371 | if (ref->bytenr == bytenr) |
| 2372 | goto out_unlock; |
| 2373 | } |
| 2374 | |
| 2375 | if (data_ref->root != root->root_key.objectid || |
| 2376 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2377 | goto out_unlock; |
| 2378 | |
| 2379 | ret = 0; |
| 2380 | out_unlock: |
| 2381 | mutex_unlock(&head->mutex); |
| 2382 | out: |
| 2383 | spin_unlock(&delayed_refs->lock); |
| 2384 | return ret; |
| 2385 | } |
| 2386 | |
| 2387 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2388 | struct btrfs_root *root, |
| 2389 | struct btrfs_path *path, |
| 2390 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2391 | { |
| 2392 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2393 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2394 | struct btrfs_extent_data_ref *ref; |
| 2395 | struct btrfs_extent_inline_ref *iref; |
| 2396 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2397 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2398 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2399 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2400 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2401 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2402 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2403 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2404 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2405 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2406 | if (ret < 0) |
| 2407 | goto out; |
| 2408 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2409 | |
| 2410 | ret = -ENOENT; |
| 2411 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2412 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2413 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2414 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2415 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2416 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2417 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2418 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2419 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2420 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2421 | ret = 1; |
| 2422 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2423 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2424 | if (item_size < sizeof(*ei)) { |
| 2425 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2426 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2427 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2428 | #endif |
| 2429 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2430 | |
| 2431 | if (item_size != sizeof(*ei) + |
| 2432 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2433 | goto out; |
| 2434 | |
| 2435 | if (btrfs_extent_generation(leaf, ei) <= |
| 2436 | btrfs_root_last_snapshot(&root->root_item)) |
| 2437 | goto out; |
| 2438 | |
| 2439 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2440 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2441 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2442 | goto out; |
| 2443 | |
| 2444 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2445 | if (btrfs_extent_refs(leaf, ei) != |
| 2446 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2447 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2448 | root->root_key.objectid || |
| 2449 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2450 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2451 | goto out; |
| 2452 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2453 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2454 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2455 | return ret; |
| 2456 | } |
| 2457 | |
| 2458 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2459 | struct btrfs_root *root, |
| 2460 | u64 objectid, u64 offset, u64 bytenr) |
| 2461 | { |
| 2462 | struct btrfs_path *path; |
| 2463 | int ret; |
| 2464 | int ret2; |
| 2465 | |
| 2466 | path = btrfs_alloc_path(); |
| 2467 | if (!path) |
| 2468 | return -ENOENT; |
| 2469 | |
| 2470 | do { |
| 2471 | ret = check_committed_ref(trans, root, path, objectid, |
| 2472 | offset, bytenr); |
| 2473 | if (ret && ret != -ENOENT) |
| 2474 | goto out; |
| 2475 | |
| 2476 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2477 | offset, bytenr); |
| 2478 | } while (ret2 == -EAGAIN); |
| 2479 | |
| 2480 | if (ret2 && ret2 != -ENOENT) { |
| 2481 | ret = ret2; |
| 2482 | goto out; |
| 2483 | } |
| 2484 | |
| 2485 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2486 | ret = 0; |
| 2487 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2488 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2489 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2490 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2491 | return ret; |
| 2492 | } |
| 2493 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2494 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2495 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2496 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2497 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2498 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2499 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2500 | u64 root_gen; |
| 2501 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2502 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2503 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2504 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2505 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2506 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2507 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2508 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2509 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2510 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2511 | shared = 0; |
| 2512 | root_gen = root->root_key.offset; |
| 2513 | } else { |
| 2514 | shared = 1; |
| 2515 | root_gen = trans->transid - 1; |
| 2516 | } |
| 2517 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2518 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2519 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2520 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2521 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2522 | struct btrfs_leaf_ref *ref; |
| 2523 | struct btrfs_extent_info *info; |
| 2524 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2525 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2526 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2527 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2528 | goto out; |
| 2529 | } |
| 2530 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2531 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2532 | ref->bytenr = buf->start; |
| 2533 | ref->owner = btrfs_header_owner(buf); |
| 2534 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2535 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2536 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2537 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2538 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2539 | u64 disk_bytenr; |
| 2540 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2541 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2542 | continue; |
| 2543 | fi = btrfs_item_ptr(buf, i, |
| 2544 | struct btrfs_file_extent_item); |
| 2545 | if (btrfs_file_extent_type(buf, fi) == |
| 2546 | BTRFS_FILE_EXTENT_INLINE) |
| 2547 | continue; |
| 2548 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2549 | if (disk_bytenr == 0) |
| 2550 | continue; |
| 2551 | |
| 2552 | info->bytenr = disk_bytenr; |
| 2553 | info->num_bytes = |
| 2554 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2555 | info->objectid = key.objectid; |
| 2556 | info->offset = key.offset; |
| 2557 | info++; |
| 2558 | } |
| 2559 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2560 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2561 | if (ret == -EEXIST && shared) { |
| 2562 | struct btrfs_leaf_ref *old; |
| 2563 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2564 | BUG_ON(!old); |
| 2565 | btrfs_remove_leaf_ref(root, old); |
| 2566 | btrfs_free_leaf_ref(root, old); |
| 2567 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2568 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2569 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2570 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2571 | } |
| 2572 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2573 | return ret; |
| 2574 | } |
| 2575 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2576 | /* when a block goes through cow, we update the reference counts of |
| 2577 | * everything that block points to. The internal pointers of the block |
| 2578 | * can be in just about any order, and it is likely to have clusters of |
| 2579 | * things that are close together and clusters of things that are not. |
| 2580 | * |
| 2581 | * To help reduce the seeks that come with updating all of these reference |
| 2582 | * counts, sort them by byte number before actual updates are done. |
| 2583 | * |
| 2584 | * struct refsort is used to match byte number to slot in the btree block. |
| 2585 | * we sort based on the byte number and then use the slot to actually |
| 2586 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2587 | * |
| 2588 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2589 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2590 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2591 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2592 | */ |
| 2593 | struct refsort { |
| 2594 | u64 bytenr; |
| 2595 | u32 slot; |
| 2596 | }; |
| 2597 | |
| 2598 | /* |
| 2599 | * for passing into sort() |
| 2600 | */ |
| 2601 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2602 | { |
| 2603 | const struct refsort *a = a_void; |
| 2604 | const struct refsort *b = b_void; |
| 2605 | |
| 2606 | if (a->bytenr < b->bytenr) |
| 2607 | return -1; |
| 2608 | if (a->bytenr > b->bytenr) |
| 2609 | return 1; |
| 2610 | return 0; |
| 2611 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2612 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2613 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2614 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2615 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2616 | struct extent_buffer *buf, |
| 2617 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2618 | { |
| 2619 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2620 | u64 num_bytes; |
| 2621 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2622 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2623 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2624 | struct btrfs_key key; |
| 2625 | struct btrfs_file_extent_item *fi; |
| 2626 | int i; |
| 2627 | int level; |
| 2628 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2629 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2630 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2631 | |
| 2632 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2633 | nritems = btrfs_header_nritems(buf); |
| 2634 | level = btrfs_header_level(buf); |
| 2635 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2636 | if (!root->ref_cows && level == 0) |
| 2637 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2638 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2639 | if (inc) |
| 2640 | process_func = btrfs_inc_extent_ref; |
| 2641 | else |
| 2642 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2643 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2644 | if (full_backref) |
| 2645 | parent = buf->start; |
| 2646 | else |
| 2647 | parent = 0; |
| 2648 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2649 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2650 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2651 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2652 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2653 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2654 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2655 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2656 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2657 | BTRFS_FILE_EXTENT_INLINE) |
| 2658 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2659 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2660 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2661 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2662 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2663 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2664 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2665 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2666 | parent, ref_root, key.objectid, |
| 2667 | key.offset); |
| 2668 | if (ret) |
| 2669 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2670 | } else { |
| 2671 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2672 | num_bytes = btrfs_level_size(root, level - 1); |
| 2673 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2674 | parent, ref_root, level - 1, 0); |
| 2675 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2676 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2677 | } |
| 2678 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2679 | return 0; |
| 2680 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2681 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2682 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2683 | } |
| 2684 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2685 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2686 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2687 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2688 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2689 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2690 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2691 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2692 | struct extent_buffer *buf, int full_backref) |
| 2693 | { |
| 2694 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2695 | } |
| 2696 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2697 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2698 | struct btrfs_root *root, |
| 2699 | struct btrfs_path *path, |
| 2700 | struct btrfs_block_group_cache *cache) |
| 2701 | { |
| 2702 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2703 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2704 | unsigned long bi; |
| 2705 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2706 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2707 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2708 | if (ret < 0) |
| 2709 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2710 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2711 | |
| 2712 | leaf = path->nodes[0]; |
| 2713 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2714 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2715 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2716 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2717 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2718 | if (ret) |
| 2719 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2720 | return 0; |
| 2721 | |
| 2722 | } |
| 2723 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2724 | static struct btrfs_block_group_cache * |
| 2725 | next_block_group(struct btrfs_root *root, |
| 2726 | struct btrfs_block_group_cache *cache) |
| 2727 | { |
| 2728 | struct rb_node *node; |
| 2729 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2730 | node = rb_next(&cache->cache_node); |
| 2731 | btrfs_put_block_group(cache); |
| 2732 | if (node) { |
| 2733 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2734 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2735 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2736 | } else |
| 2737 | cache = NULL; |
| 2738 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2739 | return cache; |
| 2740 | } |
| 2741 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2742 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2743 | struct btrfs_trans_handle *trans, |
| 2744 | struct btrfs_path *path) |
| 2745 | { |
| 2746 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2747 | struct inode *inode = NULL; |
| 2748 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2749 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2750 | int num_pages = 0; |
| 2751 | int retries = 0; |
| 2752 | int ret = 0; |
| 2753 | |
| 2754 | /* |
| 2755 | * If this block group is smaller than 100 megs don't bother caching the |
| 2756 | * block group. |
| 2757 | */ |
| 2758 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2759 | spin_lock(&block_group->lock); |
| 2760 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2761 | spin_unlock(&block_group->lock); |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
| 2765 | again: |
| 2766 | inode = lookup_free_space_inode(root, block_group, path); |
| 2767 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2768 | ret = PTR_ERR(inode); |
| 2769 | btrfs_release_path(root, path); |
| 2770 | goto out; |
| 2771 | } |
| 2772 | |
| 2773 | if (IS_ERR(inode)) { |
| 2774 | BUG_ON(retries); |
| 2775 | retries++; |
| 2776 | |
| 2777 | if (block_group->ro) |
| 2778 | goto out_free; |
| 2779 | |
| 2780 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2781 | if (ret) |
| 2782 | goto out_free; |
| 2783 | goto again; |
| 2784 | } |
| 2785 | |
| 2786 | /* |
| 2787 | * We want to set the generation to 0, that way if anything goes wrong |
| 2788 | * from here on out we know not to trust this cache when we load up next |
| 2789 | * time. |
| 2790 | */ |
| 2791 | BTRFS_I(inode)->generation = 0; |
| 2792 | ret = btrfs_update_inode(trans, root, inode); |
| 2793 | WARN_ON(ret); |
| 2794 | |
| 2795 | if (i_size_read(inode) > 0) { |
| 2796 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2797 | inode); |
| 2798 | if (ret) |
| 2799 | goto out_put; |
| 2800 | } |
| 2801 | |
| 2802 | spin_lock(&block_group->lock); |
| 2803 | if (block_group->cached != BTRFS_CACHE_FINISHED) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2804 | /* We're not cached, don't bother trying to write stuff out */ |
| 2805 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2806 | spin_unlock(&block_group->lock); |
| 2807 | goto out_put; |
| 2808 | } |
| 2809 | spin_unlock(&block_group->lock); |
| 2810 | |
| 2811 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); |
| 2812 | if (!num_pages) |
| 2813 | num_pages = 1; |
| 2814 | |
| 2815 | /* |
| 2816 | * Just to make absolutely sure we have enough space, we're going to |
| 2817 | * preallocate 12 pages worth of space for each block group. In |
| 2818 | * practice we ought to use at most 8, but we need extra space so we can |
| 2819 | * add our header and have a terminator between the extents and the |
| 2820 | * bitmaps. |
| 2821 | */ |
| 2822 | num_pages *= 16; |
| 2823 | num_pages *= PAGE_CACHE_SIZE; |
| 2824 | |
| 2825 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 2826 | if (ret) |
| 2827 | goto out_put; |
| 2828 | |
| 2829 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2830 | num_pages, num_pages, |
| 2831 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2832 | if (!ret) |
| 2833 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2834 | btrfs_free_reserved_data_space(inode, num_pages); |
| 2835 | out_put: |
| 2836 | iput(inode); |
| 2837 | out_free: |
| 2838 | btrfs_release_path(root, path); |
| 2839 | out: |
| 2840 | spin_lock(&block_group->lock); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2841 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2842 | spin_unlock(&block_group->lock); |
| 2843 | |
| 2844 | return ret; |
| 2845 | } |
| 2846 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2847 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2848 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2849 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2850 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2851 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2852 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2853 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2854 | |
| 2855 | path = btrfs_alloc_path(); |
| 2856 | if (!path) |
| 2857 | return -ENOMEM; |
| 2858 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2859 | again: |
| 2860 | while (1) { |
| 2861 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2862 | while (cache) { |
| 2863 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2864 | break; |
| 2865 | cache = next_block_group(root, cache); |
| 2866 | } |
| 2867 | if (!cache) { |
| 2868 | if (last == 0) |
| 2869 | break; |
| 2870 | last = 0; |
| 2871 | continue; |
| 2872 | } |
| 2873 | err = cache_save_setup(cache, trans, path); |
| 2874 | last = cache->key.objectid + cache->key.offset; |
| 2875 | btrfs_put_block_group(cache); |
| 2876 | } |
| 2877 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2878 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2879 | if (last == 0) { |
| 2880 | err = btrfs_run_delayed_refs(trans, root, |
| 2881 | (unsigned long)-1); |
| 2882 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2883 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2884 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2885 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2886 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2887 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 2888 | btrfs_put_block_group(cache); |
| 2889 | goto again; |
| 2890 | } |
| 2891 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2892 | if (cache->dirty) |
| 2893 | break; |
| 2894 | cache = next_block_group(root, cache); |
| 2895 | } |
| 2896 | if (!cache) { |
| 2897 | if (last == 0) |
| 2898 | break; |
| 2899 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2900 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2901 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2902 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2903 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 2904 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2905 | cache->dirty = 0; |
| 2906 | last = cache->key.objectid + cache->key.offset; |
| 2907 | |
| 2908 | err = write_one_cache_group(trans, root, path, cache); |
| 2909 | BUG_ON(err); |
| 2910 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2911 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2912 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2913 | while (1) { |
| 2914 | /* |
| 2915 | * I don't think this is needed since we're just marking our |
| 2916 | * preallocated extent as written, but just in case it can't |
| 2917 | * hurt. |
| 2918 | */ |
| 2919 | if (last == 0) { |
| 2920 | err = btrfs_run_delayed_refs(trans, root, |
| 2921 | (unsigned long)-1); |
| 2922 | BUG_ON(err); |
| 2923 | } |
| 2924 | |
| 2925 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2926 | while (cache) { |
| 2927 | /* |
| 2928 | * Really this shouldn't happen, but it could if we |
| 2929 | * couldn't write the entire preallocated extent and |
| 2930 | * splitting the extent resulted in a new block. |
| 2931 | */ |
| 2932 | if (cache->dirty) { |
| 2933 | btrfs_put_block_group(cache); |
| 2934 | goto again; |
| 2935 | } |
| 2936 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2937 | break; |
| 2938 | cache = next_block_group(root, cache); |
| 2939 | } |
| 2940 | if (!cache) { |
| 2941 | if (last == 0) |
| 2942 | break; |
| 2943 | last = 0; |
| 2944 | continue; |
| 2945 | } |
| 2946 | |
| 2947 | btrfs_write_out_cache(root, trans, cache, path); |
| 2948 | |
| 2949 | /* |
| 2950 | * If we didn't have an error then the cache state is still |
| 2951 | * NEED_WRITE, so we can set it to WRITTEN. |
| 2952 | */ |
| 2953 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2954 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2955 | last = cache->key.objectid + cache->key.offset; |
| 2956 | btrfs_put_block_group(cache); |
| 2957 | } |
| 2958 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2959 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2960 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2961 | } |
| 2962 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2963 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2964 | { |
| 2965 | struct btrfs_block_group_cache *block_group; |
| 2966 | int readonly = 0; |
| 2967 | |
| 2968 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2969 | if (!block_group || block_group->ro) |
| 2970 | readonly = 1; |
| 2971 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2972 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2973 | return readonly; |
| 2974 | } |
| 2975 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2976 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2977 | u64 total_bytes, u64 bytes_used, |
| 2978 | struct btrfs_space_info **space_info) |
| 2979 | { |
| 2980 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2981 | int i; |
| 2982 | int factor; |
| 2983 | |
| 2984 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2985 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2986 | factor = 2; |
| 2987 | else |
| 2988 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2989 | |
| 2990 | found = __find_space_info(info, flags); |
| 2991 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2992 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2993 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2994 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2995 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2996 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2997 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2998 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2999 | *space_info = found; |
| 3000 | return 0; |
| 3001 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3002 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3003 | if (!found) |
| 3004 | return -ENOMEM; |
| 3005 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3006 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 3007 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3008 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3009 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3010 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 3011 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 3012 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3013 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3014 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3015 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3016 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3017 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3018 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3019 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3020 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3021 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3022 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3023 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3024 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3025 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3026 | return 0; |
| 3027 | } |
| 3028 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3029 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3030 | { |
| 3031 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3032 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3033 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3034 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3035 | if (extra_flags) { |
| 3036 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3037 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3038 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3039 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3040 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3041 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 3042 | } |
| 3043 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3044 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3045 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3046 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 3047 | /* |
| 3048 | * we add in the count of missing devices because we want |
| 3049 | * to make sure that any RAID levels on a degraded FS |
| 3050 | * continue to be honored. |
| 3051 | */ |
| 3052 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 3053 | root->fs_info->fs_devices->missing_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3054 | |
| 3055 | if (num_devices == 1) |
| 3056 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 3057 | if (num_devices < 4) |
| 3058 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 3059 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3060 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 3061 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3062 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3063 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3064 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3065 | |
| 3066 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3067 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3068 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3069 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3070 | |
| 3071 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 3072 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 3073 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 3074 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 3075 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 3076 | return flags; |
| 3077 | } |
| 3078 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3079 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3080 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3081 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3082 | flags |= root->fs_info->avail_data_alloc_bits & |
| 3083 | root->fs_info->data_alloc_profile; |
| 3084 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3085 | flags |= root->fs_info->avail_system_alloc_bits & |
| 3086 | root->fs_info->system_alloc_profile; |
| 3087 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3088 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 3089 | root->fs_info->metadata_alloc_profile; |
| 3090 | return btrfs_reduce_alloc_profile(root, flags); |
| 3091 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3092 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3093 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
| 3094 | { |
| 3095 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3096 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3097 | if (data) |
| 3098 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3099 | else if (root == root->fs_info->chunk_root) |
| 3100 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3101 | else |
| 3102 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3103 | |
| 3104 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 3108 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3109 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3110 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | /* |
| 3114 | * This will check the space that the inode allocates from to make sure we have |
| 3115 | * enough space for bytes. |
| 3116 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3117 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3118 | { |
| 3119 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3120 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3121 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3122 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3123 | |
| 3124 | /* make sure bytes are sectorsize aligned */ |
| 3125 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3126 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3127 | if (root == root->fs_info->tree_root) { |
| 3128 | alloc_chunk = 0; |
| 3129 | committed = 1; |
| 3130 | } |
| 3131 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3132 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3133 | if (!data_sinfo) |
| 3134 | goto alloc; |
| 3135 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3136 | again: |
| 3137 | /* make sure we have enough space to handle the data first */ |
| 3138 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3139 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3140 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3141 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3142 | |
| 3143 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3144 | struct btrfs_trans_handle *trans; |
| 3145 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3146 | /* |
| 3147 | * if we don't have enough free bytes in this space then we need |
| 3148 | * to alloc a new chunk. |
| 3149 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3150 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3151 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3152 | |
| 3153 | data_sinfo->force_alloc = 1; |
| 3154 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3155 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3156 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3157 | trans = btrfs_join_transaction(root, 1); |
| 3158 | if (IS_ERR(trans)) |
| 3159 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3160 | |
| 3161 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3162 | bytes + 2 * 1024 * 1024, |
| 3163 | alloc_target, 0); |
| 3164 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3165 | if (ret < 0) { |
| 3166 | if (ret != -ENOSPC) |
| 3167 | return ret; |
| 3168 | else |
| 3169 | goto commit_trans; |
| 3170 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3171 | |
| 3172 | if (!data_sinfo) { |
| 3173 | btrfs_set_inode_space_info(root, inode); |
| 3174 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3175 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3176 | goto again; |
| 3177 | } |
| 3178 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3179 | |
| 3180 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3181 | commit_trans: |
Sage Weil | dd7e0b7 | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 3182 | if (!committed && !root->fs_info->open_ioctl_trans) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3183 | committed = 1; |
| 3184 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3185 | if (IS_ERR(trans)) |
| 3186 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3187 | ret = btrfs_commit_transaction(trans, root); |
| 3188 | if (ret) |
| 3189 | return ret; |
| 3190 | goto again; |
| 3191 | } |
| 3192 | |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 3193 | #if 0 /* I hope we never need this code again, just in case */ |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3194 | printk(KERN_ERR "no space left, need %llu, %llu bytes_used, " |
| 3195 | "%llu bytes_reserved, " "%llu bytes_pinned, " |
| 3196 | "%llu bytes_readonly, %llu may use %llu total\n", |
| 3197 | (unsigned long long)bytes, |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 3198 | (unsigned long long)data_sinfo->bytes_used, |
| 3199 | (unsigned long long)data_sinfo->bytes_reserved, |
| 3200 | (unsigned long long)data_sinfo->bytes_pinned, |
| 3201 | (unsigned long long)data_sinfo->bytes_readonly, |
| 3202 | (unsigned long long)data_sinfo->bytes_may_use, |
| 3203 | (unsigned long long)data_sinfo->total_bytes); |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 3204 | #endif |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3205 | return -ENOSPC; |
| 3206 | } |
| 3207 | data_sinfo->bytes_may_use += bytes; |
| 3208 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3209 | spin_unlock(&data_sinfo->lock); |
| 3210 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3211 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | /* |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3215 | * called when we are clearing an delalloc extent from the |
| 3216 | * inode's io_tree or there was an error for whatever reason |
| 3217 | * after calling btrfs_check_data_free_space |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3218 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3219 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3220 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3221 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3222 | struct btrfs_space_info *data_sinfo; |
| 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; |
| 3228 | spin_lock(&data_sinfo->lock); |
| 3229 | data_sinfo->bytes_may_use -= bytes; |
| 3230 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3231 | spin_unlock(&data_sinfo->lock); |
| 3232 | } |
| 3233 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3234 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3235 | { |
| 3236 | struct list_head *head = &info->space_info; |
| 3237 | struct btrfs_space_info *found; |
| 3238 | |
| 3239 | rcu_read_lock(); |
| 3240 | list_for_each_entry_rcu(found, head, list) { |
| 3241 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3242 | found->force_alloc = 1; |
| 3243 | } |
| 3244 | rcu_read_unlock(); |
| 3245 | } |
| 3246 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3247 | static int should_alloc_chunk(struct btrfs_root *root, |
| 3248 | struct btrfs_space_info *sinfo, u64 alloc_bytes) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3249 | { |
| 3250 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3251 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3252 | |
| 3253 | if (sinfo->bytes_used + sinfo->bytes_reserved + |
| 3254 | alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
| 3255 | return 0; |
| 3256 | |
| 3257 | if (sinfo->bytes_used + sinfo->bytes_reserved + |
| 3258 | alloc_bytes < div_factor(num_bytes, 8)) |
| 3259 | return 0; |
| 3260 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3261 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 3262 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
| 3263 | |
| 3264 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3265 | return 0; |
| 3266 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3267 | return 1; |
| 3268 | } |
| 3269 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3270 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3271 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3272 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3273 | { |
| 3274 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3275 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3276 | int ret = 0; |
| 3277 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3278 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3279 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3280 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3281 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3282 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3283 | if (!space_info) { |
| 3284 | ret = update_space_info(extent_root->fs_info, flags, |
| 3285 | 0, 0, &space_info); |
| 3286 | BUG_ON(ret); |
| 3287 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3288 | BUG_ON(!space_info); |
| 3289 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3290 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3291 | if (space_info->force_alloc) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3292 | force = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3293 | if (space_info->full) { |
| 3294 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3295 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3296 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3297 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3298 | if (!force && !should_alloc_chunk(extent_root, space_info, |
| 3299 | alloc_bytes)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3300 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3301 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3302 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3303 | spin_unlock(&space_info->lock); |
| 3304 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3305 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3306 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3307 | * allocating mixed chunks instead of individual chunks. |
| 3308 | */ |
| 3309 | if (btrfs_mixed_space_info(space_info)) |
| 3310 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3311 | |
| 3312 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3313 | * if we're doing a data chunk, go ahead and make sure that |
| 3314 | * we keep a reasonable number of metadata chunks allocated in the |
| 3315 | * FS as well. |
| 3316 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3317 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3318 | fs_info->data_chunk_allocations++; |
| 3319 | if (!(fs_info->data_chunk_allocations % |
| 3320 | fs_info->metadata_ratio)) |
| 3321 | force_metadata_allocation(fs_info); |
| 3322 | } |
| 3323 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3324 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3325 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3326 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3327 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3328 | else |
| 3329 | ret = 1; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3330 | space_info->force_alloc = 0; |
| 3331 | spin_unlock(&space_info->lock); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3332 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3333 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3334 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3335 | } |
| 3336 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3337 | /* |
| 3338 | * shrink metadata reservation for delalloc |
| 3339 | */ |
| 3340 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3341 | struct btrfs_root *root, u64 to_reclaim, int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3342 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3343 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3344 | struct btrfs_space_info *space_info; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3345 | u64 reserved; |
| 3346 | u64 max_reclaim; |
| 3347 | u64 reclaimed = 0; |
| 3348 | int pause = 1; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3349 | int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3350 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3351 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3352 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3353 | |
| 3354 | smp_mb(); |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3355 | reserved = space_info->bytes_reserved; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3356 | |
| 3357 | if (reserved == 0) |
| 3358 | return 0; |
| 3359 | |
| 3360 | max_reclaim = min(reserved, to_reclaim); |
| 3361 | |
| 3362 | while (1) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3363 | /* have the flusher threads jump in and do some IO */ |
| 3364 | smp_mb(); |
| 3365 | nr_pages = min_t(unsigned long, nr_pages, |
| 3366 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3367 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3368 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3369 | spin_lock(&space_info->lock); |
| 3370 | if (reserved > space_info->bytes_reserved) |
| 3371 | reclaimed += reserved - space_info->bytes_reserved; |
| 3372 | reserved = space_info->bytes_reserved; |
| 3373 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3374 | |
| 3375 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3376 | break; |
| 3377 | |
| 3378 | if (trans && trans->transaction->blocked) |
| 3379 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3380 | |
| 3381 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3382 | schedule_timeout(pause); |
| 3383 | pause <<= 1; |
| 3384 | if (pause > HZ / 10) |
| 3385 | pause = HZ / 10; |
| 3386 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3387 | } |
| 3388 | return reclaimed >= to_reclaim; |
| 3389 | } |
| 3390 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3391 | /* |
| 3392 | * Retries tells us how many times we've called reserve_metadata_bytes. The |
| 3393 | * idea is if this is the first call (retries == 0) then we will add to our |
| 3394 | * reserved count if we can't make the allocation in order to hold our place |
| 3395 | * while we go and try and free up space. That way for retries > 1 we don't try |
| 3396 | * and add space, we just check to see if the amount of unused space is >= the |
| 3397 | * total space, meaning that our reservation is valid. |
| 3398 | * |
| 3399 | * However if we don't intend to retry this reservation, pass -1 as retries so |
| 3400 | * that it short circuits this logic. |
| 3401 | */ |
| 3402 | static int reserve_metadata_bytes(struct btrfs_trans_handle *trans, |
| 3403 | struct btrfs_root *root, |
| 3404 | struct btrfs_block_rsv *block_rsv, |
| 3405 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3406 | { |
| 3407 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3408 | u64 unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3409 | u64 num_bytes = orig_bytes; |
| 3410 | int retries = 0; |
| 3411 | int ret = 0; |
| 3412 | bool reserved = false; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3413 | bool committed = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3414 | |
| 3415 | again: |
| 3416 | ret = -ENOSPC; |
| 3417 | if (reserved) |
| 3418 | num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3419 | |
| 3420 | spin_lock(&space_info->lock); |
| 3421 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3422 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3423 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3424 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3425 | /* |
| 3426 | * The idea here is that we've not already over-reserved the block group |
| 3427 | * then we can go ahead and save our reservation first and then start |
| 3428 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3429 | * lets start flushing stuff first and then come back and try to make |
| 3430 | * our reservation. |
| 3431 | */ |
| 3432 | if (unused <= space_info->total_bytes) { |
Arne Jansen | 6f33434 | 2010-11-12 23:17:56 +0000 | [diff] [blame] | 3433 | unused = space_info->total_bytes - unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3434 | if (unused >= num_bytes) { |
| 3435 | if (!reserved) |
| 3436 | space_info->bytes_reserved += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3437 | ret = 0; |
| 3438 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3439 | /* |
| 3440 | * Ok set num_bytes to orig_bytes since we aren't |
| 3441 | * overocmmitted, this way we only try and reclaim what |
| 3442 | * we need. |
| 3443 | */ |
| 3444 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3445 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3446 | } else { |
| 3447 | /* |
| 3448 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3449 | * amount plus the amount of bytes that we need for this |
| 3450 | * reservation. |
| 3451 | */ |
| 3452 | num_bytes = unused - space_info->total_bytes + |
| 3453 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3454 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3455 | |
| 3456 | /* |
| 3457 | * Couldn't make our reservation, save our place so while we're trying |
| 3458 | * to reclaim space we can actually use it instead of somebody else |
| 3459 | * stealing it from us. |
| 3460 | */ |
| 3461 | if (ret && !reserved) { |
| 3462 | space_info->bytes_reserved += orig_bytes; |
| 3463 | reserved = true; |
| 3464 | } |
| 3465 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3466 | spin_unlock(&space_info->lock); |
| 3467 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3468 | if (!ret) |
| 3469 | return 0; |
| 3470 | |
| 3471 | if (!flush) |
| 3472 | goto out; |
| 3473 | |
| 3474 | /* |
| 3475 | * We do synchronous shrinking since we don't actually unreserve |
| 3476 | * metadata until after the IO is completed. |
| 3477 | */ |
| 3478 | ret = shrink_delalloc(trans, root, num_bytes, 1); |
| 3479 | if (ret > 0) |
| 3480 | return 0; |
| 3481 | else if (ret < 0) |
| 3482 | goto out; |
| 3483 | |
| 3484 | /* |
| 3485 | * So if we were overcommitted it's possible that somebody else flushed |
| 3486 | * out enough space and we simply didn't have enough space to reclaim, |
| 3487 | * so go back around and try again. |
| 3488 | */ |
| 3489 | if (retries < 2) { |
| 3490 | retries++; |
| 3491 | goto again; |
| 3492 | } |
| 3493 | |
| 3494 | spin_lock(&space_info->lock); |
| 3495 | /* |
| 3496 | * Not enough space to be reclaimed, don't bother committing the |
| 3497 | * transaction. |
| 3498 | */ |
| 3499 | if (space_info->bytes_pinned < orig_bytes) |
| 3500 | ret = -ENOSPC; |
| 3501 | spin_unlock(&space_info->lock); |
| 3502 | if (ret) |
| 3503 | goto out; |
| 3504 | |
| 3505 | ret = -EAGAIN; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3506 | if (trans || committed) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3507 | goto out; |
| 3508 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3509 | ret = -ENOSPC; |
| 3510 | trans = btrfs_join_transaction(root, 1); |
| 3511 | if (IS_ERR(trans)) |
| 3512 | goto out; |
| 3513 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3514 | if (!ret) { |
| 3515 | trans = NULL; |
| 3516 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3517 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3518 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3519 | |
| 3520 | out: |
| 3521 | if (reserved) { |
| 3522 | spin_lock(&space_info->lock); |
| 3523 | space_info->bytes_reserved -= orig_bytes; |
| 3524 | spin_unlock(&space_info->lock); |
| 3525 | } |
| 3526 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3527 | return ret; |
| 3528 | } |
| 3529 | |
| 3530 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3531 | struct btrfs_root *root) |
| 3532 | { |
| 3533 | struct btrfs_block_rsv *block_rsv; |
| 3534 | if (root->ref_cows) |
| 3535 | block_rsv = trans->block_rsv; |
| 3536 | else |
| 3537 | block_rsv = root->block_rsv; |
| 3538 | |
| 3539 | if (!block_rsv) |
| 3540 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3541 | |
| 3542 | return block_rsv; |
| 3543 | } |
| 3544 | |
| 3545 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3546 | u64 num_bytes) |
| 3547 | { |
| 3548 | int ret = -ENOSPC; |
| 3549 | spin_lock(&block_rsv->lock); |
| 3550 | if (block_rsv->reserved >= num_bytes) { |
| 3551 | block_rsv->reserved -= num_bytes; |
| 3552 | if (block_rsv->reserved < block_rsv->size) |
| 3553 | block_rsv->full = 0; |
| 3554 | ret = 0; |
| 3555 | } |
| 3556 | spin_unlock(&block_rsv->lock); |
| 3557 | return ret; |
| 3558 | } |
| 3559 | |
| 3560 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3561 | u64 num_bytes, int update_size) |
| 3562 | { |
| 3563 | spin_lock(&block_rsv->lock); |
| 3564 | block_rsv->reserved += num_bytes; |
| 3565 | if (update_size) |
| 3566 | block_rsv->size += num_bytes; |
| 3567 | else if (block_rsv->reserved >= block_rsv->size) |
| 3568 | block_rsv->full = 1; |
| 3569 | spin_unlock(&block_rsv->lock); |
| 3570 | } |
| 3571 | |
| 3572 | void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3573 | struct btrfs_block_rsv *dest, u64 num_bytes) |
| 3574 | { |
| 3575 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3576 | |
| 3577 | spin_lock(&block_rsv->lock); |
| 3578 | if (num_bytes == (u64)-1) |
| 3579 | num_bytes = block_rsv->size; |
| 3580 | block_rsv->size -= num_bytes; |
| 3581 | if (block_rsv->reserved >= block_rsv->size) { |
| 3582 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3583 | block_rsv->reserved = block_rsv->size; |
| 3584 | block_rsv->full = 1; |
| 3585 | } else { |
| 3586 | num_bytes = 0; |
| 3587 | } |
| 3588 | spin_unlock(&block_rsv->lock); |
| 3589 | |
| 3590 | if (num_bytes > 0) { |
| 3591 | if (dest) { |
| 3592 | block_rsv_add_bytes(dest, num_bytes, 0); |
| 3593 | } else { |
| 3594 | spin_lock(&space_info->lock); |
| 3595 | space_info->bytes_reserved -= num_bytes; |
| 3596 | spin_unlock(&space_info->lock); |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3602 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3603 | { |
| 3604 | int ret; |
| 3605 | |
| 3606 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3607 | if (ret) |
| 3608 | return ret; |
| 3609 | |
| 3610 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3611 | return 0; |
| 3612 | } |
| 3613 | |
| 3614 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3615 | { |
| 3616 | memset(rsv, 0, sizeof(*rsv)); |
| 3617 | spin_lock_init(&rsv->lock); |
| 3618 | atomic_set(&rsv->usage, 1); |
| 3619 | rsv->priority = 6; |
| 3620 | INIT_LIST_HEAD(&rsv->list); |
| 3621 | } |
| 3622 | |
| 3623 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3624 | { |
| 3625 | struct btrfs_block_rsv *block_rsv; |
| 3626 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3627 | |
| 3628 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3629 | if (!block_rsv) |
| 3630 | return NULL; |
| 3631 | |
| 3632 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3633 | block_rsv->space_info = __find_space_info(fs_info, |
| 3634 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3635 | return block_rsv; |
| 3636 | } |
| 3637 | |
| 3638 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3639 | struct btrfs_block_rsv *rsv) |
| 3640 | { |
| 3641 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3642 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3643 | if (!rsv->durable) |
| 3644 | kfree(rsv); |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | /* |
| 3649 | * make the block_rsv struct be able to capture freed space. |
| 3650 | * the captured space will re-add to the the block_rsv struct |
| 3651 | * after transaction commit |
| 3652 | */ |
| 3653 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3654 | struct btrfs_block_rsv *block_rsv) |
| 3655 | { |
| 3656 | block_rsv->durable = 1; |
| 3657 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3658 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3659 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3660 | } |
| 3661 | |
| 3662 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3663 | struct btrfs_root *root, |
| 3664 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3665 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3666 | { |
| 3667 | int ret; |
| 3668 | |
| 3669 | if (num_bytes == 0) |
| 3670 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3671 | |
| 3672 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3673 | if (!ret) { |
| 3674 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3675 | return 0; |
| 3676 | } |
| 3677 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3678 | return ret; |
| 3679 | } |
| 3680 | |
| 3681 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3682 | struct btrfs_root *root, |
| 3683 | struct btrfs_block_rsv *block_rsv, |
| 3684 | u64 min_reserved, int min_factor) |
| 3685 | { |
| 3686 | u64 num_bytes = 0; |
| 3687 | int commit_trans = 0; |
| 3688 | int ret = -ENOSPC; |
| 3689 | |
| 3690 | if (!block_rsv) |
| 3691 | return 0; |
| 3692 | |
| 3693 | spin_lock(&block_rsv->lock); |
| 3694 | if (min_factor > 0) |
| 3695 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3696 | if (min_reserved > num_bytes) |
| 3697 | num_bytes = min_reserved; |
| 3698 | |
| 3699 | if (block_rsv->reserved >= num_bytes) { |
| 3700 | ret = 0; |
| 3701 | } else { |
| 3702 | num_bytes -= block_rsv->reserved; |
| 3703 | if (block_rsv->durable && |
| 3704 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3705 | commit_trans = 1; |
| 3706 | } |
| 3707 | spin_unlock(&block_rsv->lock); |
| 3708 | if (!ret) |
| 3709 | return 0; |
| 3710 | |
| 3711 | if (block_rsv->refill_used) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3712 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 3713 | num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3714 | if (!ret) { |
| 3715 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3716 | return 0; |
| 3717 | } |
| 3718 | } |
| 3719 | |
| 3720 | if (commit_trans) { |
| 3721 | if (trans) |
| 3722 | return -EAGAIN; |
| 3723 | |
| 3724 | trans = btrfs_join_transaction(root, 1); |
| 3725 | BUG_ON(IS_ERR(trans)); |
| 3726 | ret = btrfs_commit_transaction(trans, root); |
| 3727 | return 0; |
| 3728 | } |
| 3729 | |
| 3730 | WARN_ON(1); |
| 3731 | printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n", |
| 3732 | block_rsv->size, block_rsv->reserved, |
| 3733 | block_rsv->freed[0], block_rsv->freed[1]); |
| 3734 | |
| 3735 | return -ENOSPC; |
| 3736 | } |
| 3737 | |
| 3738 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3739 | struct btrfs_block_rsv *dst_rsv, |
| 3740 | u64 num_bytes) |
| 3741 | { |
| 3742 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3743 | } |
| 3744 | |
| 3745 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3746 | struct btrfs_block_rsv *block_rsv, |
| 3747 | u64 num_bytes) |
| 3748 | { |
| 3749 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3750 | if (global_rsv->full || global_rsv == block_rsv || |
| 3751 | block_rsv->space_info != global_rsv->space_info) |
| 3752 | global_rsv = NULL; |
| 3753 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3754 | } |
| 3755 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3756 | /* |
| 3757 | * helper to calculate size of global block reservation. |
| 3758 | * the desired value is sum of space used by extent tree, |
| 3759 | * checksum tree and root tree |
| 3760 | */ |
| 3761 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3762 | { |
| 3763 | struct btrfs_space_info *sinfo; |
| 3764 | u64 num_bytes; |
| 3765 | u64 meta_used; |
| 3766 | u64 data_used; |
| 3767 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
| 3768 | #if 0 |
| 3769 | /* |
| 3770 | * per tree used space accounting can be inaccuracy, so we |
| 3771 | * can't rely on it. |
| 3772 | */ |
| 3773 | spin_lock(&fs_info->extent_root->accounting_lock); |
| 3774 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item); |
| 3775 | spin_unlock(&fs_info->extent_root->accounting_lock); |
| 3776 | |
| 3777 | spin_lock(&fs_info->csum_root->accounting_lock); |
| 3778 | num_bytes += btrfs_root_used(&fs_info->csum_root->root_item); |
| 3779 | spin_unlock(&fs_info->csum_root->accounting_lock); |
| 3780 | |
| 3781 | spin_lock(&fs_info->tree_root->accounting_lock); |
| 3782 | num_bytes += btrfs_root_used(&fs_info->tree_root->root_item); |
| 3783 | spin_unlock(&fs_info->tree_root->accounting_lock); |
| 3784 | #endif |
| 3785 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3786 | spin_lock(&sinfo->lock); |
| 3787 | data_used = sinfo->bytes_used; |
| 3788 | spin_unlock(&sinfo->lock); |
| 3789 | |
| 3790 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3791 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3792 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3793 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3794 | meta_used = sinfo->bytes_used; |
| 3795 | spin_unlock(&sinfo->lock); |
| 3796 | |
| 3797 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3798 | csum_size * 2; |
| 3799 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3800 | |
| 3801 | if (num_bytes * 3 > meta_used) |
| 3802 | num_bytes = div64_u64(meta_used, 3); |
| 3803 | |
| 3804 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3805 | } |
| 3806 | |
| 3807 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3808 | { |
| 3809 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3810 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3811 | u64 num_bytes; |
| 3812 | |
| 3813 | num_bytes = calc_global_metadata_size(fs_info); |
| 3814 | |
| 3815 | spin_lock(&block_rsv->lock); |
| 3816 | spin_lock(&sinfo->lock); |
| 3817 | |
| 3818 | block_rsv->size = num_bytes; |
| 3819 | |
| 3820 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3821 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3822 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3823 | |
| 3824 | if (sinfo->total_bytes > num_bytes) { |
| 3825 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3826 | block_rsv->reserved += num_bytes; |
| 3827 | sinfo->bytes_reserved += num_bytes; |
| 3828 | } |
| 3829 | |
| 3830 | if (block_rsv->reserved >= block_rsv->size) { |
| 3831 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3832 | sinfo->bytes_reserved -= num_bytes; |
| 3833 | block_rsv->reserved = block_rsv->size; |
| 3834 | block_rsv->full = 1; |
| 3835 | } |
| 3836 | #if 0 |
| 3837 | printk(KERN_INFO"global block rsv size %llu reserved %llu\n", |
| 3838 | block_rsv->size, block_rsv->reserved); |
| 3839 | #endif |
| 3840 | spin_unlock(&sinfo->lock); |
| 3841 | spin_unlock(&block_rsv->lock); |
| 3842 | } |
| 3843 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3844 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3845 | { |
| 3846 | struct btrfs_space_info *space_info; |
| 3847 | |
| 3848 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3849 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3850 | fs_info->chunk_block_rsv.priority = 10; |
| 3851 | |
| 3852 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3853 | fs_info->global_block_rsv.space_info = space_info; |
| 3854 | fs_info->global_block_rsv.priority = 10; |
| 3855 | fs_info->global_block_rsv.refill_used = 1; |
| 3856 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3857 | fs_info->trans_block_rsv.space_info = space_info; |
| 3858 | fs_info->empty_block_rsv.space_info = space_info; |
| 3859 | fs_info->empty_block_rsv.priority = 10; |
| 3860 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3861 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3862 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3863 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3864 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3865 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3866 | |
| 3867 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3868 | |
| 3869 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3870 | |
| 3871 | update_global_block_rsv(fs_info); |
| 3872 | } |
| 3873 | |
| 3874 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3875 | { |
| 3876 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3877 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3878 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3879 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3880 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3881 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3882 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3883 | } |
| 3884 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3885 | static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items) |
| 3886 | { |
| 3887 | return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) * |
| 3888 | 3 * num_items; |
| 3889 | } |
| 3890 | |
| 3891 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3892 | struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3893 | int num_items) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3894 | { |
| 3895 | u64 num_bytes; |
| 3896 | int ret; |
| 3897 | |
| 3898 | if (num_items == 0 || root->fs_info->chunk_root == root) |
| 3899 | return 0; |
| 3900 | |
| 3901 | num_bytes = calc_trans_metadata_size(root, num_items); |
| 3902 | ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3903 | num_bytes); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3904 | if (!ret) { |
| 3905 | trans->bytes_reserved += num_bytes; |
| 3906 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 3907 | } |
| 3908 | return ret; |
| 3909 | } |
| 3910 | |
| 3911 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3912 | struct btrfs_root *root) |
| 3913 | { |
| 3914 | if (!trans->bytes_reserved) |
| 3915 | return; |
| 3916 | |
| 3917 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 3918 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3919 | trans->bytes_reserved); |
| 3920 | trans->bytes_reserved = 0; |
| 3921 | } |
| 3922 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3923 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3924 | struct inode *inode) |
| 3925 | { |
| 3926 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3927 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3928 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3929 | |
| 3930 | /* |
| 3931 | * one for deleting orphan item, one for updating inode and |
| 3932 | * two for calling btrfs_truncate_inode_items. |
| 3933 | * |
| 3934 | * btrfs_truncate_inode_items is a delete operation, it frees |
| 3935 | * more space than it uses in most cases. So two units of |
| 3936 | * metadata space should be enough for calling it many times. |
| 3937 | * If all of the metadata space is used, we can commit |
| 3938 | * transaction and use space it freed. |
| 3939 | */ |
| 3940 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3941 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3942 | } |
| 3943 | |
| 3944 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3945 | { |
| 3946 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3947 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3948 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3949 | } |
| 3950 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3951 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3952 | struct btrfs_pending_snapshot *pending) |
| 3953 | { |
| 3954 | struct btrfs_root *root = pending->root; |
| 3955 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3956 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3957 | /* |
| 3958 | * two for root back/forward refs, two for directory entries |
| 3959 | * and one for root of the snapshot. |
| 3960 | */ |
| 3961 | u64 num_bytes = calc_trans_metadata_size(root, 5); |
| 3962 | dst_rsv->space_info = src_rsv->space_info; |
| 3963 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3964 | } |
| 3965 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3966 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 3967 | { |
| 3968 | return num_bytes >>= 3; |
| 3969 | } |
| 3970 | |
| 3971 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 3972 | { |
| 3973 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3974 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
| 3975 | u64 to_reserve; |
| 3976 | int nr_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3977 | int ret; |
| 3978 | |
| 3979 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 3980 | schedule_timeout(1); |
| 3981 | |
| 3982 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3983 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3984 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 3985 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; |
| 3986 | if (nr_extents > BTRFS_I(inode)->reserved_extents) { |
| 3987 | nr_extents -= BTRFS_I(inode)->reserved_extents; |
| 3988 | to_reserve = calc_trans_metadata_size(root, nr_extents); |
| 3989 | } else { |
| 3990 | nr_extents = 0; |
| 3991 | to_reserve = 0; |
| 3992 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3993 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3994 | |
| 3995 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3996 | ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); |
| 3997 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3998 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3999 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4000 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4001 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 4002 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
| 4003 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 4004 | |
| 4005 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4006 | |
| 4007 | if (block_rsv->size > 512 * 1024 * 1024) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4008 | shrink_delalloc(NULL, root, to_reserve, 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4009 | |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
| 4013 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4014 | { |
| 4015 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4016 | u64 to_free; |
| 4017 | int nr_extents; |
| 4018 | |
| 4019 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 4020 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 4021 | |
| 4022 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 4023 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); |
| 4024 | if (nr_extents < BTRFS_I(inode)->reserved_extents) { |
| 4025 | nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents; |
| 4026 | BTRFS_I(inode)->reserved_extents -= nr_extents; |
| 4027 | } else { |
| 4028 | nr_extents = 0; |
| 4029 | } |
| 4030 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 4031 | |
| 4032 | to_free = calc_csum_metadata_size(inode, num_bytes); |
| 4033 | if (nr_extents > 0) |
| 4034 | to_free += calc_trans_metadata_size(root, nr_extents); |
| 4035 | |
| 4036 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4037 | to_free); |
| 4038 | } |
| 4039 | |
| 4040 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4041 | { |
| 4042 | int ret; |
| 4043 | |
| 4044 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4045 | if (ret) |
| 4046 | return ret; |
| 4047 | |
| 4048 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4049 | if (ret) { |
| 4050 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4051 | return ret; |
| 4052 | } |
| 4053 | |
| 4054 | return 0; |
| 4055 | } |
| 4056 | |
| 4057 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4058 | { |
| 4059 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4060 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4061 | } |
| 4062 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4063 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4064 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4065 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4066 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4067 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4068 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4069 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4070 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4071 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4072 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4073 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4074 | /* block accounting for super block */ |
| 4075 | spin_lock(&info->delalloc_lock); |
| 4076 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 4077 | if (alloc) |
| 4078 | old_val += num_bytes; |
| 4079 | else |
| 4080 | old_val -= num_bytes; |
| 4081 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4082 | spin_unlock(&info->delalloc_lock); |
| 4083 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4084 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4085 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4086 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4087 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4088 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4089 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4090 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4091 | factor = 2; |
| 4092 | else |
| 4093 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4094 | /* |
| 4095 | * If this block group has free space cache written out, we |
| 4096 | * need to make sure to load it if we are removing space. This |
| 4097 | * is because we need the unpinning stage to actually add the |
| 4098 | * space back to the block group, otherwise we will leak space. |
| 4099 | */ |
| 4100 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4101 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4102 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4103 | byte_in_group = bytenr - cache->key.objectid; |
| 4104 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4105 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4106 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4107 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4108 | |
| 4109 | if (btrfs_super_cache_generation(&info->super_copy) != 0 && |
| 4110 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4111 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4112 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4113 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4114 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4115 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4116 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4117 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4118 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4119 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4120 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4121 | cache->space_info->bytes_used += num_bytes; |
| 4122 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4123 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4124 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4125 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4126 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4127 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4128 | cache->pinned += num_bytes; |
| 4129 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4130 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4131 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4132 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4133 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4134 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4135 | set_extent_dirty(info->pinned_extents, |
| 4136 | bytenr, bytenr + num_bytes - 1, |
| 4137 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4138 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4139 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4140 | total -= num_bytes; |
| 4141 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4142 | } |
| 4143 | return 0; |
| 4144 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4145 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4146 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4147 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4148 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4149 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4150 | |
| 4151 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4152 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4153 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4154 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4155 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4156 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4157 | |
| 4158 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4159 | } |
| 4160 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4161 | static int pin_down_extent(struct btrfs_root *root, |
| 4162 | struct btrfs_block_group_cache *cache, |
| 4163 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4164 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4165 | spin_lock(&cache->space_info->lock); |
| 4166 | spin_lock(&cache->lock); |
| 4167 | cache->pinned += num_bytes; |
| 4168 | cache->space_info->bytes_pinned += num_bytes; |
| 4169 | if (reserved) { |
| 4170 | cache->reserved -= num_bytes; |
| 4171 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4172 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4173 | spin_unlock(&cache->lock); |
| 4174 | spin_unlock(&cache->space_info->lock); |
| 4175 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4176 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4177 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4178 | return 0; |
| 4179 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4180 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4181 | /* |
| 4182 | * this function must be called within transaction |
| 4183 | */ |
| 4184 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4185 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4186 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4187 | struct btrfs_block_group_cache *cache; |
| 4188 | |
| 4189 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4190 | BUG_ON(!cache); |
| 4191 | |
| 4192 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4193 | |
| 4194 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4197 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4198 | /* |
| 4199 | * update size of reserved extents. this function may return -EAGAIN |
| 4200 | * if 'reserve' is true or 'sinfo' is false. |
| 4201 | */ |
| 4202 | static int update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4203 | u64 num_bytes, int reserve, int sinfo) |
| 4204 | { |
| 4205 | int ret = 0; |
| 4206 | if (sinfo) { |
| 4207 | struct btrfs_space_info *space_info = cache->space_info; |
| 4208 | spin_lock(&space_info->lock); |
| 4209 | spin_lock(&cache->lock); |
| 4210 | if (reserve) { |
| 4211 | if (cache->ro) { |
| 4212 | ret = -EAGAIN; |
| 4213 | } else { |
| 4214 | cache->reserved += num_bytes; |
| 4215 | space_info->bytes_reserved += num_bytes; |
| 4216 | } |
| 4217 | } else { |
| 4218 | if (cache->ro) |
| 4219 | space_info->bytes_readonly += num_bytes; |
| 4220 | cache->reserved -= num_bytes; |
| 4221 | space_info->bytes_reserved -= num_bytes; |
| 4222 | } |
| 4223 | spin_unlock(&cache->lock); |
| 4224 | spin_unlock(&space_info->lock); |
| 4225 | } else { |
| 4226 | spin_lock(&cache->lock); |
| 4227 | if (cache->ro) { |
| 4228 | ret = -EAGAIN; |
| 4229 | } else { |
| 4230 | if (reserve) |
| 4231 | cache->reserved += num_bytes; |
| 4232 | else |
| 4233 | cache->reserved -= num_bytes; |
| 4234 | } |
| 4235 | spin_unlock(&cache->lock); |
| 4236 | } |
| 4237 | return ret; |
| 4238 | } |
| 4239 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4240 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4241 | struct btrfs_root *root) |
| 4242 | { |
| 4243 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4244 | struct btrfs_caching_control *next; |
| 4245 | struct btrfs_caching_control *caching_ctl; |
| 4246 | struct btrfs_block_group_cache *cache; |
| 4247 | |
| 4248 | down_write(&fs_info->extent_commit_sem); |
| 4249 | |
| 4250 | list_for_each_entry_safe(caching_ctl, next, |
| 4251 | &fs_info->caching_block_groups, list) { |
| 4252 | cache = caching_ctl->block_group; |
| 4253 | if (block_group_cache_done(cache)) { |
| 4254 | cache->last_byte_to_unpin = (u64)-1; |
| 4255 | list_del_init(&caching_ctl->list); |
| 4256 | put_caching_control(caching_ctl); |
| 4257 | } else { |
| 4258 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4263 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4264 | else |
| 4265 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4266 | |
| 4267 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4268 | |
| 4269 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4270 | return 0; |
| 4271 | } |
| 4272 | |
| 4273 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4274 | { |
| 4275 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4276 | struct btrfs_block_group_cache *cache = NULL; |
| 4277 | u64 len; |
| 4278 | |
| 4279 | while (start <= end) { |
| 4280 | if (!cache || |
| 4281 | start >= cache->key.objectid + cache->key.offset) { |
| 4282 | if (cache) |
| 4283 | btrfs_put_block_group(cache); |
| 4284 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4285 | BUG_ON(!cache); |
| 4286 | } |
| 4287 | |
| 4288 | len = cache->key.objectid + cache->key.offset - start; |
| 4289 | len = min(len, end + 1 - start); |
| 4290 | |
| 4291 | if (start < cache->last_byte_to_unpin) { |
| 4292 | len = min(len, cache->last_byte_to_unpin - start); |
| 4293 | btrfs_add_free_space(cache, start, len); |
| 4294 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4295 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4296 | start += len; |
| 4297 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4298 | spin_lock(&cache->space_info->lock); |
| 4299 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4300 | cache->pinned -= len; |
| 4301 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4302 | if (cache->ro) { |
| 4303 | cache->space_info->bytes_readonly += len; |
| 4304 | } else if (cache->reserved_pinned > 0) { |
| 4305 | len = min(len, cache->reserved_pinned); |
| 4306 | cache->reserved_pinned -= len; |
| 4307 | cache->space_info->bytes_reserved += len; |
| 4308 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4309 | spin_unlock(&cache->lock); |
| 4310 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4314 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
| 4317 | |
| 4318 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4319 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4320 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4321 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4322 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4323 | struct btrfs_block_rsv *block_rsv; |
| 4324 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc167 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4325 | u64 start; |
| 4326 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4327 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4328 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4329 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4330 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4331 | unpin = &fs_info->freed_extents[1]; |
| 4332 | else |
| 4333 | unpin = &fs_info->freed_extents[0]; |
| 4334 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4335 | while (1) { |
Chris Mason | 1a5bc167 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4336 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4337 | EXTENT_DIRTY); |
| 4338 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4339 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4340 | |
| 4341 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 4342 | |
Chris Mason | 1a5bc167 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4343 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4344 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4345 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4346 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4347 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4348 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4349 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4350 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4351 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4352 | idx = trans->transid & 0x1; |
| 4353 | if (block_rsv->freed[idx] > 0) { |
| 4354 | block_rsv_add_bytes(block_rsv, |
| 4355 | block_rsv->freed[idx], 0); |
| 4356 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4357 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4358 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4359 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4360 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4361 | if (block_rsv->freed[0] == 0 && |
| 4362 | block_rsv->freed[1] == 0) { |
| 4363 | list_del_init(&block_rsv->list); |
| 4364 | kfree(block_rsv); |
| 4365 | } |
| 4366 | } else { |
| 4367 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4368 | } |
| 4369 | } |
| 4370 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4371 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4372 | return 0; |
| 4373 | } |
| 4374 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4375 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4376 | struct btrfs_root *root, |
| 4377 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4378 | u64 root_objectid, u64 owner_objectid, |
| 4379 | u64 owner_offset, int refs_to_drop, |
| 4380 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4381 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4382 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4383 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4384 | struct btrfs_fs_info *info = root->fs_info; |
| 4385 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4386 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4387 | struct btrfs_extent_item *ei; |
| 4388 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4389 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4390 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4391 | int extent_slot = 0; |
| 4392 | int found_extent = 0; |
| 4393 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4394 | u32 item_size; |
| 4395 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4396 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4397 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4398 | if (!path) |
| 4399 | return -ENOMEM; |
| 4400 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4401 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4402 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4403 | |
| 4404 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4405 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4406 | |
| 4407 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4408 | bytenr, num_bytes, parent, |
| 4409 | root_objectid, owner_objectid, |
| 4410 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4411 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4412 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4413 | while (extent_slot >= 0) { |
| 4414 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4415 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4416 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4417 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4418 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4419 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4420 | found_extent = 1; |
| 4421 | break; |
| 4422 | } |
| 4423 | if (path->slots[0] - extent_slot > 5) |
| 4424 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4425 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4426 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4427 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4428 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4429 | if (found_extent && item_size < sizeof(*ei)) |
| 4430 | found_extent = 0; |
| 4431 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4432 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4433 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4434 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4435 | NULL, refs_to_drop, |
| 4436 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4437 | BUG_ON(ret); |
| 4438 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4439 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4440 | |
| 4441 | key.objectid = bytenr; |
| 4442 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4443 | key.offset = num_bytes; |
| 4444 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4445 | ret = btrfs_search_slot(trans, extent_root, |
| 4446 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4447 | if (ret) { |
| 4448 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4449 | ", was looking for %llu\n", ret, |
| 4450 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4451 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4452 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4453 | BUG_ON(ret); |
| 4454 | extent_slot = path->slots[0]; |
| 4455 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4456 | } else { |
| 4457 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4458 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4459 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4460 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4461 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4462 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4463 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4464 | (unsigned long long)owner_objectid, |
| 4465 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4466 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4467 | |
| 4468 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4469 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4470 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4471 | if (item_size < sizeof(*ei)) { |
| 4472 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4473 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4474 | owner_objectid, 0); |
| 4475 | BUG_ON(ret < 0); |
| 4476 | |
| 4477 | btrfs_release_path(extent_root, path); |
| 4478 | path->leave_spinning = 1; |
| 4479 | |
| 4480 | key.objectid = bytenr; |
| 4481 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4482 | key.offset = num_bytes; |
| 4483 | |
| 4484 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4485 | -1, 1); |
| 4486 | if (ret) { |
| 4487 | printk(KERN_ERR "umm, got %d back from search" |
| 4488 | ", was looking for %llu\n", ret, |
| 4489 | (unsigned long long)bytenr); |
| 4490 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4491 | } |
| 4492 | BUG_ON(ret); |
| 4493 | extent_slot = path->slots[0]; |
| 4494 | leaf = path->nodes[0]; |
| 4495 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4496 | } |
| 4497 | #endif |
| 4498 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4499 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4500 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4501 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4502 | struct btrfs_tree_block_info *bi; |
| 4503 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4504 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4505 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4506 | } |
| 4507 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4508 | refs = btrfs_extent_refs(leaf, ei); |
| 4509 | BUG_ON(refs < refs_to_drop); |
| 4510 | refs -= refs_to_drop; |
| 4511 | |
| 4512 | if (refs > 0) { |
| 4513 | if (extent_op) |
| 4514 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4515 | /* |
| 4516 | * In the case of inline back ref, reference count will |
| 4517 | * be updated by remove_extent_backref |
| 4518 | */ |
| 4519 | if (iref) { |
| 4520 | BUG_ON(!found_extent); |
| 4521 | } else { |
| 4522 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4523 | btrfs_mark_buffer_dirty(leaf); |
| 4524 | } |
| 4525 | if (found_extent) { |
| 4526 | ret = remove_extent_backref(trans, extent_root, path, |
| 4527 | iref, refs_to_drop, |
| 4528 | is_data); |
| 4529 | BUG_ON(ret); |
| 4530 | } |
| 4531 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4532 | if (found_extent) { |
| 4533 | BUG_ON(is_data && refs_to_drop != |
| 4534 | extent_data_ref_count(root, path, iref)); |
| 4535 | if (iref) { |
| 4536 | BUG_ON(path->slots[0] != extent_slot); |
| 4537 | } else { |
| 4538 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4539 | path->slots[0] = extent_slot; |
| 4540 | num_to_del = 2; |
| 4541 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4542 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4543 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4544 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4545 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4546 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4547 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4549 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4550 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4551 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4552 | } else { |
| 4553 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4554 | bytenr >> PAGE_CACHE_SHIFT, |
| 4555 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4556 | } |
| 4557 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4558 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4559 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4560 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4561 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4562 | return ret; |
| 4563 | } |
| 4564 | |
| 4565 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4566 | * when we free an block, it is possible (and likely) that we free the last |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4567 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4568 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4569 | * removes it from the tree. |
| 4570 | */ |
| 4571 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4572 | struct btrfs_root *root, u64 bytenr) |
| 4573 | { |
| 4574 | struct btrfs_delayed_ref_head *head; |
| 4575 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4576 | struct btrfs_delayed_ref_node *ref; |
| 4577 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4578 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4579 | |
| 4580 | delayed_refs = &trans->transaction->delayed_refs; |
| 4581 | spin_lock(&delayed_refs->lock); |
| 4582 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4583 | if (!head) |
| 4584 | goto out; |
| 4585 | |
| 4586 | node = rb_prev(&head->node.rb_node); |
| 4587 | if (!node) |
| 4588 | goto out; |
| 4589 | |
| 4590 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4591 | |
| 4592 | /* there are still entries for this ref, we can't drop it */ |
| 4593 | if (ref->bytenr == bytenr) |
| 4594 | goto out; |
| 4595 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4596 | if (head->extent_op) { |
| 4597 | if (!head->must_insert_reserved) |
| 4598 | goto out; |
| 4599 | kfree(head->extent_op); |
| 4600 | head->extent_op = NULL; |
| 4601 | } |
| 4602 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4603 | /* |
| 4604 | * waiting for the lock here would deadlock. If someone else has it |
| 4605 | * locked they are already in the process of dropping it anyway |
| 4606 | */ |
| 4607 | if (!mutex_trylock(&head->mutex)) |
| 4608 | goto out; |
| 4609 | |
| 4610 | /* |
| 4611 | * at this point we have a head with no other entries. Go |
| 4612 | * ahead and process it. |
| 4613 | */ |
| 4614 | head->node.in_tree = 0; |
| 4615 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4616 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4617 | delayed_refs->num_entries--; |
| 4618 | |
| 4619 | /* |
| 4620 | * we don't take a ref on the node because we're removing it from the |
| 4621 | * tree, so we just steal the ref the tree was holding. |
| 4622 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4623 | delayed_refs->num_heads--; |
| 4624 | if (list_empty(&head->cluster)) |
| 4625 | delayed_refs->num_heads_ready--; |
| 4626 | |
| 4627 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4628 | spin_unlock(&delayed_refs->lock); |
| 4629 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4630 | BUG_ON(head->extent_op); |
| 4631 | if (head->must_insert_reserved) |
| 4632 | ret = 1; |
| 4633 | |
| 4634 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4635 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4636 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4637 | out: |
| 4638 | spin_unlock(&delayed_refs->lock); |
| 4639 | return 0; |
| 4640 | } |
| 4641 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4642 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4643 | struct btrfs_root *root, |
| 4644 | struct extent_buffer *buf, |
| 4645 | u64 parent, int last_ref) |
| 4646 | { |
| 4647 | struct btrfs_block_rsv *block_rsv; |
| 4648 | struct btrfs_block_group_cache *cache = NULL; |
| 4649 | int ret; |
| 4650 | |
| 4651 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4652 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4653 | parent, root->root_key.objectid, |
| 4654 | btrfs_header_level(buf), |
| 4655 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4656 | BUG_ON(ret); |
| 4657 | } |
| 4658 | |
| 4659 | if (!last_ref) |
| 4660 | return; |
| 4661 | |
| 4662 | block_rsv = get_block_rsv(trans, root); |
| 4663 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4664 | if (block_rsv->space_info != cache->space_info) |
| 4665 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4666 | |
| 4667 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4668 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4669 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4670 | if (!ret) |
| 4671 | goto pin; |
| 4672 | } |
| 4673 | |
| 4674 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4675 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4676 | goto pin; |
| 4677 | } |
| 4678 | |
| 4679 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4680 | |
| 4681 | btrfs_add_free_space(cache, buf->start, buf->len); |
| 4682 | ret = update_reserved_bytes(cache, buf->len, 0, 0); |
| 4683 | if (ret == -EAGAIN) { |
| 4684 | /* block group became read-only */ |
| 4685 | update_reserved_bytes(cache, buf->len, 0, 1); |
| 4686 | goto out; |
| 4687 | } |
| 4688 | |
| 4689 | ret = 1; |
| 4690 | spin_lock(&block_rsv->lock); |
| 4691 | if (block_rsv->reserved < block_rsv->size) { |
| 4692 | block_rsv->reserved += buf->len; |
| 4693 | ret = 0; |
| 4694 | } |
| 4695 | spin_unlock(&block_rsv->lock); |
| 4696 | |
| 4697 | if (ret) { |
| 4698 | spin_lock(&cache->space_info->lock); |
| 4699 | cache->space_info->bytes_reserved -= buf->len; |
| 4700 | spin_unlock(&cache->space_info->lock); |
| 4701 | } |
| 4702 | goto out; |
| 4703 | } |
| 4704 | pin: |
| 4705 | if (block_rsv->durable && !cache->ro) { |
| 4706 | ret = 0; |
| 4707 | spin_lock(&cache->lock); |
| 4708 | if (!cache->ro) { |
| 4709 | cache->reserved_pinned += buf->len; |
| 4710 | ret = 1; |
| 4711 | } |
| 4712 | spin_unlock(&cache->lock); |
| 4713 | |
| 4714 | if (ret) { |
| 4715 | spin_lock(&block_rsv->lock); |
| 4716 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4717 | spin_unlock(&block_rsv->lock); |
| 4718 | } |
| 4719 | } |
| 4720 | out: |
| 4721 | btrfs_put_block_group(cache); |
| 4722 | } |
| 4723 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4724 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4725 | struct btrfs_root *root, |
| 4726 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4727 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4728 | { |
| 4729 | int ret; |
| 4730 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4731 | /* |
| 4732 | * tree log blocks never actually go into the extent allocation |
| 4733 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4734 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4735 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4736 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4737 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4738 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4739 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4740 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4741 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4742 | parent, root_objectid, (int)owner, |
| 4743 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4744 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4745 | } else { |
| 4746 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4747 | parent, root_objectid, owner, |
| 4748 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4749 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4750 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4751 | return ret; |
| 4752 | } |
| 4753 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4754 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4755 | { |
| 4756 | u64 mask = ((u64)root->stripesize - 1); |
| 4757 | u64 ret = (val + mask) & ~mask; |
| 4758 | return ret; |
| 4759 | } |
| 4760 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4761 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4762 | * when we wait for progress in the block group caching, its because |
| 4763 | * our allocation attempt failed at least once. So, we must sleep |
| 4764 | * and let some progress happen before we try again. |
| 4765 | * |
| 4766 | * This function will sleep at least once waiting for new free space to |
| 4767 | * show up, and then it will check the block group free space numbers |
| 4768 | * for our min num_bytes. Another option is to have it go ahead |
| 4769 | * and look in the rbtree for a free extent of a given size, but this |
| 4770 | * is a good start. |
| 4771 | */ |
| 4772 | static noinline int |
| 4773 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4774 | u64 num_bytes) |
| 4775 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4776 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4777 | DEFINE_WAIT(wait); |
| 4778 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4779 | caching_ctl = get_caching_control(cache); |
| 4780 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4781 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4782 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4783 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4784 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4785 | |
| 4786 | put_caching_control(caching_ctl); |
| 4787 | return 0; |
| 4788 | } |
| 4789 | |
| 4790 | static noinline int |
| 4791 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4792 | { |
| 4793 | struct btrfs_caching_control *caching_ctl; |
| 4794 | DEFINE_WAIT(wait); |
| 4795 | |
| 4796 | caching_ctl = get_caching_control(cache); |
| 4797 | if (!caching_ctl) |
| 4798 | return 0; |
| 4799 | |
| 4800 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4801 | |
| 4802 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4803 | return 0; |
| 4804 | } |
| 4805 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4806 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4807 | { |
| 4808 | int index; |
| 4809 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4810 | index = 0; |
| 4811 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4812 | index = 1; |
| 4813 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4814 | index = 2; |
| 4815 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4816 | index = 3; |
| 4817 | else |
| 4818 | index = 4; |
| 4819 | return index; |
| 4820 | } |
| 4821 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4822 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4823 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4824 | LOOP_CACHING_NOWAIT = 1, |
| 4825 | LOOP_CACHING_WAIT = 2, |
| 4826 | LOOP_ALLOC_CHUNK = 3, |
| 4827 | LOOP_NO_EMPTY_SIZE = 4, |
| 4828 | }; |
| 4829 | |
| 4830 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4831 | * walks the btree of allocated extents and find a hole of a given size. |
| 4832 | * The key ins is changed to record the hole: |
| 4833 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4834 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4835 | * ins->offset == number of blocks |
| 4836 | * Any available blocks before search_start are skipped. |
| 4837 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4838 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4839 | struct btrfs_root *orig_root, |
| 4840 | u64 num_bytes, u64 empty_size, |
| 4841 | u64 search_start, u64 search_end, |
| 4842 | u64 hint_byte, struct btrfs_key *ins, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4843 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4844 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4845 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4846 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4847 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4848 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4849 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4850 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4851 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4852 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4853 | int last_ptr_loop = 0; |
| 4854 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4855 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4856 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4857 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4858 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4859 | bool use_cluster = true; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4860 | u64 ideal_cache_percent = 0; |
| 4861 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4862 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4863 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4864 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4865 | ins->objectid = 0; |
| 4866 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4867 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4868 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4869 | if (!space_info) { |
| 4870 | printk(KERN_ERR "No space info for %d\n", data); |
| 4871 | return -ENOSPC; |
| 4872 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4873 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4874 | /* |
| 4875 | * If the space info is for both data and metadata it means we have a |
| 4876 | * small filesystem and we can't use the clustering stuff. |
| 4877 | */ |
| 4878 | if (btrfs_mixed_space_info(space_info)) |
| 4879 | use_cluster = false; |
| 4880 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4881 | if (orig_root->ref_cows || empty_size) |
| 4882 | allowed_chunk_alloc = 1; |
| 4883 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4884 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4885 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4886 | if (!btrfs_test_opt(root, SSD)) |
| 4887 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4888 | } |
| 4889 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4890 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 4891 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4892 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4893 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4894 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4895 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4896 | spin_lock(&last_ptr->lock); |
| 4897 | if (last_ptr->block_group) |
| 4898 | hint_byte = last_ptr->window_start; |
| 4899 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4900 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4901 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4902 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4903 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4904 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4905 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4906 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4907 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4908 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4909 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4910 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4911 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4912 | /* |
| 4913 | * we don't want to use the block group if it doesn't match our |
| 4914 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4915 | * |
| 4916 | * However if we are re-searching with an ideal block group |
| 4917 | * 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] | 4918 | */ |
| 4919 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4920 | (block_group->cached != BTRFS_CACHE_NO || |
| 4921 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4922 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4923 | if (list_empty(&block_group->list) || |
| 4924 | block_group->ro) { |
| 4925 | /* |
| 4926 | * someone is removing this block group, |
| 4927 | * we can't jump into the have_block_group |
| 4928 | * target because our list pointers are not |
| 4929 | * valid |
| 4930 | */ |
| 4931 | btrfs_put_block_group(block_group); |
| 4932 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4933 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4934 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4935 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4936 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4937 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4938 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4939 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4940 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4941 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4942 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4943 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4944 | list) { |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4945 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4946 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4947 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4948 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4949 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4950 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4951 | /* |
| 4952 | * this can happen if we end up cycling through all the |
| 4953 | * raid types, but we want to make sure we only allocate |
| 4954 | * for the proper type. |
| 4955 | */ |
| 4956 | if (!block_group_bits(block_group, data)) { |
| 4957 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 4958 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4959 | BTRFS_BLOCK_GROUP_RAID10; |
| 4960 | |
| 4961 | /* |
| 4962 | * if they asked for extra copies and this block group |
| 4963 | * doesn't provide them, bail. This does allow us to |
| 4964 | * fill raid0 from raid1. |
| 4965 | */ |
| 4966 | if ((data & extra) && !(block_group->flags & extra)) |
| 4967 | goto loop; |
| 4968 | } |
| 4969 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4970 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4971 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4972 | u64 free_percent; |
| 4973 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4974 | ret = cache_block_group(block_group, trans, |
| 4975 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4976 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 4977 | goto have_block_group; |
| 4978 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4979 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4980 | free_percent *= 100; |
| 4981 | free_percent = div64_u64(free_percent, |
| 4982 | block_group->key.offset); |
| 4983 | free_percent = 100 - free_percent; |
| 4984 | if (free_percent > ideal_cache_percent && |
| 4985 | likely(!block_group->ro)) { |
| 4986 | ideal_cache_offset = block_group->key.objectid; |
| 4987 | ideal_cache_percent = free_percent; |
| 4988 | } |
| 4989 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4990 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4991 | * We only want to start kthread caching if we are at |
| 4992 | * the point where we will wait for caching to make |
| 4993 | * progress, or if our ideal search is over and we've |
| 4994 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4995 | */ |
| 4996 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4997 | (loop > LOOP_FIND_IDEAL && |
| 4998 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4999 | ret = cache_block_group(block_group, trans, |
| 5000 | orig_root, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5001 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5002 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5003 | found_uncached_bg = true; |
| 5004 | |
| 5005 | /* |
| 5006 | * If loop is set for cached only, try the next block |
| 5007 | * group. |
| 5008 | */ |
| 5009 | if (loop == LOOP_FIND_IDEAL) |
| 5010 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5011 | } |
| 5012 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5013 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5014 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5015 | found_uncached_bg = true; |
| 5016 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5017 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5018 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5019 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5020 | /* |
| 5021 | * Ok we want to try and use the cluster allocator, so lets look |
| 5022 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 5023 | * have tried the cluster allocator plenty of times at this |
| 5024 | * point and not have found anything, so we are likely way too |
| 5025 | * fragmented for the clustering stuff to find anything, so lets |
| 5026 | * just skip it and let the allocator find whatever block it can |
| 5027 | * find |
| 5028 | */ |
| 5029 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5030 | /* |
| 5031 | * the refill lock keeps out other |
| 5032 | * people trying to start a new cluster |
| 5033 | */ |
| 5034 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5035 | if (last_ptr->block_group && |
| 5036 | (last_ptr->block_group->ro || |
| 5037 | !block_group_bits(last_ptr->block_group, data))) { |
| 5038 | offset = 0; |
| 5039 | goto refill_cluster; |
| 5040 | } |
| 5041 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5042 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 5043 | num_bytes, search_start); |
| 5044 | if (offset) { |
| 5045 | /* we have a block, we're done */ |
| 5046 | spin_unlock(&last_ptr->refill_lock); |
| 5047 | goto checks; |
| 5048 | } |
| 5049 | |
| 5050 | spin_lock(&last_ptr->lock); |
| 5051 | /* |
| 5052 | * whoops, this cluster doesn't actually point to |
| 5053 | * this block group. Get a ref on the block |
| 5054 | * group is does point to and try again |
| 5055 | */ |
| 5056 | if (!last_ptr_loop && last_ptr->block_group && |
| 5057 | last_ptr->block_group != block_group) { |
| 5058 | |
| 5059 | btrfs_put_block_group(block_group); |
| 5060 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5061 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5062 | spin_unlock(&last_ptr->lock); |
| 5063 | spin_unlock(&last_ptr->refill_lock); |
| 5064 | |
| 5065 | last_ptr_loop = 1; |
| 5066 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5067 | /* |
| 5068 | * we know this block group is properly |
| 5069 | * in the list because |
| 5070 | * btrfs_remove_block_group, drops the |
| 5071 | * cluster before it removes the block |
| 5072 | * group from the list |
| 5073 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5074 | goto have_block_group; |
| 5075 | } |
| 5076 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5077 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5078 | /* |
| 5079 | * this cluster didn't work out, free it and |
| 5080 | * start over |
| 5081 | */ |
| 5082 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5083 | |
| 5084 | last_ptr_loop = 0; |
| 5085 | |
| 5086 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5087 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5088 | block_group, last_ptr, |
| 5089 | offset, num_bytes, |
| 5090 | empty_cluster + empty_size); |
| 5091 | if (ret == 0) { |
| 5092 | /* |
| 5093 | * now pull our allocation out of this |
| 5094 | * cluster |
| 5095 | */ |
| 5096 | offset = btrfs_alloc_from_cluster(block_group, |
| 5097 | last_ptr, num_bytes, |
| 5098 | search_start); |
| 5099 | if (offset) { |
| 5100 | /* we found one, proceed */ |
| 5101 | spin_unlock(&last_ptr->refill_lock); |
| 5102 | goto checks; |
| 5103 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5104 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5105 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5106 | spin_unlock(&last_ptr->refill_lock); |
| 5107 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5108 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5109 | wait_block_group_cache_progress(block_group, |
| 5110 | num_bytes + empty_cluster + empty_size); |
| 5111 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5112 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5113 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5114 | /* |
| 5115 | * at this point we either didn't find a cluster |
| 5116 | * or we weren't able to allocate a block from our |
| 5117 | * cluster. Free the cluster we've been trying |
| 5118 | * to use, and go to the next block group |
| 5119 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5120 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5121 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5122 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5123 | } |
| 5124 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5125 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5126 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5127 | /* |
| 5128 | * If we didn't find a chunk, and we haven't failed on this |
| 5129 | * block group before, and this block group is in the middle of |
| 5130 | * caching and we are ok with waiting, then go ahead and wait |
| 5131 | * for progress to be made, and set failed_alloc to true. |
| 5132 | * |
| 5133 | * If failed_alloc is true then we've already waited on this |
| 5134 | * block group once and should move on to the next block group. |
| 5135 | */ |
| 5136 | if (!offset && !failed_alloc && !cached && |
| 5137 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5138 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5139 | num_bytes + empty_size); |
| 5140 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5141 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5142 | } else if (!offset) { |
| 5143 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5144 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5145 | checks: |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5146 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5147 | /* move on to the next group */ |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5148 | if (search_start + num_bytes >= search_end) { |
| 5149 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5150 | goto loop; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5151 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5152 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5153 | /* move on to the next group */ |
| 5154 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5155 | block_group->key.objectid + block_group->key.offset) { |
| 5156 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5157 | goto loop; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5158 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5159 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5160 | ins->objectid = search_start; |
| 5161 | ins->offset = num_bytes; |
| 5162 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5163 | if (offset < search_start) |
| 5164 | btrfs_add_free_space(block_group, offset, |
| 5165 | search_start - offset); |
| 5166 | BUG_ON(offset > search_start); |
| 5167 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5168 | ret = update_reserved_bytes(block_group, num_bytes, 1, |
| 5169 | (data & BTRFS_BLOCK_GROUP_DATA)); |
| 5170 | if (ret == -EAGAIN) { |
| 5171 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 5172 | goto loop; |
| 5173 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5174 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5175 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5176 | ins->objectid = search_start; |
| 5177 | ins->offset = num_bytes; |
| 5178 | |
| 5179 | if (offset < search_start) |
| 5180 | btrfs_add_free_space(block_group, offset, |
| 5181 | search_start - offset); |
| 5182 | BUG_ON(offset > search_start); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5183 | break; |
| 5184 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5185 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5186 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5187 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5188 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5189 | } |
| 5190 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5191 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5192 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5193 | goto search; |
| 5194 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5195 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 5196 | * for them to make caching progress. Also |
| 5197 | * determine the best possible bg to cache |
| 5198 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5199 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5200 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5201 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5202 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5203 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5204 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5205 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 5206 | (found_uncached_bg || empty_size || empty_cluster || |
| 5207 | allowed_chunk_alloc)) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5208 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5209 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5210 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5211 | loop++; |
| 5212 | if (!ideal_cache_percent && |
| 5213 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5214 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5215 | |
| 5216 | /* |
| 5217 | * 1 of the following 2 things have happened so far |
| 5218 | * |
| 5219 | * 1) We found an ideal block group for caching that |
| 5220 | * is mostly full and will cache quickly, so we might |
| 5221 | * as well wait for it. |
| 5222 | * |
| 5223 | * 2) We searched for cached only and we didn't find |
| 5224 | * anything, and we didn't start any caching kthreads |
| 5225 | * either, so chances are we will loop through and |
| 5226 | * start a couple caching kthreads, and then come back |
| 5227 | * around and just wait for them. This will be slower |
| 5228 | * because we will have 2 caching kthreads reading at |
| 5229 | * the same time when we could have just started one |
| 5230 | * and waited for it to get far enough to give us an |
| 5231 | * allocation, so go ahead and go to the wait caching |
| 5232 | * loop. |
| 5233 | */ |
| 5234 | loop = LOOP_CACHING_WAIT; |
| 5235 | search_start = ideal_cache_offset; |
| 5236 | ideal_cache_percent = 0; |
| 5237 | goto ideal_cache; |
| 5238 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5239 | /* |
| 5240 | * Didn't find a uncached bg, wait on anything we find |
| 5241 | * next. |
| 5242 | */ |
| 5243 | loop = LOOP_CACHING_WAIT; |
| 5244 | goto search; |
| 5245 | } |
| 5246 | |
| 5247 | if (loop < LOOP_CACHING_WAIT) { |
| 5248 | loop++; |
| 5249 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5253 | empty_size = 0; |
| 5254 | empty_cluster = 0; |
| 5255 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5256 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5257 | if (allowed_chunk_alloc) { |
| 5258 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 5259 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5260 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5261 | done_chunk_alloc = 1; |
| 5262 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5263 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5264 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5265 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5266 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5267 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5268 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5269 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5270 | ret = -ENOSPC; |
| 5271 | } else if (!ins->objectid) { |
| 5272 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 5273 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5274 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5275 | /* we found what we needed */ |
| 5276 | if (ins->objectid) { |
| 5277 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5278 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5279 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5280 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5281 | ret = 0; |
| 5282 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5283 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5284 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5285 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5286 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5287 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5288 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5289 | { |
| 5290 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5291 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5292 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5293 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5294 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 5295 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5296 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5297 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5298 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5299 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5300 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5301 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5302 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5303 | (unsigned long long)info->bytes_pinned, |
| 5304 | (unsigned long long)info->bytes_reserved, |
| 5305 | (unsigned long long)info->bytes_may_use, |
| 5306 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5307 | spin_unlock(&info->lock); |
| 5308 | |
| 5309 | if (!dump_block_groups) |
| 5310 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5311 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5312 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5313 | again: |
| 5314 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5315 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5316 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5317 | "%llu pinned %llu reserved\n", |
| 5318 | (unsigned long long)cache->key.objectid, |
| 5319 | (unsigned long long)cache->key.offset, |
| 5320 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5321 | (unsigned long long)cache->pinned, |
| 5322 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5323 | btrfs_dump_free_space(cache, bytes); |
| 5324 | spin_unlock(&cache->lock); |
| 5325 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5326 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5327 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5328 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5329 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5330 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5331 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5332 | struct btrfs_root *root, |
| 5333 | u64 num_bytes, u64 min_alloc_size, |
| 5334 | u64 empty_size, u64 hint_byte, |
| 5335 | u64 search_end, struct btrfs_key *ins, |
| 5336 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5337 | { |
| 5338 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5339 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5340 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5341 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5342 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5343 | /* |
| 5344 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5345 | * is not called recursively on allocations |
| 5346 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5347 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5348 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5349 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5350 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5351 | WARN_ON(num_bytes < root->sectorsize); |
| 5352 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5353 | search_start, search_end, hint_byte, |
| 5354 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5355 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5356 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5357 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5358 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5359 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5360 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5361 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5362 | goto again; |
| 5363 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5364 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5365 | struct btrfs_space_info *sinfo; |
| 5366 | |
| 5367 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5368 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5369 | "wanted %llu\n", (unsigned long long)data, |
| 5370 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5371 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5372 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5373 | |
| 5374 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5375 | } |
| 5376 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5377 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5378 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5379 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5380 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5381 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5382 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5383 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5384 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5385 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5386 | return -ENOSPC; |
| 5387 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5388 | |
| 5389 | ret = btrfs_discard_extent(root, start, len); |
| 5390 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5391 | btrfs_add_free_space(cache, start, len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5392 | update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5393 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5394 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5395 | return ret; |
| 5396 | } |
| 5397 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5398 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5399 | struct btrfs_root *root, |
| 5400 | u64 parent, u64 root_objectid, |
| 5401 | u64 flags, u64 owner, u64 offset, |
| 5402 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5403 | { |
| 5404 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5405 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5406 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5407 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5408 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5409 | struct extent_buffer *leaf; |
| 5410 | int type; |
| 5411 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5412 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5413 | if (parent > 0) |
| 5414 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5415 | else |
| 5416 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5417 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5418 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5419 | |
| 5420 | path = btrfs_alloc_path(); |
| 5421 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5422 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5423 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5424 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5425 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5426 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5427 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5428 | leaf = path->nodes[0]; |
| 5429 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5430 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5431 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5432 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5433 | btrfs_set_extent_flags(leaf, extent_item, |
| 5434 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5435 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5436 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5437 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5438 | if (parent > 0) { |
| 5439 | struct btrfs_shared_data_ref *ref; |
| 5440 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5441 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5442 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5443 | } else { |
| 5444 | struct btrfs_extent_data_ref *ref; |
| 5445 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5446 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5447 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5448 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5449 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5450 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5451 | |
| 5452 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5453 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5454 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5455 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5456 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5457 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5458 | "%llu\n", (unsigned long long)ins->objectid, |
| 5459 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5460 | BUG(); |
| 5461 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5462 | return ret; |
| 5463 | } |
| 5464 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5465 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5466 | struct btrfs_root *root, |
| 5467 | u64 parent, u64 root_objectid, |
| 5468 | u64 flags, struct btrfs_disk_key *key, |
| 5469 | int level, struct btrfs_key *ins) |
| 5470 | { |
| 5471 | int ret; |
| 5472 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5473 | struct btrfs_extent_item *extent_item; |
| 5474 | struct btrfs_tree_block_info *block_info; |
| 5475 | struct btrfs_extent_inline_ref *iref; |
| 5476 | struct btrfs_path *path; |
| 5477 | struct extent_buffer *leaf; |
| 5478 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5479 | |
| 5480 | path = btrfs_alloc_path(); |
| 5481 | BUG_ON(!path); |
| 5482 | |
| 5483 | path->leave_spinning = 1; |
| 5484 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5485 | ins, size); |
| 5486 | BUG_ON(ret); |
| 5487 | |
| 5488 | leaf = path->nodes[0]; |
| 5489 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5490 | struct btrfs_extent_item); |
| 5491 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5492 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5493 | btrfs_set_extent_flags(leaf, extent_item, |
| 5494 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5495 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5496 | |
| 5497 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5498 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5499 | |
| 5500 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5501 | if (parent > 0) { |
| 5502 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5503 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5504 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5505 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5506 | } else { |
| 5507 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5508 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5509 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5510 | } |
| 5511 | |
| 5512 | btrfs_mark_buffer_dirty(leaf); |
| 5513 | btrfs_free_path(path); |
| 5514 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5515 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5516 | if (ret) { |
| 5517 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5518 | "%llu\n", (unsigned long long)ins->objectid, |
| 5519 | (unsigned long long)ins->offset); |
| 5520 | BUG(); |
| 5521 | } |
| 5522 | return ret; |
| 5523 | } |
| 5524 | |
| 5525 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5526 | struct btrfs_root *root, |
| 5527 | u64 root_objectid, u64 owner, |
| 5528 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5529 | { |
| 5530 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5531 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5532 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5533 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5534 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5535 | 0, root_objectid, owner, offset, |
| 5536 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5537 | return ret; |
| 5538 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5539 | |
| 5540 | /* |
| 5541 | * this is used by the tree logging recovery code. It records that |
| 5542 | * an extent has been allocated and makes sure to clear the free |
| 5543 | * space cache bits as well |
| 5544 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5545 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5546 | struct btrfs_root *root, |
| 5547 | u64 root_objectid, u64 owner, u64 offset, |
| 5548 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5549 | { |
| 5550 | int ret; |
| 5551 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5552 | struct btrfs_caching_control *caching_ctl; |
| 5553 | u64 start = ins->objectid; |
| 5554 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5555 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5556 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5557 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5558 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5559 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5560 | if (!caching_ctl) { |
| 5561 | BUG_ON(!block_group_cache_done(block_group)); |
| 5562 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5563 | BUG_ON(ret); |
| 5564 | } else { |
| 5565 | mutex_lock(&caching_ctl->mutex); |
| 5566 | |
| 5567 | if (start >= caching_ctl->progress) { |
| 5568 | ret = add_excluded_extent(root, start, num_bytes); |
| 5569 | BUG_ON(ret); |
| 5570 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5571 | ret = btrfs_remove_free_space(block_group, |
| 5572 | start, num_bytes); |
| 5573 | BUG_ON(ret); |
| 5574 | } else { |
| 5575 | num_bytes = caching_ctl->progress - start; |
| 5576 | ret = btrfs_remove_free_space(block_group, |
| 5577 | start, num_bytes); |
| 5578 | BUG_ON(ret); |
| 5579 | |
| 5580 | start = caching_ctl->progress; |
| 5581 | num_bytes = ins->objectid + ins->offset - |
| 5582 | caching_ctl->progress; |
| 5583 | ret = add_excluded_extent(root, start, num_bytes); |
| 5584 | BUG_ON(ret); |
| 5585 | } |
| 5586 | |
| 5587 | mutex_unlock(&caching_ctl->mutex); |
| 5588 | put_caching_control(caching_ctl); |
| 5589 | } |
| 5590 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5591 | ret = update_reserved_bytes(block_group, ins->offset, 1, 1); |
| 5592 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5593 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5594 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5595 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5596 | return ret; |
| 5597 | } |
| 5598 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5599 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5600 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5601 | u64 bytenr, u32 blocksize, |
| 5602 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5603 | { |
| 5604 | struct extent_buffer *buf; |
| 5605 | |
| 5606 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5607 | if (!buf) |
| 5608 | return ERR_PTR(-ENOMEM); |
| 5609 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5610 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5611 | btrfs_tree_lock(buf); |
| 5612 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5613 | |
| 5614 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5615 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5616 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5617 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5618 | /* |
| 5619 | * we allow two log transactions at a time, use different |
| 5620 | * EXENT bit to differentiate dirty pages. |
| 5621 | */ |
| 5622 | if (root->log_transid % 2 == 0) |
| 5623 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5624 | buf->start + buf->len - 1, GFP_NOFS); |
| 5625 | else |
| 5626 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5627 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5628 | } else { |
| 5629 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5630 | buf->start + buf->len - 1, GFP_NOFS); |
| 5631 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5632 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5633 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5634 | return buf; |
| 5635 | } |
| 5636 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5637 | static struct btrfs_block_rsv * |
| 5638 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5639 | struct btrfs_root *root, u32 blocksize) |
| 5640 | { |
| 5641 | struct btrfs_block_rsv *block_rsv; |
| 5642 | int ret; |
| 5643 | |
| 5644 | block_rsv = get_block_rsv(trans, root); |
| 5645 | |
| 5646 | if (block_rsv->size == 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5647 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 5648 | blocksize, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5649 | if (ret) |
| 5650 | return ERR_PTR(ret); |
| 5651 | return block_rsv; |
| 5652 | } |
| 5653 | |
| 5654 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5655 | if (!ret) |
| 5656 | return block_rsv; |
| 5657 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5658 | return ERR_PTR(-ENOSPC); |
| 5659 | } |
| 5660 | |
| 5661 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5662 | { |
| 5663 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5664 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5665 | } |
| 5666 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5667 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5668 | * finds a free extent and does all the dirty work required for allocation |
| 5669 | * returns the key for the extent through ins, and a tree buffer for |
| 5670 | * the first block of the extent through buf. |
| 5671 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5672 | * returns the tree buffer or NULL. |
| 5673 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5674 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5675 | struct btrfs_root *root, u32 blocksize, |
| 5676 | u64 parent, u64 root_objectid, |
| 5677 | struct btrfs_disk_key *key, int level, |
| 5678 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5679 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5680 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5681 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5682 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5683 | u64 flags = 0; |
| 5684 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5685 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5686 | |
| 5687 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5688 | if (IS_ERR(block_rsv)) |
| 5689 | return ERR_CAST(block_rsv); |
| 5690 | |
| 5691 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5692 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5693 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5694 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5695 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5696 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5697 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5698 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5699 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5700 | BUG_ON(IS_ERR(buf)); |
| 5701 | |
| 5702 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5703 | if (parent == 0) |
| 5704 | parent = ins.objectid; |
| 5705 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5706 | } else |
| 5707 | BUG_ON(parent > 0); |
| 5708 | |
| 5709 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5710 | struct btrfs_delayed_extent_op *extent_op; |
| 5711 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5712 | BUG_ON(!extent_op); |
| 5713 | if (key) |
| 5714 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5715 | else |
| 5716 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5717 | extent_op->flags_to_set = flags; |
| 5718 | extent_op->update_key = 1; |
| 5719 | extent_op->update_flags = 1; |
| 5720 | extent_op->is_data = 0; |
| 5721 | |
| 5722 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5723 | ins.offset, parent, root_objectid, |
| 5724 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5725 | extent_op); |
| 5726 | BUG_ON(ret); |
| 5727 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5728 | return buf; |
| 5729 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5730 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5731 | struct walk_control { |
| 5732 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5733 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5734 | struct btrfs_key update_progress; |
| 5735 | int stage; |
| 5736 | int level; |
| 5737 | int shared_level; |
| 5738 | int update_ref; |
| 5739 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5740 | int reada_slot; |
| 5741 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5742 | }; |
| 5743 | |
| 5744 | #define DROP_REFERENCE 1 |
| 5745 | #define UPDATE_BACKREF 2 |
| 5746 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5747 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5748 | struct btrfs_root *root, |
| 5749 | struct walk_control *wc, |
| 5750 | struct btrfs_path *path) |
| 5751 | { |
| 5752 | u64 bytenr; |
| 5753 | u64 generation; |
| 5754 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5755 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5756 | u32 nritems; |
| 5757 | u32 blocksize; |
| 5758 | struct btrfs_key key; |
| 5759 | struct extent_buffer *eb; |
| 5760 | int ret; |
| 5761 | int slot; |
| 5762 | int nread = 0; |
| 5763 | |
| 5764 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5765 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5766 | wc->reada_count = max(wc->reada_count, 2); |
| 5767 | } else { |
| 5768 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5769 | wc->reada_count = min_t(int, wc->reada_count, |
| 5770 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5771 | } |
| 5772 | |
| 5773 | eb = path->nodes[wc->level]; |
| 5774 | nritems = btrfs_header_nritems(eb); |
| 5775 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5776 | |
| 5777 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5778 | if (nread >= wc->reada_count) |
| 5779 | break; |
| 5780 | |
| 5781 | cond_resched(); |
| 5782 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5783 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5784 | |
| 5785 | if (slot == path->slots[wc->level]) |
| 5786 | goto reada; |
| 5787 | |
| 5788 | if (wc->stage == UPDATE_BACKREF && |
| 5789 | generation <= root->root_key.offset) |
| 5790 | continue; |
| 5791 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5792 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5793 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5794 | &refs, &flags); |
| 5795 | BUG_ON(ret); |
| 5796 | BUG_ON(refs == 0); |
| 5797 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5798 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5799 | if (refs == 1) |
| 5800 | goto reada; |
| 5801 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5802 | if (wc->level == 1 && |
| 5803 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5804 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5805 | if (!wc->update_ref || |
| 5806 | generation <= root->root_key.offset) |
| 5807 | continue; |
| 5808 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5809 | ret = btrfs_comp_cpu_keys(&key, |
| 5810 | &wc->update_progress); |
| 5811 | if (ret < 0) |
| 5812 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5813 | } else { |
| 5814 | if (wc->level == 1 && |
| 5815 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5816 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5817 | } |
| 5818 | reada: |
| 5819 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5820 | generation); |
| 5821 | if (ret) |
| 5822 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5823 | nread++; |
| 5824 | } |
| 5825 | wc->reada_slot = slot; |
| 5826 | } |
| 5827 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5828 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5829 | * hepler to process tree block while walking down the tree. |
| 5830 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5831 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5832 | * back refs for pointers in the block. |
| 5833 | * |
| 5834 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5835 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5836 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5837 | struct btrfs_root *root, |
| 5838 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5839 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5840 | { |
| 5841 | int level = wc->level; |
| 5842 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5843 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5844 | int ret; |
| 5845 | |
| 5846 | if (wc->stage == UPDATE_BACKREF && |
| 5847 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5848 | return 1; |
| 5849 | |
| 5850 | /* |
| 5851 | * when reference count of tree block is 1, it won't increase |
| 5852 | * again. once full backref flag is set, we never clear it. |
| 5853 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5854 | if (lookup_info && |
| 5855 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5856 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5857 | BUG_ON(!path->locks[level]); |
| 5858 | ret = btrfs_lookup_extent_info(trans, root, |
| 5859 | eb->start, eb->len, |
| 5860 | &wc->refs[level], |
| 5861 | &wc->flags[level]); |
| 5862 | BUG_ON(ret); |
| 5863 | BUG_ON(wc->refs[level] == 0); |
| 5864 | } |
| 5865 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5866 | if (wc->stage == DROP_REFERENCE) { |
| 5867 | if (wc->refs[level] > 1) |
| 5868 | return 1; |
| 5869 | |
| 5870 | if (path->locks[level] && !wc->keep_locks) { |
| 5871 | btrfs_tree_unlock(eb); |
| 5872 | path->locks[level] = 0; |
| 5873 | } |
| 5874 | return 0; |
| 5875 | } |
| 5876 | |
| 5877 | /* wc->stage == UPDATE_BACKREF */ |
| 5878 | if (!(wc->flags[level] & flag)) { |
| 5879 | BUG_ON(!path->locks[level]); |
| 5880 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5881 | BUG_ON(ret); |
| 5882 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5883 | BUG_ON(ret); |
| 5884 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5885 | eb->len, flag, 0); |
| 5886 | BUG_ON(ret); |
| 5887 | wc->flags[level] |= flag; |
| 5888 | } |
| 5889 | |
| 5890 | /* |
| 5891 | * the block is shared by multiple trees, so it's not good to |
| 5892 | * keep the tree lock |
| 5893 | */ |
| 5894 | if (path->locks[level] && level > 0) { |
| 5895 | btrfs_tree_unlock(eb); |
| 5896 | path->locks[level] = 0; |
| 5897 | } |
| 5898 | return 0; |
| 5899 | } |
| 5900 | |
| 5901 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5902 | * hepler to process tree block pointer. |
| 5903 | * |
| 5904 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5905 | * reference count of the block pointed to. if the block |
| 5906 | * is shared and we need update back refs for the subtree |
| 5907 | * rooted at the block, this function changes wc->stage to |
| 5908 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5909 | * need to update back, this function drops the reference |
| 5910 | * to the block. |
| 5911 | * |
| 5912 | * NOTE: return value 1 means we should stop walking down. |
| 5913 | */ |
| 5914 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5915 | struct btrfs_root *root, |
| 5916 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5917 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5918 | { |
| 5919 | u64 bytenr; |
| 5920 | u64 generation; |
| 5921 | u64 parent; |
| 5922 | u32 blocksize; |
| 5923 | struct btrfs_key key; |
| 5924 | struct extent_buffer *next; |
| 5925 | int level = wc->level; |
| 5926 | int reada = 0; |
| 5927 | int ret = 0; |
| 5928 | |
| 5929 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5930 | path->slots[level]); |
| 5931 | /* |
| 5932 | * if the lower level block was created before the snapshot |
| 5933 | * was created, we know there is no need to update back refs |
| 5934 | * for the subtree |
| 5935 | */ |
| 5936 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5937 | generation <= root->root_key.offset) { |
| 5938 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5939 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5940 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5941 | |
| 5942 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5943 | blocksize = btrfs_level_size(root, level - 1); |
| 5944 | |
| 5945 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5946 | if (!next) { |
| 5947 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5948 | if (!next) |
| 5949 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5950 | reada = 1; |
| 5951 | } |
| 5952 | btrfs_tree_lock(next); |
| 5953 | btrfs_set_lock_blocking(next); |
| 5954 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5955 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5956 | &wc->refs[level - 1], |
| 5957 | &wc->flags[level - 1]); |
| 5958 | BUG_ON(ret); |
| 5959 | BUG_ON(wc->refs[level - 1] == 0); |
| 5960 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5961 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5962 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5963 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5964 | if (level == 1 && |
| 5965 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5966 | goto skip; |
| 5967 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5968 | if (!wc->update_ref || |
| 5969 | generation <= root->root_key.offset) |
| 5970 | goto skip; |
| 5971 | |
| 5972 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5973 | path->slots[level]); |
| 5974 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5975 | if (ret < 0) |
| 5976 | goto skip; |
| 5977 | |
| 5978 | wc->stage = UPDATE_BACKREF; |
| 5979 | wc->shared_level = level - 1; |
| 5980 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5981 | } else { |
| 5982 | if (level == 1 && |
| 5983 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5984 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5988 | btrfs_tree_unlock(next); |
| 5989 | free_extent_buffer(next); |
| 5990 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5991 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | if (!next) { |
| 5995 | if (reada && level == 1) |
| 5996 | reada_walk_down(trans, root, wc, path); |
| 5997 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5998 | btrfs_tree_lock(next); |
| 5999 | btrfs_set_lock_blocking(next); |
| 6000 | } |
| 6001 | |
| 6002 | level--; |
| 6003 | BUG_ON(level != btrfs_header_level(next)); |
| 6004 | path->nodes[level] = next; |
| 6005 | path->slots[level] = 0; |
| 6006 | path->locks[level] = 1; |
| 6007 | wc->level = level; |
| 6008 | if (wc->level == 1) |
| 6009 | wc->reada_slot = 0; |
| 6010 | return 0; |
| 6011 | skip: |
| 6012 | wc->refs[level - 1] = 0; |
| 6013 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6014 | if (wc->stage == DROP_REFERENCE) { |
| 6015 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6016 | parent = path->nodes[level]->start; |
| 6017 | } else { |
| 6018 | BUG_ON(root->root_key.objectid != |
| 6019 | btrfs_header_owner(path->nodes[level])); |
| 6020 | parent = 0; |
| 6021 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6022 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6023 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6024 | root->root_key.objectid, level - 1, 0); |
| 6025 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6026 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6027 | btrfs_tree_unlock(next); |
| 6028 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6029 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6030 | return 1; |
| 6031 | } |
| 6032 | |
| 6033 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6034 | * hepler to process tree block while walking up the tree. |
| 6035 | * |
| 6036 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6037 | * reference count on the block. |
| 6038 | * |
| 6039 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6040 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6041 | * to UPDATE_BACKREF previously while processing the block. |
| 6042 | * |
| 6043 | * NOTE: return value 1 means we should stop walking up. |
| 6044 | */ |
| 6045 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6046 | struct btrfs_root *root, |
| 6047 | struct btrfs_path *path, |
| 6048 | struct walk_control *wc) |
| 6049 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6050 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6051 | int level = wc->level; |
| 6052 | struct extent_buffer *eb = path->nodes[level]; |
| 6053 | u64 parent = 0; |
| 6054 | |
| 6055 | if (wc->stage == UPDATE_BACKREF) { |
| 6056 | BUG_ON(wc->shared_level < level); |
| 6057 | if (level < wc->shared_level) |
| 6058 | goto out; |
| 6059 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6060 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6061 | if (ret > 0) |
| 6062 | wc->update_ref = 0; |
| 6063 | |
| 6064 | wc->stage = DROP_REFERENCE; |
| 6065 | wc->shared_level = -1; |
| 6066 | path->slots[level] = 0; |
| 6067 | |
| 6068 | /* |
| 6069 | * check reference count again if the block isn't locked. |
| 6070 | * we should start walking down the tree again if reference |
| 6071 | * count is one. |
| 6072 | */ |
| 6073 | if (!path->locks[level]) { |
| 6074 | BUG_ON(level == 0); |
| 6075 | btrfs_tree_lock(eb); |
| 6076 | btrfs_set_lock_blocking(eb); |
| 6077 | path->locks[level] = 1; |
| 6078 | |
| 6079 | ret = btrfs_lookup_extent_info(trans, root, |
| 6080 | eb->start, eb->len, |
| 6081 | &wc->refs[level], |
| 6082 | &wc->flags[level]); |
| 6083 | BUG_ON(ret); |
| 6084 | BUG_ON(wc->refs[level] == 0); |
| 6085 | if (wc->refs[level] == 1) { |
| 6086 | btrfs_tree_unlock(eb); |
| 6087 | path->locks[level] = 0; |
| 6088 | return 1; |
| 6089 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6090 | } |
| 6091 | } |
| 6092 | |
| 6093 | /* wc->stage == DROP_REFERENCE */ |
| 6094 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6095 | |
| 6096 | if (wc->refs[level] == 1) { |
| 6097 | if (level == 0) { |
| 6098 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6099 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6100 | else |
| 6101 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6102 | BUG_ON(ret); |
| 6103 | } |
| 6104 | /* make block locked assertion in clean_tree_block happy */ |
| 6105 | if (!path->locks[level] && |
| 6106 | btrfs_header_generation(eb) == trans->transid) { |
| 6107 | btrfs_tree_lock(eb); |
| 6108 | btrfs_set_lock_blocking(eb); |
| 6109 | path->locks[level] = 1; |
| 6110 | } |
| 6111 | clean_tree_block(trans, root, eb); |
| 6112 | } |
| 6113 | |
| 6114 | if (eb == root->node) { |
| 6115 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6116 | parent = eb->start; |
| 6117 | else |
| 6118 | BUG_ON(root->root_key.objectid != |
| 6119 | btrfs_header_owner(eb)); |
| 6120 | } else { |
| 6121 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6122 | parent = path->nodes[level + 1]->start; |
| 6123 | else |
| 6124 | BUG_ON(root->root_key.objectid != |
| 6125 | btrfs_header_owner(path->nodes[level + 1])); |
| 6126 | } |
| 6127 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6128 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6129 | out: |
| 6130 | wc->refs[level] = 0; |
| 6131 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6132 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6133 | } |
| 6134 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6135 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6136 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6137 | struct btrfs_path *path, |
| 6138 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6139 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6140 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6141 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6142 | int ret; |
| 6143 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6144 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6145 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6146 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6147 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6148 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6149 | if (level == 0) |
| 6150 | break; |
| 6151 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6152 | if (path->slots[level] >= |
| 6153 | btrfs_header_nritems(path->nodes[level])) |
| 6154 | break; |
| 6155 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6156 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6157 | if (ret > 0) { |
| 6158 | path->slots[level]++; |
| 6159 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6160 | } else if (ret < 0) |
| 6161 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6162 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6163 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6164 | return 0; |
| 6165 | } |
| 6166 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6167 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6168 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6169 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6170 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6171 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6172 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6173 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6174 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6175 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6176 | while (level < max_level && path->nodes[level]) { |
| 6177 | wc->level = level; |
| 6178 | if (path->slots[level] + 1 < |
| 6179 | btrfs_header_nritems(path->nodes[level])) { |
| 6180 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6181 | return 0; |
| 6182 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6183 | ret = walk_up_proc(trans, root, path, wc); |
| 6184 | if (ret > 0) |
| 6185 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6186 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6187 | if (path->locks[level]) { |
| 6188 | btrfs_tree_unlock(path->nodes[level]); |
| 6189 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6190 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6191 | free_extent_buffer(path->nodes[level]); |
| 6192 | path->nodes[level] = NULL; |
| 6193 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6194 | } |
| 6195 | } |
| 6196 | return 1; |
| 6197 | } |
| 6198 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6199 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6200 | * drop a subvolume tree. |
| 6201 | * |
| 6202 | * this function traverses the tree freeing any blocks that only |
| 6203 | * referenced by the tree. |
| 6204 | * |
| 6205 | * when a shared tree block is found. this function decreases its |
| 6206 | * reference count by one. if update_ref is true, this function |
| 6207 | * also make sure backrefs for the shared block and all lower level |
| 6208 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6209 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6210 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 6211 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6212 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6213 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6214 | struct btrfs_trans_handle *trans; |
| 6215 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6216 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6217 | struct walk_control *wc; |
| 6218 | struct btrfs_key key; |
| 6219 | int err = 0; |
| 6220 | int ret; |
| 6221 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6222 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6223 | path = btrfs_alloc_path(); |
| 6224 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6225 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6226 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6227 | BUG_ON(!wc); |
| 6228 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6229 | trans = btrfs_start_transaction(tree_root, 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6230 | if (block_rsv) |
| 6231 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6232 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6233 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6234 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6235 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6236 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6237 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6238 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6239 | memset(&wc->update_progress, 0, |
| 6240 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6241 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6242 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6243 | memcpy(&wc->update_progress, &key, |
| 6244 | sizeof(wc->update_progress)); |
| 6245 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6246 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6247 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6248 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6249 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6250 | path->lowest_level = 0; |
| 6251 | if (ret < 0) { |
| 6252 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6253 | goto out; |
| 6254 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6255 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6256 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6257 | /* |
| 6258 | * unlock our path, this is safe because only this |
| 6259 | * function is allowed to delete this snapshot |
| 6260 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6261 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6262 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6263 | level = btrfs_header_level(root->node); |
| 6264 | while (1) { |
| 6265 | btrfs_tree_lock(path->nodes[level]); |
| 6266 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6267 | |
| 6268 | ret = btrfs_lookup_extent_info(trans, root, |
| 6269 | path->nodes[level]->start, |
| 6270 | path->nodes[level]->len, |
| 6271 | &wc->refs[level], |
| 6272 | &wc->flags[level]); |
| 6273 | BUG_ON(ret); |
| 6274 | BUG_ON(wc->refs[level] == 0); |
| 6275 | |
| 6276 | if (level == root_item->drop_level) |
| 6277 | break; |
| 6278 | |
| 6279 | btrfs_tree_unlock(path->nodes[level]); |
| 6280 | WARN_ON(wc->refs[level] != 1); |
| 6281 | level--; |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | wc->level = level; |
| 6286 | wc->shared_level = -1; |
| 6287 | wc->stage = DROP_REFERENCE; |
| 6288 | wc->update_ref = update_ref; |
| 6289 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6290 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6291 | |
| 6292 | while (1) { |
| 6293 | ret = walk_down_tree(trans, root, path, wc); |
| 6294 | if (ret < 0) { |
| 6295 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6296 | break; |
| 6297 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6298 | |
| 6299 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6300 | if (ret < 0) { |
| 6301 | err = ret; |
| 6302 | break; |
| 6303 | } |
| 6304 | |
| 6305 | if (ret > 0) { |
| 6306 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6307 | break; |
| 6308 | } |
| 6309 | |
| 6310 | if (wc->stage == DROP_REFERENCE) { |
| 6311 | level = wc->level; |
| 6312 | btrfs_node_key(path->nodes[level], |
| 6313 | &root_item->drop_progress, |
| 6314 | path->slots[level]); |
| 6315 | root_item->drop_level = level; |
| 6316 | } |
| 6317 | |
| 6318 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6319 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6320 | ret = btrfs_update_root(trans, tree_root, |
| 6321 | &root->root_key, |
| 6322 | root_item); |
| 6323 | BUG_ON(ret); |
| 6324 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6325 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6326 | trans = btrfs_start_transaction(tree_root, 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6327 | if (block_rsv) |
| 6328 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6329 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6330 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6331 | btrfs_release_path(root, path); |
| 6332 | BUG_ON(err); |
| 6333 | |
| 6334 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6335 | BUG_ON(ret); |
| 6336 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6337 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6338 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6339 | NULL, NULL); |
| 6340 | BUG_ON(ret < 0); |
| 6341 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6342 | /* if we fail to delete the orphan item this time |
| 6343 | * around, it'll get picked up the next time. |
| 6344 | * |
| 6345 | * The most common failure here is just -ENOENT. |
| 6346 | */ |
| 6347 | btrfs_del_orphan_item(trans, tree_root, |
| 6348 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6349 | } |
| 6350 | } |
| 6351 | |
| 6352 | if (root->in_radix) { |
| 6353 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6354 | } else { |
| 6355 | free_extent_buffer(root->node); |
| 6356 | free_extent_buffer(root->commit_root); |
| 6357 | kfree(root); |
| 6358 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6359 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6360 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6361 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6362 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6363 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6364 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6365 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6366 | /* |
| 6367 | * drop subtree rooted at tree block 'node'. |
| 6368 | * |
| 6369 | * NOTE: this function will unlock and release tree block 'node' |
| 6370 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6371 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6372 | struct btrfs_root *root, |
| 6373 | struct extent_buffer *node, |
| 6374 | struct extent_buffer *parent) |
| 6375 | { |
| 6376 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6377 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6378 | int level; |
| 6379 | int parent_level; |
| 6380 | int ret = 0; |
| 6381 | int wret; |
| 6382 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6383 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6384 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6385 | path = btrfs_alloc_path(); |
| 6386 | BUG_ON(!path); |
| 6387 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6388 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6389 | BUG_ON(!wc); |
| 6390 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6391 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6392 | parent_level = btrfs_header_level(parent); |
| 6393 | extent_buffer_get(parent); |
| 6394 | path->nodes[parent_level] = parent; |
| 6395 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6396 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6397 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6398 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6399 | path->nodes[level] = node; |
| 6400 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6401 | path->locks[level] = 1; |
| 6402 | |
| 6403 | wc->refs[parent_level] = 1; |
| 6404 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6405 | wc->level = level; |
| 6406 | wc->shared_level = -1; |
| 6407 | wc->stage = DROP_REFERENCE; |
| 6408 | wc->update_ref = 0; |
| 6409 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6410 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6411 | |
| 6412 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6413 | wret = walk_down_tree(trans, root, path, wc); |
| 6414 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6415 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6416 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6417 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6418 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6419 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6420 | if (wret < 0) |
| 6421 | ret = wret; |
| 6422 | if (wret != 0) |
| 6423 | break; |
| 6424 | } |
| 6425 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6426 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6427 | btrfs_free_path(path); |
| 6428 | return ret; |
| 6429 | } |
| 6430 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6431 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6432 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 6433 | unsigned long nr) |
| 6434 | { |
| 6435 | return min(last, start + nr - 1); |
| 6436 | } |
| 6437 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6438 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6439 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6440 | { |
| 6441 | u64 page_start; |
| 6442 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6443 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6444 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6445 | unsigned long i; |
| 6446 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6447 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6448 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6449 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6450 | unsigned int total_read = 0; |
| 6451 | unsigned int total_dirty = 0; |
| 6452 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6453 | |
| 6454 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6455 | |
| 6456 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6457 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6458 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 6459 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6460 | /* make sure the dirty trick played by the caller work */ |
| 6461 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6462 | first_index, last_index); |
| 6463 | if (ret) |
| 6464 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6465 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6466 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6467 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6468 | for (i = first_index ; i <= last_index; i++) { |
| 6469 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6470 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6471 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6472 | } |
| 6473 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6474 | again: |
| 6475 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6476 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6477 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6478 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6479 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6480 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6481 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6482 | if (!PageUptodate(page)) { |
| 6483 | btrfs_readpage(NULL, page); |
| 6484 | lock_page(page); |
| 6485 | if (!PageUptodate(page)) { |
| 6486 | unlock_page(page); |
| 6487 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6488 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6489 | goto out_unlock; |
| 6490 | } |
| 6491 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6492 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6493 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6494 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 6495 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6496 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6497 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6498 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6499 | if (ordered) { |
| 6500 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 6501 | unlock_page(page); |
| 6502 | page_cache_release(page); |
| 6503 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6504 | btrfs_put_ordered_extent(ordered); |
| 6505 | goto again; |
| 6506 | } |
| 6507 | set_page_extent_mapped(page); |
| 6508 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6509 | if (i == first_index) |
| 6510 | set_extent_bits(io_tree, page_start, page_end, |
| 6511 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6512 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6513 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6514 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6515 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6516 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6517 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6518 | unlock_page(page); |
| 6519 | page_cache_release(page); |
| 6520 | } |
| 6521 | |
| 6522 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6523 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6524 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6525 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6526 | return ret; |
| 6527 | } |
| 6528 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6529 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6530 | struct btrfs_key *extent_key, |
| 6531 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6532 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6533 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6534 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 6535 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6536 | u64 start = extent_key->objectid - offset; |
| 6537 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6538 | |
| 6539 | em = alloc_extent_map(GFP_NOFS); |
| 6540 | BUG_ON(!em || IS_ERR(em)); |
| 6541 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6542 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6543 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6544 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6545 | em->block_start = extent_key->objectid; |
| 6546 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 6547 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6548 | |
| 6549 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6550 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6551 | while (1) { |
| 6552 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6553 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6554 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6555 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6556 | if (ret != -EEXIST) { |
| 6557 | free_extent_map(em); |
| 6558 | break; |
| 6559 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6560 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6561 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6562 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6563 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6564 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6565 | } |
| 6566 | |
| 6567 | struct btrfs_ref_path { |
| 6568 | u64 extent_start; |
| 6569 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 6570 | u64 root_objectid; |
| 6571 | u64 root_generation; |
| 6572 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6573 | u32 num_refs; |
| 6574 | int lowest_level; |
| 6575 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6576 | int shared_level; |
| 6577 | |
| 6578 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 6579 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6580 | }; |
| 6581 | |
| 6582 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6583 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6584 | u64 disk_bytenr; |
| 6585 | u64 disk_num_bytes; |
| 6586 | u64 offset; |
| 6587 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6588 | u8 compression; |
| 6589 | u8 encryption; |
| 6590 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6591 | }; |
| 6592 | |
| 6593 | static int is_cowonly_root(u64 root_objectid) |
| 6594 | { |
| 6595 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 6596 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 6597 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 6598 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 6599 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6600 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6601 | return 1; |
| 6602 | return 0; |
| 6603 | } |
| 6604 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6605 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6606 | struct btrfs_root *extent_root, |
| 6607 | struct btrfs_ref_path *ref_path, |
| 6608 | int first_time) |
| 6609 | { |
| 6610 | struct extent_buffer *leaf; |
| 6611 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6612 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6613 | struct btrfs_key key; |
| 6614 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6615 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6616 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6617 | int level; |
| 6618 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6619 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6620 | path = btrfs_alloc_path(); |
| 6621 | if (!path) |
| 6622 | return -ENOMEM; |
| 6623 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6624 | if (first_time) { |
| 6625 | ref_path->lowest_level = -1; |
| 6626 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6627 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6628 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6629 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6630 | walk_down: |
| 6631 | level = ref_path->current_level - 1; |
| 6632 | while (level >= -1) { |
| 6633 | u64 parent; |
| 6634 | if (level < ref_path->lowest_level) |
| 6635 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6636 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6637 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6638 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6639 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6640 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6641 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6642 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6643 | parent = ref_path->nodes[level + 1]; |
| 6644 | ref_path->nodes[level + 1] = 0; |
| 6645 | ref_path->current_level = level; |
| 6646 | BUG_ON(parent == 0); |
| 6647 | |
| 6648 | key.objectid = bytenr; |
| 6649 | key.offset = parent + 1; |
| 6650 | key.type = BTRFS_EXTENT_REF_KEY; |
| 6651 | |
| 6652 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6653 | if (ret < 0) |
| 6654 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6655 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6656 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6657 | leaf = path->nodes[0]; |
| 6658 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6659 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6660 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6661 | if (ret < 0) |
| 6662 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6663 | if (ret > 0) |
| 6664 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6665 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6666 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6667 | |
| 6668 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6669 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6670 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 6671 | if (level < ref_path->shared_level) |
| 6672 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6673 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6674 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6675 | next: |
| 6676 | level--; |
| 6677 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6678 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6679 | } |
| 6680 | /* reached lowest level */ |
| 6681 | ret = 1; |
| 6682 | goto out; |
| 6683 | walk_up: |
| 6684 | level = ref_path->current_level; |
| 6685 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 6686 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6687 | |
| 6688 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6689 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6690 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6691 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6692 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6693 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6694 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6695 | key.objectid = bytenr; |
| 6696 | key.offset = 0; |
| 6697 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6698 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6699 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 6700 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6701 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6702 | |
| 6703 | leaf = path->nodes[0]; |
| 6704 | nritems = btrfs_header_nritems(leaf); |
| 6705 | if (path->slots[0] >= nritems) { |
| 6706 | ret = btrfs_next_leaf(extent_root, path); |
| 6707 | if (ret < 0) |
| 6708 | goto out; |
| 6709 | if (ret > 0) { |
| 6710 | /* the extent was freed by someone */ |
| 6711 | if (ref_path->lowest_level == level) |
| 6712 | goto out; |
| 6713 | btrfs_release_path(extent_root, path); |
| 6714 | goto walk_down; |
| 6715 | } |
| 6716 | leaf = path->nodes[0]; |
| 6717 | } |
| 6718 | |
| 6719 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6720 | if (found_key.objectid != bytenr || |
| 6721 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 6722 | /* the extent was freed by someone */ |
| 6723 | if (ref_path->lowest_level == level) { |
| 6724 | ret = 1; |
| 6725 | goto out; |
| 6726 | } |
| 6727 | btrfs_release_path(extent_root, path); |
| 6728 | goto walk_down; |
| 6729 | } |
| 6730 | found: |
| 6731 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 6732 | struct btrfs_extent_ref); |
| 6733 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 6734 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 6735 | if (first_time) { |
| 6736 | level = (int)ref_objectid; |
| 6737 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 6738 | ref_path->lowest_level = level; |
| 6739 | ref_path->current_level = level; |
| 6740 | ref_path->nodes[level] = bytenr; |
| 6741 | } else { |
| 6742 | WARN_ON(ref_objectid != level); |
| 6743 | } |
| 6744 | } else { |
| 6745 | WARN_ON(level != -1); |
| 6746 | } |
| 6747 | first_time = 0; |
| 6748 | |
| 6749 | if (ref_path->lowest_level == level) { |
| 6750 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6751 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 6752 | } |
| 6753 | |
| 6754 | /* |
| 6755 | * the block is tree root or the block isn't in reference |
| 6756 | * counted tree. |
| 6757 | */ |
| 6758 | if (found_key.objectid == found_key.offset || |
| 6759 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 6760 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6761 | ref_path->root_generation = |
| 6762 | btrfs_ref_generation(leaf, ref); |
| 6763 | if (level < 0) { |
| 6764 | /* special reference from the tree log */ |
| 6765 | ref_path->nodes[0] = found_key.offset; |
| 6766 | ref_path->current_level = 0; |
| 6767 | } |
| 6768 | ret = 0; |
| 6769 | goto out; |
| 6770 | } |
| 6771 | |
| 6772 | level++; |
| 6773 | BUG_ON(ref_path->nodes[level] != 0); |
| 6774 | ref_path->nodes[level] = found_key.offset; |
| 6775 | ref_path->current_level = level; |
| 6776 | |
| 6777 | /* |
| 6778 | * the reference was created in the running transaction, |
| 6779 | * no need to continue walking up. |
| 6780 | */ |
| 6781 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6782 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6783 | ref_path->root_generation = |
| 6784 | btrfs_ref_generation(leaf, ref); |
| 6785 | ret = 0; |
| 6786 | goto out; |
| 6787 | } |
| 6788 | |
| 6789 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6790 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6791 | } |
| 6792 | /* reached max tree level, but no tree root found. */ |
| 6793 | BUG(); |
| 6794 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6795 | btrfs_free_path(path); |
| 6796 | return ret; |
| 6797 | } |
| 6798 | |
| 6799 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6800 | struct btrfs_root *extent_root, |
| 6801 | struct btrfs_ref_path *ref_path, |
| 6802 | u64 extent_start) |
| 6803 | { |
| 6804 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6805 | ref_path->extent_start = extent_start; |
| 6806 | |
| 6807 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6808 | } |
| 6809 | |
| 6810 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6811 | struct btrfs_root *extent_root, |
| 6812 | struct btrfs_ref_path *ref_path) |
| 6813 | { |
| 6814 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6815 | } |
| 6816 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6817 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6818 | struct btrfs_key *extent_key, |
| 6819 | u64 offset, int no_fragment, |
| 6820 | struct disk_extent **extents, |
| 6821 | int *nr_extents) |
| 6822 | { |
| 6823 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6824 | struct btrfs_path *path; |
| 6825 | struct btrfs_file_extent_item *fi; |
| 6826 | struct extent_buffer *leaf; |
| 6827 | struct disk_extent *exts = *extents; |
| 6828 | struct btrfs_key found_key; |
| 6829 | u64 cur_pos; |
| 6830 | u64 last_byte; |
| 6831 | u32 nritems; |
| 6832 | int nr = 0; |
| 6833 | int max = *nr_extents; |
| 6834 | int ret; |
| 6835 | |
| 6836 | WARN_ON(!no_fragment && *extents); |
| 6837 | if (!exts) { |
| 6838 | max = 1; |
| 6839 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6840 | if (!exts) |
| 6841 | return -ENOMEM; |
| 6842 | } |
| 6843 | |
| 6844 | path = btrfs_alloc_path(); |
| 6845 | BUG_ON(!path); |
| 6846 | |
| 6847 | cur_pos = extent_key->objectid - offset; |
| 6848 | last_byte = extent_key->objectid + extent_key->offset; |
| 6849 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6850 | cur_pos, 0); |
| 6851 | if (ret < 0) |
| 6852 | goto out; |
| 6853 | if (ret > 0) { |
| 6854 | ret = -ENOENT; |
| 6855 | goto out; |
| 6856 | } |
| 6857 | |
| 6858 | while (1) { |
| 6859 | leaf = path->nodes[0]; |
| 6860 | nritems = btrfs_header_nritems(leaf); |
| 6861 | if (path->slots[0] >= nritems) { |
| 6862 | ret = btrfs_next_leaf(root, path); |
| 6863 | if (ret < 0) |
| 6864 | goto out; |
| 6865 | if (ret > 0) |
| 6866 | break; |
| 6867 | leaf = path->nodes[0]; |
| 6868 | } |
| 6869 | |
| 6870 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6871 | if (found_key.offset != cur_pos || |
| 6872 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6873 | found_key.objectid != reloc_inode->i_ino) |
| 6874 | break; |
| 6875 | |
| 6876 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6877 | struct btrfs_file_extent_item); |
| 6878 | if (btrfs_file_extent_type(leaf, fi) != |
| 6879 | BTRFS_FILE_EXTENT_REG || |
| 6880 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6881 | break; |
| 6882 | |
| 6883 | if (nr == max) { |
| 6884 | struct disk_extent *old = exts; |
| 6885 | max *= 2; |
| 6886 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6887 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6888 | if (old != *extents) |
| 6889 | kfree(old); |
| 6890 | } |
| 6891 | |
| 6892 | exts[nr].disk_bytenr = |
| 6893 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6894 | exts[nr].disk_num_bytes = |
| 6895 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6896 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6897 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6898 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6899 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6900 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6901 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6902 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6903 | BUG_ON(exts[nr].offset > 0); |
| 6904 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6905 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6906 | |
| 6907 | cur_pos += exts[nr].num_bytes; |
| 6908 | nr++; |
| 6909 | |
| 6910 | if (cur_pos + offset >= last_byte) |
| 6911 | break; |
| 6912 | |
| 6913 | if (no_fragment) { |
| 6914 | ret = 1; |
| 6915 | goto out; |
| 6916 | } |
| 6917 | path->slots[0]++; |
| 6918 | } |
| 6919 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6920 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6921 | if (cur_pos + offset < last_byte) { |
| 6922 | ret = -ENOENT; |
| 6923 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6924 | } |
| 6925 | ret = 0; |
| 6926 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6927 | btrfs_free_path(path); |
| 6928 | if (ret) { |
| 6929 | if (exts != *extents) |
| 6930 | kfree(exts); |
| 6931 | } else { |
| 6932 | *extents = exts; |
| 6933 | *nr_extents = nr; |
| 6934 | } |
| 6935 | return ret; |
| 6936 | } |
| 6937 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6938 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6939 | struct btrfs_root *root, |
| 6940 | struct btrfs_path *path, |
| 6941 | struct btrfs_key *extent_key, |
| 6942 | struct btrfs_key *leaf_key, |
| 6943 | struct btrfs_ref_path *ref_path, |
| 6944 | struct disk_extent *new_extents, |
| 6945 | int nr_extents) |
| 6946 | { |
| 6947 | struct extent_buffer *leaf; |
| 6948 | struct btrfs_file_extent_item *fi; |
| 6949 | struct inode *inode = NULL; |
| 6950 | struct btrfs_key key; |
| 6951 | u64 lock_start = 0; |
| 6952 | u64 lock_end = 0; |
| 6953 | u64 num_bytes; |
| 6954 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6955 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6956 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6957 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6958 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6959 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6960 | int ret; |
| 6961 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6962 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6963 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6964 | if (key.objectid < ref_path->owner_objectid || |
| 6965 | (key.objectid == ref_path->owner_objectid && |
| 6966 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6967 | key.objectid = ref_path->owner_objectid; |
| 6968 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6969 | key.offset = 0; |
| 6970 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6971 | } |
| 6972 | |
| 6973 | while (1) { |
| 6974 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6975 | if (ret < 0) |
| 6976 | goto out; |
| 6977 | |
| 6978 | leaf = path->nodes[0]; |
| 6979 | nritems = btrfs_header_nritems(leaf); |
| 6980 | next: |
| 6981 | if (extent_locked && ret > 0) { |
| 6982 | /* |
| 6983 | * the file extent item was modified by someone |
| 6984 | * before the extent got locked. |
| 6985 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6986 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6987 | lock_end, GFP_NOFS); |
| 6988 | extent_locked = 0; |
| 6989 | } |
| 6990 | |
| 6991 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6992 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6993 | break; |
| 6994 | |
| 6995 | BUG_ON(extent_locked); |
| 6996 | ret = btrfs_next_leaf(root, path); |
| 6997 | if (ret < 0) |
| 6998 | goto out; |
| 6999 | if (ret > 0) |
| 7000 | break; |
| 7001 | leaf = path->nodes[0]; |
| 7002 | nritems = btrfs_header_nritems(leaf); |
| 7003 | } |
| 7004 | |
| 7005 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 7006 | |
| 7007 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 7008 | if ((key.objectid > ref_path->owner_objectid) || |
| 7009 | (key.objectid == ref_path->owner_objectid && |
| 7010 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7011 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7012 | break; |
| 7013 | } |
| 7014 | |
| 7015 | if (inode && key.objectid != inode->i_ino) { |
| 7016 | BUG_ON(extent_locked); |
| 7017 | btrfs_release_path(root, path); |
| 7018 | mutex_unlock(&inode->i_mutex); |
| 7019 | iput(inode); |
| 7020 | inode = NULL; |
| 7021 | continue; |
| 7022 | } |
| 7023 | |
| 7024 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7025 | path->slots[0]++; |
| 7026 | ret = 1; |
| 7027 | goto next; |
| 7028 | } |
| 7029 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7030 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7031 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 7032 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 7033 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7034 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 7035 | extent_key->objectid)) { |
| 7036 | path->slots[0]++; |
| 7037 | ret = 1; |
| 7038 | goto next; |
| 7039 | } |
| 7040 | |
| 7041 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7042 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 7043 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7044 | if (search_end == (u64)-1) { |
| 7045 | search_end = key.offset - ext_offset + |
| 7046 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 7047 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7048 | |
| 7049 | if (!extent_locked) { |
| 7050 | lock_start = key.offset; |
| 7051 | lock_end = lock_start + num_bytes - 1; |
| 7052 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 7053 | if (lock_start > key.offset || |
| 7054 | lock_end + 1 < key.offset + num_bytes) { |
| 7055 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 7056 | lock_start, lock_end, GFP_NOFS); |
| 7057 | extent_locked = 0; |
| 7058 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7059 | } |
| 7060 | |
| 7061 | if (!inode) { |
| 7062 | btrfs_release_path(root, path); |
| 7063 | |
| 7064 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 7065 | key.objectid, root); |
| 7066 | if (inode->i_state & I_NEW) { |
| 7067 | BTRFS_I(inode)->root = root; |
| 7068 | BTRFS_I(inode)->location.objectid = |
| 7069 | key.objectid; |
| 7070 | BTRFS_I(inode)->location.type = |
| 7071 | BTRFS_INODE_ITEM_KEY; |
| 7072 | BTRFS_I(inode)->location.offset = 0; |
| 7073 | btrfs_read_locked_inode(inode); |
| 7074 | unlock_new_inode(inode); |
| 7075 | } |
| 7076 | /* |
| 7077 | * some code call btrfs_commit_transaction while |
| 7078 | * holding the i_mutex, so we can't use mutex_lock |
| 7079 | * here. |
| 7080 | */ |
| 7081 | if (is_bad_inode(inode) || |
| 7082 | !mutex_trylock(&inode->i_mutex)) { |
| 7083 | iput(inode); |
| 7084 | inode = NULL; |
| 7085 | key.offset = (u64)-1; |
| 7086 | goto skip; |
| 7087 | } |
| 7088 | } |
| 7089 | |
| 7090 | if (!extent_locked) { |
| 7091 | struct btrfs_ordered_extent *ordered; |
| 7092 | |
| 7093 | btrfs_release_path(root, path); |
| 7094 | |
| 7095 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7096 | lock_end, GFP_NOFS); |
| 7097 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 7098 | lock_end); |
| 7099 | if (ordered && |
| 7100 | ordered->file_offset <= lock_end && |
| 7101 | ordered->file_offset + ordered->len > lock_start) { |
| 7102 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 7103 | lock_start, lock_end, GFP_NOFS); |
| 7104 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7105 | btrfs_put_ordered_extent(ordered); |
| 7106 | key.offset += num_bytes; |
| 7107 | goto skip; |
| 7108 | } |
| 7109 | if (ordered) |
| 7110 | btrfs_put_ordered_extent(ordered); |
| 7111 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7112 | extent_locked = 1; |
| 7113 | continue; |
| 7114 | } |
| 7115 | |
| 7116 | if (nr_extents == 1) { |
| 7117 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7118 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7119 | new_extents[0].disk_bytenr); |
| 7120 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7121 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7122 | btrfs_mark_buffer_dirty(leaf); |
| 7123 | |
| 7124 | btrfs_drop_extent_cache(inode, key.offset, |
| 7125 | key.offset + num_bytes - 1, 0); |
| 7126 | |
| 7127 | ret = btrfs_inc_extent_ref(trans, root, |
| 7128 | new_extents[0].disk_bytenr, |
| 7129 | new_extents[0].disk_num_bytes, |
| 7130 | leaf->start, |
| 7131 | root->root_key.objectid, |
| 7132 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7133 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7134 | BUG_ON(ret); |
| 7135 | |
| 7136 | ret = btrfs_free_extent(trans, root, |
| 7137 | extent_key->objectid, |
| 7138 | extent_key->offset, |
| 7139 | leaf->start, |
| 7140 | btrfs_header_owner(leaf), |
| 7141 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7142 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7143 | BUG_ON(ret); |
| 7144 | |
| 7145 | btrfs_release_path(root, path); |
| 7146 | key.offset += num_bytes; |
| 7147 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7148 | BUG_ON(1); |
| 7149 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7150 | u64 alloc_hint; |
| 7151 | u64 extent_len; |
| 7152 | int i; |
| 7153 | /* |
| 7154 | * drop old extent pointer at first, then insert the |
| 7155 | * new pointers one bye one |
| 7156 | */ |
| 7157 | btrfs_release_path(root, path); |
| 7158 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 7159 | key.offset + num_bytes, |
| 7160 | key.offset, &alloc_hint); |
| 7161 | BUG_ON(ret); |
| 7162 | |
| 7163 | for (i = 0; i < nr_extents; i++) { |
| 7164 | if (ext_offset >= new_extents[i].num_bytes) { |
| 7165 | ext_offset -= new_extents[i].num_bytes; |
| 7166 | continue; |
| 7167 | } |
| 7168 | extent_len = min(new_extents[i].num_bytes - |
| 7169 | ext_offset, num_bytes); |
| 7170 | |
| 7171 | ret = btrfs_insert_empty_item(trans, root, |
| 7172 | path, &key, |
| 7173 | sizeof(*fi)); |
| 7174 | BUG_ON(ret); |
| 7175 | |
| 7176 | leaf = path->nodes[0]; |
| 7177 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7178 | struct btrfs_file_extent_item); |
| 7179 | btrfs_set_file_extent_generation(leaf, fi, |
| 7180 | trans->transid); |
| 7181 | btrfs_set_file_extent_type(leaf, fi, |
| 7182 | BTRFS_FILE_EXTENT_REG); |
| 7183 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7184 | new_extents[i].disk_bytenr); |
| 7185 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7186 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7187 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 7188 | new_extents[i].ram_bytes); |
| 7189 | |
| 7190 | btrfs_set_file_extent_compression(leaf, fi, |
| 7191 | new_extents[i].compression); |
| 7192 | btrfs_set_file_extent_encryption(leaf, fi, |
| 7193 | new_extents[i].encryption); |
| 7194 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 7195 | new_extents[i].other_encoding); |
| 7196 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7197 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 7198 | extent_len); |
| 7199 | ext_offset += new_extents[i].offset; |
| 7200 | btrfs_set_file_extent_offset(leaf, fi, |
| 7201 | ext_offset); |
| 7202 | btrfs_mark_buffer_dirty(leaf); |
| 7203 | |
| 7204 | btrfs_drop_extent_cache(inode, key.offset, |
| 7205 | key.offset + extent_len - 1, 0); |
| 7206 | |
| 7207 | ret = btrfs_inc_extent_ref(trans, root, |
| 7208 | new_extents[i].disk_bytenr, |
| 7209 | new_extents[i].disk_num_bytes, |
| 7210 | leaf->start, |
| 7211 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7212 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7213 | BUG_ON(ret); |
| 7214 | btrfs_release_path(root, path); |
| 7215 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 7216 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7217 | |
| 7218 | ext_offset = 0; |
| 7219 | num_bytes -= extent_len; |
| 7220 | key.offset += extent_len; |
| 7221 | |
| 7222 | if (num_bytes == 0) |
| 7223 | break; |
| 7224 | } |
| 7225 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7226 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7227 | } |
| 7228 | |
| 7229 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7230 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7231 | lock_end, GFP_NOFS); |
| 7232 | extent_locked = 0; |
| 7233 | } |
| 7234 | skip: |
| 7235 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7236 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7237 | break; |
| 7238 | |
| 7239 | cond_resched(); |
| 7240 | } |
| 7241 | ret = 0; |
| 7242 | out: |
| 7243 | btrfs_release_path(root, path); |
| 7244 | if (inode) { |
| 7245 | mutex_unlock(&inode->i_mutex); |
| 7246 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7247 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7248 | lock_end, GFP_NOFS); |
| 7249 | } |
| 7250 | iput(inode); |
| 7251 | } |
| 7252 | return ret; |
| 7253 | } |
| 7254 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7255 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 7256 | struct btrfs_root *root, |
| 7257 | struct extent_buffer *buf, u64 orig_start) |
| 7258 | { |
| 7259 | int level; |
| 7260 | int ret; |
| 7261 | |
| 7262 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 7263 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 7264 | |
| 7265 | level = btrfs_header_level(buf); |
| 7266 | if (level == 0) { |
| 7267 | struct btrfs_leaf_ref *ref; |
| 7268 | struct btrfs_leaf_ref *orig_ref; |
| 7269 | |
| 7270 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 7271 | if (!orig_ref) |
| 7272 | return -ENOENT; |
| 7273 | |
| 7274 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 7275 | if (!ref) { |
| 7276 | btrfs_free_leaf_ref(root, orig_ref); |
| 7277 | return -ENOMEM; |
| 7278 | } |
| 7279 | |
| 7280 | ref->nritems = orig_ref->nritems; |
| 7281 | memcpy(ref->extents, orig_ref->extents, |
| 7282 | sizeof(ref->extents[0]) * ref->nritems); |
| 7283 | |
| 7284 | btrfs_free_leaf_ref(root, orig_ref); |
| 7285 | |
| 7286 | ref->root_gen = trans->transid; |
| 7287 | ref->bytenr = buf->start; |
| 7288 | ref->owner = btrfs_header_owner(buf); |
| 7289 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 7290 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7291 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 7292 | WARN_ON(ret); |
| 7293 | btrfs_free_leaf_ref(root, ref); |
| 7294 | } |
| 7295 | return 0; |
| 7296 | } |
| 7297 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7298 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7299 | struct extent_buffer *leaf, |
| 7300 | struct btrfs_block_group_cache *group, |
| 7301 | struct btrfs_root *target_root) |
| 7302 | { |
| 7303 | struct btrfs_key key; |
| 7304 | struct inode *inode = NULL; |
| 7305 | struct btrfs_file_extent_item *fi; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7306 | struct extent_state *cached_state = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7307 | u64 num_bytes; |
| 7308 | u64 skip_objectid = 0; |
| 7309 | u32 nritems; |
| 7310 | u32 i; |
| 7311 | |
| 7312 | nritems = btrfs_header_nritems(leaf); |
| 7313 | for (i = 0; i < nritems; i++) { |
| 7314 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7315 | if (key.objectid == skip_objectid || |
| 7316 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 7317 | continue; |
| 7318 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7319 | if (btrfs_file_extent_type(leaf, fi) == |
| 7320 | BTRFS_FILE_EXTENT_INLINE) |
| 7321 | continue; |
| 7322 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 7323 | continue; |
| 7324 | if (!inode || inode->i_ino != key.objectid) { |
| 7325 | iput(inode); |
| 7326 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 7327 | key.objectid, target_root, 1); |
| 7328 | } |
| 7329 | if (!inode) { |
| 7330 | skip_objectid = key.objectid; |
| 7331 | continue; |
| 7332 | } |
| 7333 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7334 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7335 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, |
| 7336 | key.offset + num_bytes - 1, 0, &cached_state, |
| 7337 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7338 | btrfs_drop_extent_cache(inode, key.offset, |
| 7339 | key.offset + num_bytes - 1, 1); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7340 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, |
| 7341 | key.offset + num_bytes - 1, &cached_state, |
| 7342 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7343 | cond_resched(); |
| 7344 | } |
| 7345 | iput(inode); |
| 7346 | return 0; |
| 7347 | } |
| 7348 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7349 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7350 | struct btrfs_root *root, |
| 7351 | struct extent_buffer *leaf, |
| 7352 | struct btrfs_block_group_cache *group, |
| 7353 | struct inode *reloc_inode) |
| 7354 | { |
| 7355 | struct btrfs_key key; |
| 7356 | struct btrfs_key extent_key; |
| 7357 | struct btrfs_file_extent_item *fi; |
| 7358 | struct btrfs_leaf_ref *ref; |
| 7359 | struct disk_extent *new_extent; |
| 7360 | u64 bytenr; |
| 7361 | u64 num_bytes; |
| 7362 | u32 nritems; |
| 7363 | u32 i; |
| 7364 | int ext_index; |
| 7365 | int nr_extent; |
| 7366 | int ret; |
| 7367 | |
| 7368 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 7369 | BUG_ON(!new_extent); |
| 7370 | |
| 7371 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 7372 | BUG_ON(!ref); |
| 7373 | |
| 7374 | ext_index = -1; |
| 7375 | nritems = btrfs_header_nritems(leaf); |
| 7376 | for (i = 0; i < nritems; i++) { |
| 7377 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7378 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 7379 | continue; |
| 7380 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7381 | if (btrfs_file_extent_type(leaf, fi) == |
| 7382 | BTRFS_FILE_EXTENT_INLINE) |
| 7383 | continue; |
| 7384 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 7385 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7386 | if (bytenr == 0) |
| 7387 | continue; |
| 7388 | |
| 7389 | ext_index++; |
| 7390 | if (bytenr >= group->key.objectid + group->key.offset || |
| 7391 | bytenr + num_bytes <= group->key.objectid) |
| 7392 | continue; |
| 7393 | |
| 7394 | extent_key.objectid = bytenr; |
| 7395 | extent_key.offset = num_bytes; |
| 7396 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 7397 | nr_extent = 1; |
| 7398 | ret = get_new_locations(reloc_inode, &extent_key, |
| 7399 | group->key.objectid, 1, |
| 7400 | &new_extent, &nr_extent); |
| 7401 | if (ret > 0) |
| 7402 | continue; |
| 7403 | BUG_ON(ret < 0); |
| 7404 | |
| 7405 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 7406 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 7407 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 7408 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 7409 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7410 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7411 | new_extent->disk_bytenr); |
| 7412 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7413 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7414 | btrfs_mark_buffer_dirty(leaf); |
| 7415 | |
| 7416 | ret = btrfs_inc_extent_ref(trans, root, |
| 7417 | new_extent->disk_bytenr, |
| 7418 | new_extent->disk_num_bytes, |
| 7419 | leaf->start, |
| 7420 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7421 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7422 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7423 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7424 | ret = btrfs_free_extent(trans, root, |
| 7425 | bytenr, num_bytes, leaf->start, |
| 7426 | btrfs_header_owner(leaf), |
| 7427 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7428 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7429 | BUG_ON(ret); |
| 7430 | cond_resched(); |
| 7431 | } |
| 7432 | kfree(new_extent); |
| 7433 | BUG_ON(ext_index + 1 != ref->nritems); |
| 7434 | btrfs_free_leaf_ref(root, ref); |
| 7435 | return 0; |
| 7436 | } |
| 7437 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7438 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 7439 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7440 | { |
| 7441 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7442 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7443 | |
| 7444 | if (root->reloc_root) { |
| 7445 | reloc_root = root->reloc_root; |
| 7446 | root->reloc_root = NULL; |
| 7447 | list_add(&reloc_root->dead_list, |
| 7448 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7449 | |
| 7450 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 7451 | reloc_root->node->start); |
| 7452 | btrfs_set_root_level(&root->root_item, |
| 7453 | btrfs_header_level(reloc_root->node)); |
| 7454 | memset(&reloc_root->root_item.drop_progress, 0, |
| 7455 | sizeof(struct btrfs_disk_key)); |
| 7456 | reloc_root->root_item.drop_level = 0; |
| 7457 | |
| 7458 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 7459 | &reloc_root->root_key, |
| 7460 | &reloc_root->root_item); |
| 7461 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7462 | } |
| 7463 | return 0; |
| 7464 | } |
| 7465 | |
| 7466 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 7467 | { |
| 7468 | struct btrfs_trans_handle *trans; |
| 7469 | struct btrfs_root *reloc_root; |
| 7470 | struct btrfs_root *prev_root = NULL; |
| 7471 | struct list_head dead_roots; |
| 7472 | int ret; |
| 7473 | unsigned long nr; |
| 7474 | |
| 7475 | INIT_LIST_HEAD(&dead_roots); |
| 7476 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 7477 | |
| 7478 | while (!list_empty(&dead_roots)) { |
| 7479 | reloc_root = list_entry(dead_roots.prev, |
| 7480 | struct btrfs_root, dead_list); |
| 7481 | list_del_init(&reloc_root->dead_list); |
| 7482 | |
| 7483 | BUG_ON(reloc_root->commit_root != NULL); |
| 7484 | while (1) { |
| 7485 | trans = btrfs_join_transaction(root, 1); |
| 7486 | BUG_ON(!trans); |
| 7487 | |
| 7488 | mutex_lock(&root->fs_info->drop_mutex); |
| 7489 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 7490 | if (ret != -EAGAIN) |
| 7491 | break; |
| 7492 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7493 | |
| 7494 | nr = trans->blocks_used; |
| 7495 | ret = btrfs_end_transaction(trans, root); |
| 7496 | BUG_ON(ret); |
| 7497 | btrfs_btree_balance_dirty(root, nr); |
| 7498 | } |
| 7499 | |
| 7500 | free_extent_buffer(reloc_root->node); |
| 7501 | |
| 7502 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 7503 | &reloc_root->root_key); |
| 7504 | BUG_ON(ret); |
| 7505 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7506 | |
| 7507 | nr = trans->blocks_used; |
| 7508 | ret = btrfs_end_transaction(trans, root); |
| 7509 | BUG_ON(ret); |
| 7510 | btrfs_btree_balance_dirty(root, nr); |
| 7511 | |
| 7512 | kfree(prev_root); |
| 7513 | prev_root = reloc_root; |
| 7514 | } |
| 7515 | if (prev_root) { |
| 7516 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 7517 | kfree(prev_root); |
| 7518 | } |
| 7519 | return 0; |
| 7520 | } |
| 7521 | |
| 7522 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 7523 | { |
| 7524 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 7525 | return 0; |
| 7526 | } |
| 7527 | |
| 7528 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 7529 | { |
| 7530 | struct btrfs_root *reloc_root; |
| 7531 | struct btrfs_trans_handle *trans; |
| 7532 | struct btrfs_key location; |
| 7533 | int found; |
| 7534 | int ret; |
| 7535 | |
| 7536 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7537 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 7538 | BUG_ON(ret); |
| 7539 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 7540 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 7541 | |
| 7542 | if (found) { |
| 7543 | trans = btrfs_start_transaction(root, 1); |
| 7544 | BUG_ON(!trans); |
| 7545 | ret = btrfs_commit_transaction(trans, root); |
| 7546 | BUG_ON(ret); |
| 7547 | } |
| 7548 | |
| 7549 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 7550 | location.offset = (u64)-1; |
| 7551 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 7552 | |
| 7553 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 7554 | BUG_ON(!reloc_root); |
| 7555 | btrfs_orphan_cleanup(reloc_root); |
| 7556 | return 0; |
| 7557 | } |
| 7558 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7559 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7560 | struct btrfs_root *root) |
| 7561 | { |
| 7562 | struct btrfs_root *reloc_root; |
| 7563 | struct extent_buffer *eb; |
| 7564 | struct btrfs_root_item *root_item; |
| 7565 | struct btrfs_key root_key; |
| 7566 | int ret; |
| 7567 | |
| 7568 | BUG_ON(!root->ref_cows); |
| 7569 | if (root->reloc_root) |
| 7570 | return 0; |
| 7571 | |
| 7572 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 7573 | BUG_ON(!root_item); |
| 7574 | |
| 7575 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 7576 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 7577 | BUG_ON(ret); |
| 7578 | |
| 7579 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 7580 | root_key.offset = root->root_key.objectid; |
| 7581 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7582 | |
| 7583 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 7584 | btrfs_set_root_refs(root_item, 0); |
| 7585 | btrfs_set_root_bytenr(root_item, eb->start); |
| 7586 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7587 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7588 | |
| 7589 | btrfs_tree_unlock(eb); |
| 7590 | free_extent_buffer(eb); |
| 7591 | |
| 7592 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 7593 | &root_key, root_item); |
| 7594 | BUG_ON(ret); |
| 7595 | kfree(root_item); |
| 7596 | |
| 7597 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 7598 | &root_key); |
| 7599 | BUG_ON(!reloc_root); |
| 7600 | reloc_root->last_trans = trans->transid; |
| 7601 | reloc_root->commit_root = NULL; |
| 7602 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 7603 | |
| 7604 | root->reloc_root = reloc_root; |
| 7605 | return 0; |
| 7606 | } |
| 7607 | |
| 7608 | /* |
| 7609 | * Core function of space balance. |
| 7610 | * |
| 7611 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7612 | * counted roots. There is one reloc tree for each subvol, and all |
| 7613 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 7614 | * of the latest committed roots of subvols (root->commit_root). |
| 7615 | * |
| 7616 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 7617 | * COW the block through subvol's reloc tree, then update block pointer |
| 7618 | * in the subvol to point to the new block. Since all reloc trees share |
| 7619 | * same root key objectid, doing special handing for tree blocks owned |
| 7620 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 7621 | * we can use the resulting new block directly when the same block is |
| 7622 | * required to COW again through other reloc trees. By this way, relocated |
| 7623 | * tree blocks are shared between reloc trees, so they are also shared |
| 7624 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7625 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7626 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7627 | struct btrfs_root *root, |
| 7628 | struct btrfs_path *path, |
| 7629 | struct btrfs_key *first_key, |
| 7630 | struct btrfs_ref_path *ref_path, |
| 7631 | struct btrfs_block_group_cache *group, |
| 7632 | struct inode *reloc_inode) |
| 7633 | { |
| 7634 | struct btrfs_root *reloc_root; |
| 7635 | struct extent_buffer *eb = NULL; |
| 7636 | struct btrfs_key *keys; |
| 7637 | u64 *nodes; |
| 7638 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7639 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7640 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7641 | int ret; |
| 7642 | |
| 7643 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 7644 | lowest_level = ref_path->owner_objectid; |
| 7645 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7646 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7647 | path->lowest_level = lowest_level; |
| 7648 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 7649 | BUG_ON(ret < 0); |
| 7650 | path->lowest_level = 0; |
| 7651 | btrfs_release_path(root, path); |
| 7652 | return 0; |
| 7653 | } |
| 7654 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7655 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7656 | ret = init_reloc_tree(trans, root); |
| 7657 | BUG_ON(ret); |
| 7658 | reloc_root = root->reloc_root; |
| 7659 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7660 | shared_level = ref_path->shared_level; |
| 7661 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7662 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7663 | keys = ref_path->node_keys; |
| 7664 | nodes = ref_path->new_nodes; |
| 7665 | memset(&keys[shared_level + 1], 0, |
| 7666 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 7667 | memset(&nodes[shared_level + 1], 0, |
| 7668 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7669 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7670 | if (nodes[lowest_level] == 0) { |
| 7671 | path->lowest_level = lowest_level; |
| 7672 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7673 | 0, 1); |
| 7674 | BUG_ON(ret); |
| 7675 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 7676 | eb = path->nodes[level]; |
| 7677 | if (!eb || eb == reloc_root->node) |
| 7678 | break; |
| 7679 | nodes[level] = eb->start; |
| 7680 | if (level == 0) |
| 7681 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 7682 | else |
| 7683 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 7684 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7685 | if (nodes[0] && |
| 7686 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7687 | eb = path->nodes[0]; |
| 7688 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 7689 | group, reloc_inode); |
| 7690 | BUG_ON(ret); |
| 7691 | } |
| 7692 | btrfs_release_path(reloc_root, path); |
| 7693 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7694 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7695 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7696 | BUG_ON(ret); |
| 7697 | } |
| 7698 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7699 | /* |
| 7700 | * replace tree blocks in the fs tree with tree blocks in |
| 7701 | * the reloc tree. |
| 7702 | */ |
| 7703 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 7704 | BUG_ON(ret < 0); |
| 7705 | |
| 7706 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7707 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7708 | 0, 0); |
| 7709 | BUG_ON(ret); |
| 7710 | extent_buffer_get(path->nodes[0]); |
| 7711 | eb = path->nodes[0]; |
| 7712 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7713 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 7714 | BUG_ON(ret); |
| 7715 | free_extent_buffer(eb); |
| 7716 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7717 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7718 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7719 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7720 | return 0; |
| 7721 | } |
| 7722 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7723 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7724 | struct btrfs_root *root, |
| 7725 | struct btrfs_path *path, |
| 7726 | struct btrfs_key *first_key, |
| 7727 | struct btrfs_ref_path *ref_path) |
| 7728 | { |
| 7729 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7730 | |
| 7731 | ret = relocate_one_path(trans, root, path, first_key, |
| 7732 | ref_path, NULL, NULL); |
| 7733 | BUG_ON(ret); |
| 7734 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7735 | return 0; |
| 7736 | } |
| 7737 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7738 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7739 | struct btrfs_root *extent_root, |
| 7740 | struct btrfs_path *path, |
| 7741 | struct btrfs_key *extent_key) |
| 7742 | { |
| 7743 | int ret; |
| 7744 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7745 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 7746 | if (ret) |
| 7747 | goto out; |
| 7748 | ret = btrfs_del_item(trans, extent_root, path); |
| 7749 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7750 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7751 | return ret; |
| 7752 | } |
| 7753 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7754 | 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] | 7755 | struct btrfs_ref_path *ref_path) |
| 7756 | { |
| 7757 | struct btrfs_key root_key; |
| 7758 | |
| 7759 | root_key.objectid = ref_path->root_objectid; |
| 7760 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7761 | if (is_cowonly_root(ref_path->root_objectid)) |
| 7762 | root_key.offset = 0; |
| 7763 | else |
| 7764 | root_key.offset = (u64)-1; |
| 7765 | |
| 7766 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7767 | } |
| 7768 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7769 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7770 | struct btrfs_path *path, |
| 7771 | struct btrfs_key *extent_key, |
| 7772 | struct btrfs_block_group_cache *group, |
| 7773 | struct inode *reloc_inode, int pass) |
| 7774 | { |
| 7775 | struct btrfs_trans_handle *trans; |
| 7776 | struct btrfs_root *found_root; |
| 7777 | struct btrfs_ref_path *ref_path = NULL; |
| 7778 | struct disk_extent *new_extents = NULL; |
| 7779 | int nr_extents = 0; |
| 7780 | int loops; |
| 7781 | int ret; |
| 7782 | int level; |
| 7783 | struct btrfs_key first_key; |
| 7784 | u64 prev_block = 0; |
| 7785 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7786 | |
| 7787 | trans = btrfs_start_transaction(extent_root, 1); |
| 7788 | BUG_ON(!trans); |
| 7789 | |
| 7790 | if (extent_key->objectid == 0) { |
| 7791 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7792 | goto out; |
| 7793 | } |
| 7794 | |
| 7795 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7796 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7797 | ret = -ENOMEM; |
| 7798 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7799 | } |
| 7800 | |
| 7801 | for (loops = 0; ; loops++) { |
| 7802 | if (loops == 0) { |
| 7803 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7804 | extent_key->objectid); |
| 7805 | } else { |
| 7806 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7807 | } |
| 7808 | if (ret < 0) |
| 7809 | goto out; |
| 7810 | if (ret > 0) |
| 7811 | break; |
| 7812 | |
| 7813 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7814 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7815 | continue; |
| 7816 | |
| 7817 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7818 | BUG_ON(!found_root); |
| 7819 | /* |
| 7820 | * for reference counted tree, only process reference paths |
| 7821 | * rooted at the latest committed root. |
| 7822 | */ |
| 7823 | if (found_root->ref_cows && |
| 7824 | ref_path->root_generation != found_root->root_key.offset) |
| 7825 | continue; |
| 7826 | |
| 7827 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7828 | if (pass == 0) { |
| 7829 | /* |
| 7830 | * copy data extents to new locations |
| 7831 | */ |
| 7832 | u64 group_start = group->key.objectid; |
| 7833 | ret = relocate_data_extent(reloc_inode, |
| 7834 | extent_key, |
| 7835 | group_start); |
| 7836 | if (ret < 0) |
| 7837 | goto out; |
| 7838 | break; |
| 7839 | } |
| 7840 | level = 0; |
| 7841 | } else { |
| 7842 | level = ref_path->owner_objectid; |
| 7843 | } |
| 7844 | |
| 7845 | if (prev_block != ref_path->nodes[level]) { |
| 7846 | struct extent_buffer *eb; |
| 7847 | u64 block_start = ref_path->nodes[level]; |
| 7848 | u64 block_size = btrfs_level_size(found_root, level); |
| 7849 | |
| 7850 | eb = read_tree_block(found_root, block_start, |
| 7851 | block_size, 0); |
| 7852 | btrfs_tree_lock(eb); |
| 7853 | BUG_ON(level != btrfs_header_level(eb)); |
| 7854 | |
| 7855 | if (level == 0) |
| 7856 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7857 | else |
| 7858 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7859 | |
| 7860 | btrfs_tree_unlock(eb); |
| 7861 | free_extent_buffer(eb); |
| 7862 | prev_block = block_start; |
| 7863 | } |
| 7864 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7865 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7866 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7867 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7868 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7869 | /* |
| 7870 | * try to update data extent references while |
| 7871 | * keeping metadata shared between snapshots. |
| 7872 | */ |
| 7873 | if (pass == 1) { |
| 7874 | ret = relocate_one_path(trans, found_root, |
| 7875 | path, &first_key, ref_path, |
| 7876 | group, reloc_inode); |
| 7877 | if (ret < 0) |
| 7878 | goto out; |
| 7879 | continue; |
| 7880 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7881 | /* |
| 7882 | * use fallback method to process the remaining |
| 7883 | * references. |
| 7884 | */ |
| 7885 | if (!new_extents) { |
| 7886 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7887 | new_extents = kmalloc(sizeof(*new_extents), |
| 7888 | GFP_NOFS); |
| 7889 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7890 | ret = get_new_locations(reloc_inode, |
| 7891 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7892 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7893 | &new_extents, |
| 7894 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7895 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7896 | goto out; |
| 7897 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7898 | ret = replace_one_extent(trans, found_root, |
| 7899 | path, extent_key, |
| 7900 | &first_key, ref_path, |
| 7901 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7902 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7903 | ret = relocate_tree_block(trans, found_root, path, |
| 7904 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7905 | } |
| 7906 | if (ret < 0) |
| 7907 | goto out; |
| 7908 | } |
| 7909 | ret = 0; |
| 7910 | out: |
| 7911 | btrfs_end_transaction(trans, extent_root); |
| 7912 | kfree(new_extents); |
| 7913 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7914 | return ret; |
| 7915 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7916 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7917 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7918 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7919 | { |
| 7920 | u64 num_devices; |
| 7921 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7922 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7923 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 7924 | /* |
| 7925 | * we add in the count of missing devices because we want |
| 7926 | * to make sure that any RAID levels on a degraded FS |
| 7927 | * continue to be honored. |
| 7928 | */ |
| 7929 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 7930 | root->fs_info->fs_devices->missing_devices; |
| 7931 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7932 | if (num_devices == 1) { |
| 7933 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7934 | stripped = flags & ~stripped; |
| 7935 | |
| 7936 | /* turn raid0 into single device chunks */ |
| 7937 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7938 | return stripped; |
| 7939 | |
| 7940 | /* turn mirroring into duplication */ |
| 7941 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7942 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7943 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7944 | return flags; |
| 7945 | } else { |
| 7946 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7947 | if (flags & stripped) |
| 7948 | return flags; |
| 7949 | |
| 7950 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7951 | stripped = flags & ~stripped; |
| 7952 | |
| 7953 | /* switch duplicated blocks with raid1 */ |
| 7954 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7955 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7956 | |
| 7957 | /* turn single device chunks into raid0 */ |
| 7958 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7959 | } |
| 7960 | return flags; |
| 7961 | } |
| 7962 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7963 | static int set_block_group_ro(struct btrfs_block_group_cache *cache) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7964 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7965 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7966 | u64 num_bytes; |
| 7967 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7968 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7969 | if (cache->ro) |
| 7970 | return 0; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7971 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7972 | spin_lock(&sinfo->lock); |
| 7973 | spin_lock(&cache->lock); |
| 7974 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7975 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7976 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7977 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 7978 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 7979 | cache->reserved_pinned + num_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7980 | sinfo->bytes_readonly += num_bytes; |
| 7981 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 7982 | cache->reserved_pinned = 0; |
| 7983 | cache->ro = 1; |
| 7984 | ret = 0; |
| 7985 | } |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 7986 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7987 | spin_unlock(&cache->lock); |
| 7988 | spin_unlock(&sinfo->lock); |
| 7989 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7990 | } |
| 7991 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7992 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 7993 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7994 | |
| 7995 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7996 | struct btrfs_trans_handle *trans; |
| 7997 | u64 alloc_flags; |
| 7998 | int ret; |
| 7999 | |
| 8000 | BUG_ON(cache->ro); |
| 8001 | |
| 8002 | trans = btrfs_join_transaction(root, 1); |
| 8003 | BUG_ON(IS_ERR(trans)); |
| 8004 | |
| 8005 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 8006 | if (alloc_flags != cache->flags) |
| 8007 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 8008 | |
| 8009 | ret = set_block_group_ro(cache); |
| 8010 | if (!ret) |
| 8011 | goto out; |
| 8012 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
| 8013 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 8014 | if (ret < 0) |
| 8015 | goto out; |
| 8016 | ret = set_block_group_ro(cache); |
| 8017 | out: |
| 8018 | btrfs_end_transaction(trans, root); |
| 8019 | return ret; |
| 8020 | } |
| 8021 | |
| 8022 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 8023 | struct btrfs_block_group_cache *cache) |
| 8024 | { |
| 8025 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8026 | u64 num_bytes; |
| 8027 | |
| 8028 | BUG_ON(!cache->ro); |
| 8029 | |
| 8030 | spin_lock(&sinfo->lock); |
| 8031 | spin_lock(&cache->lock); |
| 8032 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8033 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 8034 | sinfo->bytes_readonly -= num_bytes; |
| 8035 | cache->ro = 0; |
| 8036 | spin_unlock(&cache->lock); |
| 8037 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8038 | return 0; |
| 8039 | } |
| 8040 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8041 | /* |
| 8042 | * checks to see if its even possible to relocate this block group. |
| 8043 | * |
| 8044 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 8045 | * ok to go ahead and try. |
| 8046 | */ |
| 8047 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8048 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8049 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8050 | struct btrfs_space_info *space_info; |
| 8051 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 8052 | struct btrfs_device *device; |
| 8053 | int full = 0; |
| 8054 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8055 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8056 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8057 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8058 | /* odd, couldn't find the block group, leave it alone */ |
| 8059 | if (!block_group) |
| 8060 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8061 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8062 | /* no bytes used, we're good */ |
| 8063 | if (!btrfs_block_group_used(&block_group->item)) |
| 8064 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8065 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8066 | space_info = block_group->space_info; |
| 8067 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8068 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8069 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8070 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8071 | /* |
| 8072 | * 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] | 8073 | * relocate it unless we're able to allocate a new chunk below. |
| 8074 | * |
| 8075 | * Otherwise, we need to make sure we have room in the space to handle |
| 8076 | * 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] | 8077 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8078 | if ((space_info->total_bytes != block_group->key.offset) && |
| 8079 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8080 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 8081 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8082 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8083 | spin_unlock(&space_info->lock); |
| 8084 | goto out; |
| 8085 | } |
| 8086 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8087 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8088 | /* |
| 8089 | * ok we don't have enough space, but maybe we have free space on our |
| 8090 | * devices to allocate new chunks for relocation, so loop through our |
| 8091 | * alloc devices and guess if we have enough space. However, if we |
| 8092 | * were marked as full, then we know there aren't enough chunks, and we |
| 8093 | * can just return. |
| 8094 | */ |
| 8095 | ret = -1; |
| 8096 | if (full) |
| 8097 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 8098 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8099 | mutex_lock(&root->fs_info->chunk_mutex); |
| 8100 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 8101 | u64 min_free = btrfs_block_group_used(&block_group->item); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame^] | 8102 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8103 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8104 | /* |
| 8105 | * check to make sure we can actually find a chunk with enough |
| 8106 | * space to fit our block group in. |
| 8107 | */ |
| 8108 | if (device->total_bytes > device->bytes_used + min_free) { |
| 8109 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame^] | 8110 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8111 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8112 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8113 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8114 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8115 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8116 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8117 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8118 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8119 | return ret; |
| 8120 | } |
| 8121 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 8122 | static int find_first_block_group(struct btrfs_root *root, |
| 8123 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8124 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8125 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8126 | struct btrfs_key found_key; |
| 8127 | struct extent_buffer *leaf; |
| 8128 | int slot; |
| 8129 | |
| 8130 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 8131 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8132 | goto out; |
| 8133 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8134 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8135 | slot = path->slots[0]; |
| 8136 | leaf = path->nodes[0]; |
| 8137 | if (slot >= btrfs_header_nritems(leaf)) { |
| 8138 | ret = btrfs_next_leaf(root, path); |
| 8139 | if (ret == 0) |
| 8140 | continue; |
| 8141 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8142 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8143 | break; |
| 8144 | } |
| 8145 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 8146 | |
| 8147 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8148 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 8149 | ret = 0; |
| 8150 | goto out; |
| 8151 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8152 | path->slots[0]++; |
| 8153 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8154 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8155 | return ret; |
| 8156 | } |
| 8157 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8158 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 8159 | { |
| 8160 | struct btrfs_block_group_cache *block_group; |
| 8161 | u64 last = 0; |
| 8162 | |
| 8163 | while (1) { |
| 8164 | struct inode *inode; |
| 8165 | |
| 8166 | block_group = btrfs_lookup_first_block_group(info, last); |
| 8167 | while (block_group) { |
| 8168 | spin_lock(&block_group->lock); |
| 8169 | if (block_group->iref) |
| 8170 | break; |
| 8171 | spin_unlock(&block_group->lock); |
| 8172 | block_group = next_block_group(info->tree_root, |
| 8173 | block_group); |
| 8174 | } |
| 8175 | if (!block_group) { |
| 8176 | if (last == 0) |
| 8177 | break; |
| 8178 | last = 0; |
| 8179 | continue; |
| 8180 | } |
| 8181 | |
| 8182 | inode = block_group->inode; |
| 8183 | block_group->iref = 0; |
| 8184 | block_group->inode = NULL; |
| 8185 | spin_unlock(&block_group->lock); |
| 8186 | iput(inode); |
| 8187 | last = block_group->key.objectid + block_group->key.offset; |
| 8188 | btrfs_put_block_group(block_group); |
| 8189 | } |
| 8190 | } |
| 8191 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8192 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 8193 | { |
| 8194 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8195 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8196 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8197 | struct rb_node *n; |
| 8198 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8199 | down_write(&info->extent_commit_sem); |
| 8200 | while (!list_empty(&info->caching_block_groups)) { |
| 8201 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 8202 | struct btrfs_caching_control, list); |
| 8203 | list_del(&caching_ctl->list); |
| 8204 | put_caching_control(caching_ctl); |
| 8205 | } |
| 8206 | up_write(&info->extent_commit_sem); |
| 8207 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8208 | spin_lock(&info->block_group_cache_lock); |
| 8209 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 8210 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 8211 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8212 | rb_erase(&block_group->cache_node, |
| 8213 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8214 | spin_unlock(&info->block_group_cache_lock); |
| 8215 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8216 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8217 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8218 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8219 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8220 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8221 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8222 | |
| 8223 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 8224 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8225 | |
| 8226 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8227 | } |
| 8228 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8229 | |
| 8230 | /* now that all the block groups are freed, go through and |
| 8231 | * free all the space_info structs. This is only called during |
| 8232 | * the final stages of unmount, and so we know nobody is |
| 8233 | * using them. We call synchronize_rcu() once before we start, |
| 8234 | * just to be on the safe side. |
| 8235 | */ |
| 8236 | synchronize_rcu(); |
| 8237 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 8238 | release_global_block_rsv(info); |
| 8239 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8240 | while(!list_empty(&info->space_info)) { |
| 8241 | space_info = list_entry(info->space_info.next, |
| 8242 | struct btrfs_space_info, |
| 8243 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8244 | if (space_info->bytes_pinned > 0 || |
| 8245 | space_info->bytes_reserved > 0) { |
| 8246 | WARN_ON(1); |
| 8247 | dump_space_info(space_info, 0, 0); |
| 8248 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8249 | list_del(&space_info->list); |
| 8250 | kfree(space_info); |
| 8251 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8252 | return 0; |
| 8253 | } |
| 8254 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8255 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 8256 | struct btrfs_block_group_cache *cache) |
| 8257 | { |
| 8258 | int index = get_block_group_index(cache); |
| 8259 | |
| 8260 | down_write(&space_info->groups_sem); |
| 8261 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 8262 | up_write(&space_info->groups_sem); |
| 8263 | } |
| 8264 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8265 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 8266 | { |
| 8267 | struct btrfs_path *path; |
| 8268 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8269 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8270 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8271 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8272 | struct btrfs_key key; |
| 8273 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8274 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8275 | int need_clear = 0; |
| 8276 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8277 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8278 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8279 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8280 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8281 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8282 | path = btrfs_alloc_path(); |
| 8283 | if (!path) |
| 8284 | return -ENOMEM; |
| 8285 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8286 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
| 8287 | if (cache_gen != 0 && |
| 8288 | btrfs_super_generation(&root->fs_info->super_copy) != cache_gen) |
| 8289 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 8290 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 8291 | need_clear = 1; |
Josef Bacik | 8216ef8 | 2010-10-28 16:55:47 -0400 | [diff] [blame] | 8292 | if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen) |
| 8293 | printk(KERN_INFO "btrfs: disk space caching is enabled\n"); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8294 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8295 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8296 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8297 | if (ret > 0) |
| 8298 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8299 | if (ret != 0) |
| 8300 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8301 | leaf = path->nodes[0]; |
| 8302 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8303 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8304 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8305 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8306 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8307 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 8308 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8309 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8310 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8311 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8312 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8313 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8314 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8315 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8316 | if (need_clear) |
| 8317 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 8318 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8319 | /* |
| 8320 | * we only want to have 32k of ram per block group for keeping |
| 8321 | * track of free space, and if we pass 1/2 of that we want to |
| 8322 | * start converting things over to using bitmaps |
| 8323 | */ |
| 8324 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8325 | sizeof(struct btrfs_free_space); |
| 8326 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8327 | read_extent_buffer(leaf, &cache->item, |
| 8328 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 8329 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8330 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8331 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8332 | key.objectid = found_key.objectid + found_key.offset; |
| 8333 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8334 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8335 | cache->sectorsize = root->sectorsize; |
| 8336 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8337 | /* |
| 8338 | * check for two cases, either we are full, and therefore |
| 8339 | * don't need to bother with the caching work since we won't |
| 8340 | * find any space, or we are empty, and we can just add all |
| 8341 | * the space in and be done with it. This saves us _alot_ of |
| 8342 | * time, particularly in the full case. |
| 8343 | */ |
| 8344 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8345 | exclude_super_stripes(root, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8346 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8347 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8348 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8349 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8350 | exclude_super_stripes(root, cache); |
| 8351 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8352 | cache->cached = BTRFS_CACHE_FINISHED; |
| 8353 | add_new_free_space(cache, root->fs_info, |
| 8354 | found_key.objectid, |
| 8355 | found_key.objectid + |
| 8356 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8357 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8358 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8359 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8360 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 8361 | btrfs_block_group_used(&cache->item), |
| 8362 | &space_info); |
| 8363 | BUG_ON(ret); |
| 8364 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8365 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8366 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8367 | spin_unlock(&cache->space_info->lock); |
| 8368 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8369 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8370 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8371 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8372 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 8373 | |
| 8374 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 8375 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8376 | set_block_group_ro(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8377 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8378 | |
| 8379 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 8380 | if (!(get_alloc_profile(root, space_info->flags) & |
| 8381 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 8382 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8383 | BTRFS_BLOCK_GROUP_DUP))) |
| 8384 | continue; |
| 8385 | /* |
| 8386 | * avoid allocating from un-mirrored block group if there are |
| 8387 | * mirrored block groups. |
| 8388 | */ |
| 8389 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8390 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8391 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8392 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8393 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8394 | |
| 8395 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8396 | ret = 0; |
| 8397 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8398 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8399 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8400 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8401 | |
| 8402 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8403 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8404 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8405 | u64 size) |
| 8406 | { |
| 8407 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8408 | struct btrfs_root *extent_root; |
| 8409 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8410 | |
| 8411 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8412 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8413 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8414 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8415 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8416 | if (!cache) |
| 8417 | return -ENOMEM; |
| 8418 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8419 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8420 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8421 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8422 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8423 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8424 | |
| 8425 | /* |
| 8426 | * we only want to have 32k of ram per block group for keeping track |
| 8427 | * of free space, and if we pass 1/2 of that we want to start |
| 8428 | * converting things over to using bitmaps |
| 8429 | */ |
| 8430 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8431 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8432 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8433 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8434 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8435 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8436 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8437 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8438 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8439 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 8440 | cache->flags = type; |
| 8441 | btrfs_set_block_group_flags(&cache->item, type); |
| 8442 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8443 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8444 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8445 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8446 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8447 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8448 | chunk_offset + size); |
| 8449 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8450 | free_excluded_extents(root, cache); |
| 8451 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8452 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8453 | &cache->space_info); |
| 8454 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8455 | |
| 8456 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8457 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8458 | spin_unlock(&cache->space_info->lock); |
| 8459 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8460 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8461 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8462 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8463 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8464 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8465 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 8466 | sizeof(cache->item)); |
| 8467 | BUG_ON(ret); |
| 8468 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8469 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8470 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8471 | return 0; |
| 8472 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8473 | |
| 8474 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8475 | struct btrfs_root *root, u64 group_start) |
| 8476 | { |
| 8477 | struct btrfs_path *path; |
| 8478 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8479 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8480 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8481 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8482 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8483 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8484 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8485 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8486 | root = root->fs_info->extent_root; |
| 8487 | |
| 8488 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8489 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8490 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8491 | |
| 8492 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8493 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 8494 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8495 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8496 | factor = 2; |
| 8497 | else |
| 8498 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8499 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8500 | /* make sure this block group isn't part of an allocation cluster */ |
| 8501 | cluster = &root->fs_info->data_alloc_cluster; |
| 8502 | spin_lock(&cluster->refill_lock); |
| 8503 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8504 | spin_unlock(&cluster->refill_lock); |
| 8505 | |
| 8506 | /* |
| 8507 | * make sure this block group isn't part of a metadata |
| 8508 | * allocation cluster |
| 8509 | */ |
| 8510 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8511 | spin_lock(&cluster->refill_lock); |
| 8512 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8513 | spin_unlock(&cluster->refill_lock); |
| 8514 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8515 | path = btrfs_alloc_path(); |
| 8516 | BUG_ON(!path); |
| 8517 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8518 | inode = lookup_free_space_inode(root, block_group, path); |
| 8519 | if (!IS_ERR(inode)) { |
| 8520 | btrfs_orphan_add(trans, inode); |
| 8521 | clear_nlink(inode); |
| 8522 | /* One for the block groups ref */ |
| 8523 | spin_lock(&block_group->lock); |
| 8524 | if (block_group->iref) { |
| 8525 | block_group->iref = 0; |
| 8526 | block_group->inode = NULL; |
| 8527 | spin_unlock(&block_group->lock); |
| 8528 | iput(inode); |
| 8529 | } else { |
| 8530 | spin_unlock(&block_group->lock); |
| 8531 | } |
| 8532 | /* One for our lookup ref */ |
| 8533 | iput(inode); |
| 8534 | } |
| 8535 | |
| 8536 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 8537 | key.offset = block_group->key.objectid; |
| 8538 | key.type = 0; |
| 8539 | |
| 8540 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 8541 | if (ret < 0) |
| 8542 | goto out; |
| 8543 | if (ret > 0) |
| 8544 | btrfs_release_path(tree_root, path); |
| 8545 | if (ret == 0) { |
| 8546 | ret = btrfs_del_item(trans, tree_root, path); |
| 8547 | if (ret) |
| 8548 | goto out; |
| 8549 | btrfs_release_path(tree_root, path); |
| 8550 | } |
| 8551 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8552 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8553 | rb_erase(&block_group->cache_node, |
| 8554 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8555 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8556 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8557 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8558 | /* |
| 8559 | * we must use list_del_init so people can check to see if they |
| 8560 | * are still on the list after taking the semaphore |
| 8561 | */ |
| 8562 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8563 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8564 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8565 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8566 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8567 | |
| 8568 | btrfs_remove_free_space_cache(block_group); |
| 8569 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8570 | spin_lock(&block_group->space_info->lock); |
| 8571 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8572 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8573 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8574 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8575 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8576 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8577 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8578 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8579 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8580 | btrfs_put_block_group(block_group); |
| 8581 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8582 | |
| 8583 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8584 | if (ret > 0) |
| 8585 | ret = -EIO; |
| 8586 | if (ret < 0) |
| 8587 | goto out; |
| 8588 | |
| 8589 | ret = btrfs_del_item(trans, root, path); |
| 8590 | out: |
| 8591 | btrfs_free_path(path); |
| 8592 | return ret; |
| 8593 | } |